-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializer.hpp
More file actions
613 lines (496 loc) · 19.5 KB
/
serializer.hpp
File metadata and controls
613 lines (496 loc) · 19.5 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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#pragma once
#include <functional>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <memory.h>
// using void_t with all template arguments as void
template< class... >
using void_t = void;
// checks for operator<<
template<typename T, typename = void>
struct HasOutputOperator : std::false_type {};
template<typename T>
struct HasOutputOperator<T, void_t<decltype(std::declval<T>().operator<<)>> : std::true_type {};
// type checks
template<typename T, typename = void>
struct IsIterable : std::false_type {};
template <typename T>
struct IsIterable<T, void_t<typename T::iterator>> : std::true_type {};
template<typename T>
struct IsVector : std::false_type {};
template<typename T, typename... Others>
struct IsVector<std::vector<T, Others...>> : std::true_type{};
template<typename T>
struct IsList : std::false_type {};
template<typename T, typename... Others>
struct IsList<std::list<T, Others...>> : std::true_type{};
template<typename T>
struct IsDeque : std::false_type {};
template<typename T, typename... Others>
struct IsDeque<std::deque<T, Others...>> : std::true_type{};
template<typename T>
struct IsMap : std::false_type {};
template<typename T, typename... Others>
struct IsMap<std::map<T, Others...>> : std::true_type{};
template<typename T>
struct IsMultimap : std::false_type {};
template<typename T, typename... Others>
struct IsMultimap<std::multimap<T, Others...>> : std::true_type{};
template<typename T>
struct IsUndorderedMap : std::false_type {};
template<typename T, typename... Others>
struct IsUndorderedMap<std::unordered_map<T, Others...>> : std::true_type{};
template<typename T>
struct IsUndorderedMultimap : std::false_type {};
template<typename T, typename... Others>
struct IsUndorderedMultimap<std::unordered_multimap<T, Others...>> : std::true_type{};
template<typename T>
struct IsSet : std::false_type {};
template<typename T, typename... Others>
struct IsSet<std::set<T, Others...>> : std::true_type{};
template<typename T>
struct IsMultiset : std::false_type {};
template<typename T, typename... Others>
struct IsMultiset<std::multiset<T, Others...>> : std::true_type{};
template<typename T>
struct IsUnorderedSet : std::false_type {};
template<typename T, typename... Others>
struct IsUnorderedSet<std::unordered_set<T, Others...>> : std::true_type{};
template<typename T>
struct IsUnorderedMultiset : std::false_type {};
template<typename T, typename... Others>
struct IsUnorderedMultiset<std::unordered_multiset<T, Others...>> : std::true_type{};
template<typename T>
struct IsString : std::false_type {};
template<>
struct IsString<std::string> : std::true_type{};
/*
Not usual serializator, it works as:
- writes data in passed string buffer
- Supported data types:
- arithmetic(numeric)
- standart containers
- user types
- User types must inherit Serializable class and provide needed interface.
WARNING! Now it accepts pointers and writes its values in buffer. So, maybe, it is good only for values now, but can be extended.
WARNING! To add functions to some else standart type, you should specify Serializer::SerializeUnit template function for this type.
*/
namespace Serialization
{
typedef uint64_t BytesCount;
typedef uint64_t ElementsCount;
typedef uint64_t SerializerId;
typedef std::function<BytesCount(std::string&)> SerializerFunction;
typedef std::function<BytesCount(const std::string&, BytesCount)> DeserializerFunction;
class SerializerExceptions
{
public:
struct InvalidType : std::runtime_error
{
InvalidType(const std::string& err);
};
struct InvalidArgs : std::runtime_error
{
InvalidArgs(const std::string& err);
};
};
/*
Contains template structs that help serialize std containers.
*/
class ContainerSerializerHelper
{
template<typename T, typename Check = void>
struct ContainerAdder;
};
template<typename T>
struct ContainerSerializerHelper::ContainerAdder<T, std::enable_if_t<IsVector<T>::value || IsList<T>::value || IsDeque<T>::value>>
{
template<typename Val>
static void addTo(T* cont, Val* val)
{
cont->push_back(*val);
}
};
template<typename T>
struct ContainerSerializerHelper::ContainerAdder<T, std::enable_if_t<IsMap<T>::value || IsMultimap<T>::value ||
IsUndorderedMap<T>::value || IsUndorderedMultimap<T>::value ||
IsSet<T>::value || IsMultiset<T>::value ||
IsUnorderedSet<T>::value || IsUnorderedMultiset<T>::value>>
{
template<typename Val>
// 'val' is std::pair here
static void addTo(T* cont, Val* val)
{
cont->insert(*val);
}
};
/*
Array serialization helper.
To array be serialized correctly, it MUST be wrapped in this structure.
*/
template<typename T>
struct ArrayWrapper
{
ArrayWrapper()
: start{nullptr}, size{0} {}
ArrayWrapper(T* _start, ElementsCount _size)
: start{_start}, size{_size} {}
T* start;
ElementsCount size;
};
/*
This class must be inherited if object wants to be serializable and has single serializer;
*/
class Serializable
{
public:
virtual BytesCount serialize(std::string& buf) = 0;
};
/*
This class must be inherited if object wants to be deserializable and has single serializer;
*/
class Deserializable
{
virtual BytesCount deserialize(const std::string& buf, BytesCount offset) = 0;
};
/*
This class must be inherited if object wants to be serializable and has multiple serializers;
This object should have map of serializers, from which needed serializer will be selected by 'id'.
*/
class MultipleSerializable
{
public:
typedef std::unordered_map<SerializerId, SerializerFunction> SerializersMap;
inline BytesCount serialize(std::string& buf) { return getCurrentSerializer()(buf); }
void registerSerializer(SerializerId _id, const SerializerFunction& f);
// throws if not have such Id
const SerializerFunction& getCurrentSerializer();
inline void setSerializerId(SerializerId _id) { curId = _id; }
inline SerializerId getSerializerId() const { return curId; }
private:
SerializersMap smap;
SerializerId curId;
};
/*
This class must be inherited if object wants to be deserializable and has multiple serializers;
This object should have map of deserializers, from which needed deserializer will be selected by 'id'.
*/
class MultipleDeserializable
{
public:
typedef std::unordered_map<SerializerId, DeserializerFunction> DeserializersMap;
inline BytesCount deserialize(const std::string& buf, BytesCount offset) { return getCurrentDeserializer()(buf, offset); }
void registerDeserializer(SerializerId _id, const DeserializerFunction& f);
// throws if not have such Id
const DeserializerFunction& getCurrentDeserializer();
inline void setDeserializerId(SerializerId _id) { curId = _id; }
inline SerializerId getDeserializerId() const { return curId; }
private:
DeserializersMap dsmap;
SerializerId curId;
};
// checks for custorm type
template<typename T, typename = void>
struct IsSerializable : std::false_type {};
template<typename T>
struct IsSerializable<T, std::enable_if_t<std::is_base_of<Serializable, T>::value>> : std::true_type {};
template<typename T, typename = void>
struct IsDeserializable : std::false_type {};
template<typename T>
struct IsDeserializable<T, std::enable_if_t<std::is_base_of<Deserializable, T>::value>> : std::true_type {};
template<typename T, typename = void>
struct IsMultipleSerializable : std::false_type {};
template<typename T>
struct IsMultipleSerializable<T, std::enable_if_t<std::is_base_of<MultipleSerializable, T>::value>> : std::true_type {};
template<typename T, typename = void>
struct IsMultipleDeserializable : std::false_type {};
template<typename T>
struct IsMultipleDeserializable<T, std::enable_if_t<std::is_base_of<MultipleDeserializable, T>::value>> : std::true_type {};
class Serializer
{
public:
template<typename T, typename Check = void>
struct SerializeUnit;
template<typename Arg>
static BytesCount serializeAll(std::string& buf, Arg* data);
template<typename Arg, typename... Args>
static BytesCount serializeAll(std::string& buf, Arg* data, Args... args);
};
/*
End of recursion
*/
template<typename Arg>
BytesCount Serializer::serializeAll(std::string& buf, Arg* data)
{
return Serializer::SerializeUnit<Arg>::serializeUnit(buf, data);
}
/*
Serializes all arguments passed. Writes as binary data in 'buf'
*/
template<typename Arg, typename... Args>
BytesCount Serializer::serializeAll(std::string& buf, Arg* data, Args... args)
{
return Serializer::SerializeUnit<Arg>::serializeUnit(buf, data) + serializeAll(buf, args...);
}
/*
Serializer for arithmetic types.
*/
template<typename T>
struct Serializer::SerializeUnit<T, std::enable_if_t<std::is_arithmetic<T>::value> >
{
SerializeUnit() = default;
static BytesCount serializeUnit(std::string& buf, T* data)
{
int initSize = buf.size();
buf.resize(buf.size() + sizeof(T));
memcpy(&buf[initSize], data, sizeof(T));
return sizeof(T);
}
};
/*
Serializer for array wrappers.
*/
template<typename T>
struct Serializer::SerializeUnit<ArrayWrapper<T>>
{
SerializeUnit() = default;
static BytesCount serializeUnit(std::string& buf, ArrayWrapper<T>* data)
{
if(data->start == nullptr || data->size <= 0) throw SerializerExceptions::InvalidArgs{"serializeUnit<ArrayWrapper>() - invalid arguments passed"};
BytesCount written = 0;
written += SerializeUnit<ElementsCount>::serializeUnit(buf, &(data->size));
for(ElementsCount i = 0; i < data->size; ++i)
{
written += SerializeUnit<T>::serializeUnit(buf, data->start + i);
}
return written;
}
};
/*
Serializer for iterables(they are in most cases can be serialized in the same way).
*/
template<typename T>
struct Serializer::SerializeUnit<T, std::enable_if_t<IsIterable<T>::value>>
{
SerializeUnit() = default;
static BytesCount serializeUnit(std::string& buf, T* data)
{
using ContSize = typename T::size_type;
using ValueType = typename T::value_type;
BytesCount dataSize = 0;
ContSize sz = data->size();
// writing size
dataSize += SerializeUnit<ContSize>::serializeUnit(buf, &sz);
auto iter = data->begin();
// writing 'size' parts of data
while(iter != data->end())
{
// container value type can be const, that should not interfere serialization
// and several containers(such as std::set) always points to const types
typedef std::remove_const_t<ValueType> NonConstValueType;
dataSize += SerializeUnit<ValueType>::serializeUnit(buf, const_cast<NonConstValueType*>(&(*iter)));
++iter;
}
return dataSize;
}
};
/*
Serializer for strings.
They are, of course, iterable, but it is more efficient to write it in one operation, because data is stores sequentially in memory.
*/
template<>
struct Serializer::SerializeUnit<std::string>
{
SerializeUnit() = default;
static BytesCount serializeUnit(std::string& buf, std::string* data)
{
using ContSize = typename std::string::size_type;
BytesCount dataSize = 0;
ContSize sz = data->size();
// writing size
dataSize += SerializeUnit<ContSize>::serializeUnit(buf, &sz);
BytesCount initSize = buf.size();
buf.resize(buf.size() + data->size());
// writing 'size' characters
memcpy(&buf[initSize], &((*data)[0]), data->size());
dataSize += data->size();
return dataSize;
}
};
/*
Serializer for user type that inherits IsSerializable
*/
template<typename T>
struct Serializer::SerializeUnit<T, std::enable_if_t<IsSerializable<T>::value>>
{
SerializeUnit() = default;
static BytesCount serializeUnit(std::string& buf, T* data)
{
return data->serialize(buf);
}
};
/*
Serializer for user type that inherits IsMultipleSerializable
*/
template<typename T>
struct Serializer::SerializeUnit<T, std::enable_if_t<IsMultipleSerializable<T>::value>>
{
SerializeUnit() = default;
static BytesCount serializeUnit(std::string& buf, T* data)
{
return data->serialize(buf);
}
};
/*
Serializer for std::pair
*/
template<typename T1, typename T2>
struct Serializer::SerializeUnit<std::pair<T1, T2>>
{
SerializeUnit() = default;
static BytesCount serializeUnit(std::string& buf, std::pair<T1, T2>* data)
{
typedef std::remove_const_t<T1> NonConstT1;
// throwing away const, because std containers, such as std::unordered_map, use const Key in their value type
return serializeAll(buf, const_cast<NonConstT1*>(&data->first), &data->second);
}
};
/*
Deserializer works in the same way as serializer,
but for custom types inheritance from Deserializable must be done.
*/
class Deserializer
{
public:
template<typename T, typename Check = void>
struct DeserializeUnit;
template<typename Arg>
static BytesCount deserializeAll(const std::string& buf, BytesCount offset, Arg* data);
template<typename Arg, typename... Args>
static BytesCount deserializeAll(const std::string& buf, BytesCount offset, Arg* data, Args... args);
};
template<typename Arg>
BytesCount Deserializer::deserializeAll(const std::string& buf, BytesCount offset, Arg* data)
{
return Deserializer::DeserializeUnit<Arg>::deserializeUnit(buf, offset, data);
}
template<typename Arg, typename... Args>
BytesCount Deserializer::deserializeAll(const std::string& buf, BytesCount offset, Arg* data, Args... args)
{
BytesCount nextOffset = Deserializer::DeserializeUnit<Arg>::deserializeUnit(buf, offset, data);
return nextOffset + deserializeAll(buf, offset + nextOffset, args...);
}
// for arithmetic
template<typename T>
struct Deserializer::DeserializeUnit<T, std::enable_if_t<std::is_arithmetic<T>::value> >
{
static BytesCount deserializeUnit(const std::string& buf, BytesCount offset, T* data)
{
memcpy(data, &buf[offset], sizeof(T));
return sizeof(T);
}
};
/*
Deserializer for array wrappers.
WARNING: before deserialization, array wrapper should have 'start' member set(memory allocated).
*/
template<typename T>
struct Deserializer::DeserializeUnit<ArrayWrapper<T>>
{
DeserializeUnit() = default;
static BytesCount deserializeUnit(const std::string& buf, BytesCount offset, ArrayWrapper<T>* data)
{
if(data->start == nullptr) throw SerializerExceptions::InvalidArgs{"serializeUnit<ArrayWrapper>() - invalid arguments passed"};
BytesCount read = 0;
BytesCount internalOffset = offset;
read += DeserializeUnit<ElementsCount>::deserializeUnit(buf, internalOffset, &(data->size));
internalOffset += sizeof(ElementsCount);
for(ElementsCount i = 0; i < data->size; ++i)
{
read += DeserializeUnit<T>::deserializeUnit(buf, internalOffset, data->start + i);
internalOffset += sizeof(T);
}
return read;
}
};
// for vector, list, deque
template<typename T>
struct Deserializer::DeserializeUnit<T, std::enable_if_t<IsIterable<T>::value>>
{
static BytesCount deserializeUnit(const std::string &buf, BytesCount offset, T *data)
{
data->clear();
using ContSize = typename T::size_type;
using DataType = typename T::value_type;
ContSize contSize;
BytesCount internalOffset = offset;
internalOffset += DeserializeUnit<ContSize>::deserializeUnit(buf, internalOffset, &contSize);
for(ContSize i = 0; i < contSize; ++i)
{
DataType dataPiece;
internalOffset += DeserializeUnit<DataType>::deserializeUnit(buf, internalOffset, &dataPiece);
ContainerSerializerHelper::ContainerAdder<T>::addTo(data, &dataPiece);
}
return internalOffset - offset;
}
};
// for std::string
template<>
struct Deserializer::DeserializeUnit<std::string>
{
DeserializeUnit() = default;
static BytesCount deserializeUnit(const std::string& buf, BytesCount offset, std::string* data)
{
data->clear();
using ContSize = typename std::string::size_type;
ContSize contSize;
BytesCount internalOffset = offset;
internalOffset += DeserializeUnit<ContSize>::deserializeUnit(buf, internalOffset, &contSize);
data->resize(contSize);
memcpy(&((*data)[0]), &buf[internalOffset], contSize);
internalOffset += contSize;
return internalOffset - offset;
}
};
// for user type
template<typename T>
struct Deserializer::DeserializeUnit<T, std::enable_if_t<IsDeserializable<T>::value>>
{
DeserializeUnit() = default;
static BytesCount deserializeUnit(const std::string& buf, BytesCount offset, T* data)
{
return data->deserialize(buf, offset);
}
};
// for user type with multiple serializators
template<typename T>
struct Deserializer::DeserializeUnit<T, std::enable_if_t<IsMultipleDeserializable<T>::value>>
{
DeserializeUnit() = default;
static BytesCount deserializeUnit(const std::string& buf, BytesCount offset, T* data)
{
return data->deserialize(buf, offset);
}
};
/*
std::pair
*/
template<typename T1, typename T2>
struct Deserializer::DeserializeUnit<std::pair<T1, T2>>
{
DeserializeUnit() = default;
static BytesCount deserializeUnit(const std::string& buf, BytesCount offset, std::pair<T1, T2>* data)
{
typedef std::remove_const_t<T1> NonConstT1;
// throwing away const, because std containers, such as std::unordered_map, use const Key in their value type
return deserializeAll(buf, offset, const_cast<NonConstT1*>(&data->first), &data->second);
}
};
}