mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-08 08:13:56 +00:00
turorial 2in1
This commit is contained in:
committed by
fraillt
parent
a1104a9b95
commit
79ee168893
@@ -1,70 +1,62 @@
|
||||
//MIT License
|
||||
//
|
||||
//Copyright (c) 2017 Mindaugas Vinkelis
|
||||
//
|
||||
//Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
//of this software and associated documentation files (the "Software"), to deal
|
||||
//in the Software without restriction, including without limitation the rights
|
||||
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
//copies of the Software, and to permit persons to whom the Software is
|
||||
//furnished to do so, subject to the following conditions:
|
||||
//
|
||||
//The above copyright notice and this permission notice shall be included in all
|
||||
//copies or substantial portions of the Software.
|
||||
//
|
||||
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
//SOFTWARE.
|
||||
|
||||
#include <vector>
|
||||
#include <bitsery/bitsery.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
enum class MyEnum:uint16_t { V1,V2,V3 };
|
||||
struct MyStruct {
|
||||
uint32_t i;
|
||||
MyEnum e;
|
||||
std::vector<float> fs;
|
||||
struct Vector3f {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
bool operator == (const Vector3f& o) const {
|
||||
return x == o.x && y == o.y && z == o.z;
|
||||
}
|
||||
};
|
||||
|
||||
//define how object should be serialized/deserialized
|
||||
SERIALIZE(MyStruct) {
|
||||
return s.
|
||||
value4(o.i).
|
||||
value2(o.e).
|
||||
container4(o.fs, 10);
|
||||
struct Player {
|
||||
Vector3f pos;
|
||||
char name[50];
|
||||
};
|
||||
|
||||
using namespace bitsery;
|
||||
|
||||
int main() {
|
||||
//set some random data
|
||||
MyStruct data{8941, MyEnum::V2, {15.0f, -8.5f, 0.045f}};
|
||||
MyStruct res{};
|
||||
SERIALIZE(Player) {
|
||||
s.value4(o.pos.x);
|
||||
s.value4(o.pos.y);
|
||||
s.value4(o.pos.z);
|
||||
s.text1(o.name);
|
||||
return s;
|
||||
}
|
||||
|
||||
//create serializer
|
||||
//1) create buffer to store data
|
||||
std::vector<uint8_t> buffer;
|
||||
//2) create buffer writer that is able to write bytes or bits to buffer
|
||||
BufferWriter bw{buffer};
|
||||
//3) create serializer
|
||||
Player createData() {
|
||||
Player data;
|
||||
data.pos.x = 0.45f;
|
||||
data.pos.y = 50.9f;
|
||||
data.pos.z = -15687.87f;
|
||||
strcpy(data.name,"Yolo");
|
||||
return data;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const Player data = createData();
|
||||
Player res{};
|
||||
|
||||
std::vector<uint8_t> buf;
|
||||
|
||||
BufferWriter bw{buf};
|
||||
Serializer<BufferWriter> ser{bw};
|
||||
|
||||
//serialize object, can also be invoked like this: serialize(ser, data)
|
||||
ser.object(data);
|
||||
serialize(ser, data);
|
||||
|
||||
//flush to buffer, before creating buffer reader
|
||||
bw.flush();
|
||||
|
||||
//create deserializer
|
||||
//1) create buffer reader
|
||||
BufferReader br{buffer};
|
||||
//2) create deserializer
|
||||
BufferReader br{buf};
|
||||
Deserializer<BufferReader> des{br};
|
||||
|
||||
//deserialize same object, can also be invoked like this: serialize(des, data)
|
||||
des.object(res);
|
||||
assert(data.fs == res.fs && data.i == res.i && data.e == res.e);
|
||||
serialize(des, res);
|
||||
|
||||
std::cout << "deserializer state: " << des.isValid() << std::endl
|
||||
<< "buffer completed: " << br.isCompleted() << std::endl
|
||||
<< "pos equals: " << (res.pos == data.pos) << std::endl
|
||||
<< "name equals: " << (strcmp(res.name, data.name) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user