openMSX
serialize_meta.hh
Go to the documentation of this file.
1 #ifndef SERIALIZE_META_HH
2 #define SERIALIZE_META_HH
3 
4 #include "hash_map.hh"
5 #include "likely.hh"
6 #include "memory.hh"
7 #include "type_traits.hh"
8 #include "xxhash.hh"
9 #include <tuple>
10 #include <typeindex>
11 #include <type_traits>
12 #include <vector>
13 
14 namespace openmsx {
15 
30 template<typename T> class Creator
31 {
32 public:
33  template<typename TUPLE>
34  std::unique_ptr<T> operator()(TUPLE args) {
35  DoInstantiate<std::tuple_size<TUPLE>::value, TUPLE> inst;
36  return inst(args);
37  }
38 
39 private:
40  template<int I, typename TUPLE> struct DoInstantiate;
41  template<typename TUPLE> struct DoInstantiate<0, TUPLE> {
42  std::unique_ptr<T> operator()(TUPLE /*args*/) {
43  return make_unique<T>();
44  }
45  };
46  template<typename TUPLE> struct DoInstantiate<1, TUPLE> {
47  std::unique_ptr<T> operator()(TUPLE args) {
48  return make_unique<T>(std::get<0>(args));
49  }
50  };
51  template<typename TUPLE> struct DoInstantiate<2, TUPLE> {
52  std::unique_ptr<T> operator()(TUPLE args) {
53  return make_unique<T>(
54  std::get<0>(args), std::get<1>(args));
55  }
56  };
57  template<typename TUPLE> struct DoInstantiate<3, TUPLE> {
58  std::unique_ptr<T> operator()(TUPLE args) {
59  return make_unique<T>(
60  std::get<0>(args), std::get<1>(args),
61  std::get<2>(args));
62  }
63  };
64 };
65 
67 
68 // Polymorphic class loader/saver
69 
70 // forward declarations
71 // ClassSaver: used to save actually save a class. We also store the name of
72 // the class so that the loader knows which concrete class it should load.
73 template<typename T> struct ClassSaver;
74 // NonPolymorphicPointerLoader: once we know which concrete type to load,
75 // we use the 'normal' class loader to load it.
76 template<typename T> struct NonPolymorphicPointerLoader;
77 // Used by PolymorphicInitializer to initialize a concrete type.
78 template<typename T> struct ClassLoader;
79 
85 template<typename T> struct PolymorphicConstructorArgs;
86 
90 template<typename T> struct PolymorphicBaseClass;
91 
92 template<typename Base> struct MapConstrArgsEmpty
93 {
95  std::tuple<> operator()(const TUPLEIn& /*t*/)
96  {
97  return std::make_tuple();
98  }
99 };
100 template<typename Base, typename Derived> struct MapConstrArgsCopy
101 {
104  static_assert(std::is_same<TUPLEIn, TUPLEOut>::value,
105  "constructor argument types must match");
107  {
108  return t;
109  }
110 };
111 
124 template<typename Base, typename Derived> struct MapConstructorArguments
125  : if_<std::is_same<std::tuple<>,
126  typename PolymorphicConstructorArgs<Derived>::type>,
127  MapConstrArgsEmpty<Base>,
128  MapConstrArgsCopy<Base, Derived>> {};
129 
136 template<typename Base> struct BaseClassName;
137 
138 template<typename Archive> class PolymorphicSaverBase
139 {
140 public:
142  virtual void save(Archive& ar, const void* p) const = 0;
143 };
144 
145 template<typename Archive> class PolymorphicLoaderBase
146 {
147 public:
149  virtual void* load(Archive& ar, unsigned id, const void* args) const = 0;
150 };
151 
152 template<typename Archive> class PolymorphicInitializerBase
153 {
154 public:
156  virtual void init(Archive& ar, void* t, unsigned id) const = 0;
157 };
158 
159 template<typename Archive, typename T>
160 class PolymorphicSaver : public PolymorphicSaverBase<Archive>
161 {
162 public:
163  PolymorphicSaver(const char* name_)
164  : name(name_)
165  {
166  }
167  void save(Archive& ar, const void* v) const override
168  {
169  using BaseType = typename PolymorphicBaseClass<T>::type;
170  auto base = static_cast<const BaseType*>(v);
171  auto tp = static_cast<const T*>(base);
172  ClassSaver<T> saver;
173  saver(ar, *tp, true, name, true); // save id, type, constr-args
174  }
175 private:
176  const char* name;
177 };
178 
179 template<typename Archive, typename T>
181 {
182 public:
183  void* load(Archive& ar, unsigned id, const void* args) const override
184  {
185  using BaseType = typename PolymorphicBaseClass<T>::type;
186  using TUPLEIn = typename PolymorphicConstructorArgs<BaseType>::type;
187  using TUPLEOut = typename PolymorphicConstructorArgs<T>::type;
188  auto& argsIn = *static_cast<const TUPLEIn*>(args);
190  TUPLEOut argsOut = mapArgs(argsIn);
192  return loader(ar, id, argsOut);
193  }
194 };
195 
196 void polyInitError(const char* expected, const char* actual);
197 template<typename Archive, typename T>
199 {
200 public:
201  void init(Archive& ar, void* v, unsigned id) const override
202  {
203  using BaseType = typename PolymorphicBaseClass<T>::type;
204  auto base = static_cast<BaseType*>(v);
205  if (unlikely(dynamic_cast<T*>(base) != static_cast<T*>(base))) {
206  polyInitError(typeid(T).name(), typeid(*base).name());
207  }
208  auto t = static_cast<T*>(base);
209  ClassLoader<T> loader;
210  loader(ar, *t, std::make_tuple(), id);
211  }
212 };
213 
214 
215 template<typename Archive>
217 {
218 public:
220 
221  template<typename T> void registerClass(const char* name)
222  {
223  static_assert(std::is_polymorphic<T>::value,
224  "must be a polymorphic type");
225  static_assert(!std::is_abstract<T>::value,
226  "can't be an abstract type");
227  registerHelper(typeid(T),
229  }
230 
231  template<typename T> static void save(Archive& ar, T* t)
232  {
233  save(ar, t, typeid(*t));
234  }
235  template<typename T> static void save(const char* tag, Archive& ar, T& t)
236  {
237  save(tag, ar, &t, typeid(t));
238  }
239 
240 private:
243  void registerHelper(const std::type_info& type,
244  std::unique_ptr<PolymorphicSaverBase<Archive>> saver);
245  static void save(Archive& ar, const void* t,
246  const std::type_info& typeInfo);
247  static void save(const char* tag, Archive& ar, const void* t,
248  const std::type_info& typeInfo);
249 
250  std::vector<std::pair<std::type_index,
251  std::unique_ptr<PolymorphicSaverBase<Archive>>>> saverMap;
252  bool initialized;
253 };
254 
255 template<typename Archive>
257 {
258 public:
260 
261  template<typename T> void registerClass(const char* name)
262  {
263  static_assert(std::is_polymorphic<T>::value,
264  "must be a polymorphic type");
265  static_assert(!std::is_abstract<T>::value,
266  "can't be an abstract type");
267  registerHelper(name,
269  }
270 
271  static void* load(Archive& ar, unsigned id, const void* args);
272 
273 private:
276  void registerHelper(
277  const char* name,
278  std::unique_ptr<PolymorphicLoaderBase<Archive>> loader);
279 
281  loaderMap;
282 };
283 
284 template<typename Archive>
286 {
287 public:
289 
290  template<typename T> void registerClass(const char* name)
291  {
292  static_assert(std::is_polymorphic<T>::value,
293  "must be a polymorphic type");
294  static_assert(!std::is_abstract<T>::value,
295  "can't be an abstract type");
296  registerHelper(name,
298  }
299 
300  static void init(const char* tag, Archive& ar, void* t);
301 
302 private:
305  void registerHelper(
306  const char* name,
307  std::unique_ptr<PolymorphicInitializerBase<Archive>> initializer);
308 
310  initializerMap;
311 };
312 
313 
314 template<typename Archive, typename T> struct RegisterSaverHelper
315 {
316  RegisterSaverHelper(const char* name)
317  {
319  template registerClass<T>(name);
320  }
321 };
322 template<typename Archive, typename T> struct RegisterLoaderHelper
323 {
324  RegisterLoaderHelper(const char* name)
325  {
327  template registerClass<T>(name);
328  }
329 };
330 template<typename Archive, typename T> struct RegisterInitializerHelper
331 {
332  RegisterInitializerHelper(const char* name)
333  {
335  template registerClass<T>(name);
336  }
337 };
338 
339 #define REGISTER_CONSTRUCTOR_ARGS_0(C) \
340 template<> struct PolymorphicConstructorArgs<C> \
341 { using type = std::tuple<>; };
342 
343 #define REGISTER_CONSTRUCTOR_ARGS_1(C,T1) \
344 template<> struct PolymorphicConstructorArgs<C> \
345 { using type = std::tuple<T1>; };
346 
347 #define REGISTER_CONSTRUCTOR_ARGS_2(C,T1,T2) \
348 template<> struct PolymorphicConstructorArgs<C> \
349 { using type = std::tuple<T1,T2>; };
350 
351 #define REGISTER_CONSTRUCTOR_ARGS_3(C,T1,T2,T3) \
352 template<> struct PolymorphicConstructorArgs<C> \
353 { using type = std::tuple<T1,T2,T3>; };
354 
355 class MemInputArchive;
356 class MemOutputArchive;
357 class XmlInputArchive;
358 class XmlOutputArchive;
359 
360 /*#define REGISTER_POLYMORPHIC_CLASS_HELPER(B,C,N) \
361 static_assert(std::is_base_of<B,C>::value, "must be base and sub class"); \
362 static RegisterLoaderHelper<TextInputArchive, C> registerHelper1##C(N); \
363 static RegisterSaverHelper <TextOutputArchive, C> registerHelper2##C(N); \
364 static RegisterLoaderHelper<XmlInputArchive, C> registerHelper3##C(N); \
365 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper4##C(N); \
366 static RegisterLoaderHelper<MemInputArchive, C> registerHelper5##C(N); \
367 static RegisterSaverHelper <MemOutputArchive, C> registerHelper6##C(N); \*/
368 #define REGISTER_POLYMORPHIC_CLASS_HELPER(B,C,N) \
369 static_assert(std::is_base_of<B,C>::value, "must be base and sub class"); \
370 static RegisterLoaderHelper<MemInputArchive, C> registerHelper3##C(N); \
371 static RegisterSaverHelper <MemOutputArchive, C> registerHelper4##C(N); \
372 static RegisterLoaderHelper<XmlInputArchive, C> registerHelper5##C(N); \
373 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper6##C(N); \
374 template<> struct PolymorphicBaseClass<C> { using type = B; };
375 
376 #define REGISTER_POLYMORPHIC_INITIALIZER_HELPER(B,C,N) \
377 static_assert(std::is_base_of<B,C>::value, "must be base and sub class"); \
378 static RegisterInitializerHelper<MemInputArchive, C> registerHelper3##C(N); \
379 static RegisterSaverHelper <MemOutputArchive, C> registerHelper4##C(N); \
380 static RegisterInitializerHelper<XmlInputArchive, C> registerHelper5##C(N); \
381 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper6##C(N); \
382 template<> struct PolymorphicBaseClass<C> { using type = B; };
383 
384 #define REGISTER_BASE_NAME_HELPER(B,N) \
385 template<> struct BaseClassName<B> \
386 { static const char* getName() { static const char* name = N; return name; } };
387 
388 // public macros
389 // these are a more convenient way to define specializations of the
390 // PolymorphicConstructorArgs and PolymorphicBaseClass classes
391 #define REGISTER_POLYMORPHIC_CLASS(BASE,CLASS,NAME) \
392  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
393  REGISTER_CONSTRUCTOR_ARGS_0(CLASS)
394 
395 #define REGISTER_POLYMORPHIC_CLASS_1(BASE,CLASS,NAME,TYPE1) \
396  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
397  REGISTER_CONSTRUCTOR_ARGS_1(CLASS,TYPE1)
398 
399 #define REGISTER_POLYMORPHIC_CLASS_2(BASE,CLASS,NAME,TYPE1,TYPE2) \
400  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
401  REGISTER_CONSTRUCTOR_ARGS_2(CLASS,TYPE1,TYPE2)
402 
403 #define REGISTER_POLYMORPHIC_CLASS_3(BASE,CLASS,NAME,TYPE1,TYPE2,TYPE3) \
404  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
405  REGISTER_CONSTRUCTOR_ARGS_3(CLASS,TYPE1,TYPE2,TYPE3)
406 
407 #define REGISTER_BASE_CLASS(CLASS,NAME) \
408  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
409  REGISTER_CONSTRUCTOR_ARGS_0(CLASS)
410 
411 #define REGISTER_BASE_CLASS_1(CLASS,NAME,TYPE1) \
412  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
413  REGISTER_CONSTRUCTOR_ARGS_1(CLASS,TYPE1)
414 
415 #define REGISTER_BASE_CLASS_2(CLASS,NAME,TYPE1,TYPE2) \
416  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
417  REGISTER_CONSTRUCTOR_ARGS_2(CLASS,TYPE1,TYPE2)
418 
419 #define REGISTER_BASE_CLASS_3(CLASS,NAME,TYPE1,TYPE2,TYPE3) \
420  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
421  REGISTER_CONSTRUCTOR_ARGS_3(CLASS,TYPE1,TYPE2,TYPE3)
422 
423 
424 #define REGISTER_POLYMORPHIC_INITIALIZER(BASE,CLASS,NAME) \
425  REGISTER_POLYMORPHIC_INITIALIZER_HELPER(BASE,CLASS,NAME)
426 
428 
444 template<typename T> struct SerializeClassVersion
445 {
446  static const unsigned value = 1;
447 };
448 #define SERIALIZE_CLASS_VERSION(CLASS, VERSION) \
449 template<> struct SerializeClassVersion<CLASS> \
450 { \
451  static const unsigned value = VERSION; \
452 };
453 
454 } // namespace openmsx
455 
456 #endif
virtual void * load(Archive &ar, unsigned id, const void *args) const =0
typename PolymorphicConstructorArgs< Derived >::type TUPLEOut
std::unique_ptr< T > operator()(TUPLE args)
void polyInitError(const char *expected, const char *actual)
std::tuple operator()(const TUPLEIn &)
#define unlikely(x)
Definition: likely.hh:15
Define mapping between constructor arg list of base- and subclass.
static PolymorphicInitializerRegistry & instance()
virtual void save(Archive &ar, const void *p) const =0
Store association between (polymorphic) sub- and baseclass.
static PolymorphicLoaderRegistry & instance()
Store association between polymorphic class (base- or subclass) and the list of constructor arguments...
Utility to do T* t = new T(...)
static void save(const char *tag, Archive &ar, T &t)
static PolymorphicSaverRegistry & instance()
static void * load(Archive &ar, unsigned id, const void *args)
Stores the name of a base class.
void init(Archive &ar, void *v, unsigned id) const override
PolymorphicSaver(const char *name_)
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
RegisterInitializerHelper(const char *name)
virtual void init(Archive &ar, void *t, unsigned id) const =0
RegisterSaverHelper(const char *name)
void save(Archive &ar, const void *v) const override
typename PolymorphicConstructorArgs< Base >::type TUPLEIn
RegisterLoaderHelper(const char *name)
typename PolymorphicConstructorArgs< Base >::type TUPLEIn
void * load(Archive &ar, unsigned id, const void *args) const override
void registerClass(const char *name)
TUPLEOut operator()(const TUPLEIn &t)
void registerClass(const char *name)
std::unique_ptr< T > make_unique()
Definition: memory.hh:27
static void init(const char *tag, Archive &ar, void *t)
static void save(Archive &ar, T *t)