Implement a higher-level API on top of the basics

This commit is contained in:
TennesseeTrash 2025-09-27 03:53:07 +02:00
parent e06b85632a
commit 2e9b507837
7 changed files with 491 additions and 174 deletions

View file

@ -1,11 +1,14 @@
#include "CBOR/Decoder.hpp"
#include "CBOR/Encoder.hpp"
#include "CBOR/Printer.hpp"
#include <CBOR/Core.hpp>
#include <CBOR/Decoder.hpp>
#include <CBOR/DecoderHooks.hpp> // IWYU pragma: keep
#include <CBOR/Encoder.hpp>
#include <CBOR/Printer.hpp>
#include <array>
#include <compare> // IWYU pragma: keep
#include <cstdint>
#include <iostream>
#include <print>
#include <ranges>
#include <stdexcept>
#include <vector>
@ -18,166 +21,47 @@ struct SomeStruct
std::int64_t slots;
std::uint32_t times;
std::vector<std::string> tools;
constexpr auto operator<=>(const SomeStruct &) const = default;
};
std::size_t Encode(const SomeStruct &value, auto &buffer)
void EncodeHook(CBOR::Encoder &enc, const SomeStruct &value)
{
CBOR::BasicEncoder enc(buffer);
enc.EncodeTag(15'000);
enc.BeginMap(7);
enc.Encode("name");
enc.Encode(value.name);
enc.Encode("speed");
enc.Encode(value.speed);
enc.Encode("fov");
enc.Encode(value.fov);
enc.Encode("thing");
enc.Encode(value.thing);
enc.Encode("slots");
enc.Encode(value.slots);
enc.Encode("times");
enc.Encode(value.times);
enc.Encode("tools");
enc.BeginIndefiniteArray();
for (std::string_view tool: value.tools) {
enc.Encode(tool);
}
enc.End();
return enc.Size();
enc.EncodeTaggedMap(
15'000,
"name", value.name,
"speed", value.speed,
"fov", value.fov,
"thing", value.thing,
"slots", value.slots,
"times", value.times,
"tools", value.tools
);
}
SomeStruct Decode1(std::span<std::uint8_t> buffer)
void DecodeHook(CBOR::Decoder &dec, SomeStruct &value)
{
SomeStruct result;
CBOR::Decoder dec(buffer);
CBOR::StructHelper helper {
CBOR::StructMember { "name", &SomeStruct::name },
CBOR::StructMember { "speed", &SomeStruct::speed },
CBOR::StructMember { "fov", &SomeStruct::fov },
CBOR::StructMember { "thing", &SomeStruct::thing },
CBOR::StructMember { "slots", &SomeStruct::slots },
CBOR::StructMember { "times", &SomeStruct::times },
CBOR::StructMember { "tools", &SomeStruct::tools },
};
CBOR::TaggedItem tagged = dec.TaggedItem();
if (tagged.Tag() != 15'000) {
throw std::runtime_error("test error: could not extract object tag");
}
CBOR::Map object = tagged.Item().Map();
while (!object.Done()) {
CBOR::KeyValue kv = object.Next();
std::string_view key = kv.Key().String();
CBOR::Item value = kv.Value();
if (key == "name") {
result.name = value.String();
}
else if (key == "speed") {
result.speed = value.Double();
}
else if (key == "fov") {
result.fov = value.Float();
}
else if (key == "thing") {
result.thing = value.Int8();
}
else if (key == "slots") {
result.slots = value.Int64();
}
else if (key == "times") {
result.times = value.Uint32();
}
else if (key == "tools") {
CBOR::Array tools = value.Array();
while(!tools.Done()) {
result.tools.push_back(std::string(tools.Next().String()));
}
}
}
return result;
}
SomeStruct Decode2(std::span<std::uint8_t> buffer)
{
SomeStruct result;
CBOR::Decoder dec(buffer);
CBOR::TaggedItem tagged = dec.TaggedItem();
if (tagged.Tag() != 15'000) {
throw std::runtime_error("test error: could not extract object tag");
}
CBOR::Map object = tagged.Item().Map();
while (!object.Done()) {
CBOR::KeyValue kv = object.Next();
std::string_view key = kv.Key().String();
CBOR::Item value = kv.Value();
if (key == "name") {
result.name = value.String();
}
else if (key == "speed") {
result.speed = value.Double();
}
else if (key == "fov") {
result.fov = value.Float();
}
else if (key == "thing") {
result.thing = value.Int8();
}
else if (key == "slots") {
result.slots = value.Int64();
}
else if (key == "times") {
result.times = value.Uint32();
}
else if (key == "tools") {
CBOR::Array tools = value.Array();
while(!tools.Done()) {
result.tools.push_back("");
CBOR::String tool = tools.Next().IndefiniteString();
while (!tool.Done()) {
result.tools.back().append(tool.Next());
}
}
}
}
return result;
}
void Compare(const SomeStruct &s1, const SomeStruct &s2)
{
if (s1.name != s2.name) {
throw std::runtime_error("test error: names are not the same");
}
if (s1.speed != s2.speed) {
throw std::runtime_error("test error: speed is not the same");
}
if (s1.fov != s2.fov) {
throw std::runtime_error("test error: fovs are not the same");
}
if (s1.thing != s2.thing) {
throw std::runtime_error("test error: things are not the same");
}
if (s1.slots != s2.slots) {
throw std::runtime_error("test error: slots are not the same");
}
if (s1.times != s2.times) {
throw std::runtime_error("test error: times are not the same");
}
for (const auto &[t1, t2]: std::ranges::views::zip(s1.tools, s2.tools)) {
if (t1 != t2) {
throw std::runtime_error("test error: some tools are not the same");
}
throw std::runtime_error("Expected item with tag value 15000");
}
helper.Decode(tagged.Item(), value);
}
int main()
{
using namespace std::string_view_literals;
//std::array<std::uint8_t, 1024> buffer {};
std::vector<std::uint8_t> buffer;
std::array<std::uint8_t, 1024> buffer {};
SomeStruct expected {
.name = "Player1",
@ -196,23 +80,20 @@ int main()
};
try {
std::size_t encodedSize = Encode(expected, buffer);
std::println("Encoded size: {}", encodedSize);
CBOR::Encoder enc(buffer);
enc.Encode(expected);
std::print("Encoded hex: ");
for (std::size_t i = 0; i < encodedSize; ++i) {
std::print("{:02X} ", buffer[i]);
CBOR::ConstBuffer encoded(buffer.data(), enc.Size());
CBOR::Decoder dec(encoded);
SomeStruct res = dec.Decode<SomeStruct>();
if (res != expected) {
throw std::runtime_error("test error: the encode/decode round trip should be lossless");
}
std::println();
SomeStruct result1 = Decode1(std::span<std::uint8_t>(buffer.data(), encodedSize));
SomeStruct result2 = Decode2(std::span<std::uint8_t>(buffer.data(), encodedSize));
CBOR::Print(std::cout, encoded);
std::println("JSON-esque serialization:");
CBOR::Print(std::cout, std::span<std::uint8_t>(buffer.data(), encodedSize));
Compare(expected, result1);
Compare(expected, result2);
std::println("The test has been completed successfully.");
}
catch (const std::exception &e) {