2 * Copyright © 2007, 2008 Ryan Lortie
3 * Copyright © 2010 Codethink Limited
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 of the licence, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Ryan Lortie <desrt@desrt.ca>
27 #include <glib/gvariant-serialiser.h>
28 #include "gvariant-internal.h"
29 #include <glib/gvariant-core.h>
30 #include <glib/gtestutils.h>
31 #include <glib/gstrfuncs.h>
32 #include <glib/gslice.h>
33 #include <glib/ghash.h>
34 #include <glib/gmem.h>
42 * @short_description: strongly typed value datatype
43 * @see_also: GVariantType
45 * #GVariant is a variant datatype; it stores a value along with
46 * information about the type of that value. The range of possible
47 * values is determined by the type. The type system used by #GVariant
50 * #GVariant instances always have a type and a value (which are given
51 * at construction time). The type and value of a #GVariant instance
52 * can never change other than by the #GVariant itself being
53 * destroyed. A #GVariant cannot contain a pointer.
55 * #GVariant is reference counted using g_variant_ref() and
56 * g_variant_unref(). #GVariant also has floating reference counts --
57 * see g_variant_ref_sink().
59 * #GVariant is completely threadsafe. A #GVariant instance can be
60 * concurrently accessed in any way from any number of threads without
63 * #GVariant is heavily optimised for dealing with data in serialised
64 * form. It works particularly well with data located in memory-mapped
65 * files. It can perform nearly all deserialisation operations in a
66 * small constant time, usually touching only a single memory page.
67 * Serialised #GVariant data can also be sent over the network.
69 * #GVariant is largely compatible with D-Bus. Almost all types of
70 * #GVariant instances can be sent over D-Bus. See #GVariantType for
71 * exceptions. (However, #GVariant's serialisation format is not the same
72 * as the serialisation format of a D-Bus message body: use #GDBusMessage,
73 * in the gio library, for those.)
75 * For convenience to C programmers, #GVariant features powerful
76 * varargs-based value construction and destruction. This feature is
77 * designed to be embedded in other libraries.
79 * There is a Python-inspired text language for describing #GVariant
80 * values. #GVariant includes a printer for this language and a parser
81 * with type inferencing.
84 * <title>Memory Use</title>
86 * #GVariant tries to be quite efficient with respect to memory use.
87 * This section gives a rough idea of how much memory is used by the
88 * current implementation. The information here is subject to change
92 * The memory allocated by #GVariant can be grouped into 4 broad
93 * purposes: memory for serialised data, memory for the type
94 * information cache, buffer management memory and memory for the
95 * #GVariant structure itself.
97 * <refsect3 id="gvariant-serialised-data-memory">
98 * <title>Serialised Data Memory</title>
100 * This is the memory that is used for storing GVariant data in
101 * serialised form. This is what would be sent over the network or
102 * what would end up on disk.
105 * The amount of memory required to store a boolean is 1 byte. 16,
106 * 32 and 64 bit integers and double precision floating point numbers
107 * use their "natural" size. Strings (including object path and
108 * signature strings) are stored with a nul terminator, and as such
109 * use the length of the string plus 1 byte.
112 * Maybe types use no space at all to represent the null value and
113 * use the same amount of space (sometimes plus one byte) as the
114 * equivalent non-maybe-typed value to represent the non-null case.
117 * Arrays use the amount of space required to store each of their
118 * members, concatenated. Additionally, if the items stored in an
119 * array are not of a fixed-size (ie: strings, other arrays, etc)
120 * then an additional framing offset is stored for each item. The
121 * size of this offset is either 1, 2 or 4 bytes depending on the
122 * overall size of the container. Additionally, extra padding bytes
123 * are added as required for alignment of child values.
126 * Tuples (including dictionary entries) use the amount of space
127 * required to store each of their members, concatenated, plus one
128 * framing offset (as per arrays) for each non-fixed-sized item in
129 * the tuple, except for the last one. Additionally, extra padding
130 * bytes are added as required for alignment of child values.
133 * Variants use the same amount of space as the item inside of the
134 * variant, plus 1 byte, plus the length of the type string for the
135 * item inside the variant.
138 * As an example, consider a dictionary mapping strings to variants.
139 * In the case that the dictionary is empty, 0 bytes are required for
143 * If we add an item "width" that maps to the int32 value of 500 then
144 * we will use 4 byte to store the int32 (so 6 for the variant
145 * containing it) and 6 bytes for the string. The variant must be
146 * aligned to 8 after the 6 bytes of the string, so that's 2 extra
147 * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
148 * for the dictionary entry. An additional 1 byte is added to the
149 * array as a framing offset making a total of 15 bytes.
152 * If we add another entry, "title" that maps to a nullable string
153 * that happens to have a value of null, then we use 0 bytes for the
154 * null value (and 3 bytes for the variant to contain it along with
155 * its type string) plus 6 bytes for the string. Again, we need 2
156 * padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
159 * We now require extra padding between the two items in the array.
160 * After the 14 bytes of the first item, that's 2 bytes required. We
161 * now require 2 framing offsets for an extra two bytes. 14 + 2 + 11
162 * + 2 = 29 bytes to encode the entire two-item dictionary.
166 * <title>Type Information Cache</title>
168 * For each GVariant type that currently exists in the program a type
169 * information structure is kept in the type information cache. The
170 * type information structure is required for rapid deserialisation.
173 * Continuing with the above example, if a #GVariant exists with the
174 * type "a{sv}" then a type information struct will exist for
175 * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type
176 * will share the same type information. Additionally, all
177 * single-digit types are stored in read-only static memory and do
178 * not contribute to the writable memory footprint of a program using
182 * Aside from the type information structures stored in read-only
183 * memory, there are two forms of type information. One is used for
184 * container types where there is a single element type: arrays and
185 * maybe types. The other is used for container types where there
186 * are multiple element types: tuples and dictionary entries.
189 * Array type info structures are 6 * sizeof (void *), plus the
190 * memory required to store the type string itself. This means that
191 * on 32bit systems, the cache entry for "a{sv}" would require 30
192 * bytes of memory (plus malloc overhead).
195 * Tuple type info structures are 6 * sizeof (void *), plus 4 *
196 * sizeof (void *) for each item in the tuple, plus the memory
197 * required to store the type string itself. A 2-item tuple, for
198 * example, would have a type information structure that consumed
199 * writable memory in the size of 14 * sizeof (void *) (plus type
200 * string) This means that on 32bit systems, the cache entry for
201 * "{sv}" would require 61 bytes of memory (plus malloc overhead).
204 * This means that in total, for our "a{sv}" example, 91 bytes of
205 * type information would be allocated.
208 * The type information cache, additionally, uses a #GHashTable to
209 * store and lookup the cached items and stores a pointer to this
210 * hash table in static storage. The hash table is freed when there
211 * are zero items in the type cache.
214 * Although these sizes may seem large it is important to remember
215 * that a program will probably only have a very small number of
216 * different types of values in it and that only one type information
217 * structure is required for many different values of the same type.
221 * <title>Buffer Management Memory</title>
223 * #GVariant uses an internal buffer management structure to deal
224 * with the various different possible sources of serialised data
225 * that it uses. The buffer is responsible for ensuring that the
226 * correct call is made when the data is no longer in use by
227 * #GVariant. This may involve a g_free() or a g_slice_free() or
228 * even g_mapped_file_unref().
231 * One buffer management structure is used for each chunk of
232 * serialised data. The size of the buffer management structure is 4
233 * * (void *). On 32bit systems, that's 16 bytes.
237 * <title>GVariant structure</title>
239 * The size of a #GVariant structure is 6 * (void *). On 32 bit
240 * systems, that's 24 bytes.
243 * #GVariant structures only exist if they are explicitly created
244 * with API calls. For example, if a #GVariant is constructed out of
245 * serialised data for the example given above (with the dictionary)
246 * then although there are 9 individual values that comprise the
247 * entire dictionary (two keys, two values, two variants containing
248 * the values, two dictionary entries, plus the dictionary itself),
249 * only 1 #GVariant instance exists -- the one referring to the
253 * If calls are made to start accessing the other values then
254 * #GVariant instances will exist for those values only for as long
255 * as they are in use (ie: until you call g_variant_unref()). The
256 * type information is shared. The serialised data and the buffer
257 * management structure for that serialised data is shared by the
262 * <title>Summary</title>
264 * To put the entire example together, for our dictionary mapping
265 * strings to variants (with two entries, as given above), we are
266 * using 91 bytes of memory for type information, 29 byes of memory
267 * for the serialised data, 16 bytes for buffer management and 24
268 * bytes for the #GVariant instance, or a total of 160 bytes, plus
269 * malloc overhead. If we were to use g_variant_get_child_value() to
270 * access the two dictionary entries, we would use an additional 48
271 * bytes. If we were to have other dictionaries of the same type, we
272 * would use more memory for the serialised data and buffer
273 * management for those dictionaries, but the type information would
280 /* definition of GVariant structure is in gvariant-core.c */
282 /* this is a g_return_val_if_fail() for making
283 * sure a (GVariant *) has the required type.
285 #define TYPE_CHECK(value, TYPE, val) \
286 if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \
287 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, \
288 "g_variant_is_of_type (" #value \
293 /* Numeric Type Constructor/Getters {{{1 */
295 * g_variant_new_from_trusted:
296 * @type: the #GVariantType
297 * @data: the data to use
298 * @size: the size of @data
299 * @returns: a new floating #GVariant
301 * Constructs a new trusted #GVariant instance from the provided data.
302 * This is used to implement g_variant_new_* for all the basic types.
305 g_variant_new_from_trusted (const GVariantType *type,
312 buffer = g_buffer_new_from_data (data, size);
313 value = g_variant_new_from_buffer (type, buffer, TRUE);
314 g_buffer_unref (buffer);
320 * g_variant_new_boolean:
321 * @value: a #gboolean value
322 * @returns: (transfer none): a floating reference to a new boolean #GVariant instance
324 * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
329 g_variant_new_boolean (gboolean value)
333 return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1);
337 * g_variant_get_boolean:
338 * @value: a boolean #GVariant instance
339 * @returns: %TRUE or %FALSE
341 * Returns the boolean value of @value.
343 * It is an error to call this function with a @value of any type
344 * other than %G_VARIANT_TYPE_BOOLEAN.
349 g_variant_get_boolean (GVariant *value)
353 TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE);
355 data = g_variant_get_data (value);
357 return data != NULL ? *data != 0 : FALSE;
360 /* the constructors and accessors for byte, int{16,32,64}, handles and
361 * doubles all look pretty much exactly the same, so we reduce
364 #define NUMERIC_TYPE(TYPE, type, ctype) \
365 GVariant *g_variant_new_##type (ctype value) { \
366 return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \
367 &value, sizeof value); \
369 ctype g_variant_get_##type (GVariant *value) { \
371 TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \
372 data = g_variant_get_data (value); \
373 return data != NULL ? *data : 0; \
378 * g_variant_new_byte:
379 * @value: a #guint8 value
380 * @returns: (transfer none): a floating reference to a new byte #GVariant instance
382 * Creates a new byte #GVariant instance.
387 * g_variant_get_byte:
388 * @value: a byte #GVariant instance
389 * @returns: a #guchar
391 * Returns the byte value of @value.
393 * It is an error to call this function with a @value of any type
394 * other than %G_VARIANT_TYPE_BYTE.
398 NUMERIC_TYPE (BYTE, byte, guchar)
401 * g_variant_new_int16:
402 * @value: a #gint16 value
403 * @returns: (transfer none): a floating reference to a new int16 #GVariant instance
405 * Creates a new int16 #GVariant instance.
410 * g_variant_get_int16:
411 * @value: a int16 #GVariant instance
412 * @returns: a #gint16
414 * Returns the 16-bit signed integer value of @value.
416 * It is an error to call this function with a @value of any type
417 * other than %G_VARIANT_TYPE_INT16.
421 NUMERIC_TYPE (INT16, int16, gint16)
424 * g_variant_new_uint16:
425 * @value: a #guint16 value
426 * @returns: (transfer none): a floating reference to a new uint16 #GVariant instance
428 * Creates a new uint16 #GVariant instance.
433 * g_variant_get_uint16:
434 * @value: a uint16 #GVariant instance
435 * @returns: a #guint16
437 * Returns the 16-bit unsigned integer value of @value.
439 * It is an error to call this function with a @value of any type
440 * other than %G_VARIANT_TYPE_UINT16.
444 NUMERIC_TYPE (UINT16, uint16, guint16)
447 * g_variant_new_int32:
448 * @value: a #gint32 value
449 * @returns: (transfer none): a floating reference to a new int32 #GVariant instance
451 * Creates a new int32 #GVariant instance.
456 * g_variant_get_int32:
457 * @value: a int32 #GVariant instance
458 * @returns: a #gint32
460 * Returns the 32-bit signed integer value of @value.
462 * It is an error to call this function with a @value of any type
463 * other than %G_VARIANT_TYPE_INT32.
467 NUMERIC_TYPE (INT32, int32, gint32)
470 * g_variant_new_uint32:
471 * @value: a #guint32 value
472 * @returns: (transfer none): a floating reference to a new uint32 #GVariant instance
474 * Creates a new uint32 #GVariant instance.
479 * g_variant_get_uint32:
480 * @value: a uint32 #GVariant instance
481 * @returns: a #guint32
483 * Returns the 32-bit unsigned integer value of @value.
485 * It is an error to call this function with a @value of any type
486 * other than %G_VARIANT_TYPE_UINT32.
490 NUMERIC_TYPE (UINT32, uint32, guint32)
493 * g_variant_new_int64:
494 * @value: a #gint64 value
495 * @returns: (transfer none): a floating reference to a new int64 #GVariant instance
497 * Creates a new int64 #GVariant instance.
502 * g_variant_get_int64:
503 * @value: a int64 #GVariant instance
504 * @returns: a #gint64
506 * Returns the 64-bit signed integer value of @value.
508 * It is an error to call this function with a @value of any type
509 * other than %G_VARIANT_TYPE_INT64.
513 NUMERIC_TYPE (INT64, int64, gint64)
516 * g_variant_new_uint64:
517 * @value: a #guint64 value
518 * @returns: (transfer none): a floating reference to a new uint64 #GVariant instance
520 * Creates a new uint64 #GVariant instance.
525 * g_variant_get_uint64:
526 * @value: a uint64 #GVariant instance
527 * @returns: a #guint64
529 * Returns the 64-bit unsigned integer value of @value.
531 * It is an error to call this function with a @value of any type
532 * other than %G_VARIANT_TYPE_UINT64.
536 NUMERIC_TYPE (UINT64, uint64, guint64)
539 * g_variant_new_handle:
540 * @value: a #gint32 value
541 * @returns: (transfer none): a floating reference to a new handle #GVariant instance
543 * Creates a new handle #GVariant instance.
545 * By convention, handles are indexes into an array of file descriptors
546 * that are sent alongside a D-Bus message. If you're not interacting
547 * with D-Bus, you probably don't need them.
552 * g_variant_get_handle:
553 * @value: a handle #GVariant instance
554 * @returns: a #gint32
556 * Returns the 32-bit signed integer value of @value.
558 * It is an error to call this function with a @value of any type other
559 * than %G_VARIANT_TYPE_HANDLE.
561 * By convention, handles are indexes into an array of file descriptors
562 * that are sent alongside a D-Bus message. If you're not interacting
563 * with D-Bus, you probably don't need them.
567 NUMERIC_TYPE (HANDLE, handle, gint32)
570 * g_variant_new_double:
571 * @value: a #gdouble floating point value
572 * @returns: (transfer none): a floating reference to a new double #GVariant instance
574 * Creates a new double #GVariant instance.
579 * g_variant_get_double:
580 * @value: a double #GVariant instance
581 * @returns: a #gdouble
583 * Returns the double precision floating point value of @value.
585 * It is an error to call this function with a @value of any type
586 * other than %G_VARIANT_TYPE_DOUBLE.
590 NUMERIC_TYPE (DOUBLE, double, gdouble)
592 /* Container type Constructor / Deconstructors {{{1 */
594 * g_variant_new_maybe:
595 * @child_type: (allow-none): the #GVariantType of the child, or %NULL
596 * @child: (allow-none): the child value, or %NULL
597 * @returns: (transfer none): a floating reference to a new #GVariant maybe instance
599 * Depending on if @child is %NULL, either wraps @child inside of a
600 * maybe container or creates a Nothing instance for the given @type.
602 * At least one of @child_type and @child must be non-%NULL.
603 * If @child_type is non-%NULL then it must be a definite type.
604 * If they are both non-%NULL then @child_type must be the type
607 * If @child is a floating reference (see g_variant_ref_sink()), the new
608 * instance takes ownership of @child.
613 g_variant_new_maybe (const GVariantType *child_type,
616 GVariantType *maybe_type;
619 g_return_val_if_fail (child_type == NULL || g_variant_type_is_definite
621 g_return_val_if_fail (child_type != NULL || child != NULL, NULL);
622 g_return_val_if_fail (child_type == NULL || child == NULL ||
623 g_variant_is_of_type (child, child_type),
626 if (child_type == NULL)
627 child_type = g_variant_get_type (child);
629 maybe_type = g_variant_type_new_maybe (child_type);
636 children = g_new (GVariant *, 1);
637 children[0] = g_variant_ref_sink (child);
638 trusted = g_variant_is_trusted (children[0]);
640 value = g_variant_new_from_children (maybe_type, children, 1, trusted);
643 value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE);
645 g_variant_type_free (maybe_type);
651 * g_variant_get_maybe:
652 * @value: a maybe-typed value
653 * @returns: (allow-none) (transfer full): the contents of @value, or %NULL
655 * Given a maybe-typed #GVariant instance, extract its value. If the
656 * value is Nothing, then this function returns %NULL.
661 g_variant_get_maybe (GVariant *value)
663 TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL);
665 if (g_variant_n_children (value))
666 return g_variant_get_child_value (value, 0);
672 * g_variant_new_variant: (constructor)
673 * @value: a #GVariant instance
674 * @returns: (transfer none): a floating reference to a new variant #GVariant instance
676 * Boxes @value. The result is a #GVariant instance representing a
677 * variant containing the original value.
679 * If @child is a floating reference (see g_variant_ref_sink()), the new
680 * instance takes ownership of @child.
685 g_variant_new_variant (GVariant *value)
687 g_return_val_if_fail (value != NULL, NULL);
689 g_variant_ref_sink (value);
691 return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
692 g_memdup (&value, sizeof value),
693 1, g_variant_is_trusted (value));
697 * g_variant_get_variant:
698 * @value: a variant #GVariant instance
699 * @returns: (transfer full): the item contained in the variant
701 * Unboxes @value. The result is the #GVariant instance that was
702 * contained in @value.
707 g_variant_get_variant (GVariant *value)
709 TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL);
711 return g_variant_get_child_value (value, 0);
715 * g_variant_new_array:
716 * @child_type: (allow-none): the element type of the new array
717 * @children: (allow-none) (array length=n_children): an array of
718 * #GVariant pointers, the children
719 * @n_children: the length of @children
720 * @returns: (transfer none): a floating reference to a new #GVariant array
722 * Creates a new #GVariant array from @children.
724 * @child_type must be non-%NULL if @n_children is zero. Otherwise, the
725 * child type is determined by inspecting the first element of the
726 * @children array. If @child_type is non-%NULL then it must be a
729 * The items of the array are taken from the @children array. No entry
730 * in the @children array may be %NULL.
732 * All items in the array must have the same type, which must be the
733 * same as @child_type, if given.
735 * If the @children are floating references (see g_variant_ref_sink()), the
736 * new instance takes ownership of them as if via g_variant_ref_sink().
741 g_variant_new_array (const GVariantType *child_type,
742 GVariant * const *children,
745 GVariantType *array_type;
746 GVariant **my_children;
751 g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
752 g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
753 g_return_val_if_fail (child_type == NULL ||
754 g_variant_type_is_definite (child_type), NULL);
756 my_children = g_new (GVariant *, n_children);
759 if (child_type == NULL)
760 child_type = g_variant_get_type (children[0]);
761 array_type = g_variant_type_new_array (child_type);
763 for (i = 0; i < n_children; i++)
765 TYPE_CHECK (children[i], child_type, NULL);
766 my_children[i] = g_variant_ref_sink (children[i]);
767 trusted &= g_variant_is_trusted (children[i]);
770 value = g_variant_new_from_children (array_type, my_children,
771 n_children, trusted);
772 g_variant_type_free (array_type);
778 * g_variant_make_tuple_type:
779 * @children: (array length=n_children): an array of GVariant *
780 * @n_children: the length of @children
782 * Return the type of a tuple containing @children as its items.
784 static GVariantType *
785 g_variant_make_tuple_type (GVariant * const *children,
788 const GVariantType **types;
792 types = g_new (const GVariantType *, n_children);
794 for (i = 0; i < n_children; i++)
795 types[i] = g_variant_get_type (children[i]);
797 type = g_variant_type_new_tuple (types, n_children);
804 * g_variant_new_tuple:
805 * @children: (array length=n_children): the items to make the tuple out of
806 * @n_children: the length of @children
807 * @returns: (transfer none): a floating reference to a new #GVariant tuple
809 * Creates a new tuple #GVariant out of the items in @children. The
810 * type is determined from the types of @children. No entry in the
811 * @children array may be %NULL.
813 * If @n_children is 0 then the unit tuple is constructed.
815 * If the @children are floating references (see g_variant_ref_sink()), the
816 * new instance takes ownership of them as if via g_variant_ref_sink().
821 g_variant_new_tuple (GVariant * const *children,
824 GVariantType *tuple_type;
825 GVariant **my_children;
830 g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
832 my_children = g_new (GVariant *, n_children);
835 for (i = 0; i < n_children; i++)
837 my_children[i] = g_variant_ref_sink (children[i]);
838 trusted &= g_variant_is_trusted (children[i]);
841 tuple_type = g_variant_make_tuple_type (children, n_children);
842 value = g_variant_new_from_children (tuple_type, my_children,
843 n_children, trusted);
844 g_variant_type_free (tuple_type);
850 * g_variant_make_dict_entry_type:
851 * @key: a #GVariant, the key
852 * @val: a #GVariant, the value
854 * Return the type of a dictionary entry containing @key and @val as its
857 static GVariantType *
858 g_variant_make_dict_entry_type (GVariant *key,
861 return g_variant_type_new_dict_entry (g_variant_get_type (key),
862 g_variant_get_type (val));
866 * g_variant_new_dict_entry: (constructor)
867 * @key: a basic #GVariant, the key
868 * @value: a #GVariant, the value
869 * @returns: (transfer none): a floating reference to a new dictionary entry #GVariant
871 * Creates a new dictionary entry #GVariant. @key and @value must be
872 * non-%NULL. @key must be a value of a basic type (ie: not a container).
874 * If the @key or @value are floating references (see g_variant_ref_sink()),
875 * the new instance takes ownership of them as if via g_variant_ref_sink().
880 g_variant_new_dict_entry (GVariant *key,
883 GVariantType *dict_type;
887 g_return_val_if_fail (key != NULL && value != NULL, NULL);
888 g_return_val_if_fail (!g_variant_is_container (key), NULL);
890 children = g_new (GVariant *, 2);
891 children[0] = g_variant_ref_sink (key);
892 children[1] = g_variant_ref_sink (value);
893 trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
895 dict_type = g_variant_make_dict_entry_type (key, value);
896 value = g_variant_new_from_children (dict_type, children, 2, trusted);
897 g_variant_type_free (dict_type);
903 * g_variant_lookup: (skip)
904 * @dictionary: a dictionary #GVariant
905 * @key: the key to lookup in the dictionary
906 * @format_string: a GVariant format string
907 * @...: the arguments to unpack the value into
909 * Looks up a value in a dictionary #GVariant.
911 * This function is a wrapper around g_variant_lookup_value() and
912 * g_variant_get(). In the case that %NULL would have been returned,
913 * this function returns %FALSE. Otherwise, it unpacks the returned
914 * value and returns %TRUE.
916 * See g_variant_get() for information about @format_string.
918 * Returns: %TRUE if a value was unpacked
923 g_variant_lookup (GVariant *dictionary,
925 const gchar *format_string,
932 g_variant_get_data (dictionary);
934 type = g_variant_format_string_scan_type (format_string, NULL, NULL);
935 value = g_variant_lookup_value (dictionary, key, type);
936 g_variant_type_free (type);
942 va_start (ap, format_string);
943 g_variant_get_va (value, format_string, NULL, &ap);
944 g_variant_unref (value);
955 * g_variant_lookup_value:
956 * @dictionary: a dictionary #GVariant
957 * @key: the key to lookup in the dictionary
958 * @expected_type: (allow-none): a #GVariantType, or %NULL
960 * Looks up a value in a dictionary #GVariant.
962 * This function works with dictionaries of the type
963 * <literal>a{s*}</literal> (and equally well with type
964 * <literal>a{o*}</literal>, but we only further discuss the string case
965 * for sake of clarity).
967 * In the event that @dictionary has the type <literal>a{sv}</literal>,
968 * the @expected_type string specifies what type of value is expected to
969 * be inside of the variant. If the value inside the variant has a
970 * different type then %NULL is returned. In the event that @dictionary
971 * has a value type other than <literal>v</literal> then @expected_type
972 * must directly match the key type and it is used to unpack the value
973 * directly or an error occurs.
975 * In either case, if @key is not found in @dictionary, %NULL is
978 * If the key is found and the value has the correct type, it is
979 * returned. If @expected_type was specified then any non-%NULL return
980 * value will have this type.
982 * Returns: (transfer full): the value of the dictionary key, or %NULL
987 g_variant_lookup_value (GVariant *dictionary,
989 const GVariantType *expected_type)
995 g_return_val_if_fail (g_variant_is_of_type (dictionary,
996 G_VARIANT_TYPE ("a{s*}")) ||
997 g_variant_is_of_type (dictionary,
998 G_VARIANT_TYPE ("a{o*}")),
1001 g_variant_iter_init (&iter, dictionary);
1003 while ((entry = g_variant_iter_next_value (&iter)))
1005 GVariant *entry_key;
1008 entry_key = g_variant_get_child_value (entry, 0);
1009 matches = strcmp (g_variant_get_string (entry_key, NULL), key) == 0;
1010 g_variant_unref (entry_key);
1015 g_variant_unref (entry);
1021 value = g_variant_get_child_value (entry, 1);
1022 g_variant_unref (entry);
1024 if (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT))
1028 tmp = g_variant_get_variant (value);
1029 g_variant_unref (value);
1031 if (expected_type && !g_variant_is_of_type (tmp, expected_type))
1033 g_variant_unref (tmp);
1040 g_return_val_if_fail (expected_type == NULL || value == NULL ||
1041 g_variant_is_of_type (value, expected_type), NULL);
1047 * g_variant_get_fixed_array:
1048 * @value: a #GVariant array with fixed-sized elements
1049 * @n_elements: (out): a pointer to the location to store the number of items
1050 * @element_size: the size of each element
1051 * @returns: (array length=n_elements) (transfer none): a pointer to
1054 * Provides access to the serialised data for an array of fixed-sized
1057 * @value must be an array with fixed-sized elements. Numeric types are
1058 * fixed-size, as are tuples containing only other fixed-sized types.
1060 * @element_size must be the size of a single element in the array,
1061 * as given by the section on
1062 * <link linkend='gvariant-serialised-data-memory'>Serialised Data
1065 * In particular, arrays of these fixed-sized types can be interpreted
1066 * as an array of the given C type, with @element_size set to
1067 * <code>sizeof</code> the appropriate type:
1071 * <thead><row><entry>element type</entry> <entry>C type</entry></row></thead>
1073 * <row><entry>%G_VARIANT_TYPE_INT16 (etc.)</entry>
1074 * <entry>#gint16 (etc.)</entry></row>
1075 * <row><entry>%G_VARIANT_TYPE_BOOLEAN</entry>
1076 * <entry>#guchar (not #gboolean!)</entry></row>
1077 * <row><entry>%G_VARIANT_TYPE_BYTE</entry> <entry>#guchar</entry></row>
1078 * <row><entry>%G_VARIANT_TYPE_HANDLE</entry> <entry>#guint32</entry></row>
1079 * <row><entry>%G_VARIANT_TYPE_DOUBLE</entry> <entry>#gdouble</entry></row>
1084 * For example, if calling this function for an array of 32 bit integers,
1085 * you might say <code>sizeof (gint32)</code>. This value isn't used
1086 * except for the purpose of a double-check that the form of the
1087 * seralised data matches the caller's expectation.
1089 * @n_elements, which must be non-%NULL is set equal to the number of
1090 * items in the array.
1095 g_variant_get_fixed_array (GVariant *value,
1099 GVariantTypeInfo *array_info;
1100 gsize array_element_size;
1104 TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
1106 g_return_val_if_fail (n_elements != NULL, NULL);
1107 g_return_val_if_fail (element_size > 0, NULL);
1109 array_info = g_variant_get_type_info (value);
1110 g_variant_type_info_query_element (array_info, NULL, &array_element_size);
1112 g_return_val_if_fail (array_element_size, NULL);
1114 if G_UNLIKELY (array_element_size != element_size)
1116 if (array_element_size)
1117 g_critical ("g_variant_get_fixed_array: assertion "
1118 "`g_variant_array_has_fixed_size (value, element_size)' "
1119 "failed: array size %"G_GSIZE_FORMAT" does not match "
1120 "given element_size %"G_GSIZE_FORMAT".",
1121 array_element_size, element_size);
1123 g_critical ("g_variant_get_fixed_array: assertion "
1124 "`g_variant_array_has_fixed_size (value, element_size)' "
1125 "failed: array does not have fixed size.");
1128 data = g_variant_get_data (value);
1129 size = g_variant_get_size (value);
1131 if (size % element_size)
1134 *n_elements = size / element_size;
1143 * g_variant_new_fixed_array:
1144 * @element_type: the #GVariantType of each element
1145 * @elements: a pointer to the fixed array of contiguous elements
1146 * @n_elements: the number of elements
1147 * @element_size: the size of each element
1148 * @returns: (transfer none): a floating reference to a new array #GVariant instance
1150 * Provides access to the serialised data for an array of fixed-sized
1153 * @value must be an array with fixed-sized elements. Numeric types are
1154 * fixed-size as are tuples containing only other fixed-sized types.
1156 * @element_size must be the size of a single element in the array. For
1157 * example, if calling this function for an array of 32 bit integers,
1158 * you might say <code>sizeof (gint32)</code>. This value isn't used
1159 * except for the purpose of a double-check that the form of the
1160 * seralised data matches the caller's expectation.
1162 * @n_elements, which must be non-%NULL is set equal to the number of
1163 * items in the array.
1168 g_variant_new_fixed_array (const GVariantType *element_type,
1169 gconstpointer elements,
1173 GVariantType *array_type;
1174 gsize array_element_size;
1175 GVariantTypeInfo *array_info;
1179 g_return_val_if_fail (g_variant_type_is_definite (element_type), NULL);
1180 g_return_val_if_fail (element_size > 0, NULL);
1182 array_type = g_variant_type_new_array (element_type);
1183 array_info = g_variant_type_info_get (array_type);
1184 g_variant_type_info_query_element (array_info, NULL, &array_element_size);
1185 if G_UNLIKELY (array_element_size != element_size)
1187 if (array_element_size)
1188 g_critical ("g_variant_new_fixed_array: array size %" G_GSIZE_FORMAT
1189 " does not match given element_size %" G_GSIZE_FORMAT ".",
1190 array_element_size, element_size);
1192 g_critical ("g_variant_get_fixed_array: array does not have fixed size.");
1196 data = g_memdup (elements, n_elements * element_size);
1197 value = g_variant_new_from_data (array_type, data,
1198 n_elements * element_size,
1199 FALSE, g_free, data);
1201 g_variant_type_free (array_type);
1202 g_variant_type_info_unref (array_info);
1207 /* String type constructor/getters/validation {{{1 */
1209 * g_variant_new_string:
1210 * @string: a normal utf8 nul-terminated string
1211 * @returns: (transfer none): a floating reference to a new string #GVariant instance
1213 * Creates a string #GVariant with the contents of @string.
1215 * @string must be valid utf8.
1220 g_variant_new_string (const gchar *string)
1222 g_return_val_if_fail (string != NULL, NULL);
1223 g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
1225 return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING,
1226 string, strlen (string) + 1);
1230 * g_variant_new_object_path:
1231 * @object_path: a normal C nul-terminated string
1232 * @returns: (transfer none): a floating reference to a new object path #GVariant instance
1234 * Creates a D-Bus object path #GVariant with the contents of @string.
1235 * @string must be a valid D-Bus object path. Use
1236 * g_variant_is_object_path() if you're not sure.
1241 g_variant_new_object_path (const gchar *object_path)
1243 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1245 return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH,
1246 object_path, strlen (object_path) + 1);
1250 * g_variant_is_object_path:
1251 * @string: a normal C nul-terminated string
1252 * @returns: %TRUE if @string is a D-Bus object path
1254 * Determines if a given string is a valid D-Bus object path. You
1255 * should ensure that a string is a valid D-Bus object path before
1256 * passing it to g_variant_new_object_path().
1258 * A valid object path starts with '/' followed by zero or more
1259 * sequences of characters separated by '/' characters. Each sequence
1260 * must contain only the characters "[A-Z][a-z][0-9]_". No sequence
1261 * (including the one following the final '/' character) may be empty.
1266 g_variant_is_object_path (const gchar *string)
1268 g_return_val_if_fail (string != NULL, FALSE);
1270 return g_variant_serialiser_is_object_path (string, strlen (string) + 1);
1274 * g_variant_new_signature:
1275 * @signature: a normal C nul-terminated string
1276 * @returns: (transfer none): a floating reference to a new signature #GVariant instance
1278 * Creates a D-Bus type signature #GVariant with the contents of
1279 * @string. @string must be a valid D-Bus type signature. Use
1280 * g_variant_is_signature() if you're not sure.
1285 g_variant_new_signature (const gchar *signature)
1287 g_return_val_if_fail (g_variant_is_signature (signature), NULL);
1289 return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE,
1290 signature, strlen (signature) + 1);
1294 * g_variant_is_signature:
1295 * @string: a normal C nul-terminated string
1296 * @returns: %TRUE if @string is a D-Bus type signature
1298 * Determines if a given string is a valid D-Bus type signature. You
1299 * should ensure that a string is a valid D-Bus type signature before
1300 * passing it to g_variant_new_signature().
1302 * D-Bus type signatures consist of zero or more definite #GVariantType
1303 * strings in sequence.
1308 g_variant_is_signature (const gchar *string)
1310 g_return_val_if_fail (string != NULL, FALSE);
1312 return g_variant_serialiser_is_signature (string, strlen (string) + 1);
1316 * g_variant_get_string:
1317 * @value: a string #GVariant instance
1318 * @length: (allow-none) (default 0) (out): a pointer to a #gsize,
1319 * to store the length
1320 * @returns: (transfer none): the constant string, utf8 encoded
1322 * Returns the string value of a #GVariant instance with a string
1323 * type. This includes the types %G_VARIANT_TYPE_STRING,
1324 * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
1326 * The string will always be utf8 encoded.
1328 * If @length is non-%NULL then the length of the string (in bytes) is
1329 * returned there. For trusted values, this information is already
1330 * known. For untrusted values, a strlen() will be performed.
1332 * It is an error to call this function with a @value of any type
1333 * other than those three.
1335 * The return value remains valid as long as @value exists.
1340 g_variant_get_string (GVariant *value,
1346 g_return_val_if_fail (value != NULL, NULL);
1347 g_return_val_if_fail (
1348 g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
1349 g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) ||
1350 g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL);
1352 data = g_variant_get_data (value);
1353 size = g_variant_get_size (value);
1355 if (!g_variant_is_trusted (value))
1357 switch (g_variant_classify (value))
1359 case G_VARIANT_CLASS_STRING:
1360 if (g_variant_serialiser_is_string (data, size))
1367 case G_VARIANT_CLASS_OBJECT_PATH:
1368 if (g_variant_serialiser_is_object_path (data, size))
1375 case G_VARIANT_CLASS_SIGNATURE:
1376 if (g_variant_serialiser_is_signature (data, size))
1384 g_assert_not_reached ();
1395 * g_variant_dup_string:
1396 * @value: a string #GVariant instance
1397 * @length: (out): a pointer to a #gsize, to store the length
1398 * @returns: (transfer full): a newly allocated string, utf8 encoded
1400 * Similar to g_variant_get_string() except that instead of returning
1401 * a constant string, the string is duplicated.
1403 * The string will always be utf8 encoded.
1405 * The return value must be freed using g_free().
1410 g_variant_dup_string (GVariant *value,
1413 return g_strdup (g_variant_get_string (value, length));
1417 * g_variant_new_strv:
1418 * @strv: (array length=length) (element-type utf8): an array of strings
1419 * @length: the length of @strv, or -1
1420 * @returns: (transfer none): a new floating #GVariant instance
1422 * Constructs an array of strings #GVariant from the given array of
1425 * If @length is -1 then @strv is %NULL-terminated.
1430 g_variant_new_strv (const gchar * const *strv,
1436 g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1439 length = g_strv_length ((gchar **) strv);
1441 strings = g_new (GVariant *, length);
1442 for (i = 0; i < length; i++)
1443 strings[i] = g_variant_ref_sink (g_variant_new_string (strv[i]));
1445 return g_variant_new_from_children (G_VARIANT_TYPE_STRING_ARRAY,
1446 strings, length, TRUE);
1450 * g_variant_get_strv:
1451 * @value: an array of strings #GVariant
1452 * @length: (out) (allow-none): the length of the result, or %NULL
1453 * @returns: (array length=length zero-terminated=1) (transfer container): an array of constant
1456 * Gets the contents of an array of strings #GVariant. This call
1457 * makes a shallow copy; the return result should be released with
1458 * g_free(), but the individual strings must not be modified.
1460 * If @length is non-%NULL then the number of elements in the result
1461 * is stored there. In any case, the resulting array will be
1464 * For an empty array, @length will be set to 0 and a pointer to a
1465 * %NULL pointer will be returned.
1470 g_variant_get_strv (GVariant *value,
1477 TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL);
1479 g_variant_get_data (value);
1480 n = g_variant_n_children (value);
1481 strv = g_new (const gchar *, n + 1);
1483 for (i = 0; i < n; i++)
1487 string = g_variant_get_child_value (value, i);
1488 strv[i] = g_variant_get_string (string, NULL);
1489 g_variant_unref (string);
1500 * g_variant_dup_strv:
1501 * @value: an array of strings #GVariant
1502 * @length: (out) (allow-none): the length of the result, or %NULL
1503 * @returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1505 * Gets the contents of an array of strings #GVariant. This call
1506 * makes a deep copy; the return result should be released with
1509 * If @length is non-%NULL then the number of elements in the result
1510 * is stored there. In any case, the resulting array will be
1513 * For an empty array, @length will be set to 0 and a pointer to a
1514 * %NULL pointer will be returned.
1519 g_variant_dup_strv (GVariant *value,
1526 TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL);
1528 n = g_variant_n_children (value);
1529 strv = g_new (gchar *, n + 1);
1531 for (i = 0; i < n; i++)
1535 string = g_variant_get_child_value (value, i);
1536 strv[i] = g_variant_dup_string (string, NULL);
1537 g_variant_unref (string);
1548 * g_variant_new_objv:
1549 * @strv: (array length=length) (element-type utf8): an array of strings
1550 * @length: the length of @strv, or -1
1551 * @returns: (transfer none): a new floating #GVariant instance
1553 * Constructs an array of object paths #GVariant from the given array of
1556 * Each string must be a valid #GVariant object path; see
1557 * g_variant_is_object_path().
1559 * If @length is -1 then @strv is %NULL-terminated.
1564 g_variant_new_objv (const gchar * const *strv,
1570 g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1573 length = g_strv_length ((gchar **) strv);
1575 strings = g_new (GVariant *, length);
1576 for (i = 0; i < length; i++)
1577 strings[i] = g_variant_ref_sink (g_variant_new_object_path (strv[i]));
1579 return g_variant_new_from_children (G_VARIANT_TYPE_OBJECT_PATH_ARRAY,
1580 strings, length, TRUE);
1584 * g_variant_get_objv:
1585 * @value: an array of object paths #GVariant
1586 * @length: (out) (allow-none): the length of the result, or %NULL
1587 * @returns: (array length=length zero-terminated=1) (transfer container): an array of constant
1590 * Gets the contents of an array of object paths #GVariant. This call
1591 * makes a shallow copy; the return result should be released with
1592 * g_free(), but the individual strings must not be modified.
1594 * If @length is non-%NULL then the number of elements in the result
1595 * is stored there. In any case, the resulting array will be
1598 * For an empty array, @length will be set to 0 and a pointer to a
1599 * %NULL pointer will be returned.
1604 g_variant_get_objv (GVariant *value,
1611 TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL);
1613 g_variant_get_data (value);
1614 n = g_variant_n_children (value);
1615 strv = g_new (const gchar *, n + 1);
1617 for (i = 0; i < n; i++)
1621 string = g_variant_get_child_value (value, i);
1622 strv[i] = g_variant_get_string (string, NULL);
1623 g_variant_unref (string);
1634 * g_variant_dup_objv:
1635 * @value: an array of object paths #GVariant
1636 * @length: (out) (allow-none): the length of the result, or %NULL
1637 * @returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1639 * Gets the contents of an array of object paths #GVariant. This call
1640 * makes a deep copy; the return result should be released with
1643 * If @length is non-%NULL then the number of elements in the result
1644 * is stored there. In any case, the resulting array will be
1647 * For an empty array, @length will be set to 0 and a pointer to a
1648 * %NULL pointer will be returned.
1653 g_variant_dup_objv (GVariant *value,
1660 TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL);
1662 n = g_variant_n_children (value);
1663 strv = g_new (gchar *, n + 1);
1665 for (i = 0; i < n; i++)
1669 string = g_variant_get_child_value (value, i);
1670 strv[i] = g_variant_dup_string (string, NULL);
1671 g_variant_unref (string);
1683 * g_variant_new_bytestring:
1684 * @string: (array zero-terminated=1) (element-type guint8): a normal
1685 * nul-terminated string in no particular encoding
1686 * @returns: (transfer none): a floating reference to a new bytestring #GVariant instance
1688 * Creates an array-of-bytes #GVariant with the contents of @string.
1689 * This function is just like g_variant_new_string() except that the
1690 * string need not be valid utf8.
1692 * The nul terminator character at the end of the string is stored in
1698 g_variant_new_bytestring (const gchar *string)
1700 g_return_val_if_fail (string != NULL, NULL);
1702 return g_variant_new_from_trusted (G_VARIANT_TYPE_BYTESTRING,
1703 string, strlen (string) + 1);
1707 * g_variant_get_bytestring:
1708 * @value: an array-of-bytes #GVariant instance
1709 * @returns: (transfer none) (array zero-terminated=1) (element-type guint8):
1710 * the constant string
1712 * Returns the string value of a #GVariant instance with an
1713 * array-of-bytes type. The string has no particular encoding.
1715 * If the array does not end with a nul terminator character, the empty
1716 * string is returned. For this reason, you can always trust that a
1717 * non-%NULL nul-terminated string will be returned by this function.
1719 * If the array contains a nul terminator character somewhere other than
1720 * the last byte then the returned string is the string, up to the first
1721 * such nul character.
1723 * It is an error to call this function with a @value that is not an
1726 * The return value remains valid as long as @value exists.
1731 g_variant_get_bytestring (GVariant *value)
1733 const gchar *string;
1736 TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING, NULL);
1738 /* Won't be NULL since this is an array type */
1739 string = g_variant_get_data (value);
1740 size = g_variant_get_size (value);
1742 if (size && string[size - 1] == '\0')
1749 * g_variant_dup_bytestring:
1750 * @value: an array-of-bytes #GVariant instance
1751 * @length: (out) (allow-none) (default NULL): a pointer to a #gsize, to store
1752 * the length (not including the nul terminator)
1753 * @returns: (transfer full) (array zero-terminated=1 length=length)
1754 * (element-type guint8): a newly allocated string
1756 * Similar to g_variant_get_bytestring() except that instead of
1757 * returning a constant string, the string is duplicated.
1759 * The return value must be freed using g_free().
1764 g_variant_dup_bytestring (GVariant *value,
1767 const gchar *original = g_variant_get_bytestring (value);
1770 /* don't crash in case get_bytestring() had an assert failure */
1771 if (original == NULL)
1774 size = strlen (original);
1779 return g_memdup (original, size + 1);
1783 * g_variant_new_bytestring_array:
1784 * @strv: (array length=length): an array of strings
1785 * @length: the length of @strv, or -1
1786 * @returns: (transfer none): a new floating #GVariant instance
1788 * Constructs an array of bytestring #GVariant from the given array of
1791 * If @length is -1 then @strv is %NULL-terminated.
1796 g_variant_new_bytestring_array (const gchar * const *strv,
1802 g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1805 length = g_strv_length ((gchar **) strv);
1807 strings = g_new (GVariant *, length);
1808 for (i = 0; i < length; i++)
1809 strings[i] = g_variant_ref_sink (g_variant_new_bytestring (strv[i]));
1811 return g_variant_new_from_children (G_VARIANT_TYPE_BYTESTRING_ARRAY,
1812 strings, length, TRUE);
1816 * g_variant_get_bytestring_array:
1817 * @value: an array of array of bytes #GVariant ('aay')
1818 * @length: (out) (allow-none): the length of the result, or %NULL
1819 * @returns: (array length=length) (transfer container): an array of constant strings
1821 * Gets the contents of an array of array of bytes #GVariant. This call
1822 * makes a shallow copy; the return result should be released with
1823 * g_free(), but the individual strings must not be modified.
1825 * If @length is non-%NULL then the number of elements in the result is
1826 * stored there. In any case, the resulting array will be
1829 * For an empty array, @length will be set to 0 and a pointer to a
1830 * %NULL pointer will be returned.
1835 g_variant_get_bytestring_array (GVariant *value,
1842 TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL);
1844 g_variant_get_data (value);
1845 n = g_variant_n_children (value);
1846 strv = g_new (const gchar *, n + 1);
1848 for (i = 0; i < n; i++)
1852 string = g_variant_get_child_value (value, i);
1853 strv[i] = g_variant_get_bytestring (string);
1854 g_variant_unref (string);
1865 * g_variant_dup_bytestring_array:
1866 * @value: an array of array of bytes #GVariant ('aay')
1867 * @length: (out) (allow-none): the length of the result, or %NULL
1868 * @returns: (array length=length) (transfer full): an array of strings
1870 * Gets the contents of an array of array of bytes #GVariant. This call
1871 * makes a deep copy; the return result should be released with
1874 * If @length is non-%NULL then the number of elements in the result is
1875 * stored there. In any case, the resulting array will be
1878 * For an empty array, @length will be set to 0 and a pointer to a
1879 * %NULL pointer will be returned.
1884 g_variant_dup_bytestring_array (GVariant *value,
1891 TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL);
1893 g_variant_get_data (value);
1894 n = g_variant_n_children (value);
1895 strv = g_new (gchar *, n + 1);
1897 for (i = 0; i < n; i++)
1901 string = g_variant_get_child_value (value, i);
1902 strv[i] = g_variant_dup_bytestring (string, NULL);
1903 g_variant_unref (string);
1913 /* Type checking and querying {{{1 */
1915 * g_variant_get_type:
1916 * @value: a #GVariant
1917 * @returns: a #GVariantType
1919 * Determines the type of @value.
1921 * The return value is valid for the lifetime of @value and must not
1926 const GVariantType *
1927 g_variant_get_type (GVariant *value)
1929 GVariantTypeInfo *type_info;
1931 g_return_val_if_fail (value != NULL, NULL);
1933 type_info = g_variant_get_type_info (value);
1935 return (GVariantType *) g_variant_type_info_get_type_string (type_info);
1939 * g_variant_get_type_string:
1940 * @value: a #GVariant
1941 * @returns: the type string for the type of @value
1943 * Returns the type string of @value. Unlike the result of calling
1944 * g_variant_type_peek_string(), this string is nul-terminated. This
1945 * string belongs to #GVariant and must not be freed.
1950 g_variant_get_type_string (GVariant *value)
1952 GVariantTypeInfo *type_info;
1954 g_return_val_if_fail (value != NULL, NULL);
1956 type_info = g_variant_get_type_info (value);
1958 return g_variant_type_info_get_type_string (type_info);
1962 * g_variant_is_of_type:
1963 * @value: a #GVariant instance
1964 * @type: a #GVariantType
1965 * @returns: %TRUE if the type of @value matches @type
1967 * Checks if a value has a type matching the provided type.
1972 g_variant_is_of_type (GVariant *value,
1973 const GVariantType *type)
1975 return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
1979 * g_variant_is_container:
1980 * @value: a #GVariant instance
1981 * @returns: %TRUE if @value is a container
1983 * Checks if @value is a container.
1986 g_variant_is_container (GVariant *value)
1988 return g_variant_type_is_container (g_variant_get_type (value));
1993 * g_variant_classify:
1994 * @value: a #GVariant
1995 * @returns: the #GVariantClass of @value
1997 * Classifies @value according to its top-level type.
2003 * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
2004 * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
2005 * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
2006 * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
2007 * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
2008 * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
2009 * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
2010 * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
2011 * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
2012 * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating
2014 * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
2015 * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a D-Bus object path
2017 * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a D-Bus signature string.
2018 * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
2019 * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
2020 * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
2021 * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
2022 * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
2024 * The range of possible top-level types of #GVariant instances.
2029 g_variant_classify (GVariant *value)
2031 g_return_val_if_fail (value != NULL, 0);
2033 return *g_variant_get_type_string (value);
2036 /* Pretty printer {{{1 */
2037 /* This function is not introspectable because if @string is NULL,
2038 @returns is (transfer full), otherwise it is (transfer none), which
2039 is not supported by GObjectIntrospection */
2041 * g_variant_print_string: (skip)
2042 * @value: a #GVariant
2043 * @string: (allow-none) (default NULL): a #GString, or %NULL
2044 * @type_annotate: %TRUE if type information should be included in
2046 * @returns: a #GString containing the string
2048 * Behaves as g_variant_print(), but operates on a #GString.
2050 * If @string is non-%NULL then it is appended to and returned. Else,
2051 * a new empty #GString is allocated and it is returned.
2056 g_variant_print_string (GVariant *value,
2058 gboolean type_annotate)
2060 if G_UNLIKELY (string == NULL)
2061 string = g_string_new (NULL);
2063 switch (g_variant_classify (value))
2065 case G_VARIANT_CLASS_MAYBE:
2067 g_string_append_printf (string, "@%s ",
2068 g_variant_get_type_string (value));
2070 if (g_variant_n_children (value))
2072 gchar *printed_child;
2077 * Consider the case of the type "mmi". In this case we could
2078 * write "just just 4", but "4" alone is totally unambiguous,
2079 * so we try to drop "just" where possible.
2081 * We have to be careful not to always drop "just", though,
2082 * since "nothing" needs to be distinguishable from "just
2083 * nothing". The case where we need to ensure we keep the
2084 * "just" is actually exactly the case where we have a nested
2087 * Instead of searching for that nested Nothing, we just print
2088 * the contained value into a separate string and see if we
2089 * end up with "nothing" at the end of it. If so, we need to
2090 * add "just" at our level.
2092 element = g_variant_get_child_value (value, 0);
2093 printed_child = g_variant_print (element, FALSE);
2094 g_variant_unref (element);
2096 if (g_str_has_suffix (printed_child, "nothing"))
2097 g_string_append (string, "just ");
2098 g_string_append (string, printed_child);
2099 g_free (printed_child);
2102 g_string_append (string, "nothing");
2106 case G_VARIANT_CLASS_ARRAY:
2107 /* it's an array so the first character of the type string is 'a'
2109 * if the first two characters are 'ay' then it's a bytestring.
2110 * under certain conditions we print those as strings.
2112 if (g_variant_get_type_string (value)[1] == 'y')
2118 /* first determine if it is a byte string.
2119 * that's when there's a single nul character: at the end.
2121 str = g_variant_get_data (value);
2122 size = g_variant_get_size (value);
2124 for (i = 0; i < size; i++)
2128 /* first nul byte is the last byte -> it's a byte string. */
2131 gchar *escaped = g_strescape (str, NULL);
2133 /* use double quotes only if a ' is in the string */
2134 if (strchr (str, '\''))
2135 g_string_append_printf (string, "b\"%s\"", escaped);
2137 g_string_append_printf (string, "b'%s'", escaped);
2144 /* fall through and handle normally... */;
2148 * if the first two characters are 'a{' then it's an array of
2149 * dictionary entries (ie: a dictionary) so we print that
2152 if (g_variant_get_type_string (value)[1] == '{')
2155 const gchar *comma = "";
2158 if ((n = g_variant_n_children (value)) == 0)
2161 g_string_append_printf (string, "@%s ",
2162 g_variant_get_type_string (value));
2163 g_string_append (string, "{}");
2167 g_string_append_c (string, '{');
2168 for (i = 0; i < n; i++)
2170 GVariant *entry, *key, *val;
2172 g_string_append (string, comma);
2175 entry = g_variant_get_child_value (value, i);
2176 key = g_variant_get_child_value (entry, 0);
2177 val = g_variant_get_child_value (entry, 1);
2178 g_variant_unref (entry);
2180 g_variant_print_string (key, string, type_annotate);
2181 g_variant_unref (key);
2182 g_string_append (string, ": ");
2183 g_variant_print_string (val, string, type_annotate);
2184 g_variant_unref (val);
2185 type_annotate = FALSE;
2187 g_string_append_c (string, '}');
2190 /* normal (non-dictionary) array */
2192 const gchar *comma = "";
2195 if ((n = g_variant_n_children (value)) == 0)
2198 g_string_append_printf (string, "@%s ",
2199 g_variant_get_type_string (value));
2200 g_string_append (string, "[]");
2204 g_string_append_c (string, '[');
2205 for (i = 0; i < n; i++)
2209 g_string_append (string, comma);
2212 element = g_variant_get_child_value (value, i);
2214 g_variant_print_string (element, string, type_annotate);
2215 g_variant_unref (element);
2216 type_annotate = FALSE;
2218 g_string_append_c (string, ']');
2223 case G_VARIANT_CLASS_TUPLE:
2227 n = g_variant_n_children (value);
2229 g_string_append_c (string, '(');
2230 for (i = 0; i < n; i++)
2234 element = g_variant_get_child_value (value, i);
2235 g_variant_print_string (element, string, type_annotate);
2236 g_string_append (string, ", ");
2237 g_variant_unref (element);
2240 /* for >1 item: remove final ", "
2241 * for 1 item: remove final " ", but leave the ","
2242 * for 0 items: there is only "(", so remove nothing
2244 g_string_truncate (string, string->len - (n > 0) - (n > 1));
2245 g_string_append_c (string, ')');
2249 case G_VARIANT_CLASS_DICT_ENTRY:
2253 g_string_append_c (string, '{');
2255 element = g_variant_get_child_value (value, 0);
2256 g_variant_print_string (element, string, type_annotate);
2257 g_variant_unref (element);
2259 g_string_append (string, ", ");
2261 element = g_variant_get_child_value (value, 1);
2262 g_variant_print_string (element, string, type_annotate);
2263 g_variant_unref (element);
2265 g_string_append_c (string, '}');
2269 case G_VARIANT_CLASS_VARIANT:
2271 GVariant *child = g_variant_get_variant (value);
2273 /* Always annotate types in nested variants, because they are
2274 * (by nature) of variable type.
2276 g_string_append_c (string, '<');
2277 g_variant_print_string (child, string, TRUE);
2278 g_string_append_c (string, '>');
2280 g_variant_unref (child);
2284 case G_VARIANT_CLASS_BOOLEAN:
2285 if (g_variant_get_boolean (value))
2286 g_string_append (string, "true");
2288 g_string_append (string, "false");
2291 case G_VARIANT_CLASS_STRING:
2293 const gchar *str = g_variant_get_string (value, NULL);
2294 gunichar quote = strchr (str, '\'') ? '"' : '\'';
2296 g_string_append_c (string, quote);
2300 gunichar c = g_utf8_get_char (str);
2302 if (c == quote || c == '\\')
2303 g_string_append_c (string, '\\');
2305 if (g_unichar_isprint (c))
2306 g_string_append_unichar (string, c);
2310 g_string_append_c (string, '\\');
2315 g_string_append_c (string, 'a');
2319 g_string_append_c (string, 'b');
2323 g_string_append_c (string, 'f');
2327 g_string_append_c (string, 'n');
2331 g_string_append_c (string, 'r');
2335 g_string_append_c (string, 't');
2339 g_string_append_c (string, 'v');
2343 g_string_append_printf (string, "u%04x", c);
2347 g_string_append_printf (string, "U%08x", c);
2350 str = g_utf8_next_char (str);
2353 g_string_append_c (string, quote);
2357 case G_VARIANT_CLASS_BYTE:
2359 g_string_append (string, "byte ");
2360 g_string_append_printf (string, "0x%02x",
2361 g_variant_get_byte (value));
2364 case G_VARIANT_CLASS_INT16:
2366 g_string_append (string, "int16 ");
2367 g_string_append_printf (string, "%"G_GINT16_FORMAT,
2368 g_variant_get_int16 (value));
2371 case G_VARIANT_CLASS_UINT16:
2373 g_string_append (string, "uint16 ");
2374 g_string_append_printf (string, "%"G_GUINT16_FORMAT,
2375 g_variant_get_uint16 (value));
2378 case G_VARIANT_CLASS_INT32:
2379 /* Never annotate this type because it is the default for numbers
2380 * (and this is a *pretty* printer)
2382 g_string_append_printf (string, "%"G_GINT32_FORMAT,
2383 g_variant_get_int32 (value));
2386 case G_VARIANT_CLASS_HANDLE:
2388 g_string_append (string, "handle ");
2389 g_string_append_printf (string, "%"G_GINT32_FORMAT,
2390 g_variant_get_handle (value));
2393 case G_VARIANT_CLASS_UINT32:
2395 g_string_append (string, "uint32 ");
2396 g_string_append_printf (string, "%"G_GUINT32_FORMAT,
2397 g_variant_get_uint32 (value));
2400 case G_VARIANT_CLASS_INT64:
2402 g_string_append (string, "int64 ");
2403 g_string_append_printf (string, "%"G_GINT64_FORMAT,
2404 g_variant_get_int64 (value));
2407 case G_VARIANT_CLASS_UINT64:
2409 g_string_append (string, "uint64 ");
2410 g_string_append_printf (string, "%"G_GUINT64_FORMAT,
2411 g_variant_get_uint64 (value));
2414 case G_VARIANT_CLASS_DOUBLE:
2419 g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
2421 for (i = 0; buffer[i]; i++)
2422 if (buffer[i] == '.' || buffer[i] == 'e' ||
2423 buffer[i] == 'n' || buffer[i] == 'N')
2426 /* if there is no '.' or 'e' in the float then add one */
2427 if (buffer[i] == '\0')
2434 g_string_append (string, buffer);
2438 case G_VARIANT_CLASS_OBJECT_PATH:
2440 g_string_append (string, "objectpath ");
2441 g_string_append_printf (string, "\'%s\'",
2442 g_variant_get_string (value, NULL));
2445 case G_VARIANT_CLASS_SIGNATURE:
2447 g_string_append (string, "signature ");
2448 g_string_append_printf (string, "\'%s\'",
2449 g_variant_get_string (value, NULL));
2453 g_assert_not_reached ();
2461 * @value: a #GVariant
2462 * @type_annotate: %TRUE if type information should be included in
2464 * @returns: (transfer full): a newly-allocated string holding the result.
2466 * Pretty-prints @value in the format understood by g_variant_parse().
2468 * The format is described <link linkend='gvariant-text'>here</link>.
2470 * If @type_annotate is %TRUE, then type information is included in
2474 g_variant_print (GVariant *value,
2475 gboolean type_annotate)
2477 return g_string_free (g_variant_print_string (value, NULL, type_annotate),
2481 /* Hash, Equal, Compare {{{1 */
2484 * @value: (type GVariant): a basic #GVariant value as a #gconstpointer
2485 * @returns: a hash value corresponding to @value
2487 * Generates a hash value for a #GVariant instance.
2489 * The output of this function is guaranteed to be the same for a given
2490 * value only per-process. It may change between different processor
2491 * architectures or even different versions of GLib. Do not use this
2492 * function as a basis for building protocols or file formats.
2494 * The type of @value is #gconstpointer only to allow use of this
2495 * function with #GHashTable. @value must be a #GVariant.
2500 g_variant_hash (gconstpointer value_)
2502 GVariant *value = (GVariant *) value_;
2504 switch (g_variant_classify (value))
2506 case G_VARIANT_CLASS_STRING:
2507 case G_VARIANT_CLASS_OBJECT_PATH:
2508 case G_VARIANT_CLASS_SIGNATURE:
2509 return g_str_hash (g_variant_get_string (value, NULL));
2511 case G_VARIANT_CLASS_BOOLEAN:
2512 /* this is a very odd thing to hash... */
2513 return g_variant_get_boolean (value);
2515 case G_VARIANT_CLASS_BYTE:
2516 return g_variant_get_byte (value);
2518 case G_VARIANT_CLASS_INT16:
2519 case G_VARIANT_CLASS_UINT16:
2523 ptr = g_variant_get_data (value);
2531 case G_VARIANT_CLASS_INT32:
2532 case G_VARIANT_CLASS_UINT32:
2533 case G_VARIANT_CLASS_HANDLE:
2537 ptr = g_variant_get_data (value);
2545 case G_VARIANT_CLASS_INT64:
2546 case G_VARIANT_CLASS_UINT64:
2547 case G_VARIANT_CLASS_DOUBLE:
2548 /* need a separate case for these guys because otherwise
2549 * performance could be quite bad on big endian systems
2554 ptr = g_variant_get_data (value);
2557 return ptr[0] + ptr[1];
2563 g_return_val_if_fail (!g_variant_is_container (value), 0);
2564 g_assert_not_reached ();
2570 * @one: (type GVariant): a #GVariant instance
2571 * @two: (type GVariant): a #GVariant instance
2572 * @returns: %TRUE if @one and @two are equal
2574 * Checks if @one and @two have the same type and value.
2576 * The types of @one and @two are #gconstpointer only to allow use of
2577 * this function with #GHashTable. They must each be a #GVariant.
2582 g_variant_equal (gconstpointer one,
2587 g_return_val_if_fail (one != NULL && two != NULL, FALSE);
2589 if (g_variant_get_type_info ((GVariant *) one) !=
2590 g_variant_get_type_info ((GVariant *) two))
2593 /* if both values are trusted to be in their canonical serialised form
2594 * then a simple memcmp() of their serialised data will answer the
2597 * if not, then this might generate a false negative (since it is
2598 * possible for two different byte sequences to represent the same
2599 * value). for now we solve this by pretty-printing both values and
2600 * comparing the result.
2602 if (g_variant_is_trusted ((GVariant *) one) &&
2603 g_variant_is_trusted ((GVariant *) two))
2605 gconstpointer data_one, data_two;
2606 gsize size_one, size_two;
2608 size_one = g_variant_get_size ((GVariant *) one);
2609 size_two = g_variant_get_size ((GVariant *) two);
2611 if (size_one != size_two)
2614 data_one = g_variant_get_data ((GVariant *) one);
2615 data_two = g_variant_get_data ((GVariant *) two);
2617 equal = memcmp (data_one, data_two, size_one) == 0;
2621 gchar *strone, *strtwo;
2623 strone = g_variant_print ((GVariant *) one, FALSE);
2624 strtwo = g_variant_print ((GVariant *) two, FALSE);
2625 equal = strcmp (strone, strtwo) == 0;
2634 * g_variant_compare:
2635 * @one: (type GVariant): a basic-typed #GVariant instance
2636 * @two: (type GVariant): a #GVariant instance of the same type
2637 * @returns: negative value if a < b;
2639 * positive value if a > b.
2641 * Compares @one and @two.
2643 * The types of @one and @two are #gconstpointer only to allow use of
2644 * this function with #GTree, #GPtrArray, etc. They must each be a
2647 * Comparison is only defined for basic types (ie: booleans, numbers,
2648 * strings). For booleans, %FALSE is less than %TRUE. Numbers are
2649 * ordered in the usual way. Strings are in ASCII lexographical order.
2651 * It is a programmer error to attempt to compare container values or
2652 * two values that have types that are not exactly equal. For example,
2653 * you cannot compare a 32-bit signed integer with a 32-bit unsigned
2654 * integer. Also note that this function is not particularly
2655 * well-behaved when it comes to comparison of doubles; in particular,
2656 * the handling of incomparable values (ie: NaN) is undefined.
2658 * If you only require an equality comparison, g_variant_equal() is more
2664 g_variant_compare (gconstpointer one,
2667 GVariant *a = (GVariant *) one;
2668 GVariant *b = (GVariant *) two;
2670 g_return_val_if_fail (g_variant_classify (a) == g_variant_classify (b), 0);
2672 switch (g_variant_classify (a))
2674 case G_VARIANT_CLASS_BYTE:
2675 return ((gint) g_variant_get_byte (a)) -
2676 ((gint) g_variant_get_byte (b));
2678 case G_VARIANT_CLASS_INT16:
2679 return ((gint) g_variant_get_int16 (a)) -
2680 ((gint) g_variant_get_int16 (b));
2682 case G_VARIANT_CLASS_UINT16:
2683 return ((gint) g_variant_get_uint16 (a)) -
2684 ((gint) g_variant_get_uint16 (b));
2686 case G_VARIANT_CLASS_INT32:
2688 gint32 a_val = g_variant_get_int32 (a);
2689 gint32 b_val = g_variant_get_int32 (b);
2691 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2694 case G_VARIANT_CLASS_UINT32:
2696 guint32 a_val = g_variant_get_uint32 (a);
2697 guint32 b_val = g_variant_get_uint32 (b);
2699 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2702 case G_VARIANT_CLASS_INT64:
2704 gint64 a_val = g_variant_get_int64 (a);
2705 gint64 b_val = g_variant_get_int64 (b);
2707 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2710 case G_VARIANT_CLASS_UINT64:
2712 guint64 a_val = g_variant_get_uint64 (a);
2713 guint64 b_val = g_variant_get_uint64 (b);
2715 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2718 case G_VARIANT_CLASS_DOUBLE:
2720 gdouble a_val = g_variant_get_double (a);
2721 gdouble b_val = g_variant_get_double (b);
2723 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2726 case G_VARIANT_CLASS_STRING:
2727 case G_VARIANT_CLASS_OBJECT_PATH:
2728 case G_VARIANT_CLASS_SIGNATURE:
2729 return strcmp (g_variant_get_string (a, NULL),
2730 g_variant_get_string (b, NULL));
2733 g_return_val_if_fail (!g_variant_is_container (a), 0);
2734 g_assert_not_reached ();
2738 /* GVariantIter {{{1 */
2740 * GVariantIter: (skip)
2742 * #GVariantIter is an opaque data structure and can only be accessed
2743 * using the following functions.
2750 const gchar *loop_format;
2756 G_STATIC_ASSERT (sizeof (struct stack_iter) <= sizeof (GVariantIter));
2760 struct stack_iter iter;
2762 GVariant *value_ref;
2766 #define GVSI(i) ((struct stack_iter *) (i))
2767 #define GVHI(i) ((struct heap_iter *) (i))
2768 #define GVSI_MAGIC ((gsize) 3579507750u)
2769 #define GVHI_MAGIC ((gsize) 1450270775u)
2770 #define is_valid_iter(i) (i != NULL && \
2771 GVSI(i)->magic == GVSI_MAGIC)
2772 #define is_valid_heap_iter(i) (GVHI(i)->magic == GVHI_MAGIC && \
2776 * g_variant_iter_new:
2777 * @value: a container #GVariant
2778 * @returns: (transfer full): a new heap-allocated #GVariantIter
2780 * Creates a heap-allocated #GVariantIter for iterating over the items
2783 * Use g_variant_iter_free() to free the return value when you no longer
2786 * A reference is taken to @value and will be released only when
2787 * g_variant_iter_free() is called.
2792 g_variant_iter_new (GVariant *value)
2796 iter = (GVariantIter *) g_slice_new (struct heap_iter);
2797 GVHI(iter)->value_ref = g_variant_ref (value);
2798 GVHI(iter)->magic = GVHI_MAGIC;
2800 g_variant_iter_init (iter, value);
2806 * g_variant_iter_init: (skip)
2807 * @iter: a pointer to a #GVariantIter
2808 * @value: a container #GVariant
2809 * @returns: the number of items in @value
2811 * Initialises (without allocating) a #GVariantIter. @iter may be
2812 * completely uninitialised prior to this call; its old value is
2815 * The iterator remains valid for as long as @value exists, and need not
2816 * be freed in any way.
2821 g_variant_iter_init (GVariantIter *iter,
2824 GVSI(iter)->magic = GVSI_MAGIC;
2825 GVSI(iter)->value = value;
2826 GVSI(iter)->n = g_variant_n_children (value);
2828 GVSI(iter)->loop_format = NULL;
2830 return GVSI(iter)->n;
2834 * g_variant_iter_copy:
2835 * @iter: a #GVariantIter
2836 * @returns: (transfer full): a new heap-allocated #GVariantIter
2838 * Creates a new heap-allocated #GVariantIter to iterate over the
2839 * container that was being iterated over by @iter. Iteration begins on
2840 * the new iterator from the current position of the old iterator but
2841 * the two copies are independent past that point.
2843 * Use g_variant_iter_free() to free the return value when you no longer
2846 * A reference is taken to the container that @iter is iterating over
2847 * and will be releated only when g_variant_iter_free() is called.
2852 g_variant_iter_copy (GVariantIter *iter)
2856 g_return_val_if_fail (is_valid_iter (iter), 0);
2858 copy = g_variant_iter_new (GVSI(iter)->value);
2859 GVSI(copy)->i = GVSI(iter)->i;
2865 * g_variant_iter_n_children:
2866 * @iter: a #GVariantIter
2867 * @returns: the number of children in the container
2869 * Queries the number of child items in the container that we are
2870 * iterating over. This is the total number of items -- not the number
2871 * of items remaining.
2873 * This function might be useful for preallocation of arrays.
2878 g_variant_iter_n_children (GVariantIter *iter)
2880 g_return_val_if_fail (is_valid_iter (iter), 0);
2882 return GVSI(iter)->n;
2886 * g_variant_iter_free:
2887 * @iter: (transfer full): a heap-allocated #GVariantIter
2889 * Frees a heap-allocated #GVariantIter. Only call this function on
2890 * iterators that were returned by g_variant_iter_new() or
2891 * g_variant_iter_copy().
2896 g_variant_iter_free (GVariantIter *iter)
2898 g_return_if_fail (is_valid_heap_iter (iter));
2900 g_variant_unref (GVHI(iter)->value_ref);
2901 GVHI(iter)->magic = 0;
2903 g_slice_free (struct heap_iter, GVHI(iter));
2907 * g_variant_iter_next_value:
2908 * @iter: a #GVariantIter
2909 * @returns: (allow-none) (transfer full): a #GVariant, or %NULL
2911 * Gets the next item in the container. If no more items remain then
2912 * %NULL is returned.
2914 * Use g_variant_unref() to drop your reference on the return value when
2915 * you no longer need it.
2918 * <title>Iterating with g_variant_iter_next_value()</title>
2920 * /<!-- -->* recursively iterate a container *<!-- -->/
2922 * iterate_container_recursive (GVariant *container)
2924 * GVariantIter iter;
2927 * g_variant_iter_init (&iter, container);
2928 * while ((child = g_variant_iter_next_value (&iter)))
2930 * g_print ("type '%s'\n", g_variant_get_type_string (child));
2932 * if (g_variant_is_container (child))
2933 * iterate_container_recursive (child);
2935 * g_variant_unref (child);
2944 g_variant_iter_next_value (GVariantIter *iter)
2946 g_return_val_if_fail (is_valid_iter (iter), FALSE);
2948 if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n)
2950 g_critical ("g_variant_iter_next_value: must not be called again "
2951 "after NULL has already been returned.");
2957 if (GVSI(iter)->i < GVSI(iter)->n)
2958 return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i);
2963 /* GVariantBuilder {{{1 */
2967 * A utility type for constructing container-type #GVariant instances.
2969 * This is an opaque structure and may only be accessed using the
2970 * following functions.
2972 * #GVariantBuilder is not threadsafe in any way. Do not attempt to
2973 * access it from more than one thread.
2976 struct stack_builder
2978 GVariantBuilder *parent;
2981 /* type constraint explicitly specified by 'type'.
2982 * for tuple types, this moves along as we add more items.
2984 const GVariantType *expected_type;
2986 /* type constraint implied by previous array item.
2988 const GVariantType *prev_item_type;
2990 /* constraints on the number of children. max = -1 for unlimited. */
2994 /* dynamically-growing pointer array */
2995 GVariant **children;
2996 gsize allocated_children;
2999 /* set to '1' if all items in the container will have the same type
3000 * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
3002 guint uniform_item_types : 1;
3004 /* set to '1' initially and changed to '0' if an untrusted value is
3012 G_STATIC_ASSERT (sizeof (struct stack_builder) <= sizeof (GVariantBuilder));
3016 GVariantBuilder builder;
3022 #define GVSB(b) ((struct stack_builder *) (b))
3023 #define GVHB(b) ((struct heap_builder *) (b))
3024 #define GVSB_MAGIC ((gsize) 1033660112u)
3025 #define GVHB_MAGIC ((gsize) 3087242682u)
3026 #define is_valid_builder(b) (b != NULL && \
3027 GVSB(b)->magic == GVSB_MAGIC)
3028 #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
3031 * g_variant_builder_new:
3032 * @type: a container type
3033 * @returns: (transfer full): a #GVariantBuilder
3035 * Allocates and initialises a new #GVariantBuilder.
3037 * You should call g_variant_builder_unref() on the return value when it
3038 * is no longer needed. The memory will not be automatically freed by
3041 * In most cases it is easier to place a #GVariantBuilder directly on
3042 * the stack of the calling function and initialise it with
3043 * g_variant_builder_init().
3048 g_variant_builder_new (const GVariantType *type)
3050 GVariantBuilder *builder;
3052 builder = (GVariantBuilder *) g_slice_new (struct heap_builder);
3053 g_variant_builder_init (builder, type);
3054 GVHB(builder)->magic = GVHB_MAGIC;
3055 GVHB(builder)->ref_count = 1;
3061 * g_variant_builder_unref:
3062 * @builder: (transfer full): a #GVariantBuilder allocated by g_variant_builder_new()
3064 * Decreases the reference count on @builder.
3066 * In the event that there are no more references, releases all memory
3067 * associated with the #GVariantBuilder.
3069 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3070 * things will happen.
3075 g_variant_builder_unref (GVariantBuilder *builder)
3077 g_return_if_fail (is_valid_heap_builder (builder));
3079 if (--GVHB(builder)->ref_count)
3082 g_variant_builder_clear (builder);
3083 GVHB(builder)->magic = 0;
3085 g_slice_free (struct heap_builder, GVHB(builder));
3089 * g_variant_builder_ref:
3090 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
3091 * @returns: (transfer full): a new reference to @builder
3093 * Increases the reference count on @builder.
3095 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3096 * things will happen.
3101 g_variant_builder_ref (GVariantBuilder *builder)
3103 g_return_val_if_fail (is_valid_heap_builder (builder), NULL);
3105 GVHB(builder)->ref_count++;
3111 * g_variant_builder_clear: (skip)
3112 * @builder: a #GVariantBuilder
3114 * Releases all memory associated with a #GVariantBuilder without
3115 * freeing the #GVariantBuilder structure itself.
3117 * It typically only makes sense to do this on a stack-allocated
3118 * #GVariantBuilder if you want to abort building the value part-way
3119 * through. This function need not be called if you call
3120 * g_variant_builder_end() and it also doesn't need to be called on
3121 * builders allocated with g_variant_builder_new (see
3122 * g_variant_builder_unref() for that).
3124 * This function leaves the #GVariantBuilder structure set to all-zeros.
3125 * It is valid to call this function on either an initialised
3126 * #GVariantBuilder or one that is set to all-zeros but it is not valid
3127 * to call this function on uninitialised memory.
3132 g_variant_builder_clear (GVariantBuilder *builder)
3136 if (GVSB(builder)->magic == 0)
3137 /* all-zeros case */
3140 g_return_if_fail (is_valid_builder (builder));
3142 g_variant_type_free (GVSB(builder)->type);
3144 for (i = 0; i < GVSB(builder)->offset; i++)
3145 g_variant_unref (GVSB(builder)->children[i]);
3147 g_free (GVSB(builder)->children);
3149 if (GVSB(builder)->parent)
3151 g_variant_builder_clear (GVSB(builder)->parent);
3152 g_slice_free (GVariantBuilder, GVSB(builder)->parent);
3155 memset (builder, 0, sizeof (GVariantBuilder));
3159 * g_variant_builder_init: (skip)
3160 * @builder: a #GVariantBuilder
3161 * @type: a container type
3163 * Initialises a #GVariantBuilder structure.
3165 * @type must be non-%NULL. It specifies the type of container to
3166 * construct. It can be an indefinite type such as
3167 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
3168 * Maybe, array, tuple, dictionary entry and variant-typed values may be
3171 * After the builder is initialised, values are added using
3172 * g_variant_builder_add_value() or g_variant_builder_add().
3174 * After all the child values are added, g_variant_builder_end() frees
3175 * the memory associated with the builder and returns the #GVariant that
3178 * This function completely ignores the previous contents of @builder.
3179 * On one hand this means that it is valid to pass in completely
3180 * uninitialised memory. On the other hand, this means that if you are
3181 * initialising over top of an existing #GVariantBuilder you need to
3182 * first call g_variant_builder_clear() in order to avoid leaking
3185 * You must not call g_variant_builder_ref() or
3186 * g_variant_builder_unref() on a #GVariantBuilder that was initialised
3187 * with this function. If you ever pass a reference to a
3188 * #GVariantBuilder outside of the control of your own code then you
3189 * should assume that the person receiving that reference may try to use
3190 * reference counting; you should use g_variant_builder_new() instead of
3196 g_variant_builder_init (GVariantBuilder *builder,
3197 const GVariantType *type)
3199 g_return_if_fail (type != NULL);
3200 g_return_if_fail (g_variant_type_is_container (type));
3202 memset (builder, 0, sizeof (GVariantBuilder));
3204 GVSB(builder)->type = g_variant_type_copy (type);
3205 GVSB(builder)->magic = GVSB_MAGIC;
3206 GVSB(builder)->trusted = TRUE;
3208 switch (*(const gchar *) type)
3210 case G_VARIANT_CLASS_VARIANT:
3211 GVSB(builder)->uniform_item_types = TRUE;
3212 GVSB(builder)->allocated_children = 1;
3213 GVSB(builder)->expected_type = NULL;
3214 GVSB(builder)->min_items = 1;
3215 GVSB(builder)->max_items = 1;
3218 case G_VARIANT_CLASS_ARRAY:
3219 GVSB(builder)->uniform_item_types = TRUE;
3220 GVSB(builder)->allocated_children = 8;
3221 GVSB(builder)->expected_type =
3222 g_variant_type_element (GVSB(builder)->type);
3223 GVSB(builder)->min_items = 0;
3224 GVSB(builder)->max_items = -1;
3227 case G_VARIANT_CLASS_MAYBE:
3228 GVSB(builder)->uniform_item_types = TRUE;
3229 GVSB(builder)->allocated_children = 1;
3230 GVSB(builder)->expected_type =
3231 g_variant_type_element (GVSB(builder)->type);
3232 GVSB(builder)->min_items = 0;
3233 GVSB(builder)->max_items = 1;
3236 case G_VARIANT_CLASS_DICT_ENTRY:
3237 GVSB(builder)->uniform_item_types = FALSE;
3238 GVSB(builder)->allocated_children = 2;
3239 GVSB(builder)->expected_type =
3240 g_variant_type_key (GVSB(builder)->type);
3241 GVSB(builder)->min_items = 2;
3242 GVSB(builder)->max_items = 2;
3245 case 'r': /* G_VARIANT_TYPE_TUPLE was given */
3246 GVSB(builder)->uniform_item_types = FALSE;
3247 GVSB(builder)->allocated_children = 8;
3248 GVSB(builder)->expected_type = NULL;
3249 GVSB(builder)->min_items = 0;
3250 GVSB(builder)->max_items = -1;
3253 case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */
3254 GVSB(builder)->allocated_children = g_variant_type_n_items (type);
3255 GVSB(builder)->expected_type =
3256 g_variant_type_first (GVSB(builder)->type);
3257 GVSB(builder)->min_items = GVSB(builder)->allocated_children;
3258 GVSB(builder)->max_items = GVSB(builder)->allocated_children;
3259 GVSB(builder)->uniform_item_types = FALSE;
3263 g_assert_not_reached ();
3266 GVSB(builder)->children = g_new (GVariant *,
3267 GVSB(builder)->allocated_children);
3271 g_variant_builder_make_room (struct stack_builder *builder)
3273 if (builder->offset == builder->allocated_children)
3275 builder->allocated_children *= 2;
3276 builder->children = g_renew (GVariant *, builder->children,
3277 builder->allocated_children);
3282 * g_variant_builder_add_value:
3283 * @builder: a #GVariantBuilder
3284 * @value: a #GVariant
3286 * Adds @value to @builder.
3288 * It is an error to call this function in any way that would create an
3289 * inconsistent value to be constructed. Some examples of this are
3290 * putting different types of items into an array, putting the wrong
3291 * types or number of items in a tuple, putting more than one value into
3294 * If @value is a floating reference (see g_variant_ref_sink()),
3295 * the @builder instance takes ownership of @value.
3300 g_variant_builder_add_value (GVariantBuilder *builder,
3303 g_return_if_fail (is_valid_builder (builder));
3304 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
3305 g_return_if_fail (!GVSB(builder)->expected_type ||
3306 g_variant_is_of_type (value,
3307 GVSB(builder)->expected_type));
3308 g_return_if_fail (!GVSB(builder)->prev_item_type ||
3309 g_variant_is_of_type (value,
3310 GVSB(builder)->prev_item_type));
3312 GVSB(builder)->trusted &= g_variant_is_trusted (value);
3314 if (!GVSB(builder)->uniform_item_types)
3316 /* advance our expected type pointers */
3317 if (GVSB(builder)->expected_type)
3318 GVSB(builder)->expected_type =
3319 g_variant_type_next (GVSB(builder)->expected_type);
3321 if (GVSB(builder)->prev_item_type)
3322 GVSB(builder)->prev_item_type =
3323 g_variant_type_next (GVSB(builder)->prev_item_type);
3326 GVSB(builder)->prev_item_type = g_variant_get_type (value);
3328 g_variant_builder_make_room (GVSB(builder));
3330 GVSB(builder)->children[GVSB(builder)->offset++] =
3331 g_variant_ref_sink (value);
3335 * g_variant_builder_open:
3336 * @builder: a #GVariantBuilder
3337 * @type: a #GVariantType
3339 * Opens a subcontainer inside the given @builder. When done adding
3340 * items to the subcontainer, g_variant_builder_close() must be called.
3342 * It is an error to call this function in any way that would cause an
3343 * inconsistent value to be constructed (ie: adding too many values or
3344 * a value of an incorrect type).
3349 g_variant_builder_open (GVariantBuilder *builder,
3350 const GVariantType *type)
3352 GVariantBuilder *parent;
3354 g_return_if_fail (is_valid_builder (builder));
3355 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
3356 g_return_if_fail (!GVSB(builder)->expected_type ||
3357 g_variant_type_is_subtype_of (type,
3358 GVSB(builder)->expected_type));
3359 g_return_if_fail (!GVSB(builder)->prev_item_type ||
3360 g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type,
3363 parent = g_slice_dup (GVariantBuilder, builder);
3364 g_variant_builder_init (builder, type);
3365 GVSB(builder)->parent = parent;
3367 /* push the prev_item_type down into the subcontainer */
3368 if (GVSB(parent)->prev_item_type)
3370 if (!GVSB(builder)->uniform_item_types)
3371 /* tuples and dict entries */
3372 GVSB(builder)->prev_item_type =
3373 g_variant_type_first (GVSB(parent)->prev_item_type);
3375 else if (!g_variant_type_is_variant (GVSB(builder)->type))
3376 /* maybes and arrays */
3377 GVSB(builder)->prev_item_type =
3378 g_variant_type_element (GVSB(parent)->prev_item_type);
3383 * g_variant_builder_close:
3384 * @builder: a #GVariantBuilder
3386 * Closes the subcontainer inside the given @builder that was opened by
3387 * the most recent call to g_variant_builder_open().
3389 * It is an error to call this function in any way that would create an
3390 * inconsistent value to be constructed (ie: too few values added to the
3396 g_variant_builder_close (GVariantBuilder *builder)
3398 GVariantBuilder *parent;
3400 g_return_if_fail (is_valid_builder (builder));
3401 g_return_if_fail (GVSB(builder)->parent != NULL);
3403 parent = GVSB(builder)->parent;
3404 GVSB(builder)->parent = NULL;
3406 g_variant_builder_add_value (parent, g_variant_builder_end (builder));
3409 g_slice_free (GVariantBuilder, parent);
3413 * g_variant_make_maybe_type:
3414 * @element: a #GVariant
3416 * Return the type of a maybe containing @element.
3418 static GVariantType *
3419 g_variant_make_maybe_type (GVariant *element)
3421 return g_variant_type_new_maybe (g_variant_get_type (element));
3425 * g_variant_make_array_type:
3426 * @element: a #GVariant
3428 * Return the type of an array containing @element.
3430 static GVariantType *
3431 g_variant_make_array_type (GVariant *element)
3433 return g_variant_type_new_array (g_variant_get_type (element));
3437 * g_variant_builder_end:
3438 * @builder: a #GVariantBuilder
3439 * @returns: (transfer none): a new, floating, #GVariant
3441 * Ends the builder process and returns the constructed value.
3443 * It is not permissible to use @builder in any way after this call
3444 * except for reference counting operations (in the case of a
3445 * heap-allocated #GVariantBuilder) or by reinitialising it with
3446 * g_variant_builder_init() (in the case of stack-allocated).
3448 * It is an error to call this function in any way that would create an
3449 * inconsistent value to be constructed (ie: insufficient number of
3450 * items added to a container with a specific number of children
3451 * required). It is also an error to call this function if the builder
3452 * was created with an indefinite array or maybe type and no children
3453 * have been added; in this case it is impossible to infer the type of
3459 g_variant_builder_end (GVariantBuilder *builder)
3461 GVariantType *my_type;
3464 g_return_val_if_fail (is_valid_builder (builder), NULL);
3465 g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items,
3467 g_return_val_if_fail (!GVSB(builder)->uniform_item_types ||
3468 GVSB(builder)->prev_item_type != NULL ||
3469 g_variant_type_is_definite (GVSB(builder)->type),
3472 if (g_variant_type_is_definite (GVSB(builder)->type))
3473 my_type = g_variant_type_copy (GVSB(builder)->type);
3475 else if (g_variant_type_is_maybe (GVSB(builder)->type))
3476 my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]);
3478 else if (g_variant_type_is_array (GVSB(builder)->type))
3479 my_type = g_variant_make_array_type (GVSB(builder)->children[0]);
3481 else if (g_variant_type_is_tuple (GVSB(builder)->type))
3482 my_type = g_variant_make_tuple_type (GVSB(builder)->children,
3483 GVSB(builder)->offset);
3485 else if (g_variant_type_is_dict_entry (GVSB(builder)->type))
3486 my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0],
3487 GVSB(builder)->children[1]);
3489 g_assert_not_reached ();
3491 value = g_variant_new_from_children (my_type,
3492 g_renew (GVariant *,
3493 GVSB(builder)->children,
3494 GVSB(builder)->offset),
3495 GVSB(builder)->offset,
3496 GVSB(builder)->trusted);
3497 GVSB(builder)->children = NULL;
3498 GVSB(builder)->offset = 0;
3500 g_variant_builder_clear (builder);
3501 g_variant_type_free (my_type);
3506 /* Format strings {{{1 */
3508 * g_variant_format_string_scan:
3509 * @string: a string that may be prefixed with a format string
3510 * @limit: (allow-none) (default NULL): a pointer to the end of @string,
3512 * @endptr: (allow-none) (default NULL): location to store the end pointer,
3514 * @returns: %TRUE if there was a valid format string
3516 * Checks the string pointed to by @string for starting with a properly
3517 * formed #GVariant varargs format string. If no valid format string is
3518 * found then %FALSE is returned.
3520 * If @string does start with a valid format string then %TRUE is
3521 * returned. If @endptr is non-%NULL then it is updated to point to the
3522 * first character after the format string.
3524 * If @limit is non-%NULL then @limit (and any charater after it) will
3525 * not be accessed and the effect is otherwise equivalent to if the
3526 * character at @limit were nul.
3528 * See the section on <link linkend='gvariant-format-strings'>GVariant
3529 * Format Strings</link>.
3534 g_variant_format_string_scan (const gchar *string,
3536 const gchar **endptr)
3538 #define next_char() (string == limit ? '\0' : *string++)
3539 #define peek_char() (string == limit ? '\0' : *string)
3542 switch (next_char())
3544 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
3545 case 'x': case 't': case 'h': case 'd': case 's': case 'o':
3546 case 'g': case 'v': case '*': case '?': case 'r':
3550 return g_variant_format_string_scan (string, limit, endptr);
3554 return g_variant_type_string_scan (string, limit, endptr);
3557 while (peek_char() != ')')
3558 if (!g_variant_format_string_scan (string, limit, &string))
3561 next_char(); /* consume ')' */
3571 if (c != 's' && c != 'o' && c != 'g')
3579 /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
3580 * The terminating null character is considered to be
3581 * part of the string.
3583 if (c != '\0' && strchr ("bynqiuxthdsog?", c) == NULL)
3587 if (!g_variant_format_string_scan (string, limit, &string))
3590 if (next_char() != '}')
3596 if ((c = next_char()) == 'a')
3598 if ((c = next_char()) == '&')
3600 if ((c = next_char()) == 'a')
3602 if ((c = next_char()) == 'y')
3603 break; /* '^a&ay' */
3606 else if (c == 's' || c == 'o')
3607 break; /* '^a&s', '^a&o' */
3612 if ((c = next_char()) == 'y')
3616 else if (c == 's' || c == 'o')
3617 break; /* '^as', '^ao' */
3624 if ((c = next_char()) == 'a')
3626 if ((c = next_char()) == 'y')
3636 if (c != 's' && c != 'o' && c != 'g')
3655 * g_variant_format_string_scan_type:
3656 * @string: a string that may be prefixed with a format string
3657 * @limit: (allow-none) (default NULL): a pointer to the end of @string,
3659 * @endptr: (allow-none) (default NULL): location to store the end pointer,
3661 * @returns: (allow-none): a #GVariantType if there was a valid format string
3663 * If @string starts with a valid format string then this function will
3664 * return the type that the format string corresponds to. Otherwise
3665 * this function returns %NULL.
3667 * Use g_variant_type_free() to free the return value when you no longer
3670 * This function is otherwise exactly like
3671 * g_variant_format_string_scan().
3676 g_variant_format_string_scan_type (const gchar *string,
3678 const gchar **endptr)
3680 const gchar *my_end;
3687 if (!g_variant_format_string_scan (string, limit, endptr))
3690 dest = new = g_malloc (*endptr - string + 1);
3691 while (string != *endptr)
3693 if (*string != '@' && *string != '&' && *string != '^')
3699 return (GVariantType *) G_VARIANT_TYPE (new);
3703 valid_format_string (const gchar *format_string,
3707 const gchar *endptr;
3710 type = g_variant_format_string_scan_type (format_string, NULL, &endptr);
3712 if G_UNLIKELY (type == NULL || (single && *endptr != '\0'))
3715 g_critical ("`%s' is not a valid GVariant format string",
3718 g_critical ("`%s' does not have a valid GVariant format "
3719 "string as a prefix", format_string);
3722 g_variant_type_free (type);
3727 if G_UNLIKELY (value && !g_variant_is_of_type (value, type))
3732 fragment = g_strndup (format_string, endptr - format_string);
3733 typestr = g_variant_type_dup_string (type);
3735 g_critical ("the GVariant format string `%s' has a type of "
3736 "`%s' but the given value has a type of `%s'",
3737 fragment, typestr, g_variant_get_type_string (value));
3739 g_variant_type_free (type);
3744 g_variant_type_free (type);
3749 /* Variable Arguments {{{1 */
3750 /* We consider 2 main classes of format strings:
3752 * - recursive format strings
3753 * these are ones that result in recursion and the collection of
3754 * possibly more than one argument. Maybe types, tuples,
3755 * dictionary entries.
3757 * - leaf format string
3758 * these result in the collection of a single argument.
3760 * Leaf format strings are further subdivided into two categories:
3762 * - single non-null pointer ("nnp")
3763 * these either collect or return a single non-null pointer.
3766 * these collect or return something else (bool, number, etc).
3768 * Based on the above, the varargs handling code is split into 4 main parts:
3770 * - nnp handling code
3771 * - leaf handling code (which may invoke nnp code)
3772 * - generic handling code (may be recursive, may invoke leaf code)
3773 * - user-facing API (which invokes the generic code)
3775 * Each section implements some of the following functions:
3778 * collect the arguments for the format string as if
3779 * g_variant_new() had been called, but do nothing with them. used
3780 * for skipping over arguments when constructing a Nothing maybe
3784 * create a GVariant *
3787 * unpack a GVariant *
3789 * - free (nnp only):
3790 * free a previously allocated item
3794 g_variant_format_string_is_leaf (const gchar *str)
3796 return str[0] != 'm' && str[0] != '(' && str[0] != '{';
3800 g_variant_format_string_is_nnp (const gchar *str)
3802 return str[0] == 'a' || str[0] == 's' || str[0] == 'o' || str[0] == 'g' ||
3803 str[0] == '^' || str[0] == '@' || str[0] == '*' || str[0] == '?' ||
3804 str[0] == 'r' || str[0] == 'v' || str[0] == '&';
3807 /* Single non-null pointer ("nnp") {{{2 */
3809 g_variant_valist_free_nnp (const gchar *str,
3815 g_variant_iter_free (ptr);
3819 if (str[2] != '&') /* '^as', '^ao' */
3821 else /* '^a&s', '^a&o' */
3835 g_variant_unref (ptr);
3842 g_assert_not_reached ();
3847 g_variant_scan_convenience (const gchar **str,
3870 g_variant_valist_new_nnp (const gchar **str,
3881 const GVariantType *type;
3884 value = g_variant_builder_end (ptr);
3885 type = g_variant_get_type (value);
3887 if G_UNLIKELY (!g_variant_type_is_array (type))
3888 g_error ("g_variant_new: expected array GVariantBuilder but "
3889 "the built value has type `%s'",
3890 g_variant_get_type_string (value));
3892 type = g_variant_type_element (type);
3894 if G_UNLIKELY (!g_variant_type_is_subtype_of (type, (GVariantType *) *str))
3895 g_error ("g_variant_new: expected GVariantBuilder array element "
3896 "type `%s' but the built value has element type `%s'",
3897 g_variant_type_dup_string ((GVariantType *) *str),
3898 g_variant_get_type_string (value) + 1);
3900 g_variant_type_string_scan (*str, NULL, str);
3906 /* special case: NULL pointer for empty array */
3908 const GVariantType *type = (GVariantType *) *str;
3910 g_variant_type_string_scan (*str, NULL, str);
3912 if G_UNLIKELY (!g_variant_type_is_definite (type))
3913 g_error ("g_variant_new: NULL pointer given with indefinite "
3914 "array type; unable to determine which type of empty "
3915 "array to construct.");
3917 return g_variant_new_array (type, NULL, 0);
3924 value = g_variant_new_string (ptr);
3927 value = g_variant_new_string ("[Invalid UTF-8]");
3933 return g_variant_new_object_path (ptr);
3936 return g_variant_new_signature (ptr);
3944 type = g_variant_scan_convenience (str, &constant, &arrays);
3947 return g_variant_new_strv (ptr, -1);
3950 return g_variant_new_objv (ptr, -1);
3953 return g_variant_new_bytestring_array (ptr, -1);
3955 return g_variant_new_bytestring (ptr);
3959 if G_UNLIKELY (!g_variant_is_of_type (ptr, (GVariantType *) *str))
3960 g_error ("g_variant_new: expected GVariant of type `%s' but "
3961 "received value has type `%s'",
3962 g_variant_type_dup_string ((GVariantType *) *str),
3963 g_variant_get_type_string (ptr));
3965 g_variant_type_string_scan (*str, NULL, str);
3973 if G_UNLIKELY (!g_variant_type_is_basic (g_variant_get_type (ptr)))
3974 g_error ("g_variant_new: format string `?' expects basic-typed "
3975 "GVariant, but received value has type `%s'",
3976 g_variant_get_type_string (ptr));
3981 if G_UNLIKELY (!g_variant_type_is_tuple (g_variant_get_type (ptr)))
3982 g_error ("g_variant_new: format string `r` expects tuple-typed "
3983 "GVariant, but received value has type `%s'",
3984 g_variant_get_type_string (ptr));
3989 return g_variant_new_variant (ptr);
3992 g_assert_not_reached ();
3997 g_variant_valist_get_nnp (const gchar **str,
4003 g_variant_type_string_scan (*str, NULL, str);
4004 return g_variant_iter_new (value);
4008 return (gchar *) g_variant_get_string (value, NULL);
4013 return g_variant_dup_string (value, NULL);
4021 type = g_variant_scan_convenience (str, &constant, &arrays);
4026 return g_variant_get_strv (value, NULL);
4028 return g_variant_dup_strv (value, NULL);
4031 else if (type == 'o')
4034 return g_variant_get_objv (value, NULL);
4036 return g_variant_dup_objv (value, NULL);
4039 else if (arrays > 1)
4042 return g_variant_get_bytestring_array (value, NULL);
4044 return g_variant_dup_bytestring_array (value, NULL);
4050 return (gchar *) g_variant_get_bytestring (value);
4052 return g_variant_dup_bytestring (value, NULL);
4057 g_variant_type_string_scan (*str, NULL, str);
4063 return g_variant_ref (value);
4066 return g_variant_get_variant (value);
4069 g_assert_not_reached ();
4075 g_variant_valist_skip_leaf (const gchar **str,
4078 if (g_variant_format_string_is_nnp (*str))
4080 g_variant_format_string_scan (*str, NULL, str);
4081 va_arg (*app, gpointer);
4099 va_arg (*app, guint64);
4103 va_arg (*app, gdouble);
4107 g_assert_not_reached ();
4112 g_variant_valist_new_leaf (const gchar **str,
4115 if (g_variant_format_string_is_nnp (*str))
4116 return g_variant_valist_new_nnp (str, va_arg (*app, gpointer));
4121 return g_variant_new_boolean (va_arg (*app, gboolean));
4124 return g_variant_new_byte (va_arg (*app, guint));
4127 return g_variant_new_int16 (va_arg (*app, gint));
4130 return g_variant_new_uint16 (va_arg (*app, guint));
4133 return g_variant_new_int32 (va_arg (*app, gint));
4136 return g_variant_new_uint32 (va_arg (*app, guint));
4139 return g_variant_new_int64 (va_arg (*app, gint64));
4142 return g_variant_new_uint64 (va_arg (*app, guint64));
4145 return g_variant_new_handle (va_arg (*app, gint));
4148 return g_variant_new_double (va_arg (*app, gdouble));
4151 g_assert_not_reached ();
4155 /* The code below assumes this */
4156 G_STATIC_ASSERT (sizeof (gboolean) == sizeof (guint32));
4157 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
4160 g_variant_valist_get_leaf (const gchar **str,
4165 gpointer ptr = va_arg (*app, gpointer);
4169 g_variant_format_string_scan (*str, NULL, str);
4173 if (g_variant_format_string_is_nnp (*str))
4175 gpointer *nnp = (gpointer *) ptr;
4177 if (free && *nnp != NULL)
4178 g_variant_valist_free_nnp (*str, *nnp);
4183 *nnp = g_variant_valist_get_nnp (str, value);
4185 g_variant_format_string_scan (*str, NULL, str);
4195 *(gboolean *) ptr = g_variant_get_boolean (value);
4199 *(guchar *) ptr = g_variant_get_byte (value);
4203 *(gint16 *) ptr = g_variant_get_int16 (value);
4207 *(guint16 *) ptr = g_variant_get_uint16 (value);
4211 *(gint32 *) ptr = g_variant_get_int32 (value);
4215 *(guint32 *) ptr = g_variant_get_uint32 (value);
4219 *(gint64 *) ptr = g_variant_get_int64 (value);
4223 *(guint64 *) ptr = g_variant_get_uint64 (value);
4227 *(gint32 *) ptr = g_variant_get_handle (value);
4231 *(gdouble *) ptr = g_variant_get_double (value);
4240 *(guchar *) ptr = 0;
4245 *(guint16 *) ptr = 0;
4252 *(guint32 *) ptr = 0;
4258 *(guint64 *) ptr = 0;
4263 g_assert_not_reached ();
4266 /* Generic (recursive) {{{2 */
4268 g_variant_valist_skip (const gchar **str,
4271 if (g_variant_format_string_is_leaf (*str))
4272 g_variant_valist_skip_leaf (str, app);
4274 else if (**str == 'm') /* maybe */
4278 if (!g_variant_format_string_is_nnp (*str))
4279 va_arg (*app, gboolean);
4281 g_variant_valist_skip (str, app);
4283 else /* tuple, dictionary entry */
4285 g_assert (**str == '(' || **str == '{');
4287 while (**str != ')' && **str != '}')
4288 g_variant_valist_skip (str, app);
4294 g_variant_valist_new (const gchar **str,
4297 if (g_variant_format_string_is_leaf (*str))
4298 return g_variant_valist_new_leaf (str, app);
4300 if (**str == 'm') /* maybe */
4302 GVariantType *type = NULL;
4303 GVariant *value = NULL;
4307 if (g_variant_format_string_is_nnp (*str))
4309 gpointer nnp = va_arg (*app, gpointer);
4312 value = g_variant_valist_new_nnp (str, nnp);
4314 type = g_variant_format_string_scan_type (*str, NULL, str);
4318 gboolean just = va_arg (*app, gboolean);
4321 value = g_variant_valist_new (str, app);
4324 type = g_variant_format_string_scan_type (*str, NULL, NULL);
4325 g_variant_valist_skip (str, app);
4329 value = g_variant_new_maybe (type, value);
4332 g_variant_type_free (type);
4336 else /* tuple, dictionary entry */
4341 g_variant_builder_init (&b, G_VARIANT_TYPE_TUPLE);
4344 g_assert (**str == '{');
4345 g_variant_builder_init (&b, G_VARIANT_TYPE_DICT_ENTRY);
4349 while (**str != ')' && **str != '}')
4350 g_variant_builder_add_value (&b, g_variant_valist_new (str, app));
4353 return g_variant_builder_end (&b);
4358 g_variant_valist_get (const gchar **str,
4363 if (g_variant_format_string_is_leaf (*str))
4364 g_variant_valist_get_leaf (str, value, free, app);
4366 else if (**str == 'm')
4371 value = g_variant_get_maybe (value);
4373 if (!g_variant_format_string_is_nnp (*str))
4375 gboolean *ptr = va_arg (*app, gboolean *);
4378 *ptr = value != NULL;
4381 g_variant_valist_get (str, value, free, app);
4384 g_variant_unref (value);
4387 else /* tuple, dictionary entry */
4391 g_assert (**str == '(' || **str == '{');
4394 while (**str != ')' && **str != '}')
4398 GVariant *child = g_variant_get_child_value (value, index++);
4399 g_variant_valist_get (str, child, free, app);
4400 g_variant_unref (child);
4403 g_variant_valist_get (str, NULL, free, app);
4409 /* User-facing API {{{2 */
4411 * g_variant_new: (skip)
4412 * @format_string: a #GVariant format string
4413 * @...: arguments, as per @format_string
4414 * @returns: a new floating #GVariant instance
4416 * Creates a new #GVariant instance.
4418 * Think of this function as an analogue to g_strdup_printf().
4420 * The type of the created instance and the arguments that are
4421 * expected by this function are determined by @format_string. See the
4422 * section on <link linkend='gvariant-format-strings'>GVariant Format
4423 * Strings</link>. Please note that the syntax of the format string is
4424 * very likely to be extended in the future.
4426 * The first character of the format string must not be '*' '?' '@' or
4427 * 'r'; in essence, a new #GVariant must always be constructed by this
4428 * function (and not merely passed through it unmodified).
4433 g_variant_new (const gchar *format_string,
4439 g_return_val_if_fail (valid_format_string (format_string, TRUE, NULL) &&
4440 format_string[0] != '?' && format_string[0] != '@' &&
4441 format_string[0] != '*' && format_string[0] != 'r',
4444 va_start (ap, format_string);
4445 value = g_variant_new_va (format_string, NULL, &ap);
4452 * g_variant_new_va: (skip)
4453 * @format_string: a string that is prefixed with a format string
4454 * @endptr: (allow-none) (default NULL): location to store the end pointer,
4456 * @app: a pointer to a #va_list
4457 * @returns: a new, usually floating, #GVariant
4459 * This function is intended to be used by libraries based on
4460 * #GVariant that want to provide g_variant_new()-like functionality
4463 * The API is more general than g_variant_new() to allow a wider range
4466 * @format_string must still point to a valid format string, but it only
4467 * needs to be nul-terminated if @endptr is %NULL. If @endptr is
4468 * non-%NULL then it is updated to point to the first character past the
4469 * end of the format string.
4471 * @app is a pointer to a #va_list. The arguments, according to
4472 * @format_string, are collected from this #va_list and the list is left
4473 * pointing to the argument following the last.
4475 * These two generalisations allow mixing of multiple calls to
4476 * g_variant_new_va() and g_variant_get_va() within a single actual
4477 * varargs call by the user.
4479 * The return value will be floating if it was a newly created GVariant
4480 * instance (for example, if the format string was "(ii)"). In the case
4481 * that the format_string was '*', '?', 'r', or a format starting with
4482 * '@' then the collected #GVariant pointer will be returned unmodified,
4483 * without adding any additional references.
4485 * In order to behave correctly in all cases it is necessary for the
4486 * calling function to g_variant_ref_sink() the return result before
4487 * returning control to the user that originally provided the pointer.
4488 * At this point, the caller will have their own full reference to the
4489 * result. This can also be done by adding the result to a container,
4490 * or by passing it to another g_variant_new() call.
4495 g_variant_new_va (const gchar *format_string,
4496 const gchar **endptr,
4501 g_return_val_if_fail (valid_format_string (format_string, !endptr, NULL),
4503 g_return_val_if_fail (app != NULL, NULL);
4505 value = g_variant_valist_new (&format_string, app);
4508 *endptr = format_string;
4514 * g_variant_get: (skip)
4515 * @value: a #GVariant instance
4516 * @format_string: a #GVariant format string
4517 * @...: arguments, as per @format_string
4519 * Deconstructs a #GVariant instance.
4521 * Think of this function as an analogue to scanf().
4523 * The arguments that are expected by this function are entirely
4524 * determined by @format_string. @format_string also restricts the
4525 * permissible types of @value. It is an error to give a value with
4526 * an incompatible type. See the section on <link
4527 * linkend='gvariant-format-strings'>GVariant Format Strings</link>.
4528 * Please note that the syntax of the format string is very likely to be
4529 * extended in the future.
4534 g_variant_get (GVariant *value,
4535 const gchar *format_string,
4540 g_return_if_fail (valid_format_string (format_string, TRUE, value));
4542 /* if any direct-pointer-access formats are in use, flatten first */
4543 if (strchr (format_string, '&'))
4544 g_variant_get_data (value);
4546 va_start (ap, format_string);
4547 g_variant_get_va (value, format_string, NULL, &ap);
4552 * g_variant_get_va: (skip)
4553 * @value: a #GVariant
4554 * @format_string: a string that is prefixed with a format string
4555 * @endptr: (allow-none) (default NULL): location to store the end pointer,
4557 * @app: a pointer to a #va_list
4559 * This function is intended to be used by libraries based on #GVariant
4560 * that want to provide g_variant_get()-like functionality to their
4563 * The API is more general than g_variant_get() to allow a wider range
4566 * @format_string must still point to a valid format string, but it only
4567 * need to be nul-terminated if @endptr is %NULL. If @endptr is
4568 * non-%NULL then it is updated to point to the first character past the
4569 * end of the format string.
4571 * @app is a pointer to a #va_list. The arguments, according to
4572 * @format_string, are collected from this #va_list and the list is left
4573 * pointing to the argument following the last.
4575 * These two generalisations allow mixing of multiple calls to
4576 * g_variant_new_va() and g_variant_get_va() within a single actual
4577 * varargs call by the user.
4582 g_variant_get_va (GVariant *value,
4583 const gchar *format_string,
4584 const gchar **endptr,
4587 g_return_if_fail (valid_format_string (format_string, !endptr, value));
4588 g_return_if_fail (value != NULL);
4589 g_return_if_fail (app != NULL);
4591 /* if any direct-pointer-access formats are in use, flatten first */
4592 if (strchr (format_string, '&'))
4593 g_variant_get_data (value);
4595 g_variant_valist_get (&format_string, value, FALSE, app);
4598 *endptr = format_string;
4601 /* Varargs-enabled Utility Functions {{{1 */
4604 * g_variant_builder_add: (skp)
4605 * @builder: a #GVariantBuilder
4606 * @format_string: a #GVariant varargs format string
4607 * @...: arguments, as per @format_string
4609 * Adds to a #GVariantBuilder.
4611 * This call is a convenience wrapper that is exactly equivalent to
4612 * calling g_variant_new() followed by g_variant_builder_add_value().
4614 * This function might be used as follows:
4618 * make_pointless_dictionary (void)
4620 * GVariantBuilder *builder;
4623 * builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
4624 * for (i = 0; i < 16; i++)
4628 * sprintf (buf, "%d", i);
4629 * g_variant_builder_add (builder, "{is}", i, buf);
4632 * return g_variant_builder_end (builder);
4639 g_variant_builder_add (GVariantBuilder *builder,
4640 const gchar *format_string,
4646 va_start (ap, format_string);
4647 variant = g_variant_new_va (format_string, NULL, &ap);
4650 g_variant_builder_add_value (builder, variant);
4654 * g_variant_get_child: (skip)
4655 * @value: a container #GVariant
4656 * @index_: the index of the child to deconstruct
4657 * @format_string: a #GVariant format string
4658 * @...: arguments, as per @format_string
4660 * Reads a child item out of a container #GVariant instance and
4661 * deconstructs it according to @format_string. This call is
4662 * essentially a combination of g_variant_get_child_value() and
4668 g_variant_get_child (GVariant *value,
4670 const gchar *format_string,
4676 child = g_variant_get_child_value (value, index_);
4677 g_return_if_fail (valid_format_string (format_string, TRUE, child));
4679 va_start (ap, format_string);
4680 g_variant_get_va (child, format_string, NULL, &ap);
4683 g_variant_unref (child);
4687 * g_variant_iter_next: (skip)
4688 * @iter: a #GVariantIter
4689 * @format_string: a GVariant format string
4690 * @...: the arguments to unpack the value into
4691 * @returns: %TRUE if a value was unpacked, or %FALSE if there as no
4694 * Gets the next item in the container and unpacks it into the variable
4695 * argument list according to @format_string, returning %TRUE.
4697 * If no more items remain then %FALSE is returned.
4699 * All of the pointers given on the variable arguments list of this
4700 * function are assumed to point at uninitialised memory. It is the
4701 * responsibility of the caller to free all of the values returned by
4702 * the unpacking process.
4704 * See the section on <link linkend='gvariant-format-strings'>GVariant
4705 * Format Strings</link>.
4708 * <title>Memory management with g_variant_iter_next()</title>
4710 * /<!-- -->* Iterates a dictionary of type 'a{sv}' *<!-- -->/
4712 * iterate_dictionary (GVariant *dictionary)
4714 * GVariantIter iter;
4718 * g_variant_iter_init (&iter, dictionary);
4719 * while (g_variant_iter_next (&iter, "{sv}", &key, &value))
4721 * g_print ("Item '%s' has type '%s'\n", key,
4722 * g_variant_get_type_string (value));
4724 * /<!-- -->* must free data for ourselves *<!-- -->/
4725 * g_variant_unref (value);
4732 * For a solution that is likely to be more convenient to C programmers
4733 * when dealing with loops, see g_variant_iter_loop().
4738 g_variant_iter_next (GVariantIter *iter,
4739 const gchar *format_string,
4744 value = g_variant_iter_next_value (iter);
4746 g_return_val_if_fail (valid_format_string (format_string, TRUE, value),
4753 va_start (ap, format_string);
4754 g_variant_valist_get (&format_string, value, FALSE, &ap);
4757 g_variant_unref (value);
4760 return value != NULL;
4764 * g_variant_iter_loop: (skip)
4765 * @iter: a #GVariantIter
4766 * @format_string: a GVariant format string
4767 * @...: the arguments to unpack the value into
4768 * @returns: %TRUE if a value was unpacked, or %FALSE if there as no
4771 * Gets the next item in the container and unpacks it into the variable
4772 * argument list according to @format_string, returning %TRUE.
4774 * If no more items remain then %FALSE is returned.
4776 * On the first call to this function, the pointers appearing on the
4777 * variable argument list are assumed to point at uninitialised memory.
4778 * On the second and later calls, it is assumed that the same pointers
4779 * will be given and that they will point to the memory as set by the
4780 * previous call to this function. This allows the previous values to
4781 * be freed, as appropriate.
4783 * This function is intended to be used with a while loop as
4784 * demonstrated in the following example. This function can only be
4785 * used when iterating over an array. It is only valid to call this
4786 * function with a string constant for the format string and the same
4787 * string constant must be used each time. Mixing calls to this
4788 * function and g_variant_iter_next() or g_variant_iter_next_value() on
4789 * the same iterator is not recommended.
4791 * See the section on <link linkend='gvariant-format-strings'>GVariant
4792 * Format Strings</link>.
4795 * <title>Memory management with g_variant_iter_loop()</title>
4797 * /<!-- -->* Iterates a dictionary of type 'a{sv}' *<!-- -->/
4799 * iterate_dictionary (GVariant *dictionary)
4801 * GVariantIter iter;
4805 * g_variant_iter_init (&iter, dictionary);
4806 * while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
4808 * g_print ("Item '%s' has type '%s'\n", key,
4809 * g_variant_get_type_string (value));
4811 * /<!-- -->* no need to free 'key' and 'value' here *<!-- -->/
4817 * For most cases you should use g_variant_iter_next().
4819 * This function is really only useful when unpacking into #GVariant or
4820 * #GVariantIter in order to allow you to skip the call to
4821 * g_variant_unref() or g_variant_iter_free().
4823 * For example, if you are only looping over simple integer and string
4824 * types, g_variant_iter_next() is definitely preferred. For string
4825 * types, use the '&' prefix to avoid allocating any memory at all (and
4826 * thereby avoiding the need to free anything as well).
4831 g_variant_iter_loop (GVariantIter *iter,
4832 const gchar *format_string,
4835 gboolean first_time = GVSI(iter)->loop_format == NULL;
4839 g_return_val_if_fail (first_time ||
4840 format_string == GVSI(iter)->loop_format,
4845 TYPE_CHECK (GVSI(iter)->value, G_VARIANT_TYPE_ARRAY, FALSE);
4846 GVSI(iter)->loop_format = format_string;
4848 if (strchr (format_string, '&'))
4849 g_variant_get_data (GVSI(iter)->value);
4852 value = g_variant_iter_next_value (iter);
4854 g_return_val_if_fail (!first_time ||
4855 valid_format_string (format_string, TRUE, value),
4858 va_start (ap, format_string);
4859 g_variant_valist_get (&format_string, value, !first_time, &ap);
4863 g_variant_unref (value);
4865 return value != NULL;
4868 /* Serialised data {{{1 */
4870 g_variant_deep_copy (GVariant *value)
4872 switch (g_variant_classify (value))
4874 case G_VARIANT_CLASS_MAYBE:
4875 case G_VARIANT_CLASS_ARRAY:
4876 case G_VARIANT_CLASS_TUPLE:
4877 case G_VARIANT_CLASS_DICT_ENTRY:
4878 case G_VARIANT_CLASS_VARIANT:
4880 GVariantBuilder builder;
4884 g_variant_builder_init (&builder, g_variant_get_type (value));
4885 g_variant_iter_init (&iter, value);
4887 while ((child = g_variant_iter_next_value (&iter)))
4889 g_variant_builder_add_value (&builder, g_variant_deep_copy (child));
4890 g_variant_unref (child);
4893 return g_variant_builder_end (&builder);
4896 case G_VARIANT_CLASS_BOOLEAN:
4897 return g_variant_new_boolean (g_variant_get_boolean (value));
4899 case G_VARIANT_CLASS_BYTE:
4900 return g_variant_new_byte (g_variant_get_byte (value));
4902 case G_VARIANT_CLASS_INT16:
4903 return g_variant_new_int16 (g_variant_get_int16 (value));
4905 case G_VARIANT_CLASS_UINT16:
4906 return g_variant_new_uint16 (g_variant_get_uint16 (value));
4908 case G_VARIANT_CLASS_INT32:
4909 return g_variant_new_int32 (g_variant_get_int32 (value));
4911 case G_VARIANT_CLASS_UINT32:
4912 return g_variant_new_uint32 (g_variant_get_uint32 (value));
4914 case G_VARIANT_CLASS_INT64:
4915 return g_variant_new_int64 (g_variant_get_int64 (value));
4917 case G_VARIANT_CLASS_UINT64:
4918 return g_variant_new_uint64 (g_variant_get_uint64 (value));
4920 case G_VARIANT_CLASS_HANDLE:
4921 return g_variant_new_handle (g_variant_get_handle (value));
4923 case G_VARIANT_CLASS_DOUBLE:
4924 return g_variant_new_double (g_variant_get_double (value));
4926 case G_VARIANT_CLASS_STRING:
4927 return g_variant_new_string (g_variant_get_string (value, NULL));
4929 case G_VARIANT_CLASS_OBJECT_PATH:
4930 return g_variant_new_object_path (g_variant_get_string (value, NULL));
4932 case G_VARIANT_CLASS_SIGNATURE:
4933 return g_variant_new_signature (g_variant_get_string (value, NULL));
4936 g_assert_not_reached ();
4940 * g_variant_get_normal_form:
4941 * @value: a #GVariant
4942 * @returns: (transfer full): a trusted #GVariant
4944 * Gets a #GVariant instance that has the same value as @value and is
4945 * trusted to be in normal form.
4947 * If @value is already trusted to be in normal form then a new
4948 * reference to @value is returned.
4950 * If @value is not already trusted, then it is scanned to check if it
4951 * is in normal form. If it is found to be in normal form then it is
4952 * marked as trusted and a new reference to it is returned.
4954 * If @value is found not to be in normal form then a new trusted
4955 * #GVariant is created with the same value as @value.
4957 * It makes sense to call this function if you've received #GVariant
4958 * data from untrusted sources and you want to ensure your serialised
4959 * output is definitely in normal form.
4964 g_variant_get_normal_form (GVariant *value)
4968 if (g_variant_is_normal_form (value))
4969 return g_variant_ref (value);
4971 trusted = g_variant_deep_copy (value);
4972 g_assert (g_variant_is_trusted (trusted));
4974 return g_variant_ref_sink (trusted);
4978 * g_variant_byteswap:
4979 * @value: a #GVariant
4980 * @returns: (transfer full): the byteswapped form of @value
4982 * Performs a byteswapping operation on the contents of @value. The
4983 * result is that all multi-byte numeric data contained in @value is
4984 * byteswapped. That includes 16, 32, and 64bit signed and unsigned
4985 * integers as well as file handles and double precision floating point
4988 * This function is an identity mapping on any value that does not
4989 * contain multi-byte numeric data. That include strings, booleans,
4990 * bytes and containers containing only these things (recursively).
4992 * The returned value is always in normal form and is marked as trusted.
4997 g_variant_byteswap (GVariant *value)
4999 GVariantTypeInfo *type_info;
5003 type_info = g_variant_get_type_info (value);
5005 g_variant_type_info_query (type_info, &alignment, NULL);
5008 /* (potentially) contains multi-byte numeric data */
5010 GVariantSerialised serialised;
5014 trusted = g_variant_get_normal_form (value);
5015 serialised.type_info = g_variant_get_type_info (trusted);
5016 serialised.size = g_variant_get_size (trusted);
5017 serialised.data = g_malloc (serialised.size);
5018 g_variant_store (trusted, serialised.data);
5019 g_variant_unref (trusted);
5021 g_variant_serialised_byteswap (serialised);
5023 buffer = g_buffer_new_take_data (serialised.data, serialised.size);
5024 new = g_variant_new_from_buffer (g_variant_get_type (value), buffer, TRUE);
5025 g_buffer_unref (buffer);
5028 /* contains no multi-byte data */
5031 return g_variant_ref_sink (new);
5035 * g_variant_new_from_data:
5036 * @type: a definite #GVariantType
5037 * @data: (array length=size) (element-type guint8): the serialised data
5038 * @size: the size of @data
5039 * @trusted: %TRUE if @data is definitely in normal form
5040 * @notify: (scope async): function to call when @data is no longer needed
5041 * @user_data: data for @notify
5042 * @returns: (transfer none): a new floating #GVariant of type @type
5044 * Creates a new #GVariant instance from serialised data.
5046 * @type is the type of #GVariant instance that will be constructed.
5047 * The interpretation of @data depends on knowing the type.
5049 * @data is not modified by this function and must remain valid with an
5050 * unchanging value until such a time as @notify is called with
5051 * @user_data. If the contents of @data change before that time then
5052 * the result is undefined.
5054 * If @data is trusted to be serialised data in normal form then
5055 * @trusted should be %TRUE. This applies to serialised data created
5056 * within this process or read from a trusted location on the disk (such
5057 * as a file installed in /usr/lib alongside your application). You
5058 * should set trusted to %FALSE if @data is read from the network, a
5059 * file in the user's home directory, etc.
5061 * @notify will be called with @user_data when @data is no longer
5062 * needed. The exact time of this call is unspecified and might even be
5063 * before this function returns.
5068 g_variant_new_from_data (const GVariantType *type,
5072 GDestroyNotify notify,
5078 g_return_val_if_fail (g_variant_type_is_definite (type), NULL);
5079 g_return_val_if_fail (data != NULL || size == 0, NULL);
5082 buffer = g_buffer_new_from_pointer (data, size, notify, user_data);
5084 buffer = g_buffer_new_from_static_data (data, size);
5086 value = g_variant_new_from_buffer (type, buffer, trusted);
5087 g_buffer_unref (buffer);
5093 /* vim:set foldmethod=marker: */