forked from mpmilano/mutils-serialization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializationSupport.hpp
1403 lines (1210 loc) · 48.4 KB
/
SerializationSupport.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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "SerializationMacros.hpp"
#include "context_ptr.hpp"
#include <cstring>
#include <mutils/macro_utils.hpp>
#include <mutils/mutils.hpp>
#include <mutils/tuple_extras.hpp>
#include <mutils/type_utils.hpp>
#include <tuple>
#include <vector>
// BEGIN DECLARATIONS AND USAGE INFORMATION
namespace mutils {
// forward declaration
struct DeserializationManager;
/**
* A non-POD type which wishes to mark itself byte representable should extend
* this class. Intended to use to convert classes to and from contiguous
* sequences of bytes.
*/
struct ByteRepresentable {
/**
* Write this class's marshalled representation into the array found at dest.
* Assumes dest has at least bytes_size() of free memory available; behavior
* is undefined otherwise.
*
* Returns number of bytes written, which should be the same as bytes_size().
*
* NOTE: it is recommended that users not call this directly, and prefer
* to use mutils::to_bytes(T, dest) instead.
*/
virtual std::size_t to_bytes(uint8_t* dest) const = 0;
/**
* Pass a pointer to a buffer containing this class's marshalled
* representation into the function f. This pointer is not guaranteed to live
* beyond the duration of the call to f, so make a copy if you need to keep it
* around.
*
* NOTE: it is recommended that users not call this directly, and prefer
* to use mutils::post_object(f,T) instead.
*/
virtual void post_object(
const std::function<void(uint8_t const* const, std::size_t)>&) const = 0;
/**
* the size of the marshalled representation of this object.
* useful when allocating arrays in which to store this object.
*
* NOTE: it is recommended that users not call this directly, and prefer
* to use mutils::bytes_size(T) instead.
*/
virtual std::size_t bytes_size() const = 0;
#ifdef MUTILS_DEBUG
/**
* If this object requires context in order to correctly
* deserialize, this method will associate that context
* with the provided DeserializationManager. Many objects
* will not require context, and so can leave this function
* empty.
*/
virtual void ensure_registered(DeserializationManager&) = 0;
#endif
virtual ~ByteRepresentable() {}
/**
* from_bytes takes the DeserializationManager which manages this object's
* context (or nullptr, if this object does not require a context), a byte
* array of size at least bytes_size(), and returns a new heap-allocated
* instance of that object.
*
* NOTE: it is recommended that users not call this directly, and prefer
* to use mutils::from_bytes<T>(DeserializationManager*, buf) instead.
*/
// needs to exist, but can't declare virtual statics
// virtual static std::unique_ptr<T> from_bytes(DeserializationManager *p, const uint8_t* buf) const = 0;
/**
* from_bytes_noalloc takes the DeserializationManager which manages this
* object's context (or nullptr, if this object does not require a context), a
* byte array of size at least bytes_size(), and returns an instance of that
* object. This instance may share storage with the provided byte array, and
* is not valid past the end of life of the byte array.
*
* NOTE: it is recommended that users not call this directly, and prefer
* to use mutils::deserialize_and_run<T>(DeserializationManager*, buf, f)
* instead. If the cost of passing a function is too high, please still
* prefer mutils::from_bytes_noalloc<T>(DeserializationManager*, buf).
*/
// needs to exist, but can't declare virtual statics
// virtual static context_ptr<T> from_bytes_noalloc(DeserializationManager *p, const uint8_t* buf) const = 0;
};
/**
* If a class which implements ByteRepresentable requires a context in order
* to correctly deserialize, that context should be represented as a class that
* extends RemoteDeserializationContext. If no context is required, then this
* class is not necessary.
*/
struct RemoteDeserializationContext {
RemoteDeserializationContext(const RemoteDeserializationContext&) = delete;
RemoteDeserializationContext(const RemoteDeserializationContext&&) = delete;
virtual ~RemoteDeserializationContext() {}
RemoteDeserializationContext() {}
};
// a pointer to a RemoteDeserializationContext. This exists
// so that I can switch it out with smart pointer types when
// debugging stuff.
using RemoteDeserializationContext_p = RemoteDeserializationContext*;
// a vector of RemoteDeserializationContext*.
// it's got a using declaration for the same reason
// as the above.
using RemoteDeserialization_v = std::vector<RemoteDeserializationContext_p>;
/**
* The manager for any RemoteDeserializationContexts.
* Don't subclass this; rather construct it with any context managers
* you need as arguments to it.
* /be sure to have a pointer to this on hand whenever you need to deserialize
* something. If you're dead certain you never need a deserialization
* context, then you can not use this at all and just pass null
* to from_bytes* in place of this.
*/
struct DeserializationManager {
/**
* Various registered managers. Please note that this class
* does *not* own these pointers; you need to keep them
* managed somewhere else. Also ensure lifetime of this
* class is shorter than or the same as those registered
* contexts.
*/
RemoteDeserialization_v registered_v;
DeserializationManager(RemoteDeserialization_v rv) : registered_v(rv) {}
DeserializationManager(const DeserializationManager&) = delete;
DeserializationManager(DeserializationManager&& o)
: registered_v(std::move(o.registered_v)) {}
DeserializationManager& register_ctx(RemoteDeserializationContext_p ctx) {
registered_v.emplace_back(ctx);
return *this;
}
/**
* Lookup the context registered at this DeserializationManager
* whose type is T. Note this means we assume that types uniquely
* identify contexts.
*/
template <typename T>
T& mgr() {
for(auto& candidate : registered_v) {
if(auto* t = dynamic_cast<T*>(candidate))
return *t;
}
assert(false && "Error: no registered manager exists");
struct dead_code {};
throw dead_code{};
}
/**
* As the above, but const.
*/
template <typename T>
const T& mgr() const {
for(auto& candidate : registered_v) {
if(auto* t = dynamic_cast<T*>(candidate))
return t;
}
assert(false && "Error: no registered manager exists");
struct dead_code {};
throw dead_code{};
}
/**
* checks to see if a context of type T has been
* registered with this DeserializationManager.
*/
template <typename T>
bool registered() const {
for(auto& candidate : registered_v) {
if(dynamic_cast<T const*>(candidate))
return true;
}
return false;
}
};
/**
* Just calls sizeof(T)
*/
template <typename T, restrict2(std::is_pod<T>::value)>
auto bytes_size(const T&) {
return sizeof(T);
}
/**
* calls b.bytes_size() when b is a ByteRepresentable;
* calls sizeof(decay_t<decltype(b)>) when b is a POD;
* custom logic is implemented for some STL types.
*/
std::size_t bytes_size(const ByteRepresentable& b);
/**
* effectively strlen().
*/
std::size_t bytes_size(const std::string& b);
template <typename... T>
std::size_t bytes_size(const std::tuple<T...>& t);
/**
* sums the size of both pair elements
*/
template <typename T, typename V>
std::size_t bytes_size(const std::pair<T, V>& pair) {
return bytes_size(pair.first) + bytes_size(pair.second);
}
/**
* all of the elements of this vector, plus one int for the number of elements.
*/
std::size_t bytes_size(const std::vector<bool>& v);
template <typename T>
std::size_t bytes_size(const std::vector<T>& v) {
whenmutilsdebug(
static const auto typenonce_size = bytes_size(
type_name<std::vector<T>>());) if(std::is_pod<T>::value) return v
.size()
* bytes_size(v.back())
+ sizeof(int) whenmutilsdebug(+typenonce_size);
else {
int accum = 0;
for(auto& e : v)
accum += bytes_size(e);
return accum + sizeof(int) whenmutilsdebug(+typenonce_size);
}
}
/**
* Sums the size of all elements of this list, plus one int for the number
* of elements.
*/
template <typename T>
std::size_t bytes_size(const std::list<T>& list) {
if(std::is_pod<T>::value)
return list.size() * bytes_size(list.back()) + sizeof(int);
else {
int accum = 0;
for(const auto& e : list)
accum += bytes_size(e);
return accum + sizeof(int);
}
}
/**
* All the elements of the set, plus one int for the number of elements.
*/
template <typename T>
std::size_t bytes_size(const std::set<T>& s) {
int size = sizeof(int);
for(auto& a : s) {
size += bytes_size(a);
}
return size;
}
/**
* Sums the size of each key and value in the map, plus one int for the
* number of entries
*/
template <typename K, typename V>
std::size_t bytes_size(const std::map<K, V>& m) {
int size = sizeof(int);
for(const auto& p : m) {
size += bytes_size(p.first);
size += bytes_size(p.second);
}
return size;
}
/**
* Sums the size of each key and value in the unordered_map, plus one int for the
* number of entries
*/
template <typename K, typename V>
std::size_t bytes_size(const std::unordered_map<K, V>& m) {
int size = sizeof(int);
for(const auto& p : m) {
size += bytes_size(p.first);
size += bytes_size(p.second);
}
return size;
}
/**
* Sums the size of each element of the tuple
*/
template <typename... T>
std::size_t bytes_size_helper(const T&... t) {
return (bytes_size(t) + ... + 0);
}
template <typename... T>
std::size_t bytes_size(const std::tuple<T...>& t) {
return std::apply(bytes_size_helper<T...>, t);
}
template <typename T>
std::size_t bytes_size(const std::unique_ptr<T>& ptr) {
return 1 + (ptr ? bytes_size(*ptr) : 0);
}
/**
* In-place serialization is also sometimes possible.
* This will take a function that expects buffers to be posted,
* and will post the object (potentially in multiple buffers)
* via repeated calls to the function
*/
template <typename F, typename BR, typename... Args>
std::enable_if_t<std::is_pod<BR>::value> post_object(const F& consumer, const BR& br,
Args&&... args) {
consumer(std::forward<Args>(args)..., (uint8_t*)&br, sizeof(BR));
}
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const ByteRepresentable& br);
#ifdef MUTILS_DEBUG
/**
* Calls b.ensure_registered(dm) when b is a ByteRepresentable;
* returns true when b is POD.
*/
void ensure_registered(ByteRepresentable& b, DeserializationManager& dm);
/**
* Calls b.ensure_registered(dm) when b is a ByteRepresentable;
* returns true when b is POD.
*/
template <typename T, restrict(std::is_pod<T>::value)>
void ensure_registered(const T&, DeserializationManager&) {}
#endif
/**
* calls b.to_bytes(buf) when b is a ByteRepresentable;
* calls std::memcpy() when b is POD. Custom logic
* is implemented for some STL types. When ubuntu
* gets GCC5.0 or better, this will also work if
* b is trivially copyable.
*/
std::size_t to_bytes(const ByteRepresentable& b, uint8_t* buffer);
/**
* extracts the C string (char*) equivalent to this
* std::string and stores it in buf
*/
std::size_t to_bytes(const std::string& b, uint8_t* buffer);
/**
* Calls T::from_bytes(ctx,buf) when T is a ByteRepresentable.
* uses std::memcpy() when T is a POD.
* custom logic is implemented for some STL types.
*/
template <typename T>
std::enable_if_t<std::is_base_of<ByteRepresentable CMA T>::value,
std::unique_ptr<T>>
from_bytes(DeserializationManager* ctx, uint8_t const* buffer) {
static_assert(!std::is_same<std::decay_t<T>, ByteRepresentable>::value,
"Error: must deserialize as implementing type, not as ByteRepresentable");
return T::from_bytes(ctx, buffer);
}
/**
* Calls T::from_bytes(ctx,buf) when T is a ByteRepresentable.
* uses std::memcpy() when T is a POD.
* custom logic is implemented for some STL types.
*/
template <typename T>
std::enable_if_t<std::is_pod<T>::value, std::unique_ptr<std::decay_t<T>>>
from_bytes(DeserializationManager*, uint8_t const* buffer);
/**
* Calls T::from_bytes_noalloc(ctx,buf) when T is a ByteRepresentable.
* returns raw pointer when T is a POD
* custom logic is implemented for some STL types.
*/
template <typename T>
std::enable_if_t<std::is_base_of<ByteRepresentable CMA std::decay_t<T>>::value,
context_ptr<T>>
from_bytes_noalloc(
DeserializationManager* ctx, uint8_t* buffer,
context_ptr<std::decay_t<T>> = context_ptr<std::decay_t<T>>{}) {
return std::decay_t<T>::from_bytes_noalloc(ctx, buffer);
}
template <typename T>
std::enable_if_t<std::is_base_of<ByteRepresentable CMA std::decay_t<T>>::value,
context_ptr<T>>
from_bytes_noalloc(
DeserializationManager* ctx, uint8_t const* const buffer,
context_ptr<const std::decay_t<T>> = context_ptr<const std::decay_t<T>>{}) {
if constexpr(std::is_const<T>::value) {
return std::decay_t<T>::from_bytes_noalloc_const(ctx, buffer);
} else
return std::decay_t<T>::from_bytes_noalloc(ctx, buffer);
}
/**
* Calls T::from_bytes_noalloc(ctx,v) when T is a ByteRepresentable.
* returns raw pointer when T is a POD
* custom logic is implemented for some STL types.
*/
template <typename T>
std::enable_if_t<std::is_pod<T>::value, context_ptr<std::decay_t<T>>>
from_bytes_noalloc(DeserializationManager*, uint8_t* buffer,
context_ptr<std::decay_t<T>> = context_ptr<std::decay_t<T>>{});
template <typename T>
std::enable_if_t<std::is_pod<T>::value, context_ptr<const std::decay_t<T>>>
from_bytes_noalloc(DeserializationManager*, uint8_t const* const buffer,
context_ptr<const std::decay_t<T>> = context_ptr<const std::decay_t<T>>{});
/**
* Calls mutils::from_bytes_noalloc<T>(ctx,buf), dereferences the result, and
* passes it to fun. Returns whatever fun returns. Memory safe, assuming fun
* doesn't do something stupid.
*/
template <typename T, typename F>
auto deserialize_and_run(DeserializationManager* dsm, uint8_t* buf, const F& fun);
/**
* The "marshalled" type is a wrapper for already-serialized types;
*/
struct marshalled : public ByteRepresentable {
const std::size_t size;
uint8_t const* const data;
marshalled(decltype(size) size, decltype(data) data)
: size(size), data(data) {}
std::size_t to_bytes(uint8_t* v) const {
assert(false && "revisit this");
std::memcpy(v, data, size);
return size;
}
std::size_t bytes_size() const { return size; }
#ifdef MUTILS_DEBUG
void ensure_registered(DeserializationManager&) {}
#endif
template <typename DSM>
static std::unique_ptr<marshalled> from_bytes(DSM const* const,
uint8_t const* const) {
static_assert(std::is_same<DSM, void>::value && !std::is_same<DSM, void>::value,
"Do not deserialize into a marshalled. please.");
return nullptr;
}
static context_ptr<marshalled>
from_bytes_noalloc(DeserializationManager const* const, uint8_t* v);
};
/**
* Serialization is also implemented for the following STL types:
* vector
* pair
* string
* set
*/
// end forward-declaring; everything past this point is implementation,
// and not essential to understanding the interface.
// Templates that become true_type when matched to the thing they identify,
// or become false_type if they fail to match, similar to std::is_pod
template <typename>
struct is_pair : std::false_type {};
template <typename T, typename U>
struct is_pair<std::pair<T, U>> : std::true_type {};
template <typename T, typename U>
struct is_pair<const std::pair<T, U>> : std::true_type {};
/* use the definition in mutils/tuple_extras.hpp +21
template <typename> struct is_tuple : std::false_type {};
template <typename...T>
struct is_tuple<std::tuple<T...>> : std::true_type {};
*/
template <typename>
struct is_list : std::false_type {};
template <typename T>
struct is_list<std::list<T>> : std::true_type {};
template <typename T>
struct is_list<const std::list<T>> : std::true_type {};
template <typename>
struct is_map : std::false_type {};
template <typename K, typename V>
struct is_map<std::map<K, V>> : std::true_type {};
template <typename K, typename V>
struct is_map<const std::map<K, V>> : std::true_type {};
template <typename>
struct is_unordered_map : std::false_type {};
template <typename K, typename V>
struct is_unordered_map<std::unordered_map<K, V>> : std::true_type {};
template <typename K, typename V>
struct is_unordered_map<const std::unordered_map<K, V>> : std::true_type {};
template <typename>
struct is_string : std::false_type {};
template <>
struct is_string<std::string> : std::true_type {};
template <>
struct is_string<const std::string> : std::true_type {};
/**
* Constructs a buffer-consuming function that will copy its input to the
* provided destination buffer at the specified index. The created function
* can be used as an input to post_object to make post_object serialize the
* object to a buffer.
* @param index The offset within dest_buf at which the function should copy
* inputs
* @param dest_buf The buffer that should receive bytes read by the function
* @return A function that consumes a byte buffer and writes it to dest_buf
*/
std::function<void(uint8_t const* const, std::size_t)>
post_to_buffer(std::size_t& index, uint8_t* dest_buf);
// Forward declarations of post_object functions for STL types
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::string& str);
template <typename T, typename V>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::pair<T, V>& pair);
template <typename... T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::tuple<T...>& t);
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::vector<bool>& vec);
template <typename T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::vector<T>& vec);
template <typename T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::list<T>& list);
template <typename T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::set<T>& s);
template <typename K, typename V>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::map<K, V>& map);
template <typename K, typename V>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::unordered_map<K, V>& map);
// Forward declarations of to_bytes functions for STL types
template <typename T, restrict(std::is_pod<T>::value)>
std::size_t to_bytes(const T& t, uint8_t* buffer);
std::size_t to_bytes(const std::vector<bool>& vec, uint8_t* buffer);
template <typename T>
std::size_t to_bytes(const std::vector<T>& vec, uint8_t* buffer);
template <typename T>
std::size_t to_bytes(const std::list<T>& list, uint8_t* buffer);
template <typename T, typename V>
std::size_t to_bytes(const std::pair<T, V>& pair, uint8_t* buffer);
template <typename... T>
std::size_t to_bytes(const std::tuple<T...>& tuple, uint8_t* buffer);
template <typename T>
std::size_t to_bytes(const std::set<T>& s, uint8_t* buffer);
template <typename K, typename V>
std::size_t to_bytes(const std::map<K, V>& m, uint8_t* buffer);
template <typename K, typename V>
std::size_t to_bytes(const std::unordered_map<K, V>& m, uint8_t* buffer);
// Forward declarations of from_bytes functions for STL types
template <typename T>
std::unique_ptr<type_check<is_string, T>> from_bytes(DeserializationManager*,
uint8_t const* v);
template <typename T>
context_ptr<type_check<is_string, T>>
from_bytes_noalloc(DeserializationManager*, uint8_t* buffer,
context_ptr<T> = context_ptr<T>{});
template <typename T>
std::enable_if_t<is_string<T>::value, context_ptr<const T>>
from_bytes_noalloc(DeserializationManager*, uint8_t const* const buffer,
context_ptr<const T> = context_ptr<const T>{});
// Note that remove_cv_t is needed here because is_set won't match const set,
// whereas is_string does match const string
template <typename T>
std::enable_if_t<is_set<std::remove_cv_t<T>>::value, std::unique_ptr<T>>
from_bytes(DeserializationManager* ctx, const uint8_t* _buffer);
template <typename T>
context_ptr<type_check<is_set, T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t* buffer,
context_ptr<T> = context_ptr<T>{});
template <typename T>
std::enable_if_t<is_set<std::remove_cv_t<T>>::value, context_ptr<const T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t const* const buffer,
context_ptr<const T> = context_ptr<const T>{});
template <typename T>
std::unique_ptr<type_check<is_pair, T>> from_bytes(DeserializationManager* ctx,
const uint8_t* buffer);
template <typename L>
std::unique_ptr<type_check<is_list, L>> from_bytes(DeserializationManager* ctx,
const uint8_t* buffer);
template <typename T>
context_ptr<type_check<is_pair, T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t* buffer,
context_ptr<T> = context_ptr<T>{});
template <typename T>
std::enable_if_t<is_pair<T>::value, context_ptr<const T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t const* const buffer,
context_ptr<const T> = context_ptr<const T>{});
template <typename T>
context_ptr<type_check<is_tuple, T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t* buffer,
context_ptr<T> = context_ptr<T>{});
template <typename T>
std::enable_if_t<is_tuple<std::remove_cv_t<T>>::value, context_ptr<const T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t const* const buffer,
context_ptr<const T> = context_ptr<const T>{});
template <typename T>
std::unique_ptr<T> boolvec_from_bytes(DeserializationManager* ctx,
uint8_t const* v);
template <typename T>
std::unique_ptr<type_check<is_tuple, T>> from_bytes(DeserializationManager* ctx, uint8_t const* buffer);
template <typename T>
std::enable_if_t<is_vector<std::remove_cv_t<T>>::value, std::unique_ptr<T>>
from_bytes(DeserializationManager* ctx, uint8_t const* buffer);
template <typename T>
context_ptr<type_check<is_vector, T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t* buffer,
context_ptr<T> = context_ptr<T>{});
template <typename T>
std::enable_if_t<is_vector<std::remove_cv_t<T>>::value, context_ptr<const T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t const* const buffer,
context_ptr<const T> = context_ptr<const T>{});
template <typename T>
std::enable_if_t<is_map<T>::value || is_unordered_map<T>::value, std::unique_ptr<T>>
from_bytes(DeserializationManager* ctx, uint8_t const* buffer);
template <typename T>
context_ptr<type_check<is_map, T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t* buffer,
context_ptr<T> = context_ptr<T>{});
template <typename T>
std::enable_if_t<is_map<T>::value, context_ptr<const T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t const* const buffer,
context_ptr<const T> = context_ptr<const T>{});
template <typename T>
context_ptr<type_check<is_unordered_map, T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t* buffer,
context_ptr<T> = context_ptr<T>{});
template <typename T>
std::enable_if_t<is_unordered_map<T>::value, context_ptr<const T>>
from_bytes_noalloc(DeserializationManager* ctx, uint8_t const* const buffer,
context_ptr<const T> = context_ptr<const T>{});
// End forward declarations of STL support
// Implementations of post_object functions for STL types
template <typename T, typename V>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::pair<T, V>& pair) {
post_object(consumer, pair.first);
post_object(consumer, pair.second);
}
template <typename... T>
void post_object_helper(
const std::function<void(uint8_t const* const, std::size_t)>& f,
const T&... t) {
(post_object(f, t), ...);
}
template <typename... T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::tuple<T...>& t) {
// std::apply(std::bind(post_object_helper<T...>,f,/*variadic template?*/), t);
std::apply([consumer](T... args) { post_object_helper(consumer, args...); }, t);
}
template <typename T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::vector<T>& vec) {
whenmutilsdebug(post_object(f, type_name<std::vector<T>>());) int size = vec.size();
consumer((uint8_t*)&size, sizeof(size));
if(std::is_pod<T>::value) {
std::size_t size = vec.size() * bytes_size(vec.back());
consumer((uint8_t*)vec.data(), size);
} else {
for(const auto& e : vec) {
post_object(consumer, e);
}
}
}
template <typename T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::list<T>& list) {
int size = list.size();
consumer((uint8_t*)&size, sizeof(size));
for(const auto& e : list) {
post_object(consumer, e);
}
}
template <typename T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::set<T>& s) {
int size = s.size();
consumer((uint8_t*)&size, sizeof(size));
for(const auto& a : s) {
post_object(consumer, a);
}
}
template <typename K, typename V>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::map<K, V>& map) {
int size = map.size();
consumer((uint8_t*)&size, sizeof(size));
for(const auto& pair : map) {
post_object(consumer, pair.first);
post_object(consumer, pair.second);
}
}
template <typename K, typename V>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& consumer,
const std::unordered_map<K, V>& map) {
int size = map.size();
consumer((uint8_t*)&size, sizeof(size));
for(const auto& pair : map) {
post_object(consumer, pair.first);
post_object(consumer, pair.second);
}
}
template <typename T>
void post_object(const std::function<void(uint8_t const* const, std::size_t)>& f,
const std::unique_ptr<T>& ptr) {
bool has_value(ptr);
f((uint8_t*)&has_value, sizeof(has_value));
if(has_value) {
post_object(f, *ptr);
}
}
// end post_object section
// Implementation of to_bytes functions for STL types
// To reduce code duplication, these are all implemented in terms of post_object
/**
* Special to_bytes for POD types, which just uses memcpy
*/
template <typename T, typename>
std::size_t to_bytes(const T& t, uint8_t* buffer) {
auto res = std::memcpy(buffer, &t, sizeof(T));
assert(res);
(void)res;
return sizeof(T);
}
template <typename T>
std::size_t to_bytes(const std::vector<T>& vec, uint8_t* buffer) {
int vector_size = vec.size();
std::size_t bsize = to_bytes(vector_size, buffer);
for (const auto& e: vec) {
bsize += to_bytes(e,buffer+bsize);
}
return bsize;
}
template <typename T>
std::size_t to_bytes(const std::list<T>& list, uint8_t* buffer) {
int list_size = list.size();
std::size_t bsize = to_bytes(list_size, buffer);
for (const auto& e: list) {
bsize += to_bytes(e,buffer+bsize);
}
return bsize;
}
template <typename T, typename V>
std::size_t to_bytes(const std::pair<T, V>& pair, uint8_t* buffer) {
std::size_t bsize = 0;
bsize += to_bytes(pair.first,buffer+bsize);
bsize += to_bytes(pair.second,buffer+bsize);
return bsize;
}
template <typename T>
void to_bytes_helper1(uint8_t* buffer,std::size_t& offset,const T& t) {
offset += to_bytes(t,buffer+offset);
}
template <typename... T>
std::size_t to_bytes_helper(uint8_t* buffer,const T&... t) {
std::size_t bsize = 0;
(to_bytes_helper1(buffer,bsize,t), ...);
return bsize;
}
template <typename... T>
std::size_t to_bytes(const std::tuple<T...>& tuple, uint8_t* buffer) {
return std::apply([buffer](T... args){return to_bytes_helper(buffer,args...);}, tuple);
}
template <typename T>
std::size_t to_bytes(const std::set<T>& s, uint8_t* buffer) {
int set_size = s.size();
std::size_t bsize = to_bytes(set_size,buffer);
for (const auto& e: s) {
bsize += to_bytes(e, buffer+bsize);
}
return bsize;
}
template <typename K, typename V>
std::size_t to_bytes(const std::map<K, V>& m, uint8_t* buffer) {
int map_size = m.size();
std::size_t bsize = to_bytes(map_size,buffer);
for (const auto& e: m) {
bsize += to_bytes(e.first,buffer+bsize);
bsize += to_bytes(e.second,buffer+bsize);
}
return bsize;
}
template <typename K, typename V>
std::size_t to_bytes(const std::unordered_map<K, V>& m, uint8_t* buffer) {
int map_size = m.size();
std::size_t bsize = to_bytes(map_size,buffer);
for (const auto& e: m) {
bsize += to_bytes(e.first,buffer+bsize);
bsize += to_bytes(e.second,buffer+bsize);
}
return bsize;
}
template <typename T>
std::size_t to_bytes(const std::unique_ptr<T>& ptr, uint8_t* buffer) {
std::size_t index = 0;
post_object(post_to_buffer(index, buffer), ptr);
return bytes_size(ptr);
}
// end to_bytes section
#ifdef MUTILS_DEBUG
// ensure_registered definitions -- these could go anywhere since they don't
// depend on any other functions
void ensure_registered(const std::vector<bool>& v, DeserializationManager& dm);
template <typename T>
void ensure_registered(const std::vector<T>& v, DeserializationManager& dm) {
for(auto& e : v)
ensure_registered(e, dm);
}
template <typename L, typename R>
void ensure_registered(const std::pair<L, R>& v, DeserializationManager& dm) {
ensure_registered(v.first, dm);
ensure_registered(v.second, dm);
}
template <typename T>
void ensure_registered(const std::set<T>& v, DeserializationManager& dm) {
for(auto& e : v)
ensure_registered(e, dm);
}
template <typename T>
void ensure_registered(const std::list<T>& v, DeserializationManager& dm) {
for(auto& e : v)
ensure_registered(e, dm);
}
template <typename T>
void ensure_registered(const std::unique_ptr<T>& ptr, DeserializationManager& dm) {
if(ptr)
ensure_registered(*ptr, dm);
}
// end ensure_registered section
#endif
// from_string definition
template <typename T>
std::unique_ptr<type_check<std::is_integral, T>>
from_string(DeserializationManager*, char const* buffer, std::size_t length) {
return std::make_unique<T>(std::stoll(std::string{buffer, length}));
}
template <typename T>
std::unique_ptr<type_check<std::is_floating_point, T>>
from_string(DeserializationManager*, char const* buffer, std::size_t length) {
return std::make_unique<T>(std::stold(std::string{buffer, length}));
}
template <typename T>
std::unique_ptr<type_check<is_string, T>>
from_string(DeserializationManager*, char const* buffer, std::size_t length) {
return std::make_unique<T>(std::string{buffer, length});
}
// Implementation of from_bytes functions for STL types
template <typename T>
std::enable_if_t<std::is_pod<T>::value, std::unique_ptr<std::decay_t<T>>>
from_bytes(DeserializationManager*, uint8_t const* buffer) {
using T2 = std::decay_t<T>;
if(buffer) {
auto pod = std::make_unique<T2>(*(T2*)buffer);
// std::memcpy(t.get(),v,sizeof(T));
#if __GNUC_PREREQ(9, 0)
return pod; // RVO optimization is default for
#else
return std::move(pod);
#endif
} else
return nullptr;
}
template <typename T>
std::enable_if_t<std::is_pod<T>::value, context_ptr<std::decay_t<T>>>
from_bytes_noalloc(DeserializationManager*, uint8_t* buffer,
context_ptr<std::decay_t<T>>) {
using T2 = std::decay_t<T>;
return context_ptr<T2>{(T2*)buffer};
}
template <typename T>
std::enable_if_t<std::is_pod<T>::value, context_ptr<const std::decay_t<T>>>
from_bytes_noalloc(DeserializationManager*, uint8_t const* const buffer,
context_ptr<const std::decay_t<T>>) {
using T2 = std::decay_t<T>;
return context_ptr<const T2>{(const T2*)buffer};