bitis 0.10.5

Bitwise serialization of messages defined in a data description language with interfaces for rust, python and c++.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198


#include <cfloat>
#include "inttypes.h"
#include <type_traits>
#include <limits>
#include <cstdint>
#include <vector>
#include <array>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <inttypes.h>


inline void print_indent(const int16_t indent) {
    for(int i=0; i < indent; i++) { printf(" "); }
}

template<typename T>
uint64_t calc_bitmask(T bits) {
    static_assert(std::is_integral<T>::value, "calc_bitmask(): T must be an integral type");

    uint64_t val = 1;
    if (bits == 0) return 0;
    for (T i = 1; i < bits; ++i) {
        val = (val << 1) + 1;
    }
    return val;
}
static const uint8_t bit_masks[] = {0, 1, 3, 7, 15, 31, 63, 127, 255};
static const uint8_t max_bitmask = 8;

struct BitisSize {
    std::size_t bits;
    std::size_t bytes;
};


struct BitisSerializer {
    uint16_t cur_cache_u8;
    uint8_t sub_byte_counter;
    std::vector<uint8_t> data_cache;
    std::size_t final_total_bits;

    template<typename T>
    std::size_t add_data_bits(const T *value, uint8_t bits_left) {
        uint8_t bits_left_work = bits_left;
        // ReSharper disable once CppCStyleCast
        const auto data = (uint8_t*) value;

        uint8_t data_index = 0;

        while (bits_left_work > 0) {
            const uint8_t bits_to_use = std::min(bits_left_work, max_bitmask);

            const uint8_t masked_data = data[data_index] & bit_masks[bits_to_use];
            cur_cache_u8 += masked_data << sub_byte_counter;

            sub_byte_counter += bits_to_use;
            if (sub_byte_counter >= 8) {
                data_cache.push_back(static_cast<uint8_t>(cur_cache_u8 & 255));
                sub_byte_counter -= 8;
                cur_cache_u8 >>= 8;
                data_index++;
            }
            bits_left_work -= bits_to_use;
        }
        return bits_left;
    }

    template<typename T, std::size_t BITS>
    std::size_t add_data(const T *value) {
        return add_data_bits(value, BITS);
    }

    BitisSize finalize() {
        final_total_bits = data_cache.size() * 8;
        if (sub_byte_counter > 0) {
            data_cache.push_back(static_cast<uint8_t>(cur_cache_u8 & 255));
            final_total_bits += sub_byte_counter;
        }
        return BitisSize{final_total_bits, data_cache.size()};
    }
};

namespace bitis_helper {
    struct BitiaDeserializerUint8Helper {
        std::size_t bits;
        uint8_t data;
    };
    template<typename T> struct BitiaDeserializerHelper {
        std::size_t bits;
        T data;
    };

    //  convert a type T to a non-cv-qualified type - remove_const<T>
    template <class T> struct remove_const{ typedef T type; };
    template <class T> struct remove_const<T const>{ typedef T type; };
}

struct BitisDeserializer {
    std::size_t cur_read_pos;
    uint8_t sub_byte_counter;
    std::vector<uint8_t> data_cache;

    explicit BitisDeserializer(const std::vector<uint8_t> &data)
    : cur_read_pos(0), sub_byte_counter(0), data_cache(data) { }

    bitis_helper::BitiaDeserializerUint8Helper decode_data_uint8(const uint8_t total_bits) {
        // if (cur_read_pos >= total_bits) {
        //     return bitis_helper::BitiaDeserializerUint8Helper{.bits = 0, .data = 0};
        // }
        uint16_t cur_u16 = data_cache[cur_read_pos];
        if (cur_read_pos+1 < data_cache.size()) {
            // todo what is this for?
            cur_u16 += data_cache[cur_read_pos+1] << 8;
        }
        const auto bits_to_use = std::min(total_bits, max_bitmask);
        const auto d = (uint8_t) ((cur_u16 >> sub_byte_counter) & bit_masks[bits_to_use]);
        sub_byte_counter += bits_to_use;
        if (sub_byte_counter >= 8) {
            cur_read_pos ++;
            sub_byte_counter &= 7;
        }
        return bitis_helper::BitiaDeserializerUint8Helper{.bits = bits_to_use, .data = d};
    }
    template<typename T>
    // bitis_helper::BitiaDeserializerHelper<T> decode_data(T & data, const uint8_t total_bits) {
    std::size_t  decode_data(T & data, const uint8_t total_bits) {
        uint8_t bits_left = total_bits;
        // T data;
        // ReSharper disable once CppCStyleCast
        auto *data_ptr = (uint8_t *) &data;
        uint8_t idx = 0;

        while (bits_left > 0) {
            const auto rd = decode_data_uint8(bits_left);
            data_ptr[idx] = rd.data;
            bits_left -= rd.bits;
            idx++;
        }
        // return bitis_helper::BitiaDeserializerHelper<T>{.bits = total_bits, .data = data};
        return total_bits;
    }
};


// ***************************************************************
struct BitisBool {
    bool value;

    BitisBool() : value(false) {}
    BitisBool(const bool value) : value(value) {}

    // ReSharper disable once CppDFAConstantFunctionResult
    std::size_t serialize(BitisSerializer &ser) {
        const uint8_t data = value ? 1 : 0;
        return ser.add_data<uint8_t, 1>(&data);
    }
    static bitis_helper::BitiaDeserializerHelper<BitisBool> deserialize(BitisDeserializer &des) {
        uint8_t data = 0;
        des.decode_data<uint8_t>(data, 1);
        return bitis_helper::BitiaDeserializerHelper<BitisBool>{.bits = 1, .data = BitisBool(data==1)
        };
    }
    void print(int16_t indent=0) const {
        printf("%s", (value) ? "true" : "false" );
    }
    bool is_equal(const BitisBool &other) const {
        return value == other.value;
    }
    bool operator==(const BitisBool &other) const { return is_equal(other); }
    bool operator!=(const BitisBool &other) const { return !is_equal(other); }
};

