-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.hpp
150 lines (129 loc) · 3.67 KB
/
serialization.hpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef BOMBERMAN_SERIALIZATION_HPP
#define BOMBERMAN_SERIALIZATION_HPP
#include <limits>
#include <variant>
#include "boost/pfr.hpp"
#include "messages.hpp"
class invalid_message : public std::runtime_error {
using std::runtime_error::runtime_error;
};
template <typename T>
concept is_class = std::is_class<T>::value;
streamable_buffer& operator<<(streamable_buffer& stream, const std::string& s) {
if (s.size() > std::numeric_limits<uint8_t>::max()) {
throw invalid_message("string too long");
}
stream << (uint8_t) s.size();
for (uint8_t c : s) { stream << c; };
return stream;
}
template <typename T>
streamable_buffer& operator<<(streamable_buffer& stream, const std::vector<T>& s) {
if (s.size() > std::numeric_limits<uint32_t>::max()) {
throw invalid_message("vector too long");
}
stream << (uint32_t)s.size();
for (const T& c : s) { stream << c; };
return stream;
}
template <typename T, typename U>
streamable_buffer& operator<<(streamable_buffer& stream, const std::map<T, U>& s) {
if (s.size() > std::numeric_limits<uint32_t>::max()) {
throw invalid_message("map too long");
}
stream << (uint32_t)s.size();
for (const auto& [key, val] : s) { stream << key << val; };
return stream;
}
template <typename ... Ts>
streamable_buffer& operator<<(streamable_buffer& stream, std::variant<Ts ...>& variant) {
std::visit([&stream](auto&& x){ stream << x; }, variant);
return stream;
}
template <is_class Compound>
streamable_buffer& operator<<(streamable_buffer& stream, Compound obj) {
if constexpr (requires {obj.msg_id;}) {
stream << obj.msg_id;
}
boost::pfr::for_each_field(
obj,
[&stream](const auto& elt) { stream << elt; }
);
return stream;
}
// unfortunately PFR doesn't support members with std::variant type
streamable_buffer& operator<<(streamable_buffer& stream, ServerMessageTurn& msg) {
stream << msg.msg_id << msg.turn;
for (const Event& e : msg.events) {
std::visit([&stream] (auto&& x) { stream << x; }, e);
}
return stream;
}
streamable_buffer& operator>>(streamable_buffer& stream, std::string& s) {
uint8_t size;
stream >> size;
s.clear();
for (size_t i=0; i < size; ++i) {
uint8_t c;
stream >> c;
s += c;
}
return stream;
}
template <typename T>
streamable_buffer& operator>>(streamable_buffer& stream, std::vector<T>& s) {
uint32_t size;
stream >> size;
s.clear();
for (size_t i=0; i < size; ++i) {
T c;
stream >> c;
s.push_back(c);
}
return stream;
}
template <typename T, typename U>
streamable_buffer& operator>>(streamable_buffer& stream, std::map<T, U>& s) {
uint32_t size;
stream >> size;
s.clear();
for (size_t i=0; i < size; ++i) {
T key;
U val;
stream >> key >> val;
s[key] = val;
}
return stream;
}
template <is_class Compound>
streamable_buffer& operator>>(streamable_buffer& stream, Compound& obj) {
obj = {};
boost::pfr::for_each_field(
obj,
[&stream](auto& elt) { stream >> elt; }
);
return stream;
}
template <typename Variant, size_t I = 0, typename Stream>
Variant get(Stream& s, size_t msg_id) {
if constexpr (I >= std::variant_size_v<Variant>) {
throw invalid_message("unknown message id");
} else if (I == msg_id) {
typename std::variant_alternative_t<I, Variant> obj;
boost::pfr::for_each_field(
obj,
[&s](auto& elt) { s >> elt; }
);
return obj;
} else {
return get<Variant, I + 1, Stream>(s, msg_id);
}
}
template <typename ... Ts>
streamable_buffer& operator>>(streamable_buffer& stream, std::variant<Ts ...>& variant) {
uint8_t msg_id;
stream >> msg_id;
variant = get<std::variant<Ts ...>>(stream, msg_id);
return stream;
}
#endif // BOMBERMAN_SERIALIZATION_HPP