Antkeeper  0.0.1
serializer.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 
21 #include <string>
22 
23 template <>
24 void serializer<bool>::serialize(const bool& value, serialize_context& ctx)
25 {
26  const std::uint8_t temp = (value) ? 1 : 0;
27  ctx.write8(reinterpret_cast<const std::byte*>(&temp), 1);
28 };
29 
30 template <>
31 void serializer<std::uint8_t>::serialize(const std::uint8_t& value, serialize_context& ctx)
32 {
33  ctx.write8(reinterpret_cast<const std::byte*>(&value), 1);
34 };
35 
36 template <>
37 void serializer<std::uint16_t>::serialize(const std::uint16_t& value, serialize_context& ctx)
38 {
39  ctx.write16<std::endian::big>(reinterpret_cast<const std::byte*>(&value), 1);
40 };
41 
42 template <>
43 void serializer<std::uint32_t>::serialize(const std::uint32_t& value, serialize_context& ctx)
44 {
45  ctx.write32<std::endian::big>(reinterpret_cast<const std::byte*>(&value), 1);
46 };
47 
48 template <>
49 void serializer<std::uint64_t>::serialize(const std::uint64_t& value, serialize_context& ctx)
50 {
51  ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&value), 1);
52 };
53 
54 template <>
55 void serializer<std::int8_t>::serialize(const std::int8_t& value, serialize_context& ctx)
56 {
57  ctx.write8(reinterpret_cast<const std::byte*>(&value), 1);
58 };
59 
60 template <>
61 void serializer<std::int16_t>::serialize(const std::int16_t& value, serialize_context& ctx)
62 {
63  ctx.write16<std::endian::big>(reinterpret_cast<const std::byte*>(&value), 1);
64 };
65 
66 template <>
67 void serializer<std::int32_t>::serialize(const std::int32_t& value, serialize_context& ctx)
68 {
69  ctx.write32<std::endian::big>(reinterpret_cast<const std::byte*>(&value), 1);
70 };
71 
72 template <>
73 void serializer<std::int64_t>::serialize(const std::int64_t& value, serialize_context& ctx)
74 {
75  ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&value), 1);
76 };
77 
78 template <>
79 void serializer<float>::serialize(const float& value, serialize_context& ctx)
80 {
81  ctx.write32<std::endian::big>(reinterpret_cast<const std::byte*>(&value), 1);
82 };
83 
84 template <>
85 void serializer<double>::serialize(const double& value, serialize_context& ctx)
86 {
87  ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&value), 1);
88 };
89 
90 template <>
91 void serializer<std::string>::serialize(const std::string& value, serialize_context& ctx)
92 {
93  const std::uint64_t length = static_cast<std::uint64_t>(value.length());
94  ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&length), 1);
95  ctx.write8(reinterpret_cast<const std::byte*>(value.data()), static_cast<std::size_t>(length));
96 };
97 
98 template <>
99 void serializer<std::u8string>::serialize(const std::u8string& value, serialize_context& ctx)
100 {
101  const std::uint64_t length = static_cast<std::uint64_t>(value.length());
102  ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&length), 1);
103  ctx.write8(reinterpret_cast<const std::byte*>(value.data()), static_cast<std::size_t>(length));
104 };
105 
106 template <>
107 void serializer<std::u16string>::serialize(const std::u16string& value, serialize_context& ctx)
108 {
109  const std::uint64_t length = static_cast<std::uint64_t>(value.length());
110  ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&length), 1);
111  ctx.write16<std::endian::big>(reinterpret_cast<const std::byte*>(value.data()), static_cast<std::size_t>(length));
112 };
113 
114 template <>
115 void serializer<std::u32string>::serialize(const std::u32string& value, serialize_context& ctx)
116 {
117  const std::uint64_t length = static_cast<std::uint64_t>(value.length());
118  ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&length), 1);
119  ctx.write32<std::endian::big>(reinterpret_cast<const std::byte*>(value.data()), static_cast<std::size_t>(length));
120 };
T length(const quaternion< T > &q)
Calculates the length of a quaternion.
Definition: quaternion.hpp:602
Provides access to a serialization state.
std::size_t write16(const std::byte *data, std::size_t count) noexcept(false)
Writes 16-bit (word) data.
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.
std::size_t write64(const std::byte *data, std::size_t count) noexcept(false)
Writes 64-bit (quad word) data.
void serialize(const T &value, serialize_context &ctx)
Serializes a value.