// ***************************************************************
template <typename T, uint8_t BITS>
struct IntgralWithGivenBitSize {
    static_assert(std::is_integral<T>::value, "Integral type required for IntgralWithGivenBitSize.");
    static_assert(std::numeric_limits<T>::digits >= BITS, "BITS have to be lower or equal than the number of bits of the type.");

    T value;

    IntgralWithGivenBitSize() : value() {}
    explicit IntgralWithGivenBitSize(T value) : value(value) {}
    // IntgralWithGivenBitSize(T &value) : value(value) {}

    std::size_t serialize(BitisSerializer &ser) {
        uint8_t num_bits = 0;
        T tval = value;

        if (std::is_signed<T>::value) {
            uint8_t tdata;

            if (tval < 0) {
                tdata = 1;
                tval = -tval;
            }
            else tdata = 0;

            ser.add_data<uint8_t, 1>(&tdata);
            num_bits += 1;
        }
        return ser.add_data<T, BITS>(&tval) + num_bits;
    }
    static bitis_helper::BitiaDeserializerHelper<IntgralWithGivenBitSize<T, BITS>>
    deserialize(BitisDeserializer &des) {
        std::size_t num_bits = 0;
        bool is_negative = false;
        if (std::is_signed<T>::value) {
            const auto r = BitisBool::deserialize(des);
            num_bits += r.bits;
            is_negative = r.data.value;
        }
        T v(0);
        auto dr = des.decode_data(v, BITS);
        if (std::is_signed<T>::value) {
            if (is_negative) {
                v = -v;
            }
        }
        return  bitis_helper::BitiaDeserializerHelper<IntgralWithGivenBitSize>{
            .bits = num_bits+dr, .data = IntgralWithGivenBitSize(v)
        };
    }
    void print(int16_t indent=0) {
        // ReSharper disable once CppCStyleCast
        printf("%" PRIi64 " [%sint_%d]", (int64_t)value, (std::is_signed<T>::value) ? "" : "u", BITS);
    }
    bool is_equal(const IntgralWithGivenBitSize &other) const {
        return value == other.value;
    }
    bool operator==(const IntgralWithGivenBitSize& other) const { return is_equal(other); }
    bool operator!=(const IntgralWithGivenBitSize& other) const { return !is_equal(other); }
};

template <typename T>
struct BitisFloatingPoint {
    static_assert(std::is_floating_point<T>::value, "Floating point type required for BitisFloatingPoint.");

    T value;

    BitisFloatingPoint() : value() {}
    explicit BitisFloatingPoint(T v) : value(v) { }

    std::size_t serialize(BitisSerializer &ser) {
        return ser.add_data<T, sizeof(T)*8>(&value) ;
    }
    static bitis_helper::BitiaDeserializerHelper<BitisFloatingPoint> deserialize(
        BitisDeserializer &des) {
        T val(0.0);
        auto r = des.decode_data(val, sizeof(T)*8);
        return bitis_helper::BitiaDeserializerHelper<BitisFloatingPoint>{
        .bits = sizeof(T)*8, .data = BitisFloatingPoint(val)};
    }
    void print(int16_t indent=0) const {
        // ReSharper disable once CppCStyleCast
        printf("%f [%s]", value, (std::numeric_limits<T>::digits == FLT_MANT_DIG) ? "float" : "double");
    }
    bool is_equal(const BitisFloatingPoint &other) const {
        return value == other.value;
    }
    bool operator==(const BitisFloatingPoint &other) const { return is_equal(other); }
    bool operator!=(const BitisFloatingPoint &other) const { return !is_equal(other); }
};

// ***************************************************************
template <typename T>
struct BitisOptional {
    typedef T ValT;
    T value;
    bool is_none;

    BitisOptional() : value(), is_none(true) {}
    BitisOptional(T val) : value(val), is_none(false) {}
    explicit BitisOptional(T value, bool _is_none) : value(value), is_none(_is_none) {}
    static BitisOptional create_none() { return BitisOptional(T(), true); }
    static BitisOptional create_val(T v) { return BitisOptional(v, false); }

    std::size_t serialize(BitisSerializer &ser) {
        std::size_t num_bits = 1;
        BitisBool(!is_none).serialize(ser);
        if (!is_none) {
            num_bits += value.serialize(ser);
        }
        return num_bits;
    }
    static bitis_helper::BitiaDeserializerHelper<BitisOptional> deserialize(BitisDeserializer &des) {
        std::size_t num_bits = 0;
        const auto r = BitisBool::deserialize(des);
        num_bits += r.bits;
        const bool is_none = !r.data.value;
        if (!is_none) {
            auto rr = T::deserialize(des);
            num_bits += rr.bits;
            return bitis_helper::BitiaDeserializerHelper<BitisOptional>{.bits = num_bits,
                .data = BitisOptional::create_val(rr.data)};
        }
        return bitis_helper::BitiaDeserializerHelper<BitisOptional>{.bits = num_bits,
            .data = BitisOptional::create_none()};
    }
    void print(int16_t indent=0) {
        if (is_none) { printf("None [Optional]"); }
        else {
            value.print(indent);
            printf("[Optional]");
        }
    }
    bool is_equal(const BitisOptional &other) const {
        if (is_none != other.is_none) { return false; }

        if (!is_none) {
            return value.is_equal(other.value);
        }
        return true;
    }
    bool operator==(const BitisOptional &other) const { return is_equal(other); }
    bool operator!=(const BitisOptional &other) const { return !is_equal(other); }
};
// ***************************************************************
template <typename T, uint8_t MAX_BITS, uint8_t BIT_PACKS>
struct DynInteger {
    T value;

    DynInteger() : value() {}
    explicit DynInteger(T value) : value(value) {}

