summaryrefslogtreecommitdiffstats
path: root/non-qt-api/glib-var-example/variant.h
blob: f9ded4601851da9516a04eb4b44db09a4847623c (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef VARIANT_H
#define VARIANT_H

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

class SmartPointerBase;

class VariantList;
class VariantMap;

class Variant
{
public:
    Variant() : _type(Invalid) {}
    Variant(bool v) : _type(Bool) { _bVal = v; }
    Variant(int v) : _type(Int) { _nVal = v; }
    Variant(double v)  : _type(Double) { _dVal = v; }
    Variant(long long v)  : _type(LongLong) { _llVal = v; }
    Variant(const char * v);

    Variant(const std::string &v);
    Variant(const VariantList &v);
    Variant(const VariantMap &v);

    Variant(const Variant &v);
    ~Variant();

    Variant& operator = (const Variant& );

    enum Type
    {
        Invalid = -1, Bool, Int, LongLong, Double, String, Map, List
    };

    Type type() const { return _type; }
    bool isValid() const { return Invalid != _type; }

    bool toBool() const { return type() == Bool ? _bVal : false; }
    int toInt() const { return type() == Int ? _nVal : 0; }
    int toLongLong() const { return type() == LongLong ? _llVal : 0; }
    double toDouble() const { return type() == Double ? _dVal : 0; }
    std::string toString() const;
    VariantList toList() const;
    VariantMap toMap() const;

private:

    Type _type;

    union
    {
        bool _bVal;
        int _nVal;
        double _dVal;
        long long _llVal;
        SmartPointerBase *_ptr;
    };
};

class VariantMap : public std::map<std::string, Variant>
{
public:
    void insert(const std::string & key, const Variant & val)
    {
        std::map<std::string, Variant>::insert(std::pair<std::string, Variant>(key,val));
    }

    bool contains(const std::string & val) const
    {
        return find(val) != end();
    }

    Variant value(const std::string & val) const
    {
        std::map<std::string, Variant>::const_iterator it;
        it = find(val);
        return it != end() ? it->second : Variant();
    }
};

class VariantList : public std::vector<Variant>
{
public:
    void append(const Variant & val)
    {
        push_back(val);
    }

    void insert(int nPos, const Variant & val)
    {
        std::vector<Variant>::insert(begin() + nPos, val);
    }

    template<typename T>
    inline VariantList &operator<<(const T & val)
    {
        append(val);

        return *this;
    }
};

#endif // VARIANT_H