31 #include <unordered_map>
46 any = std::move(value);
62 static const std::unordered_map
72 {std::type_index(
typeid(
bool)), {
"bool", &serialize_any<bool>}},
73 {std::type_index(
typeid(std::uint8_t)), {
"uint8", &serialize_any<std::uint8_t>}},
74 {std::type_index(
typeid(std::uint16_t)), {
"uint16", &serialize_any<std::uint16_t>}},
75 {std::type_index(
typeid(std::uint32_t)), {
"uint32", &serialize_any<std::uint32_t>}},
76 {std::type_index(
typeid(std::uint64_t)), {
"uint64", &serialize_any<std::uint64_t>}},
77 {std::type_index(
typeid(std::int8_t)), {
"int8", &serialize_any<std::int8_t>}},
78 {std::type_index(
typeid(std::int16_t)), {
"int16", &serialize_any<std::int16_t>}},
79 {std::type_index(
typeid(std::int32_t)), {
"int32", &serialize_any<std::int32_t>}},
80 {std::type_index(
typeid(std::int64_t)), {
"int64", &serialize_any<std::int64_t>}},
81 {std::type_index(
typeid(
float)), {
"float", &serialize_any<float>}},
82 {std::type_index(
typeid(
double)), {
"double", &serialize_any<double>}},
83 {std::type_index(
typeid(std::string)), {
"string", &serialize_any<std::string>}},
84 {std::type_index(
typeid(std::u8string)), {
"u8string", &serialize_any<std::u8string>}},
85 {std::type_index(
typeid(std::u16string)), {
"u16string", &serialize_any<std::u16string>}},
86 {std::type_index(
typeid(std::u32string)), {
"u32string", &serialize_any<std::u32string>}}
90 std::uint64_t size =
static_cast<std::uint64_t
>(
dict.size());
91 ctx.
write64<std::endian::big>(
reinterpret_cast<const std::byte*
>(&size), 1);
94 for (
const auto& [key, value]:
dict)
96 if (
auto i = type_map.find(value.type()); i != type_map.end())
98 const auto& [type_hash, type_serializer] = i->second;
101 ctx.
write32<std::endian::big>(
reinterpret_cast<const std::byte*
>(&type_hash), 1);
102 ctx.
write32<std::endian::big>(
reinterpret_cast<const std::byte*
>(&key), 1);
105 type_serializer(value, ctx);
127 static const std::unordered_map
133 {
"bool", &deserialize_any<bool>},
134 {
"uint8", &deserialize_any<std::uint8_t>},
135 {
"uint16", &deserialize_any<std::uint16_t>},
136 {
"uint32", &deserialize_any<std::uint32_t>},
137 {
"uint64", &deserialize_any<std::uint64_t>},
138 {
"int8", &deserialize_any<std::int8_t>},
139 {
"int16", &deserialize_any<std::int16_t>},
140 {
"int32", &deserialize_any<std::int32_t>},
141 {
"int64", &deserialize_any<std::int64_t>},
142 {
"float", &deserialize_any<float>},
143 {
"double", &deserialize_any<double>},
144 {
"string", &deserialize_any<std::string>},
145 {
"u8string", &deserialize_any<std::u8string>},
146 {
"u16string", &deserialize_any<std::u16string>},
147 {
"u32string", &deserialize_any<std::u32string>}
153 std::uint64_t size = 0;
154 ctx.
read64<std::endian::big>(
reinterpret_cast<std::byte*
>(&size), 1);
157 for (std::size_t i = 0; i < size; ++i)
160 std::uint32_t type_hash = 0;
161 ctx.
read32<std::endian::big>(
reinterpret_cast<std::byte*
>(&type_hash), 1);
163 if (
auto i = type_map.find(type_hash); i != type_map.end())
167 ctx.
read32<std::endian::big>(
reinterpret_cast<std::byte*
>(&key), 1);
170 i->second(
dict[key], ctx);
182 auto resource = std::make_unique<dict<hash::fnv1a32_t>>();
An exception of this type is thrown when an error occurs during deserialization.
Templated resource loader.
Manages the loading, caching, and saving of resources.
An exception of this type is thrown when an error occurs during serialization.
std::unordered_map< Key, std::any > dict
Unordered dictionary type.
User-defined literals for compile-time string hashing.
constexpr bool any(const vector< bool, N > &x) noexcept
Checks if any elements of a boolean vector are true.
Provides access to a deserialization state.
std::size_t read32(std::byte *data, std::size_t count) noexcept(false)
Reads 32-bit (double word) data.
std::size_t read64(std::byte *data, std::size_t count) noexcept(false)
Reads 64-bit (quad word) data.
Specializations of deserializer define the deserialization process for a given type.
void deserialize(T &value, deserialize_context &ctx)
Deserializes a value.
32-bit FNV-1a hash value.
Provides access to a serialization state.
std::size_t write32(const std::byte *data, std::size_t count) noexcept(false)
Writes 32-bit (double word) data.
std::size_t write64(const std::byte *data, std::size_t count) noexcept(false)
Writes 64-bit (quad word) data.
Specializations of serializer define the serialization process for a given type.
void serialize(const T &value, serialize_context &ctx)
Serializes a value.