CppSerdes  1.0
A serialization/deserialization library designed with embedded systems in mind
05_nested_object_oriented_serial.cpp

This example demonstrates nesting format definitions in an object oriented manner. Both through inheritance, and through instance nesting.

#include "../include/serdes.h"
#include <stdio.h>
#include "../test/notify_when_dynamic_allocation_used.h"
struct header_type : serdes::packet_base
{
uint8_t id = 9;
uint16_t length = 1;
int8_t source = 2;
void format(serdes::packet &p) override
{
p.add(id);
p.add(length, [&]() noexcept
{ return length < 3; }); // a validation criteria
p.add(source);
p.pad(32);
}
};
// nesting via inheritance
struct obj_with_header_inherited : header_type
{
bool flags[3] = {true, false, true};
double x = -1.0, y = -2.0, z = 3.14;
uint32_t pattern = 0xABCD0123;
void format(serdes::packet &p) final
{
// inherited formats need to be called before or after any surrounding formats like so:
p + serdes::pad(5) + flags + serdes::bitpack(pattern, 23) + serdes::align(8) + x + y + z;
}
};
// nesting via nested instantiation of a obj_with_header_inherited type object
struct compound_type : serdes::packet_base
{
obj_with_header_inherited beginning_data = {};
uint16_t ending_data = 0x1234;
void format(serdes::packet &p) final
{
// since the format is literally nested instead of inherited, it can be added to the format directly
p + beginning_data + ending_data;
}
};
int main()
{
uint8_t reservoir[70] = {};
compound_type object;
auto result = object.store(reservoir);
printf("Bits Stored = %zu with %s\n", result.bits, serdes::status2str(result.status));
// corrupting the data, so that it's obvious when its recovered
object.beginning_data.id = 0;
object.beginning_data.length = 0;
object.beginning_data.source = 0;
object.beginning_data.flags[0] = false;
object.beginning_data.flags[1] = false;
object.beginning_data.flags[2] = false;
object.beginning_data.x = 0.0;
object.beginning_data.y = 0.0;
object.beginning_data.z = 0.0;
object.beginning_data.pattern = 0;
object.ending_data = 0;
result = object.load(reservoir);
printf("Bits Loaded = %zu with %s\n", result.bits, serdes::status2str(result.status));
printf("obj_with_header_inherited = {\n");
printf(" id = %d\n", object.beginning_data.id);
printf(" length = %d\n", object.beginning_data.length);
printf(" source = %d\n", object.beginning_data.source);
printf(" flags[0] = %d\n", object.beginning_data.flags[0]);
printf(" flags[1] = %d\n", object.beginning_data.flags[1]);
printf(" flags[2] = %d\n", object.beginning_data.flags[2]);
printf(" x = %.2f\n", object.beginning_data.x);
printf(" y = %.2f\n", object.beginning_data.y);
printf(" z = %.2f\n", object.beginning_data.z);
printf(" pattern = 0x%08X\n", object.beginning_data.pattern);
printf(" ending_data = 0x%04X\n}", object.ending_data);
}
serdes::status2str
const char * status2str(status_e err_status) noexcept
converts an error status enum to a c style string
Definition: serdes_errors.h:57
serdes::packet::add
CPP_SERDES_LIB_PACKET_API_INLINE1 void add(T &&x, size_t bits)
adds a referenced field to the serial format for both serialization and deserialization
Definition: serdes.h:1011
serdes::bitpack
bitpack a value into an exact number of specified bits. If applied to an array it will be applied to ...
Definition: serdes_format_modifiers.h:54
serdes::packet
a serialization/deserialization helper class, with load, store, and stream operators
Definition: serdes.h:54
serdes::align
specifies a number of bits to be used to align the bit offset as a multiple of
Definition: serdes_format_modifiers.h:18
serdes::packet_base
inheritable base class to allow format recording and application with no additional memory storage (e...
Definition: serdes_fwd_declarations.h:45
serdes::packet::pad
void pad(const size_t bits) noexcept
moves the bit offset head the specified bits
Definition: serdes.h:123
serdes::packet_base::format
virtual void format(packet &)=0
override to declare the serdes process used in store() and load()
serdes::pad
specifies a number of bits to be used to pad (add to) the current bit offset
Definition: serdes_format_modifiers.h:35