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, see <http://www.gnu.org/licenses/>.
18 * Author: Ryan Lortie <desrt@desrt.ca>
25 #include <glib/gvariant-serialiser.h>
26 #include "gvariant-internal.h"
27 #include <glib/gvariant-core.h>
28 #include <glib/gtestutils.h>
29 #include <glib/gstrfuncs.h>
30 #include <glib/gslice.h>
31 #include <glib/ghash.h>
32 #include <glib/gmem.h>
40 * @short_description: strongly typed value datatype
41 * @see_also: GVariantType
43 * #GVariant is a variant datatype; it stores a value along with
44 * information about the type of that value. The range of possible
45 * values is determined by the type. The type system used by #GVariant
48 * #GVariant instances always have a type and a value (which are given
49 * at construction time). The type and value of a #GVariant instance
50 * can never change other than by the #GVariant itself being
51 * destroyed. A #GVariant cannot contain a pointer.
53 * #GVariant is reference counted using g_variant_ref() and
54 * g_variant_unref(). #GVariant also has floating reference counts --
55 * see g_variant_ref_sink().
57 * #GVariant is completely threadsafe. A #GVariant instance can be
58 * concurrently accessed in any way from any number of threads without
61 * #GVariant is heavily optimised for dealing with data in serialised
62 * form. It works particularly well with data located in memory-mapped
63 * files. It can perform nearly all deserialisation operations in a
64 * small constant time, usually touching only a single memory page.
65 * Serialised #GVariant data can also be sent over the network.
67 * #GVariant is largely compatible with D-Bus. Almost all types of
68 * #GVariant instances can be sent over D-Bus. See #GVariantType for
69 * exceptions. (However, #GVariant's serialisation format is not the same
70 * as the serialisation format of a D-Bus message body: use #GDBusMessage,
71 * in the gio library, for those.)
73 * For space-efficiency, the #GVariant serialisation format does not
74 * automatically include the variant's type or endianness, which must
75 * either be implied from context (such as knowledge that a particular
76 * file format always contains a little-endian %G_VARIANT_TYPE_VARIANT)
77 * or supplied out-of-band (for instance, a type and/or endianness
78 * indicator could be placed at the beginning of a file, network message
81 * A #GVariant's size is limited mainly by any lower level operating
82 * system constraints, such as the number of bits in #gsize. For
83 * example, it is reasonable to have a 2GB file mapped into memory
84 * with #GMappedFile, and call g_variant_new_from_data() on it.
86 * For convenience to C programmers, #GVariant features powerful
87 * varargs-based value construction and destruction. This feature is
88 * designed to be embedded in other libraries.
90 * There is a Python-inspired text language for describing #GVariant
91 * values. #GVariant includes a printer for this language and a parser
92 * with type inferencing.
96 * #GVariant tries to be quite efficient with respect to memory use.
97 * This section gives a rough idea of how much memory is used by the
98 * current implementation. The information here is subject to change
101 * The memory allocated by #GVariant can be grouped into 4 broad
102 * purposes: memory for serialised data, memory for the type
103 * information cache, buffer management memory and memory for the
104 * #GVariant structure itself.
106 * ## Serialised Data Memory
108 * This is the memory that is used for storing GVariant data in
109 * serialised form. This is what would be sent over the network or
110 * what would end up on disk.
112 * The amount of memory required to store a boolean is 1 byte. 16,
113 * 32 and 64 bit integers and double precision floating point numbers
114 * use their "natural" size. Strings (including object path and
115 * signature strings) are stored with a nul terminator, and as such
116 * use the length of the string plus 1 byte.
118 * Maybe types use no space at all to represent the null value and
119 * use the same amount of space (sometimes plus one byte) as the
120 * equivalent non-maybe-typed value to represent the non-null case.
122 * Arrays use the amount of space required to store each of their
123 * members, concatenated. Additionally, if the items stored in an
124 * array are not of a fixed-size (ie: strings, other arrays, etc)
125 * then an additional framing offset is stored for each item. The
126 * size of this offset is either 1, 2 or 4 bytes depending on the
127 * overall size of the container. Additionally, extra padding bytes
128 * are added as required for alignment of child values.
130 * Tuples (including dictionary entries) use the amount of space
131 * required to store each of their members, concatenated, plus one
132 * framing offset (as per arrays) for each non-fixed-sized item in
133 * the tuple, except for the last one. Additionally, extra padding
134 * bytes are added as required for alignment of child values.
136 * Variants use the same amount of space as the item inside of the
137 * variant, plus 1 byte, plus the length of the type string for the
138 * item inside the variant.
140 * As an example, consider a dictionary mapping strings to variants.
141 * In the case that the dictionary is empty, 0 bytes are required for
144 * If we add an item "width" that maps to the int32 value of 500 then
145 * we will use 4 byte to store the int32 (so 6 for the variant
146 * containing it) and 6 bytes for the string. The variant must be
147 * aligned to 8 after the 6 bytes of the string, so that's 2 extra
148 * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
149 * for the dictionary entry. An additional 1 byte is added to the
150 * 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.
158 * We now require extra padding between the two items in the array.
159 * After the 14 bytes of the first item, that's 2 bytes required. We
160 * now require 2 framing offsets for an extra two bytes. 14 + 2 + 11
161 * + 2 = 29 bytes to encode the entire two-item dictionary.
163 * ## Type Information Cache
165 * For each GVariant type that currently exists in the program a type
166 * information structure is kept in the type information cache. The
167 * type information structure is required for rapid deserialisation.
169 * Continuing with the above example, if a #GVariant exists with the
170 * type "a{sv}" then a type information struct will exist for
171 * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type
172 * will share the same type information. Additionally, all
173 * single-digit types are stored in read-only static memory and do
174 * not contribute to the writable memory footprint of a program using
177 * Aside from the type information structures stored in read-only
178 * memory, there are two forms of type information. One is used for
179 * container types where there is a single element type: arrays and
180 * maybe types. The other is used for container types where there
181 * are multiple element types: tuples and dictionary entries.
183 * Array type info structures are 6 * sizeof (void *), plus the
184 * memory required to store the type string itself. This means that
185 * on 32-bit systems, the cache entry for "a{sv}" would require 30
186 * bytes of memory (plus malloc overhead).
188 * Tuple type info structures are 6 * sizeof (void *), plus 4 *
189 * sizeof (void *) for each item in the tuple, plus the memory
190 * required to store the type string itself. A 2-item tuple, for
191 * example, would have a type information structure that consumed
192 * writable memory in the size of 14 * sizeof (void *) (plus type
193 * string) This means that on 32-bit systems, the cache entry for
194 * "{sv}" would require 61 bytes of memory (plus malloc overhead).
196 * This means that in total, for our "a{sv}" example, 91 bytes of
197 * type information would be allocated.
199 * The type information cache, additionally, uses a #GHashTable to
200 * store and lookup the cached items and stores a pointer to this
201 * hash table in static storage. The hash table is freed when there
202 * are zero items in the type cache.
204 * Although these sizes may seem large it is important to remember
205 * that a program will probably only have a very small number of
206 * different types of values in it and that only one type information
207 * structure is required for many different values of the same type.
209 * ## Buffer Management Memory
211 * #GVariant uses an internal buffer management structure to deal
212 * with the various different possible sources of serialised data
213 * that it uses. The buffer is responsible for ensuring that the
214 * correct call is made when the data is no longer in use by
215 * #GVariant. This may involve a g_free() or a g_slice_free() or
216 * even g_mapped_file_unref().
218 * One buffer management structure is used for each chunk of
219 * serialised data. The size of the buffer management structure
220 * is 4 * (void *). On 32-bit systems, that's 16 bytes.
222 * ## GVariant structure
224 * The size of a #GVariant structure is 6 * (void *). On 32-bit
225 * systems, that's 24 bytes.
227 * #GVariant structures only exist if they are explicitly created
228 * with API calls. For example, if a #GVariant is constructed out of
229 * serialised data for the example given above (with the dictionary)
230 * then although there are 9 individual values that comprise the
231 * entire dictionary (two keys, two values, two variants containing
232 * the values, two dictionary entries, plus the dictionary itself),
233 * only 1 #GVariant instance exists -- the one referring to the
236 * If calls are made to start accessing the other values then
237 * #GVariant instances will exist for those values only for as long
238 * as they are in use (ie: until you call g_variant_unref()). The
239 * type information is shared. The serialised data and the buffer
240 * management structure for that serialised data is shared by the
245 * To put the entire example together, for our dictionary mapping
246 * strings to variants (with two entries, as given above), we are
247 * using 91 bytes of memory for type information, 29 byes of memory
248 * for the serialised data, 16 bytes for buffer management and 24
249 * bytes for the #GVariant instance, or a total of 160 bytes, plus
250 * malloc overhead. If we were to use g_variant_get_child_value() to
251 * access the two dictionary entries, we would use an additional 48
252 * bytes. If we were to have other dictionaries of the same type, we
253 * would use more memory for the serialised data and buffer
254 * management for those dictionaries, but the type information would
258 /* definition of GVariant structure is in gvariant-core.c */
260 /* this is a g_return_val_if_fail() for making
261 * sure a (GVariant *) has the required type.
263 #define TYPE_CHECK(value, TYPE, val) \
264 if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \
265 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, \
266 "g_variant_is_of_type (" #value \
271 /* Numeric Type Constructor/Getters {{{1 */
273 * g_variant_new_from_trusted:
274 * @type: the #GVariantType
275 * @data: the data to use
276 * @size: the size of @data
278 * Constructs a new trusted #GVariant instance from the provided data.
279 * This is used to implement g_variant_new_* for all the basic types.
281 * Returns: a new floating #GVariant
284 g_variant_new_from_trusted (const GVariantType *type,
291 bytes = g_bytes_new (data, size);
292 value = g_variant_new_from_bytes (type, bytes, TRUE);
293 g_bytes_unref (bytes);
299 * g_variant_new_boolean:
300 * @value: a #gboolean value
302 * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
304 * Returns: (transfer none): a floating reference to a new boolean #GVariant instance
309 g_variant_new_boolean (gboolean value)
313 return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1);
317 * g_variant_get_boolean:
318 * @value: a boolean #GVariant instance
320 * Returns the boolean value of @value.
322 * It is an error to call this function with a @value of any type
323 * other than %G_VARIANT_TYPE_BOOLEAN.
325 * Returns: %TRUE or %FALSE
330 g_variant_get_boolean (GVariant *value)
334 TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE);
336 data = g_variant_get_data (value);
338 return data != NULL ? *data != 0 : FALSE;
341 /* the constructors and accessors for byte, int{16,32,64}, handles and
342 * doubles all look pretty much exactly the same, so we reduce
345 #define NUMERIC_TYPE(TYPE, type, ctype) \
346 GVariant *g_variant_new_##type (ctype value) { \
347 return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \
348 &value, sizeof value); \
350 ctype g_variant_get_##type (GVariant *value) { \
352 TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \
353 data = g_variant_get_data (value); \
354 return data != NULL ? *data : 0; \
359 * g_variant_new_byte:
360 * @value: a #guint8 value
362 * Creates a new byte #GVariant instance.
364 * Returns: (transfer none): a floating reference to a new byte #GVariant instance
369 * g_variant_get_byte:
370 * @value: a byte #GVariant instance
372 * Returns the byte value of @value.
374 * It is an error to call this function with a @value of any type
375 * other than %G_VARIANT_TYPE_BYTE.
381 NUMERIC_TYPE (BYTE, byte, guchar)
384 * g_variant_new_int16:
385 * @value: a #gint16 value
387 * Creates a new int16 #GVariant instance.
389 * Returns: (transfer none): a floating reference to a new int16 #GVariant instance
394 * g_variant_get_int16:
395 * @value: a int16 #GVariant instance
397 * Returns the 16-bit signed integer value of @value.
399 * It is an error to call this function with a @value of any type
400 * other than %G_VARIANT_TYPE_INT16.
406 NUMERIC_TYPE (INT16, int16, gint16)
409 * g_variant_new_uint16:
410 * @value: a #guint16 value
412 * Creates a new uint16 #GVariant instance.
414 * Returns: (transfer none): a floating reference to a new uint16 #GVariant instance
419 * g_variant_get_uint16:
420 * @value: a uint16 #GVariant instance
422 * Returns the 16-bit unsigned integer value of @value.
424 * It is an error to call this function with a @value of any type
425 * other than %G_VARIANT_TYPE_UINT16.
427 * Returns: a #guint16
431 NUMERIC_TYPE (UINT16, uint16, guint16)
434 * g_variant_new_int32:
435 * @value: a #gint32 value
437 * Creates a new int32 #GVariant instance.
439 * Returns: (transfer none): a floating reference to a new int32 #GVariant instance
444 * g_variant_get_int32:
445 * @value: a int32 #GVariant instance
447 * Returns the 32-bit signed integer value of @value.
449 * It is an error to call this function with a @value of any type
450 * other than %G_VARIANT_TYPE_INT32.
456 NUMERIC_TYPE (INT32, int32, gint32)
459 * g_variant_new_uint32:
460 * @value: a #guint32 value
462 * Creates a new uint32 #GVariant instance.
464 * Returns: (transfer none): a floating reference to a new uint32 #GVariant instance
469 * g_variant_get_uint32:
470 * @value: a uint32 #GVariant instance
472 * Returns the 32-bit unsigned integer value of @value.
474 * It is an error to call this function with a @value of any type
475 * other than %G_VARIANT_TYPE_UINT32.
477 * Returns: a #guint32
481 NUMERIC_TYPE (UINT32, uint32, guint32)
484 * g_variant_new_int64:
485 * @value: a #gint64 value
487 * Creates a new int64 #GVariant instance.
489 * Returns: (transfer none): a floating reference to a new int64 #GVariant instance
494 * g_variant_get_int64:
495 * @value: a int64 #GVariant instance
497 * Returns the 64-bit signed integer value of @value.
499 * It is an error to call this function with a @value of any type
500 * other than %G_VARIANT_TYPE_INT64.
506 NUMERIC_TYPE (INT64, int64, gint64)
509 * g_variant_new_uint64:
510 * @value: a #guint64 value
512 * Creates a new uint64 #GVariant instance.
514 * Returns: (transfer none): a floating reference to a new uint64 #GVariant instance
519 * g_variant_get_uint64:
520 * @value: a uint64 #GVariant instance
522 * Returns the 64-bit unsigned integer value of @value.
524 * It is an error to call this function with a @value of any type
525 * other than %G_VARIANT_TYPE_UINT64.
527 * Returns: a #guint64
531 NUMERIC_TYPE (UINT64, uint64, guint64)
534 * g_variant_new_handle:
535 * @value: a #gint32 value
537 * Creates a new handle #GVariant instance.
539 * By convention, handles are indexes into an array of file descriptors
540 * that are sent alongside a D-Bus message. If you're not interacting
541 * with D-Bus, you probably don't need them.
543 * Returns: (transfer none): a floating reference to a new handle #GVariant instance
548 * g_variant_get_handle:
549 * @value: a handle #GVariant instance
551 * Returns the 32-bit signed integer value of @value.
553 * It is an error to call this function with a @value of any type other
554 * than %G_VARIANT_TYPE_HANDLE.
556 * By convention, handles are indexes into an array of file descriptors
557 * that are sent alongside a D-Bus message. If you're not interacting
558 * with D-Bus, you probably don't need them.
564 NUMERIC_TYPE (HANDLE, handle, gint32)
567 * g_variant_new_double:
568 * @value: a #gdouble floating point value
570 * Creates a new double #GVariant instance.
572 * Returns: (transfer none): a floating reference to a new double #GVariant instance
577 * g_variant_get_double:
578 * @value: a double #GVariant instance
580 * Returns the double precision floating point value of @value.
582 * It is an error to call this function with a @value of any type
583 * other than %G_VARIANT_TYPE_DOUBLE.
585 * Returns: a #gdouble
589 NUMERIC_TYPE (DOUBLE, double, gdouble)
591 /* Container type Constructor / Deconstructors {{{1 */
593 * g_variant_new_maybe:
594 * @child_type: (allow-none): the #GVariantType of the child, or %NULL
595 * @child: (allow-none): the child value, or %NULL
597 * Depending on if @child is %NULL, either wraps @child inside of a
598 * maybe container or creates a Nothing instance for the given @type.
600 * At least one of @child_type and @child must be non-%NULL.
601 * If @child_type is non-%NULL then it must be a definite type.
602 * If they are both non-%NULL then @child_type must be the type
605 * If @child is a floating reference (see g_variant_ref_sink()), the new
606 * instance takes ownership of @child.
608 * Returns: (transfer none): a floating reference to a new #GVariant maybe instance
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
654 * Given a maybe-typed #GVariant instance, extract its value. If the
655 * value is Nothing, then this function returns %NULL.
657 * Returns: (allow-none) (transfer full): the contents of @value, or %NULL
662 g_variant_get_maybe (GVariant *value)
664 TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL);
666 if (g_variant_n_children (value))
667 return g_variant_get_child_value (value, 0);
673 * g_variant_new_variant: (constructor)
674 * @value: a #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.
682 * Returns: (transfer none): a floating reference to a new variant #GVariant instance
687 g_variant_new_variant (GVariant *value)
689 g_return_val_if_fail (value != NULL, NULL);
691 g_variant_ref_sink (value);
693 return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
694 g_memdup (&value, sizeof value),
695 1, g_variant_is_trusted (value));
699 * g_variant_get_variant:
700 * @value: a variant #GVariant instance
702 * Unboxes @value. The result is the #GVariant instance that was
703 * contained in @value.
705 * Returns: (transfer full): the item contained in the variant
710 g_variant_get_variant (GVariant *value)
712 TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL);
714 return g_variant_get_child_value (value, 0);
718 * g_variant_new_array:
719 * @child_type: (allow-none): the element type of the new array
720 * @children: (allow-none) (array length=n_children): an array of
721 * #GVariant pointers, the children
722 * @n_children: the length of @children
724 * Creates a new #GVariant array from @children.
726 * @child_type must be non-%NULL if @n_children is zero. Otherwise, the
727 * child type is determined by inspecting the first element of the
728 * @children array. If @child_type is non-%NULL then it must be a
731 * The items of the array are taken from the @children array. No entry
732 * in the @children array may be %NULL.
734 * All items in the array must have the same type, which must be the
735 * same as @child_type, if given.
737 * If the @children are floating references (see g_variant_ref_sink()), the
738 * new instance takes ownership of them as if via g_variant_ref_sink().
740 * Returns: (transfer none): a floating reference to a new #GVariant array
745 g_variant_new_array (const GVariantType *child_type,
746 GVariant * const *children,
749 GVariantType *array_type;
750 GVariant **my_children;
755 g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
756 g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
757 g_return_val_if_fail (child_type == NULL ||
758 g_variant_type_is_definite (child_type), NULL);
760 my_children = g_new (GVariant *, n_children);
763 if (child_type == NULL)
764 child_type = g_variant_get_type (children[0]);
765 array_type = g_variant_type_new_array (child_type);
767 for (i = 0; i < n_children; i++)
769 TYPE_CHECK (children[i], child_type, NULL);
770 my_children[i] = g_variant_ref_sink (children[i]);
771 trusted &= g_variant_is_trusted (children[i]);
774 value = g_variant_new_from_children (array_type, my_children,
775 n_children, trusted);
776 g_variant_type_free (array_type);
782 * g_variant_make_tuple_type:
783 * @children: (array length=n_children): an array of GVariant *
784 * @n_children: the length of @children
786 * Return the type of a tuple containing @children as its items.
788 static GVariantType *
789 g_variant_make_tuple_type (GVariant * const *children,
792 const GVariantType **types;
796 types = g_new (const GVariantType *, n_children);
798 for (i = 0; i < n_children; i++)
799 types[i] = g_variant_get_type (children[i]);
801 type = g_variant_type_new_tuple (types, n_children);
808 * g_variant_new_tuple:
809 * @children: (array length=n_children): the items to make the tuple out of
810 * @n_children: the length of @children
812 * Creates a new tuple #GVariant out of the items in @children. The
813 * type is determined from the types of @children. No entry in the
814 * @children array may be %NULL.
816 * If @n_children is 0 then the unit tuple is constructed.
818 * If the @children are floating references (see g_variant_ref_sink()), the
819 * new instance takes ownership of them as if via g_variant_ref_sink().
821 * Returns: (transfer none): a floating reference to a new #GVariant tuple
826 g_variant_new_tuple (GVariant * const *children,
829 GVariantType *tuple_type;
830 GVariant **my_children;
835 g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
837 my_children = g_new (GVariant *, n_children);
840 for (i = 0; i < n_children; i++)
842 my_children[i] = g_variant_ref_sink (children[i]);
843 trusted &= g_variant_is_trusted (children[i]);
846 tuple_type = g_variant_make_tuple_type (children, n_children);
847 value = g_variant_new_from_children (tuple_type, my_children,
848 n_children, trusted);
849 g_variant_type_free (tuple_type);
855 * g_variant_make_dict_entry_type:
856 * @key: a #GVariant, the key
857 * @val: a #GVariant, the value
859 * Return the type of a dictionary entry containing @key and @val as its
862 static GVariantType *
863 g_variant_make_dict_entry_type (GVariant *key,
866 return g_variant_type_new_dict_entry (g_variant_get_type (key),
867 g_variant_get_type (val));
871 * g_variant_new_dict_entry: (constructor)
872 * @key: a basic #GVariant, the key
873 * @value: a #GVariant, the value
875 * Creates a new dictionary entry #GVariant. @key and @value must be
876 * non-%NULL. @key must be a value of a basic type (ie: not a container).
878 * If the @key or @value are floating references (see g_variant_ref_sink()),
879 * the new instance takes ownership of them as if via g_variant_ref_sink().
881 * Returns: (transfer none): a floating reference to a new dictionary entry #GVariant
886 g_variant_new_dict_entry (GVariant *key,
889 GVariantType *dict_type;
893 g_return_val_if_fail (key != NULL && value != NULL, NULL);
894 g_return_val_if_fail (!g_variant_is_container (key), NULL);
896 children = g_new (GVariant *, 2);
897 children[0] = g_variant_ref_sink (key);
898 children[1] = g_variant_ref_sink (value);
899 trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
901 dict_type = g_variant_make_dict_entry_type (key, value);
902 value = g_variant_new_from_children (dict_type, children, 2, trusted);
903 g_variant_type_free (dict_type);
909 * g_variant_lookup: (skip)
910 * @dictionary: a dictionary #GVariant
911 * @key: the key to lookup in the dictionary
912 * @format_string: a GVariant format string
913 * @...: the arguments to unpack the value into
915 * Looks up a value in a dictionary #GVariant.
917 * This function is a wrapper around g_variant_lookup_value() and
918 * g_variant_get(). In the case that %NULL would have been returned,
919 * this function returns %FALSE. Otherwise, it unpacks the returned
920 * value and returns %TRUE.
922 * @format_string determines the C types that are used for unpacking
923 * the values and also determines if the values are copied or borrowed,
925 * <link linkend='gvariant-format-strings-pointers'>GVariant Format Strings</link>.
927 * This function is currently implemented with a linear scan. If you
928 * plan to do many lookups then #GVariantDict may be more efficient.
930 * Returns: %TRUE if a value was unpacked
935 g_variant_lookup (GVariant *dictionary,
937 const gchar *format_string,
944 g_variant_get_data (dictionary);
946 type = g_variant_format_string_scan_type (format_string, NULL, NULL);
947 value = g_variant_lookup_value (dictionary, key, type);
948 g_variant_type_free (type);
954 va_start (ap, format_string);
955 g_variant_get_va (value, format_string, NULL, &ap);
956 g_variant_unref (value);
967 * g_variant_lookup_value:
968 * @dictionary: a dictionary #GVariant
969 * @key: the key to lookup in the dictionary
970 * @expected_type: (allow-none): a #GVariantType, or %NULL
972 * Looks up a value in a dictionary #GVariant.
974 * This function works with dictionaries of the type a{s*} (and equally
975 * well with type a{o*}, but we only further discuss the string case
976 * for sake of clarity).
978 * In the event that @dictionary has the type a{sv}, the @expected_type
979 * string specifies what type of value is expected to be inside of the
980 * variant. If the value inside the variant has a different type then
981 * %NULL is returned. In the event that @dictionary has a value type other
982 * than v then @expected_type must directly match the key type and it is
983 * used to unpack the value directly or an error occurs.
985 * In either case, if @key is not found in @dictionary, %NULL is returned.
987 * If the key is found and the value has the correct type, it is
988 * returned. If @expected_type was specified then any non-%NULL return
989 * value will have this type.
991 * This function is currently implemented with a linear scan. If you
992 * plan to do many lookups then #GVariantDict may be more efficient.
994 * Returns: (transfer full): the value of the dictionary key, or %NULL
999 g_variant_lookup_value (GVariant *dictionary,
1001 const GVariantType *expected_type)
1007 g_return_val_if_fail (g_variant_is_of_type (dictionary,
1008 G_VARIANT_TYPE ("a{s*}")) ||
1009 g_variant_is_of_type (dictionary,
1010 G_VARIANT_TYPE ("a{o*}")),
1013 g_variant_iter_init (&iter, dictionary);
1015 while ((entry = g_variant_iter_next_value (&iter)))
1017 GVariant *entry_key;
1020 entry_key = g_variant_get_child_value (entry, 0);
1021 matches = strcmp (g_variant_get_string (entry_key, NULL), key) == 0;
1022 g_variant_unref (entry_key);
1027 g_variant_unref (entry);
1033 value = g_variant_get_child_value (entry, 1);
1034 g_variant_unref (entry);
1036 if (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT))
1040 tmp = g_variant_get_variant (value);
1041 g_variant_unref (value);
1043 if (expected_type && !g_variant_is_of_type (tmp, expected_type))
1045 g_variant_unref (tmp);
1052 g_return_val_if_fail (expected_type == NULL || value == NULL ||
1053 g_variant_is_of_type (value, expected_type), NULL);
1059 * g_variant_get_fixed_array:
1060 * @value: a #GVariant array with fixed-sized elements
1061 * @n_elements: (out): a pointer to the location to store the number of items
1062 * @element_size: the size of each element
1064 * Provides access to the serialised data for an array of fixed-sized
1067 * @value must be an array with fixed-sized elements. Numeric types are
1068 * fixed-size, as are tuples containing only other fixed-sized types.
1070 * @element_size must be the size of a single element in the array,
1071 * as given by the section on
1072 * <link linkend='gvariant-serialised-data-memory'>Serialised Data
1075 * In particular, arrays of these fixed-sized types can be interpreted
1076 * as an array of the given C type, with @element_size set to the size
1077 * the appropriate type:
1081 * <thead><row><entry>element type</entry> <entry>C type</entry></row></thead>
1083 * <row><entry>%G_VARIANT_TYPE_INT16 (etc.)</entry>
1084 * <entry>#gint16 (etc.)</entry></row>
1085 * <row><entry>%G_VARIANT_TYPE_BOOLEAN</entry>
1086 * <entry>#guchar (not #gboolean!)</entry></row>
1087 * <row><entry>%G_VARIANT_TYPE_BYTE</entry> <entry>#guchar</entry></row>
1088 * <row><entry>%G_VARIANT_TYPE_HANDLE</entry> <entry>#guint32</entry></row>
1089 * <row><entry>%G_VARIANT_TYPE_DOUBLE</entry> <entry>#gdouble</entry></row>
1094 * For example, if calling this function for an array of 32-bit integers,
1095 * you might say sizeof(gint32). This value isn't used except for the purpose
1096 * of a double-check that the form of the serialised data matches the caller's
1099 * @n_elements, which must be non-%NULL is set equal to the number of
1100 * items in the array.
1102 * Returns: (array length=n_elements) (transfer none): a pointer to
1108 g_variant_get_fixed_array (GVariant *value,
1112 GVariantTypeInfo *array_info;
1113 gsize array_element_size;
1117 TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
1119 g_return_val_if_fail (n_elements != NULL, NULL);
1120 g_return_val_if_fail (element_size > 0, NULL);
1122 array_info = g_variant_get_type_info (value);
1123 g_variant_type_info_query_element (array_info, NULL, &array_element_size);
1125 g_return_val_if_fail (array_element_size, NULL);
1127 if G_UNLIKELY (array_element_size != element_size)
1129 if (array_element_size)
1130 g_critical ("g_variant_get_fixed_array: assertion "
1131 "'g_variant_array_has_fixed_size (value, element_size)' "
1132 "failed: array size %"G_GSIZE_FORMAT" does not match "
1133 "given element_size %"G_GSIZE_FORMAT".",
1134 array_element_size, element_size);
1136 g_critical ("g_variant_get_fixed_array: assertion "
1137 "'g_variant_array_has_fixed_size (value, element_size)' "
1138 "failed: array does not have fixed size.");
1141 data = g_variant_get_data (value);
1142 size = g_variant_get_size (value);
1144 if (size % element_size)
1147 *n_elements = size / element_size;
1156 * g_variant_new_fixed_array:
1157 * @element_type: the #GVariantType of each element
1158 * @elements: a pointer to the fixed array of contiguous elements
1159 * @n_elements: the number of elements
1160 * @element_size: the size of each element
1162 * Provides access to the serialised data for an array of fixed-sized
1165 * @value must be an array with fixed-sized elements. Numeric types are
1166 * fixed-size as are tuples containing only other fixed-sized types.
1168 * @element_size must be the size of a single element in the array.
1169 * For example, if calling this function for an array of 32-bit integers,
1170 * you might say sizeof(gint32). This value isn't used except for the purpose
1171 * of a double-check that the form of the serialised data matches the caller's
1174 * @n_elements, which must be non-%NULL is set equal to the number of
1175 * items in the array.
1177 * Returns: (transfer none): a floating reference to a new array #GVariant instance
1182 g_variant_new_fixed_array (const GVariantType *element_type,
1183 gconstpointer elements,
1187 GVariantType *array_type;
1188 gsize array_element_size;
1189 GVariantTypeInfo *array_info;
1193 g_return_val_if_fail (g_variant_type_is_definite (element_type), NULL);
1194 g_return_val_if_fail (element_size > 0, NULL);
1196 array_type = g_variant_type_new_array (element_type);
1197 array_info = g_variant_type_info_get (array_type);
1198 g_variant_type_info_query_element (array_info, NULL, &array_element_size);
1199 if G_UNLIKELY (array_element_size != element_size)
1201 if (array_element_size)
1202 g_critical ("g_variant_new_fixed_array: array size %" G_GSIZE_FORMAT
1203 " does not match given element_size %" G_GSIZE_FORMAT ".",
1204 array_element_size, element_size);
1206 g_critical ("g_variant_get_fixed_array: array does not have fixed size.");
1210 data = g_memdup (elements, n_elements * element_size);
1211 value = g_variant_new_from_data (array_type, data,
1212 n_elements * element_size,
1213 FALSE, g_free, data);
1215 g_variant_type_free (array_type);
1216 g_variant_type_info_unref (array_info);
1221 /* String type constructor/getters/validation {{{1 */
1223 * g_variant_new_string:
1224 * @string: a normal utf8 nul-terminated string
1226 * Creates a string #GVariant with the contents of @string.
1228 * @string must be valid utf8.
1230 * Returns: (transfer none): a floating reference to a new string #GVariant instance
1235 g_variant_new_string (const gchar *string)
1237 g_return_val_if_fail (string != NULL, NULL);
1238 g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
1240 return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING,
1241 string, strlen (string) + 1);
1245 * g_variant_new_take_string: (skip)
1246 * @string: a normal utf8 nul-terminated string
1248 * Creates a string #GVariant with the contents of @string.
1250 * @string must be valid utf8.
1252 * This function consumes @string. g_free() will be called on @string
1253 * when it is no longer required.
1255 * You must not modify or access @string in any other way after passing
1256 * it to this function. It is even possible that @string is immediately
1259 * Returns: (transfer none): a floating reference to a new string
1260 * #GVariant instance
1265 g_variant_new_take_string (gchar *string)
1270 g_return_val_if_fail (string != NULL, NULL);
1271 g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
1273 bytes = g_bytes_new_take (string, strlen (string) + 1);
1274 value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE);
1275 g_bytes_unref (bytes);
1281 * g_variant_new_printf: (skip)
1282 * @format_string: a printf-style format string
1283 * @...: arguments for @format_string
1285 * Creates a string-type GVariant using printf formatting.
1287 * This is similar to calling g_strdup_printf() and then
1288 * g_variant_new_string() but it saves a temporary variable and an
1291 * Returns: (transfer none): a floating reference to a new string
1292 * #GVariant instance
1297 g_variant_new_printf (const gchar *format_string,
1305 g_return_val_if_fail (format_string != NULL, NULL);
1307 va_start (ap, format_string);
1308 string = g_strdup_vprintf (format_string, ap);
1311 bytes = g_bytes_new_take (string, strlen (string) + 1);
1312 value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE);
1313 g_bytes_unref (bytes);
1319 * g_variant_new_object_path:
1320 * @object_path: a normal C nul-terminated string
1322 * Creates a D-Bus object path #GVariant with the contents of @string.
1323 * @string must be a valid D-Bus object path. Use
1324 * g_variant_is_object_path() if you're not sure.
1326 * Returns: (transfer none): a floating reference to a new object path #GVariant instance
1331 g_variant_new_object_path (const gchar *object_path)
1333 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1335 return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH,
1336 object_path, strlen (object_path) + 1);
1340 * g_variant_is_object_path:
1341 * @string: a normal C nul-terminated string
1343 * Determines if a given string is a valid D-Bus object path. You
1344 * should ensure that a string is a valid D-Bus object path before
1345 * passing it to g_variant_new_object_path().
1347 * A valid object path starts with '/' followed by zero or more
1348 * sequences of characters separated by '/' characters. Each sequence
1349 * must contain only the characters "[A-Z][a-z][0-9]_". No sequence
1350 * (including the one following the final '/' character) may be empty.
1352 * Returns: %TRUE if @string is a D-Bus object path
1357 g_variant_is_object_path (const gchar *string)
1359 g_return_val_if_fail (string != NULL, FALSE);
1361 return g_variant_serialiser_is_object_path (string, strlen (string) + 1);
1365 * g_variant_new_signature:
1366 * @signature: a normal C nul-terminated string
1368 * Creates a D-Bus type signature #GVariant with the contents of
1369 * @string. @string must be a valid D-Bus type signature. Use
1370 * g_variant_is_signature() if you're not sure.
1372 * Returns: (transfer none): a floating reference to a new signature #GVariant instance
1377 g_variant_new_signature (const gchar *signature)
1379 g_return_val_if_fail (g_variant_is_signature (signature), NULL);
1381 return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE,
1382 signature, strlen (signature) + 1);
1386 * g_variant_is_signature:
1387 * @string: a normal C nul-terminated string
1389 * Determines if a given string is a valid D-Bus type signature. You
1390 * should ensure that a string is a valid D-Bus type signature before
1391 * passing it to g_variant_new_signature().
1393 * D-Bus type signatures consist of zero or more definite #GVariantType
1394 * strings in sequence.
1396 * Returns: %TRUE if @string is a D-Bus type signature
1401 g_variant_is_signature (const gchar *string)
1403 g_return_val_if_fail (string != NULL, FALSE);
1405 return g_variant_serialiser_is_signature (string, strlen (string) + 1);
1409 * g_variant_get_string:
1410 * @value: a string #GVariant instance
1411 * @length: (allow-none) (default 0) (out): a pointer to a #gsize,
1412 * to store the length
1414 * Returns the string value of a #GVariant instance with a string
1415 * type. This includes the types %G_VARIANT_TYPE_STRING,
1416 * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
1418 * The string will always be utf8 encoded.
1420 * If @length is non-%NULL then the length of the string (in bytes) is
1421 * returned there. For trusted values, this information is already
1422 * known. For untrusted values, a strlen() will be performed.
1424 * It is an error to call this function with a @value of any type
1425 * other than those three.
1427 * The return value remains valid as long as @value exists.
1429 * Returns: (transfer none): the constant string, utf8 encoded
1434 g_variant_get_string (GVariant *value,
1440 g_return_val_if_fail (value != NULL, NULL);
1441 g_return_val_if_fail (
1442 g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
1443 g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) ||
1444 g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL);
1446 data = g_variant_get_data (value);
1447 size = g_variant_get_size (value);
1449 if (!g_variant_is_trusted (value))
1451 switch (g_variant_classify (value))
1453 case G_VARIANT_CLASS_STRING:
1454 if (g_variant_serialiser_is_string (data, size))
1461 case G_VARIANT_CLASS_OBJECT_PATH:
1462 if (g_variant_serialiser_is_object_path (data, size))
1469 case G_VARIANT_CLASS_SIGNATURE:
1470 if (g_variant_serialiser_is_signature (data, size))
1478 g_assert_not_reached ();
1489 * g_variant_dup_string:
1490 * @value: a string #GVariant instance
1491 * @length: (out): a pointer to a #gsize, to store the length
1493 * Similar to g_variant_get_string() except that instead of returning
1494 * a constant string, the string is duplicated.
1496 * The string will always be utf8 encoded.
1498 * The return value must be freed using g_free().
1500 * Returns: (transfer full): a newly allocated string, utf8 encoded
1505 g_variant_dup_string (GVariant *value,
1508 return g_strdup (g_variant_get_string (value, length));
1512 * g_variant_new_strv:
1513 * @strv: (array length=length) (element-type utf8): an array of strings
1514 * @length: the length of @strv, or -1
1516 * Constructs an array of strings #GVariant from the given array of
1519 * If @length is -1 then @strv is %NULL-terminated.
1521 * Returns: (transfer none): a new floating #GVariant instance
1526 g_variant_new_strv (const gchar * const *strv,
1532 g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1535 length = g_strv_length ((gchar **) strv);
1537 strings = g_new (GVariant *, length);
1538 for (i = 0; i < length; i++)
1539 strings[i] = g_variant_ref_sink (g_variant_new_string (strv[i]));
1541 return g_variant_new_from_children (G_VARIANT_TYPE_STRING_ARRAY,
1542 strings, length, TRUE);
1546 * g_variant_get_strv:
1547 * @value: an array of strings #GVariant
1548 * @length: (out) (allow-none): the length of the result, or %NULL
1550 * Gets the contents of an array of strings #GVariant. This call
1551 * makes a shallow copy; the return result should be released with
1552 * g_free(), but the individual strings must not be modified.
1554 * If @length is non-%NULL then the number of elements in the result
1555 * is stored there. In any case, the resulting array will be
1558 * For an empty array, @length will be set to 0 and a pointer to a
1559 * %NULL pointer will be returned.
1561 * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings
1566 g_variant_get_strv (GVariant *value,
1573 TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL);
1575 g_variant_get_data (value);
1576 n = g_variant_n_children (value);
1577 strv = g_new (const gchar *, n + 1);
1579 for (i = 0; i < n; i++)
1583 string = g_variant_get_child_value (value, i);
1584 strv[i] = g_variant_get_string (string, NULL);
1585 g_variant_unref (string);
1596 * g_variant_dup_strv:
1597 * @value: an array of strings #GVariant
1598 * @length: (out) (allow-none): the length of the result, or %NULL
1600 * Gets the contents of an array of strings #GVariant. This call
1601 * makes a deep copy; the return result should be released with
1604 * If @length is non-%NULL then the number of elements in the result
1605 * is stored there. In any case, the resulting array will be
1608 * For an empty array, @length will be set to 0 and a pointer to a
1609 * %NULL pointer will be returned.
1611 * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1616 g_variant_dup_strv (GVariant *value,
1623 TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL);
1625 n = g_variant_n_children (value);
1626 strv = g_new (gchar *, n + 1);
1628 for (i = 0; i < n; i++)
1632 string = g_variant_get_child_value (value, i);
1633 strv[i] = g_variant_dup_string (string, NULL);
1634 g_variant_unref (string);
1645 * g_variant_new_objv:
1646 * @strv: (array length=length) (element-type utf8): an array of strings
1647 * @length: the length of @strv, or -1
1649 * Constructs an array of object paths #GVariant from the given array of
1652 * Each string must be a valid #GVariant object path; see
1653 * g_variant_is_object_path().
1655 * If @length is -1 then @strv is %NULL-terminated.
1657 * Returns: (transfer none): a new floating #GVariant instance
1662 g_variant_new_objv (const gchar * const *strv,
1668 g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1671 length = g_strv_length ((gchar **) strv);
1673 strings = g_new (GVariant *, length);
1674 for (i = 0; i < length; i++)
1675 strings[i] = g_variant_ref_sink (g_variant_new_object_path (strv[i]));
1677 return g_variant_new_from_children (G_VARIANT_TYPE_OBJECT_PATH_ARRAY,
1678 strings, length, TRUE);
1682 * g_variant_get_objv:
1683 * @value: an array of object paths #GVariant
1684 * @length: (out) (allow-none): the length of the result, or %NULL
1686 * Gets the contents of an array of object paths #GVariant. This call
1687 * makes a shallow copy; the return result should be released with
1688 * g_free(), but the individual strings must not be modified.
1690 * If @length is non-%NULL then the number of elements in the result
1691 * is stored there. In any case, the resulting array will be
1694 * For an empty array, @length will be set to 0 and a pointer to a
1695 * %NULL pointer will be returned.
1697 * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings
1702 g_variant_get_objv (GVariant *value,
1709 TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL);
1711 g_variant_get_data (value);
1712 n = g_variant_n_children (value);
1713 strv = g_new (const gchar *, n + 1);
1715 for (i = 0; i < n; i++)
1719 string = g_variant_get_child_value (value, i);
1720 strv[i] = g_variant_get_string (string, NULL);
1721 g_variant_unref (string);
1732 * g_variant_dup_objv:
1733 * @value: an array of object paths #GVariant
1734 * @length: (out) (allow-none): the length of the result, or %NULL
1736 * Gets the contents of an array of object paths #GVariant. This call
1737 * makes a deep copy; the return result should be released with
1740 * If @length is non-%NULL then the number of elements in the result
1741 * is stored there. In any case, the resulting array will be
1744 * For an empty array, @length will be set to 0 and a pointer to a
1745 * %NULL pointer will be returned.
1747 * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1752 g_variant_dup_objv (GVariant *value,
1759 TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL);
1761 n = g_variant_n_children (value);
1762 strv = g_new (gchar *, n + 1);
1764 for (i = 0; i < n; i++)
1768 string = g_variant_get_child_value (value, i);
1769 strv[i] = g_variant_dup_string (string, NULL);
1770 g_variant_unref (string);
1782 * g_variant_new_bytestring:
1783 * @string: (array zero-terminated=1) (element-type guint8): a normal
1784 * nul-terminated string in no particular encoding
1786 * Creates an array-of-bytes #GVariant with the contents of @string.
1787 * This function is just like g_variant_new_string() except that the
1788 * string need not be valid utf8.
1790 * The nul terminator character at the end of the string is stored in
1793 * Returns: (transfer none): a floating reference to a new bytestring #GVariant instance
1798 g_variant_new_bytestring (const gchar *string)
1800 g_return_val_if_fail (string != NULL, NULL);
1802 return g_variant_new_from_trusted (G_VARIANT_TYPE_BYTESTRING,
1803 string, strlen (string) + 1);
1807 * g_variant_get_bytestring:
1808 * @value: an array-of-bytes #GVariant instance
1810 * Returns the string value of a #GVariant instance with an
1811 * array-of-bytes type. The string has no particular encoding.
1813 * If the array does not end with a nul terminator character, the empty
1814 * string is returned. For this reason, you can always trust that a
1815 * non-%NULL nul-terminated string will be returned by this function.
1817 * If the array contains a nul terminator character somewhere other than
1818 * the last byte then the returned string is the string, up to the first
1819 * such nul character.
1821 * It is an error to call this function with a @value that is not an
1824 * The return value remains valid as long as @value exists.
1826 * Returns: (transfer none) (array zero-terminated=1) (element-type guint8):
1827 * the constant string
1832 g_variant_get_bytestring (GVariant *value)
1834 const gchar *string;
1837 TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING, NULL);
1839 /* Won't be NULL since this is an array type */
1840 string = g_variant_get_data (value);
1841 size = g_variant_get_size (value);
1843 if (size && string[size - 1] == '\0')
1850 * g_variant_dup_bytestring:
1851 * @value: an array-of-bytes #GVariant instance
1852 * @length: (out) (allow-none) (default NULL): a pointer to a #gsize, to store
1853 * the length (not including the nul terminator)
1855 * Similar to g_variant_get_bytestring() except that instead of
1856 * returning a constant string, the string is duplicated.
1858 * The return value must be freed using g_free().
1860 * Returns: (transfer full) (array zero-terminated=1 length=length) (element-type guint8):
1861 * a newly allocated string
1866 g_variant_dup_bytestring (GVariant *value,
1869 const gchar *original = g_variant_get_bytestring (value);
1872 /* don't crash in case get_bytestring() had an assert failure */
1873 if (original == NULL)
1876 size = strlen (original);
1881 return g_memdup (original, size + 1);
1885 * g_variant_new_bytestring_array:
1886 * @strv: (array length=length): an array of strings
1887 * @length: the length of @strv, or -1
1889 * Constructs an array of bytestring #GVariant from the given array of
1892 * If @length is -1 then @strv is %NULL-terminated.
1894 * Returns: (transfer none): a new floating #GVariant instance
1899 g_variant_new_bytestring_array (const gchar * const *strv,
1905 g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1908 length = g_strv_length ((gchar **) strv);
1910 strings = g_new (GVariant *, length);
1911 for (i = 0; i < length; i++)
1912 strings[i] = g_variant_ref_sink (g_variant_new_bytestring (strv[i]));
1914 return g_variant_new_from_children (G_VARIANT_TYPE_BYTESTRING_ARRAY,
1915 strings, length, TRUE);
1919 * g_variant_get_bytestring_array:
1920 * @value: an array of array of bytes #GVariant ('aay')
1921 * @length: (out) (allow-none): the length of the result, or %NULL
1923 * Gets the contents of an array of array of bytes #GVariant. This call
1924 * makes a shallow copy; the return result should be released with
1925 * g_free(), but the individual strings must not be modified.
1927 * If @length is non-%NULL then the number of elements in the result is
1928 * stored there. In any case, the resulting array will be
1931 * For an empty array, @length will be set to 0 and a pointer to a
1932 * %NULL pointer will be returned.
1934 * Returns: (array length=length) (transfer container): an array of constant strings
1939 g_variant_get_bytestring_array (GVariant *value,
1946 TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL);
1948 g_variant_get_data (value);
1949 n = g_variant_n_children (value);
1950 strv = g_new (const gchar *, n + 1);
1952 for (i = 0; i < n; i++)
1956 string = g_variant_get_child_value (value, i);
1957 strv[i] = g_variant_get_bytestring (string);
1958 g_variant_unref (string);
1969 * g_variant_dup_bytestring_array:
1970 * @value: an array of array of bytes #GVariant ('aay')
1971 * @length: (out) (allow-none): the length of the result, or %NULL
1973 * Gets the contents of an array of array of bytes #GVariant. This call
1974 * makes a deep copy; the return result should be released with
1977 * If @length is non-%NULL then the number of elements in the result is
1978 * stored there. In any case, the resulting array will be
1981 * For an empty array, @length will be set to 0 and a pointer to a
1982 * %NULL pointer will be returned.
1984 * Returns: (array length=length) (transfer full): an array of strings
1989 g_variant_dup_bytestring_array (GVariant *value,
1996 TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL);
1998 g_variant_get_data (value);
1999 n = g_variant_n_children (value);
2000 strv = g_new (gchar *, n + 1);
2002 for (i = 0; i < n; i++)
2006 string = g_variant_get_child_value (value, i);
2007 strv[i] = g_variant_dup_bytestring (string, NULL);
2008 g_variant_unref (string);
2018 /* Type checking and querying {{{1 */
2020 * g_variant_get_type:
2021 * @value: a #GVariant
2023 * Determines the type of @value.
2025 * The return value is valid for the lifetime of @value and must not
2028 * Returns: a #GVariantType
2032 const GVariantType *
2033 g_variant_get_type (GVariant *value)
2035 GVariantTypeInfo *type_info;
2037 g_return_val_if_fail (value != NULL, NULL);
2039 type_info = g_variant_get_type_info (value);
2041 return (GVariantType *) g_variant_type_info_get_type_string (type_info);
2045 * g_variant_get_type_string:
2046 * @value: a #GVariant
2048 * Returns the type string of @value. Unlike the result of calling
2049 * g_variant_type_peek_string(), this string is nul-terminated. This
2050 * string belongs to #GVariant and must not be freed.
2052 * Returns: the type string for the type of @value
2057 g_variant_get_type_string (GVariant *value)
2059 GVariantTypeInfo *type_info;
2061 g_return_val_if_fail (value != NULL, NULL);
2063 type_info = g_variant_get_type_info (value);
2065 return g_variant_type_info_get_type_string (type_info);
2069 * g_variant_is_of_type:
2070 * @value: a #GVariant instance
2071 * @type: a #GVariantType
2073 * Checks if a value has a type matching the provided type.
2075 * Returns: %TRUE if the type of @value matches @type
2080 g_variant_is_of_type (GVariant *value,
2081 const GVariantType *type)
2083 return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
2087 * g_variant_is_container:
2088 * @value: a #GVariant instance
2090 * Checks if @value is a container.
2092 * Returns: %TRUE if @value is a container
2097 g_variant_is_container (GVariant *value)
2099 return g_variant_type_is_container (g_variant_get_type (value));
2104 * g_variant_classify:
2105 * @value: a #GVariant
2107 * Classifies @value according to its top-level type.
2109 * Returns: the #GVariantClass of @value
2115 * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
2116 * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
2117 * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
2118 * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
2119 * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
2120 * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
2121 * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
2122 * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
2123 * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
2124 * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating
2126 * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
2127 * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a D-Bus object path
2129 * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a D-Bus signature string.
2130 * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
2131 * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
2132 * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
2133 * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
2134 * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
2136 * The range of possible top-level types of #GVariant instances.
2141 g_variant_classify (GVariant *value)
2143 g_return_val_if_fail (value != NULL, 0);
2145 return *g_variant_get_type_string (value);
2148 /* Pretty printer {{{1 */
2149 /* This function is not introspectable because if @string is NULL,
2150 @returns is (transfer full), otherwise it is (transfer none), which
2151 is not supported by GObjectIntrospection */
2153 * g_variant_print_string: (skip)
2154 * @value: a #GVariant
2155 * @string: (allow-none) (default NULL): a #GString, or %NULL
2156 * @type_annotate: %TRUE if type information should be included in
2159 * Behaves as g_variant_print(), but operates on a #GString.
2161 * If @string is non-%NULL then it is appended to and returned. Else,
2162 * a new empty #GString is allocated and it is returned.
2164 * Returns: a #GString containing the string
2169 g_variant_print_string (GVariant *value,
2171 gboolean type_annotate)
2173 if G_UNLIKELY (string == NULL)
2174 string = g_string_new (NULL);
2176 switch (g_variant_classify (value))
2178 case G_VARIANT_CLASS_MAYBE:
2180 g_string_append_printf (string, "@%s ",
2181 g_variant_get_type_string (value));
2183 if (g_variant_n_children (value))
2185 gchar *printed_child;
2190 * Consider the case of the type "mmi". In this case we could
2191 * write "just just 4", but "4" alone is totally unambiguous,
2192 * so we try to drop "just" where possible.
2194 * We have to be careful not to always drop "just", though,
2195 * since "nothing" needs to be distinguishable from "just
2196 * nothing". The case where we need to ensure we keep the
2197 * "just" is actually exactly the case where we have a nested
2200 * Instead of searching for that nested Nothing, we just print
2201 * the contained value into a separate string and see if we
2202 * end up with "nothing" at the end of it. If so, we need to
2203 * add "just" at our level.
2205 element = g_variant_get_child_value (value, 0);
2206 printed_child = g_variant_print (element, FALSE);
2207 g_variant_unref (element);
2209 if (g_str_has_suffix (printed_child, "nothing"))
2210 g_string_append (string, "just ");
2211 g_string_append (string, printed_child);
2212 g_free (printed_child);
2215 g_string_append (string, "nothing");
2219 case G_VARIANT_CLASS_ARRAY:
2220 /* it's an array so the first character of the type string is 'a'
2222 * if the first two characters are 'ay' then it's a bytestring.
2223 * under certain conditions we print those as strings.
2225 if (g_variant_get_type_string (value)[1] == 'y')
2231 /* first determine if it is a byte string.
2232 * that's when there's a single nul character: at the end.
2234 str = g_variant_get_data (value);
2235 size = g_variant_get_size (value);
2237 for (i = 0; i < size; i++)
2241 /* first nul byte is the last byte -> it's a byte string. */
2244 gchar *escaped = g_strescape (str, NULL);
2246 /* use double quotes only if a ' is in the string */
2247 if (strchr (str, '\''))
2248 g_string_append_printf (string, "b\"%s\"", escaped);
2250 g_string_append_printf (string, "b'%s'", escaped);
2257 /* fall through and handle normally... */;
2261 * if the first two characters are 'a{' then it's an array of
2262 * dictionary entries (ie: a dictionary) so we print that
2265 if (g_variant_get_type_string (value)[1] == '{')
2268 const gchar *comma = "";
2271 if ((n = g_variant_n_children (value)) == 0)
2274 g_string_append_printf (string, "@%s ",
2275 g_variant_get_type_string (value));
2276 g_string_append (string, "{}");
2280 g_string_append_c (string, '{');
2281 for (i = 0; i < n; i++)
2283 GVariant *entry, *key, *val;
2285 g_string_append (string, comma);
2288 entry = g_variant_get_child_value (value, i);
2289 key = g_variant_get_child_value (entry, 0);
2290 val = g_variant_get_child_value (entry, 1);
2291 g_variant_unref (entry);
2293 g_variant_print_string (key, string, type_annotate);
2294 g_variant_unref (key);
2295 g_string_append (string, ": ");
2296 g_variant_print_string (val, string, type_annotate);
2297 g_variant_unref (val);
2298 type_annotate = FALSE;
2300 g_string_append_c (string, '}');
2303 /* normal (non-dictionary) array */
2305 const gchar *comma = "";
2308 if ((n = g_variant_n_children (value)) == 0)
2311 g_string_append_printf (string, "@%s ",
2312 g_variant_get_type_string (value));
2313 g_string_append (string, "[]");
2317 g_string_append_c (string, '[');
2318 for (i = 0; i < n; i++)
2322 g_string_append (string, comma);
2325 element = g_variant_get_child_value (value, i);
2327 g_variant_print_string (element, string, type_annotate);
2328 g_variant_unref (element);
2329 type_annotate = FALSE;
2331 g_string_append_c (string, ']');
2336 case G_VARIANT_CLASS_TUPLE:
2340 n = g_variant_n_children (value);
2342 g_string_append_c (string, '(');
2343 for (i = 0; i < n; i++)
2347 element = g_variant_get_child_value (value, i);
2348 g_variant_print_string (element, string, type_annotate);
2349 g_string_append (string, ", ");
2350 g_variant_unref (element);
2353 /* for >1 item: remove final ", "
2354 * for 1 item: remove final " ", but leave the ","
2355 * for 0 items: there is only "(", so remove nothing
2357 g_string_truncate (string, string->len - (n > 0) - (n > 1));
2358 g_string_append_c (string, ')');
2362 case G_VARIANT_CLASS_DICT_ENTRY:
2366 g_string_append_c (string, '{');
2368 element = g_variant_get_child_value (value, 0);
2369 g_variant_print_string (element, string, type_annotate);
2370 g_variant_unref (element);
2372 g_string_append (string, ", ");
2374 element = g_variant_get_child_value (value, 1);
2375 g_variant_print_string (element, string, type_annotate);
2376 g_variant_unref (element);
2378 g_string_append_c (string, '}');
2382 case G_VARIANT_CLASS_VARIANT:
2384 GVariant *child = g_variant_get_variant (value);
2386 /* Always annotate types in nested variants, because they are
2387 * (by nature) of variable type.
2389 g_string_append_c (string, '<');
2390 g_variant_print_string (child, string, TRUE);
2391 g_string_append_c (string, '>');
2393 g_variant_unref (child);
2397 case G_VARIANT_CLASS_BOOLEAN:
2398 if (g_variant_get_boolean (value))
2399 g_string_append (string, "true");
2401 g_string_append (string, "false");
2404 case G_VARIANT_CLASS_STRING:
2406 const gchar *str = g_variant_get_string (value, NULL);
2407 gunichar quote = strchr (str, '\'') ? '"' : '\'';
2409 g_string_append_c (string, quote);
2413 gunichar c = g_utf8_get_char (str);
2415 if (c == quote || c == '\\')
2416 g_string_append_c (string, '\\');
2418 if (g_unichar_isprint (c))
2419 g_string_append_unichar (string, c);
2423 g_string_append_c (string, '\\');
2428 g_string_append_c (string, 'a');
2432 g_string_append_c (string, 'b');
2436 g_string_append_c (string, 'f');
2440 g_string_append_c (string, 'n');
2444 g_string_append_c (string, 'r');
2448 g_string_append_c (string, 't');
2452 g_string_append_c (string, 'v');
2456 g_string_append_printf (string, "u%04x", c);
2460 g_string_append_printf (string, "U%08x", c);
2463 str = g_utf8_next_char (str);
2466 g_string_append_c (string, quote);
2470 case G_VARIANT_CLASS_BYTE:
2472 g_string_append (string, "byte ");
2473 g_string_append_printf (string, "0x%02x",
2474 g_variant_get_byte (value));
2477 case G_VARIANT_CLASS_INT16:
2479 g_string_append (string, "int16 ");
2480 g_string_append_printf (string, "%"G_GINT16_FORMAT,
2481 g_variant_get_int16 (value));
2484 case G_VARIANT_CLASS_UINT16:
2486 g_string_append (string, "uint16 ");
2487 g_string_append_printf (string, "%"G_GUINT16_FORMAT,
2488 g_variant_get_uint16 (value));
2491 case G_VARIANT_CLASS_INT32:
2492 /* Never annotate this type because it is the default for numbers
2493 * (and this is a *pretty* printer)
2495 g_string_append_printf (string, "%"G_GINT32_FORMAT,
2496 g_variant_get_int32 (value));
2499 case G_VARIANT_CLASS_HANDLE:
2501 g_string_append (string, "handle ");
2502 g_string_append_printf (string, "%"G_GINT32_FORMAT,
2503 g_variant_get_handle (value));
2506 case G_VARIANT_CLASS_UINT32:
2508 g_string_append (string, "uint32 ");
2509 g_string_append_printf (string, "%"G_GUINT32_FORMAT,
2510 g_variant_get_uint32 (value));
2513 case G_VARIANT_CLASS_INT64:
2515 g_string_append (string, "int64 ");
2516 g_string_append_printf (string, "%"G_GINT64_FORMAT,
2517 g_variant_get_int64 (value));
2520 case G_VARIANT_CLASS_UINT64:
2522 g_string_append (string, "uint64 ");
2523 g_string_append_printf (string, "%"G_GUINT64_FORMAT,
2524 g_variant_get_uint64 (value));
2527 case G_VARIANT_CLASS_DOUBLE:
2532 g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
2534 for (i = 0; buffer[i]; i++)
2535 if (buffer[i] == '.' || buffer[i] == 'e' ||
2536 buffer[i] == 'n' || buffer[i] == 'N')
2539 /* if there is no '.' or 'e' in the float then add one */
2540 if (buffer[i] == '\0')
2547 g_string_append (string, buffer);
2551 case G_VARIANT_CLASS_OBJECT_PATH:
2553 g_string_append (string, "objectpath ");
2554 g_string_append_printf (string, "\'%s\'",
2555 g_variant_get_string (value, NULL));
2558 case G_VARIANT_CLASS_SIGNATURE:
2560 g_string_append (string, "signature ");
2561 g_string_append_printf (string, "\'%s\'",
2562 g_variant_get_string (value, NULL));
2566 g_assert_not_reached ();
2574 * @value: a #GVariant
2575 * @type_annotate: %TRUE if type information should be included in
2578 * Pretty-prints @value in the format understood by g_variant_parse().
2580 * The format is described <link linkend='gvariant-text'>here</link>.
2582 * If @type_annotate is %TRUE, then type information is included in
2585 * Returns: (transfer full): a newly-allocated string holding the result.
2590 g_variant_print (GVariant *value,
2591 gboolean type_annotate)
2593 return g_string_free (g_variant_print_string (value, NULL, type_annotate),
2597 /* Hash, Equal, Compare {{{1 */
2600 * @value: (type GVariant): a basic #GVariant value as a #gconstpointer
2602 * Generates a hash value for a #GVariant instance.
2604 * The output of this function is guaranteed to be the same for a given
2605 * value only per-process. It may change between different processor
2606 * architectures or even different versions of GLib. Do not use this
2607 * function as a basis for building protocols or file formats.
2609 * The type of @value is #gconstpointer only to allow use of this
2610 * function with #GHashTable. @value must be a #GVariant.
2612 * Returns: a hash value corresponding to @value
2617 g_variant_hash (gconstpointer value_)
2619 GVariant *value = (GVariant *) value_;
2621 switch (g_variant_classify (value))
2623 case G_VARIANT_CLASS_STRING:
2624 case G_VARIANT_CLASS_OBJECT_PATH:
2625 case G_VARIANT_CLASS_SIGNATURE:
2626 return g_str_hash (g_variant_get_string (value, NULL));
2628 case G_VARIANT_CLASS_BOOLEAN:
2629 /* this is a very odd thing to hash... */
2630 return g_variant_get_boolean (value);
2632 case G_VARIANT_CLASS_BYTE:
2633 return g_variant_get_byte (value);
2635 case G_VARIANT_CLASS_INT16:
2636 case G_VARIANT_CLASS_UINT16:
2640 ptr = g_variant_get_data (value);
2648 case G_VARIANT_CLASS_INT32:
2649 case G_VARIANT_CLASS_UINT32:
2650 case G_VARIANT_CLASS_HANDLE:
2654 ptr = g_variant_get_data (value);
2662 case G_VARIANT_CLASS_INT64:
2663 case G_VARIANT_CLASS_UINT64:
2664 case G_VARIANT_CLASS_DOUBLE:
2665 /* need a separate case for these guys because otherwise
2666 * performance could be quite bad on big endian systems
2671 ptr = g_variant_get_data (value);
2674 return ptr[0] + ptr[1];
2680 g_return_val_if_fail (!g_variant_is_container (value), 0);
2681 g_assert_not_reached ();
2687 * @one: (type GVariant): a #GVariant instance
2688 * @two: (type GVariant): a #GVariant instance
2690 * Checks if @one and @two have the same type and value.
2692 * The types of @one and @two are #gconstpointer only to allow use of
2693 * this function with #GHashTable. They must each be a #GVariant.
2695 * Returns: %TRUE if @one and @two are equal
2700 g_variant_equal (gconstpointer one,
2705 g_return_val_if_fail (one != NULL && two != NULL, FALSE);
2707 if (g_variant_get_type_info ((GVariant *) one) !=
2708 g_variant_get_type_info ((GVariant *) two))
2711 /* if both values are trusted to be in their canonical serialised form
2712 * then a simple memcmp() of their serialised data will answer the
2715 * if not, then this might generate a false negative (since it is
2716 * possible for two different byte sequences to represent the same
2717 * value). for now we solve this by pretty-printing both values and
2718 * comparing the result.
2720 if (g_variant_is_trusted ((GVariant *) one) &&
2721 g_variant_is_trusted ((GVariant *) two))
2723 gconstpointer data_one, data_two;
2724 gsize size_one, size_two;
2726 size_one = g_variant_get_size ((GVariant *) one);
2727 size_two = g_variant_get_size ((GVariant *) two);
2729 if (size_one != size_two)
2732 data_one = g_variant_get_data ((GVariant *) one);
2733 data_two = g_variant_get_data ((GVariant *) two);
2735 equal = memcmp (data_one, data_two, size_one) == 0;
2739 gchar *strone, *strtwo;
2741 strone = g_variant_print ((GVariant *) one, FALSE);
2742 strtwo = g_variant_print ((GVariant *) two, FALSE);
2743 equal = strcmp (strone, strtwo) == 0;
2752 * g_variant_compare:
2753 * @one: (type GVariant): a basic-typed #GVariant instance
2754 * @two: (type GVariant): a #GVariant instance of the same type
2756 * Compares @one and @two.
2758 * The types of @one and @two are #gconstpointer only to allow use of
2759 * this function with #GTree, #GPtrArray, etc. They must each be a
2762 * Comparison is only defined for basic types (ie: booleans, numbers,
2763 * strings). For booleans, %FALSE is less than %TRUE. Numbers are
2764 * ordered in the usual way. Strings are in ASCII lexographical order.
2766 * It is a programmer error to attempt to compare container values or
2767 * two values that have types that are not exactly equal. For example,
2768 * you cannot compare a 32-bit signed integer with a 32-bit unsigned
2769 * integer. Also note that this function is not particularly
2770 * well-behaved when it comes to comparison of doubles; in particular,
2771 * the handling of incomparable values (ie: NaN) is undefined.
2773 * If you only require an equality comparison, g_variant_equal() is more
2776 * Returns: negative value if a < b;
2778 * positive value if a > b.
2783 g_variant_compare (gconstpointer one,
2786 GVariant *a = (GVariant *) one;
2787 GVariant *b = (GVariant *) two;
2789 g_return_val_if_fail (g_variant_classify (a) == g_variant_classify (b), 0);
2791 switch (g_variant_classify (a))
2793 case G_VARIANT_CLASS_BOOLEAN:
2794 return g_variant_get_boolean (a) -
2795 g_variant_get_boolean (b);
2797 case G_VARIANT_CLASS_BYTE:
2798 return ((gint) g_variant_get_byte (a)) -
2799 ((gint) g_variant_get_byte (b));
2801 case G_VARIANT_CLASS_INT16:
2802 return ((gint) g_variant_get_int16 (a)) -
2803 ((gint) g_variant_get_int16 (b));
2805 case G_VARIANT_CLASS_UINT16:
2806 return ((gint) g_variant_get_uint16 (a)) -
2807 ((gint) g_variant_get_uint16 (b));
2809 case G_VARIANT_CLASS_INT32:
2811 gint32 a_val = g_variant_get_int32 (a);
2812 gint32 b_val = g_variant_get_int32 (b);
2814 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2817 case G_VARIANT_CLASS_UINT32:
2819 guint32 a_val = g_variant_get_uint32 (a);
2820 guint32 b_val = g_variant_get_uint32 (b);
2822 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2825 case G_VARIANT_CLASS_INT64:
2827 gint64 a_val = g_variant_get_int64 (a);
2828 gint64 b_val = g_variant_get_int64 (b);
2830 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2833 case G_VARIANT_CLASS_UINT64:
2835 guint64 a_val = g_variant_get_uint64 (a);
2836 guint64 b_val = g_variant_get_uint64 (b);
2838 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2841 case G_VARIANT_CLASS_DOUBLE:
2843 gdouble a_val = g_variant_get_double (a);
2844 gdouble b_val = g_variant_get_double (b);
2846 return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2849 case G_VARIANT_CLASS_STRING:
2850 case G_VARIANT_CLASS_OBJECT_PATH:
2851 case G_VARIANT_CLASS_SIGNATURE:
2852 return strcmp (g_variant_get_string (a, NULL),
2853 g_variant_get_string (b, NULL));
2856 g_return_val_if_fail (!g_variant_is_container (a), 0);
2857 g_assert_not_reached ();
2861 /* GVariantIter {{{1 */
2863 * GVariantIter: (skip)
2865 * #GVariantIter is an opaque data structure and can only be accessed
2866 * using the following functions.
2873 const gchar *loop_format;
2879 G_STATIC_ASSERT (sizeof (struct stack_iter) <= sizeof (GVariantIter));
2883 struct stack_iter iter;
2885 GVariant *value_ref;
2889 #define GVSI(i) ((struct stack_iter *) (i))
2890 #define GVHI(i) ((struct heap_iter *) (i))
2891 #define GVSI_MAGIC ((gsize) 3579507750u)
2892 #define GVHI_MAGIC ((gsize) 1450270775u)
2893 #define is_valid_iter(i) (i != NULL && \
2894 GVSI(i)->magic == GVSI_MAGIC)
2895 #define is_valid_heap_iter(i) (GVHI(i)->magic == GVHI_MAGIC && \
2899 * g_variant_iter_new:
2900 * @value: a container #GVariant
2902 * Creates a heap-allocated #GVariantIter for iterating over the items
2905 * Use g_variant_iter_free() to free the return value when you no longer
2908 * A reference is taken to @value and will be released only when
2909 * g_variant_iter_free() is called.
2911 * Returns: (transfer full): a new heap-allocated #GVariantIter
2916 g_variant_iter_new (GVariant *value)
2920 iter = (GVariantIter *) g_slice_new (struct heap_iter);
2921 GVHI(iter)->value_ref = g_variant_ref (value);
2922 GVHI(iter)->magic = GVHI_MAGIC;
2924 g_variant_iter_init (iter, value);
2930 * g_variant_iter_init: (skip)
2931 * @iter: a pointer to a #GVariantIter
2932 * @value: a container #GVariant
2934 * Initialises (without allocating) a #GVariantIter. @iter may be
2935 * completely uninitialised prior to this call; its old value is
2938 * The iterator remains valid for as long as @value exists, and need not
2939 * be freed in any way.
2941 * Returns: the number of items in @value
2946 g_variant_iter_init (GVariantIter *iter,
2949 GVSI(iter)->magic = GVSI_MAGIC;
2950 GVSI(iter)->value = value;
2951 GVSI(iter)->n = g_variant_n_children (value);
2953 GVSI(iter)->loop_format = NULL;
2955 return GVSI(iter)->n;
2959 * g_variant_iter_copy:
2960 * @iter: a #GVariantIter
2962 * Creates a new heap-allocated #GVariantIter to iterate over the
2963 * container that was being iterated over by @iter. Iteration begins on
2964 * the new iterator from the current position of the old iterator but
2965 * the two copies are independent past that point.
2967 * Use g_variant_iter_free() to free the return value when you no longer
2970 * A reference is taken to the container that @iter is iterating over
2971 * and will be releated only when g_variant_iter_free() is called.
2973 * Returns: (transfer full): a new heap-allocated #GVariantIter
2978 g_variant_iter_copy (GVariantIter *iter)
2982 g_return_val_if_fail (is_valid_iter (iter), 0);
2984 copy = g_variant_iter_new (GVSI(iter)->value);
2985 GVSI(copy)->i = GVSI(iter)->i;
2991 * g_variant_iter_n_children:
2992 * @iter: a #GVariantIter
2994 * Queries the number of child items in the container that we are
2995 * iterating over. This is the total number of items -- not the number
2996 * of items remaining.
2998 * This function might be useful for preallocation of arrays.
3000 * Returns: the number of children in the container
3005 g_variant_iter_n_children (GVariantIter *iter)
3007 g_return_val_if_fail (is_valid_iter (iter), 0);
3009 return GVSI(iter)->n;
3013 * g_variant_iter_free:
3014 * @iter: (transfer full): a heap-allocated #GVariantIter
3016 * Frees a heap-allocated #GVariantIter. Only call this function on
3017 * iterators that were returned by g_variant_iter_new() or
3018 * g_variant_iter_copy().
3023 g_variant_iter_free (GVariantIter *iter)
3025 g_return_if_fail (is_valid_heap_iter (iter));
3027 g_variant_unref (GVHI(iter)->value_ref);
3028 GVHI(iter)->magic = 0;
3030 g_slice_free (struct heap_iter, GVHI(iter));
3034 * g_variant_iter_next_value:
3035 * @iter: a #GVariantIter
3037 * Gets the next item in the container. If no more items remain then
3038 * %NULL is returned.
3040 * Use g_variant_unref() to drop your reference on the return value when
3041 * you no longer need it.
3043 * Here is an example for iterating with g_variant_iter_next_value():
3044 * |[<!-- language="C" -->
3045 * /* recursively iterate a container */
3047 * iterate_container_recursive (GVariant *container)
3049 * GVariantIter iter;
3052 * g_variant_iter_init (&iter, container);
3053 * while ((child = g_variant_iter_next_value (&iter)))
3055 * g_print ("type '%s'\n", g_variant_get_type_string (child));
3057 * if (g_variant_is_container (child))
3058 * iterate_container_recursive (child);
3060 * g_variant_unref (child);
3065 * Returns: (allow-none) (transfer full): a #GVariant, or %NULL
3070 g_variant_iter_next_value (GVariantIter *iter)
3072 g_return_val_if_fail (is_valid_iter (iter), FALSE);
3074 if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n)
3076 g_critical ("g_variant_iter_next_value: must not be called again "
3077 "after NULL has already been returned.");
3083 if (GVSI(iter)->i < GVSI(iter)->n)
3084 return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i);
3089 /* GVariantBuilder {{{1 */
3093 * A utility type for constructing container-type #GVariant instances.
3095 * This is an opaque structure and may only be accessed using the
3096 * following functions.
3098 * #GVariantBuilder is not threadsafe in any way. Do not attempt to
3099 * access it from more than one thread.
3102 struct stack_builder
3104 GVariantBuilder *parent;
3107 /* type constraint explicitly specified by 'type'.
3108 * for tuple types, this moves along as we add more items.
3110 const GVariantType *expected_type;
3112 /* type constraint implied by previous array item.
3114 const GVariantType *prev_item_type;
3116 /* constraints on the number of children. max = -1 for unlimited. */
3120 /* dynamically-growing pointer array */
3121 GVariant **children;
3122 gsize allocated_children;
3125 /* set to '1' if all items in the container will have the same type
3126 * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
3128 guint uniform_item_types : 1;
3130 /* set to '1' initially and changed to '0' if an untrusted value is
3138 G_STATIC_ASSERT (sizeof (struct stack_builder) <= sizeof (GVariantBuilder));
3142 GVariantBuilder builder;
3148 #define GVSB(b) ((struct stack_builder *) (b))
3149 #define GVHB(b) ((struct heap_builder *) (b))
3150 #define GVSB_MAGIC ((gsize) 1033660112u)
3151 #define GVHB_MAGIC ((gsize) 3087242682u)
3152 #define is_valid_builder(b) (b != NULL && \
3153 GVSB(b)->magic == GVSB_MAGIC)
3154 #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
3157 * g_variant_builder_new:
3158 * @type: a container type
3160 * Allocates and initialises a new #GVariantBuilder.
3162 * You should call g_variant_builder_unref() on the return value when it
3163 * is no longer needed. The memory will not be automatically freed by
3166 * In most cases it is easier to place a #GVariantBuilder directly on
3167 * the stack of the calling function and initialise it with
3168 * g_variant_builder_init().
3170 * Returns: (transfer full): a #GVariantBuilder
3175 g_variant_builder_new (const GVariantType *type)
3177 GVariantBuilder *builder;
3179 builder = (GVariantBuilder *) g_slice_new (struct heap_builder);
3180 g_variant_builder_init (builder, type);
3181 GVHB(builder)->magic = GVHB_MAGIC;
3182 GVHB(builder)->ref_count = 1;
3188 * g_variant_builder_unref:
3189 * @builder: (transfer full): a #GVariantBuilder allocated by g_variant_builder_new()
3191 * Decreases the reference count on @builder.
3193 * In the event that there are no more references, releases all memory
3194 * associated with the #GVariantBuilder.
3196 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3197 * things will happen.
3202 g_variant_builder_unref (GVariantBuilder *builder)
3204 g_return_if_fail (is_valid_heap_builder (builder));
3206 if (--GVHB(builder)->ref_count)
3209 g_variant_builder_clear (builder);
3210 GVHB(builder)->magic = 0;
3212 g_slice_free (struct heap_builder, GVHB(builder));
3216 * g_variant_builder_ref:
3217 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
3219 * Increases the reference count on @builder.
3221 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3222 * things will happen.
3224 * Returns: (transfer full): a new reference to @builder
3229 g_variant_builder_ref (GVariantBuilder *builder)
3231 g_return_val_if_fail (is_valid_heap_builder (builder), NULL);
3233 GVHB(builder)->ref_count++;
3239 * g_variant_builder_clear: (skip)
3240 * @builder: a #GVariantBuilder
3242 * Releases all memory associated with a #GVariantBuilder without
3243 * freeing the #GVariantBuilder structure itself.
3245 * It typically only makes sense to do this on a stack-allocated
3246 * #GVariantBuilder if you want to abort building the value part-way
3247 * through. This function need not be called if you call
3248 * g_variant_builder_end() and it also doesn't need to be called on
3249 * builders allocated with g_variant_builder_new (see
3250 * g_variant_builder_unref() for that).
3252 * This function leaves the #GVariantBuilder structure set to all-zeros.
3253 * It is valid to call this function on either an initialised
3254 * #GVariantBuilder or one that is set to all-zeros but it is not valid
3255 * to call this function on uninitialised memory.
3260 g_variant_builder_clear (GVariantBuilder *builder)
3264 if (GVSB(builder)->magic == 0)
3265 /* all-zeros case */
3268 g_return_if_fail (is_valid_builder (builder));
3270 g_variant_type_free (GVSB(builder)->type);
3272 for (i = 0; i < GVSB(builder)->offset; i++)
3273 g_variant_unref (GVSB(builder)->children[i]);
3275 g_free (GVSB(builder)->children);
3277 if (GVSB(builder)->parent)
3279 g_variant_builder_clear (GVSB(builder)->parent);
3280 g_slice_free (GVariantBuilder, GVSB(builder)->parent);
3283 memset (builder, 0, sizeof (GVariantBuilder));
3287 * g_variant_builder_init: (skip)
3288 * @builder: a #GVariantBuilder
3289 * @type: a container type
3291 * Initialises a #GVariantBuilder structure.
3293 * @type must be non-%NULL. It specifies the type of container to
3294 * construct. It can be an indefinite type such as
3295 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
3296 * Maybe, array, tuple, dictionary entry and variant-typed values may be
3299 * After the builder is initialised, values are added using
3300 * g_variant_builder_add_value() or g_variant_builder_add().
3302 * After all the child values are added, g_variant_builder_end() frees
3303 * the memory associated with the builder and returns the #GVariant that
3306 * This function completely ignores the previous contents of @builder.
3307 * On one hand this means that it is valid to pass in completely
3308 * uninitialised memory. On the other hand, this means that if you are
3309 * initialising over top of an existing #GVariantBuilder you need to
3310 * first call g_variant_builder_clear() in order to avoid leaking
3313 * You must not call g_variant_builder_ref() or
3314 * g_variant_builder_unref() on a #GVariantBuilder that was initialised
3315 * with this function. If you ever pass a reference to a
3316 * #GVariantBuilder outside of the control of your own code then you
3317 * should assume that the person receiving that reference may try to use
3318 * reference counting; you should use g_variant_builder_new() instead of
3324 g_variant_builder_init (GVariantBuilder *builder,
3325 const GVariantType *type)
3327 g_return_if_fail (type != NULL);
3328 g_return_if_fail (g_variant_type_is_container (type));
3330 memset (builder, 0, sizeof (GVariantBuilder));
3332 GVSB(builder)->type = g_variant_type_copy (type);
3333 GVSB(builder)->magic = GVSB_MAGIC;
3334 GVSB(builder)->trusted = TRUE;
3336 switch (*(const gchar *) type)
3338 case G_VARIANT_CLASS_VARIANT:
3339 GVSB(builder)->uniform_item_types = TRUE;
3340 GVSB(builder)->allocated_children = 1;
3341 GVSB(builder)->expected_type = NULL;
3342 GVSB(builder)->min_items = 1;
3343 GVSB(builder)->max_items = 1;
3346 case G_VARIANT_CLASS_ARRAY:
3347 GVSB(builder)->uniform_item_types = TRUE;
3348 GVSB(builder)->allocated_children = 8;
3349 GVSB(builder)->expected_type =
3350 g_variant_type_element (GVSB(builder)->type);
3351 GVSB(builder)->min_items = 0;
3352 GVSB(builder)->max_items = -1;
3355 case G_VARIANT_CLASS_MAYBE:
3356 GVSB(builder)->uniform_item_types = TRUE;
3357 GVSB(builder)->allocated_children = 1;
3358 GVSB(builder)->expected_type =
3359 g_variant_type_element (GVSB(builder)->type);
3360 GVSB(builder)->min_items = 0;
3361 GVSB(builder)->max_items = 1;
3364 case G_VARIANT_CLASS_DICT_ENTRY:
3365 GVSB(builder)->uniform_item_types = FALSE;
3366 GVSB(builder)->allocated_children = 2;
3367 GVSB(builder)->expected_type =
3368 g_variant_type_key (GVSB(builder)->type);
3369 GVSB(builder)->min_items = 2;
3370 GVSB(builder)->max_items = 2;
3373 case 'r': /* G_VARIANT_TYPE_TUPLE was given */
3374 GVSB(builder)->uniform_item_types = FALSE;
3375 GVSB(builder)->allocated_children = 8;
3376 GVSB(builder)->expected_type = NULL;
3377 GVSB(builder)->min_items = 0;
3378 GVSB(builder)->max_items = -1;
3381 case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */
3382 GVSB(builder)->allocated_children = g_variant_type_n_items (type);
3383 GVSB(builder)->expected_type =
3384 g_variant_type_first (GVSB(builder)->type);
3385 GVSB(builder)->min_items = GVSB(builder)->allocated_children;
3386 GVSB(builder)->max_items = GVSB(builder)->allocated_children;
3387 GVSB(builder)->uniform_item_types = FALSE;
3391 g_assert_not_reached ();
3394 GVSB(builder)->children = g_new (GVariant *,
3395 GVSB(builder)->allocated_children);
3399 g_variant_builder_make_room (struct stack_builder *builder)
3401 if (builder->offset == builder->allocated_children)
3403 builder->allocated_children *= 2;
3404 builder->children = g_renew (GVariant *, builder->children,
3405 builder->allocated_children);
3410 * g_variant_builder_add_value:
3411 * @builder: a #GVariantBuilder
3412 * @value: a #GVariant
3414 * Adds @value to @builder.
3416 * It is an error to call this function in any way that would create an
3417 * inconsistent value to be constructed. Some examples of this are
3418 * putting different types of items into an array, putting the wrong
3419 * types or number of items in a tuple, putting more than one value into
3422 * If @value is a floating reference (see g_variant_ref_sink()),
3423 * the @builder instance takes ownership of @value.
3428 g_variant_builder_add_value (GVariantBuilder *builder,
3431 g_return_if_fail (is_valid_builder (builder));
3432 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
3433 g_return_if_fail (!GVSB(builder)->expected_type ||
3434 g_variant_is_of_type (value,
3435 GVSB(builder)->expected_type));
3436 g_return_if_fail (!GVSB(builder)->prev_item_type ||
3437 g_variant_is_of_type (value,
3438 GVSB(builder)->prev_item_type));
3440 GVSB(builder)->trusted &= g_variant_is_trusted (value);
3442 if (!GVSB(builder)->uniform_item_types)
3444 /* advance our expected type pointers */
3445 if (GVSB(builder)->expected_type)
3446 GVSB(builder)->expected_type =
3447 g_variant_type_next (GVSB(builder)->expected_type);
3449 if (GVSB(builder)->prev_item_type)
3450 GVSB(builder)->prev_item_type =
3451 g_variant_type_next (GVSB(builder)->prev_item_type);
3454 GVSB(builder)->prev_item_type = g_variant_get_type (value);
3456 g_variant_builder_make_room (GVSB(builder));
3458 GVSB(builder)->children[GVSB(builder)->offset++] =
3459 g_variant_ref_sink (value);
3463 * g_variant_builder_open:
3464 * @builder: a #GVariantBuilder
3465 * @type: a #GVariantType
3467 * Opens a subcontainer inside the given @builder. When done adding
3468 * items to the subcontainer, g_variant_builder_close() must be called.
3470 * It is an error to call this function in any way that would cause an
3471 * inconsistent value to be constructed (ie: adding too many values or
3472 * a value of an incorrect type).
3477 g_variant_builder_open (GVariantBuilder *builder,
3478 const GVariantType *type)
3480 GVariantBuilder *parent;
3482 g_return_if_fail (is_valid_builder (builder));
3483 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
3484 g_return_if_fail (!GVSB(builder)->expected_type ||
3485 g_variant_type_is_subtype_of (type,
3486 GVSB(builder)->expected_type));
3487 g_return_if_fail (!GVSB(builder)->prev_item_type ||
3488 g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type,
3491 parent = g_slice_dup (GVariantBuilder, builder);
3492 g_variant_builder_init (builder, type);
3493 GVSB(builder)->parent = parent;
3495 /* push the prev_item_type down into the subcontainer */
3496 if (GVSB(parent)->prev_item_type)
3498 if (!GVSB(builder)->uniform_item_types)
3499 /* tuples and dict entries */
3500 GVSB(builder)->prev_item_type =
3501 g_variant_type_first (GVSB(parent)->prev_item_type);
3503 else if (!g_variant_type_is_variant (GVSB(builder)->type))
3504 /* maybes and arrays */
3505 GVSB(builder)->prev_item_type =
3506 g_variant_type_element (GVSB(parent)->prev_item_type);
3511 * g_variant_builder_close:
3512 * @builder: a #GVariantBuilder
3514 * Closes the subcontainer inside the given @builder that was opened by
3515 * the most recent call to g_variant_builder_open().
3517 * It is an error to call this function in any way that would create an
3518 * inconsistent value to be constructed (ie: too few values added to the
3524 g_variant_builder_close (GVariantBuilder *builder)
3526 GVariantBuilder *parent;
3528 g_return_if_fail (is_valid_builder (builder));
3529 g_return_if_fail (GVSB(builder)->parent != NULL);
3531 parent = GVSB(builder)->parent;
3532 GVSB(builder)->parent = NULL;
3534 g_variant_builder_add_value (parent, g_variant_builder_end (builder));
3537 g_slice_free (GVariantBuilder, parent);
3541 * g_variant_make_maybe_type:
3542 * @element: a #GVariant
3544 * Return the type of a maybe containing @element.
3546 static GVariantType *
3547 g_variant_make_maybe_type (GVariant *element)
3549 return g_variant_type_new_maybe (g_variant_get_type (element));
3553 * g_variant_make_array_type:
3554 * @element: a #GVariant
3556 * Return the type of an array containing @element.
3558 static GVariantType *
3559 g_variant_make_array_type (GVariant *element)
3561 return g_variant_type_new_array (g_variant_get_type (element));
3565 * g_variant_builder_end:
3566 * @builder: a #GVariantBuilder
3568 * Ends the builder process and returns the constructed value.
3570 * It is not permissible to use @builder in any way after this call
3571 * except for reference counting operations (in the case of a
3572 * heap-allocated #GVariantBuilder) or by reinitialising it with
3573 * g_variant_builder_init() (in the case of stack-allocated).
3575 * It is an error to call this function in any way that would create an
3576 * inconsistent value to be constructed (ie: insufficient number of
3577 * items added to a container with a specific number of children
3578 * required). It is also an error to call this function if the builder
3579 * was created with an indefinite array or maybe type and no children
3580 * have been added; in this case it is impossible to infer the type of
3583 * Returns: (transfer none): a new, floating, #GVariant
3588 g_variant_builder_end (GVariantBuilder *builder)
3590 GVariantType *my_type;
3593 g_return_val_if_fail (is_valid_builder (builder), NULL);
3594 g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items,
3596 g_return_val_if_fail (!GVSB(builder)->uniform_item_types ||
3597 GVSB(builder)->prev_item_type != NULL ||
3598 g_variant_type_is_definite (GVSB(builder)->type),
3601 if (g_variant_type_is_definite (GVSB(builder)->type))
3602 my_type = g_variant_type_copy (GVSB(builder)->type);
3604 else if (g_variant_type_is_maybe (GVSB(builder)->type))
3605 my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]);
3607 else if (g_variant_type_is_array (GVSB(builder)->type))
3608 my_type = g_variant_make_array_type (GVSB(builder)->children[0]);
3610 else if (g_variant_type_is_tuple (GVSB(builder)->type))
3611 my_type = g_variant_make_tuple_type (GVSB(builder)->children,
3612 GVSB(builder)->offset);
3614 else if (g_variant_type_is_dict_entry (GVSB(builder)->type))
3615 my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0],
3616 GVSB(builder)->children[1]);
3618 g_assert_not_reached ();
3620 value = g_variant_new_from_children (my_type,
3621 g_renew (GVariant *,
3622 GVSB(builder)->children,
3623 GVSB(builder)->offset),
3624 GVSB(builder)->offset,
3625 GVSB(builder)->trusted);
3626 GVSB(builder)->children = NULL;
3627 GVSB(builder)->offset = 0;
3629 g_variant_builder_clear (builder);
3630 g_variant_type_free (my_type);
3635 /* GVariantDict {{{1 */
3638 * GVariantDict: (skip)
3640 * #GVariantDict is a mutable interface to #GVariant dictionaries.
3642 * It can be used for doing a sequence of dictionary lookups in an
3643 * efficient way on an existing #GVariant dictionary or it can be used
3644 * to construct new dictionaries with a hashtable-like interface. It
3645 * can also be used for taking existing dictionaries and modifying them
3646 * in order to create new ones.
3648 * #GVariantDict can only be used with %G_VARIANT_TYPE_VARDICT
3651 * It is possible to use #GVariantDict allocated on the stack or on the
3652 * heap. When using a stack-allocated #GVariantDict, you begin with a
3653 * call to g_variant_dict_init() and free the resources with a call to
3654 * g_variant_dict_clear().
3656 * Heap-allocated #GVariantDict follows normal refcounting rules: you
3657 * allocate it with g_variant_dict_new() and use g_variant_dict_ref()
3658 * and g_variant_dict_unref().
3660 * g_variant_dict_end() is used to convert the #GVariantDict back into a
3661 * dictionary-type #GVariant. When used with stack-allocated instances,
3662 * this also implicitly frees all associated memory, but for
3663 * heap-allocated instances, you must still call g_variant_dict_unref()
3666 * You will typically want to use a heap-allocated #GVariantDict when
3667 * you expose it as part of an API. For most other uses, the
3668 * stack-allocated form will be more convenient.
3670 * Consider the following two examples that do the same thing in each
3671 * style: take an existing dictionary and look up the "count" uint32
3672 * key, adding 1 to it if it is found, or returning an error if the
3673 * key is not found. Each returns the new dictionary as a floating
3677 * <title>Using stack-allocated #GVariantDict</title>
3680 * add_to_count (GVariant *orig,
3683 * GVariantDict dict;
3686 * g_variant_dict_init (&dict, orig);
3687 * if (!g_variant_dict_lookup (&dict, "count", "u", &count))
3689 * g_set_error (...);
3690 * g_variant_dict_clear (&dict);
3694 * g_variant_dict_insert (&dict, "count", "u", count + 1);
3696 * return g_variant_dict_end (&dict);
3702 * <title>Using heap-allocated #GVariantDict</title>
3705 * add_to_count (GVariant *orig,
3708 * GVariantDict *dict;
3712 * dict = g_variant_dict_new (orig);
3714 * if (g_variant_dict_lookup (dict, "count", "u", &count))
3716 * g_variant_dict_insert (dict, "count", "u", count + 1);
3717 * result = g_variant_dict_end (dict);
3721 * g_set_error (...);
3725 * g_variant_dict_unref (dict);
3740 G_STATIC_ASSERT (sizeof (struct stack_dict) <= sizeof (GVariantDict));
3744 struct stack_dict dict;
3749 #define GVSD(d) ((struct stack_dict *) (d))
3750 #define GVHD(d) ((struct heap_dict *) (d))
3751 #define GVSD_MAGIC ((gsize) 2579507750u)
3752 #define GVHD_MAGIC ((gsize) 2450270775u)
3753 #define is_valid_dict(d) (d != NULL && \
3754 GVSD(d)->magic == GVSD_MAGIC)
3755 #define is_valid_heap_dict(d) (GVHD(d)->magic == GVHD_MAGIC)
3758 * g_variant_dict_new:
3759 * @from_asv: (allow-none): the #GVariant with which to initialise the
3762 * Allocates and initialises a new #GVariantDict.
3764 * You should call g_variant_dict_unref() on the return value when it
3765 * is no longer needed. The memory will not be automatically freed by
3768 * In some cases it may be easier to place a #GVariantDict directly on
3769 * the stack of the calling function and initialise it with
3770 * g_variant_dict_init(). This is particularly useful when you are
3771 * using #GVariantDict to construct a #GVariant.
3773 * Returns: (transfer full): a #GVariantDict
3778 g_variant_dict_new (GVariant *from_asv)
3782 dict = g_slice_alloc (sizeof (struct heap_dict));
3783 g_variant_dict_init (dict, from_asv);
3784 GVHD(dict)->magic = GVHD_MAGIC;
3785 GVHD(dict)->ref_count = 1;
3791 * g_variant_dict_init: (skip)
3792 * @dict: a #GVariantDict
3793 * @from_asv: (allow-none): the initial value for @dict
3795 * Initialises a #GVariantDict structure.
3797 * If @from_asv is given, it is used to initialise the dictionary.
3799 * This function completely ignores the previous contents of @dict. On
3800 * one hand this means that it is valid to pass in completely
3801 * uninitialised memory. On the other hand, this means that if you are
3802 * initialising over top of an existing #GVariantDict you need to first
3803 * call g_variant_dict_clear() in order to avoid leaking memory.
3805 * You must not call g_variant_dict_ref() or g_variant_dict_unref() on a
3806 * #GVariantDict that was initialised with this function. If you ever
3807 * pass a reference to a #GVariantDict outside of the control of your
3808 * own code then you should assume that the person receiving that
3809 * reference may try to use reference counting; you should use
3810 * g_variant_dict_new() instead of this function.
3815 g_variant_dict_init (GVariantDict *dict,
3822 GVSD(dict)->values = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
3823 GVSD(dict)->magic = GVSD_MAGIC;
3827 g_variant_iter_init (&iter, from_asv);
3828 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
3829 g_hash_table_insert (GVSD(dict)->values, key, value);
3834 * g_variant_dict_lookup:
3835 * @dict: a #GVariantDict
3836 * @key: the key to lookup in the dictionary
3837 * @format_string: a GVariant format string
3838 * @...: the arguments to unpack the value into
3840 * Looks up a value in a #GVariantDict.
3842 * This function is a wrapper around g_variant_dict_lookup_value() and
3843 * g_variant_get(). In the case that %NULL would have been returned,
3844 * this function returns %FALSE. Otherwise, it unpacks the returned
3845 * value and returns %TRUE.
3847 * @format_string determines the C types that are used for unpacking
3848 * the values and also determines if the values are copied or borrowed,
3849 * see the section on
3850 * <link linkend='gvariant-format-strings-pointers'>GVariant Format Strings</link>.
3852 * Returns: %TRUE if a value was unpacked
3857 g_variant_dict_lookup (GVariantDict *dict,
3859 const gchar *format_string,
3865 g_return_val_if_fail (is_valid_dict (dict), FALSE);
3866 g_return_val_if_fail (key != NULL, FALSE);
3867 g_return_val_if_fail (format_string != NULL, FALSE);
3869 value = g_hash_table_lookup (GVSD(dict)->values, key);
3871 if (value == NULL || !g_variant_check_format_string (value, format_string, FALSE))
3874 va_start (ap, format_string);
3875 g_variant_get_va (value, format_string, NULL, &ap);
3882 * g_variant_dict_lookup_value:
3883 * @dict: a #GVariantDict
3884 * @key: the key to lookup in the dictionary
3885 * @expected_type: (allow-none): a #GVariantType, or %NULL
3887 * Looks up a value in a #GVariantDict.
3889 * If @key is not found in @dictionary, %NULL is returned.
3891 * The @expected_type string specifies what type of value is expected.
3892 * If the value associated with @key has a different type then %NULL is
3895 * If the key is found and the value has the correct type, it is
3896 * returned. If @expected_type was specified then any non-%NULL return
3897 * value will have this type.
3899 * Returns: (transfer full): the value of the dictionary key, or %NULL
3904 g_variant_dict_lookup_value (GVariantDict *dict,
3906 const GVariantType *expected_type)
3910 g_return_val_if_fail (is_valid_dict (dict), NULL);
3911 g_return_val_if_fail (key != NULL, NULL);
3913 result = g_hash_table_lookup (GVSD(dict)->values, key);
3915 if (result && (!expected_type || g_variant_is_of_type (result, expected_type)))
3916 return g_variant_ref (result);
3922 * g_variant_dict_contains:
3923 * @dict: a #GVariantDict
3924 * @key: the key to lookup in the dictionary
3926 * Checks if @key exists in @dict.
3928 * Returns: %TRUE if @key is in @dict
3933 g_variant_dict_contains (GVariantDict *dict,
3936 g_return_val_if_fail (is_valid_dict (dict), FALSE);
3937 g_return_val_if_fail (key != NULL, FALSE);
3939 return g_hash_table_contains (GVSD(dict)->values, key);
3943 * g_variant_dict_insert:
3944 * @dict: a #GVariantDict
3945 * @key: the key to insert a value for
3946 * @format_string: a #GVariant varargs format string
3947 * @...: arguments, as per @format_string
3949 * Inserts a value into a #GVariantDict.
3951 * This call is a convenience wrapper that is exactly equivalent to
3952 * calling g_variant_new() followed by g_variant_dict_insert_value().
3957 g_variant_dict_insert (GVariantDict *dict,
3959 const gchar *format_string,
3964 g_return_if_fail (is_valid_dict (dict));
3965 g_return_if_fail (key != NULL);
3966 g_return_if_fail (format_string != NULL);
3968 va_start (ap, format_string);
3969 g_variant_dict_insert_value (dict, key, g_variant_new_va (format_string, NULL, &ap));
3974 * g_variant_dict_insert_value:
3975 * @dict: a #GVariantDict
3976 * @key: the key to insert a value for
3977 * @value: the value to insert
3979 * Inserts (or replaces) a key in a #GVariantDict.
3981 * @value is consumed if it is floating.
3986 g_variant_dict_insert_value (GVariantDict *dict,
3990 g_return_if_fail (is_valid_dict (dict));
3991 g_return_if_fail (key != NULL);
3992 g_return_if_fail (value != NULL);
3994 g_hash_table_insert (GVSD(dict)->values, g_strdup (key), g_variant_ref_sink (value));
3998 * g_variant_dict_remove:
3999 * @dict: a #GVariantDict
4000 * @key: the key to remove
4002 * Removes a key and its associated value from a #GVariantDict.
4004 * Returns: %TRUE if the key was found and removed
4009 g_variant_dict_remove (GVariantDict *dict,
4012 g_return_val_if_fail (is_valid_dict (dict), FALSE);
4013 g_return_val_if_fail (key != NULL, FALSE);
4015 return g_hash_table_remove (GVSD(dict)->values, key);
4019 * g_variant_dict_clear:
4020 * @dict: a #GVariantDict
4022 * Releases all memory associated with a #GVariantDict without freeing
4023 * the #GVariantDict structure itself.
4025 * It typically only makes sense to do this on a stack-allocated
4026 * #GVariantDict if you want to abort building the value part-way
4027 * through. This function need not be called if you call
4028 * g_variant_dict_end() and it also doesn't need to be called on dicts
4029 * allocated with g_variant_dict_new (see g_variant_dict_unref() for
4032 * It is valid to call this function on either an initialised
4033 * #GVariantDict or one that was previously cleared by an earlier call
4034 * to g_variant_dict_clear() but it is not valid to call this function
4035 * on uninitialised memory.
4040 g_variant_dict_clear (GVariantDict *dict)
4042 if (GVSD(dict)->magic == 0)
4043 /* all-zeros case */
4046 g_return_if_fail (is_valid_dict (dict));
4048 g_hash_table_unref (GVSD(dict)->values);
4049 GVSD(dict)->values = NULL;
4051 GVSD(dict)->magic = 0;
4055 * g_variant_dict_end:
4056 * @dict: a #GVariantDict
4058 * Returns the current value of @dict as a #GVariant of type
4059 * %G_VARIANT_TYPE_VARDICT, clearing it in the process.
4061 * It is not permissible to use @dict in any way after this call except
4062 * for reference counting operations (in the case of a heap-allocated
4063 * #GVariantDict) or by reinitialising it with g_variant_dict_init() (in
4064 * the case of stack-allocated).
4066 * Returns: (transfer none): a new, floating, #GVariant
4071 g_variant_dict_end (GVariantDict *dict)
4073 GVariantBuilder builder;
4074 GHashTableIter iter;
4075 gpointer key, value;
4077 g_return_val_if_fail (is_valid_dict (dict), NULL);
4079 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
4081 g_hash_table_iter_init (&iter, GVSD(dict)->values);
4082 while (g_hash_table_iter_next (&iter, &key, &value))
4083 g_variant_builder_add (&builder, "{sv}", (const gchar *) key, (GVariant *) value);
4085 g_variant_dict_clear (dict);
4087 return g_variant_builder_end (&builder);
4091 * g_variant_dict_ref:
4092 * @dict: a heap-allocated #GVariantDict
4094 * Increases the reference count on @dict.
4096 * Don't call this on stack-allocated #GVariantDict instances or bad
4097 * things will happen.
4099 * Returns: (transfer full): a new reference to @dict
4104 g_variant_dict_ref (GVariantDict *dict)
4106 g_return_val_if_fail (is_valid_heap_dict (dict), NULL);
4108 GVHD(dict)->ref_count++;
4114 * g_variant_dict_unref:
4115 * @dict: (transfer full): a heap-allocated #GVariantDict
4117 * Decreases the reference count on @dict.
4119 * In the event that there are no more references, releases all memory
4120 * associated with the #GVariantDict.
4122 * Don't call this on stack-allocated #GVariantDict instances or bad
4123 * things will happen.
4128 g_variant_dict_unref (GVariantDict *dict)
4130 g_return_if_fail (is_valid_heap_dict (dict));
4132 if (--GVHD(dict)->ref_count == 0)
4134 g_variant_dict_clear (dict);
4135 g_slice_free (struct heap_dict, (struct heap_dict *) dict);
4140 /* Format strings {{{1 */
4142 * g_variant_format_string_scan:
4143 * @string: a string that may be prefixed with a format string
4144 * @limit: (allow-none) (default NULL): a pointer to the end of @string,
4146 * @endptr: (allow-none) (default NULL): location to store the end pointer,
4149 * Checks the string pointed to by @string for starting with a properly
4150 * formed #GVariant varargs format string. If no valid format string is
4151 * found then %FALSE is returned.
4153 * If @string does start with a valid format string then %TRUE is
4154 * returned. If @endptr is non-%NULL then it is updated to point to the
4155 * first character after the format string.
4157 * If @limit is non-%NULL then @limit (and any charater after it) will
4158 * not be accessed and the effect is otherwise equivalent to if the
4159 * character at @limit were nul.
4161 * See the section on <link linkend='gvariant-format-strings'>GVariant
4162 * Format Strings</link>.
4164 * Returns: %TRUE if there was a valid format string
4169 g_variant_format_string_scan (const gchar *string,
4171 const gchar **endptr)
4173 #define next_char() (string == limit ? '\0' : *string++)
4174 #define peek_char() (string == limit ? '\0' : *string)
4177 switch (next_char())
4179 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
4180 case 'x': case 't': case 'h': case 'd': case 's': case 'o':
4181 case 'g': case 'v': case '*': case '?': case 'r':
4185 return g_variant_format_string_scan (string, limit, endptr);
4189 return g_variant_type_string_scan (string, limit, endptr);
4192 while (peek_char() != ')')
4193 if (!g_variant_format_string_scan (string, limit, &string))
4196 next_char(); /* consume ')' */
4206 if (c != 's' && c != 'o' && c != 'g')
4214 /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
4215 * The terminating null character is considered to be
4216 * part of the string.
4218 if (c != '\0' && strchr ("bynqiuxthdsog?", c) == NULL)
4222 if (!g_variant_format_string_scan (string, limit, &string))
4225 if (next_char() != '}')
4231 if ((c = next_char()) == 'a')
4233 if ((c = next_char()) == '&')
4235 if ((c = next_char()) == 'a')
4237 if ((c = next_char()) == 'y')
4238 break; /* '^a&ay' */
4241 else if (c == 's' || c == 'o')
4242 break; /* '^a&s', '^a&o' */
4247 if ((c = next_char()) == 'y')
4251 else if (c == 's' || c == 'o')
4252 break; /* '^as', '^ao' */
4259 if ((c = next_char()) == 'a')
4261 if ((c = next_char()) == 'y')
4271 if (c != 's' && c != 'o' && c != 'g')
4290 * g_variant_check_format_string:
4291 * @value: a #GVariant
4292 * @format_string: a valid #GVariant format string
4293 * @copy_only: %TRUE to ensure the format string makes deep copies
4295 * Checks if calling g_variant_get() with @format_string on @value would
4296 * be valid from a type-compatibility standpoint. @format_string is
4297 * assumed to be a valid format string (from a syntactic standpoint).
4299 * If @copy_only is %TRUE then this function additionally checks that it
4300 * would be safe to call g_variant_unref() on @value immediately after
4301 * the call to g_variant_get() without invalidating the result. This is
4302 * only possible if deep copies are made (ie: there are no pointers to
4303 * the data inside of the soon-to-be-freed #GVariant instance). If this
4304 * check fails then a g_critical() is printed and %FALSE is returned.
4306 * This function is meant to be used by functions that wish to provide
4307 * varargs accessors to #GVariant values of uncertain values (eg:
4308 * g_variant_lookup() or g_menu_model_get_item_attribute()).
4310 * Returns: %TRUE if @format_string is safe to use
4315 g_variant_check_format_string (GVariant *value,
4316 const gchar *format_string,
4319 const gchar *original_format = format_string;
4320 const gchar *type_string;
4322 /* Interesting factoid: assuming a format string is valid, it can be
4323 * converted to a type string by removing all '@' '&' and '^'
4326 * Instead of doing that, we can just skip those characters when
4327 * comparing it to the type string of @value.
4329 * For the copy-only case we can just drop the '&' from the list of
4330 * characters to skip over. A '&' will never appear in a type string
4331 * so we know that it won't be possible to return %TRUE if it is in a
4334 type_string = g_variant_get_type_string (value);
4336 while (*type_string || *format_string)
4338 gchar format = *format_string++;
4343 if G_UNLIKELY (copy_only)
4345 /* for the love of all that is good, please don't mark this string for translation... */
4346 g_critical ("g_variant_check_format_string() is being called by a function with a GVariant varargs "
4347 "interface to validate the passed format string for type safety. The passed format "
4348 "(%s) contains a '&' character which would result in a pointer being returned to the "
4349 "data inside of a GVariant instance that may no longer exist by the time the function "
4350 "returns. Modify your code to use a format string without '&'.", original_format);
4357 /* ignore these 2 (or 3) */
4361 /* attempt to consume one of 'bynqiuxthdsog' */
4363 char s = *type_string++;
4365 if (s == '\0' || strchr ("bynqiuxthdsog", s) == NULL)
4371 /* ensure it's a tuple */
4372 if (*type_string != '(')
4377 /* consume a full type string for the '*' or 'r' */
4378 if (!g_variant_type_string_scan (type_string, NULL, &type_string))
4384 /* attempt to consume exactly one character equal to the format */
4385 if (format != *type_string++)
4394 * g_variant_format_string_scan_type:
4395 * @string: a string that may be prefixed with a format string
4396 * @limit: (allow-none) (default NULL): a pointer to the end of @string,
4398 * @endptr: (allow-none) (default NULL): location to store the end pointer,
4401 * If @string starts with a valid format string then this function will
4402 * return the type that the format string corresponds to. Otherwise
4403 * this function returns %NULL.
4405 * Use g_variant_type_free() to free the return value when you no longer
4408 * This function is otherwise exactly like
4409 * g_variant_format_string_scan().
4411 * Returns: (allow-none): a #GVariantType if there was a valid format string
4416 g_variant_format_string_scan_type (const gchar *string,
4418 const gchar **endptr)
4420 const gchar *my_end;
4427 if (!g_variant_format_string_scan (string, limit, endptr))
4430 dest = new = g_malloc (*endptr - string + 1);
4431 while (string != *endptr)
4433 if (*string != '@' && *string != '&' && *string != '^')
4439 return (GVariantType *) G_VARIANT_TYPE (new);
4443 valid_format_string (const gchar *format_string,
4447 const gchar *endptr;
4450 type = g_variant_format_string_scan_type (format_string, NULL, &endptr);
4452 if G_UNLIKELY (type == NULL || (single && *endptr != '\0'))
4455 g_critical ("'%s' is not a valid GVariant format string",
4458 g_critical ("'%s' does not have a valid GVariant format "
4459 "string as a prefix", format_string);
4462 g_variant_type_free (type);
4467 if G_UNLIKELY (value && !g_variant_is_of_type (value, type))
4472 fragment = g_strndup (format_string, endptr - format_string);
4473 typestr = g_variant_type_dup_string (type);
4475 g_critical ("the GVariant format string '%s' has a type of "
4476 "'%s' but the given value has a type of '%s'",
4477 fragment, typestr, g_variant_get_type_string (value));
4479 g_variant_type_free (type);
4486 g_variant_type_free (type);
4491 /* Variable Arguments {{{1 */
4492 /* We consider 2 main classes of format strings:
4494 * - recursive format strings
4495 * these are ones that result in recursion and the collection of
4496 * possibly more than one argument. Maybe types, tuples,
4497 * dictionary entries.
4499 * - leaf format string
4500 * these result in the collection of a single argument.
4502 * Leaf format strings are further subdivided into two categories:
4504 * - single non-null pointer ("nnp")
4505 * these either collect or return a single non-null pointer.
4508 * these collect or return something else (bool, number, etc).
4510 * Based on the above, the varargs handling code is split into 4 main parts:
4512 * - nnp handling code
4513 * - leaf handling code (which may invoke nnp code)
4514 * - generic handling code (may be recursive, may invoke leaf code)
4515 * - user-facing API (which invokes the generic code)
4517 * Each section implements some of the following functions:
4520 * collect the arguments for the format string as if
4521 * g_variant_new() had been called, but do nothing with them. used
4522 * for skipping over arguments when constructing a Nothing maybe
4526 * create a GVariant *
4529 * unpack a GVariant *
4531 * - free (nnp only):
4532 * free a previously allocated item
4536 g_variant_format_string_is_leaf (const gchar *str)
4538 return str[0] != 'm' && str[0] != '(' && str[0] != '{';
4542 g_variant_format_string_is_nnp (const gchar *str)
4544 return str[0] == 'a' || str[0] == 's' || str[0] == 'o' || str[0] == 'g' ||
4545 str[0] == '^' || str[0] == '@' || str[0] == '*' || str[0] == '?' ||
4546 str[0] == 'r' || str[0] == 'v' || str[0] == '&';
4549 /* Single non-null pointer ("nnp") {{{2 */
4551 g_variant_valist_free_nnp (const gchar *str,
4557 g_variant_iter_free (ptr);
4561 if (str[2] != '&') /* '^as', '^ao' */
4563 else /* '^a&s', '^a&o' */
4577 g_variant_unref (ptr);
4584 g_assert_not_reached ();
4589 g_variant_scan_convenience (const gchar **str,
4612 g_variant_valist_new_nnp (const gchar **str,
4623 const GVariantType *type;
4626 value = g_variant_builder_end (ptr);
4627 type = g_variant_get_type (value);
4629 if G_UNLIKELY (!g_variant_type_is_array (type))
4630 g_error ("g_variant_new: expected array GVariantBuilder but "
4631 "the built value has type '%s'",
4632 g_variant_get_type_string (value));
4634 type = g_variant_type_element (type);
4636 if G_UNLIKELY (!g_variant_type_is_subtype_of (type, (GVariantType *) *str))
4637 g_error ("g_variant_new: expected GVariantBuilder array element "
4638 "type '%s' but the built value has element type '%s'",
4639 g_variant_type_dup_string ((GVariantType *) *str),
4640 g_variant_get_type_string (value) + 1);
4642 g_variant_type_string_scan (*str, NULL, str);
4648 /* special case: NULL pointer for empty array */
4650 const GVariantType *type = (GVariantType *) *str;
4652 g_variant_type_string_scan (*str, NULL, str);
4654 if G_UNLIKELY (!g_variant_type_is_definite (type))
4655 g_error ("g_variant_new: NULL pointer given with indefinite "
4656 "array type; unable to determine which type of empty "
4657 "array to construct.");
4659 return g_variant_new_array (type, NULL, 0);
4666 value = g_variant_new_string (ptr);
4669 value = g_variant_new_string ("[Invalid UTF-8]");
4675 return g_variant_new_object_path (ptr);
4678 return g_variant_new_signature (ptr);
4686 type = g_variant_scan_convenience (str, &constant, &arrays);
4689 return g_variant_new_strv (ptr, -1);
4692 return g_variant_new_objv (ptr, -1);
4695 return g_variant_new_bytestring_array (ptr, -1);
4697 return g_variant_new_bytestring (ptr);
4701 if G_UNLIKELY (!g_variant_is_of_type (ptr, (GVariantType *) *str))
4702 g_error ("g_variant_new: expected GVariant of type '%s' but "
4703 "received value has type '%s'",
4704 g_variant_type_dup_string ((GVariantType *) *str),
4705 g_variant_get_type_string (ptr));
4707 g_variant_type_string_scan (*str, NULL, str);
4715 if G_UNLIKELY (!g_variant_type_is_basic (g_variant_get_type (ptr)))
4716 g_error ("g_variant_new: format string '?' expects basic-typed "
4717 "GVariant, but received value has type '%s'",
4718 g_variant_get_type_string (ptr));
4723 if G_UNLIKELY (!g_variant_type_is_tuple (g_variant_get_type (ptr)))
4724 g_error ("g_variant_new: format string 'r' expects tuple-typed "
4725 "GVariant, but received value has type '%s'",
4726 g_variant_get_type_string (ptr));
4731 return g_variant_new_variant (ptr);
4734 g_assert_not_reached ();
4739 g_variant_valist_get_nnp (const gchar **str,
4745 g_variant_type_string_scan (*str, NULL, str);
4746 return g_variant_iter_new (value);
4750 return (gchar *) g_variant_get_string (value, NULL);
4755 return g_variant_dup_string (value, NULL);
4763 type = g_variant_scan_convenience (str, &constant, &arrays);
4768 return g_variant_get_strv (value, NULL);
4770 return g_variant_dup_strv (value, NULL);
4773 else if (type == 'o')
4776 return g_variant_get_objv (value, NULL);
4778 return g_variant_dup_objv (value, NULL);
4781 else if (arrays > 1)
4784 return g_variant_get_bytestring_array (value, NULL);
4786 return g_variant_dup_bytestring_array (value, NULL);
4792 return (gchar *) g_variant_get_bytestring (value);
4794 return g_variant_dup_bytestring (value, NULL);
4799 g_variant_type_string_scan (*str, NULL, str);
4805 return g_variant_ref (value);
4808 return g_variant_get_variant (value);
4811 g_assert_not_reached ();
4817 g_variant_valist_skip_leaf (const gchar **str,
4820 if (g_variant_format_string_is_nnp (*str))
4822 g_variant_format_string_scan (*str, NULL, str);
4823 va_arg (*app, gpointer);
4841 va_arg (*app, guint64);
4845 va_arg (*app, gdouble);
4849 g_assert_not_reached ();
4854 g_variant_valist_new_leaf (const gchar **str,
4857 if (g_variant_format_string_is_nnp (*str))
4858 return g_variant_valist_new_nnp (str, va_arg (*app, gpointer));
4863 return g_variant_new_boolean (va_arg (*app, gboolean));
4866 return g_variant_new_byte (va_arg (*app, guint));
4869 return g_variant_new_int16 (va_arg (*app, gint));
4872 return g_variant_new_uint16 (va_arg (*app, guint));
4875 return g_variant_new_int32 (va_arg (*app, gint));
4878 return g_variant_new_uint32 (va_arg (*app, guint));
4881 return g_variant_new_int64 (va_arg (*app, gint64));
4884 return g_variant_new_uint64 (va_arg (*app, guint64));
4887 return g_variant_new_handle (va_arg (*app, gint));
4890 return g_variant_new_double (va_arg (*app, gdouble));
4893 g_assert_not_reached ();
4897 /* The code below assumes this */
4898 G_STATIC_ASSERT (sizeof (gboolean) == sizeof (guint32));
4899 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
4902 g_variant_valist_get_leaf (const gchar **str,
4907 gpointer ptr = va_arg (*app, gpointer);
4911 g_variant_format_string_scan (*str, NULL, str);
4915 if (g_variant_format_string_is_nnp (*str))
4917 gpointer *nnp = (gpointer *) ptr;
4919 if (free && *nnp != NULL)
4920 g_variant_valist_free_nnp (*str, *nnp);
4925 *nnp = g_variant_valist_get_nnp (str, value);
4927 g_variant_format_string_scan (*str, NULL, str);
4937 *(gboolean *) ptr = g_variant_get_boolean (value);
4941 *(guchar *) ptr = g_variant_get_byte (value);
4945 *(gint16 *) ptr = g_variant_get_int16 (value);
4949 *(guint16 *) ptr = g_variant_get_uint16 (value);
4953 *(gint32 *) ptr = g_variant_get_int32 (value);
4957 *(guint32 *) ptr = g_variant_get_uint32 (value);
4961 *(gint64 *) ptr = g_variant_get_int64 (value);
4965 *(guint64 *) ptr = g_variant_get_uint64 (value);
4969 *(gint32 *) ptr = g_variant_get_handle (value);
4973 *(gdouble *) ptr = g_variant_get_double (value);
4982 *(guchar *) ptr = 0;
4987 *(guint16 *) ptr = 0;
4994 *(guint32 *) ptr = 0;
5000 *(guint64 *) ptr = 0;
5005 g_assert_not_reached ();
5008 /* Generic (recursive) {{{2 */
5010 g_variant_valist_skip (const gchar **str,
5013 if (g_variant_format_string_is_leaf (*str))
5014 g_variant_valist_skip_leaf (str, app);
5016 else if (**str == 'm') /* maybe */
5020 if (!g_variant_format_string_is_nnp (*str))
5021 va_arg (*app, gboolean);
5023 g_variant_valist_skip (str, app);
5025 else /* tuple, dictionary entry */
5027 g_assert (**str == '(' || **str == '{');
5029 while (**str != ')' && **str != '}')
5030 g_variant_valist_skip (str, app);
5036 g_variant_valist_new (const gchar **str,
5039 if (g_variant_format_string_is_leaf (*str))
5040 return g_variant_valist_new_leaf (str, app);
5042 if (**str == 'm') /* maybe */
5044 GVariantType *type = NULL;
5045 GVariant *value = NULL;
5049 if (g_variant_format_string_is_nnp (*str))
5051 gpointer nnp = va_arg (*app, gpointer);
5054 value = g_variant_valist_new_nnp (str, nnp);
5056 type = g_variant_format_string_scan_type (*str, NULL, str);
5060 gboolean just = va_arg (*app, gboolean);
5063 value = g_variant_valist_new (str, app);
5066 type = g_variant_format_string_scan_type (*str, NULL, NULL);
5067 g_variant_valist_skip (str, app);
5071 value = g_variant_new_maybe (type, value);
5074 g_variant_type_free (type);
5078 else /* tuple, dictionary entry */
5083 g_variant_builder_init (&b, G_VARIANT_TYPE_TUPLE);
5086 g_assert (**str == '{');
5087 g_variant_builder_init (&b, G_VARIANT_TYPE_DICT_ENTRY);
5091 while (**str != ')' && **str != '}')
5092 g_variant_builder_add_value (&b, g_variant_valist_new (str, app));
5095 return g_variant_builder_end (&b);
5100 g_variant_valist_get (const gchar **str,
5105 if (g_variant_format_string_is_leaf (*str))
5106 g_variant_valist_get_leaf (str, value, free, app);
5108 else if (**str == 'm')
5113 value = g_variant_get_maybe (value);
5115 if (!g_variant_format_string_is_nnp (*str))
5117 gboolean *ptr = va_arg (*app, gboolean *);
5120 *ptr = value != NULL;
5123 g_variant_valist_get (str, value, free, app);
5126 g_variant_unref (value);
5129 else /* tuple, dictionary entry */
5133 g_assert (**str == '(' || **str == '{');
5136 while (**str != ')' && **str != '}')
5140 GVariant *child = g_variant_get_child_value (value, index++);
5141 g_variant_valist_get (str, child, free, app);
5142 g_variant_unref (child);
5145 g_variant_valist_get (str, NULL, free, app);
5151 /* User-facing API {{{2 */
5153 * g_variant_new: (skip)
5154 * @format_string: a #GVariant format string
5155 * @...: arguments, as per @format_string
5157 * Creates a new #GVariant instance.
5159 * Think of this function as an analogue to g_strdup_printf().
5161 * The type of the created instance and the arguments that are
5162 * expected by this function are determined by @format_string. See the
5163 * section on <link linkend='gvariant-format-strings'>GVariant Format
5164 * Strings</link>. Please note that the syntax of the format string is
5165 * very likely to be extended in the future.
5167 * The first character of the format string must not be '*' '?' '@' or
5168 * 'r'; in essence, a new #GVariant must always be constructed by this
5169 * function (and not merely passed through it unmodified).
5171 * Note that the arguments must be of the correct width for their types
5172 * specified in @format_string. This can be achieved by casting them. See
5173 * the <link linkend='gvariant-varargs'>GVariant varargs documentation</link>.
5176 * MyFlags some_flags = FLAG_ONE | FLAG_TWO;
5177 * const gchar *some_strings[] = { "a", "b", "c", NULL };
5178 * GVariant *new_variant;
5180 * new_variant = g_variant_new ("(t^as)",
5181 * /<!-- -->* This cast is required. *<!-- -->/
5182 * (guint64) some_flags,
5186 * Returns: a new floating #GVariant instance
5191 g_variant_new (const gchar *format_string,
5197 g_return_val_if_fail (valid_format_string (format_string, TRUE, NULL) &&
5198 format_string[0] != '?' && format_string[0] != '@' &&
5199 format_string[0] != '*' && format_string[0] != 'r',
5202 va_start (ap, format_string);
5203 value = g_variant_new_va (format_string, NULL, &ap);
5210 * g_variant_new_va: (skip)
5211 * @format_string: a string that is prefixed with a format string
5212 * @endptr: (allow-none) (default NULL): location to store the end pointer,
5214 * @app: a pointer to a #va_list
5216 * This function is intended to be used by libraries based on
5217 * #GVariant that want to provide g_variant_new()-like functionality
5220 * The API is more general than g_variant_new() to allow a wider range
5223 * @format_string must still point to a valid format string, but it only
5224 * needs to be nul-terminated if @endptr is %NULL. If @endptr is
5225 * non-%NULL then it is updated to point to the first character past the
5226 * end of the format string.
5228 * @app is a pointer to a #va_list. The arguments, according to
5229 * @format_string, are collected from this #va_list and the list is left
5230 * pointing to the argument following the last.
5232 * Note that the arguments in @app must be of the correct width for their types
5233 * specified in @format_string when collected into the #va_list. See
5234 * the <link linkend='gvariant-varargs'>GVariant varargs documentation</link>.
5236 * These two generalisations allow mixing of multiple calls to
5237 * g_variant_new_va() and g_variant_get_va() within a single actual
5238 * varargs call by the user.
5240 * The return value will be floating if it was a newly created GVariant
5241 * instance (for example, if the format string was "(ii)"). In the case
5242 * that the format_string was '*', '?', 'r', or a format starting with
5243 * '@' then the collected #GVariant pointer will be returned unmodified,
5244 * without adding any additional references.
5246 * In order to behave correctly in all cases it is necessary for the
5247 * calling function to g_variant_ref_sink() the return result before
5248 * returning control to the user that originally provided the pointer.
5249 * At this point, the caller will have their own full reference to the
5250 * result. This can also be done by adding the result to a container,
5251 * or by passing it to another g_variant_new() call.
5253 * Returns: a new, usually floating, #GVariant
5258 g_variant_new_va (const gchar *format_string,
5259 const gchar **endptr,
5264 g_return_val_if_fail (valid_format_string (format_string, !endptr, NULL),
5266 g_return_val_if_fail (app != NULL, NULL);
5268 value = g_variant_valist_new (&format_string, app);
5271 *endptr = format_string;
5277 * g_variant_get: (skip)
5278 * @value: a #GVariant instance
5279 * @format_string: a #GVariant format string
5280 * @...: arguments, as per @format_string
5282 * Deconstructs a #GVariant instance.
5284 * Think of this function as an analogue to scanf().
5286 * The arguments that are expected by this function are entirely
5287 * determined by @format_string. @format_string also restricts the
5288 * permissible types of @value. It is an error to give a value with
5289 * an incompatible type. See the section on <link
5290 * linkend='gvariant-format-strings'>GVariant Format Strings</link>.
5291 * Please note that the syntax of the format string is very likely to be
5292 * extended in the future.
5294 * @format_string determines the C types that are used for unpacking
5295 * the values and also determines if the values are copied or borrowed,
5296 * see the section on
5297 * <link linkend='gvariant-format-strings-pointers'>GVariant Format Strings</link>.
5302 g_variant_get (GVariant *value,
5303 const gchar *format_string,
5308 g_return_if_fail (valid_format_string (format_string, TRUE, value));
5310 /* if any direct-pointer-access formats are in use, flatten first */
5311 if (strchr (format_string, '&'))
5312 g_variant_get_data (value);
5314 va_start (ap, format_string);
5315 g_variant_get_va (value, format_string, NULL, &ap);
5320 * g_variant_get_va: (skip)
5321 * @value: a #GVariant
5322 * @format_string: a string that is prefixed with a format string
5323 * @endptr: (allow-none) (default NULL): location to store the end pointer,
5325 * @app: a pointer to a #va_list
5327 * This function is intended to be used by libraries based on #GVariant
5328 * that want to provide g_variant_get()-like functionality to their
5331 * The API is more general than g_variant_get() to allow a wider range
5334 * @format_string must still point to a valid format string, but it only
5335 * need to be nul-terminated if @endptr is %NULL. If @endptr is
5336 * non-%NULL then it is updated to point to the first character past the
5337 * end of the format string.
5339 * @app is a pointer to a #va_list. The arguments, according to
5340 * @format_string, are collected from this #va_list and the list is left
5341 * pointing to the argument following the last.
5343 * These two generalisations allow mixing of multiple calls to
5344 * g_variant_new_va() and g_variant_get_va() within a single actual
5345 * varargs call by the user.
5347 * @format_string determines the C types that are used for unpacking
5348 * the values and also determines if the values are copied or borrowed,
5349 * see the section on
5350 * <link linkend='gvariant-format-strings-pointers'>GVariant Format Strings</link>.
5355 g_variant_get_va (GVariant *value,
5356 const gchar *format_string,
5357 const gchar **endptr,
5360 g_return_if_fail (valid_format_string (format_string, !endptr, value));
5361 g_return_if_fail (value != NULL);
5362 g_return_if_fail (app != NULL);
5364 /* if any direct-pointer-access formats are in use, flatten first */
5365 if (strchr (format_string, '&'))
5366 g_variant_get_data (value);
5368 g_variant_valist_get (&format_string, value, FALSE, app);
5371 *endptr = format_string;
5374 /* Varargs-enabled Utility Functions {{{1 */
5377 * g_variant_builder_add: (skip)
5378 * @builder: a #GVariantBuilder
5379 * @format_string: a #GVariant varargs format string
5380 * @...: arguments, as per @format_string
5382 * Adds to a #GVariantBuilder.
5384 * This call is a convenience wrapper that is exactly equivalent to
5385 * calling g_variant_new() followed by g_variant_builder_add_value().
5387 * Note that the arguments must be of the correct width for their types
5388 * specified in @format_string. This can be achieved by casting them. See
5389 * the <link linkend='gvariant-varargs'>GVariant varargs documentation</link>.
5391 * This function might be used as follows:
5393 * |[<!-- language="C" -->
5395 * make_pointless_dictionary (void)
5397 * GVariantBuilder builder;
5400 * g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
5401 * for (i = 0; i < 16; i++)
5405 * sprintf (buf, "%d", i);
5406 * g_variant_builder_add (&builder, "{is}", i, buf);
5409 * return g_variant_builder_end (&builder);
5416 g_variant_builder_add (GVariantBuilder *builder,
5417 const gchar *format_string,
5423 va_start (ap, format_string);
5424 variant = g_variant_new_va (format_string, NULL, &ap);
5427 g_variant_builder_add_value (builder, variant);
5431 * g_variant_get_child: (skip)
5432 * @value: a container #GVariant
5433 * @index_: the index of the child to deconstruct
5434 * @format_string: a #GVariant format string
5435 * @...: arguments, as per @format_string
5437 * Reads a child item out of a container #GVariant instance and
5438 * deconstructs it according to @format_string. This call is
5439 * essentially a combination of g_variant_get_child_value() and
5442 * @format_string determines the C types that are used for unpacking
5443 * the values and also determines if the values are copied or borrowed,
5444 * see the section on
5445 * <link linkend='gvariant-format-strings-pointers'>GVariant Format Strings</link>.
5450 g_variant_get_child (GVariant *value,
5452 const gchar *format_string,
5458 child = g_variant_get_child_value (value, index_);
5459 g_return_if_fail (valid_format_string (format_string, TRUE, child));
5461 va_start (ap, format_string);
5462 g_variant_get_va (child, format_string, NULL, &ap);
5465 g_variant_unref (child);
5469 * g_variant_iter_next: (skip)
5470 * @iter: a #GVariantIter
5471 * @format_string: a GVariant format string
5472 * @...: the arguments to unpack the value into
5474 * Gets the next item in the container and unpacks it into the variable
5475 * argument list according to @format_string, returning %TRUE.
5477 * If no more items remain then %FALSE is returned.
5479 * All of the pointers given on the variable arguments list of this
5480 * function are assumed to point at uninitialised memory. It is the
5481 * responsibility of the caller to free all of the values returned by
5482 * the unpacking process.
5484 * Here is an example for memory management with g_variant_iter_next():
5485 * |[<!-- language="C" -->
5486 * /* Iterates a dictionary of type 'a{sv}' */
5488 * iterate_dictionary (GVariant *dictionary)
5490 * GVariantIter iter;
5494 * g_variant_iter_init (&iter, dictionary);
5495 * while (g_variant_iter_next (&iter, "{sv}", &key, &value))
5497 * g_print ("Item '%s' has type '%s'\n", key,
5498 * g_variant_get_type_string (value));
5500 * /* must free data for ourselves */
5501 * g_variant_unref (value);
5507 * For a solution that is likely to be more convenient to C programmers
5508 * when dealing with loops, see g_variant_iter_loop().
5510 * @format_string determines the C types that are used for unpacking
5511 * the values and also determines if the values are copied or borrowed.
5513 * See the section on
5514 * <link linkend='gvariant-format-strings-pointers'>GVariant Format Strings</link>.
5516 * Returns: %TRUE if a value was unpacked, or %FALSE if there as no value
5521 g_variant_iter_next (GVariantIter *iter,
5522 const gchar *format_string,
5527 value = g_variant_iter_next_value (iter);
5529 g_return_val_if_fail (valid_format_string (format_string, TRUE, value),
5536 va_start (ap, format_string);
5537 g_variant_valist_get (&format_string, value, FALSE, &ap);
5540 g_variant_unref (value);
5543 return value != NULL;
5547 * g_variant_iter_loop: (skip)
5548 * @iter: a #GVariantIter
5549 * @format_string: a GVariant format string
5550 * @...: the arguments to unpack the value into
5552 * Gets the next item in the container and unpacks it into the variable
5553 * argument list according to @format_string, returning %TRUE.
5555 * If no more items remain then %FALSE is returned.
5557 * On the first call to this function, the pointers appearing on the
5558 * variable argument list are assumed to point at uninitialised memory.
5559 * On the second and later calls, it is assumed that the same pointers
5560 * will be given and that they will point to the memory as set by the
5561 * previous call to this function. This allows the previous values to
5562 * be freed, as appropriate.
5564 * This function is intended to be used with a while loop as
5565 * demonstrated in the following example. This function can only be
5566 * used when iterating over an array. It is only valid to call this
5567 * function with a string constant for the format string and the same
5568 * string constant must be used each time. Mixing calls to this
5569 * function and g_variant_iter_next() or g_variant_iter_next_value() on
5570 * the same iterator causes undefined behavior.
5572 * If you break out of a such a while loop using g_variant_iter_loop() then
5573 * you must free or unreference all the unpacked values as you would with
5574 * g_variant_get(). Failure to do so will cause a memory leak.
5576 * Here is an example for memory management with g_variant_iter_loop():
5577 * |[<!-- language="C" -->
5578 * /* Iterates a dictionary of type 'a{sv}' */
5580 * iterate_dictionary (GVariant *dictionary)
5582 * GVariantIter iter;
5586 * g_variant_iter_init (&iter, dictionary);
5587 * while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
5589 * g_print ("Item '%s' has type '%s'\n", key,
5590 * g_variant_get_type_string (value));
5592 * /* no need to free 'key' and 'value' here
5593 * * unless breaking out of this loop
5599 * For most cases you should use g_variant_iter_next().
5601 * This function is really only useful when unpacking into #GVariant or
5602 * #GVariantIter in order to allow you to skip the call to
5603 * g_variant_unref() or g_variant_iter_free().
5605 * For example, if you are only looping over simple integer and string
5606 * types, g_variant_iter_next() is definitely preferred. For string
5607 * types, use the '&' prefix to avoid allocating any memory at all (and
5608 * thereby avoiding the need to free anything as well).
5610 * @format_string determines the C types that are used for unpacking
5611 * the values and also determines if the values are copied or borrowed.
5613 * See the section on
5614 * <link linkend='gvariant-format-strings-pointers'>GVariant Format Strings</link>.
5616 * Returns: %TRUE if a value was unpacked, or %FALSE if there was no
5622 g_variant_iter_loop (GVariantIter *iter,
5623 const gchar *format_string,
5626 gboolean first_time = GVSI(iter)->loop_format == NULL;
5630 g_return_val_if_fail (first_time ||
5631 format_string == GVSI(iter)->loop_format,
5636 TYPE_CHECK (GVSI(iter)->value, G_VARIANT_TYPE_ARRAY, FALSE);
5637 GVSI(iter)->loop_format = format_string;
5639 if (strchr (format_string, '&'))
5640 g_variant_get_data (GVSI(iter)->value);
5643 value = g_variant_iter_next_value (iter);
5645 g_return_val_if_fail (!first_time ||
5646 valid_format_string (format_string, TRUE, value),
5649 va_start (ap, format_string);
5650 g_variant_valist_get (&format_string, value, !first_time, &ap);
5654 g_variant_unref (value);
5656 return value != NULL;
5659 /* Serialised data {{{1 */
5661 g_variant_deep_copy (GVariant *value)
5663 switch (g_variant_classify (value))
5665 case G_VARIANT_CLASS_MAYBE:
5666 case G_VARIANT_CLASS_ARRAY:
5667 case G_VARIANT_CLASS_TUPLE:
5668 case G_VARIANT_CLASS_DICT_ENTRY:
5669 case G_VARIANT_CLASS_VARIANT:
5671 GVariantBuilder builder;
5675 g_variant_builder_init (&builder, g_variant_get_type (value));
5676 g_variant_iter_init (&iter, value);
5678 while ((child = g_variant_iter_next_value (&iter)))
5680 g_variant_builder_add_value (&builder, g_variant_deep_copy (child));
5681 g_variant_unref (child);
5684 return g_variant_builder_end (&builder);
5687 case G_VARIANT_CLASS_BOOLEAN:
5688 return g_variant_new_boolean (g_variant_get_boolean (value));
5690 case G_VARIANT_CLASS_BYTE:
5691 return g_variant_new_byte (g_variant_get_byte (value));
5693 case G_VARIANT_CLASS_INT16:
5694 return g_variant_new_int16 (g_variant_get_int16 (value));
5696 case G_VARIANT_CLASS_UINT16:
5697 return g_variant_new_uint16 (g_variant_get_uint16 (value));
5699 case G_VARIANT_CLASS_INT32:
5700 return g_variant_new_int32 (g_variant_get_int32 (value));
5702 case G_VARIANT_CLASS_UINT32:
5703 return g_variant_new_uint32 (g_variant_get_uint32 (value));
5705 case G_VARIANT_CLASS_INT64:
5706 return g_variant_new_int64 (g_variant_get_int64 (value));
5708 case G_VARIANT_CLASS_UINT64:
5709 return g_variant_new_uint64 (g_variant_get_uint64 (value));
5711 case G_VARIANT_CLASS_HANDLE:
5712 return g_variant_new_handle (g_variant_get_handle (value));
5714 case G_VARIANT_CLASS_DOUBLE:
5715 return g_variant_new_double (g_variant_get_double (value));
5717 case G_VARIANT_CLASS_STRING:
5718 return g_variant_new_string (g_variant_get_string (value, NULL));
5720 case G_VARIANT_CLASS_OBJECT_PATH:
5721 return g_variant_new_object_path (g_variant_get_string (value, NULL));
5723 case G_VARIANT_CLASS_SIGNATURE:
5724 return g_variant_new_signature (g_variant_get_string (value, NULL));
5727 g_assert_not_reached ();
5731 * g_variant_get_normal_form:
5732 * @value: a #GVariant
5734 * Gets a #GVariant instance that has the same value as @value and is
5735 * trusted to be in normal form.
5737 * If @value is already trusted to be in normal form then a new
5738 * reference to @value is returned.
5740 * If @value is not already trusted, then it is scanned to check if it
5741 * is in normal form. If it is found to be in normal form then it is
5742 * marked as trusted and a new reference to it is returned.
5744 * If @value is found not to be in normal form then a new trusted
5745 * #GVariant is created with the same value as @value.
5747 * It makes sense to call this function if you've received #GVariant
5748 * data from untrusted sources and you want to ensure your serialised
5749 * output is definitely in normal form.
5751 * Returns: (transfer full): a trusted #GVariant
5756 g_variant_get_normal_form (GVariant *value)
5760 if (g_variant_is_normal_form (value))
5761 return g_variant_ref (value);
5763 trusted = g_variant_deep_copy (value);
5764 g_assert (g_variant_is_trusted (trusted));
5766 return g_variant_ref_sink (trusted);
5770 * g_variant_byteswap:
5771 * @value: a #GVariant
5773 * Performs a byteswapping operation on the contents of @value. The
5774 * result is that all multi-byte numeric data contained in @value is
5775 * byteswapped. That includes 16, 32, and 64bit signed and unsigned
5776 * integers as well as file handles and double precision floating point
5779 * This function is an identity mapping on any value that does not
5780 * contain multi-byte numeric data. That include strings, booleans,
5781 * bytes and containers containing only these things (recursively).
5783 * The returned value is always in normal form and is marked as trusted.
5785 * Returns: (transfer full): the byteswapped form of @value
5790 g_variant_byteswap (GVariant *value)
5792 GVariantTypeInfo *type_info;
5796 type_info = g_variant_get_type_info (value);
5798 g_variant_type_info_query (type_info, &alignment, NULL);
5801 /* (potentially) contains multi-byte numeric data */
5803 GVariantSerialised serialised;
5807 trusted = g_variant_get_normal_form (value);
5808 serialised.type_info = g_variant_get_type_info (trusted);
5809 serialised.size = g_variant_get_size (trusted);
5810 serialised.data = g_malloc (serialised.size);
5811 g_variant_store (trusted, serialised.data);
5812 g_variant_unref (trusted);
5814 g_variant_serialised_byteswap (serialised);
5816 bytes = g_bytes_new_take (serialised.data, serialised.size);
5817 new = g_variant_new_from_bytes (g_variant_get_type (value), bytes, TRUE);
5818 g_bytes_unref (bytes);
5821 /* contains no multi-byte data */
5824 return g_variant_ref_sink (new);
5828 * g_variant_new_from_data:
5829 * @type: a definite #GVariantType
5830 * @data: (array length=size) (element-type guint8): the serialised data
5831 * @size: the size of @data
5832 * @trusted: %TRUE if @data is definitely in normal form
5833 * @notify: (scope async): function to call when @data is no longer needed
5834 * @user_data: data for @notify
5836 * Creates a new #GVariant instance from serialised data.
5838 * @type is the type of #GVariant instance that will be constructed.
5839 * The interpretation of @data depends on knowing the type.
5841 * @data is not modified by this function and must remain valid with an
5842 * unchanging value until such a time as @notify is called with
5843 * @user_data. If the contents of @data change before that time then
5844 * the result is undefined.
5846 * If @data is trusted to be serialised data in normal form then
5847 * @trusted should be %TRUE. This applies to serialised data created
5848 * within this process or read from a trusted location on the disk (such
5849 * as a file installed in /usr/lib alongside your application). You
5850 * should set trusted to %FALSE if @data is read from the network, a
5851 * file in the user's home directory, etc.
5853 * If @data was not stored in this machine's native endianness, any multi-byte
5854 * numeric values in the returned variant will also be in non-native
5855 * endianness. g_variant_byteswap() can be used to recover the original values.
5857 * @notify will be called with @user_data when @data is no longer
5858 * needed. The exact time of this call is unspecified and might even be
5859 * before this function returns.
5861 * Returns: (transfer none): a new floating #GVariant of type @type
5866 g_variant_new_from_data (const GVariantType *type,
5870 GDestroyNotify notify,
5876 g_return_val_if_fail (g_variant_type_is_definite (type), NULL);
5877 g_return_val_if_fail (data != NULL || size == 0, NULL);
5880 bytes = g_bytes_new_with_free_func (data, size, notify, user_data);
5882 bytes = g_bytes_new_static (data, size);
5884 value = g_variant_new_from_bytes (type, bytes, trusted);
5885 g_bytes_unref (bytes);
5891 /* vim:set foldmethod=marker: */