11template<
typename Archive>
18template<
typename Archive>
20 const std::type_info& type, SaveFunction saver)
23 assert(!
contains(saverMap, type, &Entry::index));
24 saverMap.emplace_back(type, std::move(saver));
27template<
typename Archive>
29 Archive& ar,
const void*
t,
const std::type_info& typeInfo)
32 if (!reg.initialized) [[unlikely]] {
33 reg.initialized =
true;
36 auto s =
binary_find(reg.saverMap, std::type_index(typeInfo), {}, &Entry::index);
38 std::cerr <<
"Trying to save an unregistered polymorphic type: "
39 << typeInfo.name() <<
'\n';
40 assert(
false);
return;
44template<
typename Archive>
46 const char* tag, Archive& ar,
const void*
t,
const std::type_info& typeInfo)
49 save(ar,
t, typeInfo);
53template class PolymorphicSaverRegistry<MemOutputArchive>;
54template class PolymorphicSaverRegistry<XmlOutputArchive>;
58template<
typename Archive>
65template<
typename Archive>
67 const char* name, LoadFunction loader)
69 assert(!loaderMap.contains(name));
70 loaderMap.emplace_noDuplicateCheck(name, std::move(loader));
73template<
typename Archive>
75 Archive& ar,
unsigned id,
const void* args)
78 ar.attribute(
"type", type);
80 auto* v =
lookup(reg.loaderMap, type);
82 throw MSXException(
"Deserialize unknown polymorphic type: '", type,
"'.");
84 return (*v)(ar,
id, args);
94 throw MSXException(
"Expected type: ", expected,
" but got: ", actual,
'.');
97template<
typename Archive>
104template<
typename Archive>
106 const char* name, InitFunction initializer)
108 assert(!initializerMap.contains(name));
109 initializerMap.emplace_noDuplicateCheck(name, std::move(initializer));
112template<
typename Archive>
114 const char* tag, Archive& ar,
void*
t)
118 ar.attribute(
"id",
id);
121 ar.attribute(
"type", type);
124 auto v =
lookup(reg.initializerMap, type);
126 throw MSXException(
"Deserialize unknown polymorphic type: '", type,
"'.");
static void init(const char *tag, Archive &ar, void *t)
static PolymorphicInitializerRegistry & instance()
static PolymorphicLoaderRegistry & instance()
static void * load(Archive &ar, unsigned id, const void *args)
static PolymorphicSaverRegistry & instance()
static void save(Archive &ar, T *t)
const Value * lookup(const hash_map< Key, Value, Hasher, Equal > &map, const Key2 &key)
This file implemented 3 utility functions:
void polyInitError(const char *expected, const char *actual)
constexpr void sort(RandomAccessRange &&range)
auto * binary_find(ForwardRange &&range, const T &value, Compare comp={}, Proj proj={})
constexpr bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.