-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstexprSerializerBuffer.cpp
103 lines (85 loc) · 1.79 KB
/
ConstexprSerializerBuffer.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Kobe Vrijsen 2022
// Licensed under the EUPL-1.2-or-later
#include "ConstexprSerializerBuffer.h"
#include <string>
#include <vector>
static_assert(
[]
{
Serializer<8> io{};
io.write(13);
return io.read<int>() == 13;
}
(),
"Integer serialization"
);
static_assert(
[] {
Serializer io(16); // do not call initialiser list as it will allocate a single size_t
io.write(13l);
io.write(27ll);
return io.read<long>() == 13l
&& io.read<long long>() == 27ll;
}(),
"dynamic buffer"
);
static_assert(
[] {
return Serializer{ "\xDE\xAF\xFA\xDE" }.read<unsigned long>() == (
std::endian::native == std::endian::big
? 0xDEAFFADEul : 0xDEFAAFDEul
);
} (),
"Reinterpreting integers"
);
static_assert(
[]
{
std::wstring wstr{ L"Hello world" };
std::vector<short> nums(wstr.size(), {});
Serializer{ wstr }.read(nums); // From string to vector
Serializer{ nums }.read(wstr); // Vice-versa
return wstr == L"Hello world";
}
(),
"String serialization"
);
static_assert(
[]
{
struct Data
{
bool b;
char c;
float f;
constexpr bool operator == (Data const& other) const
{
return b == other.b && c == other.c && f == other.f;
}
};
using Container = std::vector<Data>;
Serializer<64> io{};
Container container_origin{
{false, 'a', 2.47f },
{true , 'b', 3.14f }
};
{
io.write(std::ranges::size(container_origin));
io.write(container_origin);
}
Container container_replica{};
{
std::ranges::generate_n(
std::inserter(container_replica, std::ranges::end(container_replica)),
io.read<Container::size_type>(),
[&io] ()
{
return io.read<Container::value_type>();
}
);
}
return std::ranges::equal(container_origin, container_replica);
}
(),
"vector and struct serialization"
);