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 "xxhash.hh"
7 #include <memory>
8 #include <tuple>
9 #include <typeindex>
10 #include <type_traits>
11 #include <utility>
12 #include <vector>
13 
14 namespace openmsx {
15 
30 template<typename T> struct Creator
31 {
32  template<typename TUPLE>
33  std::unique_ptr<T> operator()(TUPLE tuple) {
34  auto makeT = [](auto&& ...args) {
35  return std::make_unique<T>(std::forward<decltype(args)>(args)...);
36  };
37  return std::apply(makeT, tuple);
38  }
39 };
40 
42 
43 // Polymorphic class loader/saver
44 
45 // forward declarations
46 // ClassSaver: used to actually save a class. We also store the name of
47 // the class so that the loader knows which concrete class it should load.
48 template<typename T> struct ClassSaver;
49 // NonPolymorphicPointerLoader: once we know which concrete type to load,
50 // we use the 'normal' class loader to load it.
51 template<typename T> struct NonPolymorphicPointerLoader;
52 // Used by PolymorphicInitializer to initialize a concrete type.
53 template<typename T> struct ClassLoader;
54 
60 template<typename T> struct PolymorphicConstructorArgs;
61 
65 template<typename T> struct PolymorphicBaseClass;
66 
67 template<typename Base> struct MapConstrArgsEmpty
68 {
70  std::tuple<> operator()(const TUPLEIn& /*t*/)
71  {
72  return std::tuple<>();
73  }
74 };
75 template<typename Base, typename Derived> struct MapConstrArgsCopy
76 {
79  static_assert(std::is_same_v<TUPLEIn, TUPLEOut>,
80  "constructor argument types must match");
82  {
83  return t;
84  }
85 };
86 
99 template<typename Base, typename Derived> struct MapConstructorArguments
100  : std::conditional_t<std::is_same<std::tuple<>,
101  typename PolymorphicConstructorArgs<Derived>::type>::value,
102  MapConstrArgsEmpty<Base>,
103  MapConstrArgsCopy<Base, Derived>> {};
104 
111 template<typename Base> struct BaseClassName;
112 
113 template<typename Archive> class PolymorphicSaverBase
114 {
115 public:
116  virtual ~PolymorphicSaverBase() = default;
117  virtual void save(Archive& ar, const void* p) const = 0;
118 };
119 
120 template<typename Archive> class PolymorphicLoaderBase
121 {
122 public:
123  virtual ~PolymorphicLoaderBase() = default;
124  virtual void* load(Archive& ar, unsigned id, const void* args) const = 0;
125 };
126 
127 template<typename Archive> class PolymorphicInitializerBase
128 {
129 public:
130  virtual ~PolymorphicInitializerBase() = default;
131  virtual void init(Archive& ar, void* t, unsigned id) const = 0;
132 };
133 
134 template<typename Archive, typename T>
135 class PolymorphicSaver : public PolymorphicSaverBase<Archive>
136 {
137 public:
138  explicit PolymorphicSaver(const char* name_)
139  : name(name_)
140  {
141  }
142  void save(Archive& ar, const void* v) const override
143  {
144  using BaseType = typename PolymorphicBaseClass<T>::type;
145  auto base = static_cast<const BaseType*>(v);
146  auto tp = static_cast<const T*>(base);
147  ClassSaver<T> saver;
148  saver(ar, *tp, true, name, true); // save id, type, constr-args
149  }
150 private:
151  const char* name;
152 };
153 
154 template<typename Archive, typename T>
156 {
157 public:
158  void* load(Archive& ar, unsigned id, const void* args) const override
159  {
160  using BaseType = typename PolymorphicBaseClass<T>::type;
161  using TUPLEIn = typename PolymorphicConstructorArgs<BaseType>::type;
162  using TUPLEOut = typename PolymorphicConstructorArgs<T>::type;
163  auto& argsIn = *static_cast<const TUPLEIn*>(args);
165  TUPLEOut argsOut = mapArgs(argsIn);
167  return loader(ar, id, argsOut);
168  }
169 };
170 
171 void polyInitError(const char* expected, const char* actual);
172 template<typename Archive, typename T>
174 {
175 public:
176  void init(Archive& ar, void* v, unsigned id) const override
177  {
178  using BaseType = typename PolymorphicBaseClass<T>::type;
179  auto base = static_cast<BaseType*>(v);
180  if (unlikely(dynamic_cast<T*>(base) != static_cast<T*>(base))) {
181  polyInitError(typeid(T).name(), typeid(*base).name());
182  }
183  auto t = static_cast<T*>(base);
184  ClassLoader<T> loader;
185  loader(ar, *t, std::tuple<>(), id);
186  }
187 };
188 
189 
190 template<typename Archive>
192 {
193 public:
196 
198 
199  template<typename T> void registerClass(const char* name)
200  {
201  static_assert(std::is_polymorphic_v<T>,
202  "must be a polymorphic type");
203  static_assert(!std::is_abstract_v<T>,
204  "can't be an abstract type");
205  registerHelper(typeid(T),
206  std::make_unique<PolymorphicSaver<Archive, T>>(name));
207  }
208 
209  template<typename T> static void save(Archive& ar, T* t)
210  {
211  save(ar, t, typeid(*t));
212  }
213  template<typename T> static void save(const char* tag, Archive& ar, T& t)
214  {
215  save(tag, ar, &t, typeid(t));
216  }
217 
218 private:
219  PolymorphicSaverRegistry() = default;
220  ~PolymorphicSaverRegistry() = default;
221 
222  void registerHelper(const std::type_info& type,
223  std::unique_ptr<PolymorphicSaverBase<Archive>> saver);
224  static void save(Archive& ar, const void* t,
225  const std::type_info& typeInfo);
226  static void save(const char* tag, Archive& ar, const void* t,
227  const std::type_info& typeInfo);
228 
229  std::vector<std::pair<std::type_index,
230  std::unique_ptr<PolymorphicSaverBase<Archive>>>> saverMap;
231  bool initialized = false;
232 };
233 
234 template<typename Archive>
236 {
237 public:
240 
242 
243  template<typename T> void registerClass(const char* name)
244  {
245  static_assert(std::is_polymorphic_v<T>,
246  "must be a polymorphic type");
247  static_assert(!std::is_abstract_v<T>,
248  "can't be an abstract type");
249  registerHelper(name,
250  std::make_unique<PolymorphicLoader<Archive, T>>());
251  }
252 
253  static void* load(Archive& ar, unsigned id, const void* args);
254 
255 private:
256  PolymorphicLoaderRegistry() = default;
257  ~PolymorphicLoaderRegistry() = default;
258 
259  void registerHelper(
260  const char* name,
261  std::unique_ptr<PolymorphicLoaderBase<Archive>> loader);
262 
264  loaderMap;
265 };
266 
267 template<typename Archive>
269 {
270 public:
273 
275 
276  template<typename T> void registerClass(const char* name)
277  {
278  static_assert(std::is_polymorphic_v<T>,
279  "must be a polymorphic type");
280  static_assert(!std::is_abstract_v<T>,
281  "can't be an abstract type");
282  registerHelper(name,
283  std::make_unique<PolymorphicInitializer<Archive, T>>());
284  }
285 
286  static void init(const char* tag, Archive& ar, void* t);
287 
288 private:
289  PolymorphicInitializerRegistry() = default;
290  ~PolymorphicInitializerRegistry() = default;
291 
292  void registerHelper(
293  const char* name,
294  std::unique_ptr<PolymorphicInitializerBase<Archive>> initializer);
295 
297  initializerMap;
298 };
299 
300 
301 template<typename Archive, typename T> struct RegisterSaverHelper
302 {
303  explicit RegisterSaverHelper(const char* name)
304  {
306  template registerClass<T>(name);
307  }
308 };
309 template<typename Archive, typename T> struct RegisterLoaderHelper
310 {
311  explicit RegisterLoaderHelper(const char* name)
312  {
314  template registerClass<T>(name);
315  }
316 };
317 template<typename Archive, typename T> struct RegisterInitializerHelper
318 {
319  explicit RegisterInitializerHelper(const char* name)
320  {
322  template registerClass<T>(name);
323  }
324 };
325 
326 #define REGISTER_CONSTRUCTOR_ARGS_0(C) \
327 template<> struct PolymorphicConstructorArgs<C> \
328 { using type = std::tuple<>; };
329 
330 #define REGISTER_CONSTRUCTOR_ARGS_1(C,T1) \
331 template<> struct PolymorphicConstructorArgs<C> \
332 { using type = std::tuple<T1>; };
333 
334 #define REGISTER_CONSTRUCTOR_ARGS_2(C,T1,T2) \
335 template<> struct PolymorphicConstructorArgs<C> \
336 { using type = std::tuple<T1,T2>; };
337 
338 #define REGISTER_CONSTRUCTOR_ARGS_3(C,T1,T2,T3) \
339 template<> struct PolymorphicConstructorArgs<C> \
340 { using type = std::tuple<T1,T2,T3>; };
341 
342 class MemInputArchive;
343 class MemOutputArchive;
344 class XmlInputArchive;
345 class XmlOutputArchive;
346 
347 /*#define REGISTER_POLYMORPHIC_CLASS_HELPER(B,C,N) \
348 static_assert(std::is_base_of_v<B,C>, "must be base and sub class"); \
349 static RegisterLoaderHelper<TextInputArchive, C> registerHelper1##C(N); \
350 static RegisterSaverHelper <TextOutputArchive, C> registerHelper2##C(N); \
351 static RegisterLoaderHelper<XmlInputArchive, C> registerHelper3##C(N); \
352 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper4##C(N); \
353 static RegisterLoaderHelper<MemInputArchive, C> registerHelper5##C(N); \
354 static RegisterSaverHelper <MemOutputArchive, C> registerHelper6##C(N); \*/
355 #define REGISTER_POLYMORPHIC_CLASS_HELPER(B,C,N) \
356 static_assert(std::is_base_of_v<B,C>, "must be base and sub class"); \
357 static RegisterLoaderHelper<MemInputArchive, C> registerHelper3##C(N); \
358 static RegisterSaverHelper <MemOutputArchive, C> registerHelper4##C(N); \
359 static RegisterLoaderHelper<XmlInputArchive, C> registerHelper5##C(N); \
360 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper6##C(N); \
361 template<> struct PolymorphicBaseClass<C> { using type = B; };
362 
363 #define REGISTER_POLYMORPHIC_INITIALIZER_HELPER(B,C,N) \
364 static_assert(std::is_base_of_v<B,C>, "must be base and sub class"); \
365 static RegisterInitializerHelper<MemInputArchive, C> registerHelper3##C(N); \
366 static RegisterSaverHelper <MemOutputArchive, C> registerHelper4##C(N); \
367 static RegisterInitializerHelper<XmlInputArchive, C> registerHelper5##C(N); \
368 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper6##C(N); \
369 template<> struct PolymorphicBaseClass<C> { using type = B; };
370 
371 #define REGISTER_BASE_NAME_HELPER(B,N) \
372 template<> struct BaseClassName<B> \
373 { static const char* getName() { static constexpr const char* const name = N; return name; } };
374 
375 // public macros
376 // these are a more convenient way to define specializations of the
377 // PolymorphicConstructorArgs and PolymorphicBaseClass classes
378 #define REGISTER_POLYMORPHIC_CLASS(BASE,CLASS,NAME) \
379  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
380  REGISTER_CONSTRUCTOR_ARGS_0(CLASS)
381 
382 #define REGISTER_POLYMORPHIC_CLASS_1(BASE,CLASS,NAME,TYPE1) \
383  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
384  REGISTER_CONSTRUCTOR_ARGS_1(CLASS,TYPE1)
385 
386 #define REGISTER_POLYMORPHIC_CLASS_2(BASE,CLASS,NAME,TYPE1,TYPE2) \
387  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
388  REGISTER_CONSTRUCTOR_ARGS_2(CLASS,TYPE1,TYPE2)
389 
390 #define REGISTER_POLYMORPHIC_CLASS_3(BASE,CLASS,NAME,TYPE1,TYPE2,TYPE3) \
391  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
392  REGISTER_CONSTRUCTOR_ARGS_3(CLASS,TYPE1,TYPE2,TYPE3)
393 
394 #define REGISTER_BASE_CLASS(CLASS,NAME) \
395  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
396  REGISTER_CONSTRUCTOR_ARGS_0(CLASS)
397 
398 #define REGISTER_BASE_CLASS_1(CLASS,NAME,TYPE1) \
399  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
400  REGISTER_CONSTRUCTOR_ARGS_1(CLASS,TYPE1)
401 
402 #define REGISTER_BASE_CLASS_2(CLASS,NAME,TYPE1,TYPE2) \
403  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
404  REGISTER_CONSTRUCTOR_ARGS_2(CLASS,TYPE1,TYPE2)
405 
406 #define REGISTER_BASE_CLASS_3(CLASS,NAME,TYPE1,TYPE2,TYPE3) \
407  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
408  REGISTER_CONSTRUCTOR_ARGS_3(CLASS,TYPE1,TYPE2,TYPE3)
409 
410 
411 #define REGISTER_POLYMORPHIC_INITIALIZER(BASE,CLASS,NAME) \
412  REGISTER_POLYMORPHIC_INITIALIZER_HELPER(BASE,CLASS,NAME)
413 
415 
431 template<typename T> struct SerializeClassVersion
432 {
433  static constexpr unsigned value = 1;
434 };
435 #define SERIALIZE_CLASS_VERSION(CLASS, VERSION) \
436 template<> struct SerializeClassVersion<CLASS> \
437 { \
438  static constexpr unsigned value = VERSION; \
439 };
440 
441 } // namespace openmsx
442 
443 #endif
TclObject t
virtual ~PolymorphicInitializerBase()=default
virtual void init(Archive &ar, void *t, unsigned id) const =0
static void init(const char *tag, Archive &ar, void *t)
PolymorphicInitializerRegistry(const PolymorphicInitializerRegistry &)=delete
PolymorphicInitializerRegistry & operator=(const PolymorphicInitializerRegistry &)=delete
static PolymorphicInitializerRegistry & instance()
void init(Archive &ar, void *v, unsigned id) const override
virtual void * load(Archive &ar, unsigned id, const void *args) const =0
virtual ~PolymorphicLoaderBase()=default
static PolymorphicLoaderRegistry & instance()
void registerClass(const char *name)
static void * load(Archive &ar, unsigned id, const void *args)
PolymorphicLoaderRegistry(const PolymorphicLoaderRegistry &)=delete
PolymorphicLoaderRegistry & operator=(const PolymorphicLoaderRegistry &)=delete
void * load(Archive &ar, unsigned id, const void *args) const override
virtual void save(Archive &ar, const void *p) const =0
virtual ~PolymorphicSaverBase()=default
PolymorphicSaverRegistry(const PolymorphicSaverRegistry &)=delete
static PolymorphicSaverRegistry & instance()
PolymorphicSaverRegistry & operator=(const PolymorphicSaverRegistry &)=delete
static void save(const char *tag, Archive &ar, T &t)
void registerClass(const char *name)
static void save(Archive &ar, T *t)
void save(Archive &ar, const void *v) const override
PolymorphicSaver(const char *name_)
#define unlikely(x)
Definition: likely.hh:15
This file implemented 3 utility functions:
Definition: Autofire.cc:5
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
std::tuple operator()(const TUPLEIn &)
typename PolymorphicConstructorArgs< Base >::type TUPLEIn
Define mapping between constructor arg list of base- and subclass.
Store association between (polymorphic) sub- and baseclass.
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.