mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-08 08:13:56 +00:00
written more tests and first code example
This commit is contained in:
11
examples/CMakeLists.txt
Normal file
11
examples/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
|
||||
file(GLOB EXAMPLE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
|
||||
|
||||
FOREACH(EXAMPLE ${EXAMPLE_FILES})
|
||||
get_filename_component(EXAMPLE_NAME ${EXAMPLE} NAME_WE)
|
||||
add_executable(${EXAMPLE_NAME} ${EXAMPLE})
|
||||
ENDFOREACH()
|
||||
|
||||
70
examples/basic_usage.cpp
Normal file
70
examples/basic_usage.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// Created by fraillt on 17.2.9.
|
||||
//
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "BufferReader.h"
|
||||
#include "BufferWriter.h"
|
||||
#include "Serializer.h"
|
||||
#include "Deserializer.h"
|
||||
|
||||
enum class MyEnum {
|
||||
V1,V2,V3
|
||||
};
|
||||
|
||||
struct MyStruct {
|
||||
int i;
|
||||
MyEnum e;
|
||||
std::vector<float> fs;
|
||||
};
|
||||
|
||||
SERIALIZE(MyStruct) {
|
||||
return s.
|
||||
value(o.i).
|
||||
value(o.e).
|
||||
container(o.fs);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//create serializer
|
||||
std::vector<uint8_t> buffer;
|
||||
BufferWriter bw{buffer};
|
||||
Serializer<BufferWriter> ser{bw};
|
||||
//call serialize function
|
||||
serialize(ser, data);
|
||||
//this is required if using bit operations
|
||||
bw.flush();
|
||||
|
||||
MyStruct result{};
|
||||
//create deserializer
|
||||
BufferReader br{buffer};
|
||||
Deserializer<BufferReader> des{br};
|
||||
|
||||
//call same function with different arguments
|
||||
serialize(des, result);
|
||||
|
||||
//print results
|
||||
print("initial data", data);
|
||||
print("result", result);
|
||||
}
|
||||
Reference in New Issue
Block a user