SerialPod.hpp

Tools to save and load data from classes.

This file is part of Empirical, https://github.com/devosoft/Empirical Copyright (C) 2023-2024 Michigan State University MIT Software license; see doc/LICENSE.md

The SerialPod object knows whether it is loading or saving and object, and provides a simple mechanism to do so.

=> How to use: A standard class can use a SerialPod by adding a Serialize(SerialPod & pod) member function.

An object of an independent class MyClass with a fixed interface can have a stand-alone Serialize function in the form of Serialize(SerialPod & pod, const MyClass & obj).

Any class can have a constructor that takes a SerialPod to allow reconstruction of const objects. As long as such a constructor is provided, the Serialize function can be const.

More complex classes (e.g., those that do memory management) will need to have separate SerialSave(SerialPod & pod) const and SerialLoad(SerialPod & pod) functions.

=> Example: Inside of MyClass we might have:

void MyClass(SerialPod & pod) { pod(member_a, member_b, member_c); // List all members to save/load }

=> Resolving duplications: If more than one version of a serialization function exists, functions external to the class always have precedence over those internal (so that classes can later be changed how they serialize).

If both a specific function (e.g., Load) and general function (i.e, Serialize) exist, the more specific one will always be used.

Regular streaming is used only when no other options exist.

Note

Status: ALPHA

Defines

INCLUDE_EMP_SERIALIZE_SERIAL_POD_HPP_GUARD

Functions

template<typename T>
void Serialize(SerialPod &pod, std::vector<T> &vec)
template<typename T>
void SerialLoad(SerialPod &pod, std::vector<T> &vec)
template<typename T>
void SerialSave(SerialPod &pod, const std::vector<T> &vec)

Variables

template<typename OBJ_T> concept hasSerializeMember   = requires(OBJ_T & value, SerialPod& pod) {{value.Serialize(pod) };}

Concept to identify id a type has a Serialize() member function.

template<typename OBJ_T> concept hasSerialLoadMember   = requires(OBJ_T & value, SerialPod& pod) {{value.SerialLoad(pod) };}

Concept to identify id a type has a SerialLoad() member function.

template<typename OBJ_T> concept hasSerialSaveMember   = requires(OBJ_T & value, SerialPod& pod) {{value.SerialSave(pod) };}

Concept to identify id a type has a SerialSave() member function.

template<typename OBJ_T> concept hasSerializeOverload   = requires(OBJ_T & value, SerialPod& pod) {{Serialize(pod, value) };}

Concept to identify id a type has a stand-alone Serialize() overload.

template<typename OBJ_T> concept hasSerialLoadOverload   = requires(OBJ_T & value, SerialPod& pod) {{SerialLoad(pod, value) };}

Concept to identify id a type has a stand-alone SerialLoad() overload.

template<typename OBJ_T> concept hasSerialSaveOverload   = requires(OBJ_T & value, SerialPod& pod) {{SerialSave(pod, value) };}

Concept to identify id a type has a stand-alone SerialSave() overload.

template<typename OBJ_T> concept hasSerializeConstructor   = requires(SerialPod& pod) {{ OBJ_T(pod) };}

Concept to identify id a type has a de-serialization constructor.

class SerialPod
#include <SerialPod.hpp>

A SerialPod manages information about other classes for serialization.

Public Functions

inline SerialPod(std::ostream &os)
inline SerialPod(std::istream &is)
inline SerialPod(std::iostream &ios, bool is_save)
inline SerialPod(SerialPod &&rhs)

Move constructor.

inline SerialPod &operator=(SerialPod &&rhs)

Move operator.

SerialPod() = delete
SerialPod(const SerialPod&) = delete
inline ~SerialPod()
inline bool IsLoad() const
inline bool IsSave() const
template<typename T>
inline T LoadValue()
inline SerialPod &Load()
inline SerialPod &Save()
template<typename T, typename ...EXTRA_Ts>
inline SerialPod &Load(T &in, EXTRA_Ts&... extras)
template<typename T, typename ...EXTRA_Ts>
inline SerialPod &Save(T &&in, EXTRA_Ts&&... extras)
template<typename T, typename ...EXTRA_Ts>
inline SerialPod &operator()(T &&in, EXTRA_Ts&&... extras)

Private Types

using is_ptr_t = Ptr<std::istream>
using os_ptr_t = Ptr<std::ostream>

Private Functions

inline std::istream &IStream()
inline std::ostream &OStream()
inline void ClearData()

Private Members

std::variant<is_ptr_t, os_ptr_t> stream_ptr = static_cast<is_ptr_t>(nullptr)
bool own_ptr = false