    std::size_t serialize(BitisSerializer &ser) {
        int num_bits = 1;
        uint8_t tdata;
        T tvalue = value;
        // indicator of the sign: send one if negative
        if (std::is_signed<T>::value) {
            if (value < 0) {
                tdata = 1;
                tvalue = -tvalue;
            }
            else tdata = 0;

            ser.add_data<uint8_t, 1>(&tdata);
            num_bits += 1;
        }
        // tvalue is a positive value here

        // first marker
        {   auto b = BitisBool(tvalue>0);
            b.serialize(ser);
        }
        //
        uint64_t tval = tvalue; // should be always a positive value
        uint8_t max_num_bits_left = MAX_BITS;
        while (tval > 0) {
            const auto cur_num_bits = std::min(max_num_bits_left, BIT_PACKS);
            ser.add_data_bits(&tval, cur_num_bits);

            tval >>= cur_num_bits;
            num_bits += cur_num_bits;
            max_num_bits_left -= cur_num_bits;

            if (max_num_bits_left > 0) {
                auto b = BitisBool(tval>0);
                b.serialize(ser);
                num_bits += 1;
            }
        }
        return num_bits;
    }
    static bitis_helper::BitiaDeserializerHelper<DynInteger> deserialize(BitisDeserializer &des) {
        bool is_negative = false;
        uint8_t bits_num = 1;
        uint64_t tval = 0;

        if (std::is_signed<T>::value) {
            auto rv = BitisBool::deserialize(des);
            bits_num += rv.bits;
            is_negative = rv.data.value;
        }

        auto rv = BitisBool::deserialize(des);
        bool further_data_to_read = rv.data.value;
        uint8_t shift_bits = 0;
        uint8_t max_num_bits_left = MAX_BITS;
        while (further_data_to_read) {
            const auto cur_num_bits = std::min(max_num_bits_left, BIT_PACKS);

            uint64_t cdata = 0;
            const auto r = des.decode_data(cdata, cur_num_bits);

            bits_num += r;
            tval += cdata << shift_bits;
            shift_bits += cur_num_bits;
            max_num_bits_left -= cur_num_bits;

            if (max_num_bits_left > 0) {
                rv = BitisBool::deserialize(des);
                further_data_to_read = rv.data.value;
                bits_num += 1;
            }
            else { further_data_to_read = false; }
        }
        if (is_negative) {
            tval = -tval;
        }
        // ReSharper disable once CppCStyleCast
        return bitis_helper::BitiaDeserializerHelper<DynInteger>{.bits=bits_num, .data=DynInteger((T)tval)};
    }
    void print(int16_t indent=0) {
        // ReSharper disable once CppCStyleCast
        printf("%" PRIi64 " [%sint_%dd%d]", (int64_t)value, (std::is_signed<T>::value) ? "" : "u", MAX_BITS, BIT_PACKS);
    }
    bool is_equal(const DynInteger &other) const {
        return value == other.value;
    }
    bool operator==(const DynInteger &other) const { return is_equal(other); }
    bool operator!=(const DynInteger &other) const { return !is_equal(other); }
};

// ***************************************************************
template <typename T, std::size_t ARRAY_SIZE>
struct FixedArray {
    typedef T ValT;
    // check that T is bitis type
    //T values[ARRAY_SIZE];
    std::array<T, ARRAY_SIZE> values;

    FixedArray() : values() {}
    explicit FixedArray(const std::array<T, ARRAY_SIZE> &v) : values(v) {
        // for (std::size_t i = 0; i < ARRAY_SIZE; i++) { values[i] = v[i]; }
    }

    std::size_t serialize(BitisSerializer &ser) {
        std::size_t num_bits = 0;
        for (std::size_t i=0; i < ARRAY_SIZE; i++) {
            num_bits += values[i].serialize(ser);
        }
        return num_bits;
    }
    static bitis_helper::BitiaDeserializerHelper<FixedArray> deserialize(BitisDeserializer &des) {
        std::size_t num_bits = 0;
        //T values[ARRAY_SIZE];
        std::array<T, ARRAY_SIZE> values;

        for (std::size_t i=0; i < ARRAY_SIZE; i++) {
            auto dr = T::deserialize(des);
            values[i] = dr.data;
            num_bits += dr.bits;
        }
        return bitis_helper::BitiaDeserializerHelper<FixedArray>{.bits = num_bits, .data = FixedArray(values)};
    }
    void print(int16_t indent=0) {
        // ReSharper disable once CppCStyleCast
        printf("[");
        for (std::size_t i=0; i < ARRAY_SIZE; i++) {
            values[i].print(indent);
            if (i != ARRAY_SIZE-1) printf(", ");
        }
        printf("]{%zu}", ARRAY_SIZE);
    }
    bool is_equal(const FixedArray &other) const {
        for (int i = 0; i < ARRAY_SIZE; i++) {
            if (values[i] != other.values[i]) { return false; }
        }
        return true;
    }
    bool operator==(const FixedArray& other) const { return is_equal(other); }
    bool operator!=(const FixedArray& other) const { return !is_equal(other); }
};

#define ENUM_INSTANCE(x) struct x { static constexpr const char* name = #x; }


// ***************************************************************
template <typename T, uint8_t DYN_BITS>
struct DynArray {
    std::vector<T> values;
    typedef T ValT;

    DynArray() : values() { }
    explicit DynArray(const std::vector<T> &v) : values(v) { }

