Skip to content

RedSkittleFox/BinaryLove3

Repository files navigation

MSVC Unit Tests

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.

  • agregate types (e.g. std::pair, struct { uint32_t a; std::vector<uint32_t> b; }):

    Types matching requirements for std::is_aggregate.

  • iterable types (e.g. std::vector):

    Type is required to be compatible with std::begin, std::end and std::inserter. Additionally member type value_type is also required.

    If type's iterator fulfils std::random_access_iterator requirement and value_type matches requirement is_trivial then optimisations will be made.

  • types providing specializations for BinaryLove3::serialize and BinaryLove3::deserialize. Refer to Providing custom serialization methods

Usage

// demo.cpp

#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <string>
#include <list>

#include "BinaryLove3.hpp"

struct foo
{
	uint32_t v0 = 3;
	uint32_t v1 = 2;
	float_t v2 = 2.5f;
	char v3 = 'c';
	struct
	{
		std::vector<int> vec_of_trivial = { 1, 2, 3 };
		std::vector<std::string> vec_of_nontrivial = { "I am a Fox!", "In a big Box!" };
		std::string str = "Foxes can fly!";
		std::list<int> 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<std::byte> serialize(const T& var_)
  • bool deserialize(const std::byte*& cur_, const std::byte* end_, T& obj_)
#include <BinaryLove3>

class foo
{
  friend std::vector<std::byte> serialize(const foo& var_);
  friend bool deserialize(const std::byte*& cur_, const std::byte* end_, foo& obj_);
private:
  uint32_t m_a;
  std::vector<uint32_t> m_b;
public:
  void foo_method();
}

std::vector<std::byte> 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<uint32_t> 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!

About

Simple C++ 20 Serialization Library that works out of the box with aggregate types!

Topics

Resources

License

Stars

Watchers

Forks

Languages