mirror of
https://github.com/fraillt/bitsery.git
synced 2026-10-23 17:20:58 +00:00
added bitsery.h header
This commit is contained in:
51
README.md
51
README.md
@@ -15,22 +15,17 @@ It has basic features, serialize arithmetic types, enums, containers and text, a
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "BufferReader.h"
|
||||
#include "BufferWriter.h"
|
||||
#include "Serializer.h"
|
||||
#include "Deserializer.h"
|
||||
#include <Bitsery.h>
|
||||
|
||||
enum class MyEnum {
|
||||
V1,V2,V3
|
||||
};
|
||||
|
||||
enum class MyEnum { V1,V2,V3 };
|
||||
struct MyStruct {
|
||||
int i;
|
||||
MyEnum e;
|
||||
std::vector<float> fs;
|
||||
};
|
||||
|
||||
//define how object should be serialized
|
||||
//define how object should be serialized/deserialized
|
||||
SERIALIZE(MyStruct) {
|
||||
return s.
|
||||
value(o.i).
|
||||
@@ -38,27 +33,12 @@ SERIALIZE(MyStruct) {
|
||||
container(o.fs, 100);
|
||||
}
|
||||
|
||||
void print(const char* msg, const MyStruct& v) {
|
||||
std::cout << msg << std::endl;
|
||||
std::cout << "i:" << v.i << std::endl;
|
||||
std::cout << "e:" << (int)v.e << std::endl;
|
||||
std::cout << "fs:";
|
||||
for (auto p:v.fs)
|
||||
std::cout << '\t' << p;
|
||||
std::cout << std::endl << std::endl;
|
||||
}
|
||||
using namespace bitsery;
|
||||
|
||||
int main() {
|
||||
//set some random data
|
||||
MyStruct data{};
|
||||
data.e = MyEnum::V2;
|
||||
data.i = 48465;
|
||||
data.fs.resize(4);
|
||||
float tmp = 4253;
|
||||
for (auto& v: data.fs) {
|
||||
tmp /=2;
|
||||
v = tmp;
|
||||
}
|
||||
MyStruct data{8941, MyEnum::V2, {15.0f, -8.5f, 0.045f}};
|
||||
MyStruct res{};
|
||||
|
||||
//create serializer
|
||||
//1) create buffer to store data
|
||||
@@ -68,32 +48,29 @@ int main() {
|
||||
//3) create serializer
|
||||
Serializer<BufferWriter> ser{bw};
|
||||
|
||||
//call serialize function
|
||||
serialize(ser, data);
|
||||
//serialize object, can also be invoked like this: serialize(ser, data)
|
||||
ser.object(data);
|
||||
|
||||
//flush to buffer
|
||||
//flush to buffer, before creating buffer reader
|
||||
bw.flush();
|
||||
|
||||
MyStruct result{};
|
||||
|
||||
//create deserializer
|
||||
//1) create buffer reader
|
||||
BufferReader br{buffer};
|
||||
//2) create deserializer
|
||||
Deserializer<BufferReader> des{br};
|
||||
|
||||
//call same function with different arguments
|
||||
serialize(des, result);
|
||||
//deserialize same object, can also be invoked like this: serialize(des, data)
|
||||
des.object(res);
|
||||
|
||||
//print results
|
||||
print("initial data", data);
|
||||
print("result", result);
|
||||
//check is equal
|
||||
std::cout << "is equal: " << (data.fs == res.fs && data.i == res.i && data.e == res.e) << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
## Platforms
|
||||
|
||||
This library was tested on
|
||||
* Windows: Visual Studio 2015
|
||||
* Windows: Visual Studio 2015, MinGW (gcc 5.2)
|
||||
* Linux: GCC 5.4, GCC 6.2, Clang 3.9
|
||||
|
||||
|
||||
@@ -20,25 +20,19 @@
|
||||
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
//SOFTWARE.
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "BufferReader.h"
|
||||
#include "BufferWriter.h"
|
||||
#include "Serializer.h"
|
||||
#include "Deserializer.h"
|
||||
#include <Bitsery.h>
|
||||
|
||||
enum class MyEnum {
|
||||
V1,V2,V3
|
||||
};
|
||||
|
||||
enum class MyEnum { V1,V2,V3 };
|
||||
struct MyStruct {
|
||||
int i;
|
||||
MyEnum e;
|
||||
std::vector<float> fs;
|
||||
};
|
||||
|
||||
//define how object should be serialized
|
||||
//define how object should be serialized/deserialized
|
||||
SERIALIZE(MyStruct) {
|
||||
return s.
|
||||
value(o.i).
|
||||
@@ -46,29 +40,12 @@ SERIALIZE(MyStruct) {
|
||||
container(o.fs, 100);
|
||||
}
|
||||
|
||||
void print(const char* msg, const MyStruct& v) {
|
||||
std::cout << msg << std::endl;
|
||||
std::cout << "i:" << v.i << std::endl;
|
||||
std::cout << "e:" << (int)v.e << std::endl;
|
||||
std::cout << "fs:";
|
||||
for (auto p:v.fs)
|
||||
std::cout << '\t' << p;
|
||||
std::cout << std::endl << std::endl;
|
||||
}
|
||||
|
||||
using namespace bitsery;
|
||||
|
||||
int main() {
|
||||
//set some random data
|
||||
MyStruct data{};
|
||||
data.e = MyEnum::V2;
|
||||
data.i = 48465;
|
||||
data.fs.resize(4);
|
||||
float tmp = 4253;
|
||||
for (auto& v: data.fs) {
|
||||
tmp /=2;
|
||||
v = tmp;
|
||||
}
|
||||
MyStruct data{8941, MyEnum::V2, {15.0f, -8.5f, 0.045f}};
|
||||
MyStruct res{};
|
||||
|
||||
//create serializer
|
||||
//1) create buffer to store data
|
||||
@@ -78,24 +55,21 @@ int main() {
|
||||
//3) create serializer
|
||||
Serializer<BufferWriter> ser{bw};
|
||||
|
||||
//call serialize function
|
||||
serialize(ser, data);
|
||||
//serialize object, can also be invoked like this: serialize(ser, data)
|
||||
ser.object(data);
|
||||
|
||||
//flush to buffer
|
||||
//flush to buffer, before creating buffer reader
|
||||
bw.flush();
|
||||
|
||||
MyStruct result{};
|
||||
|
||||
//create deserializer
|
||||
//1) create buffer reader
|
||||
BufferReader br{buffer};
|
||||
//2) create deserializer
|
||||
Deserializer<BufferReader> des{br};
|
||||
|
||||
//call same function with different arguments
|
||||
serialize(des, result);
|
||||
//deserialize same object, can also be invoked like this: serialize(des, data)
|
||||
des.object(res);
|
||||
|
||||
//print results
|
||||
print("initial data", data);
|
||||
print("result", result);
|
||||
//check is equal
|
||||
std::cout << "is equal: " << (data.fs == res.fs && data.i == res.i && data.e == res.e) << std::endl;
|
||||
}
|
||||
46
include/Bitsery.h
Normal file
46
include/Bitsery.h
Normal file
@@ -0,0 +1,46 @@
|
||||
//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.
|
||||
|
||||
|
||||
#ifndef BITSERY_BITSERY_H
|
||||
#define BITSERY_BITSERY_H
|
||||
|
||||
#define BITSERY_MAJOR_VERSION 1
|
||||
#define BITSERY_MINOR_VERSION 0
|
||||
#define BITSERY_PATCH_VERSION 0
|
||||
|
||||
#define BITSERY_QUOTE_MACRO(name) #name
|
||||
#define BITSERY_BUILD_VERSION_STR(major,minor, patch) \
|
||||
BITSERY_QUOTE_MACRO(major) "." \
|
||||
BITSERY_QUOTE_MACRO(minor) "." \
|
||||
BITSERY_QUOTE_MACRO(patch)
|
||||
|
||||
#define BITSERY_VERSION \
|
||||
BITSERY_BUILD_VERSION_STR(BITSERY_MAJOR_VERSION, BITSERY_MINOR_VERSION, BITSERY_PATCH_VERSION)
|
||||
|
||||
|
||||
#include "BufferWriter.h"
|
||||
#include "BufferReader.h"
|
||||
#include "Serializer.h"
|
||||
#include "Deserializer.h"
|
||||
|
||||
#endif //BITSERY_BITSERY_H
|
||||
Reference in New Issue
Block a user