    std::size_t serialize(BitisSerializer &ser) {
        std::size_t num_bits = 0;
        auto data_size = DynInteger<uint32_t, 32, DYN_BITS>(values.size());
        num_bits += data_size.serialize(ser);
        for (std::size_t i=0; i < values.size(); i++) {
            // num_bits += ser.add_data(values[i].serialize(ser));
            num_bits += values[i].serialize(ser);
        }
        return num_bits;
    }
    static bitis_helper::BitiaDeserializerHelper<DynArray> deserialize(BitisDeserializer &des) {
        std::size_t num_bits = 0;
        auto tbits = sizeof(T)*8;
        auto rv_size = DynInteger<uint32_t, 32, DYN_BITS>::deserialize(des);
        num_bits += rv_size.bits;

        auto tvalues = std::vector<T>(rv_size.data.value);
        for (std::size_t i=0; i < rv_size.data.value; i++) {
            auto dr = T::deserialize(des);
            tvalues[i] = dr.data;
            num_bits += dr.bits;
            // T cdata;
            // auto bits = des.decode_data(cdata, tbits);
            // tvalues[i] = cdata;
            // num_bits += bits;
        }
        return bitis_helper::BitiaDeserializerHelper<DynArray>{.bits=num_bits, .data=DynArray(tvalues)};
    }
    void print(int16_t indent=0) {
        // ReSharper disable once CppCStyleCast
        printf("[");
        for (std::size_t i=0; i < values.size(); i++) {
            values[i].print(indent);
            if (i != values.size()-1) printf(", ");
        }
        printf("]{dyn:%zu}", values.size());
    }
    bool is_equal(const DynArray &other) const {
        if (values.size() != other.values.size()) { return false; }
        for (int i = 0; i < values.size(); i++) {
            if (values[i] != other.values[i]) { return false; }
        }
        return true;
    }
    bool operator==(const DynArray& other) const { return is_equal(other); }
    bool operator!=(const DynArray& other) const { return !is_equal(other); }
};

// ***************************************************************
template <uint8_t DYN_BITS>
struct BinaryBase {
    BinaryBase() : values() {}
    explicit BinaryBase(const std::vector<uint8_t> &v) : values(v) {}

    std::vector<uint8_t> *get_u8_vec() { return &this->values; }

    std::size_t serialize(BitisSerializer &ser) {
        std::size_t num_bits = 0;
        auto data_size = DynInteger<uint32_t, 32, DYN_BITS>(values.size());
        num_bits += data_size.serialize(ser);

        for (std::size_t i=0; i < values.size(); i++) {
            auto cv = values[i];
            num_bits += ser.add_data<uint8_t, 8>(&cv);
        }
        return num_bits;
    }

protected:
    static bitis_helper::BitiaDeserializerHelper<BinaryBase> deserialize_base(BitisDeserializer &des) {
        std::size_t num_bits = 0;
        auto tbits = 8;
        auto rv_size = DynInteger<uint32_t, 32, DYN_BITS>::deserialize(des);
        num_bits += rv_size.bits;

        auto tvalues = std::vector<uint8_t>(rv_size.data.value);
        for (std::size_t i=0; i < rv_size.data.value; i++) {
            uint8_t cdata = 0;
            auto bits = des.decode_data(cdata, tbits);
            tvalues[i] = cdata;
            num_bits += bits;
        }
        return bitis_helper::BitiaDeserializerHelper<BinaryBase>{.bits=num_bits, .data=BinaryBase(tvalues)};
    }

    std::vector<uint8_t> values;
};

// ***************************************************************
template <uint8_t DYN_BITS>
struct Binary : BinaryBase<DYN_BITS>{

    Binary() : BinaryBase<DYN_BITS>() {}
    explicit Binary(const std::vector<uint8_t> &v) : BinaryBase<DYN_BITS>() { }

    void set(const std::vector<uint8_t> &v) {
        this->values = v;
    }

    static bitis_helper::BitiaDeserializerHelper<Binary> deserialize(BitisDeserializer &des) {
        auto v = BinaryBase<DYN_BITS>::deserialize_base(des);
        return bitis_helper::BitiaDeserializerHelper<Binary>{.bits=v.bits, .data=Binary(*v.data.get_u8_vec())};
    }

    void print(int16_t indent=0) {
        // ReSharper disable once CppCStyleCast
        printf("'");
        for (std::size_t i=0; i < this->value.values.size(); i++) {
            printf("%c", this->value.values[i]);
        }
        printf("' {d%u}", DYN_BITS);
    }

    bool is_equal(const Binary &other) const {
        return this->value == other.value;
    }
    bool operator==(const Binary& other) const { return is_equal(other); }
    bool operator!=(const Binary& other) const { return !is_equal(other); }
};

template <uint8_t DYN_BITS>
struct BitisAString : BinaryBase<DYN_BITS>{

    BitisAString() : BinaryBase<DYN_BITS>() {}
    explicit BitisAString(const char* s) { set(s); }
    explicit BitisAString(const std::vector<uint8_t> &v) : BinaryBase<DYN_BITS>(v) { }

    void set(const char* s) {
        this->values.clear();
        this->values.reserve(strlen(s));
        for (int i = 0; s[i] != '\0'; i++) {
            this->values.push_back(s[i]);
        }
    }
    const char *get() {
        // ReSharper disable once CppCStyleCast
        return (const char *) this->values.data();
    }

    static bitis_helper::BitiaDeserializerHelper<BitisAString> deserialize(BitisDeserializer &des) {
        auto v = BinaryBase<DYN_BITS>::deserialize_base(des);
        return bitis_helper::BitiaDeserializerHelper<BitisAString>{.bits=v.bits, .data=BitisAString(*v.data.get_u8_vec())};
    }

    void print(int16_t indent=0) {
        // ReSharper disable once CppCStyleCast
        printf("'");
        for (std::size_t i=0; i < this->values.size(); i++) {
            printf("%c", this->values[i]);
        }
        printf("' {d%u}", DYN_BITS);
    }
    bool is_equal(const BitisAString &other) const {
        return this->values == other.values;
    }
    bool operator==(const BitisAString& other) const { return is_equal(other); }
    bool operator!=(const BitisAString& other) const { return !is_equal(other); }
};

