serialize.hpp

Tools to save and load data from classes.

All of the important information about a class is stored in a DataPod, which can be used to restore the class at a later time.

Why is this better than other serialization techniques?

  1. Only one line of code is added to a custom class to make it serializable.

  2. Serialized objects do not need a default constructor (a DataPod constructor is added)

  3. Serialized objects can be const since they get rebuilt during construction.

  4. Synergistic interactions with other EMP classes, such as config and tuple_struct

In order to setup a target class to be able to be serialized into a pod, you must add a macro to include the needed functionality. For a basic class, use:

EMP_SETUP_DATAPOD(ClassName, var1, var2, …)

Where ClassName is the target class’ name and var1, var2, etc are the names of the member variables that also need to be stored. Note that member variables can be either built-in types or custom types that have also had DataPods setup in them.

If the target class is a derived class, you must use either:

EMP_SETUP_DATAPOD_D(ClassName, BassClassName, var1, var2, …)

-or-

EMP_SETUP_DATAPOD_D2(ClassName, BassClass1Name, BaseClass2Name, var1, var2, …)

…depending on how many base classes it was derived from (currently max 2).

Note also that this macro must either go in the public section of the target class definition, or the target class must be made a friend to the serialize::DataPod class.

Todo:

Build custom load/store function for more STL objects (especially containers)

To deal with pointers we should recurse, but keep map to new pointer locations.

Setup a more robust method for dealing with arbitrary strings so we don’t have to worry about collisions in streams (JSon format??)

Setup a (compressed) binary save format in DataPods in addition to JSon.

Setup promised synergistic interactions with config and tuple_struct to auto store and load without any additional effort on the part of the library user.

Note

Status: ALPHA

namespace serialize

Functions

template<typename T>
auto StoreVar(DataPod &pod, const T &var, bool) -> typename T::emp_load_return_type&

StoreVar() takes a DataPod and a variable and stores that variable to the pod. The third argument (bool vs. int) will receive a bool, and thus bool versions are preferred in the case of a tie. Specialized versions of this function can be included elsewhere, as needed, and should take a bool as the third argument.

template<typename T>
void StoreVar(DataPod &pod, const vector<T> &var, bool)
template<typename T>
void StoreVar(DataPod &pod, const T &var, int)
template<typename T>
auto SetupLoad(DataPod &pod, T*, bool) -> typename T::emp_load_return_type&
template<typename T>
auto SetupLoad(DataPod &pod, const T*, int) -> T
std::string SetupLoad(DataPod &pod, std::string*, bool)
template<typename T>
vector<T> SetupLoad(DataPod &pod, vector<T>*, bool)
template<typename ...ARG_TYPES>
void Store(DataPod &pod, ARG_TYPES&... args)
class DataPod
#include <serialize.hpp>

A DataPod managed information about another class for serialization.

Public Functions

inline DataPod(std::ostream &_os, std::istream &_is)
inline DataPod(std::iostream &_ios)
inline DataPod(DataPod &&rhs)
inline DataPod &operator=(DataPod &&rhs)
DataPod() = delete
DataPod(const DataPod&) = delete
inline ~DataPod()
inline std::ostream &OStream()
inline std::istream &IStream()

Protected Functions

inline void ClearData()

Protected Attributes

Ptr<std::ostream> os = nullptr
Ptr<std::istream> is = nullptr
bool own_os = false
bool own_is = false