openMSX
serialize_core.hh
Go to the documentation of this file.
1 #ifndef SERIALIZE_CORE_HH
2 #define SERIALIZE_CORE_HH
3 
4 #include "serialize_constr.hh"
5 #include "serialize_meta.hh"
6 #include "one_of.hh"
7 #include "unreachable.hh"
8 #include "xrange.hh"
9 #include <cassert>
10 #include <initializer_list>
11 #include <memory>
12 #include <string>
13 #include <type_traits>
14 #include <variant>
15 
16 namespace openmsx {
17 
18 // Is there a specialized way to serialize 'T'.
19 template<typename T> struct Serializer : std::false_type {
20  struct Saver {};
21  struct Loader {};
22 };
23 
24 // Type-queries for serialization framework
25 // is_primitive<T>
26 template<typename T> struct is_primitive : std::false_type {};
27 template<> struct is_primitive<bool> : std::true_type {};
28 template<> struct is_primitive<char> : std::true_type {};
29 template<> struct is_primitive<signed char> : std::true_type {};
30 template<> struct is_primitive<signed short> : std::true_type {};
31 template<> struct is_primitive<signed int> : std::true_type {};
32 template<> struct is_primitive<signed long> : std::true_type {};
33 template<> struct is_primitive<unsigned char> : std::true_type {};
34 template<> struct is_primitive<unsigned short> : std::true_type {};
35 template<> struct is_primitive<unsigned int> : std::true_type {};
36 template<> struct is_primitive<unsigned long> : std::true_type {};
37 template<> struct is_primitive<float> : std::true_type {};
38 template<> struct is_primitive<double> : std::true_type {};
39 template<> struct is_primitive<long double> : std::true_type {};
40 template<> struct is_primitive<long long> : std::true_type {};
41 template<> struct is_primitive<unsigned long long> : std::true_type {};
42 template<> struct is_primitive<std::string> : std::true_type {};
43 
44 
45 // Normally to make a class serializable, you have to implement a serialize()
46 // method on the class. For some classes we cannot extend the source code. So
47 // we need an alternative, non-intrusive, way to make those classes
48 // serializable.
49 template<typename Archive, typename T>
50 void serialize(Archive& ar, T& t, unsigned version)
51 {
52  // By default use the serialize() member. But this function can
53  // be overloaded to serialize classes in a non-intrusive way.
54  t.serialize(ar, version);
55 }
56 
57 template<typename Archive, typename T1, typename T2>
58 void serialize(Archive& ar, std::pair<T1, T2>& p, unsigned /*version*/)
59 {
60  ar.serialize("first", p.first,
61  "second", p.second);
62 }
63 template<typename T1, typename T2> struct SerializeClassVersion<std::pair<T1, T2>>
64 {
65  static constexpr unsigned value = 0;
66 };
67 
69 
106 template<typename T> struct serialize_as_enum : std::false_type {};
107 
108 template<typename T> struct enum_string {
109  const char* str;
110  T e;
111 };
112 void enumError(const std::string_view str);
113 
114 template<typename T>
115 inline std::string toString(std::initializer_list<enum_string<T>> list, T t_)
116 {
117  for (auto& [str, t] : list) {
118  if (t == t_) return str;
119  }
120  assert(false);
121  return "internal-error-unknown-enum-value";
122 }
123 
124 template<typename T>
125 T fromString(std::initializer_list<enum_string<T>> list, std::string_view str_)
126 {
127  for (auto& [str, t] : list) {
128  if (str == str_) return t;
129  }
130  enumError(str_); // does not return (throws)
131  return T(); // avoid warning
132 }
133 
134 #define SERIALIZE_ENUM(TYPE,INFO) \
135 template<> struct serialize_as_enum< TYPE > : std::true_type { \
136  serialize_as_enum() : info(INFO) {} \
137  std::initializer_list<enum_string< TYPE >> info; \
138 };
139 
140 template<typename Archive, typename T, typename SaveAction>
141 void saveEnum(std::initializer_list<enum_string<T>> list, T t, SaveAction save)
142 {
143  if constexpr (Archive::TRANSLATE_ENUM_TO_STRING) {
144  save(toString(list, t));
145  } else {
146  save(int(t));
147  }
148 }
149 
150 template<typename Archive, typename T, typename LoadAction>
151 void loadEnum(std::initializer_list<enum_string<T>> list, T& t, LoadAction load)
152 {
153  if constexpr (Archive::TRANSLATE_ENUM_TO_STRING) {
154  std::string str;
155  load(str);
156  t = fromString(list, str);
157  } else {
158  int i;
159  load(i);
160  t = T(i);
161  }
162 }
163 
165 
166 template<size_t I, typename Variant>
168  Variant operator()(size_t index) const {
169  if constexpr (I == std::variant_size_v<Variant>) {
170  UNREACHABLE;
171  } else if (index == I) {
172  return std::variant_alternative_t<I, Variant>{};
173  } else {
175  }
176  }
177 };
178 template<typename Variant>
179 Variant defaultConstructVariant(size_t index)
180 {
181  return DefaultConstructVariant<0, Variant>{}(index);
182 }
183 
184 template<typename V> struct VariantSerializer : std::true_type
185 {
186  template<typename A>
187  static inline constexpr size_t index = get_index<A, V>::value;
188 
189  struct Saver {
190  template<typename Archive>
191  void operator()(Archive& ar, const V& v, bool saveId) {
192  saveEnum<Archive>(Serializer<V>::list, v.index(),
193  [&](const auto& t) { ar.attribute("type", t); });
194  std::visit([&](auto& e) {
195  using TNC = std::remove_cvref_t<decltype(e)>;
196  auto& e2 = const_cast<TNC&>(e);
197  ClassSaver<TNC> saver;
198  saver(ar, e2, saveId);
199  }, v);
200  }
201  };
202  struct Loader {
203  template<typename Archive, typename TUPLE>
204  void operator()(Archive& ar, V& v, TUPLE args, int id) {
205  size_t idx;
206  loadEnum<Archive>(Serializer<V>::list, idx,
207  [&](auto& l) { ar.attribute("type", l); });
208  v = defaultConstructVariant<V>(idx);
209  std::visit([&](auto& e) {
210  ClassLoader<decltype(e)> loader;
211  loader(ar, e, args, id);
212  }, v);
213  }
214  };
215 };
216 
217 
218 
220 
221 // serialize_as_pointer<T>
222 //
223 // Type-trait class that indicates whether a certain type should be serialized
224 // as a pointer or not. There can be multiple pointers to the same object,
225 // only the first such pointer is actually stored, the others are stored as
226 // a reference to this first object.
227 //
228 // By default all pointer types are treated as pointer, but also smart pointer
229 // can be treated as such. Though only unique_ptr<T> is implemented ATM.
230 //
231 // The serialize_as_pointer class has the following members:
232 // - static bool value
233 // True iff this type must be serialized as a pointer.
234 // The fields below are only valid (even only present) if this variable
235 // is true.
236 // - using type = T
237 // The pointed-to type
238 // - T* getPointer(X x)
239 // Get an actual pointer from the abstract pointer x
240 // (X can be a smart pointer type)
241 // - void setPointer(X x, T* p, Archive& ar)
242 // Copy the raw-pointer p to the abstract pointer x
243 // The archive can be used to store per-archive data, this is for example
244 // needed for shared_ptr.
245 
246 template<typename T> struct serialize_as_pointer : std::false_type {};
247 template<typename T> struct serialize_as_pointer_impl : std::true_type
248 {
249  // pointer to primitive types not supported
250  static_assert(!is_primitive<T>::value,
251  "can't serialize ptr to primitive type");
252  using type = T;
253 };
254 template<typename T> struct serialize_as_pointer<T*>
256 {
257  static inline T* getPointer(T* t) { return t; }
258  template<typename Archive>
259  static inline void setPointer(T*& t, T* p, Archive& /*ar*/) {
260  t = p;
261  }
262 };
263 template<typename T> struct serialize_as_pointer<std::unique_ptr<T>>
265 {
266  static inline T* getPointer(const std::unique_ptr<T>& t) { return t.get(); }
267  template<typename Archive>
268  static inline void setPointer(std::unique_ptr<T>& t, T* p, Archive& /*ar*/) {
269  t.reset(p);
270  }
271 };
272 template<typename T> struct serialize_as_pointer<std::shared_ptr<T>>
274 {
275  static T* getPointer(const std::shared_ptr<T>& t) { return t.get(); }
276  template<typename Archive>
277  static void setPointer(std::shared_ptr<T>& t, T* p, Archive& ar) {
278  ar.resetSharedPtr(t, p);
279  }
280 };
281 
283 
284 // serialize_as_collection<T>
285 //
286 // Type-trait class that indicates whether a certain type should be serialized
287 // as a collection or not. The serialization code 'knows' how to serialize
288 // collections, so as a user of the serializer you only need to list the
289 // collection to have it serialized (you don't have to iterate over it
290 // manually).
291 //
292 // By default arrays, std::vector, std::list, std::deque and std::map are
293 // recognized as collections. Though for STL collections you need to add
294 // #include "serialize_stl.hh"
295 //
296 // The serialize_as_collection class has the following members:
297 //
298 // - static bool value
299 // True iff this type must be serialized as a collection.
300 // The fields below are only valid (even only present) if this variable
301 // is true.
302 // - int size
303 // The size of the collection, -1 for variable sized collections.
304 // Fixed sized collections can be serialized slightly more efficient
305 // because we don't need to explicitly store the size.
306 // - using value_type = ...
307 // The type stored in the collection (only homogeneous collections are
308 // supported).
309 // - bool loadInPlace
310 // Indicates whether we can directly load the elements in the correct
311 // position in the container, otherwise there will be an extra assignment.
312 // For this to be possible, the output iterator must support a dereference
313 // operator that returns a 'regular' value_type.
314 // - using const_iterator = ...
315 // - const_iterator begin(...)
316 // - const_iterator end(...)
317 // Returns begin/end iterator for the given collection. Used for saving.
318 // - void prepare(..., int n)
319 // - output_iterator output(...)
320 // These are used for loading. The prepare() method should prepare the
321 // collection to receive 'n' elements. The output() method returns an
322 // output_iterator to the beginning of the collection.
323 
324 template<typename T> struct serialize_as_collection : std::false_type {};
325 template<typename T, int N> struct serialize_as_collection<T[N]> : std::true_type
326 {
327  static constexpr int size = N; // fixed size
328  using value_type = T;
329  // save
330  using const_iterator = const T*;
331  static const T* begin(const T (&array)[N]) { return &array[0]; }
332  static const T* end (const T (&array)[N]) { return &array[N]; }
333  // load
334  static constexpr bool loadInPlace = true;
335  static void prepare(T (&/*array*/)[N], int /*n*/) { }
336  static T* output(T (&array)[N]) { return &array[0]; }
337 };
338 
340 
341 // Implementation of the different save-strategies.
342 //
343 // ATM we have
344 // - PrimitiveSaver
345 // Saves primitive values: int, bool, string, ...
346 // Primitive values cannot be versioned.
347 // - EnumSaver
348 // Depending on the archive type, enums are either saved as strings (XML
349 // archive) or as integers (memory archive).
350 // This does not work automatically: it needs a specialization of
351 // serialize_as_enum, see above.
352 // - ClassSaver
353 // From a serialization POV classes are a (heterogeneous) collection of
354 // other to-be-serialized items.
355 // Classes can have a version number, this allows to evolve the class
356 // structure while still being able to load older versions (the load
357 // method receive the version number as parameter, the user still needs
358 // to keep the old loading code in place).
359 // Optionally the name of the (concrete) class is saved in the stream.
360 // This is used to support loading of polymorphic classes.
361 // There is also an (optional) id saved in the stream. This used to
362 // resolve possible multiple pointers to the same class.
363 // - PointerSaver
364 // Saves a pointer to a class (pointers to primitive types are not
365 // supported). See also serialize_as_pointer
366 // - IDSaver
367 // Weaker version of PointerSaver: it can only save pointers to objects
368 // that are already saved before (so it's will be saved by storing a
369 // reference). To only reason to use IDSaver (iso PointerSaver) is that
370 // it will not instantiate the object construction code.
371 // - CollectionSaver
372 // Saves a whole collection. See also serialize_as_collection
373 //
374 // All these strategies have a method:
375 // template<typename Archive> void operator()(Archive& ar, const T& t)
376 // 'ar' is archive where the serialized stream will go
377 // 't' is the to-be-saved object
378 // 'saveId' Should ID be saved
379 
380 template<typename T> struct PrimitiveSaver
381 {
382  template<typename Archive> void operator()(Archive& ar, const T& t,
383  bool /*saveId*/)
384  {
385  static_assert(is_primitive<T>::value, "must be primitive type");
386  ar.save(t);
387  }
388 };
389 template<typename T> struct EnumSaver
390 {
391  template<typename Archive> void operator()(Archive& ar, const T& t,
392  bool /*saveId*/)
393  {
395  saveEnum<Archive>(sae.info, t,
396  [&](const auto& s) { ar.save(s); });
397  }
398 };
399 template<typename T> struct ClassSaver
400 {
401  template<typename Archive> void operator()(
402  Archive& ar, const T& t, bool saveId,
403  const char* type = nullptr, bool saveConstrArgs = false)
404  {
405  // Order is important (for non-xml archives). We use this order:
406  // - id
407  // - type
408  // - version
409  // - constructor args
410  // Rational:
411  // - 'id' must be first: it could be nullptr, in that
412  // case the other fields are not even present.
413  // - 'type' must be before version, because for some types we
414  // might not need to store version (though it's unlikely)
415  // - version must be before constructor args because the
416  // constr args depend on the version
417  if (saveId) {
418  unsigned id = ar.generateId(&t);
419  ar.attribute("id", id);
420  }
421 
422  if (type) {
423  ar.attribute("type", type);
424  }
425 
426  unsigned version = SerializeClassVersion<T>::value;
427  if ((version != 0) && ar.NEED_VERSION) {
428  if (!ar.CAN_HAVE_OPTIONAL_ATTRIBUTES ||
429  (version != 1)) {
430  ar.attribute("version", version);
431  }
432  }
433 
434  if (saveConstrArgs) {
435  // save local constructor args (if any)
436  SerializeConstructorArgs<T> constrArgs;
437  constrArgs.save(ar, t);
438  }
439 
440  using TNC = std::remove_const_t<T>;
441  auto& t2 = const_cast<TNC&>(t);
442  serialize(ar, t2, version);
443  }
444 };
445 template<typename TP> struct PointerSaver
446 {
447  // note: we only support pointer to class
448  template<typename Archive> void operator()(Archive& ar, const TP& tp2,
449  bool /*saveId*/)
450  {
451  static_assert(serialize_as_pointer<TP>::value,
452  "must be serialized as pointer");
453  using T = typename serialize_as_pointer<TP>::type;
454  const T* tp = serialize_as_pointer<TP>::getPointer(tp2);
455  if (!tp) {
456  unsigned id = 0;
457  ar.attribute("id_ref", id);
458  return;
459  }
460  if (unsigned id = ar.getId(tp)) {
461  ar.attribute("id_ref", id);
462  } else {
463  if constexpr (std::is_polymorphic_v<T>) {
465  } else {
466  ClassSaver<T> saver;
467  // don't store type
468  // store id, constr-args
469  saver(ar, *tp, true, nullptr, true);
470  }
471  }
472  }
473 };
474 template<typename TP> struct IDSaver
475 {
476  template<typename Archive> void operator()(Archive& ar, const TP& tp2)
477  {
478  static_assert(serialize_as_pointer<TP>::value,
479  "must be serialized as pointer");
481  unsigned id;
482  if (!tp) {
483  id = 0;
484  } else {
485  id = ar.getId(tp);
486  assert(id);
487  }
488  ar.attribute("id_ref", id);
489  }
490 };
491 template<typename TC> struct CollectionSaver
492 {
493  template<typename Archive> void operator()(Archive& ar, const TC& tc,
494  bool saveId)
495  {
496  using sac = serialize_as_collection<TC>;
497  static_assert(sac::value, "must be serialized as collection");
498  auto begin = sac::begin(tc);
499  auto end = sac::end (tc);
500  if ((sac::size < 0) && (!ar.CAN_COUNT_CHILDREN)) {
501  // variable size
502  // e.g. in an XML archive the loader can look-ahead and
503  // count the number of sub-tags, so no need to
504  // explicitly store the size for such archives.
505  int n = int(std::distance(begin, end));
506  ar.serialize("size", n);
507  }
508  for (; begin != end; ++begin) {
509  if (saveId) {
510  ar.serializeWithID("item", *begin);
511  } else {
512  ar.serialize("item", *begin);
513  }
514  }
515  }
516 };
517 
518 // Delegate to a specific Saver class
519 // (implemented as inheriting from a specific baseclass).
520 template<typename T> struct Saver
521  : std::conditional_t<is_primitive<T>::value, PrimitiveSaver<T>,
522  std::conditional_t<Serializer<T>::value, typename Serializer<T>::Saver,
523  std::conditional_t<serialize_as_enum<T>::value, EnumSaver<T>,
524  std::conditional_t<serialize_as_pointer<T>::value, PointerSaver<T>,
525  std::conditional_t<serialize_as_collection<T>::value, CollectionSaver<T>,
526  ClassSaver<T>>>>>> {};
527 
529 
530 // Implementation of the different load-strategies.
531 //
532 // This matches very closely with the save-strategies above.
533 //
534 // All these strategies have a method:
535 // template<typename Archive, typename TUPLE>
536 // void operator()(Archive& ar, const T& t, TUPLE args)
537 // 'ar' Is archive where the serialized stream will go
538 // 't' Is the object that has to be restored.
539 // In case of a class (not a pointer to a class) the actual object
540 // is already constructed, but it still needs to be filled in with
541 // the correct data.
542 // 'args' (Only used by PointerLoader) holds extra parameters used
543 // to construct objects.
544 // 'id' Used to skip loading an ID, see comment in ClassLoader
545 
546 template<typename T> struct PrimitiveLoader
547 {
548  template<typename Archive, typename TUPLE>
549  void operator()(Archive& ar, T& t, TUPLE /*args*/, int /*id*/)
550  {
551  static_assert(std::tuple_size_v<TUPLE> == 0,
552  "can't have constructor arguments");
553  ar.load(t);
554  }
555 };
556 template<typename T> struct EnumLoader
557 {
558  template<typename Archive, typename TUPLE>
559  void operator()(Archive& ar, T& t, TUPLE /*args*/, int /*id*/)
560  {
561  static_assert(std::tuple_size_v<TUPLE> == 0,
562  "can't have constructor arguments");
564  loadEnum<Archive>(sae.info, t, [&](auto& l) { ar.load(l); });
565  }
566 };
567 
568 unsigned loadVersionHelper(MemInputArchive& ar, const char* className,
569  unsigned latestVersion);
570 unsigned loadVersionHelper(XmlInputArchive& ar, const char* className,
571  unsigned latestVersion);
572 template<typename T, typename Archive> unsigned loadVersion(Archive& ar)
573 {
574  unsigned latestVersion = SerializeClassVersion<T>::value;
575  if ((latestVersion != 0) && ar.NEED_VERSION) {
576  return loadVersionHelper(ar, typeid(T).name(), latestVersion);
577  } else {
578  return latestVersion;
579  }
580 }
581 template<typename T> struct ClassLoader
582 {
583  template<typename Archive, typename TUPLE>
584  void operator()(Archive& ar, T& t, TUPLE /*args*/, int id = 0,
585  int version = -1)
586  {
587  static_assert(std::tuple_size_v<TUPLE> == 0,
588  "can't have constructor arguments");
589 
590  // id == -1: don't load id, don't addPointer
591  // id == 0: load id from archive, addPointer
592  // id == N: id already loaded, still addPointer
593  if (id != -1) {
594  if (id == 0) {
595  ar.attribute("id", id);
596  }
597  ar.addPointer(id, &t);
598  }
599 
600  // version == -1: load version
601  // version == N: version already loaded
602  if (version == -1) {
603  version = loadVersion<T>(ar);
604  }
605 
606  using TNC = std::remove_const_t<T>;
607  auto& t2 = const_cast<TNC&>(t);
608  serialize(ar, t2, version);
609  }
610 };
611 template<typename T> struct NonPolymorphicPointerLoader
612 {
613  template<typename Archive, typename GlobalTuple>
614  T* operator()(Archive& ar, unsigned id, GlobalTuple globalArgs)
615  {
616  int version = loadVersion<T>(ar);
617 
618  // load (local) constructor args (if any)
619  using TNC = std::remove_const_t<T>;
620  using ConstrArgs = SerializeConstructorArgs<TNC>;
621  ConstrArgs constrArgs;
622  auto localArgs = constrArgs.load(ar, version);
623 
624  // combine global and local constr args
625  auto args = std::tuple_cat(globalArgs, localArgs);
626  // TODO make combining global/local constr args configurable
627 
628  Creator<T> creator;
629  auto tp = creator(args);
630  ClassLoader<T> loader;
631  loader(ar, *tp, std::tuple<>(), id, version);
632  return tp.release();
633  }
634 };
635 template<typename T> struct PolymorphicPointerLoader
636 {
637  template<typename Archive, typename TUPLE>
638  T* operator()(Archive& ar, unsigned id, TUPLE args)
639  {
640  using ArgsType = typename PolymorphicConstructorArgs<T>::type;
641  static_assert(std::is_same_v<TUPLE, ArgsType>,
642  "constructor arguments types must match");
643  return static_cast<T*>(
645  }
646 };
647 template<typename T> struct PointerLoader2
648  // extra indirection needed because inlining the body of
649  // NonPolymorphicPointerLoader in PointerLoader does not compile
650  // for abstract types
651  : std::conditional_t<std::is_polymorphic_v<T>,
652  PolymorphicPointerLoader<T>,
653  NonPolymorphicPointerLoader<T>> {};
654 
655 template<typename TP> struct PointerLoader
656 {
657  template<typename Archive, typename GlobalTuple>
658  void operator()(Archive& ar, TP& tp2, GlobalTuple globalArgs, int /*id*/)
659  {
660  static_assert(serialize_as_pointer<TP>::value,
661  "must be serialized as a pointer");
662  // in XML archives we use 'id_ref' or 'id', in other archives
663  // we don't care about the name
664  unsigned id = [&] {
665  if constexpr (Archive::CAN_HAVE_OPTIONAL_ATTRIBUTES) {
666  if (auto i = ar.template findAttributeAs<unsigned>("id_ref")) {
667  return *i;
668  }
669  }
670  unsigned i;
671  ar.attribute("id", i);
672  return i;
673  }();
674 
675  using T = typename serialize_as_pointer<TP>::type;
676  T* tp = [&]() -> T* {
677  if (id == 0) {
678  return nullptr;
679  } else {
680  if (void* p = ar.getPointer(id)) {
681  return static_cast<T*>(p);
682  } else {
683  PointerLoader2<T> loader;
684  return loader(ar, id, globalArgs);
685  }
686  }
687  }();
689  }
690 };
691 void pointerError(unsigned id);
692 template<typename TP> struct IDLoader
693 {
694  template<typename Archive>
695  void operator()(Archive& ar, TP& tp2)
696  {
697  static_assert(serialize_as_pointer<TP>::value,
698  "must be serialized as a pointer");
699  unsigned id;
700  ar.attribute("id_ref", id);
701 
702  using T = typename serialize_as_pointer<TP>::type;
703  T* tp;
704  if (id == 0) {
705  tp = nullptr;
706  } else {
707  void* p = ar.getPointer(id);
708  if (!p) {
709  pointerError(id);
710  }
711  tp = static_cast<T*>(p);
712  }
714  }
715 };
716 
717 template<typename sac, bool IN_PLACE = sac::loadInPlace> struct CollectionLoaderHelper;
718 template<typename sac> struct CollectionLoaderHelper<sac, true>
719 {
720  // used for array and vector
721  template<typename Archive, typename TUPLE, typename OUT_ITER>
722  void operator()(Archive& ar, TUPLE args, OUT_ITER it, int id)
723  {
724  ar.doSerialize("item", *it, args, id);
725  }
726 };
727 template<typename sac> struct CollectionLoaderHelper<sac, false>
728 {
729  // We can't directly load the element in the correct position:
730  // This screws-up id/pointer management because the element is still
731  // copied after construction (and pointer value of initial object is
732  // stored).
733  template<typename Archive, typename TUPLE, typename OUT_ITER>
734  void operator()(Archive& ar, TUPLE args, OUT_ITER it, int id)
735  {
736  typename sac::value_type elem;
737  ar.doSerialize("item", elem, args, id);
738  *it = std::move(elem);
739  }
740 };
741 template<typename TC> struct CollectionLoader
742 {
743  template<typename Archive, typename TUPLE>
744  void operator()(Archive& ar, TC& tc, TUPLE args, int id = 0)
745  {
746  assert(id == one_of(0, -1));
747  using sac = serialize_as_collection<TC>;
748  static_assert(sac::value, "must be serialized as a collection");
749  int n = sac::size;
750  if (n < 0) {
751  // variable size
752  if constexpr (Archive::CAN_COUNT_CHILDREN) {
753  n = ar.countChildren();
754  } else {
755  ar.serialize("size", n);
756  }
757  }
758  sac::prepare(tc, n);
759  auto it = sac::output(tc);
760  CollectionLoaderHelper<sac> loadOneElement;
761  repeat(n, [&] {
762  loadOneElement(ar, args, it, id);
763  ++it;
764  });
765  }
766 };
767 template<typename T> struct Loader
768  : std::conditional_t<is_primitive<T>::value, PrimitiveLoader<T>,
769  std::conditional_t<Serializer<T>::value, typename Serializer<T>::Loader,
770  std::conditional_t<serialize_as_enum<T>::value, EnumLoader<T>,
771  std::conditional_t<serialize_as_pointer<T>::value, PointerLoader<T>,
772  std::conditional_t<serialize_as_collection<T>::value, CollectionLoader<T>,
773  ClassLoader<T>>>>>> {};
774 
775 } // namespace openmsx
776 
777 #endif
uintptr_t id
Definition: Interpreter.cc:26
TclObject t
Definition: one_of.hh:7
static void * load(Archive &ar, unsigned id, const void *args)
static void save(Archive &ar, T *t)
constexpr double e
Definition: Math.hh:18
SDLSurfacePtr load(const std::string &filename, bool want32bpp)
Load the given PNG file in a SDL_Surface.
Definition: PNG.cc:95
This file implemented 3 utility functions:
Definition: Autofire.cc:9
Variant defaultConstructVariant(size_t index)
unsigned loadVersion(Archive &ar)
constexpr unsigned N
Definition: ResampleHQ.cc:226
auto visit(Visitor &&visitor, const Event &event)
Definition: Event.hh:652
T fromString(std::initializer_list< enum_string< T >> list, std::string_view str_)
void serialize(Archive &ar, T &t, unsigned version)
void pointerError(unsigned id)
unsigned loadVersionHelper(MemInputArchive &, const char *, unsigned)
void enumError(const std::string_view str)
void saveEnum(std::initializer_list< enum_string< T >> list, T t, SaveAction save)
std::string toString(const Event &event)
Get a string representation of this event.
Definition: Event.cc:180
void loadEnum(std::initializer_list< enum_string< T >> list, T &t, LoadAction load)
size_t size(std::string_view utf8)
auto distance(octet_iterator first, octet_iterator last)
void operator()(Archive &ar, T &t, TUPLE, int id=0, int version=-1)
void operator()(Archive &ar, const T &t, bool saveId, const char *type=nullptr, bool saveConstrArgs=false)
void operator()(Archive &ar, TUPLE args, OUT_ITER it, int id)
void operator()(Archive &ar, TUPLE args, OUT_ITER it, int id)
void operator()(Archive &ar, TC &tc, TUPLE args, int id=0)
void operator()(Archive &ar, const TC &tc, bool saveId)
Utility to do T* t = new T(...)
Variant operator()(size_t index) const
void operator()(Archive &ar, T &t, TUPLE, int)
void operator()(Archive &ar, const T &t, bool)
void operator()(Archive &ar, TP &tp2)
void operator()(Archive &ar, const TP &tp2)
T * operator()(Archive &ar, unsigned id, GlobalTuple globalArgs)
void operator()(Archive &ar, TP &tp2, GlobalTuple globalArgs, int)
void operator()(Archive &ar, const TP &tp2, bool)
Store association between polymorphic class (base- or subclass) and the list of constructor arguments...
T * operator()(Archive &ar, unsigned id, TUPLE args)
void operator()(Archive &ar, T &t, TUPLE, int)
void operator()(Archive &ar, const T &t, bool)
Store serialization-version number of a class.
static constexpr unsigned value
Serialize (local) constructor arguments.
type load(Archive &, unsigned)
void save(Archive &, const T &)
void operator()(Archive &ar, V &v, TUPLE args, int id)
void operator()(Archive &ar, const V &v, bool saveId)
static constexpr size_t index
static const T * begin(const T(&array)[N])
static const T * end(const T(&array)[N])
serialize_as_enum<T>
static void setPointer(T *&t, T *p, Archive &)
static T * getPointer(const std::shared_ptr< T > &t)
static void setPointer(std::shared_ptr< T > &t, T *p, Archive &ar)
static T * getPointer(const std::unique_ptr< T > &t)
static void setPointer(std::unique_ptr< T > &t, T *p, Archive &)
#define UNREACHABLE
Definition: unreachable.hh:38
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
Definition: xrange.hh:148
constexpr auto begin(const zstring_view &x)
constexpr auto end(const zstring_view &x)