CppSerdes  1.0
A serialization/deserialization library designed with embedded systems in mind
03_serial_usage.cpp

These examples demonstrate using the serdes::packet class with stream operators as well as method operators, along with several format customization techniques such as modifying padding, alignment, and and bit size (bitpacking)

#include "../include/serdes.h"
#include "../include/bitprint.h"
int main()
{
constexpr size_t buffer_size = sizeof(int) * 3 / sizeof(uint32_t);
// serializing and deserializing using the << >> stream operators
{
uint32_t buffer[buffer_size] = {};
serdes::packet buffer_stream(buffer);
// store some values into the buffer
buffer_stream << 1 << 5 << 9;
// display the stored serial data
printf("stored data: ");
// recover the values from the buffer
int x, y, z;
buffer_stream >> x >> y >> z;
printf("recovered data: {x = %i, y = %i, z = %i}\n\n", x, y, z);
}
// serializing and deserializing using the store/load/pad/align methods for bitpacking
{
uint32_t buffer[buffer_size] = {};
serdes::packet buffer_stream(buffer);
// store some values into the buffer
buffer_stream.store(-5);
buffer_stream.pad(1); // adding 1 padding bit
buffer_stream.store(-2, 3); // bitpacking into 3 bits
buffer_stream.align(32); // adding pad bits to allign to 32bit boundaries
buffer_stream.store(-123);
// display the stored serial data
printf("stored data: ");
// recover the values from the buffer
int x, y, z;
buffer_stream.load(x);
buffer_stream.pad(1); // adding 1 padding bit
buffer_stream.load(y, 3); // bitpacking into 3 bits
buffer_stream.align(32); // adding pad bits to allign to 32bit boundaries
buffer_stream.load(z);
printf("recovered data: {x = %i, y = %i, z = %i}\n\n", x, y, z);
}
// serializing and deserializing using the "<<" ">>" operators (additionally showing off how bitpacking can be applied to arrays)
{
uint32_t buffer[buffer_size] = {};
serdes::packet buffer_stream(buffer);
uint64_t flags[8] = {1, 0, 1, 0, 1, 0, 1, 1};
// store some values into the buffer
buffer_stream << -5 << serdes::pad(1) << serdes::bitpack(-2, 3) << serdes::align(16) << -123 << serdes::bitpack(flags, 1);
// display the stored serial data
printf("stored data: ");
// recover the values from the buffer
int x, y, z;
buffer_stream >> x >> serdes::pad(1) >> serdes::bitpack(y, 3) >> serdes::align(16) >> z >> serdes::bitpack(flags, 1);
printf("recovered data: {x = %i, y = %i, z = %i}\n\n", x, y, z);
}
}
serdes::packet::align
void align(size_t bits) noexcept
aligns (increases) the bit offset to a multiple of the specified bits
Definition: serdes.h:131
serdes::printhex
void printhex(const uint8_t data, const bool add_newline=true)
prints uint8_t value in hex format
Definition: bitprint.h:81
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::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::pad
void pad(const size_t bits) noexcept
moves the bit offset head the specified bits
Definition: serdes.h:123
serdes::packet::store
CPP_SERDES_LIB_PACKET_API_INLINE1 void store(const T &value, size_t bits=detail::default_bitsize< T >::value)
[[serialize]] stores a value reference into a serial buffer
Definition: serdes.h:578
serdes::pad
specifies a number of bits to be used to pad (add to) the current bit offset
Definition: serdes_format_modifiers.h:35