29 #ifndef CEREAL_CEREAL_HPP_
30 #define CEREAL_CEREAL_HPP_
32 #include <type_traits>
36 #include <unordered_map>
37 #include <unordered_set>
54 template <
class T>
inline
57 return {name.c_str(), std::forward<T>(
value)};
63 template <
class T>
inline
66 return {name, std::forward<T>(
value)};
72 #define CEREAL_NVP(T) ::cereal::make_nvp(#T, T)
80 template <
class T>
inline
83 return {std::forward<T>(data), size};
95 template <
class T>
inline
98 return {std::forward<T>(sz)};
148 template <
class T>
inline
151 return {std::forward<T>(
value)};
161 template <
class Archive,
class T>
inline
168 template <
class Archive,
class T>
inline
195 #define CEREAL_REGISTER_ARCHIVE(Archive) \
196 namespace cereal { namespace detail { \
197 template <class T, class BindingTag> \
198 typename polymorphic_serialization_support<Archive, T>::type \
199 instantiate_polymorphic_binding( T*, Archive*, BindingTag, adl_tag ); \
203 #if defined(__GNUC__)
205 #define CEREAL_UNUSED_FUNCTION
207 #define CEREAL_UNUSED_FUNCTION static void unused() { (void)version; }
265 #ifdef CEREAL_HAS_CPP17
266 #define CEREAL_CLASS_VERSION(TYPE, VERSION_NUMBER) \
267 namespace cereal { namespace detail { \
268 template <> struct Version<TYPE> \
270 static std::uint32_t registerVersion() \
272 ::cereal::detail::StaticObject<Versions>::getInstance().mapping.emplace( \
273 std::type_index(typeid(TYPE)).hash_code(), VERSION_NUMBER ); \
274 return VERSION_NUMBER; \
276 static inline const std::uint32_t version = registerVersion(); \
277 CEREAL_UNUSED_FUNCTION \
281 #define CEREAL_CLASS_VERSION(TYPE, VERSION_NUMBER) \
282 namespace cereal { namespace detail { \
283 template <> struct Version<TYPE> \
285 static const std::uint32_t version; \
286 static std::uint32_t registerVersion() \
288 ::cereal::detail::StaticObject<Versions>::getInstance().mapping.emplace( \
289 std::type_index(typeid(TYPE)).hash_code(), VERSION_NUMBER ); \
290 return VERSION_NUMBER; \
292 CEREAL_UNUSED_FUNCTION \
294 const std::uint32_t Version<TYPE>::version = \
295 Version<TYPE>::registerVersion(); \
317 template<
class ArchiveType, std::u
int32_t Flags = 0>
330 template <
class ... Types>
inline
333 self->process( std::forward<Types>( args )... );
368 template <
class T>
inline
371 self->process( std::forward<T>( arg ) );
379 template <
class T>
inline
382 self->process( std::forward<T>( arg ) );
400 void const * addr = sharedPointer.get();
403 if(addr == 0)
return 0;
440 template <
class T>
inline
444 self->processImpl( head );
449 template <
class T,
class ... Other>
inline
452 self->process( std::forward<T>( head ) );
453 self->process( std::forward<Other>( tail )... );
458 template <
class T>
inline
472 template <
class T>
inline
481 template <
class T>
inline
484 std::function<void(
void)> deferment( [
this,
d](){
self->process(
d.value ); } );
497 #define PROCESS_IF(name) \
498 traits::EnableIf<traits::has_##name<T, ArchiveType>::value, \
499 !traits::has_invalid_output_versioning<T, ArchiveType>::value, \
500 (traits::is_output_serializable<T, ArchiveType>::value && \
501 (traits::is_specialized_##name<T, ArchiveType>::value || \
502 !traits::is_specialized<T, ArchiveType>::value))> = traits::sfinae
505 template <
class T, PROCESS_IF(member_serialize)>
inline
513 template <
class T, PROCESS_IF(non_member_serialize)>
inline
521 template <
class T, PROCESS_IF(member_save)>
inline
529 template <
class T, PROCESS_IF(non_member_save)>
inline
537 template <
class T, PROCESS_IF(member_save_minimal)>
inline
545 template <
class T, PROCESS_IF(non_member_save_minimal)>
inline
571 "cereal could not find any output serialization functions for the provided type and archive combination. \n\n "
572 "Types must either have a serialize function, load/save pair, or load_minimal/save_minimal pair (you may not mix these). \n "
573 "Serialize functions generally have the following signature: \n\n "
574 "template<class Archive> \n "
575 " void serialize(Archive & ar) \n "
577 " ar( member1, member2, member3 ); \n "
581 "cereal found more than one compatible output serialization function for the provided type and archive combination. \n\n "
582 "Types must either have a serialize function, load/save pair, or load_minimal/save_minimal pair (you may not mix these). \n "
583 "Use specialization (see access.hpp) if you need to disambiguate between serialize vs load/save functions. \n "
584 "Note that serialization functions can be inherited which may lead to the aforementioned ambiguities. \n "
585 "In addition, you may not mix versioned with non-versioned serialization functions. \n\n ");
595 template <
class T>
inline
598 static const auto hash = std::type_index(
typeid(T)).hash_code();
604 if( insertResult.second )
605 process( make_nvp<ArchiveType>(
"cereal_class_version", version) );
612 template <
class T, PROCESS_IF(member_versioned_serialize)>
inline
621 template <
class T, PROCESS_IF(non_member_versioned_serialize)>
inline
630 template <
class T, PROCESS_IF(member_versioned_save)>
inline
639 template <
class T, PROCESS_IF(non_member_versioned_save)>
inline
648 template <
class T, PROCESS_IF(member_versioned_save_minimal)>
inline
657 template <
class T, PROCESS_IF(non_member_versioned_save_minimal)>
inline
667 ArchiveType *
const self;
670 std::unordered_set<traits::detail::base_class_id, traits::detail::base_class_id_hash>
itsBaseClassSet;
709 template<
class ArchiveType, std::u
int32_t Flags = 0>
727 template <
class ... Types>
inline
730 process( std::forward<Types>( args )... );
765 template <
class T>
inline
768 self->process( std::forward<T>( arg ) );
776 template <
class T>
inline
779 self->process( std::forward<T>( arg ) );
795 if(
id == 0)
return std::shared_ptr<void>(
nullptr);
829 throw Exception(
"Error while trying to deserialize a polymorphic pointer. Could not find type id " +
std::to_string(
id));
849 template <
class T>
inline
853 self->processImpl( head );
858 template <
class T,
class ... Other>
inline
861 process( std::forward<T>( head ) );
862 process( std::forward<Other>( tail )... );
867 template <
class T>
inline
881 template <
class T>
inline
890 template <
class T>
inline
893 std::function<void(
void)> deferment( [
this,
d](){
self->process(
d.value ); } );
906 #define PROCESS_IF(name) \
907 traits::EnableIf<traits::has_##name<T, ArchiveType>::value, \
908 !traits::has_invalid_input_versioning<T, ArchiveType>::value, \
909 (traits::is_input_serializable<T, ArchiveType>::value && \
910 (traits::is_specialized_##name<T, ArchiveType>::value || \
911 !traits::is_specialized<T, ArchiveType>::value))> = traits::sfinae
914 template <
class T, PROCESS_IF(member_serialize)>
inline
922 template <
class T, PROCESS_IF(non_member_serialize)>
inline
930 template <
class T, PROCESS_IF(member_load)>
inline
938 template <
class T, PROCESS_IF(non_member_load)>
inline
946 template <
class T, PROCESS_IF(member_load_minimal)>
inline
951 self->process(
value );
957 template <
class T, PROCESS_IF(non_member_load_minimal)>
inline
962 self->process(
value );
986 "cereal could not find any input serialization functions for the provided type and archive combination. \n\n "
987 "Types must either have a serialize function, load/save pair, or load_minimal/save_minimal pair (you may not mix these). \n "
988 "Serialize functions generally have the following signature: \n\n "
989 "template<class Archive> \n "
990 " void serialize(Archive & ar) \n "
992 " ar( member1, member2, member3 ); \n "
996 "cereal found more than one compatible input serialization function for the provided type and archive combination. \n\n "
997 "Types must either have a serialize function, load/save pair, or load_minimal/save_minimal pair (you may not mix these). \n "
998 "Use specialization (see access.hpp) if you need to disambiguate between serialize vs load/save functions. \n "
999 "Note that serialization functions can be inherited which may lead to the aforementioned ambiguities. \n "
1000 "In addition, you may not mix versioned with non-versioned serialization functions. \n\n ");
1013 template <
class T>
inline
1016 static const auto hash = std::type_index(
typeid(T)).hash_code();
1020 return lookupResult->second;
1025 process( make_nvp<ArchiveType>(
"cereal_class_version", version) );
1034 template <
class T, PROCESS_IF(member_versioned_serialize)>
inline
1037 const auto version = loadClassVersion<T>();
1044 template <
class T, PROCESS_IF(non_member_versioned_serialize)>
inline
1047 const auto version = loadClassVersion<T>();
1054 template <
class T, PROCESS_IF(member_versioned_load)>
inline
1057 const auto version = loadClassVersion<T>();
1064 template <
class T, PROCESS_IF(non_member_versioned_load)>
inline
1067 const auto version = loadClassVersion<T>();
1074 template <
class T, PROCESS_IF(member_versioned_load_minimal)>
inline
1078 const auto version = loadClassVersion<T>();
1080 self->process(
value);
1087 template <
class T, PROCESS_IF(non_member_versioned_load_minimal)>
inline
1091 const auto version = loadClassVersion<T>();
1093 self->process(
value);
1101 ArchiveType *
const self;
1104 std::unordered_set<traits::detail::base_class_id, traits::detail::base_class_id_hash>
itsBaseClassSet;
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:25318
Support for base classes (virtual and non-virtual)
A wrapper around data that should be serialized after all non-deferred data.
Definition: helpers.hpp:233
DeferredData< T > defer(T &&value)
Marks data for deferred serialization.
Definition: cereal.hpp:149
For holding name value pairs.
Definition: helpers.hpp:140
NameValuePair< T > make_nvp(const char *name, T &&value)
Creates a name value pair.
Definition: cereal.hpp:64
NameValuePair< T > make_nvp(std::string const &name, T &&value)
Creates a name value pair.
Definition: cereal.hpp:55
The base output archive class.
Definition: cereal.hpp:319
ArchiveType & operator<<(T &&arg)
Serializes passed in data.
Definition: cereal.hpp:380
ArchiveType & processImpl(DeferredData< T > const &d)
Definition: cereal.hpp:482
std::unordered_map< char const *, std::uint32_t > itsPolymorphicTypeMap
Maps from polymorphic type name strings to ids.
Definition: cereal.hpp:683
std::uint32_t registerSharedPointer(const std::shared_ptr< const void > &sharedPointer)
Registers a shared pointer with the archive.
Definition: cereal.hpp:398
OutputArchive & operator=(OutputArchive const &)=delete
std::unordered_set< traits::detail::base_class_id, traits::detail::base_class_id_hash > itsBaseClassSet
A set of all base classes that have been serialized.
Definition: cereal.hpp:670
ArchiveType & processImpl(virtual_base_class< T > const &b)
Serialization of a virtual_base_class wrapper.
Definition: cereal.hpp:459
std::uint32_t itsCurrentPolymorphicTypeId
The id to be given to the next polymorphic type name.
Definition: cereal.hpp:686
ArchiveType *const self
Definition: cereal.hpp:667
std::unordered_set< size_type > itsVersionedTypes
Keeps track of classes that have versioning information associated with them.
Definition: cereal.hpp:689
ArchiveType & operator&(T &&arg)
Serializes passed in data.
Definition: cereal.hpp:369
void process(T &&head)
Serializes data after calling prologue, then calls epilogue.
Definition: cereal.hpp:441
ArchiveType & processImpl(T const &t)
Member serialization.
Definition: cereal.hpp:506
std::unordered_map< void const *, std::uint32_t > itsSharedPointerMap
Maps from addresses to pointer ids.
Definition: cereal.hpp:673
std::vector< std::function< void(void)> > itsDeferments
Definition: cereal.hpp:479
void process(T &&head, Other &&... tail)
Unwinds to process all data.
Definition: cereal.hpp:450
ArchiveType & processImpl(T const &)
Empty class specialization.
Definition: cereal.hpp:556
std::false_type is_loading
Indicates this archive is not intended for loading.
Definition: cereal.hpp:355
OutputArchive(ArchiveType *const derived)
Construct the output archive.
Definition: cereal.hpp:323
std::vector< std::shared_ptr< const void > > itsSharedPointerStorage
Copy of shared pointers used in itsSharedPointerMap to make sure they are kept alive.
Definition: cereal.hpp:677
ArchiveType & processImpl(base_class< T > const &b)
Serialization of a base_class wrapper.
Definition: cereal.hpp:473
ArchiveType & operator()(Types &&... args)
Serializes all passed in data.
Definition: cereal.hpp:331
std::uint32_t registerPolymorphicType(char const *name)
Registers a polymorphic type name with the archive.
Definition: cereal.hpp:425
void serializeDeferments()
Serializes any data marked for deferment using defer.
Definition: cereal.hpp:339
std::true_type is_saving
Indicates this archive is intended for saving.
Definition: cereal.hpp:362
std::uint32_t registerClassVersion()
Registers a class version with the archive and serializes it if necessary.
Definition: cereal.hpp:596
std::uint32_t itsCurrentPointerId
The id to be given to the next pointer.
Definition: cereal.hpp:680
A wrapper around size metadata.
Definition: helpers.hpp:313
SizeTag< T > make_size_tag(T &&sz)
Creates a size tag from some variable.
Definition: cereal.hpp:96
static auto member_load(Archive &ar, T &t) -> decltype(t.CEREAL_LOAD_FUNCTION_NAME(ar))
Definition: access.hpp:257
static auto member_load_minimal(Archive const &ar, T &t, U &&u) -> decltype(t.CEREAL_LOAD_MINIMAL_FUNCTION_NAME(ar, std::forward< U >(u)))
Definition: access.hpp:269
static auto member_save(Archive &ar, T const &t) -> decltype(t.CEREAL_SAVE_FUNCTION_NAME(ar))
Definition: access.hpp:249
static auto member_serialize(Archive &ar, T &t) -> decltype(t.CEREAL_SERIALIZE_FUNCTION_NAME(ar))
Definition: access.hpp:245
static auto member_save_minimal(Archive const &ar, T const &t) -> decltype(t.CEREAL_SAVE_MINIMAL_FUNCTION_NAME(ar))
Definition: access.hpp:261
Definition: helpers.hpp:270
static T & getInstance()
Definition: static_object.hpp:82
static LockGuard lock()
Attempts to lock this static object for the current scope.
Definition: static_object.hpp:110
Support common types - always included automatically.
Internal helper functionality.
Preprocessor macros that can customise the cereal library.
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
static const uint32_t msb_32bit
Definition: helpers.hpp:298
in certain simple scenarios. They should probably not be used if maximizing performance is the main o...
Definition: access.hpp:42
std::enable_if< common_detail::is_enum< T >::value, typename common_detail::is_enum< T >::base_type >::type CEREAL_SAVE_MINIMAL_FUNCTION_NAME(Archive const &, T const &t)
Saving for enum types.
Definition: common.hpp:95
void epilogue(JSONOutputArchive &, NameValuePair< T > const &)
Epilogue for NVPs for JSON archives.
Definition: json.hpp:780
std::enable_if< std::is_arithmetic< T >::value, void >::type CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive &ar, T &t)
Loading for POD types from binary.
Definition: binary.hpp:126
CEREAL_SERIALIZE_FUNCTION_NAME(Archive &ar, NameValuePair< T > &t)
Serializing NVP types to binary.
Definition: binary.hpp:134
std::enable_if< std::is_arithmetic< T >::value, void >::type CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive &ar, T const &t)
Saving for POD types to binary.
Definition: binary.hpp:118
Flags
Special flags for archives.
Definition: cereal.hpp:185
@ AllowEmptyClassElision
Definition: cereal.hpp:185
std::enable_if< common_detail::is_enum< T >::value, void >::type CEREAL_LOAD_MINIMAL_FUNCTION_NAME(Archive const &, T &&t, typename common_detail::is_enum< T >::base_type const &value)
Loading for enum types.
Definition: common.hpp:103
void prologue(JSONOutputArchive &, NameValuePair< T > const &)
Prologue for NVPs for JSON archives.
Definition: json.hpp:768
detail namespace with internal helper functions
Definition: json.hpp:260
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.hpp:6370
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
unsigned int uint32_t
Definition: stdint.h:126
A wrapper around data that can be serialized in a binary fashion.
Definition: helpers.hpp:212
BinaryData< T > binary_data(T &&data, size_t size)
Convenience function to create binary data for both const and non const pointers.
Definition: cereal.hpp:81
An exception class thrown when things go wrong at runtime.
Definition: helpers.hpp:49
Casts a derived class to its non-virtual base class in a way that safely supports abstract classes.
Definition: base_class.hpp:101
Base * base_ptr
Definition: base_class.hpp:110
Definition: traits.hpp:1339
Version information class.
Definition: helpers.hpp:402
Definition: traits.hpp:1146
The number of output serialization functions available.
Definition: traits.hpp:1107
Definition: traits.hpp:1112
Casts a derived class to its virtual base class in a way that allows cereal to track inheritance.
Definition: base_class.hpp:189
Base * base_ptr
Definition: base_class.hpp:198
Internal type trait support.
typename detail::EnableIfHelper< Conditions... >::type EnableIf
Provides a way to enable a function if conditions are met.
Definition: traits.hpp:116
static const detail::sfinae sfinae
Used as the default value for EnableIf and DisableIf template parameters.
Definition: traits.hpp:88