// ***************************************************************
enum FixPrecisionMinMaxEnum {Ok, Underflow, Overflow};

template <uint8_t BITS, int64_t MIN_IVALUE, int64_t MAX_IVALUE>
struct FixPrecisionMinMax {
    static_assert(BITS > 2);

    FixPrecisionMinMax() : value(0.), state(Overflow) { }
    explicit FixPrecisionMinMax(const double &v) { // NOLINT(*-pro-type-member-init)
        set(v);
    }

    void set(const double &v) {
        value = 0; state = Ok;

        if (v < MIN_IVALUE) { state = Underflow; }
        else if (v > MAX_IVALUE) { state = Overflow; }
        else { value = v; }
        FixPrecisionMinMax::u64_to_val(val_to_u64(value, state), this->value, this->state);
    }
    double get_value() const { return value; }
    FixPrecisionMinMaxEnum get_state() const { return state; }

    static uint64_t val_to_u64(double value, FixPrecisionMinMaxEnum state) {
        const auto max_value = calc_bitmask(BITS);
        uint64_t v;
        if (state == Underflow) { v = 0; }
        else if (state == Overflow) { v = max_value; }
        else {
            auto of = (value - ((double)MIN_IVALUE));
            auto diff = (double)(MAX_IVALUE - MIN_IVALUE);
            auto mv = (double)max_value - 2.;
            v = lround(of / diff * mv) + 1;

            // v = (uint64_t) ((value - ((double)MIN_IVALUE))
            //     / ((double)(MAX_IVALUE - MIN_IVALUE))
            //     * (double)(max_value - 2)) + 1;
        }
        return v;
    }
    static void u64_to_val(uint64_t vu, double &value, FixPrecisionMinMaxEnum &state) {
        const auto max_value = calc_bitmask(BITS);

        if(vu == 0) { state = Underflow; }
        else if(vu == max_value) { state = Overflow; }
        else {
            state = Ok;
            value = (((double)vu-1.) / ((double)(max_value-2)) *
                ((double)(MAX_IVALUE-MIN_IVALUE))) + (double)MIN_IVALUE;
        }
    }

    std::size_t serialize(BitisSerializer &ser) {
        std::size_t num_bits = 0;
        auto max_value = calc_bitmask(BITS);

        uint64_t v = FixPrecisionMinMax::val_to_u64(value, state);
/*        if (state == Underflow) { v = 0; }
        else if (state == Overflow) { v = max_value; }
        else {
            v = lround((value - ((double)MIN_IVALUE))
                / ((double)(MAX_IVALUE - MIN_IVALUE))
                * (double)(max_value - 2)) + 1;
        }*/
        auto t = IntgralWithGivenBitSize<uint64_t, BITS>{v};
        num_bits += t.serialize(ser);
        return num_bits;
    }
    static bitis_helper::BitiaDeserializerHelper<FixPrecisionMinMax> deserialize(BitisDeserializer &des) {
        // auto max_value = calc_bitmask(BITS);

        auto d = FixPrecisionMinMax(0.0);
        auto data_u64 = IntgralWithGivenBitSize<uint64_t, BITS>::deserialize(des);
        // printf("data_u64: %" PRIu64, data_u64.data.value);
        FixPrecisionMinMax::u64_to_val(data_u64.data.value, d.value, d.state);

/*        if(data_u64.data.value == 0) {
            d.state = Underflow;
        } else if(data_u64.data.value == max_value) {
            d.state = Overflow;
        } else {
            d.state = Ok;
            d.value = (((double)data_u64.data.value-1.) / (static_cast<double>(max_value-2)) *
                ((double)(MAX_IVALUE-MIN_IVALUE))) + (double)MIN_IVALUE;
        }*/

        return bitis_helper::BitiaDeserializerHelper<FixPrecisionMinMax>{.bits = BITS, .data = d };
    }
    void print(int16_t indent=0) {
        if (state == Underflow) {
            printf("Underflow");
        }
        else if (state == Overflow) {
            printf("Overflow");
        }
        else {
            printf("%f", value);
        }
        printf(" [FP[%ld,%ld]]", MIN_IVALUE, MAX_IVALUE);
    }

    bool is_equal(const FixPrecisionMinMax &other) const {
        if (state == Underflow) {
            if (other.state != Underflow) { return false; }
        }
        else if (state == Overflow) {
            if (other.state != Overflow) { return false; }
        }
        else {
            if (value != other.value) { return false; }
        }
        return true;
    }
    bool operator==(const FixPrecisionMinMax &other) const { return is_equal(other); }
    bool operator!=(const FixPrecisionMinMax &other) const { return !is_equal(other); }

private:
    double value;
    FixPrecisionMinMaxEnum state;
};

// ***************************************************************
// ***************************************************************
namespace bitis_helper {
    template<typename ...Args> struct Collector{ };

    template<typename T, uint32_t IDX> struct ETN {
        typedef T type;
        typedef std::integral_constant<uint32_t, IDX> id;
    };

    // ***
    template<typename ...> struct EnumeratedListImpl;
    template<typename T, uint32_t CIDX, typename ... ETs, typename TT, typename ... TTs>
    struct EnumeratedListImpl<Collector<ETs...>, ETN<T, CIDX>, TT, TTs...> {
        typedef typename EnumeratedListImpl<Collector<ETs..., ETN<T, CIDX>>,
            ETN<TT, CIDX+1>, TTs...>::type type;
    };
    template<typename T, uint32_t CIDX, typename ... ETs>
    struct EnumeratedListImpl<Collector<ETs...>, ETN<T, CIDX>> {
        typedef Collector<ETs..., ETN<T, CIDX>> type;
    };

    template<typename ... > struct EnumeratedList;
    template<typename T, typename ... Ts>
    struct EnumeratedList<T, Ts ...> {
        typedef typename EnumeratedListImpl<Collector<>, ETN<T, 0>, Ts...>::type type;
    };

