forked from miki151/keeperrl
/
serialization.h
345 lines (290 loc) · 9.48 KB
/
serialization.h
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#ifndef _SERIALIZATION
#define _SERIALIZATION
#define SUBCLASS(X) boost::serialization::make_nvp("Base", boost::serialization::base_object<X>(*this))
#define SERIALIZABLE(T) \
template void T::serialize(boost::archive::binary_iarchive&, unsigned); \
template void T::serialize(boost::archive::binary_oarchive&, unsigned);
// template void T::serialize(boost::archive::xml_iarchive&, unsigned);
// template void T::serialize(boost::archive::xml_oarchive&, unsigned);
#define REGISTER_TYPES(T) \
template void T::registerTypes(boost::archive::binary_iarchive&); \
template void T::registerTypes(boost::archive::binary_oarchive&);
// template void T::registerTypes(boost::archive::xml_iarchive&);
// template void T::registerTypes(boost::archive::xml_oarchive&);
#define REGISTER_TYPE(T, A)\
(T).register_type(static_cast<A*>(nullptr))
#ifndef RELEASE
//#define SERIALIZATION_DEBUG
#endif
#ifdef SERIALIZATION_DEBUG
#define SERIAL_CHECKER SerialChecker serialChecker
#define CHECK_SERIAL serialChecker.checkSerial();
#define SERIAL(X) X; SerialChecker::Check X##_Check = SerialChecker::Check(serialChecker,#X)
#define SERIAL2(X, Y) X = Y; SerialChecker::Check X##_Check = SerialChecker::Check(serialChecker,#X)
#define SERIAL3(X) SerialChecker::Check X##_Check = SerialChecker::Check(serialChecker,#X);
#define SVAR(X) boost::serialization::make_nvp(#X, checkSerial(X, X##_Check))
#else
#define SERIAL_CHECKER
#define CHECK_SERIAL
#define SERIAL(X) X
#define SERIAL2(X, Y) X = Y
#define SERIAL3(X)
#define SVAR(X) boost::serialization::make_nvp(#X, X)
#endif
#define SERIALIZATION_DECL(A) \
friend boost::serialization::access; \
A() {} \
template <class Archive> \
void serialize(Archive& ar, const unsigned int version); \
SERIAL_CHECKER
#define SERIALIZATION_CONSTRUCTOR(A) \
A() {} \
SERIAL_CHECKER
class Serialization {
public:
template <class Archive>
static void registerTypes(Archive& ar);
};
class SerialChecker {
public:
SerialChecker() {}
SerialChecker(const SerialChecker&);
void checkSerial();
SerialChecker& operator = (const SerialChecker&);
class Check {
public:
Check(SerialChecker& checker, const string& name);
Check(const Check&);
Check& operator = (const Check&);
void tick();
void tickOff();
void setNewCopy(SerialChecker*);
private:
bool ticked = false;
string name;
SerialChecker* newCopy = nullptr;
};
void addCheck(Check* c);
private:
vector<Check*> checks;
};
template <class T>
T& checkSerial(T& t, SerialChecker::Check& check) {
check.tick();
return t;
}
namespace boost {
namespace serialization {
// why the f** are these things not implemented by default in boost?
//unordered_map
template<class Archive, class T, class U>
inline void save(Archive& ar, const unordered_map<T, U>& t, unsigned int file_version){
int count = t.size();
ar << BOOST_SERIALIZATION_NVP(count);
for (auto& elem : t)
ar << boost::serialization::make_nvp("key", elem.first) << boost::serialization::make_nvp("value", elem.second);
}
template<class Archive, class T, class U>
inline void load(Archive& ar, unordered_map<T, U>& t, unsigned int){
int count;
ar >> BOOST_SERIALIZATION_NVP(count);
t.clear();
t.reserve(count);
while (count-- > 0) {
pair<T, U> p;
ar >> boost::serialization::make_nvp("key", p.first) >> boost::serialization::make_nvp("value", p.second);
t.insert(std::move(p));
}
}
template<class Archive, class T, class U>
inline void serialize(Archive& ar, unordered_map<T, U>& t, unsigned int file_version){
boost::serialization::split_free(ar, t, file_version);
}
//priority queue
template<class Archive, class T, class U, class V>
inline void save(Archive& ar, priority_queue<T, U, V> t, unsigned int file_version){
int count = t.size();
ar << BOOST_SERIALIZATION_NVP(count);
T array[count];
int cnt = 0;
while (!t.empty()) {
array[cnt++] = t.top();
t.pop();
}
ar << boost::serialization::make_array(array, count);
}
template<class Archive, class T, class U, class V>
inline void load(Archive& ar, priority_queue<T, U, V>& t, unsigned int){
int count;
ar >> BOOST_SERIALIZATION_NVP(count);
T array[count];
ar >> boost::serialization::make_array(array, count);
for (int i = 0; i < count; ++i) {
t.push(std::move(array[i]));
}
}
template<class Archive, class T, class U, class V>
inline void serialize(Archive& ar, priority_queue<T, U, V>& t, unsigned int file_version){
boost::serialization::split_free(ar, t, file_version);
}
//queue
template<class Archive, class T>
inline void save(Archive& ar, queue<T> t, unsigned int file_version){
int count = t.size();
ar << BOOST_SERIALIZATION_NVP(count);
while (!t.empty()) {
ar << boost::serialization::make_nvp("elem", t.front());
t.pop();
}
}
template<class Archive, class T>
inline void load(Archive& ar, queue<T>& t, unsigned int){
int count;
ar >> BOOST_SERIALIZATION_NVP(count);
while (count-- > 0) {
T elem;
ar >> boost::serialization::make_nvp("elem", elem);
t.push(std::move(elem));
}
}
template<class Archive, class T>
inline void serialize(Archive& ar, queue<T>& t, unsigned int file_version){
boost::serialization::split_free(ar, t, file_version);
}
//stack
template<class Archive, class T>
inline void save(Archive& ar, stack<T> t, unsigned int file_version){
int count = t.size();
ar << BOOST_SERIALIZATION_NVP(count);
while (!t.empty()) {
ar << boost::serialization::make_nvp("elem", t.top());
t.pop();
}
}
template<class Archive, class T>
inline void load(Archive& ar, stack<T>& t, unsigned int){
int count;
ar >> BOOST_SERIALIZATION_NVP(count);
stack<T> s;
while (count-- > 0) {
T elem;
ar >> boost::serialization::make_nvp("elem", elem);
s.push(std::move(elem));
}
while (!s.empty()) {
t.push(std::move(s.top()));
s.pop();
}
}
template<class Archive, class T>
inline void serialize(Archive& ar, stack<T>& t, unsigned int file_version){
boost::serialization::split_free(ar, t, file_version);
}
//unordered_set
template<class Archive, class T>
inline void save(Archive& ar, const unordered_set<T>& t, unsigned int file_version){
int count = t.size();
ar << BOOST_SERIALIZATION_NVP(count);
for (auto elem : t)
ar << boost::serialization::make_nvp("item", elem);
}
template<class Archive, class T>
inline void load(Archive& ar, unordered_set<T>& t, unsigned int){
int count;
ar >> BOOST_SERIALIZATION_NVP(count);
t.clear();
t.reserve(count);
while (count-- > 0) {
T elem;
ar >> boost::serialization::make_nvp("item", elem);
t.insert(elem);
}
}
template<class Archive, class T>
inline void serialize(Archive& ar, unordered_set<T>& t, unsigned int file_version){
boost::serialization::split_free(ar, t, file_version);
}
// unique_ptr
template<class Archive, class T>
inline void save(Archive & ar, const std::unique_ptr< T > &t, unsigned int file_version){
const T * const ptr = t.get();
ar << BOOST_SERIALIZATION_NVP(ptr);
}
template<class Archive, class T>
inline void load(Archive & ar, std::unique_ptr< T > &t, unsigned int file_version){
T *ptr;
ar >> BOOST_SERIALIZATION_NVP(ptr);
t.reset(ptr);
}
template<class Archive, class T>
inline void serialize(Archive & ar, std::unique_ptr< T > &t, unsigned int file_version){
boost::serialization::split_free(ar, t, file_version);
}
// vector
template<class Archive, class T, class Allocator>
inline void save(Archive & ar, const std::vector<T, Allocator> &t, unsigned int){
int count = t.size();
ar << BOOST_SERIALIZATION_NVP(count);
for(auto it=t.begin(), end=t.end(); it!=end; ++it)
ar << boost::serialization::make_nvp("item", (*it));
}
template<class Archive, class T, class Allocator>
inline void load(Archive & ar, std::vector<T, Allocator> &t, unsigned int){
int count;
ar >> BOOST_SERIALIZATION_NVP(count);
t.clear();
t.reserve(count);
while( count-- > 0 ) {
T i;
ar >> boost::serialization::make_nvp("item", i);
t.push_back(std::move(i));
}
}
template<class Archive, class T, class Allocator>
inline void serialize(Archive & ar, std::vector<T, Allocator> & t, unsigned int file_version){
boost::serialization::split_free(ar, t, file_version);
}
#ifdef DEBUG_STL
// stl debug dummies
template<class Archive, class T>
inline void serialize(Archive & ar, __gnu_debug::vector< T > &t, unsigned int file_version) {
}
template<class Archive>
inline void serialize(Archive & ar, __gnu_debug::string &t, unsigned int file_version) {
}
template<class Archive, class T, class U>
inline void serialize(Archive & ar, __gnu_debug::map< T, U > &t, unsigned int file_version) {
}
template<class Archive, class T>
inline void serialize(Archive & ar, __gnu_debug::set< T > &t, unsigned int file_version) {
}
template<class Archive, class T>
inline void serialize(Archive & ar, __gnu_debug::deque< T > &t, unsigned int file_version) {
}
#endif
//tuple
template<int N>
struct Serialize
{
template<class Archive, typename... Args>
static void serialize(Archive & ar, std::tuple<Args...> & t, const unsigned int version)
{
ar & boost::serialization::make_nvp("elem", std::get<N-1>(t));
Serialize<N-1>::serialize(ar, t, version);
}
};
template<>
struct Serialize<0>
{
template<class Archive, typename... Args>
static void serialize(Archive & ar, std::tuple<Args...> & t, const unsigned int version)
{
}
};
template<class Archive, typename... Args>
void serialize(Archive & ar, std::tuple<Args...> & t, const unsigned int version)
{
Serialize<sizeof...(Args)>::serialize(ar, t, version);
}
}} // namespace boost::serialization
#endif