30 #ifndef CEREAL_TYPES_POLYMORPHIC_HPP_
31 #define CEREAL_TYPES_POLYMORPHIC_HPP_
41 #if defined(_MSC_VER) && _MSC_VER < 1916
42 #define CEREAL_STATIC_CONSTEXPR static
44 #define CEREAL_STATIC_CONSTEXPR static constexpr
82 #define CEREAL_REGISTER_TYPE(...) \
86 struct binding_name<__VA_ARGS__> \
88 CEREAL_STATIC_CONSTEXPR char const * name() { return #__VA_ARGS__; } \
91 CEREAL_BIND_TO_ARCHIVES(__VA_ARGS__)
99 #define CEREAL_REGISTER_TYPE_WITH_NAME(T, Name) \
103 struct binding_name<T> \
104 { CEREAL_STATIC_CONSTEXPR char const * name() { return Name; } }; \
106 CEREAL_BIND_TO_ARCHIVES(T)
121 #define CEREAL_REGISTER_POLYMORPHIC_RELATION(Base, Derived) \
125 struct PolymorphicRelation<Base, Derived> \
126 { static void bind() { RegisterPolymorphicCaster<Base, Derived>::bind(); } }; \
155 #define CEREAL_REGISTER_DYNAMIC_INIT(LibName) \
158 void CEREAL_DLL_EXPORT dynamic_init_dummy_##LibName() {} \
168 #define CEREAL_FORCE_DYNAMIC_INIT(LibName) \
171 void CEREAL_DLL_EXPORT dynamic_init_dummy_##LibName(); \
175 struct dynamic_init_##LibName { \
176 dynamic_init_##LibName() { \
177 ::cereal::detail::dynamic_init_dummy_##LibName(); \
179 } dynamic_init_instance_##LibName; \
184 namespace polymorphic_detail
188 #define UNREGISTERED_POLYMORPHIC_EXCEPTION(LoadSave, Name) \
189 throw cereal::Exception("Trying to " #LoadSave " an unregistered polymorphic type (" + Name + ").\n" \
190 "Make sure your type is registered with CEREAL_REGISTER_TYPE and that the archive " \
191 "you are using was included (and registered with CEREAL_REGISTER_ARCHIVE) prior to calling CEREAL_REGISTER_TYPE.\n" \
192 "If your type is already registered and you still see this error, you may need to use CEREAL_REGISTER_DYNAMIC_INIT.");
196 template<
class Archive>
inline
202 typename ::cereal::detail::InputBindingMap<Archive>::Serializers emptySerializers;
203 emptySerializers.shared_ptr = [](
void*, std::shared_ptr<void> & ptr, std::type_info
const &) { ptr.reset(); };
204 emptySerializers.unique_ptr = [](
void*, std::unique_ptr<void, ::cereal::detail::EmptyDeleter<void>> & ptr, std::type_info
const &) { ptr.reset(
nullptr ); };
205 return emptySerializers;
212 ar.registerPolymorphicName(nameid, name);
215 name = ar.getPolymorphicName(nameid);
219 auto binding = bindingMap.find(name);
220 if(binding == bindingMap.end())
222 return binding->second;
232 template<
class Archive,
class T>
inline
250 template<
class Archive,
class T,
class D>
inline
270 template<
class Archive,
class T>
inline
277 throw cereal::Exception(
"Cannot load a polymorphic type that is not default constructable and does not have a load_and_construct function");
287 template<
class Archive,
class T,
class D>
inline
294 throw cereal::Exception(
"Cannot load a polymorphic type that is not default constructable and does not have a load_and_construct function");
303 template <
class Archive,
class T>
inline
314 std::type_info
const & ptrinfo =
typeid(*ptr.get());
315 static std::type_info
const & tinfo =
typeid(T);
322 auto binding = bindingMap.find(std::type_index(ptrinfo));
323 if(binding == bindingMap.end())
326 binding->second.shared_ptr(&ar, ptr.get(), tinfo);
330 template <
class Archive,
class T>
inline
341 std::type_info
const & ptrinfo =
typeid(*ptr.get());
342 static std::type_info
const & tinfo =
typeid(T);
357 auto binding = bindingMap.find(std::type_index(ptrinfo));
358 if(binding == bindingMap.end())
361 binding->second.shared_ptr(&ar, ptr.get(), tinfo);
365 template <
class Archive,
class T>
inline
377 std::shared_ptr<void> result;
378 binding.shared_ptr(&ar, result,
typeid(T));
379 ptr = std::static_pointer_cast<T>(result);
383 template <
class Archive,
class T>
inline
387 auto const sptr = ptr.lock();
392 template <
class Archive,
class T>
inline
396 std::shared_ptr<T> sptr;
402 template <
class Archive,
class T,
class D>
inline
413 std::type_info
const & ptrinfo =
typeid(*ptr.get());
414 static std::type_info
const & tinfo =
typeid(T);
421 auto binding = bindingMap.find(std::type_index(ptrinfo));
422 if(binding == bindingMap.end())
425 binding->second.unique_ptr(&ar, ptr.get(), tinfo);
429 template <
class Archive,
class T,
class D>
inline
440 std::type_info
const & ptrinfo =
typeid(*ptr.get());
441 static std::type_info
const & tinfo =
typeid(T);
456 auto binding = bindingMap.find(std::type_index(ptrinfo));
457 if(binding == bindingMap.end())
460 binding->second.unique_ptr(&ar, ptr.get(), tinfo);
464 template <
class Archive,
class T,
class D>
inline
476 std::unique_ptr<void, ::cereal::detail::EmptyDeleter<void>> result;
477 binding.unique_ptr(&ar, result,
typeid(T));
478 ptr.reset(
static_cast<T*
>(result.release()));
481 #undef UNREGISTERED_POLYMORPHIC_EXCEPTION
Main cereal functionality.
A static, pre-execution object.
Definition: static_object.hpp:68
Internal helper functionality.
#define CEREAL_NVP_(name, value)
Convenience for creating a templated NVP.
Definition: helpers.hpp:201
Support for types found in <memory>
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
static const uint32_t msb_32bit
Definition: helpers.hpp:298
static const int32_t msb2_32bit
Definition: helpers.hpp:299
PtrWrapper< T > make_ptr_wrapper(T &&t)
Make a PtrWrapper.
Definition: memory.hpp:57
typename ::cereal::detail::InputBindingMap< Archive >::Serializers getInputBinding(Archive &ar, std::uint32_t const nameid)
Get an input binding from the given archive by deserializing the type meta data.
Definition: polymorphic.hpp:197
std::enable_if<(traits::is_default_constructible< T >::value||traits::has_load_and_construct< T, Archive >::value) &&!std::is_abstract< T >::value, bool >::type serialize_wrapper(Archive &ar, std::shared_ptr< T > &ptr, std::uint32_t const nameid)
Serialize a shared_ptr if the 2nd msb in the nameid is set, and if we can actually construct the poin...
Definition: polymorphic.hpp:236
void save(Archive &ar, SetT const &set)
Definition: set.hpp:42
void load(Archive &ar, SetT &set)
Definition: set.hpp:52
std::string demangle(std::string mangledName)
Demangles the type encoded in a string.
Definition: util.hpp:62
in certain simple scenarios. They should probably not be used if maximizing performance is the main o...
Definition: access.hpp:42
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
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
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
#define UNREGISTERED_POLYMORPHIC_EXCEPTION(LoadSave, Name)
Error message used for unregistered polymorphic types.
Definition: polymorphic.hpp:188
Internal polymorphism support.
unsigned int uint32_t
Definition: stdint.h:126
An exception class thrown when things go wrong at runtime.
Definition: helpers.hpp:49
Non member load and construct check.
Definition: traits.hpp:942
Determines whether the class T can be default constructed by cereal::access.
Definition: traits.hpp:1255
Internal type trait support.