    template<typename ... > struct EnumeratedListCollector;
    template<typename T, typename ... Ts>
    struct EnumeratedListCollector<Collector<T, Ts ...>> {
        typedef typename EnumeratedListImpl<Collector<>, ETN<T, 0>, Ts...>::type type;
    };

    template<typename ...> struct ContainsType;
    template<typename U, typename T> struct ContainsType<U, Collector<T>> { static constexpr auto value = false; };
    template<typename U> struct ContainsType<U, Collector<U>> { static constexpr auto value = true; };
    template<typename U, typename... Ts> struct ContainsType<U, Collector<U, Ts...>> {
        static constexpr auto value = true;
    };
    template<typename U, typename T, typename... Ts> struct ContainsType<U, Collector<T, Ts...>> {
        static constexpr auto value = ContainsType<U, Collector<Ts...>>::value;
    };
}

// ***************************************************************
namespace bitis_enum_helper {
    using namespace bitis_helper;

    template<typename ...> struct EnumID_Impl;
    template<typename SEL_ENUM, typename T, typename ... Ts>
    struct EnumID_Impl<SEL_ENUM, Collector<T, Ts ...>> {
        static size_t get_id() {
            return EnumID_Impl<SEL_ENUM, Collector<Ts ...>>::get_id();
        }
    };
    template<typename T, typename ... Ts>
    struct EnumID_Impl<typename T::type, Collector<T, Ts ...>> {
        static size_t get_id() { return T::id::value; }
    };
    template<typename SEL_ENUM>
    struct EnumID_Impl<SEL_ENUM, Collector<>> {
        // ReSharper disable once CppStaticAssertFailure
        static_assert(false, "Enum unknown");
        static size_t get_id() { return 0; }
    };
    template<typename ...> struct EnumName_Impl;
    template<typename T, typename ... Ts>
    struct EnumName_Impl<Collector<T, Ts ...>> {
        static const char *get_name(uint32_t enum_id) {
            if (enum_id == T::id::value) {
                return T::type::name;
            }
            return EnumName_Impl<Collector<Ts ...>>::get_name(enum_id);
        }
    };
    template<>
    struct EnumName_Impl<Collector<>> {
        static const char *get_name(uint32_t enum_id) { return nullptr; }
    };

    template<typename ENUM_TO_FIND, typename ENUMERATED_ENUM_COL>
    uint32_t get_id() {
        return EnumID_Impl<ENUM_TO_FIND, ENUMERATED_ENUM_COL>::get_id();
    }
    template<typename ENUMERATED_ENUM_COL>
    static const char *get_name(uint32_t enum_id) {
        return EnumName_Impl<ENUMERATED_ENUM_COL>::get_name(enum_id);
    }
}


// template<typename ...> struct BitisEnum;
template<typename ES_COLLECTOR, typename DEFAULT_ENUM, uint8_t DYN_BITS>
struct BitisEnum {
    DynInteger<uint32_t, 32, DYN_BITS> value;

    typedef ES_COLLECTOR EnumCollector;
    typedef typename bitis_helper::EnumeratedListCollector<ES_COLLECTOR>::type EnumeratedEnumCollector;

    static_assert(bitis_helper::ContainsType<DEFAULT_ENUM, EnumCollector>::value);

    explicit BitisEnum() : value(bitis_enum_helper::get_id<DEFAULT_ENUM, EnumeratedEnumCollector>()) {}

private:
    explicit BitisEnum(uint32_t enum_id) : value(enum_id) {}

public:
    template<typename SET_ENUM>
    static BitisEnum create_enum() {
        auto id_in = bitis_enum_helper::get_id<SET_ENUM, EnumeratedEnumCollector>();
        return BitisEnum(id_in);
    }

    template<typename SET_ENUM>
    void set_enum() {
        static_assert(bitis_helper::ContainsType<SET_ENUM, EnumCollector>::value);
        this->value.value = bitis_enum_helper::get_id<SET_ENUM, EnumeratedEnumCollector>();
    }
    template<typename SET_ENUM>
    bool is_enum() const {
        static_assert(bitis_helper::ContainsType<SET_ENUM, EnumCollector>::value);
        auto id_in = bitis_enum_helper::get_id<SET_ENUM, EnumeratedEnumCollector>();
        return this->value.value == id_in;
    }

    std::size_t serialize(BitisSerializer &ser) {
        return value.serialize(ser);
    }
    static bitis_helper::BitiaDeserializerHelper<BitisEnum> deserialize(BitisDeserializer &des) {
        auto r = DynInteger<uint32_t, 32, DYN_BITS>::deserialize(des);
        return bitis_helper::BitiaDeserializerHelper<BitisEnum>{.bits=r.bits, .data=BitisEnum(r.data.value)};
    }

    void print(int16_t indent=0) {
        auto v = bitis_enum_helper::get_name<EnumeratedEnumCollector>(value.value);
        // ReSharper disable once CppCStyleCast
        printf("%s (id:%" PRIu32 ") [enum_%u]", v, value.value, DYN_BITS);
    }

    bool is_equal(const BitisEnum &other) const {
        return this->value == other.value;
    }
    bool operator==(const BitisEnum &other) const { return is_equal(other); }
    bool operator!=(const BitisEnum &other) const { return !is_equal(other); }

    // BitisEnum &operator=(const BitisEnum &other) {
    //     this->value = other.value;
    //     return *this;
    // }
    // BitisEnum &operator=(const DynInteger<uint32_t, DYN_BITS> &other) {
    //     this->value = other.value;
    //     return *this;
    // }
};



namespace message_helper{
    using namespace bitis_helper;

    // ***
    template<typename ...> struct MessageAttribute;
    template<typename T, uint32_t IDX> struct MessageAttribute<ETN<T, IDX>> {
        typedef std::integral_constant<uint32_t, IDX> id;
        typedef T type;
        T value;
    };

