Hide some decoder implementation details from public header files
This commit is contained in:
parent
11636af323
commit
228854a31d
3 changed files with 156 additions and 214 deletions
|
@ -21,7 +21,7 @@ struct SomeStruct
|
|||
std::size_t Encode(const SomeStruct &value, std::span<std::uint8_t> buffer)
|
||||
{
|
||||
CBOR::BasicEncoder enc(buffer);
|
||||
enc.BeginIndefiniteMap();
|
||||
enc.BeginMap(7);
|
||||
|
||||
enc.Encode("name");
|
||||
enc.Encode(value.name);
|
||||
|
@ -48,12 +48,10 @@ std::size_t Encode(const SomeStruct &value, std::span<std::uint8_t> buffer)
|
|||
}
|
||||
enc.End();
|
||||
|
||||
enc.End();
|
||||
|
||||
return enc.EncodedSize();
|
||||
}
|
||||
|
||||
SomeStruct Decode(std::span<std::uint8_t> buffer)
|
||||
SomeStruct Decode1(std::span<std::uint8_t> buffer)
|
||||
{
|
||||
SomeStruct result;
|
||||
|
||||
|
@ -92,6 +90,50 @@ SomeStruct Decode(std::span<std::uint8_t> buffer)
|
|||
return result;
|
||||
}
|
||||
|
||||
SomeStruct Decode2(std::span<std::uint8_t> buffer)
|
||||
{
|
||||
SomeStruct result;
|
||||
|
||||
CBOR::Decoder dec(buffer);
|
||||
CBOR::Map object = dec.Map();
|
||||
while (!object.Done()) {
|
||||
CBOR::KeyValue kv = object.Next();
|
||||
std::string_view key = kv.Key().String().Get();
|
||||
CBOR::Item value = kv.Value();
|
||||
if (key == "name") {
|
||||
result.name = value.String().Get();
|
||||
}
|
||||
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().String();
|
||||
tool.AllowIndefinite();
|
||||
while (!tool.Done()) {
|
||||
result.tools.back().append(tool.Next());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Compare(const SomeStruct &s1, const SomeStruct &s2)
|
||||
{
|
||||
if (s1.name != s2.name) {
|
||||
|
@ -145,9 +187,11 @@ int main()
|
|||
std::size_t encodedSize = Encode(expected, buffer);
|
||||
std::println("Encoded size: {}", encodedSize);
|
||||
|
||||
SomeStruct result = Decode(std::span<std::uint8_t>(buffer.data(), encodedSize));
|
||||
SomeStruct result1 = Decode1(std::span<std::uint8_t>(buffer.data(), encodedSize));
|
||||
SomeStruct result2 = Decode2(std::span<std::uint8_t>(buffer.data(), encodedSize));
|
||||
|
||||
Compare(expected, result);
|
||||
Compare(expected, result1);
|
||||
Compare(expected, result2);
|
||||
std::println("The test has been completed successfully.");
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue