Dan Parnham edited this page May 11, 2018 · 9 revisions

Any struct or class can be a serialisable entity by simply implementing the ent::mapping ent_describe() function. This is simplified by using the emap() macro as shown below.

Note: If building an application with mingw there can be a type ambiguity issue when using long in an entity, simply using an int64_t instead resolves this problem.

Describing the entity

struct Simple
{
    string name;
    bool flag;
    int integer;

    emap(eref(name), eref(flag), eref(integer))
};

The eref helper macro expands to create a mapping entry where the name is that of the parameter being referenced. If you need to deserialise objects that originated elsewhere then you can specify a custom name where required: eref("int", integer).

Since the automatic naming is based on a macro avoid the use of this-> otherwise it will become part of the name.

Collections

Entities can contain collections but they have to be in the form std::vector<T> or std::map<string, T> where T is a supported type.

struct Collection
{
    std::vector<string> strings;
    std::vector<double> doubles;
    std::vector<uint8_t> binary;
    std::map<string, string> dictionary;

    emap(eref(strings), eref(doubles), eref(binary), eref(dictionary))
};

The above example should be fairly self-explanatory except there is a special case: std::vector<uint8_t> will be serialised to or deserialised from base64 in situations where the parser format does not support binary (e.g., JSON).

Complex objects

Complex object trees will be mapped automatically as long as each class or structure in that tree implements a public description function via emap().

struct Complex
{
    std::vector<Simple> entities;
    Collection collection;
    Simple simple;

    emap(eref(entities), eref(collection), eref(simple))
};

Inheritance

An entity can derive from another entity but it must provide a description that includes all properties or else use the helper emerge() macro to merge with the description of the base class as shown in the following example.

struct Base
{
    string name;

    emap(eref(name))
};

struct Derived : Base
{
    bool flag;
    int integer;

    emerge(Base, eref(flag), eref(integer))
};
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.