Serialization Library¶
- group serialization_library
Serialization support using the
cereallibrary.Provides serialization functions for
TensorandShapeand convenience utilities.Note the following guidelines for serialization:
By default you should use save/load pairs and explicit versioning. The provided macros encourage this usage.
Saving an object must not mutate it.
save()beingconsthelps, but be careful aboutVariablewhich is ashared_ptrto non-const.Loading an object must provide the basic exception guarantee. After an exception, the object must be safe to destroy and no leaks can occur.
For simplicity,
load()may assume that the initial state of the object is default-constructed. Conversely, one must only callload()on a default-constructed object.Avoid serializing
long,size_t, and Flashlight’sDimsince these types have platform-dependent sizes. Fixed-size types likeint64_tare always fine.int,long longshould be fine on virtually all platforms.
Defines
-
FL_SAVE_LOAD(...)¶
Convenience macro for specifying a simple serialization method for Cereal.
Serializes the specified arguments, usually class members. Should be placed in the
privatesection of a class. For polymorphic classes, useFL_SAVE_LOAD_WITH_BASEinstead.Supports the common case when one adds fields to a class, which should be conditionally loaded for newer file versions. See
fl::versioned().
-
FL_SAVE_LOAD_WITH_BASE(Base, ...)¶
Like
FL_SAVE_LOAD, but also serializes the base class, which must be specified as the first argument.NB: You do not need to use
CEREAL_REGISTER_POLYMORPHIC_RELATIONif you are using this macro. However you will still needCEREAL_REGISTER_TYPE.
-
FL_SAVE_LOAD_DECLARE()¶
Declaration-only.
Intended to reduce clutter in class definitions. This macro should be placed in the
privatesection of a class. The method must be defined later (outside the class). Do not use this macro if you want your class to be unversioned.
Functions
-
template<typename ...Args>
void save(const fs::path &filepath, const Args&... args)¶ Save (serialize) the specified args to a binary file (via Cereal).
- Parameters:
filepath – the file path to save to
args – the objects to save (e.g. shared_ptr to Module)
-
template<typename ...Args>
void save(std::ostream &ostr, const Args&... args)¶ Save (serialize) the specified args to a binary file (via Cereal).
- Parameters:
ostr – output stream
args – the objects to save (e.g. shared_ptr to Module)
-
template<typename ...Args>
void load(const fs::path &filepath, Args&... args)¶ Load (deserialize) the specified args from a binary file (via Cereal).
- Parameters:
filepath – the file path to load from
args – the objects to load (expects default-constructed)
-
template<typename ...Args>
void load(std::istream &istr, Args&... args)¶ Load (deserialize) the specified args from a binary file (via Cereal).
- Parameters:
istr – input stream
args – the objects to load (expects default-constructed)
-
template<typename T>
detail::Versioned<T> versioned(T &&t, uint32_t minVersion, uint32_t maxVersion = UINT32_MAX)¶ Serialize an expression iff the version is in the given range (inclusive).
Only intended to wrap arguments of
FL_SAVE_LOAD*macros.Example: if we have field
xin version 0, and add fieldyin version 1, we might write:FL_SAVE_LOAD(x, fl::versioned(y, 1)).
-
template<typename S, typename T>
detail::SerializeAs<S, T> serializeAs(T &&t)¶ Serialize an object of type T as another type S using static_cast on-the-fly.
Only intended to wrap arguments of
FL_SAVE_LOAD*macros.Example:
FL_SAVE_LOAD(fl::serializeAs<double>(x))
-
template<typename S, typename T, typename SaveConvFn, typename LoadConvFn>
detail::SerializeAs<S, T> serializeAs(T &&t, SaveConvFn saveConverter, LoadConvFn loadConverter)¶ Serialize an object of type T as another type S using the provided conversion functions on-the-fly.
Only intended to wrap arguments of
FL_SAVE_LOAD*macros.Note: Technically, T will be a reference type; let T0 be the decayed type.
Example: please see tests/common/SerializationTest.cpp
- Parameters:
t – object to be serialized
saveConverter – callable with signature S(const T0&)
loadConverter – callable with signature T0(S)