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:
194  static PolymorphicSaverRegistry& instance();
195 
196  template<typename T> void registerClass(const char* name)
197  {
198  static_assert(std::is_polymorphic_v<T>,
199  "must be a polymorphic type");
200  static_assert(!std::is_abstract_v<T>,
201  "can't be an abstract type");
202  registerHelper(typeid(T),
203  std::make_unique<PolymorphicSaver<Archive, T>>(name));
204  }
205 
206  template<typename T> static void save(Archive& ar, T* t)
207  {
208  save(ar, t, typeid(*t));
209  }
210  template<typename T> static void save(const char* tag, Archive& ar, T& t)
211  {
212  save(tag, ar, &t, typeid(t));
213  }
214 
215 private:
216  PolymorphicSaverRegistry() = default;
217  ~PolymorphicSaverRegistry() = default;
218  void registerHelper(const std::type_info& type,
219  std::unique_ptr<PolymorphicSaverBase<Archive>> saver);
220  static void save(Archive& ar, const void* t,
221  const std::type_info& typeInfo);
222  static void save(const char* tag, Archive& ar, const void* t,
223  const std::type_info& typeInfo);
224 
225  std::vector<std::pair<std::type_index,
226  std::unique_ptr<PolymorphicSaverBase<Archive>>>> saverMap;
227  bool initialized = false;
228 };
229 
230 template<typename Archive>
232 {
233 public:
234  static PolymorphicLoaderRegistry& instance();
235 
236  template<typename T> void registerClass(const char* name)
237  {
238  static_assert(std::is_polymorphic_v<T>,
239  "must be a polymorphic type");
240  static_assert(!std::is_abstract_v<T>,
241  "can't be an abstract type");
242  registerHelper(name,
243  std::make_unique<PolymorphicLoader<Archive, T>>());
244  }
245 
246  static void* load(Archive& ar, unsigned id, const void* args);
247 
248 private:
249  PolymorphicLoaderRegistry() = default;
250  ~PolymorphicLoaderRegistry() = default;
251  void registerHelper(
252  const char* name,
253  std::unique_ptr<PolymorphicLoaderBase<Archive>> loader);
254 
256  loaderMap;
257 };
258 
259 template<typename Archive>
261 {
262 public:
263  static PolymorphicInitializerRegistry& instance();
264 
265  template<typename T> void registerClass(const char* name)
266  {
267  static_assert(std::is_polymorphic_v<T>,
268  "must be a polymorphic type");
269  static_assert(!std::is_abstract_v<T>,
270  "can't be an abstract type");
271  registerHelper(name,
272  std::make_unique<PolymorphicInitializer<Archive, T>>());
273  }
274 
275  static void init(const char* tag, Archive& ar, void* t);
276 
277 private:
278  PolymorphicInitializerRegistry() = default;
279  ~PolymorphicInitializerRegistry() = default;
280  void registerHelper(
281  const char* name,
282  std::unique_ptr<PolymorphicInitializerBase<Archive>> initializer);
283 
285  initializerMap;
286 };
287 
288 
289 template<typename Archive, typename T> struct RegisterSaverHelper
290 {
291  explicit RegisterSaverHelper(const char* name)
292  {
294  template registerClass<T>(name);
295  }
296 };
297 template<typename Archive, typename T> struct RegisterLoaderHelper
298 {
299  explicit RegisterLoaderHelper(const char* name)
300  {
302  template registerClass<T>(name);
303  }
304 };
305 template<typename Archive, typename T> struct RegisterInitializerHelper
306 {
307  explicit RegisterInitializerHelper(const char* name)
308  {
310  template registerClass<T>(name);
311  }
312 };
313 
314 #define REGISTER_CONSTRUCTOR_ARGS_0(C) \
315 template<> struct PolymorphicConstructorArgs<C> \
316 { using type = std::tuple<>; };
317 
318 #define REGISTER_CONSTRUCTOR_ARGS_1(C,T1) \
319 template<> struct PolymorphicConstructorArgs<C> \
320 { using type = std::tuple<T1>; };
321 
322 #define REGISTER_CONSTRUCTOR_ARGS_2(C,T1,T2) \
323 template<> struct PolymorphicConstructorArgs<C> \
324 { using type = std::tuple<T1,T2>; };
325 
326 #define REGISTER_CONSTRUCTOR_ARGS_3(C,T1,T2,T3) \
327 template<> struct PolymorphicConstructorArgs<C> \
328 { using type = std::tuple<T1,T2,T3>; };
329 
330 class MemInputArchive;
331 class MemOutputArchive;
332 class XmlInputArchive;
333 class XmlOutputArchive;
334 
335 /*#define REGISTER_POLYMORPHIC_CLASS_HELPER(B,C,N) \
336 static_assert(std::is_base_of_v<B,C>, "must be base and sub class"); \
337 static RegisterLoaderHelper<TextInputArchive, C> registerHelper1##C(N); \
338 static RegisterSaverHelper <TextOutputArchive, C> registerHelper2##C(N); \
339 static RegisterLoaderHelper<XmlInputArchive, C> registerHelper3##C(N); \
340 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper4##C(N); \
341 static RegisterLoaderHelper<MemInputArchive, C> registerHelper5##C(N); \
342 static RegisterSaverHelper <MemOutputArchive, C> registerHelper6##C(N); \*/
343 #define REGISTER_POLYMORPHIC_CLASS_HELPER(B,C,N) \
344 static_assert(std::is_base_of_v<B,C>, "must be base and sub class"); \
345 static RegisterLoaderHelper<MemInputArchive, C> registerHelper3##C(N); \
346 static RegisterSaverHelper <MemOutputArchive, C> registerHelper4##C(N); \
347 static RegisterLoaderHelper<XmlInputArchive, C> registerHelper5##C(N); \
348 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper6##C(N); \
349 template<> struct PolymorphicBaseClass<C> { using type = B; };
350 
351 #define REGISTER_POLYMORPHIC_INITIALIZER_HELPER(B,C,N) \
352 static_assert(std::is_base_of_v<B,C>, "must be base and sub class"); \
353 static RegisterInitializerHelper<MemInputArchive, C> registerHelper3##C(N); \
354 static RegisterSaverHelper <MemOutputArchive, C> registerHelper4##C(N); \
355 static RegisterInitializerHelper<XmlInputArchive, C> registerHelper5##C(N); \
356 static RegisterSaverHelper <XmlOutputArchive, C> registerHelper6##C(N); \
357 template<> struct PolymorphicBaseClass<C> { using type = B; };
358 
359 #define REGISTER_BASE_NAME_HELPER(B,N) \
360 template<> struct BaseClassName<B> \
361 { static const char* getName() { static constexpr const char* const name = N; return name; } };
362 
363 // public macros
364 // these are a more convenient way to define specializations of the
365 // PolymorphicConstructorArgs and PolymorphicBaseClass classes
366 #define REGISTER_POLYMORPHIC_CLASS(BASE,CLASS,NAME) \
367  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
368  REGISTER_CONSTRUCTOR_ARGS_0(CLASS)
369 
370 #define REGISTER_POLYMORPHIC_CLASS_1(BASE,CLASS,NAME,TYPE1) \
371  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
372  REGISTER_CONSTRUCTOR_ARGS_1(CLASS,TYPE1)
373 
374 #define REGISTER_POLYMORPHIC_CLASS_2(BASE,CLASS,NAME,TYPE1,TYPE2) \
375  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
376  REGISTER_CONSTRUCTOR_ARGS_2(CLASS,TYPE1,TYPE2)
377 
378 #define REGISTER_POLYMORPHIC_CLASS_3(BASE,CLASS,NAME,TYPE1,TYPE2,TYPE3) \
379  REGISTER_POLYMORPHIC_CLASS_HELPER(BASE,CLASS,NAME) \
380  REGISTER_CONSTRUCTOR_ARGS_3(CLASS,TYPE1,TYPE2,TYPE3)
381 
382 #define REGISTER_BASE_CLASS(CLASS,NAME) \
383  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
384  REGISTER_CONSTRUCTOR_ARGS_0(CLASS)
385 
386 #define REGISTER_BASE_CLASS_1(CLASS,NAME,TYPE1) \
387  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
388  REGISTER_CONSTRUCTOR_ARGS_1(CLASS,TYPE1)
389 
390 #define REGISTER_BASE_CLASS_2(CLASS,NAME,TYPE1,TYPE2) \
391  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
392  REGISTER_CONSTRUCTOR_ARGS_2(CLASS,TYPE1,TYPE2)
393 
394 #define REGISTER_BASE_CLASS_3(CLASS,NAME,TYPE1,TYPE2,TYPE3) \
395  REGISTER_BASE_NAME_HELPER(CLASS,NAME) \
396  REGISTER_CONSTRUCTOR_ARGS_3(CLASS,TYPE1,TYPE2,TYPE3)
397 
398 
399 #define REGISTER_POLYMORPHIC_INITIALIZER(BASE,CLASS,NAME) \
400  REGISTER_POLYMORPHIC_INITIALIZER_HELPER(BASE,CLASS,NAME)
401 
403 
419 template<typename T> struct SerializeClassVersion
420 {
421  static constexpr unsigned value = 1;
422 };
423 #define SERIALIZE_CLASS_VERSION(CLASS, VERSION) \
424 template<> struct SerializeClassVersion<CLASS> \
425 { \
426  static constexpr unsigned value = VERSION; \
427 };
428 
429 } // namespace openmsx
430 
431 #endif
typename PolymorphicConstructorArgs< Derived >::type TUPLEOut
std::unique_ptr< T > operator()(TUPLE tuple)
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()
Store association between (polymorphic) sub- and baseclass.
static PolymorphicLoaderRegistry & instance()
Store association between polymorphic class (base- or subclass) and the list of constructor arguments...
static void save(const char *tag, Archive &ar, T &t)
static PolymorphicSaverRegistry & instance()
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)
XMLElement load(string_view filename, string_view systemID)
Definition: XMLLoader.cc:33
RegisterSaverHelper(const char *name)
void save(Archive &ar, const void *v) const override
Utility to do T* t = new T(...)
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)
TclObject t
static void save(Archive &ar, T *t)