1/*
2 * Copyright © 2007, 2008 Ryan Lortie
3 * Copyright © 2009, 2010 Codethink Limited
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Ryan Lortie <desrt@desrt.ca>
19 */
20
21#ifndef __G_VARIANT_H__
22#define __G_VARIANT_H__
23
24#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
25#error "Only <glib.h> can be included directly."
26#endif
27
28#include <glib/gvarianttype.h>
29#include <glib/gstring.h>
30#include <glib/gbytes.h>
31
32G_BEGIN_DECLS
33
34typedef struct _GVariant GVariant;
35
36typedef enum
37{
38 G_VARIANT_CLASS_BOOLEAN = 'b',
39 G_VARIANT_CLASS_BYTE = 'y',
40 G_VARIANT_CLASS_INT16 = 'n',
41 G_VARIANT_CLASS_UINT16 = 'q',
42 G_VARIANT_CLASS_INT32 = 'i',
43 G_VARIANT_CLASS_UINT32 = 'u',
44 G_VARIANT_CLASS_INT64 = 'x',
45 G_VARIANT_CLASS_UINT64 = 't',
46 G_VARIANT_CLASS_HANDLE = 'h',
47 G_VARIANT_CLASS_DOUBLE = 'd',
48 G_VARIANT_CLASS_STRING = 's',
49 G_VARIANT_CLASS_OBJECT_PATH = 'o',
50 G_VARIANT_CLASS_SIGNATURE = 'g',
51 G_VARIANT_CLASS_VARIANT = 'v',
52 G_VARIANT_CLASS_MAYBE = 'm',
53 G_VARIANT_CLASS_ARRAY = 'a',
54 G_VARIANT_CLASS_TUPLE = '(',
55 G_VARIANT_CLASS_DICT_ENTRY = '{'
56} GVariantClass;
57
58GLIB_AVAILABLE_IN_ALL
59void g_variant_unref (GVariant *value);
60GLIB_AVAILABLE_IN_ALL
61GVariant * g_variant_ref (GVariant *value);
62GLIB_AVAILABLE_IN_ALL
63GVariant * g_variant_ref_sink (GVariant *value);
64GLIB_AVAILABLE_IN_ALL
65gboolean g_variant_is_floating (GVariant *value);
66GLIB_AVAILABLE_IN_ALL
67GVariant * g_variant_take_ref (GVariant *value);
68
69GLIB_AVAILABLE_IN_ALL
70const GVariantType * g_variant_get_type (GVariant *value);
71GLIB_AVAILABLE_IN_ALL
72const gchar * g_variant_get_type_string (GVariant *value);
73GLIB_AVAILABLE_IN_ALL
74gboolean g_variant_is_of_type (GVariant *value,
75 const GVariantType *type);
76GLIB_AVAILABLE_IN_ALL
77gboolean g_variant_is_container (GVariant *value);
78GLIB_AVAILABLE_IN_ALL
79GVariantClass g_variant_classify (GVariant *value);
80GLIB_AVAILABLE_IN_ALL
81GVariant * g_variant_new_boolean (gboolean value);
82GLIB_AVAILABLE_IN_ALL
83GVariant * g_variant_new_byte (guint8 value);
84GLIB_AVAILABLE_IN_ALL
85GVariant * g_variant_new_int16 (gint16 value);
86GLIB_AVAILABLE_IN_ALL
87GVariant * g_variant_new_uint16 (guint16 value);
88GLIB_AVAILABLE_IN_ALL
89GVariant * g_variant_new_int32 (gint32 value);
90GLIB_AVAILABLE_IN_ALL
91GVariant * g_variant_new_uint32 (guint32 value);
92GLIB_AVAILABLE_IN_ALL
93GVariant * g_variant_new_int64 (gint64 value);
94GLIB_AVAILABLE_IN_ALL
95GVariant * g_variant_new_uint64 (guint64 value);
96GLIB_AVAILABLE_IN_ALL
97GVariant * g_variant_new_handle (gint32 value);
98GLIB_AVAILABLE_IN_ALL
99GVariant * g_variant_new_double (gdouble value);
100GLIB_AVAILABLE_IN_ALL
101GVariant * g_variant_new_string (const gchar *string);
102GLIB_AVAILABLE_IN_2_38
103GVariant * g_variant_new_take_string (gchar *string);
104GLIB_AVAILABLE_IN_2_38
105GVariant * g_variant_new_printf (const gchar *format_string,
106 ...) G_GNUC_PRINTF (1, 2);
107GLIB_AVAILABLE_IN_ALL
108GVariant * g_variant_new_object_path (const gchar *object_path);
109GLIB_AVAILABLE_IN_ALL
110gboolean g_variant_is_object_path (const gchar *string);
111GLIB_AVAILABLE_IN_ALL
112GVariant * g_variant_new_signature (const gchar *signature);
113GLIB_AVAILABLE_IN_ALL
114gboolean g_variant_is_signature (const gchar *string);
115GLIB_AVAILABLE_IN_ALL
116GVariant * g_variant_new_variant (GVariant *value);
117GLIB_AVAILABLE_IN_ALL
118GVariant * g_variant_new_strv (const gchar * const *strv,
119 gssize length);
120GLIB_AVAILABLE_IN_2_30
121GVariant * g_variant_new_objv (const gchar * const *strv,
122 gssize length);
123GLIB_AVAILABLE_IN_ALL
124GVariant * g_variant_new_bytestring (const gchar *string);
125GLIB_AVAILABLE_IN_ALL
126GVariant * g_variant_new_bytestring_array (const gchar * const *strv,
127 gssize length);
128GLIB_AVAILABLE_IN_ALL
129GVariant * g_variant_new_fixed_array (const GVariantType *element_type,
130 gconstpointer elements,
131 gsize n_elements,
132 gsize element_size);
133GLIB_AVAILABLE_IN_ALL
134gboolean g_variant_get_boolean (GVariant *value);
135GLIB_AVAILABLE_IN_ALL
136guint8 g_variant_get_byte (GVariant *value);
137GLIB_AVAILABLE_IN_ALL
138gint16 g_variant_get_int16 (GVariant *value);
139GLIB_AVAILABLE_IN_ALL
140guint16 g_variant_get_uint16 (GVariant *value);
141GLIB_AVAILABLE_IN_ALL
142gint32 g_variant_get_int32 (GVariant *value);
143GLIB_AVAILABLE_IN_ALL
144guint32 g_variant_get_uint32 (GVariant *value);
145GLIB_AVAILABLE_IN_ALL
146gint64 g_variant_get_int64 (GVariant *value);
147GLIB_AVAILABLE_IN_ALL
148guint64 g_variant_get_uint64 (GVariant *value);
149GLIB_AVAILABLE_IN_ALL
150gint32 g_variant_get_handle (GVariant *value);
151GLIB_AVAILABLE_IN_ALL
152gdouble g_variant_get_double (GVariant *value);
153GLIB_AVAILABLE_IN_ALL
154GVariant * g_variant_get_variant (GVariant *value);
155GLIB_AVAILABLE_IN_ALL
156const gchar * g_variant_get_string (GVariant *value,
157 gsize *length);
158GLIB_AVAILABLE_IN_ALL
159gchar * g_variant_dup_string (GVariant *value,
160 gsize *length);
161GLIB_AVAILABLE_IN_ALL
162const gchar ** g_variant_get_strv (GVariant *value,
163 gsize *length);
164GLIB_AVAILABLE_IN_ALL
165gchar ** g_variant_dup_strv (GVariant *value,
166 gsize *length);
167GLIB_AVAILABLE_IN_2_30
168const gchar ** g_variant_get_objv (GVariant *value,
169 gsize *length);
170GLIB_AVAILABLE_IN_ALL
171gchar ** g_variant_dup_objv (GVariant *value,
172 gsize *length);
173GLIB_AVAILABLE_IN_ALL
174const gchar * g_variant_get_bytestring (GVariant *value);
175GLIB_AVAILABLE_IN_ALL
176gchar * g_variant_dup_bytestring (GVariant *value,
177 gsize *length);
178GLIB_AVAILABLE_IN_ALL
179const gchar ** g_variant_get_bytestring_array (GVariant *value,
180 gsize *length);
181GLIB_AVAILABLE_IN_ALL
182gchar ** g_variant_dup_bytestring_array (GVariant *value,
183 gsize *length);
184
185GLIB_AVAILABLE_IN_ALL
186GVariant * g_variant_new_maybe (const GVariantType *child_type,
187 GVariant *child);
188GLIB_AVAILABLE_IN_ALL
189GVariant * g_variant_new_array (const GVariantType *child_type,
190 GVariant * const *children,
191 gsize n_children);
192GLIB_AVAILABLE_IN_ALL
193GVariant * g_variant_new_tuple (GVariant * const *children,
194 gsize n_children);
195GLIB_AVAILABLE_IN_ALL
196GVariant * g_variant_new_dict_entry (GVariant *key,
197 GVariant *value);
198
199GLIB_AVAILABLE_IN_ALL
200GVariant * g_variant_get_maybe (GVariant *value);
201GLIB_AVAILABLE_IN_ALL
202gsize g_variant_n_children (GVariant *value);
203GLIB_AVAILABLE_IN_ALL
204void g_variant_get_child (GVariant *value,
205 gsize index_,
206 const gchar *format_string,
207 ...);
208GLIB_AVAILABLE_IN_ALL
209GVariant * g_variant_get_child_value (GVariant *value,
210 gsize index_);
211GLIB_AVAILABLE_IN_ALL
212gboolean g_variant_lookup (GVariant *dictionary,
213 const gchar *key,
214 const gchar *format_string,
215 ...);
216GLIB_AVAILABLE_IN_ALL
217GVariant * g_variant_lookup_value (GVariant *dictionary,
218 const gchar *key,
219 const GVariantType *expected_type);
220GLIB_AVAILABLE_IN_ALL
221gconstpointer g_variant_get_fixed_array (GVariant *value,
222 gsize *n_elements,
223 gsize element_size);
224
225GLIB_AVAILABLE_IN_ALL
226gsize g_variant_get_size (GVariant *value);
227GLIB_AVAILABLE_IN_ALL
228gconstpointer g_variant_get_data (GVariant *value);
229GLIB_AVAILABLE_IN_2_36
230GBytes * g_variant_get_data_as_bytes (GVariant *value);
231GLIB_AVAILABLE_IN_ALL
232void g_variant_store (GVariant *value,
233 gpointer data);
234
235GLIB_AVAILABLE_IN_ALL
236gchar * g_variant_print (GVariant *value,
237 gboolean type_annotate);
238GLIB_AVAILABLE_IN_ALL
239GString * g_variant_print_string (GVariant *value,
240 GString *string,
241 gboolean type_annotate);
242
243GLIB_AVAILABLE_IN_ALL
244guint g_variant_hash (gconstpointer value);
245GLIB_AVAILABLE_IN_ALL
246gboolean g_variant_equal (gconstpointer one,
247 gconstpointer two);
248
249GLIB_AVAILABLE_IN_ALL
250GVariant * g_variant_get_normal_form (GVariant *value);
251GLIB_AVAILABLE_IN_ALL
252gboolean g_variant_is_normal_form (GVariant *value);
253GLIB_AVAILABLE_IN_ALL
254GVariant * g_variant_byteswap (GVariant *value);
255
256GLIB_AVAILABLE_IN_2_36
257GVariant * g_variant_new_from_bytes (const GVariantType *type,
258 GBytes *bytes,
259 gboolean trusted);
260GLIB_AVAILABLE_IN_ALL
261GVariant * g_variant_new_from_data (const GVariantType *type,
262 gconstpointer data,
263 gsize size,
264 gboolean trusted,
265 GDestroyNotify notify,
266 gpointer user_data);
267
268typedef struct _GVariantIter GVariantIter;
269struct _GVariantIter {
270 /*< private >*/
271 gsize x[16];
272};
273
274GLIB_AVAILABLE_IN_ALL
275GVariantIter * g_variant_iter_new (GVariant *value);
276GLIB_AVAILABLE_IN_ALL
277gsize g_variant_iter_init (GVariantIter *iter,
278 GVariant *value);
279GLIB_AVAILABLE_IN_ALL
280GVariantIter * g_variant_iter_copy (GVariantIter *iter);
281GLIB_AVAILABLE_IN_ALL
282gsize g_variant_iter_n_children (GVariantIter *iter);
283GLIB_AVAILABLE_IN_ALL
284void g_variant_iter_free (GVariantIter *iter);
285GLIB_AVAILABLE_IN_ALL
286GVariant * g_variant_iter_next_value (GVariantIter *iter);
287GLIB_AVAILABLE_IN_ALL
288gboolean g_variant_iter_next (GVariantIter *iter,
289 const gchar *format_string,
290 ...);
291GLIB_AVAILABLE_IN_ALL
292gboolean g_variant_iter_loop (GVariantIter *iter,
293 const gchar *format_string,
294 ...);
295
296
297typedef struct _GVariantBuilder GVariantBuilder;
298struct _GVariantBuilder {
299 /*< private >*/
300 union
301 {
302 struct {
303 gsize partial_magic;
304 const GVariantType *type;
305 gsize y[14];
306 } s;
307 gsize x[16];
308 } u;
309};
310
311typedef enum
312{
313 G_VARIANT_PARSE_ERROR_FAILED,
314 G_VARIANT_PARSE_ERROR_BASIC_TYPE_EXPECTED,
315 G_VARIANT_PARSE_ERROR_CANNOT_INFER_TYPE,
316 G_VARIANT_PARSE_ERROR_DEFINITE_TYPE_EXPECTED,
317 G_VARIANT_PARSE_ERROR_INPUT_NOT_AT_END,
318 G_VARIANT_PARSE_ERROR_INVALID_CHARACTER,
319 G_VARIANT_PARSE_ERROR_INVALID_FORMAT_STRING,
320 G_VARIANT_PARSE_ERROR_INVALID_OBJECT_PATH,
321 G_VARIANT_PARSE_ERROR_INVALID_SIGNATURE,
322 G_VARIANT_PARSE_ERROR_INVALID_TYPE_STRING,
323 G_VARIANT_PARSE_ERROR_NO_COMMON_TYPE,
324 G_VARIANT_PARSE_ERROR_NUMBER_OUT_OF_RANGE,
325 G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG,
326 G_VARIANT_PARSE_ERROR_TYPE_ERROR,
327 G_VARIANT_PARSE_ERROR_UNEXPECTED_TOKEN,
328 G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD,
329 G_VARIANT_PARSE_ERROR_UNTERMINATED_STRING_CONSTANT,
330 G_VARIANT_PARSE_ERROR_VALUE_EXPECTED,
331 G_VARIANT_PARSE_ERROR_RECURSION
332} GVariantParseError;
333#define G_VARIANT_PARSE_ERROR (g_variant_parse_error_quark ())
334
335GLIB_DEPRECATED_IN_2_38_FOR(g_variant_parse_error_quark)
336GQuark g_variant_parser_get_error_quark (void);
337
338GLIB_AVAILABLE_IN_ALL
339GQuark g_variant_parse_error_quark (void);
340
341/**
342 * G_VARIANT_BUILDER_INIT:
343 * @variant_type: a const GVariantType*
344 *
345 * A stack-allocated #GVariantBuilder must be initialized if it is
346 * used together with g_auto() to avoid warnings or crashes if
347 * function returns before g_variant_builder_init() is called on the
348 * builder.
349 *
350 * This macro can be used as initializer instead of an
351 * explicit zeroing a variable when declaring it and a following
352 * g_variant_builder_init(), but it cannot be assigned to a variable.
353 *
354 * The passed @variant_type should be a static GVariantType to avoid
355 * lifetime issues, as copying the @variant_type does not happen in
356 * the G_VARIANT_BUILDER_INIT() call, but rather in functions that
357 * make sure that #GVariantBuilder is valid.
358 *
359 * |[<!-- language="C" -->
360 * g_auto(GVariantBuilder) builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_BYTESTRING);
361 * ]|
362 *
363 * Since: 2.50
364 */
365#define G_VARIANT_BUILDER_INIT(variant_type) \
366 { \
367 { \
368 { \
369 2942751021u /* == GVSB_MAGIC_PARTIAL, see gvariant.c */, variant_type, { 0, } \
370 } \
371 } \
372 }
373
374GLIB_AVAILABLE_IN_ALL
375GVariantBuilder * g_variant_builder_new (const GVariantType *type);
376GLIB_AVAILABLE_IN_ALL
377void g_variant_builder_unref (GVariantBuilder *builder);
378GLIB_AVAILABLE_IN_ALL
379GVariantBuilder * g_variant_builder_ref (GVariantBuilder *builder);
380GLIB_AVAILABLE_IN_ALL
381void g_variant_builder_init (GVariantBuilder *builder,
382 const GVariantType *type);
383GLIB_AVAILABLE_IN_ALL
384GVariant * g_variant_builder_end (GVariantBuilder *builder);
385GLIB_AVAILABLE_IN_ALL
386void g_variant_builder_clear (GVariantBuilder *builder);
387GLIB_AVAILABLE_IN_ALL
388void g_variant_builder_open (GVariantBuilder *builder,
389 const GVariantType *type);
390GLIB_AVAILABLE_IN_ALL
391void g_variant_builder_close (GVariantBuilder *builder);
392GLIB_AVAILABLE_IN_ALL
393void g_variant_builder_add_value (GVariantBuilder *builder,
394 GVariant *value);
395GLIB_AVAILABLE_IN_ALL
396void g_variant_builder_add (GVariantBuilder *builder,
397 const gchar *format_string,
398 ...);
399GLIB_AVAILABLE_IN_ALL
400void g_variant_builder_add_parsed (GVariantBuilder *builder,
401 const gchar *format,
402 ...);
403
404GLIB_AVAILABLE_IN_ALL
405GVariant * g_variant_new (const gchar *format_string,
406 ...);
407GLIB_AVAILABLE_IN_ALL
408void g_variant_get (GVariant *value,
409 const gchar *format_string,
410 ...);
411GLIB_AVAILABLE_IN_ALL
412GVariant * g_variant_new_va (const gchar *format_string,
413 const gchar **endptr,
414 va_list *app);
415GLIB_AVAILABLE_IN_ALL
416void g_variant_get_va (GVariant *value,
417 const gchar *format_string,
418 const gchar **endptr,
419 va_list *app);
420GLIB_AVAILABLE_IN_2_34
421gboolean g_variant_check_format_string (GVariant *value,
422 const gchar *format_string,
423 gboolean copy_only);
424
425GLIB_AVAILABLE_IN_ALL
426GVariant * g_variant_parse (const GVariantType *type,
427 const gchar *text,
428 const gchar *limit,
429 const gchar **endptr,
430 GError **error);
431GLIB_AVAILABLE_IN_ALL
432GVariant * g_variant_new_parsed (const gchar *format,
433 ...);
434GLIB_AVAILABLE_IN_ALL
435GVariant * g_variant_new_parsed_va (const gchar *format,
436 va_list *app);
437
438GLIB_AVAILABLE_IN_2_40
439gchar * g_variant_parse_error_print_context (GError *error,
440 const gchar *source_str);
441
442GLIB_AVAILABLE_IN_ALL
443gint g_variant_compare (gconstpointer one,
444 gconstpointer two);
445
446typedef struct _GVariantDict GVariantDict;
447struct _GVariantDict {
448 /*< private >*/
449 union
450 {
451 struct {
452 GVariant *asv;
453 gsize partial_magic;
454 gsize y[14];
455 } s;
456 gsize x[16];
457 } u;
458};
459
460/**
461 * G_VARIANT_DICT_INIT:
462 * @asv: (nullable): a GVariant*
463 *
464 * A stack-allocated #GVariantDict must be initialized if it is used
465 * together with g_auto() to avoid warnings or crashes if function
466 * returns before g_variant_dict_init() is called on the builder.
467 *
468 * This macro can be used as initializer instead of an explicit
469 * zeroing a variable when declaring it and a following
470 * g_variant_dict_init(), but it cannot be assigned to a variable.
471 *
472 * The passed @asv has to live long enough for #GVariantDict to gather
473 * the entries from, as the gathering does not happen in the
474 * G_VARIANT_DICT_INIT() call, but rather in functions that make sure
475 * that #GVariantDict is valid. In context where the initialization
476 * value has to be a constant expression, the only possible value of
477 * @asv is %NULL. It is still possible to call g_variant_dict_init()
478 * safely with a different @asv right after the variable was
479 * initialized with G_VARIANT_DICT_INIT().
480 *
481 * |[<!-- language="C" -->
482 * g_autoptr(GVariant) variant = get_asv_variant ();
483 * g_auto(GVariantDict) dict = G_VARIANT_DICT_INIT (variant);
484 * ]|
485 *
486 * Since: 2.50
487 */
488#define G_VARIANT_DICT_INIT(asv) \
489 { \
490 { \
491 { \
492 asv, 3488698669u /* == GVSD_MAGIC_PARTIAL, see gvariant.c */, { 0, } \
493 } \
494 } \
495 }
496
497GLIB_AVAILABLE_IN_2_40
498GVariantDict * g_variant_dict_new (GVariant *from_asv);
499
500GLIB_AVAILABLE_IN_2_40
501void g_variant_dict_init (GVariantDict *dict,
502 GVariant *from_asv);
503
504GLIB_AVAILABLE_IN_2_40
505gboolean g_variant_dict_lookup (GVariantDict *dict,
506 const gchar *key,
507 const gchar *format_string,
508 ...);
509GLIB_AVAILABLE_IN_2_40
510GVariant * g_variant_dict_lookup_value (GVariantDict *dict,
511 const gchar *key,
512 const GVariantType *expected_type);
513GLIB_AVAILABLE_IN_2_40
514gboolean g_variant_dict_contains (GVariantDict *dict,
515 const gchar *key);
516GLIB_AVAILABLE_IN_2_40
517void g_variant_dict_insert (GVariantDict *dict,
518 const gchar *key,
519 const gchar *format_string,
520 ...);
521GLIB_AVAILABLE_IN_2_40
522void g_variant_dict_insert_value (GVariantDict *dict,
523 const gchar *key,
524 GVariant *value);
525GLIB_AVAILABLE_IN_2_40
526gboolean g_variant_dict_remove (GVariantDict *dict,
527 const gchar *key);
528GLIB_AVAILABLE_IN_2_40
529void g_variant_dict_clear (GVariantDict *dict);
530GLIB_AVAILABLE_IN_2_40
531GVariant * g_variant_dict_end (GVariantDict *dict);
532GLIB_AVAILABLE_IN_2_40
533GVariantDict * g_variant_dict_ref (GVariantDict *dict);
534GLIB_AVAILABLE_IN_2_40
535void g_variant_dict_unref (GVariantDict *dict);
536
537G_END_DECLS
538
539#endif /* __G_VARIANT_H__ */
540

source code of include/glib-2.0/glib/gvariant.h