mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-08 08:13:56 +00:00
maxSize archive modifying function now works with extensions
This commit is contained in:
59
examples/file_stream.cpp
Normal file
59
examples/file_stream.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <bitsery/bitsery.h>
|
||||
//in order to work with streams include stream adapter
|
||||
#include <bitsery/adapter/stream.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
enum class MyEnum:uint16_t { V1,V2,V3 };
|
||||
struct MyStruct {
|
||||
uint32_t i;
|
||||
MyEnum e;
|
||||
double f;
|
||||
};
|
||||
|
||||
//define how object should be serialized/deserialized
|
||||
template <typename S>
|
||||
void serialize(S& s, MyStruct& o) {
|
||||
s.value4b(o.i);
|
||||
s.value2b(o.e);
|
||||
s.value8b(o.f);
|
||||
};
|
||||
|
||||
using namespace bitsery;
|
||||
|
||||
//some helper types
|
||||
using Stream = std::fstream;
|
||||
using IOAdapter = IOStreamAdapter;
|
||||
|
||||
int main() {
|
||||
//set some random data
|
||||
MyStruct data{8941, MyEnum::V2, 0.045};
|
||||
MyStruct res{};
|
||||
|
||||
//open file stream for writing and reading
|
||||
auto fileName = "test_file.bin";
|
||||
Stream s{fileName, s.binary | s.trunc | s.out};
|
||||
if (!s.is_open()) {
|
||||
std::cout << "cannot open " << fileName << " for writing\n";
|
||||
return 0;
|
||||
}
|
||||
//use same quick serialization function
|
||||
//streams do not return written size
|
||||
quickSerialization<IOAdapter>(s, data);
|
||||
|
||||
s.close();
|
||||
//reopen for reading
|
||||
|
||||
s.open(fileName, s.binary | s.in);
|
||||
if (!s.is_open()) {
|
||||
std::cout << "cannot open " << fileName << " for reading\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
//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<IOAdapter>(s, res);
|
||||
|
||||
assert(state.first == ReaderError::NoError && state.second);
|
||||
assert(data.f == res.f && data.i == res.i && data.e == res.e);
|
||||
}
|
||||
45
examples/flexible_assert_linux_x64.cpp
Normal file
45
examples/flexible_assert_linux_x64.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <bitsery/bitsery.h>
|
||||
#include <bitsery/adapter/buffer.h>
|
||||
#include <bitsery/flexible.h>
|
||||
#include <bitsery/flexible/vector.h>
|
||||
|
||||
|
||||
struct MyStruct {
|
||||
int i;
|
||||
unsigned short s;
|
||||
std::vector<long> vl;
|
||||
long long ll;
|
||||
|
||||
template <typename S>
|
||||
void serialize(S& s) {
|
||||
//now we can use flexible syntax with
|
||||
//member function has same name as parameter
|
||||
s.archive(this->s, i, vl, ll);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
using namespace bitsery;
|
||||
|
||||
//some helper types
|
||||
using Buffer = std::vector<uint8_t>;
|
||||
using OutputAdapter = OutputBufferAdapter<Buffer>;
|
||||
using InputAdapter = InputBufferAdapter<Buffer>;
|
||||
|
||||
int main() {
|
||||
//this will only work on linux or mac x64
|
||||
bitsery::assertFundamentalTypeSizes<2,4,8,8>();
|
||||
//set some random data
|
||||
MyStruct data{8941, 3, {15l, -8l, 045l}, 8459845ll};
|
||||
MyStruct res{};
|
||||
|
||||
//serialization, deserialization flow is unchanged as in basic usage
|
||||
Buffer buffer;
|
||||
auto writtenSize = quickSerialization<OutputAdapter>(buffer, data);
|
||||
|
||||
auto state = quickDeserialization<InputAdapter>({buffer.begin(), writtenSize}, res);
|
||||
|
||||
assert(state.first == ReaderError::NoError && state.second);
|
||||
assert(data.vl == res.vl && data.s == res.s && data.i == res.i && data.ll == res.ll);
|
||||
}
|
||||
47
examples/flexible_syntax.cpp
Normal file
47
examples/flexible_syntax.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <bitsery/bitsery.h>
|
||||
#include <bitsery/adapter/buffer.h>
|
||||
//include flexible header, to use flexible syntax
|
||||
#include <bitsery/flexible.h>
|
||||
//we also need additional traits to work with container types,
|
||||
//instead of including <bitsery/traits/vector.h> for vector traits, now we also need traits to work with flexible types.
|
||||
//so include everything from <bitsery/flexible/...> instead of <bitsery/traits/...>
|
||||
//otherwise we'll get static assert error, saying to define serialize function.
|
||||
#include <bitsery/flexible/vector.h>
|
||||
|
||||
enum class MyEnum:uint16_t { V1,V2,V3 };
|
||||
struct MyStruct {
|
||||
uint32_t i;
|
||||
MyEnum e;
|
||||
std::vector<float> fs;
|
||||
|
||||
//define serialize function as usual
|
||||
template <typename S>
|
||||
void serialize(S& s) {
|
||||
//now we can use flexible syntax with
|
||||
s.archive(i, e, fs);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
using namespace bitsery;
|
||||
|
||||
//some helper types
|
||||
using Buffer = std::vector<uint8_t>;
|
||||
using OutputAdapter = OutputBufferAdapter<Buffer>;
|
||||
using InputAdapter = InputBufferAdapter<Buffer>;
|
||||
|
||||
int main() {
|
||||
//set some random data
|
||||
MyStruct data{8941, MyEnum::V2, {15.0f, -8.5f, 0.045f}};
|
||||
MyStruct res{};
|
||||
|
||||
//serialization, deserialization flow is unchanged as in basic usage
|
||||
Buffer buffer;
|
||||
auto writtenSize = quickSerialization<OutputAdapter>(buffer, data);
|
||||
|
||||
auto state = quickDeserialization<InputAdapter>({buffer.begin(), writtenSize}, res);
|
||||
|
||||
assert(state.first == ReaderError::NoError && state.second);
|
||||
assert(data.fs == res.fs && data.i == res.i && data.e == res.e);
|
||||
}
|
||||
Reference in New Issue
Block a user