summaryrefslogtreecommitdiff
path: root/src/syncevo/GValueSupport.h
blob: a7b904645ca306cdadb45fe36692e22493734f66 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef INCL_GVALUE_SUPPORT
#define INCL_GVALUE_SUPPORT

#include <glib-object.h>
#include <syncevo/GLibSupport.h>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

/**
 * Base C++ wrapper for GValue. Owns the data stored in it.
 * init() must be called before using it.
 */
class GValueCXX : public GValue
{
 public:
    GValueCXX() { memset(static_cast<GValue *>(this), 0, sizeof(GValue)); }
    GValueCXX(const GValue &other) {
        memset(static_cast<GValue *>(this), 0, sizeof(GValue));
        *this = other;
    }
    ~GValueCXX() { g_value_unset(this); }

    void init(GType gType) { g_value_init(this, gType); }
    GValueCXX &operator = (const GValue &other) {
        if (&other != this) {
            g_value_copy(&other, this);
        }
        return *this;
    }

    /** text representation, for debugging */
    PlainGStr toString() const {
        PlainGStr str(g_strdup_value_contents(this));
        return str;
    }

    /**
     * A GDestroyNotify for dynamically allocated GValueCXX instances,
     * for use in containers like GHashTable.
     */
    static void destroy(gpointer gvaluecxx) {
        delete static_cast<GValueCXX *>(gvaluecxx);
    }
};

template<class C> void dummyTake(GValue *, C);
template<class C> void dummySetStatic(GValue *, const C);

/**
 * Declares a C++ wrapper for a GValue containing a specific
 * GType.
 */
template<
    class nativeType,
    class constNativeType,
    GType gType,
    void (*setGValue)(GValue *, constNativeType),
    constNativeType (*getFromGValue)(const GValue *),
    void (*takeIntoGValue)(GValue *, nativeType) = dummyTake<nativeType>,
    void (*setStaticInGValue)(GValue *, constNativeType) = dummySetStatic<constNativeType>
> class GValueTypedCXX : public GValueCXX
{
 public:
 typedef GValueTypedCXX<nativeType, constNativeType, gType, setGValue, getFromGValue, takeIntoGValue, setStaticInGValue> value_type;

    /**
     * prepare value, without setting it (isSet() will return false)
     */
    GValueTypedCXX() {
        init(gType);
    }
    /**
     * copy other value, referencing (GObject) or duplicating (string) it
     */
    GValueTypedCXX(const value_type &other) {
        init(gType);
        *this = other;
    }

    /**
     * copy value, referencing (GObject) or duplicating (string) it
     */
    GValueTypedCXX(constNativeType value) {
        init(gType);
        set(value);
    }

    /**
     * copy (addRef = true) or take ownership of value during construction
     */
    GValueTypedCXX(nativeType value, bool addRef) {
        init(gType);
        if (addRef) {
            set(value);
        } else {
            take(value);
        }
    }

    /** copy other value */
    GValueCXX &operator = (const value_type &other) {
        if (&other != this) {
            g_value_copy(&other, this);
        }
        return *this;
    }

    /** copy other value */
    GValueCXX &operator = (constNativeType other) {
        set(other);
        return *this;
    }

    /**
     * set value, copying (string) or referencing (GObject) it if necessary
     */
    void set(constNativeType other) {
        setGValue(this, other);
    }

    /**
     * store pointer to static instance which does not have to be copied or freed
     * (like a const char * string)
     */
    void setStatic(constNativeType other) {
        setStaticInGValue(this, other);
    }

    /** transfer ownership of complex object (string, GObject) to GValue */
    void take(nativeType other) {
        takeIntoGValue(this, other);
    }

    /** access content without transfering ownership */
    constNativeType get() const {
        return getFromGValue(this);
    }

};

/**
 * Declares a C++ wrapper for a GValue containing a dynamically
 * created GType. Uses g_value_set/get/take_boxed() with the necessary
 * type casting.
 *
 * Example:
 * typedef GValueDynTypedCXX<GDateTime *, g_date_time_get_type> GValueDateTimeCXX;
 */
template<
    class nativeType,
    GType (*gTypeFactory)()
