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(Entry{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 return (*v)(ar,
id, args);
92 throw MSXException(
"Expected type: ", expected,
" but got: ", actual,
'.');
95template<
typename Archive>
102template<
typename Archive>
104 const char* name, InitFunction initializer)
106 assert(!initializerMap.contains(name));
107 initializerMap.emplace_noDuplicateCheck(name, std::move(initializer));
110template<
typename Archive>
112 const char* tag, Archive& ar,
void*
t)
116 ar.attribute(
"id",
id);
119 ar.attribute(
"type", type);
122 auto v =
lookup(reg.initializerMap, type);
124 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)
void save(size_t width, std::span< const void * > rowPointers, const PixelFormat &format, const std::string &filename)
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.