summaryrefslogtreecommitdiffstats
path: root/non-qt-api/glib-var-example/variant.cpp
blob: 238d11c68e6b145adbb94c0456d81fdcbaaf9c9b (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "variant.h"

#include <string.h>

/*!
    \class RefCounter
    \brief The RefCounter class implements a simple reference counter.
*/
class RefCounter
{
private:
    int count; // Reference count

    public:
    void AddRef()
    {
        // Increment the reference count
        count++;
    }

    int Release()
    {
        // Decrement the reference count and return the reference count.
        return --count;
    }
};

/*!
    \class SmartPointerBase
    \brief The SmartPointerBase class is a base class for SmartPointer.
*/
class SmartPointerBase
{
public:
    virtual ~SmartPointerBase() {}
    virtual SmartPointerBase* create() = 0;

protected:
    RefCounter* reference; // Reference count
};

/*!
    \class SmartPointer
    \brief The SmartPointer class implements a simple smart pointer template.

    Note: The SmartPointer is not thread safe.
*/
template < typename T > class SmartPointer : public SmartPointerBase
{
private:
    T*    pData;       // pointer

public:
    SmartPointer() : pData(0)
    {
        // Create a new reference and increment the reference count
        reference = new RefCounter();
        reference->AddRef();
    }

    SmartPointer(T* pValue) : pData(pValue)
    {
        // Create a new reference and increment the reference count
        reference = new RefCounter();
        reference->AddRef();
    }

    // Copy constructor
    // Copy the data and reference pointer and increment the reference count
    SmartPointer(const SmartPointer<T>& sp) : pData(sp.pData)
    {
        reference = sp.reference;
        reference->AddRef();
    }

    // Destructor
    // Decrement the reference count if reference become zero delete the data
    ~SmartPointer()
    {
        if (reference->Release() == 0)
        {
            delete pData;
            delete reference;
        }
    }

    T& operator* ()
    {
        return *pData;
    }

    T* operator-> ()
    {
        return pData;
    }

    SmartPointer<T>& operator = (const SmartPointer<T>& sp)
    {
        // Assignment operator
        if (this != &sp) // Avoid self assignment
        {
            // Decrement the old reference count if reference become zero delete the old data
            if (reference->Release() == 0)
            {
                delete pData;
                delete reference;
            }

            // Copy the data and reference pointer and increment the reference count
            pData = sp.pData;
            reference = sp.reference;
            reference->AddRef();
        }
        return *this;
    }

    SmartPointerBase* create()
    {
        return new SmartPointer<T>(*this);
    }
};

/*!
    \class Variant
    \brief The Variant class is a variant type implementation
*/
Variant::Variant(const char * v)
    : _type(String)
{
    _ptr = new SmartPointer<std::string>(new std::string(v));
}

Variant::Variant(const std::string &v)
    : _type(String)
{
    _ptr = new SmartPointer<std::string>(new std::string(v));
}

Variant::Variant(const VariantList &v)
    : _type(List)
{
    _ptr = new SmartPointer<VariantList>(new VariantList(v));
}

Variant::Variant(const VariantMap &v)
    : _type(Map)
{
    _ptr = new SmartPointer<VariantMap>(new VariantMap(v));
}

// Copy constructor
Variant::Variant(const Variant &v)
{
    memcpy(this, &v, sizeof(Variant));
    if (type() >= String)
    {
        _ptr = v._ptr->create();
    }
}

// Assignment operator
Variant& Variant::operator = (const Variant &v)
{
    if (this != &v) // Avoid self assignment
    {
        if (type() >= String)
        {
            delete _ptr;
        }

        memcpy(this, &v, sizeof(Variant));
        if (type() >= String)
        {
            _ptr = v._ptr->create();
        }
    }
    return *this;
}

Variant::~Variant()
{
    if (type() >= String)
    {
        delete _ptr;
    }
}

std::string Variant::toString() const
{
    return type() == String ? **static_cast< SmartPointer<std::string> *>(_ptr) : "";
}

/*!
    \class VariantList
    \brief The VariantList class is a variant list implementation
*/
VariantList Variant::toList() const
{
    return type() == List ? **static_cast< SmartPointer<VariantList> *>(_ptr) : VariantList();
}

/*!
    \class VariantMap
    \brief The VariantMap class is a variant map implementation
*/
VariantMap Variant::toMap() const
{
    return type() == Map ? **static_cast< SmartPointer<VariantMap> *>(_ptr) : VariantMap();
}