    template<typename ...> struct MessageTE;
    template<typename ... Ts>
    struct MessageTE<Collector<Ts ...>> : MessageAttribute<Ts> ... { };

    template<typename ... Ts>
    struct MessageT : MessageTE<typename EnumeratedList<Ts...>::type> {

    };


    template<typename ...> struct MessageT_Impl;
    template<typename Msg_STRUCT, typename T, typename ... Ts>
    struct MessageT_Impl<Msg_STRUCT, Collector<T, Ts ...>> {
        size_t serialize(Msg_STRUCT *d, BitisSerializer &ser) {
            auto dd = static_cast<MessageAttribute<T>*>(d);

            std::size_t r = dd->value.serialize(ser);

            MessageT_Impl<Msg_STRUCT, Collector<Ts ...>> inner;
            return r + inner.serialize(d, ser);
        }
        size_t deserialize(Msg_STRUCT *d, BitisDeserializer &des) {
            auto dd = static_cast<MessageAttribute<T>*>(d);

            // auto dd = reinterpret_cast<MessageAttribute<T>*>(d);
            bitis_helper::BitiaDeserializerHelper<typename MessageAttribute<T>::type> r =
                MessageAttribute<T>::type::deserialize(des);
            dd->value = r.data;

            MessageT_Impl<Msg_STRUCT, Collector<Ts ...>> inner;
            return r.bits + inner.deserialize(d, des);
        }

        void print(Msg_STRUCT *d, int16_t indent, const char **attr_names) {
            print_indent(indent);
            printf("%s: ", attr_names[MessageAttribute<T>::id::value]);

            auto dd = static_cast<MessageAttribute<T>*>(d);
            dd->value.print(indent);
            if (indent >= 0) printf(",\n");
            else printf(", ");

            MessageT_Impl<Msg_STRUCT, Collector<Ts ...>> inner;
            return inner.print(d, indent, attr_names);
        }
    };
    template<typename Msg_STRUCT>
    struct MessageT_Impl<Msg_STRUCT, Collector<>> {
        // ReSharper disable once CppMemberFunctionMayBeStatic
        size_t serialize(Msg_STRUCT *d, BitisSerializer &ser) { return 0; }
        size_t deserialize(Msg_STRUCT *d, BitisDeserializer &) { return 0; }
        void print(Msg_STRUCT *, int16_t , const char **) { }
    };

    template<typename ...> struct MessageT_ImplStart;
    template<typename ... Ts>
    struct MessageT_ImplStart<MessageT<Ts ...>> {
        size_t serialize(MessageT<Ts ...> *d, BitisSerializer &ser) {
            MessageT_Impl<MessageT<Ts ...>, typename EnumeratedList<Ts ...>::type> inner;
            return inner.serialize(d, ser);
        }
        size_t deserialize(MessageT<Ts ...> *d, BitisDeserializer &des) {
            MessageT_Impl<MessageT<Ts ...>, typename EnumeratedList<Ts ...>::type> inner;
            return inner.deserialize(d, des);
        }
        void print(MessageT<Ts ...> *d, int16_t indent, const char **attr_names) {
            MessageT_Impl<MessageT<Ts ...>, typename EnumeratedList<Ts ...>::type> inner;
            return inner.print(d, indent, attr_names);
        }
    };

    template<typename Msg>
    size_t msg_serialize(Msg *d, BitisSerializer &ser) {
        // ReSharper disable once CppCStyleCast
        auto *dt = (typename Msg::MsgT*) d;

        MessageT_ImplStart<typename Msg::MsgT> inner;
        return inner.serialize(dt, ser);
    }
    template<typename Msg>
    static bitis_helper::BitiaDeserializerHelper<Msg> msg_deserialize(BitisDeserializer &des) {
        Msg buffer;
        // ReSharper disable once CppCStyleCast
        auto *dt = (typename Msg::MsgT*) &buffer;

        MessageT_ImplStart<typename Msg::MsgT> inner;
        auto r = inner.deserialize(dt, des);

        return bitis_helper::BitiaDeserializerHelper<Msg>(
            // ReSharper disable once CppCStyleCast
            bitis_helper::BitiaDeserializerHelper<Msg>{.bits=r, .data = *((Msg*)dt)});
    }
    template<typename Msg>
    void msg_print(Msg *d, int16_t indent, const char **attr_names) {
        // ReSharper disable once CppCStyleCast
        auto *dt = (typename Msg::MsgT*) d;
        MessageT_ImplStart<typename Msg::MsgT> inner;
        return inner.print(dt, indent, attr_names);
    }
}

namespace oneof_helper {
    using namespace bitis_helper;

    template<typename...> struct MaxSizeof;
    template<typename T> struct MaxSizeof<T> {
        static constexpr size_t value = sizeof(T);
    };
    template<typename T, typename... Ts> struct MaxSizeof<T, Ts...> {
      static constexpr size_t value = std::max(sizeof(T), MaxSizeof<Ts...>::value);
    };

    template<typename ...> struct UnionT;
    template<typename ... Ts>
    struct UnionT {
    private:
        void *data[MaxSizeof<Ts ...>::value] = {};
    public:
        template<typename T>
        void set(T v) {
            static_assert(ContainsType<T, Collector<Ts...>>::value);
            *((T*)data) = v;
        }
        template<typename T>
        T *get() const {
            static_assert(ContainsType<T, Collector<Ts...>>::value);
            return (T*)data;
        }
    };


