1#ifndef SERIALIZE_META_HH
2#define SERIALIZE_META_HH
31 template<
typename TUPLE>
33 auto makeT = []<
typename... Args>(Args&& ...args) {
34 return std::make_unique<T>(std::forward<Args>(args)...);
36 return std::apply(makeT, tuple);
47template<
typename T>
struct ClassSaver;
50template<
typename T>
struct NonPolymorphicPointerLoader;
52template<
typename T>
struct ClassLoader;
78 static_assert(std::is_same_v<TUPLEIn, TUPLEOut>,
79 "constructor argument types must match");
99 : std::conditional_t<std::is_same_v<std::tuple<>,
100 typename PolymorphicConstructorArgs<Derived>::type>,
101 MapConstrArgsEmpty<Base>,
102 MapConstrArgsCopy<Base, Derived>> {};
112[[noreturn]]
void polyInitError(
const char* expected,
const char* actual);
114template<
typename Archive>
127 static_assert(std::is_polymorphic_v<T>,
128 "must be a polymorphic type");
129 static_assert(!std::is_abstract_v<T>,
130 "can't be an abstract type");
131 registerHelper(
typeid(T), [name](Archive& ar,
const void* v) {
133 auto base =
static_cast<const BaseType*
>(v);
134 auto tp =
static_cast<const T*
>(base);
136 saver(ar, *tp,
true, name,
true);
140 template<
typename T>
static void save(Archive& ar, T*
t)
144 template<
typename T>
static void save(
const char* tag, Archive& ar, T&
t)
146 save(tag, ar, &
t,
typeid(
t));
153 using SaveFunction = std::function<void(Archive&,
const void*)>;
154 void registerHelper(
const std::type_info& type,
156 static void save(Archive& ar,
const void*
t,
157 const std::type_info& typeInfo);
158 static void save(
const char* tag, Archive& ar,
const void*
t,
159 const std::type_info& typeInfo);
162 Entry(std::type_index i, SaveFunction s)
163 : index(i), saver(
std::move(s)) {}
165 std::type_index index;
168 std::vector<Entry> saverMap;
169 bool initialized =
false;
172template<
typename Archive>
185 static_assert(std::is_polymorphic_v<T>,
186 "must be a polymorphic type");
187 static_assert(!std::is_abstract_v<T>,
188 "can't be an abstract type");
189 registerHelper(name, [](Archive& ar,
unsigned id,
const void* args) {
193 auto& argsIn = *
static_cast<const TUPLEIn*
>(args);
195 TUPLEOut argsOut = mapArgs(argsIn);
197 return loader(ar,
id, argsOut);
201 static void*
load(Archive& ar,
unsigned id,
const void* args);
207 using LoadFunction = std::function<
void*(Archive&, unsigned,
const void*)>;
208 void registerHelper(
const char* name, LoadFunction loader);
213template<
typename Archive>
226 static_assert(std::is_polymorphic_v<T>,
227 "must be a polymorphic type");
228 static_assert(!std::is_abstract_v<T>,
229 "can't be an abstract type");
230 registerHelper(name, [](Archive& ar,
void* v,
unsigned id) {
232 auto base =
static_cast<BaseType*
>(v);
233 if (
dynamic_cast<T*
>(base) !=
static_cast<T*
>(base)) [[unlikely]] {
236 auto t =
static_cast<T*
>(base);
238 loader(ar, *
t, std::tuple<>(),
id);
242 static void init(
const char* tag, Archive& ar,
void*
t);
248 using InitFunction = std::function<void(Archive&,
void*,
unsigned)>;
249 void registerHelper(
const char* name, InitFunction initializer);
260 template registerClass<T>(name);
268 template registerClass<T>(name);
276 template registerClass<T>(name);
280#define REGISTER_CONSTRUCTOR_ARGS_0(C) \
281template<> struct PolymorphicConstructorArgs<C> \
282{ using type = std::tuple<>; };
284#define REGISTER_CONSTRUCTOR_ARGS_1(C,T1) \
285template<> struct PolymorphicConstructorArgs<C> \
286{ using type = std::tuple<T1>; };
288#define REGISTER_CONSTRUCTOR_ARGS_2(C,T1,T2) \
289template<> struct PolymorphicConstructorArgs<C> \
290{ using type = std::tuple<T1,T2>; };
292#define REGISTER_CONSTRUCTOR_ARGS_3(C,T1,T2,T3) \
293template<> struct PolymorphicConstructorArgs<C> \
294{ using type = std::tuple<T1,T2,T3>; };
296class MemInputArchive;
297class MemOutputArchive;
298class XmlInputArchive;
299class XmlOutputArchive;
309#define REGISTER_POLYMORPHIC_CLASS_HELPER(B,C,N) \
310static_assert(std::is_base_of_v<B,C>, "must be base and sub class"); \
311static const RegisterLoaderHelper<MemInputArchive, C> registerHelper3##C(N); \
312static const RegisterSaverHelper <MemOutputArchive, C> registerHelper4##C(N); \
313static const RegisterLoaderHelper<XmlInputArchive, C> registerHelper5##C(N); \
314static const RegisterSaverHelper <XmlOutputArchive, C> registerHelper6##C(N); \
315template<> struct PolymorphicBaseClass<C> { using type = B; };
317#define REGISTER_POLYMORPHIC_INITIALIZER_HELPER(B,C,N) \
318static_assert(std::is_base_of_v<B,C>, "must be base and sub class"); \
319static const RegisterInitializerHelper<MemInputArchive, C> registerHelper3##C(N); \
320static const RegisterSaverHelper <MemOutputArchive, C> registerHelper4##C(N); \
321static const RegisterInitializerHelper<XmlInputArchive, C> registerHelper5##C(N); \
322static const RegisterSaverHelper <XmlOutputArchive, C> registerHelper6##C(N); \
323template<> struct PolymorphicBaseClass<C> { using type = B; };
325#define REGISTER_BASE_NAME_HELPER(B,N) \
326template<> struct BaseClassName<B> \
327{ static const char* getName() { static constexpr const char* const name = N; return name; } };
332#define REGISTER_POLYMORPHIC_CLASS(BASE,CLASS,NAME) \
333 REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
334 REGISTER_CONSTRUCTOR_ARGS_0(CLASS)
336#define REGISTER_POLYMORPHIC_CLASS_1(BASE,CLASS,NAME,TYPE1) \
337 REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
338 REGISTER_CONSTRUCTOR_ARGS_1(CLASS,TYPE1)
340#define REGISTER_POLYMORPHIC_CLASS_2(BASE,CLASS,NAME,TYPE1,TYPE2) \
341 REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
342 REGISTER_CONSTRUCTOR_ARGS_2(CLASS,TYPE1,TYPE2)
344#define REGISTER_POLYMORPHIC_CLASS_3(BASE,CLASS,NAME,TYPE1,TYPE2,TYPE3) \
345 REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
346 REGISTER_CONSTRUCTOR_ARGS_3(CLASS,TYPE1,TYPE2,TYPE3)
348#define REGISTER_BASE_CLASS(CLASS,NAME) \
349 REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
350 REGISTER_CONSTRUCTOR_ARGS_0(CLASS)
352#define REGISTER_BASE_CLASS_1(CLASS,NAME,TYPE1) \
353 REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
354 REGISTER_CONSTRUCTOR_ARGS_1(CLASS,TYPE1)
356#define REGISTER_BASE_CLASS_2(CLASS,NAME,TYPE1,TYPE2) \
357 REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
358 REGISTER_CONSTRUCTOR_ARGS_2(CLASS,TYPE1,TYPE2)
360#define REGISTER_BASE_CLASS_3(CLASS,NAME,TYPE1,TYPE2,TYPE3) \
361 REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
362 REGISTER_CONSTRUCTOR_ARGS_3(CLASS,TYPE1,TYPE2,TYPE3)
365#define REGISTER_POLYMORPHIC_INITIALIZER(BASE,CLASS,NAME) \
366 REGISTER_POLYMORPHIC_INITIALIZER_HELPER(BASE,CLASS,NAME)
387 static constexpr unsigned value = 1;
389#define SERIALIZE_CLASS_VERSION(CLASS, VERSION) \
390template<> struct SerializeClassVersion<CLASS> \
392 static constexpr unsigned value = VERSION; \
PolymorphicInitializerRegistry & operator=(PolymorphicInitializerRegistry &&)=delete
PolymorphicInitializerRegistry(PolymorphicInitializerRegistry &&)=delete
static void init(const char *tag, Archive &ar, void *t)
PolymorphicInitializerRegistry & operator=(const PolymorphicInitializerRegistry &)=delete
PolymorphicInitializerRegistry(const PolymorphicInitializerRegistry &)=delete
static PolymorphicInitializerRegistry & instance()
void registerClass(const char *name)
static PolymorphicLoaderRegistry & instance()
PolymorphicLoaderRegistry & operator=(PolymorphicLoaderRegistry &&)=delete
void registerClass(const char *name)
PolymorphicLoaderRegistry & operator=(const PolymorphicLoaderRegistry &)=delete
static void * load(Archive &ar, unsigned id, const void *args)
PolymorphicLoaderRegistry(PolymorphicLoaderRegistry &&)=delete
PolymorphicLoaderRegistry(const PolymorphicLoaderRegistry &)=delete
PolymorphicSaverRegistry(const PolymorphicSaverRegistry &)=delete
static PolymorphicSaverRegistry & instance()
PolymorphicSaverRegistry & operator=(const PolymorphicSaverRegistry &)=delete
static void save(const char *tag, Archive &ar, T &t)
PolymorphicSaverRegistry & operator=(PolymorphicSaverRegistry &&)=delete
void registerClass(const char *name)
static void save(Archive &ar, T *t)
PolymorphicSaverRegistry(PolymorphicSaverRegistry &&)=delete
This file implemented 3 utility functions:
void polyInitError(const char *expected, const char *actual)
Stores the name of a base class.
Utility to do T* t = new T(...)
std::unique_ptr< T > operator()(TUPLE tuple)
typename PolymorphicConstructorArgs< Base >::type TUPLEIn
TUPLEOut operator()(const TUPLEIn &t)
typename PolymorphicConstructorArgs< Derived >::type TUPLEOut
typename PolymorphicConstructorArgs< Base >::type TUPLEIn
std::tuple operator()(const TUPLEIn &) const
Define mapping between constructor arg list of base- and subclass.
Store association between (polymorphic) sub- and base class.
Store association between polymorphic class (base- or subclass) and the list of constructor arguments...
RegisterInitializerHelper(const char *name)
RegisterLoaderHelper(const char *name)
RegisterSaverHelper(const char *name)
Store serialization-version number of a class.