Antkeeper  0.0.1
string-map.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 #include <algorithm>
27 #include <utility>
28 
37 template <>
39 {
40  // Write number of entries
41  std::uint32_t size = static_cast<std::uint32_t>(map.size());
42  ctx.write32<std::endian::little>(reinterpret_cast<const std::byte*>(&size), 1);
43 
44  // Write entries
45  for (const auto& [key, value]: map)
46  {
47  // Write key
48  ctx.write32<std::endian::little>(reinterpret_cast<const std::byte*>(&key), 1);
49 
50  // Write string length
51  std::uint32_t length = static_cast<std::uint32_t>(value.length());
52  ctx.write32<std::endian::little>(reinterpret_cast<const std::byte*>(&length), 1);
53 
54  // Write string
55  ctx.write8(reinterpret_cast<const std::byte*>(value.data()), length);
56  }
57 }
58 
67 template <>
69 {
70  map.clear();
71 
72  // Read number of entries
73  std::uint32_t size = 0;
74  ctx.read32<std::endian::little>(reinterpret_cast<std::byte*>(&size), 1);
75 
76  // Read entries
77  for (std::uint32_t i = 0; i < size; ++i)
78  {
79  // Read key
80  hash::fnv1a32_t key;
81  ctx.read32<std::endian::little>(reinterpret_cast<std::byte*>(&key), 1);
82 
83  // Read string length
84  std::uint32_t length = 0;
85  ctx.read32<std::endian::little>(reinterpret_cast<std::byte*>(&length), 1);
86 
87  // Insert empty string into map
88  auto [iterator, inserted] = map.emplace
89  (
90  std::piecewise_construct,
91  std::forward_as_tuple(key),
92  std::forward_as_tuple(static_cast<std::size_t>(length), '\0')
93  );
94 
95  // Read string
96  ctx.read8(reinterpret_cast<std::byte*>(iterator->second.data()), length);
97  }
98 }
99 
100 template <>
102 {
103  auto resource = std::make_unique<i18n::string_map>();
104 
106 
107  return resource;
108 }
static std::unique_ptr< T > load(::resource_manager &resource_manager, deserialize_context &ctx)
Loads a resource.
Manages the loading, caching, and saving of resources.
std::unordered_map< hash::fnv1a32_t, std::string > string_map
Maps 32-bit keys to strings.
Definition: string-map.hpp:32
T length(const quaternion< T > &q)
Calculates the length of a quaternion.
Definition: quaternion.hpp:602
constexpr T map(T x, T from_min, T from_max, T to_min, T to_max) noexcept
Remaps a number from one range to another.
Definition: map.hpp:37
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.
virtual std::size_t read8(std::byte *data, std::size_t count) noexcept(false)=0
Reads 8-bit (byte) 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.
Definition: fnv1a.hpp:117
Provides access to a serialization state.
virtual std::size_t write8(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 8-bit (byte) data.
std::size_t write32(const std::byte *data, std::size_t count) noexcept(false)
Writes 32-bit (double word) data.
void serialize(const T &value, serialize_context &ctx)
Serializes a value.