summaryrefslogtreecommitdiff
path: root/src/include/serialise.hpp
blob: be5658a8b8a07eb8e2002a7d71923be553db007a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 */
#ifndef _SERIALSE_HPP_INCLUDED_
#define _SERIALSE_HPP_INCLUDED_

#include <vector>
#include <string>
#include <map>

class Serialiser;

#define SERIALISABLE_PROTOTYPES()\
    virtual const char* serialise_tag() const override; \
    virtual void serialise(::Serialiser& s) const override
#define SERIALISE_TYPE(method_prefix, tag_str, body) \
    const char* method_prefix serialise_tag() const { return tag_str; } \
    void method_prefix serialise(::Serialiser& s) const { body }

class Serialisable
{
public:
    virtual const char* serialise_tag() const = 0;
    virtual void serialise(Serialiser& s) const = 0;
};

class Serialiser
{
protected:
    virtual void start_object(const char *tag) = 0;
    virtual void end_object(const char *tag) = 0;
    virtual void start_array(unsigned int size) = 0;
    virtual void end_array() = 0;
public:
    virtual Serialiser& operator<<(bool val) = 0;
    virtual Serialiser& operator<<(unsigned int val) = 0;
    virtual Serialiser& operator<<(const ::std::string& s) = 0;
    Serialiser& operator<<(const Serialisable& subobj);

    template<typename T>
    Serialiser& operator<<(const ::std::vector<T>& v)
    {
        start_array(v.size());
        for(const auto& ent : v)
            *this << ent;
        end_array();
        return *this;
    }
    template<typename T1, typename T2>
    Serialiser& operator<<(const ::std::pair<T1,T2>& v)
    {
        start_array(2);
        *this << v.first;
        *this << v.second;
        end_array();
        return *this;
    }
    template<typename T1, typename T2>
    Serialiser& operator<<(const ::std::map<T1,T2>& v)
    {
        start_object("map");
        for(const auto& ent : v)
            *this << ent;
        end_object("map");
        return *this;
    }
};

#endif