-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathIntegerSerializer.hpp
More file actions
67 lines (55 loc) · 2.33 KB
/
IntegerSerializer.hpp
File metadata and controls
67 lines (55 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef ARK_COMPILER_SERIALIZATION_INTEGERSERIALIZER_HPP
#define ARK_COMPILER_SERIALIZATION_INTEGERSERIALIZER_HPP
#include <span>
#include <vector>
#include <concepts>
namespace Ark::internal
{
void serializeToVecLE(std::integral auto number, std::vector<uint8_t>& out)
{
constexpr auto mask = static_cast<decltype(number)>(0xff);
for (std::size_t i = 0; i < sizeof(decltype(number)); ++i)
out.push_back(static_cast<uint8_t>((number & (mask << (8 * i))) >> (8 * i)));
}
void serializeToVecBE(std::integral auto number, std::vector<uint8_t>& out)
{
constexpr auto pad = sizeof(decltype(number)) - 1;
constexpr auto mask = static_cast<decltype(number)>(0xff);
for (std::size_t i = 0; i < sizeof(decltype(number)); ++i)
{
const auto shift = 8 * (pad - i);
out.push_back(static_cast<uint8_t>((number & (mask << shift)) >> shift));
}
}
void serializeOn2BytesToVecLE(std::integral auto number, std::vector<uint8_t>& out)
{
constexpr auto mask = static_cast<decltype(number)>(0xff);
out.push_back(static_cast<uint8_t>(number & mask));
out.push_back(static_cast<uint8_t>((number & (mask << 8)) >> 8));
}
void serializeOn2BytesToVecBE(std::integral auto number, std::vector<uint8_t>& out)
{
constexpr auto mask = static_cast<decltype(number)>(0xff);
out.push_back(static_cast<uint8_t>((number & (mask << 8)) >> 8));
out.push_back(static_cast<uint8_t>(number & mask));
}
template <std::integral T>
T deserializeLE(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end)
{
constexpr std::size_t length = sizeof(T);
T result {};
for (std::size_t i = 0; i < length && begin != end; ++i, ++begin)
result += static_cast<T>(*begin) << (8 * i);
return result;
}
template <std::integral T>
T deserializeBE(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end)
{
constexpr std::size_t length = sizeof(T);
T result {};
for (std::size_t i = 0; i < length && begin != end; ++i, ++begin)
result += static_cast<T>(*begin) << (8 * (length - i - 1));
return result;
}
}
#endif // ARK_COMPILER_SERIALIZATION_INTEGERSERIALIZER_HPP