Turi Create
4.0
|
The two key serialization objects are oarchive and iarchive (in src/turicreate/serialization/oarchive.hpp
and src/turicreate/serialization/iarchive.hpp
) respectively. oarchive internally wraps either a direct char*
buffer which it resizes automatically, or a ostream
object. Similarly, iarchive internally wraps either a direct char*
buffer with a known length, or an istream
object. All these members are directly accessible.
The oarchive overloads operator<< and iarchive overloads operator>> to allow the use of the stream operator to serialize / deserialize objects.
Using the oarchive as an example: this is done by a function:
which immediately re-dispatches into the following template class:
Where IsPodType is true if T is a scalar or inherits from turi::IS_POD_TYPE (see is_pod.hpp) and false otherwise. The serialize_impl class is then further specialized around T to provide serializers for basic types (int, float, etc) , and common STL types (pair, string, vector, etc) using recursive calls if necessary. The base template class (when none of the specializations match), simply calls t.save(oarc). This thus implements the user-defined serializers for save and load functions.
The templatization over OutArcType allows for alternate definitions of oarchive which behave differently. For instance, turi::oarchive_soft_fail which will only fail at runtime if an object type cannot be serialized. This is done via a use of SFINAE in has_save.hpp
and has_load.hpp
. To permit generic implementations of serialize_impl which are independent of OutArcType, the oarchive/iarchive must implement the following functions: