summaryrefslogtreecommitdiff
path: root/src/libnm-glib-aux/nm-value-type.h
blob: 771021da4c320ef029d063007a972b0320dff4b3 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019 Red Hat, Inc.
 */

#ifndef __NM_VALUE_TYPE_H__
#define __NM_VALUE_TYPE_H__

typedef enum _nm_packed {
    NM_VALUE_TYPE_NONE   = 0,
    NM_VALUE_TYPE_UNSPEC = 1,

    NM_VALUE_TYPE_BOOL,
    NM_VALUE_TYPE_INT32,
    NM_VALUE_TYPE_INT,
    NM_VALUE_TYPE_INT64,
    NM_VALUE_TYPE_UINT32,
    NM_VALUE_TYPE_UINT,
    NM_VALUE_TYPE_UINT64,

    /* Flags are for G_TYPE_FLAGS. That is, internally they are tracked
     * as a guint, they have a g_param_spec_flags() property and they are
     * serialized on D-Bus as "u". */
    NM_VALUE_TYPE_FLAGS,

    /* G_TYPE_ENUM */
    NM_VALUE_TYPE_ENUM,

    NM_VALUE_TYPE_STRING,
    NM_VALUE_TYPE_BYTES,
    NM_VALUE_TYPE_STRV,
} NMValueType;

/*****************************************************************************/

#ifdef NM_VALUE_TYPE_DEFINE_FUNCTIONS

typedef union {
    bool        v_bool;
    gint32      v_int32;
    gint64      v_int64;
    guint64     v_uint64;
    int         v_int;
    const char *v_string;

    /* for convenience, also let the union contain other pointer types. These are
     * for NM_VALUE_TYPE_UNSPEC. */
    gconstpointer   *v_ptr;
    const GPtrArray *v_ptrarray;

} NMValueTypUnion;

/* Set the NMValueTypUnion. You can also assign the member directly.
 * The only purpose of this is that it also returns a pointer to the
 * union. So, you can do
 *
 *   ptr = NM_VALUE_TYP_UNION_SET (&value_typ_union_storage, v_bool, TRUE);
 */
#define NM_VALUE_TYP_UNION_SET(_arg, _type, _val) \
    ({                                            \
        NMValueTypUnion *const _arg2 = (_arg);    \
                                                  \
        *_arg2 = (NMValueTypUnion){               \
            ._type = (_val),                      \
        };                                        \
        _arg2;                                    \
    })

typedef struct {
    bool            has;
    NMValueTypUnion val;
} NMValueTypUnioMaybe;

#define NM_VALUE_TYP_UNIO_MAYBE_SET(_arg, _type, _val) \
    ({                                                 \
        NMValueTypUnioMaybe *const _arg2 = (_arg);     \
                                                       \
        *_arg2 = (NMValueTypUnioMaybe){                \
            .has       = TRUE,                         \
            .val._type = (_val),                       \
        };                                             \
        _arg2;                                         \
    })

/*****************************************************************************/

static inline int
nm_value_type_cmp(NMValueType value_type, gconstpointer p_a, gconstpointer p_b)
{
    switch (value_type) {
    case NM_VALUE_TYPE_BOOL:
        NM_CMP_DIRECT(*((const bool *) p_a), *((const bool *) p_b));
        return 0;
    case NM_VALUE_TYPE_INT32:
        NM_CMP_DIRECT(*((const gint32 *) p_a), *((const gint32 *) p_b));
        return 0;
    case NM_VALUE_TYPE_INT:
    case NM_VALUE_TYPE_ENUM:
        NM_CMP_DIRECT(*((const int *) p_a), *((const int *) p_b));
        return 0;
    case NM_VALUE_TYPE_INT64:
        NM_CMP_DIRECT(*((const gint64 *) p_a), *((const gint64 *) p_b));
        return 0;
    case NM_VALUE_TYPE_UINT32:
        NM_CMP_DIRECT(*((const guint32 *) p_a), *((const guint32 *) p_b));
        return 0;
    case NM_VALUE_TYPE_UINT:
    case NM_VALUE_TYPE_FLAGS:
        NM_CMP_DIRECT(*((const guint *) p_a), *((const guint *) p_b));
        return 0;
    case NM_VALUE_TYPE_UINT64:
        NM_CMP_DIRECT(*((const guint64 *) p_a), *((const guint64 *) p_b));
        return 0;
    case NM_VALUE_TYPE_STRING:
        return nm_strcmp0(*((const char *const *) p_a), *((const char *const *) p_b));

    case NM_VALUE_TYPE_BYTES:
    case NM_VALUE_TYPE_STRV:
        /* These types have implementation define memory representations. */
        break;

    case NM_VALUE_TYPE_NONE:
    case NM_VALUE_TYPE_UNSPEC:
        break;
    }
    return nm_assert_unreachable_val(0);
}

static inline gboolean
nm_value_type_equal(NMValueType value_type, gconstpointer p_a, gconstpointer p_b)
{
    return nm_value_type_cmp(value_type, p_a, p_b) == 0;
}

static inline void
nm_value_type_copy(NMValueType value_type, gpointer dst, gconstpointer src)
{
    switch (value_type) {
    case NM_VALUE_TYPE_BOOL:
        (*((bool *) dst) = *((const bool *) src));
        return;
    case NM_VALUE_TYPE_INT32:
        (*((gint32 *) dst) = *((const gint32 *) src));
        return;
    case NM_VALUE_TYPE_INT:
    case NM_VALUE_TYPE_ENUM:
        (*((int *) dst) = *((const int *) src));
        return;
    case NM_VALUE_TYPE_INT64:
        (*((gint64 *) dst) = *((const gint64 *) src));
        return;
    case NM_VALUE_TYPE_UINT32:
        (*((guint32 *) dst) = *((const guint32 *) src));
        return;
    case NM_VALUE_TYPE_UINT:
    case NM_VALUE_TYPE_FLAGS:
        (*((guint *) dst) = *((const guint *) src));
        return;
    case NM_VALUE_TYPE_UINT64:
        (*((guint64 *) dst) = *((const guint64 *) src));
        return;
    case NM_VALUE_TYPE_STRING:
        /* self assignment safe! */
        if (*((char **) dst) != *((const char *const *) src)) {
            _nm_unused char *old = *((char **) dst);

            *((char **) dst) = g_strdup(*((const char *const *) src));
        }
        return;

    case NM_VALUE_TYPE_BYTES:
    case NM_VALUE_TYPE_STRV:
        /* These types have implementation define memory representations. */
        break;

    case NM_VALUE_TYPE_NONE:
    case NM_VALUE_TYPE_UNSPEC:
        break;
    }
    nm_assert_not_reached();
}

static inline void
nm_value_type_get_from_variant(NMValueType value_type,
                               gpointer    dst,
                               GVariant   *variant,
                               gboolean    clone)
{
    switch (value_type) {
    case NM_VALUE_TYPE_BOOL:
        *((bool *) dst) = g_variant_get_boolean(variant);
        return;
    case NM_VALUE_TYPE_INT32:
        *((gint32 *) dst) = g_variant_get_int32(variant);
        return;
    case NM_VALUE_TYPE_INT64:
        *((gint64 *) dst) = g_variant_get_int64(variant);
        return;
    case NM_VALUE_TYPE_UINT32:
        *((guint32 *) dst) = g_variant_get_uint32(variant);
        return;
    case NM_VALUE_TYPE_UINT64:
        *((guint64 *) dst) = g_variant_get_uint64(variant);
        return;
    case NM_VALUE_TYPE_STRING:
        if (clone) {
            _nm_unused gs_free char *old = *((char **) dst);

            *((char **) dst) = g_variant_dup_string(variant, NULL);
        } else {
            /* we don't clone the string, nor free the previous value. */
            *((const char **) dst) = g_variant_get_string(variant, NULL);
        }
        return;

    case NM_VALUE_TYPE_BYTES:
    case NM_VALUE_TYPE_STRV:
        /* These types have implementation define memory representations. */
        break;

    case NM_VALUE_TYPE_INT:
    case NM_VALUE_TYPE_UINT:
    case NM_VALUE_TYPE_ENUM:
    case NM_VALUE_TYPE_FLAGS:
        /* These types don't have a defined variant type, because it's not
         * clear how many bits we would need or how to handle the type. */
        break;

    case NM_VALUE_TYPE_NONE:
    case NM_VALUE_TYPE_UNSPEC:
        break;
    }
    nm_assert_not_reached();
}

static inline GVariant *
nm_value_type_to_variant(NMValueType value_type, gconstpointer src)
{
    const char *v_string;

    switch (value_type) {
    case NM_VALUE_TYPE_BOOL:
        return g_variant_new_boolean(*((const bool *) src));
    case NM_VALUE_TYPE_INT32:
        return g_variant_new_int32(*((const gint32 *) src));
    case NM_VALUE_TYPE_INT64:
        return g_variant_new_int64(*((const gint64 *) src));
    case NM_VALUE_TYPE_UINT32:
        return g_variant_new_uint32(*((const guint32 *) src));
    case NM_VALUE_TYPE_UINT64:
        return g_variant_new_uint64(*((const guint64 *) src));
    case NM_VALUE_TYPE_STRING:
        v_string = *((const char *const *) src);
        return v_string ? g_variant_new_string(v_string) : NULL;

    case NM_VALUE_TYPE_BYTES:
    case NM_VALUE_TYPE_STRV:
        /* These types have implementation define memory representations. */
        break;

    case NM_VALUE_TYPE_INT:
    case NM_VALUE_TYPE_UINT:
    case NM_VALUE_TYPE_ENUM:
    case NM_VALUE_TYPE_FLAGS:
        /* These types don't have a defined variant type, because it's not
         * clear how many bits we would need or how to handle the type. */
        break;

    case NM_VALUE_TYPE_NONE:
    case NM_VALUE_TYPE_UNSPEC:
        break;
    }
    return nm_assert_unreachable_val(NULL);
}

static inline const GVariantType *
nm_value_type_get_variant_type(NMValueType value_type)
{
    switch (value_type) {
    case NM_VALUE_TYPE_BOOL:
        return G_VARIANT_TYPE_BOOLEAN;
    case NM_VALUE_TYPE_INT32:
        return G_VARIANT_TYPE_INT32;
    case NM_VALUE_TYPE_INT64:
        return G_VARIANT_TYPE_INT64;
    case NM_VALUE_TYPE_UINT32:
        return G_VARIANT_TYPE_UINT32;
    case NM_VALUE_TYPE_UINT64:
        return G_VARIANT_TYPE_UINT64;
    case NM_VALUE_TYPE_STRING:
        return G_VARIANT_TYPE_STRING;
    case NM_VALUE_TYPE_BYTES:
        return G_VARIANT_TYPE_BYTESTRING;
    case NM_VALUE_TYPE_STRV:
        return G_VARIANT_TYPE_STRING_ARRAY;

    case NM_VALUE_TYPE_INT:
    case NM_VALUE_TYPE_UINT:
    case NM_VALUE_TYPE_ENUM:
    case NM_VALUE_TYPE_FLAGS:
        /* These types don't have a defined variant type, because it's not
         * clear how many bits we would need or how to handle the type. */

        /* fall-through */
    case NM_VALUE_TYPE_NONE:
    case NM_VALUE_TYPE_UNSPEC:
        break;
    }
    nm_assert_not_reached();
    return NULL;
}

/*****************************************************************************/

#endif /* NM_VALUE_TYPE_DEFINE_FUNCTIONS */

#endif /* __NM_VALUE_TYPE_H__ */