// // Created by fraillt on 17.2.8. // #ifndef BITSERY_SERIALIZERTESTS_H #define BITSERY_SERIALIZERTESTS_H #include #include #include "BufferWriter.h" #include "Serializer.h" #include struct MyStruct1 { MyStruct1(int v1, int v2):i1{v1}, i2{v2} {} MyStruct1():MyStruct1{0,0} {} int i1; int i2; bool operator == (const MyStruct1& rhs) const { return i1 == rhs.i1 && i2 == rhs.i2; } static constexpr size_t SIZE = sizeof(MyStruct1::i1) + sizeof(MyStruct1::i2); }; SERIALIZE(MyStruct1) { return s. value(o.i1). value(o.i2); } enum class MyEnumClass { E1, E2, E3, E4, E5, E6 }; struct MyStruct2 { enum MyEnum { V1, V2, V3, V4, V5, V6 }; MyStruct2(MyEnum e, MyStruct1 s):e1{e}, s1{s} {} MyStruct2():MyStruct2{V1,{0,0}} {} MyEnum e1; MyStruct1 s1; bool operator == (const MyStruct2& rhs) const { return e1 == rhs.e1 && s1 == rhs.s1; } static constexpr size_t SIZE = MyStruct1::SIZE + sizeof(MyStruct2::e1); }; SERIALIZE(MyStruct2) { return s. value(o.e1). object(o.s1); } class SerializationContext { std::vector buf{}; std::unique_ptr bw; std::unique_ptr br; public: Serializer createSerializer() { bw = std::make_unique(buf); return {*bw}; }; size_t getBufferSize() const { return buf.size(); } //since all containers .size() method returns size_t, it cannot be dirrectly serialized, because size_t is platform dependant //this function returns number of bytes writen to buffer, when reading/writing size of container static size_t containerSizeSerializedBytesCount(size_t elemsCount) { return 4; } Deserializer createDeserializer() { bw->flush(); br = std::make_unique(buf); return {*br}; }; }; #endif //BITSERY_SERIALIZERTESTS_H