-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathpretty_archive.h
541 lines (448 loc) · 14.1 KB
/
pretty_archive.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#pragma once
#include "extern/iomanip.h"
#include "util.h"
struct PrettyException {
string text;
};
struct StreamPos {
signed char filename;
short line = -1;
signed char column;
};
using StreamPosStack = std::array<StreamPos, 4>;
struct StreamChar {
StreamPosStack pos;
char c;
};
enum class BracketType {
ROUND,
CURLY
};
class KeyVerifier;
class PrettyInputArchive {
public:
PrettyInputArchive(const vector<string>& inputs, const vector<string>& filenames, KeyVerifier* v);
string eat(const char* expected = nullptr);
struct LoaderInfo {
string name;
function<void(bool)> load;
bool optional;
};
struct NodeData {
deque<LoaderInfo> loaders;
bool inherited = false;
BracketType bracket = BracketType::CURLY;
};
NodeData& getNode() {
return nodeData.back();
}
void error(const string& s);
StreamPosStack getCurrentPosition();
template <typename T>
bool readMaybe(T& elem) {
auto b = bookmark();
is >> elem;
if (!is) {
is.clear();
seek(b);
return false;
}
return true;
}
bool eatMaybe(const string& s);
void openBracket(BracketType);
void closeBracket(BracketType);
bool isOpenBracket(BracketType);
bool isCloseBracket(BracketType);
string peek(int cnt = 1);
template <typename T>
PrettyInputArchive& readText(T&& elem) {
auto b = bookmark();
is >> std::forward<T>(elem);
if (!is) {
is.clear();
seek(b);
error("Error reading value of type: "_s + typeid(T).name());
}
return *this;
}
long bookmark();
template <typename T>
void loadInherited(T& elem) {
nextElemInherited = true;
this->operator()(elem);
}
struct DefInfo {
int begin;
int end;
vector<string> args;
};
PrettyInputArchive& operator()() {
return *this;
}
template <typename T, typename... Types>
PrettyInputArchive& operator()(T&& arg1, Types&& ... args) {
prologue(*this, arg1);
load(*this, arg1);
epilogue(*this, arg1);
return this->operator()(args...);
}
void seek(long p);
void startNode();
void endNode();
KeyVerifier& keyVerifier;
bool inheritingKey = false;
optional<string> lastPrimaryId;
optional<string> lastNamedField;
optional<GameConfigId> gameConfigId;
using is_loading = std::true_type;
using is_saving = std::false_type;
//private:
vector<NodeData> nodeData;
bool nextElemInherited = false;
std::istringstream is;
vector<StreamPosStack> streamPos;
vector<string> filenames;
void throwException(const StreamPosStack&, const string&);
string positionToString(const StreamPosStack&);
using DefsMap = map<pair<string, int>, DefInfo>;
pair<DefsMap, vector<StreamChar>> parseDefs(const vector<StreamChar>& content);
vector<StreamChar> preprocess(const vector<StreamChar>& content);
};
template<typename T, typename int_<decltype(serialize(std::declval<PrettyInputArchive&>(), std::declval<T&>()))>::type = 0>
void load(PrettyInputArchive& ar1, T& obj) {
serialize(ar1, obj);
}
template<typename T, typename int_<decltype(serialize(std::declval<PrettyInputArchive&>(), std::declval<T&>(), std::declval<unsigned>()))>::type = 0>
void load(PrettyInputArchive& ar1, T& obj) {
serialize(ar1, obj, unsigned(1000));
}
template<typename T, typename int_<decltype(std::declval<T&>().serialize(std::declval<PrettyInputArchive&>(), std::declval<unsigned>()))>::type = 0>
void load(PrettyInputArchive& ar1, T& obj) {
obj.serialize(ar1, unsigned(1000));
}
template<typename T, typename int_<decltype(std::declval<T&>().serialize(std::declval<PrettyInputArchive&>()))>::type = 0>
void load(PrettyInputArchive& ar1, T& obj) {
obj.serialize(ar1);
}
template<typename T>
void load(PrettyInputArchive& ar1, cereal::base_class<T>& obj) {
load(ar1, *obj.base_ptr);
}
namespace variant_detail {
template<int N, class Variant, class ... Args>
typename std::enable_if<N == Variant::num_types>::type
load_variant(PrettyInputArchive & ar, const string& target, Variant & /*variant*/) {
ar.error("Element \"" + target + "\" not part of type " + Variant::getVariantName());
}
template<int N, class Variant, class H, class ... T>
typename std::enable_if<N < Variant::num_types, void>::type
load_variant(PrettyInputArchive & ar1, const string& target, Variant & variant) {
if (variant.getName(N) == target) {
H value;
ar1(value);
variant = Variant(value);
} else
load_variant<N+1, Variant, T...>(ar1, target, variant);
}
}
//! Loading for NamedVariant
template <typename VariantType1, const char* Str(bool), typename... VariantTypes> inline
void serialize( PrettyInputArchive & ar, NamedVariant<Str, VariantType1, VariantTypes...> & v )
{
string name;
ar.readText(name);
variant_detail::load_variant<0, NamedVariant<Str, VariantType1, VariantTypes...>, VariantType1, VariantTypes...>(
ar, name, v);
}
//! Loading for enum types
template <class T> inline
typename std::enable_if<std::is_enum<T>::value, void>::type
serialize( PrettyInputArchive & ar, T & t)
{
string s;
ar.readText(s);
if (auto res = EnumInfo<T>::fromStringSafe(s))
t = *res;
else
ar.error("Error reading "_s + EnumInfo<T>::getName() + " value \"" + s + "\"");
}
template<class T> inline
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_enum<T>::value, void>::type
serialize(PrettyInputArchive& ar, T& t) {
ar.readText(t);
}
void serialize(PrettyInputArchive& ar, std::string& t);
void serialize(PrettyInputArchive& ar, char& c);
void serialize(PrettyInputArchive& ar, bool& c);
void serialize(PrettyInputArchive& ar, int& c);
struct PrettyFlag {
bool value = false;
};
void serialize(PrettyInputArchive& ar, PrettyFlag& c);
template <typename T>
inline void serializeVecImpl(PrettyInputArchive& ar1, vector<T>& v, BracketType bracketType) {
if (!ar1.eatMaybe("append"))
v.clear();
if (!ar1.isOpenBracket(bracketType)) {
T t;
ar1(t);
v.push_back(std::move(t));
} else {
ar1.openBracket(bracketType);
while (!ar1.isCloseBracket(bracketType)) {
T t;
ar1(t);
v.push_back(std::move(t));
if (bracketType == BracketType::ROUND && !ar1.isCloseBracket(bracketType))
ar1.eat(",");
}
ar1.closeBracket(bracketType);
}
}
template <typename T>
inline void serialize(PrettyInputArchive& ar1, vector<T>& v) {
serializeVecImpl(ar1, v, BracketType::CURLY);
}
template <typename T>
struct VectorWithRoundBrackets {
vector<T>& v;
};
template <typename T>
VectorWithRoundBrackets<T> withRoundBrackets(vector<T>& v) {
return VectorWithRoundBrackets<T>{v};
}
template <typename T>
inline void serialize(PrettyInputArchive& ar1, VectorWithRoundBrackets<T>& v) {
serializeVecImpl(ar1, v.v, BracketType::ROUND);
}
template <typename Archive, typename T>
inline void serialize(Archive& ar1, VectorWithRoundBrackets<T>& v) {
ar1(v.v);
}
template <typename T, typename H>
inline void serialize(PrettyInputArchive& ar1, unordered_set<T, H>& v) {
if (!ar1.eatMaybe("append"))
v.clear();
auto bracketType = BracketType::CURLY;
ar1.openBracket(bracketType);
while (!ar1.isCloseBracket(bracketType)) {
T t;
ar1(t);
v.insert(std::move(t));
}
ar1.closeBracket(bracketType);
}
template <typename T, std::size_t N>
inline void serialize(PrettyInputArchive& ar1, array<T, N>& a) {
ar1.openBracket(BracketType::CURLY);
for (int i : Range(N))
ar1(a[i]);
ar1.closeBracket(BracketType::CURLY);
}
template <typename M>
inline void serializeMap(PrettyInputArchive& ar1, M& m) {
if (!ar1.eatMaybe("append"))
m.clear();
string s;
ar1.readText(s);
if (s != "{")
ar1.error("Expected list of items surrounded by { and }");
set<typename M::key_type> keys;
while (1) {
auto keyText = ar1.peek();
if (keyText == "}")
break;
typename M::key_type key;
ar1(key);
if (ar1.peek() != "modify" && keys.count(key))
ar1.error("Duplicate key");
keys.insert(key);
typename M::mapped_type value;
vector<long> toRead;
bool wasInherited = false;
bool modifying = false;
if (ar1.eatMaybe("inherit") || (modifying = ar1.eatMaybe("modify"))) {
typename M::key_type inheritKey;
ar1.inheritingKey = true;
if (!modifying)
ar1(inheritKey);
else
inheritKey = key;
ar1.inheritingKey = false;
wasInherited = true;
if (auto v = getReferenceMaybe(m, inheritKey))
value = *v;
else
ar1.error((modifying ? "Key to modify not found " : "Key to inherit not found ") + keyText);
}
if (wasInherited)
ar1.loadInherited(value);
else
ar1(value);
m.erase(key);
m.insert(make_pair(std::move(key), std::move(value)));
}
ar1.eat("}");
}
template <typename T, typename U>
inline void serialize(PrettyInputArchive& ar1, map<T, U>& m) {
serializeMap(ar1, m);
}
template <typename T, typename U, typename H>
inline void serialize(PrettyInputArchive& ar1, unordered_map<T, U, H>& m) {
serializeMap(ar1, m);
}
template <typename T, typename U>
inline void serialize(PrettyInputArchive& ar1, EnumMap<T, U>& m) {
if (!ar1.eatMaybe("append"))
m.clear();
vector<pair<T, U>> v;
ar1(v);
EnumSet<T> used;
for (auto& elem : v) {
if (used.contains(elem.first))
ar1.error("Repeated enum element: \"" + EnumInfo<T>::getString(elem.first));
used.insert(elem.first);
m[elem.first] = std::move(elem.second);
}
}
template <typename T>
inline void serialize(PrettyInputArchive& ar1, optional<T>& v) {
if (ar1.eatMaybe("append")) {
if (!v)
ar1.error("Appending to an optional value that was not initialized");
auto& t = *v;
ar1.loadInherited(t);
} else {
v.reset();
if (ar1.eatMaybe("none"))
return;
T t;
ar1(t);
v = std::move(t);
}
}
template <typename T>
class optional_no_none : public optional<T> {
public:
using optional<T>::optional;
};
template <typename T>
inline void serialize(PrettyInputArchive& ar1, optional_no_none<T>& v) {
v.reset();
if (ar1.eatMaybe("none"))
ar1.error("This value can't be reset");
T t;
ar1(t);
v = std::move(t);
}
template <typename T>
inline void serialize(PrettyInputArchive& ar1, unique_ptr<T>& v) {
v.reset();
if (ar1.eatMaybe("none"))
return;
T* t = new T();
ar1(*t);
v.reset(t);
}
struct EndPrettyInput {};
EndPrettyInput& endInput();
struct SetRoundBracket {};
SetRoundBracket& roundBracket();
template <class T>
inline void handleNamePair(PrettyInputArchive& ar1, const string& name, T& value, bool optional) {
ar1.getNode().loaders.push_back(PrettyInputArchive::LoaderInfo{name,
[&ar1, &value](bool init){ if (init) value = T{}; ar1(value); }, optional});
}
template <class T>
inline void serialize(PrettyInputArchive& ar1, OptionalNameValuePair<T>& t) {
handleNamePair(ar1, t.name, t.value, true);
}
void serialize(PrettyInputArchive& ar1, SetRoundBracket&);
template <class T>
inline void serialize(PrettyInputArchive& ar1, SkipPrettyValue<T>& t) {
}
template <class T>
inline void serialize(PrettyInputArchive& ar1, cereal::NameValuePair<T>& t) {
handleNamePair(ar1, t.name, t.value, false);
}
template <class T>
inline void serialize(PrettyInputArchive& ar1, cereal::NameValuePair<optional<T>&>& t) {
handleNamePair(ar1, t.name, t.value, true);
}
template <class T>
inline void serialize(PrettyInputArchive& ar1, cereal::NameValuePair<heap_optional<T>&>& t) {
handleNamePair(ar1, t.name, t.value, true);
}
template <class T>
inline void serialize(PrettyInputArchive& ar1, cereal::NameValuePair<optional_no_none<T>&>& t) {
handleNamePair(ar1, t.name, t.value, true);
}
template <class T, class U> inline
void serialize(PrettyInputArchive& ar1, std::pair<T, U>& t) {
ar1(t.first, t.second);
}
namespace pretty_tuple_detail {
template <size_t Height>
struct serialize {
template <class Archive, class ... Types> inline
static void apply(Archive& ar1, std::tuple<Types...>& tuple) {
serialize<Height - 1>::template apply(ar1, tuple);
ar1(std::get<Height - 1>(tuple));
}
};
template <>
struct serialize<0> {
template <class Archive, class ... Types> inline
static void apply( Archive &, std::tuple<Types...>& ) { }
};
}
template <class ... Types> inline
void serialize(PrettyInputArchive& ar, std::tuple<Types...>& tuple) {
pretty_tuple_detail::serialize<std::tuple_size<std::tuple<Types...>>::value>::template apply( ar, tuple );
}
template <class T>
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
prologue(PrettyInputArchive&, T const & ) {
}
template <class T>
typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
prologue(PrettyInputArchive& ar1, T const & ) {
ar1.startNode();
}
void prettyEpilogue(PrettyInputArchive& ar1);
void serialize(PrettyInputArchive& ar1, EndPrettyInput&);
template <class T>
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
epilogue(PrettyInputArchive& ar1, T const &) {
}
template <class T>
typename std::enable_if<!std::is_arithmetic<T>::value, void>::type
epilogue(PrettyInputArchive& ar1, T const &) {
prettyEpilogue(ar1);
ar1.endNode();
}
// Ignore these inputs
template <class T> inline
void prologue(PrettyInputArchive&, cereal::NameValuePair<T> const & ) { }
template <class T> inline
void epilogue(PrettyInputArchive&, cereal::NameValuePair<T> const & ) { }
template <class T> inline
void prologue(PrettyInputArchive&, OptionalNameValuePair<T> const & ) { }
template <class T> inline
void epilogue(PrettyInputArchive&, OptionalNameValuePair<T> const & ) { }
template <class T> inline
void prologue(PrettyInputArchive&, SkipPrettyValue<T> const & ) { }
template <class T> inline
void epilogue(PrettyInputArchive&, SkipPrettyValue<T> const & ) { }
template <class T> inline
void prologue(PrettyInputArchive&, cereal::SizeTag<T> const & ) { }
template <class T> inline
void epilogue(PrettyInputArchive&, cereal::SizeTag<T> const & ) { }
inline void prologue(PrettyInputArchive&, EndPrettyInput const & ) { }
inline void epilogue(PrettyInputArchive&, EndPrettyInput const & ) { }
template <class T> inline
void serialize(PrettyInputArchive& ar1, cereal::SizeTag<T> & t) {
ar1(t.size);
}