in ,

apankrat / cpp-serializer, Hacker News

apankrat / cpp-serializer, Hacker News


                    

        

Small, light and easy to understand data serialization library for C .

This is an extract from the live source ofBvckup 2. It’s not meant to be an universal serialization library, but rather a real-life working example of one of many ways to do serialization. Doing itsimplyat that.

Quick intro

Given two processes that use the same data model and exchange parts of it with each other, this library helps packing and unpacking in-memory data structures to / from binary blobs withas little fuss as possible.

No separate schemas, no precompilation, no verbose description of protocols, etc.

Just define your data structures …

struct foo {         bool aaa;         mapbbb; };

… specify which fields to serialize …

OBJECT_SPEC (foo)         __f (aaa),         __f (bbb) END_OF_SPEC

… and you are done!

// store: data ->blob  buffer buf; foo something;  store (buf, something);  // parse: blob ->data  parser par; foo inbound;  par.init (buf); parse (par, inbound);  assert (parse.eof ());

How it works

The library revolves around two functions –store ()andparse ().

Storeappends binary representation of its argument to an existing data blob.

Parseextracts an instance of specified type from the blob.

The library knows how to serialize basic types likebool,uint8_t, etc. That is, it has overloaded versions of

void store (buffer & buf, const bool & v ); void store (buffer & buf, const uint8_t & v); ...  bool parse (parser & par, bool & v); bool parse (parser & par, uint8_t & v); ...

It knows how to serializestring,wstringor any other specialization ofstd :: basic_string.

It knows how to serializevector,setandmapand it can be easily extended to support other container types likelistandDeque.

However, in order to serialize a container, it obviously needs to know how to store / parse the elements contained within. If they are of a known type, e.g.bool, then we are good. But if their type is a class or a struct, then we need to teach the library how to handle them.

Serializing structs

If we havestruct foo, then we can obviously implementstore (..., const foo &)andparse (..., foo &)and that will do it. That’s not terribly elegant though. *****

It is also too verbose. Both functions will end up lookingalmostthe same, except one will be callingstorefor every field and another –parse.

It’s C , the most overpowered language in existence. We can do better.

Enterserialize_obj.h

Through a combination ofpointers to members,variadic templatesand a modest sprinkle of macros it coerces the compiler into auto-generatingstoreand (parse) ******************** (overloads from a plain list of struct members.)

So we can just go:

OBJECT_SPEC (foo) - the type we are describing         __f (aaa), - field we want to be stored / parsed         __f (bbb) - another field END_OF_SPEC - close all open brackets and what not

and we getstore (..., const foo &)auto-generated as soon as we call it.

Magic.

Caveats

A quick list of cases that aren’t supported by the library:

  1. Classes with complicated inheritance, members that are references, pointers, etc., i.e. cases that require some sort of pre- or post-processing when instantiating an object.

  2. Classes that use getters and setters. Not hard to accommodate with a bit of extra code.

  3. Serializing into a tagged format, like JSON or XML. Can be supported by capturing field names inmember_ptr

  4. Storing integers in a network byte order. This too is very easy to support, but a pointless thing to do forIPCwithin the same host, which is what we have.

Footnotes

For a similar, but a bit more generic library seehttps://github.com/eliasdaler/MetaStuff

  

Brave Browser
Read More
Payeer

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

The Pot Stock Crash Will Dent Bitcoin Price, Claims Peter Schiff, Crypto Coins News

The Pot Stock Crash Will Dent Bitcoin Price, Claims Peter Schiff, Crypto Coins News

Let's Encrypt certificate issuance down, Hacker News