forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.hpp
2346 lines (2058 loc) · 74.4 KB
/
binary.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
// solid/serialization/binary.hpp
//
// Copyright (c) 2007, 2008 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#ifndef SOLID_SERIALIZATION_BINARY_HPP
#define SOLID_SERIALIZATION_BINARY_HPP
#include <typeinfo>
#include <string>
#include <istream>
#include <ostream>
#include "solid/serialization/typeidmap.hpp"
#include "binarybasic.hpp"
#include "solid/system/cassert.hpp"
#include "solid/system/debug.hpp"
#include "solid/utility/common.hpp"
#include "solid/utility/algorithm.hpp"
#include "solid/utility/stack.hpp"
#include "solid/utility/dynamicpointer.hpp"
namespace solid{
namespace serialization{
namespace binary{
BASIC_DECL(int8_t);
BASIC_DECL(uint8_t);
BASIC_DECL(int16_t);
BASIC_DECL(uint16_t);
BASIC_DECL(int32_t);
BASIC_DECL(uint32_t);
BASIC_DECL(int64_t);
BASIC_DECL(uint64_t);
BASIC_DECL(std::string);
typedef void(*StringCheckFncT)(std::string const &/*_rstr*/, const char* /*_pb*/, size_t /*_len*/);
//! Nonintrusive string solid/serialization/deserialization specification
// template <class S>
// S& operator&(std::string &_t, S &_s){
// return _s.push(_t, "string");
// }
template <class S, class T>
void serialize(S &_s, T &_t){
_t.serialize(_s);
}
template <class S, class Ctx, class T>
void serialize(S &_s, T &_t, Ctx &_ctx){
_t.serialize(_s, _ctx);
}
enum {
MIN_STREAM_BUFFER_SIZE = 16 //if the free space for current buffer is less than this value
//storring a stream will end up returning Wait
};
enum ReturnValues{
SuccessE,
WaitE,
FailureE,
ContinueE,
LastReturnValueE,
};
struct Limits{
static Limits const& the();
Limits():stringlimit(InvalidSize()), containerlimit(InvalidSize()), streamlimit(InvalidSize()){}//unlimited by default
size_t stringlimit;
size_t containerlimit;
uint64_t streamlimit;
};
struct ExtendedData{
enum{
MAX_GENERIC_SIZE = 256,
};
typedef void (*DeleteFunctionT)(void*);
union BasicValue{
uint8_t uint8_t_value;
uint32_t uint32_t_value;
uint64_t uint64_t_value;
int64_t int64_t_value;
int32_t int32_t_value;
void* void_value;
};
struct A{
BasicValue values[3];
} tuple_;
struct Generic{
char buffer[MAX_GENERIC_SIZE];
DeleteFunctionT delete_fnc;
size_t type_id;
void *ptr;
} generic_;
const uint8_t& first_uint8_t_value()const{
return tuple_.values[0].uint8_t_value;
}
uint8_t& first_uint8_t_value(){
return tuple_.values[0].uint8_t_value;
}
const uint32_t& first_uint32_t_value()const{
return tuple_.values[0].uint32_t_value;
}
uint32_t& first_uint32_t_value(){
return tuple_.values[0].uint32_t_value;
}
const uint64_t& first_uint64_t_value()const{
return tuple_.values[0].uint64_t_value;
}
uint64_t& first_uint64_t_value(){
return tuple_.values[0].uint64_t_value;
}
const int32_t& first_int32_t_value()const{
return tuple_.values[0].int32_t_value;
}
int32_t& first_int32_t_value(){
return tuple_.values[0].int32_t_value;
}
const int64_t& first_int64_t_value()const{
return tuple_.values[0].int64_t_value;
}
int64_t& first_int64_t_value(){
return tuple_.values[0].int64_t_value;
}
const int64_t& second_int64_t_value()const{
return tuple_.values[1].int64_t_value;
}
int64_t& second_int64_t_value(){
return tuple_.values[1].int64_t_value;
}
const uint64_t& second_uint64_t_value()const{
return tuple_.values[1].uint64_t_value;
}
uint64_t& second_uint64_t_value(){
return tuple_.values[1].uint64_t_value;
}
void* const & first_void_value()const{
return tuple_.values[0].void_value;
}
void*& first_void_value(){
return tuple_.values[0].void_value;
}
void* const & third_void_value()const{
return tuple_.values[2].void_value;
}
void*& third_void_value(){
return tuple_.values[2].void_value;
}
ExtendedData(){
init();
}
explicit ExtendedData(uint8_t _u8){
init();
first_uint8_t_value() = _u8;
}
explicit ExtendedData(uint32_t _u32){
init();
first_uint32_t_value() = _u32;
}
explicit ExtendedData(int32_t _i32){
init();
first_int32_t_value() = _i32;
}
explicit ExtendedData(uint64_t _u64){
init();
first_uint64_t_value() = _u64;
}
explicit ExtendedData(uint64_t _u64, uint64_t _u64_1){
init();
first_uint64_t_value() = _u64; second_uint64_t_value() = _u64_1;
}
explicit ExtendedData(void *_p){
init();
first_void_value() = _p;
}
explicit ExtendedData(int32_t _i32, int64_t _i64_1){
init();
first_int32_t_value() = _i32; second_int64_t_value() = _i64_1;
}
explicit ExtendedData(uint32_t _u32, int64_t _i64_1){
init();
first_uint32_t_value() = _u32; second_int64_t_value() = _i64_1;
}
explicit ExtendedData(int64_t _i64){
init();
first_int64_t_value() = _i64;
}
explicit ExtendedData(int64_t _i64_0, int64_t _i64_1){
init();
first_int64_t_value() = _i64_0; second_int64_t_value() = _i64_1;
}
explicit ExtendedData(uint64_t _u64_0, uint64_t _u64_1, void *_pv){
init();
first_uint64_t_value() = _u64_0;
second_uint64_t_value() = _u64_1;
third_void_value() = _pv;
}
~ExtendedData(){
clear();
}
template <class T>
static void destroyer(void *_pv){
static_cast<T*>(_pv)->~T();
}
template <class T>
static void deleter(void *_pv){
delete static_cast<T*>(_pv);
}
template <class T>
T* generic(const T &_rt){
clear();
T *retval = nullptr;
if(sizeof(T) <= MAX_GENERIC_SIZE){
retval = new(generic_.buffer) T(_rt);
generic_.ptr = retval;
generic_.delete_fnc = &destroyer<T>;
generic_.type_id = typeId<T>();
}else{
retval = new T(_rt);
generic_.ptr = retval;
generic_.delete_fnc = &deleter<T>;
generic_.type_id = typeId<T>();
}
return retval;
}
template <class T>
T* generic(T &&_ut){
clear();
T *retval = nullptr;
if(sizeof(T) <= MAX_GENERIC_SIZE){
retval = new(generic_.buffer) T{std::move(_ut)};
generic_.ptr = retval;
generic_.delete_fnc = &destroyer<T>;
generic_.type_id = typeId<T>();
}else{
retval = new T{std::move(_ut)};
generic_.ptr = retval;
generic_.delete_fnc = &deleter<T>;
generic_.type_id = typeId<T>();
}
return retval;
}
template <class T>
T* genericCast(){
if(generic_.type_id == typeId<T>()){
return static_cast<T*>(generic_.ptr);
}else{
return nullptr;
}
}
template <class T>
const T* genericCast()const{
if(generic_.type_id == typeId<T>()){
return static_cast<T*>(generic_.ptr);
}else{
return nullptr;
}
}
void clear(){
if(generic_.delete_fnc){
(*generic_.delete_fnc)(generic_.buffer);
generic_.delete_fnc = nullptr;
generic_.type_id = 0;
generic_.ptr = nullptr;
generic_.type_id = 0;
}
}
private:
void init(){
tuple_.values[0].uint64_t_value = 0;
tuple_.values[1].uint64_t_value = 0;
tuple_.values[2].uint64_t_value = 0;
generic_.delete_fnc = nullptr;
generic_.type_id = 0;
}
static size_t newTypeId();
template <class T>
static size_t typeId(){
static const size_t id(newTypeId());
return id;
}
};
//===============================================================
//! A base class for binary serializer and deserializer
/*!
The main goals for serializer and deserializer was to
be ease to use and to be reentrant.
The ease of use means that one should do little things to
make a class serializable. E.g. :<br>
<code>
struct RemoteListCommand{<br>
//...<br>
template \< class S><br>
S& operator&(S &_s){<br>
_s.pushContainer(ppthlst, "strlst").push(err, "error").push(tout,"timeout");<br>
_s.push(requid, "requid").push(strpth, "strpth").push(fromv, "from");<br>
_s.push(cmduid.idx, "cmduid.idx").push(cmduid.uid,"cmduid.uid");<br>
return _s;<br>
}<br>
//...<br>
//data:<br>
RemoteList::PathListT *ppthlst;<br>
String strpth;<br>
int err;<br>
uint32_t tout;<br>
fdt::ipc::ConnectionUid conid;<br>
fdt::ipc::CommandUid cmduid;<br>
uint32_t requid;<br>
ObjectUidT fromv;<br>
</code>
<br>
Reentrant means for serializer that:<br>
* you push serializable objects onto the serializer<br>
* you do a loop to actually serialize using a fixed size buffer
until there is nothing to serialize:<br>
<code>
while((rv = ser.run(buf, blen)) == blen){<br>
cnt += rv;<br>
sock.write(buf, rv);<br>
}<br>
if(rv > 0){<br>
sock.write(buf, blen);<br>
}<br>
</code>
For deserializer means something equivalent:<br>
* you push the serializable objects onto the deserializer<br>
* you do a loop where you fed the deserializer, buffers filled
e.g. from a file or a socket etc.
*/
class Base{
public:
void resetLimits(){
lmts = rdefaultlmts;
}
bool ok()const{return !err;}
ErrorConditionT error()const{
return err;
}
ErrorConditionT streamError()const{
return streamerr;
}
void pop(){
fstk.pop();
}
uint64_t const& streamSize()const{
return streamsz;
}
Limits& limits(){
return lmts;
}
protected:
friend class ErrorCategory;
enum Errors{
ERR_NOERROR = 0,
ERR_ARRAY_LIMIT,
ERR_ARRAY_MAX_LIMIT,
ERR_CONTAINER_LIMIT,
ERR_CONTAINER_MAX_LIMIT,
ERR_STREAM_LIMIT,
ERR_STREAM_CHUNK_MAX_LIMIT,
ERR_STREAM_SEEK,
ERR_STREAM_READ,
ERR_STREAM_WRITE,
ERR_STREAM_SENDER,
ERR_STRING_LIMIT,
ERR_STRING_MAX_LIMIT,
ERR_UTF8_LIMIT,
ERR_UTF8_MAX_LIMIT,
ERR_POINTER_UNKNOWN,
ERR_REINIT,
ERR_NO_TYPE_MAP,
ERR_DESERIALIZE_VALUE,
ERR_CROSS_VALUE_SMALL
};
struct FncData;
typedef ReturnValues (*FncT)(Base &, FncData &, void*);
//! Data associated to a callback
struct FncData{
FncData(
FncT _f,
const void *_p,
const char *_n = nullptr,
uint64_t _s = InvalidSize()
): f(_f), p(const_cast<void*>(_p)), n(_n), s(_s){}
FncT f; //!< Pointer to function
void *p; //!< Pointer to data
const char *n; //!< Some name - of the item serialized
uint64_t s; //!< Some size
};
protected:
static ReturnValues setStringLimit(Base& _rd, FncData &_rfd, void */*_pctx*/);
static ReturnValues setStreamLimit(Base& _rd, FncData &_rfd, void */*_pctx*/);
static ReturnValues setContainerLimit(Base& _rd, FncData &_rfd, void */*_pctx*/);
static ErrorConditionT make_error(Errors _err);
Base():rdefaultlmts(Limits::the()), lmts(rdefaultlmts){}
Base(Limits const &_rdefaultlmts):rdefaultlmts(_rdefaultlmts), lmts(rdefaultlmts){}
//! Replace the top callback from the stack
void replace(const FncData &_rfd);
static ReturnValues popExtStack(Base &_rs, FncData &_rfd, void */*_pctx*/);
static const char* default_name;
protected:
typedef Stack<FncData> FncDataStackT;
typedef Stack<ExtendedData> ExtendedDataStackT;
const Limits &rdefaultlmts;
Limits lmts;
ErrorConditionT err;
ErrorConditionT streamerr;
uint64_t streamsz;
ulong uls;
FncDataStackT fstk;
ExtendedDataStackT estk;
};
//===============================================================
template <class T>
struct SerializerPushHelper;
//! A reentrant binary serializer
/*!
See serialization::bin::Base for details
*/
class SerializerBase: public Base{
protected:
template <uint S>
static ReturnValues storeBinary(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <typename T>
static ReturnValues store(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <typename T, class Ser>
static ReturnValues store(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <typename T, class Ser, class Ctx>
static ReturnValues store(Base &_rs, FncData &_rfd, void */*_pctx*/);
static ReturnValues storeUtf8(Base &_rs, FncData &_rfd, void */*_pctx*/);
static ReturnValues storeCrossContinue(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <typename N>
static ReturnValues storeCross(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <typename T, class Ser>
static ReturnValues storeContainer(Base &_rs, FncData &_rfd, void *_pctx){
idbgx(Debug::ser_bin, "store generic container sizeof(iterator) = "<<sizeof(typename T::iterator)<<" "<<_rfd.n);
SerializerBase &rs(static_cast<SerializerBase&>(_rs));
if(!rs.cpb) return SuccessE;
T *c = reinterpret_cast<T*>(_rfd.p);
const char *n = _rfd.n;
if(c){
SOLID_ASSERT(sizeof(typename T::iterator) <= sizeof(ExtendedData));
if(c->size() > rs.lmts.containerlimit){
rs.err = make_error(ERR_CONTAINER_LIMIT);
return FailureE;
}
uint64_t crcsz;
if(not compute_value_with_crc(crcsz, c->size())){
rs.err = make_error(ERR_CONTAINER_MAX_LIMIT);
return FailureE;
}
rs.estk.push(ExtendedData());
//typename T::iterator *pit(new(rs.estk.top().buf) typename T::iterator(c->begin()));
//typename T::const_iterator &pit = *reinterpret_cast<typename T::const_iterator*>(estk.top().buf);
typename T::iterator *pit = rs.estk.top().generic(c->begin());
*pit = c->begin();
rs.estk.push(ExtendedData(crcsz));
_rfd.f = &SerializerBase::storeContainerContinue<T, Ser>;
rs.fstk.push(FncData(&Base::popExtStack, nullptr));
idbgx(Debug::ser_bin, " sz = "<<rs.estk.top().first_uint64_t_value());
rs.fstk.push(FncData(&SerializerBase::template storeCross<uint64_t>, &rs.estk.top().first_uint64_t_value(), n));
}else{
rs.estk.push(ExtendedData(static_cast<uint64_t>(-1)));
rs.fstk.pop();
rs.fstk.push(FncData(&Base::popExtStack, nullptr));
idbgx(Debug::ser_bin, " sz = "<<rs.estk.top().first_uint64_t_value());
rs.fstk.push(FncData(&SerializerBase::template storeCross<uint64_t>, &rs.estk.top().first_uint64_t_value(), n));
}
return ContinueE;
}
template <typename T, class Ser>
static ReturnValues storeContainerContinue(Base &_rs, FncData &_rfd, void */*_pctx*/){
Ser &rs(static_cast<Ser&>(_rs));
using IteratorT = typename T::iterator;
//typename T::iterator &rit = *reinterpret_cast<typename T::iterator*>(rs.estk.top().buf);
ExtendedData &rextdata = rs.estk.top();
IteratorT &rit = *(rextdata.genericCast<IteratorT>());
T *c = reinterpret_cast<T*>(_rfd.p);
if(rs.cpb && rit != c->end()){
rs.push(*rit, _rfd.n);
++rit;
return ContinueE;
}
//TODO:?!how
//rit.T::~const_iterator();//only call the destructor
rs.estk.pop();
return SuccessE;
}
template <typename T, class Ser>
static ReturnValues storeArray(Base &_rs, FncData &_rfd, void */*_pctx*/){
idbgx(Debug::ser_bin, "store generic array "<<_rfd.n);
SerializerBase &rs(static_cast<SerializerBase&>(_rs));
if(!rs.cpb){
rs.estk.pop();
return SuccessE;
}
T *c = reinterpret_cast<T*>(_rfd.p);
const char *n = _rfd.n;
if(c && rs.estk.top().first_uint64_t_value() != static_cast<uint64_t>(-1)){
uint64_t crcsz;
if(rs.estk.top().first_uint64_t_value() > rs.lmts.containerlimit){
rs.err = make_error(ERR_ARRAY_LIMIT);
return FailureE;
}else if(compute_value_with_crc(crcsz, rs.estk.top().first_uint64_t_value())){
_rfd.f = &SerializerBase::storeArrayContinue<T, Ser>;
rs.estk.push(ExtendedData(crcsz));
rs.fstk.push(FncData(&Base::popExtStack, nullptr));
idbgx(Debug::ser_bin, "store array size "<<rs.estk.top().first_uint64_t_value());
rs.fstk.push(FncData(&SerializerBase::template storeCross<uint64_t>, &rs.estk.top().first_uint64_t_value(), n));
}else{
rs.err = make_error(ERR_ARRAY_MAX_LIMIT);
return FailureE;
}
}else{
rs.estk.top().first_uint64_t_value() = -1;
rs.fstk.pop();
rs.fstk.push(FncData(&Base::popExtStack, nullptr));
idbgx(Debug::ser_bin, "store array size "<<rs.estk.top().first_uint64_t_value());
rs.fstk.push(FncData(&SerializerBase::template storeCross<uint64_t>, &rs.estk.top().first_uint64_t_value(), n));
}
return ContinueE;
}
template <typename T, class Ser>
static ReturnValues storeArrayContinue(Base &_rs, FncData &_rfd, void */*_pctx*/){
Ser &rs(static_cast<Ser&>(_rs));
T *c = reinterpret_cast<T*>(_rfd.p);
const uint64_t &rsz(rs.estk.top().first_uint64_t_value());
int64_t &ri(rs.estk.top().second_int64_t_value());
idbgx(Debug::ser_bin, "store generic array cont "<<_rfd.n<<" rsz = "<<rsz<<" ri = "<<ri);
if(rs.cpb && static_cast<uint64_t>(ri) < rsz){
rs.push(c[ri], _rfd.n);
++ri;
return ContinueE;
}
//TODO:?!how
//rit.T::~const_iterator();//only call the destructor
rs.estk.pop();
return SuccessE;
}
static ReturnValues storeStreamBegin(Base &_rs, FncData &_rfd, void */*_pctx*/);
static ReturnValues storeStreamCheck(Base &_rs, FncData &_rfd, void */*_pctx*/);
//! Internal callback for storing a stream
static ReturnValues storeStream(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <class Ser>
static ReturnValues storeCall(Base &_rs, FncData &_rfd, void */*_pctx*/){
Ser &rs(static_cast<Ser&>(_rs));
//const uint32_t val = _rfd.s;
if(!rs.cpb){
rs.estk.pop();
return SuccessE;
}
typename Ser::CallT call = std::move(*rs.estk.top().template genericCast<typename Ser::CallT>());
rs.estk.pop();
call(rs, _rfd.s, rs.err);
if(rs.err){
_rfd.f = storeReturnError;
}else{
_rfd.f = storeReturnSuccess;
}
return ContinueE;
}
template <class Ser, class Ctx>
static ReturnValues storeCall(Base &_rs, FncData &_rfd, void *_pctx){
Ser &rs(static_cast<Ser&>(_rs));
//const uint32_t val = _rfd.s;
if(!rs.cpb){
return SuccessE;
}
Ctx &rctx = *reinterpret_cast<Ctx*>(_pctx);
typename Ser::CallT call = std::move(*rs.estk.top().template genericCast<typename Ser::CallT>());
rs.estk.pop();
call(rs, rctx, _rfd.s, rs.err);
if(rs.err){
_rfd.f = storeReturnError;
}else{
_rfd.f = storeReturnSuccess;
}
return ContinueE;
}
static ReturnValues storeReturnError(Base &_rs, FncData &_rfd, void */*_pctx*/){
SerializerBase &rs(static_cast<SerializerBase&>(_rs));
if(!rs.err){
rs.err = make_error(static_cast<Errors>(_rfd.s));
}
return FailureE;
}
static ReturnValues storeReturnSuccess(Base &_rs, FncData &_rfd, void */*_pctx*/){
return SuccessE;
}
void doPushStringLimit();
void doPushStringLimit(size_t _v);
void doPushStreamLimit();
void doPushStreamLimit(uint64_t _v);
void doPushContainerLimit();
void doPushContainerLimit(size_t _v);
int run(char *_pb, unsigned _bl, void *_pctx);
public:
typedef void ContextT;
enum {IsSerializer = true, IsDeserializer = false};
void clear();
bool empty()const {return fstk.empty();}
static char* storeValue(char *_pd, const uint8_t _val);
static char* storeValue(char *_pd, const uint16_t _val);
static char* storeValue(char *_pd, const uint32_t _val);
static char* storeValue(char *_pd, const uint64_t _val);
protected:
SerializerBase():pb(nullptr), cpb(nullptr), be(nullptr){
tmpstr.reserve(sizeof(ulong));
}
SerializerBase(
Limits const & _rdefaultlmts
):Base(_rdefaultlmts), pb(nullptr), cpb(nullptr), be(nullptr){
tmpstr.reserve(sizeof(ulong));
}
~SerializerBase();
private:
template <class T>
friend struct SerializerPushHelper;
friend class Base;
char *pb;
char *cpb;
char *be;
std::string tmpstr;
};
//===============================================================
template <>
ReturnValues SerializerBase::storeBinary<0>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::storeBinary<1>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::storeBinary<2>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::storeBinary<4>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::storeBinary<8>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<int8_t>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<uint8_t>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<int16_t>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<uint16_t>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<int32_t>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<uint32_t>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<int64_t>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<uint64_t>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::store<std::string>(Base &_rb, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::storeCross<uint8_t>(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::storeCross<uint16_t>(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::storeCross<uint32_t>(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <>
ReturnValues SerializerBase::storeCross<uint64_t>(Base &_rs, FncData &_rfd, void */*_pctx*/);
template <typename T>
ReturnValues SerializerBase::store(Base &_rs, FncData &_rfd, void */*_pctx*/){
//DUMMY - should never get here
return FailureE;
}
template <typename T, class Ser>
ReturnValues SerializerBase::store(Base &_rs, FncData &_rfd, void */*_pctx*/){
idbgx(Debug::ser_bin, "store generic non pointer");
Ser &rs(static_cast<Ser&>(_rs));
if(!rs.cpb) return SuccessE;
T& rt = *((T*)_rfd.p);
rs.fstk.pop();
serialize(rs, rt);
return ContinueE;
}
template <typename T, class Ser, class Ctx>
ReturnValues SerializerBase::store(Base &_rs, FncData &_rfd, void *_pctx){
idbgx(Debug::ser_bin, "store generic non pointer with context");
Ser &rs(static_cast<Ser&>(_rs));
if(!rs.cpb) return SuccessE;
T &rt = *((T*)_rfd.p);
Ctx &rctx = *reinterpret_cast<Ctx*>(_pctx);
rs.fstk.pop();
serialize(rs, rt, rctx);
return ContinueE;
}
template <>
struct SerializerPushHelper<int8_t>{
void operator()(SerializerBase &_rs, int8_t &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<int8_t>, &_rv, _name));
}
};
template <>
struct SerializerPushHelper<uint8_t>{
void operator()(SerializerBase &_rs, uint8_t &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<uint8_t>, &_rv, _name));
}
};
template <>
struct SerializerPushHelper<uint16_t>{
void operator()(SerializerBase &_rs, uint16_t &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<uint16_t>, &_rv, _name));
}
};
template <>
struct SerializerPushHelper<int16_t>{
void operator()(SerializerBase &_rs, int16_t &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<int16_t>, &_rv, _name));
}
};
template <>
struct SerializerPushHelper<uint32_t>{
void operator()(SerializerBase &_rs, uint32_t &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<uint32_t>, &_rv, _name));
}
};
template <>
struct SerializerPushHelper<int32_t>{
void operator()(SerializerBase &_rs, int32_t &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<int32_t>, &_rv, _name));
}
};
template <>
struct SerializerPushHelper<uint64_t>{
void operator()(SerializerBase &_rs, uint64_t &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<uint64_t>, &_rv, _name));
}
};
template <>
struct SerializerPushHelper<int64_t>{
void operator()(SerializerBase &_rs, int64_t &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<int64_t>, &_rv, _name));
}
};
template <>
struct SerializerPushHelper<std::string>{
void operator()(SerializerBase &_rs, std::string &_rv, const char *_name, bool _b = false){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<std::string>, &_rv, _name));
}
};
template <class T>
struct SerializerPushHelper{
template <class Ser>
void operator()(Ser &_rs, T &_rv, const char *_name){
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<T, Ser>, &_rv, _name));
}
template <class Ser>
void operator()(Ser &_rs, T &_rv, const char *_name, bool _b){
typedef typename Ser::ContextT ContextT;
_rs.fstk.push(SerializerBase::FncData(&SerializerBase::store<T, Ser, ContextT>, &_rv, _name));
}
};
//--------------------------------------------------------------
//--------------------------------------------------------------
template <class Ctx = void>
class Serializer;
//--------------------------------------------------------------
template <>
class Serializer<void>: public SerializerBase{
public:
using ContextT = void;
using SerializerT = Serializer<ContextT>;
using BaseT = SerializerBase;
using TypeIdMapT = TypeIdMapSer<SerializerT>;
using CallT = std::function<void(SerializerT&, uint64_t, ErrorConditionT&)>;
Serializer(
const TypeIdMapT *_ptypeidmap = nullptr
): ptypeidmap(_ptypeidmap){
}
Serializer(
Limits const & _rdefaultlmts,
const TypeIdMapT *_ptypeidmap = nullptr
):BaseT(_rdefaultlmts), ptypeidmap(_ptypeidmap){
}
int run(char *_pb, unsigned _bl){
return SerializerBase::run(_pb, _bl, nullptr);
}
SerializerT& pushStringLimit(){
SerializerBase::doPushStringLimit();
return *this;
}
SerializerT& pushStringLimit(size_t _v){
SerializerBase::doPushStringLimit(_v);
return *this;
}
SerializerT& pushStreamLimit(){
SerializerBase::doPushStreamLimit();
return *this;
}
SerializerT& pushStreamLimit(uint64_t _v){
SerializerBase::doPushStreamLimit(_v);
return *this;
}
SerializerT& pushContainerLimit(){
SerializerBase::doPushContainerLimit();
return *this;
}
SerializerT& pushContainerLimit(size_t _v){
SerializerBase::doPushContainerLimit(_v);
return *this;
}
template <size_t V>
SerializerT & push(std::array<uint8_t, V> &_rarray, const char *_name = Base::default_name){
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::storeBinary<0>, _rarray.data(), _name, V));
return *this;
}
//! Schedule a non pointer object for serialization
/*!
The object is only scheduled for serialization so it must remain in memory
up until the serialization will end.
The given name is meaningless for binary serialization, it will be usefull for
text oriented serialization, and I want a common interface for push, so one
can write a single template function for serializing an object.
*/
template <typename T>
SerializerT& push(T &_t, const char *_name = Base::default_name){
SerializerPushHelper<T> sph;
sph(*this, _t, _name);
return *this;
}
template <typename T>
SerializerT& push(T* _pt, const char *_name = Base::default_name){
if(ptypeidmap){
err = ptypeidmap->store(*this, _pt, _name);
if(err){
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::storeReturnError, nullptr, _name, 0));
}
}else{
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::storeReturnError, nullptr, _name, SerializerBase::ERR_NO_TYPE_MAP));
}
return *this;
}
template <typename T1, typename T2>
SerializerT& push(std::pair<T1,T2> &_rpair, const char *_name = Base::default_name){
push(_rpair.second, "second");
push(_rpair.first, "first");
return *this;
}
template <typename T>
SerializerT& push(std::shared_ptr<T> &_rptr, const char *_name = Base::default_name){
if(ptypeidmap){
err = ptypeidmap->store(*this, _rptr.get(), _name);
if(err){
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::storeReturnError, nullptr, _name, SerializerBase::ERR_POINTER_UNKNOWN));
}
}else{
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::storeReturnError, nullptr, _name, SerializerBase::ERR_NO_TYPE_MAP));
}
return *this;
}
//! Schedules a stl style container for serialization
template <typename T>
SerializerT& pushContainer(T &_t, const char *_name = Base::default_name){
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::template storeContainer<T, SerializerT>, (void*)&_t, _name));
return *this;
}
//! Schedules a pointer to a stl style container for serialization
template <typename T>
SerializerT& pushContainer(T *_t, const char *_name = Base::default_name){
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::template storeContainer<T, SerializerT>, (void*)_t, _name));
return *this;
}
SerializerT& pushBinary(void *_p, size_t _sz, const char *_name = Base::default_name){
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::storeBinary<0>, _p, _name, _sz));
return *this;
}
template <typename T>
SerializerT& pushArray(T *_p, const size_t &_rsz, const char *_name = Base::default_name){
SerializerBase::fstk.push(SerializerBase::FncData(&SerializerBase::template storeArray<T, SerializerT>, (void*)_p, _name));
SerializerBase::estk.push(ExtendedData((uint64_t)_rsz, (uint64_t)0));
return *this;
}
template <typename T>
SerializerT& pushDynamicArray(
T* &_rp, const size_t &_rsz, const char *_name = Base::default_name
){