https://github.com/redskittlefox/binarylove3
Simple C++ 20 Serialization Library that works out of the box with aggregate types!
https://github.com/redskittlefox/binarylove3
cpp cpp20 cpp20-library serialization serialization-library
Last synced: 8 months ago
JSON representation
Simple C++ 20 Serialization Library that works out of the box with aggregate types!
- Host: GitHub
- URL: https://github.com/redskittlefox/binarylove3
- Owner: RedSkittleFox
- License: mit
- Created: 2021-01-28T20:35:43.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-09-11T23:26:05.000Z (over 4 years ago)
- Last Synced: 2023-07-03T02:54:33.761Z (over 2 years ago)
- Topics: cpp, cpp20, cpp20-library, serialization, serialization-library
- Language: C++
- Homepage:
- Size: 79.1 KB
- Stars: 17
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README


# BinaryLove3
Simple C++ 20 Serialization Library that works out of the box with aggregate types!
# Requirements
BinaryLove3 is a c++20 only library.
Library can serialize only types matching these requirements:
* trivial types (e.g. `uint32_t`, `struct { uint32_t a; float_t b; }`):
Types matching requirements for [`std::is_trivial`](https://en.cppreference.com/w/cpp/types/is_trivial).
* agregate types (e.g. `std::pair`, `struct { uint32_t a; std::vector b; }`):
Types matching requirements for [`std::is_aggregate`](https://en.cppreference.com/w/cpp/types/is_aggregate).
* iterable types (e.g. `std::vector`):
Type is required to be compatible with [`std::begin`](https://en.cppreference.com/w/cpp/iterator/begin), [`std::end`](https://en.cppreference.com/w/cpp/iterator/end) and [`std::inserter`](https://en.cppreference.com/w/cpp/iterator/inserter). Additionally member type `value_type` is also required.
If type's iterator fulfils [`std::random_access_iterator`](https://en.cppreference.com/w/cpp/iterator/random_access_iterator) requirement and `value_type` matches requirement [`is_trivial`](https://en.cppreference.com/w/cpp/types/is_trivial) then optimisations will be made.
* types providing specializations for `BinaryLove3::serialize` and `BinaryLove3::deserialize`. Refer to [Providing custom serialization methods](https://github.com/RedSkittleFox/BinaryLove3#providing-custom-serialization-methods)
# Usage
```cpp
// demo.cpp
#include
#include
#include
#include
#include
#include
#include "BinaryLove3.hpp"
struct foo
{
uint32_t v0 = 3;
uint32_t v1 = 2;
float_t v2 = 2.5f;
char v3 = 'c';
struct
{
std::vector vec_of_trivial = { 1, 2, 3 };
std::vector vec_of_nontrivial = { "I am a Fox!", "In a big Box!" };
std::string str = "Foxes can fly!";
std::list non_random_access_container = { 3, 4, 5 };
} non_trivial;
struct
{
uint32_t v0 = 1;
uint32_t v1 = 2;
} trivial;
};
auto main() -> int32_t
{
foo out = { 4, 5, 6.7f, 'd', {{5, 4, 3, 2}, {"cc", "dd"}, "Fly me to the moon..." , {7, 8, 9}}, {3, 4} };
auto data = BinaryLove3::serialize(bobux);
foo in;
BinaryLove3::deserialize(data, in);
return int32_t(0);
}
```
# Providing custom serialization methods.
One can provide custom serialization methods by creating specializations for the following functions:
* `std::vector serialize(const T& var_)`
* `bool deserialize(const std::byte*& cur_, const std::byte* end_, T& obj_)`
```cpp
#include
class foo
{
friend std::vector serialize(const foo& var_);
friend bool deserialize(const std::byte*& cur_, const std::byte* end_, foo& obj_);
private:
uint32_t m_a;
std::vector m_b;
public:
void foo_method();
}
std::vector serialize(const foo& var_)
{
auto ret = BinaryLove3::serialize(var_.m_a);
auto data = BinaryLove3::serialize(var_.m_b);
ret.insert(std::end(insert), std::begin(data), std::end(data));
return ret;
}
bool deserialize(const std::byte*& cur_, const std::byte* end_, foo& obj_)
{
uint32_t a;
std::vector b;
if(!BinaryLove3::deserialize(cur_, end_, a))
return false;
if(!BinaryLove3::deserialize(cur_, end_, b))
return false;
obj_.m_a = std::move(a);
obj_.m_b = std::move(b);
return true;
}
```
# Notes
This library works only with up to 10 member variables of an aggregate type right now. This will be expanded in the near future. Cheers!