maat::serial::Serializer class

Class that serializes a serializable class into a stream

A 'Serializer' instance is intended to be used only once, then deleted. The serialize() method allows to serialize an object on the disk, so that it can later be reconstructed using the 'Deserializer'.

Objects content can be added to the serialization stream using the '<<' operator. The serializer accepts either objects that inherit from the 'Serializable' class, or raw bits.

'Serializable' objects are serialized automatically using their dump() method. Operator '<<' seemlessly supports smart pointers, containers, std::optional, raw pointers, and maps of 'Serializable' objects.

In order to serialize raw bits (primitive types, buffers, etc), one needs to wrap the variable(s) with one of the following functions available in the serial:: namespace:

  • bits(): for primitive types (int, char, size_t, etc)
  • buffer(): for byte buffers with a known size
  • optional_bits(): for primitive types wrapped in std::optional (e.g optional<bool>)
  • container_bits(): for primitive types wrapper in std container (e.g vector<int>)

❱   Public types

struct IndexEntry
Index entry used to keep track of an object being serialized.

class Stream
Raw stream to write serialized data to.

❱   Constructors, destructors, conversion operators

Serializer(std::ostream& os)

❱   Public functions

void serialize(const Serializable& obj)
Serialize object.

void serialize(const std::shared_ptr<Serializable>& obj)
Serialize object from shared pointer.

auto ptr(const Serializable* obj) -> uid_t

template<typename T>
auto operator<<(Bits<T&> obj) -> Serializer&
Dump primitive type by reference.

template<typename T>
auto operator<<(OptionalBits<T&> obj) -> Serializer&
Dump std::optional primitive type by reference.

template<typename T>
auto operator<<(Buffer<T> obj) -> Serializer&
Dump raw buffer.

auto operator<<(Empty& obj) -> Serializer&
Dump empty object.

auto operator<<(const std::string& str) -> Serializer&
Dump string.

template<typename T>
auto operator<<(ContainerBits<T&> container) -> Serializer&
Dump standard container of primitive type.

template<typename T, template<typename ELEM, typename ALLOC=std::allocator<ELEM>> class C>
auto operator<<(const C<T>& container) -> Serializer&
Dump standard container of non-primitive type.

template<template<typename...> class Map, typename K, typename V>
auto operator<<(const Map<K, V>& map) -> Serializer&
Dump map non-primitive type.

auto operator<<(const std::shared_ptr<Serializable>& s) -> Serializer&
Dump shared_ptr of serializable.

auto operator<<(const Serializable* s) -> Serializer&
Dump ptr of serializable.

auto operator<<(const Serializable& s) -> Serializer&
Dump serializable object.

template<typename T>
auto operator<<(const std::optional<T>& s) -> Serializer&
Dump optional of non-serializable non-primitive type.

❱   Protected functions

auto stream() -> Stream&
Get data stream.

❱   Function documentation

uid_t maat::serial::Serializer::ptr(const Serializable* obj)

Add an object to the serialization queue and return it's object uid. This method is intended to be used from within the Serializable::dump() overloads