CppSerdes  1.0
A serialization/deserialization library designed with embedded systems in mind
07_delimited_arrays.cpp

This example demonstrates a serializing and deserializing a dynamically sized array by looking for a special reserved value marking the end of the array, called a "delimiter". The typical use case is for null terminated strings, though any type/value pair could be used.

#include "../include/serdes.h"
#include <stdio.h>
struct my_delimited_data : serdes::packet_base
{
char data[100] = {};
// defining the format to use for both serialization and deserialization
void format(serdes::packet &p) override
{
p + serdes::delimited_array(data, '\0');
}
};
int main()
{
uint8_t serial_data[50] = "Hello World!";
my_delimited_data object;
auto load_result = object.load(serial_data);
printf("Loaded \"%s\" (%zu bits total) with %s\n",
object.data, load_result.bits, serdes::status2str(load_result.status));
serdes::packet(serial_data) << "now we'll print out this instead";
auto load2_result = object.load(serial_data);
printf("Loaded \"%s\" (%zu bits total) with %s\n",
object.data, load2_result.bits, serdes::status2str(load2_result.status));
serial_data[0] = '\x00'; // corrupt the serial data
// store the object back into the serial data to show the corruption is remmoved
auto store_result = object.store(serial_data);
printf("Stored \"%s\" (%zu bits total) with %s\n",
serial_data, store_result.bits, serdes::status2str(store_result.status));
}
serdes::packet::load
CPP_SERDES_LIB_PACKET_API_INLINE1 void load(T(&value)[N], size_t bits=detail::default_bitsize< T >::value)
[[deserialize]] loads from serial buffer into the passed array of values
Definition: serdes.h:147
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
a serialization/deserialization helper class, with load, store, and stream operators
Definition: serdes.h:54
serdes::delimited_array
a container for dynamically sized arrays with their ending marked by a reserved delimiter value
Definition: serdes_format_modifiers.h:160
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_base::format
virtual void format(packet &)=0
override to declare the serdes process used in store() and load()