A single header C++ library for serialization
- single header
- easy to use
#include "packme/packme.h"
- aggregate struct
- container with
begin()
,end()
,insert(iter, val)
- tuple-like
- trivially copyable type
- ...
struct A{ int a; double b;};
struct B
{
int a;
std::string b;
std::vector<int> c;
std::map<std::string, int> d;
A e;
};
B b{ 1, "2", std::vector<int>{ 3 },
std::map<std::string, int>{{ "4", 5 }},
A{ 6, 7.0 }};
std::string str = pack(b);
B new_b = unpack<B>(str);
- types with
PACKME_FIELDS(typename, field1, field2, ...)
class C
{
private:
int a;
double* b;
public:
C(std::string b_): a(128), b(new double(3.14)) { }
~C()
{
delete b;
b = nullptr;
}
PACKME_FIELDS(C, a, b);
};
C c("packme");
std::string str = pack(c);
C new_c = unpack<C>(str);
PACKME_FIELDS
will generate two functions in place.
class C
{
public:
using packme_tuple_type = std::tuple<int, double*>;
inline auto packme_make_tuple() const
{ return std::make_tuple(a, b); }
explicit C(const CustomTypeHelper<C> &t)
: a(t.get<0>()), b(t.get<1>()) {}
// CustomTypeHelper is a helper class defined in packme.h
};
- Requires C++ 20