    template<typename ...> struct OneOfT_Impl;
    template<typename OOT_STRUCT, typename T, typename ... Ts>
    struct OneOfT_Impl<OOT_STRUCT, Collector<T, Ts ...>> {
        size_t serialize(OOT_STRUCT *d, BitisSerializer &ser) {
            // ReSharper disable once CppCStyleCast
            auto data_pointer = d->template get_oo<T>();
            if (data_pointer) {
                // ReSharper disable once CppCStyleCast
                const std::size_t r = data_pointer->serialize(ser);
                //printf("inner bits: %lu\n", r);
                return r;
            }
            OneOfT_Impl<OOT_STRUCT, Collector<Ts ...>> inner;
            return inner.serialize(d, ser);
        }
        size_t deserialize_union(OOT_STRUCT *d, BitisDeserializer &des) {
            auto data_pointer = d->template get_oo<typename remove_const<T>::type>();
            if (data_pointer) {
                auto r = T::OOType::deserialize(des);
                d->oo_value.set(r.data);
                return r.bits;
            }
            OneOfT_Impl<OOT_STRUCT, Collector<Ts ...>> inner;
            return inner.deserialize_union(d, des);
        }
        void oneof_print(OOT_STRUCT *d, int16_t indent) {
            auto data_pointer = d->template get_oo<typename remove_const<T>::type>();
            if (data_pointer) {
                data_pointer->print(indent);
                return;
            }
            OneOfT_Impl<OOT_STRUCT, Collector<Ts ...>> inner;
            return inner.oneof_print(d, indent);
        }
        bool is_equal(const OOT_STRUCT *self, const OOT_STRUCT *other) const {
            auto data_pointer_self = self->template get_oo<typename remove_const<T>::type>();
            auto data_pointer_other = other->template get_oo<typename remove_const<T>::type>();
            if (data_pointer_self || data_pointer_other) {
                if (data_pointer_self==nullptr || data_pointer_other==nullptr) {
                    return false; }
                return data_pointer_self->is_equal(*data_pointer_other);
            }
            OneOfT_Impl<OOT_STRUCT, Collector<Ts ...>> inner;
            return inner.is_equal(self, other);
        }
    };
    template<typename OOT_STRUCT>
    struct OneOfT_Impl<OOT_STRUCT, Collector<>> {
        // ReSharper disable once CppMemberFunctionMayBeStatic
        size_t serialize(OOT_STRUCT *, BitisSerializer &) { return 0; }
        size_t deserialize_union(OOT_STRUCT *, BitisDeserializer &) { return 0; }
        void oneof_print(OOT_STRUCT *, int16_t ) { }
        bool is_equal(const OOT_STRUCT *self, const OOT_STRUCT *other) const  { return false; }
    };

    template<typename ...> struct OneOfT_ImplStart;
    template<typename OOT_STRUCT, typename ... Ts>
    struct OneOfT_ImplStart<OOT_STRUCT, Collector<Ts ...>> {
        size_t serialize(OOT_STRUCT *d, BitisSerializer &ser) {
            // serialize enum for selected type
            auto bits_num = d->oo_selector.serialize(ser);
            //printf("oo_selector bits_num: %lu\n", bits_num);

            // serialize selected type
            OneOfT_Impl<OOT_STRUCT, Collector<Ts ...>> inner;
            return bits_num + inner.serialize(d, ser);
        }
        static bitis_helper::BitiaDeserializerHelper<OOT_STRUCT> deserialize(BitisDeserializer &des) {
            std::size_t bit_num = 0;

            auto d = OOT_STRUCT();
            auto r = OOT_STRUCT::T_OOEnum::deserialize(des);
            bit_num += r.bits;
            d.oo_selector = r.data;

            OneOfT_Impl<OOT_STRUCT, Collector<Ts ...>> inner;
            bit_num += inner.deserialize_union(&d, des);
            return bitis_helper::BitiaDeserializerHelper<OOT_STRUCT>{.bits=bit_num, .data=d};
        }
        void oneof_print(OOT_STRUCT *d, int16_t indent) {
            d->oo_selector.print(indent);
            printf(" -> ");
            OneOfT_Impl<OOT_STRUCT, Collector<Ts ...>> inner;
            inner.oneof_print(d, indent);
        }
        bool is_equal(const OOT_STRUCT *self, const OOT_STRUCT *other) const {
            OneOfT_Impl<OOT_STRUCT, Collector<Ts ...>> inner;
            return inner.is_equal(self, other);
        }
    };

    template<typename OOTS>
    size_t oneof_serialize(OOTS *d, BitisSerializer &ser) {
        OneOfT_ImplStart<OOTS, typename OOTS::T_OOEnum::EnumCollector> inner;
        return inner.serialize(d, ser);
    }

    template<typename OOTS>
    BitiaDeserializerHelper<OOTS> oneof_deserialize(BitisDeserializer &des) {
        OneOfT_ImplStart<OOTS, typename OOTS::T_OOEnum::EnumCollector> inner;
        return inner.deserialize(des);
    }

    template<typename OOTS>
    void oneof_print(OOTS *d, int16_t indent) {
        // ReSharper disable once CppCStyleCast
        OneOfT_ImplStart<OOTS, typename OOTS::T_OOEnum::EnumCollector> inner;
        inner.oneof_print(d, indent);
    }

    template<typename OOTS>
    bool oneof_is_equal(const OOTS *self, const OOTS *other) {
        // OneOfT_ImplStart<OOTS, typename OOTS::OOEnum::EnumCollector> inner;
        // return inner.serialize(d, ser);
        OneOfT_ImplStart<typename remove_const<OOTS>::type,typename OOTS::T_OOEnum::EnumCollector> inner;
        return inner.is_equal(self, other);
    }
}


template<typename T>
std::vector<uint8_t> serialize(T & msg) {
    auto ser = BitisSerializer();
    // auto r = serialize(a, ser);
    msg.serialize(ser);
    auto r = ser.finalize();
    return ser.data_cache;
}

template<typename T>
T deserialize(const std::vector<uint8_t>& data) {
    auto des = BitisDeserializer(data);
    auto dd = T::deserialize(des);
    return dd.data;
}