new stream adapter, and lots of refactorings

This commit is contained in:
fraillt
2017-10-06 13:50:04 +03:00
parent 5ede853954
commit f3c9a33849
46 changed files with 967 additions and 701 deletions

View File

@@ -1,5 +1,10 @@
//include bitsery.h to get serialization and deserialization classes
#include <bitsery/bitsery.h>
#include <bitsery/adapters/buffer_adapters.h>
//in ordered to serialize/deserialize data to buffer, include buffer adapter
#include <bitsery/adapter/buffer.h>
//bitsery itself doesn't is lightweight, and doesnt include any unnessessary files,
//traits helps library to know how to use types correctly,
//in this case we'll be using vector both, to serialize/deserialize data and to store use as a buffer.
#include <bitsery/traits/vector.h>
enum class MyEnum:uint16_t { V1,V2,V3 };
@@ -12,13 +17,14 @@ struct MyStruct {
//define how object should be serialized/deserialized
template <typename S>
void serialize(S& s, MyStruct& o) {
s.value4b(o.i);
s.value4b(o.i);//fundamental types (ints, floats, enums) of size 4b
s.value2b(o.e);
s.container4b(o.fs, 10);
s.container4b(o.fs, 10);//resizable containers also requires maxSize, to make it safe from buffer-overflow attacks
};
using namespace bitsery;
//some helper types
using Buffer = std::vector<uint8_t>;
using OutputAdapter = OutputBufferAdapter<Buffer>;
using InputAdapter = InputBufferAdapter<Buffer>;
@@ -29,12 +35,16 @@ int main() {
MyStruct res{};
//create buffer to store data
std::vector<uint8_t> buffer;
Buffer buffer;
//use quick serialization function,
//it will use default configuration to setup all the nesessary steps
//and serialize data to container
auto writtenSize = quickSerialization<OutputAdapter>(buffer, data);
auto writtenSize = startSerialization<OutputAdapter>(buffer, data);
//same as serialization, but returns deserialization state as a pair
//first = error code, second = is buffer was successfully read from begin to the end.
auto state = quickDeserialization<InputAdapter>({buffer.begin(), writtenSize}, res);
auto state = startDeserialization<InputAdapter>(InputAdapter{buffer.begin(), writtenSize}, res);
assert(state.first == ReaderError::NO_ERROR && state.second);
assert(state.first == ReaderError::NoError && state.second);
assert(data.fs == res.fs && data.i == res.i && data.e == res.e);
}