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"
{
uint8_t id = 9;
uint16_t length = 1;
int8_t source = 2;
{
p.
add(length, [&]() noexcept
{ return length < 3; });
}
};
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;
{
}
};
{
obj_with_header_inherited beginning_data = {};
uint16_t ending_data = 0x1234;
{
p + beginning_data + ending_data;
}
};
int main()
{
uint8_t reservoir[70] = {};
compound_type object;
auto result = object.store(reservoir);
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("obj_with_header_inherited = {\n");
printf(" id = %u\n", object.beginning_data.id);
printf(" length = %u\n", object.beginning_data.length);
printf(" source = %u\n", object.beginning_data.source);
printf(" flags[0] = %u\n", object.beginning_data.flags[0]);
printf(" flags[1] = %u\n", object.beginning_data.flags[1]);
printf(" flags[2] = %u\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);
}