Skip to content

Commit eaadae1

Browse files
committed
Update serialize.hpp
:)
1 parent 9b8ef92 commit eaadae1

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/include/sndx/data/serialize.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ namespace sndx {
8484
serializeToAdjust(it, bytes);
8585
}
8686
};
87+
88+
template<>
89+
struct Serializer<size_t> {
90+
template <class SerializeIt>
91+
constexpr void serialize(size_t value, SerializeIt& it) const {
92+
static_assert(sizeof(size_t) <= sizeof(uint64_t));
93+
94+
value = utility::fromEndianess<std::endian::little>(value);
95+
96+
auto bytes = std::bit_cast<std::array<std::byte, sizeof(size_t)>>(value);
97+
98+
serializeToAdjust(it, bytes);
99+
100+
if constexpr (sizeof(size_t) != sizeof(uint64_t)) {
101+
for (size_t i = 0; i < sizeof(uint64_t) - sizeof(size_t); ++i) {
102+
serializeToAdjust(it, '\0');
103+
}
104+
}
105+
}
106+
};
87107
}
88108

89109
namespace sndx {
@@ -170,4 +190,29 @@ namespace sndx {
170190
to = utility::fromEndianess<std::endian::little>(value);
171191
}
172192
};
193+
194+
template <>
195+
struct Deserializer<size_t> {
196+
template <class DeserializeIt>
197+
constexpr void deserialize(size_t& to, DeserializeIt& in, DeserializeIt end) const {
198+
using BufferT = std::array<uint8_t, sizeof(size_t)>;
199+
200+
BufferT buffer{};
201+
deserializeFromAdjust(buffer, in, end);
202+
auto value = std::bit_cast<size_t>(buffer);
203+
204+
to = utility::fromEndianess<std::endian::little>(value);
205+
206+
if constexpr (sizeof(size_t) != sizeof(uint64_t)) {
207+
std::array<uint8_t, sizeof(uint64_t) - sizeof(size_t)> buf{};
208+
deserializeFromAdjust(buf, in, end);
209+
210+
for (auto v : buf) {
211+
if (v != 0) {
212+
throw bad_field_error("Narrowing 64-bit size_t to 32-bit size_t discarded data!");
213+
}
214+
}
215+
}
216+
}
217+
};
173218
}

0 commit comments

Comments
 (0)