> class GValueDynTypedCXX : public GValueCXX
{
 public:
 typedef GValueDynTypedCXX<nativeType, gTypeFactory> value_type;

    /**
     * prepare value, without setting it (isSet() will return false)
     */
    GValueDynTypedCXX() {
        init(gTypeFactory());
    }
    /**
     * copy other value
     */
    GValueDynTypedCXX(const value_type &other) {
        init(gTypeFactory());
        *this = other;
    }

    /**
     * copy value, referencing (GObject) or duplicating (string) it
     */
    GValueDynTypedCXX(const nativeType value) {
        init(gTypeFactory());
        set(value);
    }

    /**
     * copy (addRef = true) or take ownership of value during construction
     */
    GValueDynTypedCXX(nativeType value, bool addRef) {
        init(gTypeFactory());
        if (addRef) {
            set(value);
        } else {
            take(value);
        }
    }

    /** copy other value */
    value_type &operator = (const value_type &other) {
        if (&other != this) {
            g_value_copy(&other, this);
        }
        return *this;
    }

    /** copy other value, referencing (GObject) or duplicating (string) it */
    value_type &operator = (const nativeType other) {
        set(other);
        return *this;
    }

    /**
     * set value, copying (string) or referencing (GObject) it if necessary
     */
    void set(const nativeType other) {
        g_value_set_boxed(this, other);
    }

    /**
     * store pointer to static instance which does not have to be copied or freed
     * (like a const char * string)
     */
    void setStatic(const nativeType other) {
        g_value_set_static_boxed(this, other);
    }

    /** transfer ownership of complex object (string, GObject) to GValue */
    void take(nativeType other) {
        g_value_take_boxed(this, other);
    }

    /** access content without transfering ownership */
    const nativeType get() const {
        return static_cast<const nativeType *>(g_value_get_boxed(this));
    }
};

typedef GValueTypedCXX<gboolean, gboolean, G_TYPE_BOOLEAN, g_value_set_boolean, g_value_get_boolean> GValueBooleanCXX;
typedef GValueTypedCXX<gint8, gint8, G_TYPE_CHAR, g_value_set_schar, g_value_get_schar> GValueCharCXX;
typedef GValueTypedCXX<guchar, guchar, G_TYPE_UCHAR, g_value_set_uchar, g_value_get_uchar> GValueUCharCXX;
typedef GValueTypedCXX<gint, gint, G_TYPE_INT, g_value_set_int, g_value_get_int> GValueIntCXX;
typedef GValueTypedCXX<guint, guint, G_TYPE_UINT, g_value_set_uint, g_value_get_uint> GValueUIntCXX;
typedef GValueTypedCXX<glong, glong, G_TYPE_LONG, g_value_set_long, g_value_get_long> GValueLongCXX;
typedef GValueTypedCXX<gulong, gulong, G_TYPE_ULONG, g_value_set_ulong, g_value_get_ulong> GValueULongCXX;
typedef GValueTypedCXX<gint64, gint64, G_TYPE_INT64, g_value_set_int64, g_value_get_int64> GValueInt64CXX;
typedef GValueTypedCXX<guint64, guint64, G_TYPE_UINT64, g_value_set_uint64, g_value_get_uint64> GValueUInt64CXX;
typedef GValueTypedCXX<gfloat, gfloat, G_TYPE_FLOAT, g_value_set_float, g_value_get_float> GValueFloatCXX;
typedef GValueTypedCXX<gdouble, gdouble, G_TYPE_DOUBLE, g_value_set_double, g_value_get_double> GValueDoubleCXX;
typedef GValueTypedCXX<gint, gint, G_TYPE_ENUM, g_value_set_enum, g_value_get_enum> GValueEnumCXX;
typedef GValueTypedCXX<gboolean, gboolean, G_TYPE_BOOLEAN, g_value_set_boolean, g_value_get_boolean> GValueBooleanCXX;
typedef GValueTypedCXX<gchar *, const gchar *, G_TYPE_STRING, g_value_set_string, g_value_get_string, g_value_take_string, g_value_set_static_string> GValueStringCXX;
typedef GValueTypedCXX<gpointer, gpointer, G_TYPE_OBJECT, g_value_set_object, g_value_get_object, g_value_take_object> GValueObjectCXX;



SE_END_CXX

#endif // INCL_GVALUE_SUPPORT