2 * Copyright © 2007, 2008 Ryan Lortie
3 * Copyright © 2010 Codethink Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Ryan Lortie <desrt@desrt.ca>
27 #include <glib/gvariant-serialiser.h>
28 #include "gvariant-internal.h"
29 #include <glib/gvariant-core.h>
30 #include <glib/gtestutils.h>
31 #include <glib/gstrfuncs.h>
32 #include <glib/ghash.h>
33 #include <glib/gmem.h>
42 * @short_description: strongly typed value datatype
43 * @see_also: GVariantType
45 * #GVariant is a variant datatype; it stores a value along with
46 * information about the type of that value. The range of possible
47 * values is determined by the type. The type system used by #GVariant
50 * #GVariant instances always have a type and a value (which are given
51 * at construction time). The type and value of a #GVariant instance
52 * can never change other than by the #GVariant itself being
53 * destroyed. A #GVariant can not contain a pointer.
55 * #GVariant is reference counted using g_variant_ref() and
56 * g_variant_unref(). #GVariant also has floating reference counts --
57 * see g_variant_ref_sink().
59 * #GVariant is completely threadsafe. A #GVariant instance can be
60 * concurrently accessed in any way from any number of threads without
63 * #GVariant is heavily optimised for dealing with data in serialised
64 * form. It works particularly well with data located in memory-mapped
65 * files. It can perform nearly all deserialisation operations in a
66 * small constant time, usually touching only a single memory page.
67 * Serialised #GVariant data can also be sent over the network.
69 * #GVariant is largely compatible with DBus. Almost all types of
70 * #GVariant instances can be sent over DBus. See #GVariantType for
73 * For convenience to C programmers, #GVariant features powerful
74 * varargs-based value construction and destruction. This feature is
75 * designed to be embedded in other libraries.
77 * There is a Python-inspired text language for describing #GVariant
78 * values. #GVariant includes a printer for this language and a parser
79 * with type inferencing.
82 * <title>Memory Use</title>
84 * #GVariant tries to be quite efficient with respect to memory use.
85 * This section gives a rough idea of how much memory is used by the
86 * current implementation. The information here is subject to change
90 * The memory allocated by #GVariant can be grouped into 4 broad
91 * purposes: memory for serialised data, memory for the type
92 * information cache, buffer management memory and memory for the
93 * #GVariant structure itself.
96 * <title>Serialised Data Memory</title>
98 * This is the memory that is used for storing GVariant data in
99 * serialised form. This is what would be sent over the network or
100 * what would end up on disk.
103 * The amount of memory required to store a boolean is 1 byte. 16,
104 * 32 and 64 bit integers and double precision floating point numbers
105 * use their "natural" size. Strings (including object path and
106 * signature strings) are stored with a nul terminator, and as such
107 * use the length of the string plus 1 byte.
110 * Maybe types use no space at all to represent the null value and
111 * use the same amount of space (sometimes plus one byte) as the
112 * equivalent non-maybe-typed value to represent the non-null case.
115 * Arrays use the amount of space required to store each of their
116 * members, concatenated. Additionally, if the items stored in an
117 * array are not of a fixed-size (ie: strings, other arrays, etc)
118 * then an additional framing offset is stored for each item. The
119 * size of this offset is either 1, 2 or 4 bytes depending on the
120 * overall size of the container. Additionally, extra padding bytes
121 * are added as required for alignment of child values.
124 * Tuples (including dictionary entries) use the amount of space
125 * required to store each of their members, concatenated, plus one
126 * framing offset (as per arrays) for each non-fixed-sized item in
127 * the tuple, except for the last one. Additionally, extra padding
128 * bytes are added as required for alignment of child values.
131 * Variants use the same amount of space as the item inside of the
132 * variant, plus 1 byte, plus the length of the type string for the
133 * item inside the variant.
136 * As an example, consider a dictionary mapping strings to variants.
137 * In the case that the dictionary is empty, 0 bytes are required for
141 * If we add an item "width" that maps to the int32 value of 500 then
142 * we will use 4 byte to store the int32 (so 6 for the variant
143 * containing it) and 6 bytes for the string. The variant must be
144 * aligned to 8 after the 6 bytes of the string, so that's 2 extra
145 * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
146 * for the dictionary entry. An additional 1 byte is added to the
147 * array as a framing offset making a total of 15 bytes.
150 * If we add another entry, "title" that maps to a nullable string
151 * that happens to have a value of null, then we use 0 bytes for the
152 * null value (and 3 bytes for the variant to contain it along with
153 * its type string) plus 6 bytes for the string. Again, we need 2
154 * padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
157 * We now require extra padding between the two items in the array.
158 * After the 14 bytes of the first item, that's 2 bytes required. We
159 * now require 2 framing offsets for an extra two bytes. 14 + 2 + 11
160 * + 2 = 29 bytes to encode the entire two-item dictionary.
164 * <title>Type Information Cache</title>
166 * For each GVariant type that currently exists in the program a type
167 * information structure is kept in the type information cache. The
168 * type information structure is required for rapid deserialisation.
171 * Continuing with the above example, if a #GVariant exists with the
172 * type "a{sv}" then a type information struct will exist for
173 * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type
174 * will share the same type information. Additionally, all
175 * single-digit types are stored in read-only static memory and do
176 * not contribute to the writable memory footprint of a program using
180 * Aside from the type information structures stored in read-only
181 * memory, there are two forms of type information. One is used for
182 * container types where there is a single element type: arrays and
183 * maybe types. The other is used for container types where there
184 * are multiple element types: tuples and dictionary entries.
187 * Array type info structures are 6 * sizeof (void *), plus the
188 * memory required to store the type string itself. This means that
189 * on 32bit systems, the cache entry for "a{sv}" would require 30
190 * bytes of memory (plus malloc overhead).
193 * Tuple type info structures are 6 * sizeof (void *), plus 4 *
194 * sizeof (void *) for each item in the tuple, plus the memory
195 * required to store the type string itself. A 2-item tuple, for
196 * example, would have a type information structure that consumed
197 * writable memory in the size of 14 * sizeof (void *) (plus type
198 * string) This means that on 32bit systems, the cache entry for
199 * "{sv}" would require 61 bytes of memory (plus malloc overhead).
202 * This means that in total, for our "a{sv}" example, 91 bytes of
203 * type information would be allocated.
206 * The type information cache, additionally, uses a #GHashTable to
207 * store and lookup the cached items and stores a pointer to this
208 * hash table in static storage. The hash table is freed when there
209 * are zero items in the type cache.
212 * Although these sizes may seem large it is important to remember
213 * that a program will probably only have a very small number of
214 * different types of values in it and that only one type information
215 * structure is required for many different values of the same type.
219 * <title>Buffer Management Memory</title>
221 * #GVariant uses an internal buffer management structure to deal
222 * with the various different possible sources of serialised data
223 * that it uses. The buffer is responsible for ensuring that the
224 * correct call is made when the data is no longer in use by
225 * #GVariant. This may involve a g_free() or a g_slice_free() or
226 * even g_mapped_file_unref().
229 * One buffer management structure is used for each chunk of
230 * serialised data. The size of the buffer management structure is 4
231 * * (void *). On 32bit systems, that's 16 bytes.
235 * <title>GVariant structure</title>
237 * The size of a #GVariant structure is 6 * (void *). On 32 bit
238 * systems, that's 24 bytes.
241 * #GVariant structures only exist if they are explicitly created
242 * with API calls. For example, if a #GVariant is constructed out of
243 * serialised data for the example given above (with the dictionary)
244 * then although there are 9 individual values that comprise the
245 * entire dictionary (two keys, two values, two variants containing
246 * the values, two dictionary entries, plus the dictionary itself),
247 * only 1 #GVariant instance exists -- the one refering to the
251 * If calls are made to start accessing the other values then
252 * #GVariant instances will exist for those values only for as long
253 * as they are in use (ie: until you call g_variant_unref()). The
254 * type information is shared. The serialised data and the buffer
255 * management structure for that serialised data is shared by the
260 * <title>Summary</title>
262 * To put the entire example together, for our dictionary mapping
263 * strings to variants (with two entries, as given above), we are
264 * using 91 bytes of memory for type information, 29 byes of memory
265 * for the serialised data, 16 bytes for buffer management and 24
266 * bytes for the #GVariant instance, or a total of 160 bytes, plus
267 * malloc overhead. If we were to use g_variant_get_child_value() to
268 * access the two dictionary entries, we would use an additional 48
269 * bytes. If we were to have other dictionaries of the same type, we
270 * would use more memory for the serialised data and buffer
271 * management for those dictionaries, but the type information would
278 /* definition of GVariant structure is in gvariant-core.c */
280 /* this is a g_return_val_if_fail() for making
281 * sure a (GVariant *) has the required type.
283 #define TYPE_CHECK(value, TYPE, val) \
284 if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \
285 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, \
286 "g_variant_is_of_type (" #value \
291 /* Numeric Type Constructor/Getters {{{1 */
293 * g_variant_new_from_trusted:
294 * @type: the #GVariantType
295 * @data: the data to use
296 * @size: the size of @data
297 * @returns: a new floating #GVariant
299 * Constructs a new trusted #GVariant instance from the provided data.
300 * This is used to implement g_variant_new_* for all the basic types.
303 g_variant_new_from_trusted (const GVariantType *type,
310 buffer = g_buffer_new_from_data (data, size);
311 value = g_variant_new_from_buffer (type, buffer, TRUE);
312 g_buffer_unref (buffer);
318 * g_variant_new_boolean:
319 * @boolean: a #gboolean value
320 * @returns: a new boolean #GVariant instance
322 * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
327 g_variant_new_boolean (gboolean value)
331 return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1);
335 * g_variant_get_boolean:
336 * @value: a boolean #GVariant instance
337 * @returns: %TRUE or %FALSE
339 * Returns the boolean value of @value.
341 * It is an error to call this function with a @value of any type
342 * other than %G_VARIANT_TYPE_BOOLEAN.
347 g_variant_get_boolean (GVariant *value)
351 TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE);
353 data = g_variant_get_data (value);
355 return data != NULL ? *data != 0 : FALSE;
358 /* the constructors and accessors for byte, int{16,32,64}, handles and
359 * doubles all look pretty much exactly the same, so we reduce
362 #define NUMERIC_TYPE(TYPE, type, ctype) \
363 GVariant *g_variant_new_##type (ctype value) { \
364 return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \
365 &value, sizeof value); \
367 ctype g_variant_get_##type (GVariant *value) { \
369 TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \
370 data = g_variant_get_data (value); \
371 return data != NULL ? *data : 0; \
376 * g_variant_new_byte:
377 * @byte: a #guint8 value
378 * @returns: a new byte #GVariant instance
380 * Creates a new byte #GVariant instance.
385 * g_variant_get_byte:
386 * @value: a byte #GVariant instance
387 * @returns: a #guchar
389 * Returns the byte value of @value.
391 * It is an error to call this function with a @value of any type
392 * other than %G_VARIANT_TYPE_BYTE.
396 NUMERIC_TYPE (BYTE, byte, guchar)
399 * g_variant_new_int16:
400 * @int16: a #gint16 value
401 * @returns: a new int16 #GVariant instance
403 * Creates a new int16 #GVariant instance.
408 * g_variant_get_int16:
409 * @value: a int16 #GVariant instance
410 * @returns: a #gint16
412 * Returns the 16-bit signed integer value of @value.
414 * It is an error to call this function with a @value of any type
415 * other than %G_VARIANT_TYPE_INT16.
419 NUMERIC_TYPE (INT16, int16, gint16)
422 * g_variant_new_uint16:
423 * @uint16: a #guint16 value
424 * @returns: a new uint16 #GVariant instance
426 * Creates a new uint16 #GVariant instance.
431 * g_variant_get_uint16:
432 * @value: a uint16 #GVariant instance
433 * @returns: a #guint16
435 * Returns the 16-bit unsigned integer value of @value.
437 * It is an error to call this function with a @value of any type
438 * other than %G_VARIANT_TYPE_UINT16.
442 NUMERIC_TYPE (UINT16, uint16, guint16)
445 * g_variant_new_int32:
446 * @int32: a #gint32 value
447 * @returns: a new int32 #GVariant instance
449 * Creates a new int32 #GVariant instance.
454 * g_variant_get_int32:
455 * @value: a int32 #GVariant instance
456 * @returns: a #gint32
458 * Returns the 32-bit signed integer value of @value.
460 * It is an error to call this function with a @value of any type
461 * other than %G_VARIANT_TYPE_INT32.
465 NUMERIC_TYPE (INT32, int32, gint32)
468 * g_variant_new_uint32:
469 * @uint32: a #guint32 value
470 * @returns: a new uint32 #GVariant instance
472 * Creates a new uint32 #GVariant instance.
477 * g_variant_get_uint32:
478 * @value: a uint32 #GVariant instance
479 * @returns: a #guint32
481 * Returns the 32-bit unsigned integer value of @value.
483 * It is an error to call this function with a @value of any type
484 * other than %G_VARIANT_TYPE_UINT32.
488 NUMERIC_TYPE (UINT32, uint32, guint32)
491 * g_variant_new_int64:
492 * @int64: a #gint64 value
493 * @returns: a new int64 #GVariant instance
495 * Creates a new int64 #GVariant instance.
500 * g_variant_get_int64:
501 * @value: a int64 #GVariant instance
502 * @returns: a #gint64
504 * Returns the 64-bit signed integer value of @value.
506 * It is an error to call this function with a @value of any type
507 * other than %G_VARIANT_TYPE_INT64.
511 NUMERIC_TYPE (INT64, int64, gint64)
514 * g_variant_new_uint64:
515 * @uint64: a #guint64 value
516 * @returns: a new uint64 #GVariant instance
518 * Creates a new uint64 #GVariant instance.
523 * g_variant_get_uint64:
524 * @value: a uint64 #GVariant instance
525 * @returns: a #guint64
527 * Returns the 64-bit unsigned integer value of @value.
529 * It is an error to call this function with a @value of any type
530 * other than %G_VARIANT_TYPE_UINT64.
534 NUMERIC_TYPE (UINT64, uint64, guint64)
537 * g_variant_new_handle:
538 * @handle: a #gint32 value
539 * @returns: a new handle #GVariant instance
541 * Creates a new handle #GVariant instance.
543 * By convention, handles are indexes into an array of file descriptors
544 * that are sent alongside a DBus message. If you're not interacting
545 * with DBus, you probably don't need them.
550 * g_variant_get_handle:
551 * @value: a handle #GVariant instance
552 * @returns: a #gint32
554 * Returns the 32-bit signed integer value of @value.
556 * It is an error to call this function with a @value of any type other
557 * than %G_VARIANT_TYPE_HANDLE.
559 * By convention, handles are indexes into an array of file descriptors
560 * that are sent alongside a DBus message. If you're not interacting
561 * with DBus, you probably don't need them.
565 NUMERIC_TYPE (HANDLE, handle, gint32)
568 * g_variant_new_double:
569 * @floating: a #gdouble floating point value
570 * @returns: a new double #GVariant instance
572 * Creates a new double #GVariant instance.
577 * g_variant_get_double:
578 * @value: a double #GVariant instance
579 * @returns: a #gdouble
581 * Returns the double precision floating point value of @value.
583 * It is an error to call this function with a @value of any type
584 * other than %G_VARIANT_TYPE_DOUBLE.
588 NUMERIC_TYPE (DOUBLE, double, gdouble)
590 /* Container type Constructor / Deconstructors {{{1 */
592 * g_variant_new_maybe:
593 * @child_type: the #GVariantType of the child
594 * @child: the child value, or %NULL
595 * @returns: a new #GVariant maybe instance
597 * Depending on if @value is %NULL, either wraps @value inside of a
598 * maybe container or creates a Nothing instance for the given @type.
600 * At least one of @type and @value must be non-%NULL. If @type is
601 * non-%NULL then it must be a definite type. If they are both
602 * non-%NULL then @type must be the type of @value.
607 g_variant_new_maybe (const GVariantType *child_type,
610 GVariantType *maybe_type;
613 g_return_val_if_fail (child_type == NULL || g_variant_type_is_definite
615 g_return_val_if_fail (child_type != NULL || child != NULL, NULL);
616 g_return_val_if_fail (child_type == NULL || child == NULL ||
617 g_variant_is_of_type (child, child_type),
620 if (child_type == NULL)
621 child_type = g_variant_get_type (child);
623 maybe_type = g_variant_type_new_maybe (child_type);
630 children = g_new (GVariant *, 1);
631 children[0] = g_variant_ref_sink (child);
632 trusted = g_variant_is_trusted (children[0]);
634 value = g_variant_new_from_children (maybe_type, children, 1, trusted);
637 value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE);
639 g_variant_type_free (maybe_type);
645 * g_variant_get_maybe:
646 * @value: a maybe-typed value
647 * @returns: the contents of @value, or %NULL
649 * Given a maybe-typed #GVariant instance, extract its value. If the
650 * value is Nothing, then this function returns %NULL.
655 g_variant_get_maybe (GVariant *value)
657 TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL);
659 if (g_variant_n_children (value))
660 return g_variant_get_child_value (value, 0);
666 * g_variant_new_variant:
667 * @value: a #GVariance instance
668 * @returns: a new variant #GVariant instance
670 * Boxes @value. The result is a #GVariant instance representing a
671 * variant containing the original value.
676 g_variant_new_variant (GVariant *value)
678 g_return_val_if_fail (value != NULL, NULL);
680 g_variant_ref_sink (value);
682 return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
683 g_memdup (&value, sizeof value),
684 1, g_variant_is_trusted (value));
688 * g_variant_get_variant:
689 * @value: a variant #GVariance instance
690 * @returns: the item contained in the variant
692 * Unboxes @value. The result is the #GVariant instance that was
693 * contained in @value.
698 g_variant_get_variant (GVariant *value)
700 TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL);
702 return g_variant_get_child_value (value, 0);
706 * g_variant_new_array:
707 * @child_type: the element type of the new array
708 * @children: an array of #GVariant pointers, the children
709 * @n_children: the length of @children
710 * @returns: a new #GVariant array
712 * Creates a new #GVariant array from @children.
714 * @child_type must be non-%NULL if @n_children is zero. Otherwise, the
715 * child type is determined by inspecting the first element of the
716 * @children array. If @child_type is non-%NULL then it must be a
719 * The items of the array are taken from the @children array. No entry
720 * in the @children array may be %NULL.
722 * All items in the array must have the same type, which must be the
723 * same as @child_type, if given.
728 g_variant_new_array (const GVariantType *child_type,
729 GVariant * const *children,
732 GVariantType *array_type;
733 GVariant **my_children;
738 g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
739 g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
740 g_return_val_if_fail (child_type == NULL ||
741 g_variant_type_is_definite (child_type), NULL);
743 my_children = g_new (GVariant *, n_children);
746 if (child_type == NULL)
747 child_type = g_variant_get_type (children[0]);
748 array_type = g_variant_type_new_array (child_type);
750 for (i = 0; i < n_children; i++)
752 TYPE_CHECK (children[i], child_type, NULL);
753 my_children[i] = g_variant_ref_sink (children[i]);
754 trusted &= g_variant_is_trusted (children[i]);
757 value = g_variant_new_from_children (array_type, my_children,
758 n_children, trusted);
759 g_variant_type_free (array_type);
765 * g_variant_make_tuple_type:
766 * @children: an array of GVariant *
767 * @n_children: the length of @children
769 * Return the type of a tuple containing @children as its items.
771 static GVariantType *
772 g_variant_make_tuple_type (GVariant * const *children,
775 const GVariantType **types;
779 types = g_new (const GVariantType *, n_children);
781 for (i = 0; i < n_children; i++)
782 types[i] = g_variant_get_type (children[i]);
784 type = g_variant_type_new_tuple (types, n_children);
791 * g_variant_new_tuple:
792 * @children: the items to make the tuple out of
793 * @n_children: the length of @children
794 * @returns: a new #GVariant tuple
796 * Creates a new tuple #GVariant out of the items in @children. The
797 * type is determined from the types of @children. No entry in the
798 * @children array may be %NULL.
800 * If @n_children is 0 then the unit tuple is constructed.
805 g_variant_new_tuple (GVariant * const *children,
808 GVariantType *tuple_type;
809 GVariant **my_children;
814 g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
816 my_children = g_new (GVariant *, n_children);
819 for (i = 0; i < n_children; i++)
821 my_children[i] = g_variant_ref_sink (children[i]);
822 trusted &= g_variant_is_trusted (children[i]);
825 tuple_type = g_variant_make_tuple_type (children, n_children);
826 value = g_variant_new_from_children (tuple_type, my_children,
827 n_children, trusted);
828 g_variant_type_free (tuple_type);
834 * g_variant_make_dict_entry_type:
835 * @key: a #GVariant, the key
836 * @val: a #GVariant, the value
838 * Return the type of a dictionary entry containing @key and @val as its
841 static GVariantType *
842 g_variant_make_dict_entry_type (GVariant *key,
845 return g_variant_type_new_dict_entry (g_variant_get_type (key),
846 g_variant_get_type (val));
850 * g_variant_new_dict_entry:
851 * @key: a basic #GVariant, the key
852 * @value: a #GVariant, the value
853 * @returns: a new dictionary entry #GVariant
855 * Creates a new dictionary entry #GVariant. @key and @value must be
858 * @key must be a value of a basic type (ie: not a container).
863 g_variant_new_dict_entry (GVariant *key,
866 GVariantType *dict_type;
870 g_return_val_if_fail (key != NULL && value != NULL, NULL);
871 g_return_val_if_fail (!g_variant_is_container (key), NULL);
873 children = g_new (GVariant *, 2);
874 children[0] = g_variant_ref_sink (key);
875 children[1] = g_variant_ref_sink (value);
876 trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
878 dict_type = g_variant_make_dict_entry_type (key, value);
879 value = g_variant_new_from_children (dict_type, children, 2, trusted);
880 g_variant_type_free (dict_type);
886 * g_variant_get_fixed_array:
887 * @value: a #GVariant array with fixed-sized elements
888 * @n_elements: a pointer to the location to store the number of items
889 * @element_size: the size of each element
890 * @returns: a pointer to the fixed array
892 * Provides access to the serialised data for an array of fixed-sized
895 * @value must be an array with fixed-sized elements. Numeric types are
896 * fixed-size as are tuples containing only other fixed-sized types.
898 * @element_size must be the size of a single element in the array. For
899 * example, if calling this function for an array of 32 bit integers,
900 * you might say <code>sizeof (gint32)</code>. This value isn't used
901 * except for the purpose of a double-check that the form of the
902 * seralised data matches the caller's expectation.
904 * @n_elements, which must be non-%NULL is set equal to the number of
905 * items in the array.
910 g_variant_get_fixed_array (GVariant *value,
914 GVariantTypeInfo *array_info;
915 gsize array_element_size;
919 TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
921 g_return_val_if_fail (n_elements != NULL, NULL);
922 g_return_val_if_fail (element_size > 0, NULL);
924 array_info = g_variant_get_type_info (value);
925 g_variant_type_info_query_element (array_info, NULL, &array_element_size);
927 g_return_val_if_fail (array_element_size, NULL);
929 if G_UNLIKELY (array_element_size != element_size)
931 if (array_element_size)
932 g_critical ("g_variant_get_fixed_array: assertion "
933 "`g_variant_array_has_fixed_size (value, element_size)' "
934 "failed: array size %"G_GSIZE_FORMAT" does not match "
935 "given element_size %"G_GSIZE_FORMAT".",
936 array_element_size, element_size);
938 g_critical ("g_variant_get_fixed_array: assertion "
939 "`g_variant_array_has_fixed_size (value, element_size)' "
940 "failed: array does not have fixed size.");
943 data = g_variant_get_data (value);
944 size = g_variant_get_size (value);
946 if (size % element_size)
949 *n_elements = size / element_size;
957 /* String type constructor/getters/validation {{{1 */
959 * g_variant_new_string:
960 * @string: a normal C nul-terminated string
961 * @returns: a new string #GVariant instance
963 * Creates a string #GVariant with the contents of @string.
968 g_variant_new_string (const gchar *string)
970 g_return_val_if_fail (string != NULL, NULL);
972 return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING,
973 string, strlen (string) + 1);
977 * g_variant_new_object_path:
978 * @object_path: a normal C nul-terminated string
979 * @returns: a new object path #GVariant instance
981 * Creates a DBus object path #GVariant with the contents of @string.
982 * @string must be a valid DBus object path. Use
983 * g_variant_is_object_path() if you're not sure.
988 g_variant_new_object_path (const gchar *object_path)
990 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
992 return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH,
993 object_path, strlen (object_path) + 1);
997 * g_variant_is_object_path:
998 * @string: a normal C nul-terminated string
999 * @returns: %TRUE if @string is a DBus object path
1001 * Determines if a given string is a valid DBus object path. You
1002 * should ensure that a string is a valid DBus object path before
1003 * passing it to g_variant_new_object_path().
1005 * A valid object path starts with '/' followed by zero or more
1006 * sequences of characters separated by '/' characters. Each sequence
1007 * must contain only the characters "[A-Z][a-z][0-9]_". No sequence
1008 * (including the one following the final '/' character) may be empty.
1013 g_variant_is_object_path (const gchar *string)
1015 g_return_val_if_fail (string != NULL, FALSE);
1017 return g_variant_serialiser_is_object_path (string, strlen (string) + 1);
1021 * g_variant_new_signature:
1022 * @signature: a normal C nul-terminated string
1023 * @returns: a new signature #GVariant instance
1025 * Creates a DBus type signature #GVariant with the contents of
1026 * @string. @string must be a valid DBus type signature. Use
1027 * g_variant_is_signature() if you're not sure.
1032 g_variant_new_signature (const gchar *signature)
1034 g_return_val_if_fail (g_variant_is_signature (signature), NULL);
1036 return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE,
1037 signature, strlen (signature) + 1);
1041 * g_variant_is_signature:
1042 * @string: a normal C nul-terminated string
1043 * @returns: %TRUE if @string is a DBus type signature
1045 * Determines if a given string is a valid DBus type signature. You
1046 * should ensure that a string is a valid DBus object path before
1047 * passing it to g_variant_new_signature().
1049 * DBus type signatures consist of zero or more definite #GVariantType
1050 * strings in sequence.
1055 g_variant_is_signature (const gchar *string)
1057 g_return_val_if_fail (string != NULL, FALSE);
1059 return g_variant_serialiser_is_signature (string, strlen (string) + 1);
1063 * g_variant_get_string:
1064 * @value: a string #GVariant instance
1065 * @length: a pointer to a #gsize, to store the length
1066 * @returns: the constant string
1068 * Returns the string value of a #GVariant instance with a string
1069 * type. This includes the types %G_VARIANT_TYPE_STRING,
1070 * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
1072 * If @length is non-%NULL then the length of the string (in bytes) is
1073 * returned there. For trusted values, this information is already
1074 * known. For untrusted values, a strlen() will be performed.
1076 * It is an error to call this function with a @value of any type
1077 * other than those three.
1079 * The return value remains valid as long as @value exists.
1084 g_variant_get_string (GVariant *value,
1090 g_return_val_if_fail (value != NULL, NULL);
1091 g_return_val_if_fail (
1092 g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
1093 g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) ||
1094 g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL);
1096 data = g_variant_get_data (value);
1097 size = g_variant_get_size (value);
1099 if (!g_variant_is_trusted (value))
1101 switch (g_variant_classify (value))
1103 case G_VARIANT_CLASS_STRING:
1104 if (g_variant_serialiser_is_string (data, size))
1111 case G_VARIANT_CLASS_OBJECT_PATH:
1112 if (g_variant_serialiser_is_object_path (data, size))
1119 case G_VARIANT_CLASS_SIGNATURE:
1120 if (g_variant_serialiser_is_signature (data, size))
1128 g_assert_not_reached ();
1139 * g_variant_dup_string:
1140 * @value: a string #GVariant instance
1141 * @length: a pointer to a #gsize, to store the length
1142 * @returns: a newly allocated string
1144 * Similar to g_variant_get_string() except that instead of returning
1145 * a constant string, the string is duplicated.
1147 * The return value must be freed using g_free().
1152 g_variant_dup_string (GVariant *value,
1155 return g_strdup (g_variant_get_string (value, length));
1159 * g_variant_new_strv:
1160 * @strv: an array of strings
1161 * @length: the length of @strv, or -1
1162 * @returns: a new floating #GVariant instance
1164 * Constructs an array of strings #GVariant from the given array of
1167 * If @length is not -1 then it gives the maximum length of @strv. In
1168 * any case, a %NULL pointer in @strv is taken as a terminator.
1173 g_variant_new_strv (const gchar * const *strv,
1179 g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1182 length = g_strv_length ((gchar **) strv);
1184 strings = g_new (GVariant *, length);
1185 for (i = 0; i < length; i++)
1186 strings[i] = g_variant_ref_sink (g_variant_new_string (strv[i]));
1188 return g_variant_new_from_children (G_VARIANT_TYPE ("as"),
1189 strings, length, TRUE);
1193 * g_variant_get_strv:
1194 * @value: an array of strings #GVariant
1195 * @length: the length of the result, or %NULL
1196 * @returns: an array of constant strings
1198 * Gets the contents of an array of strings #GVariant. This call
1199 * makes a shallow copy; the return result should be released with
1200 * g_free(), but the individual strings must not be modified.
1202 * If @length is non-%NULL then the number of elements in the result
1203 * is stored there. In any case, the resulting array will be
1206 * For an empty array, @length will be set to 0 and a pointer to a
1207 * %NULL pointer will be returned.
1212 g_variant_get_strv (GVariant *value,
1219 g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("as")) ||
1220 g_variant_is_of_type (value, G_VARIANT_TYPE ("ao")) ||
1221 g_variant_is_of_type (value, G_VARIANT_TYPE ("ag")),
1224 g_variant_get_data (value);
1225 n = g_variant_n_children (value);
1226 strv = g_new (const gchar *, n + 1);
1228 for (i = 0; i < n; i++)
1232 string = g_variant_get_child_value (value, i);
1233 strv[i] = g_variant_get_string (string, NULL);
1234 g_variant_unref (string);
1245 * g_variant_dup_strv:
1246 * @value: an array of strings #GVariant
1247 * @length: the length of the result, or %NULL
1248 * @returns: an array of constant strings
1250 * Gets the contents of an array of strings #GVariant. This call
1251 * makes a deep copy; the return result should be released with
1254 * If @length is non-%NULL then the number of elements in the result
1255 * is stored there. In any case, the resulting array will be
1258 * For an empty array, @length will be set to 0 and a pointer to a
1259 * %NULL pointer will be returned.
1264 g_variant_dup_strv (GVariant *value,
1271 g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("as")) ||
1272 g_variant_is_of_type (value, G_VARIANT_TYPE ("ao")) ||
1273 g_variant_is_of_type (value, G_VARIANT_TYPE ("ag")),
1276 n = g_variant_n_children (value);
1277 strv = g_new (gchar *, n + 1);
1279 for (i = 0; i < n; i++)
1283 string = g_variant_get_child_value (value, i);
1284 strv[i] = g_variant_dup_string (string, NULL);
1285 g_variant_unref (string);
1295 /* Type checking and querying {{{1 */
1297 * g_variant_get_type:
1298 * @value: a #GVariant
1299 * @returns: a #GVariantType
1301 * Determines the type of @value.
1303 * The return value is valid for the lifetime of @value and must not
1308 const GVariantType *
1309 g_variant_get_type (GVariant *value)
1311 GVariantTypeInfo *type_info;
1313 g_return_val_if_fail (value != NULL, NULL);
1315 type_info = g_variant_get_type_info (value);
1317 return (GVariantType *) g_variant_type_info_get_type_string (type_info);
1321 * g_variant_get_type_string:
1322 * @value: a #GVariant
1323 * @returns: the type string for the type of @value
1325 * Returns the type string of @value. Unlike the result of calling
1326 * g_variant_type_peek_string(), this string is nul-terminated. This
1327 * string belongs to #GVariant and must not be freed.
1332 g_variant_get_type_string (GVariant *value)
1334 GVariantTypeInfo *type_info;
1336 g_return_val_if_fail (value != NULL, NULL);
1338 type_info = g_variant_get_type_info (value);
1340 return g_variant_type_info_get_type_string (type_info);
1344 * g_variant_is_of_type:
1345 * @value: a #GVariant instance
1346 * @type: a #GVariantType
1347 * @returns: %TRUE if the type of @value matches @type
1349 * Checks if a value has a type matching the provided type.
1354 g_variant_is_of_type (GVariant *value,
1355 const GVariantType *type)
1357 return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
1361 * g_variant_is_container:
1362 * @value: a #GVariant instance
1363 * @returns: %TRUE if @value is a container
1365 * Checks if @value is a container.
1368 g_variant_is_container (GVariant *value)
1370 return g_variant_type_is_container (g_variant_get_type (value));
1375 * g_variant_classify:
1376 * @value: a #GVariant
1377 * @returns: the #GVariantClass of @value
1379 * Classifies @value according to its top-level type.
1385 * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
1386 * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
1387 * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
1388 * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
1389 * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
1390 * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
1391 * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
1392 * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
1393 * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
1394 * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating
1396 * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
1397 * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a DBus object path
1399 * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a DBus signature string.
1400 * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
1401 * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
1402 * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
1403 * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
1404 * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
1406 * The range of possible top-level types of #GVariant instances.
1411 g_variant_classify (GVariant *value)
1413 g_return_val_if_fail (value != NULL, 0);
1415 return *g_variant_get_type_string (value);
1418 /* Pretty printer {{{1 */
1420 * g_variant_print_string:
1421 * @value: a #GVariant
1422 * @string: a #GString, or %NULL
1423 * @type_annotate: %TRUE if type information should be included in
1425 * @returns: a #GString containing the string
1427 * Behaves as g_variant_print(), but operates on a #GString.
1429 * If @string is non-%NULL then it is appended to and returned. Else,
1430 * a new empty #GString is allocated and it is returned.
1435 g_variant_print_string (GVariant *value,
1437 gboolean type_annotate)
1439 if G_UNLIKELY (string == NULL)
1440 string = g_string_new (NULL);
1442 switch (g_variant_classify (value))
1444 case G_VARIANT_CLASS_MAYBE:
1446 g_string_append_printf (string, "@%s ",
1447 g_variant_get_type_string (value));
1449 if (g_variant_n_children (value))
1451 gchar *printed_child;
1456 * Consider the case of the type "mmi". In this case we could
1457 * write "Just Just 4", but "4" alone is totally unambiguous,
1458 * so we try to drop "Just" where possible.
1460 * We have to be careful not to always drop "Just", though,
1461 * since "Nothing" needs to be distinguishable from "Just
1462 * Nothing". The case where we need to ensure we keep the
1463 * "Just" is actually exactly the case where we have a nested
1466 * Instead of searching for that nested Nothing, we just print
1467 * the contained value into a separate string and see if we
1468 * end up with "Nothing" at the end of it. If so, we need to
1469 * add "Just" at our level.
1471 element = g_variant_get_child_value (value, 0);
1472 printed_child = g_variant_print (element, FALSE);
1473 g_variant_unref (element);
1475 if (g_str_has_suffix (printed_child, "Nothing"))
1476 g_string_append (string, "Just ");
1477 g_string_append (string, printed_child);
1478 g_free (printed_child);
1481 g_string_append (string, "Nothing");
1485 case G_VARIANT_CLASS_ARRAY:
1486 /* it's an array so the first character of the type string is 'a'
1488 * if the first two characters are 'a{' then it's an array of
1489 * dictionary entries (ie: a dictionary) so we print that
1492 if (g_variant_get_type_string (value)[1] == '{')
1495 const gchar *comma = "";
1498 if ((n = g_variant_n_children (value)) == 0)
1501 g_string_append_printf (string, "@%s ",
1502 g_variant_get_type_string (value));
1503 g_string_append (string, "{}");
1507 g_string_append_c (string, '{');
1508 for (i = 0; i < n; i++)
1510 GVariant *entry, *key, *val;
1512 g_string_append (string, comma);
1515 entry = g_variant_get_child_value (value, i);
1516 key = g_variant_get_child_value (entry, 0);
1517 val = g_variant_get_child_value (entry, 1);
1518 g_variant_unref (entry);
1520 g_variant_print_string (key, string, type_annotate);
1521 g_variant_unref (key);
1522 g_string_append (string, ": ");
1523 g_variant_print_string (val, string, type_annotate);
1524 g_variant_unref (val);
1525 type_annotate = FALSE;
1527 g_string_append_c (string, '}');
1530 /* normal (non-dictionary) array */
1532 const gchar *comma = "";
1535 if ((n = g_variant_n_children (value)) == 0)
1538 g_string_append_printf (string, "@%s ",
1539 g_variant_get_type_string (value));
1540 g_string_append (string, "[]");
1544 g_string_append_c (string, '[');
1545 for (i = 0; i < n; i++)
1549 g_string_append (string, comma);
1552 element = g_variant_get_child_value (value, i);
1554 g_variant_print_string (element, string, type_annotate);
1555 g_variant_unref (element);
1556 type_annotate = FALSE;
1558 g_string_append_c (string, ']');
1563 case G_VARIANT_CLASS_TUPLE:
1567 n = g_variant_n_children (value);
1569 g_string_append_c (string, '(');
1570 for (i = 0; i < n; i++)
1574 element = g_variant_get_child_value (value, i);
1575 g_variant_print_string (element, string, type_annotate);
1576 g_string_append (string, ", ");
1577 g_variant_unref (element);
1580 /* for >1 item: remove final ", "
1581 * for 1 item: remove final " ", but leave the ","
1582 * for 0 items: there is only "(", so remove nothing
1584 g_string_truncate (string, string->len - (n > 0) - (n > 1));
1585 g_string_append_c (string, ')');
1589 case G_VARIANT_CLASS_DICT_ENTRY:
1593 g_string_append_c (string, '{');
1595 element = g_variant_get_child_value (value, 0);
1596 g_variant_print_string (element, string, type_annotate);
1597 g_variant_unref (element);
1599 g_string_append (string, ", ");
1601 element = g_variant_get_child_value (value, 1);
1602 g_variant_print_string (element, string, type_annotate);
1603 g_variant_unref (element);
1605 g_string_append_c (string, '}');
1609 case G_VARIANT_CLASS_VARIANT:
1611 GVariant *child = g_variant_get_variant (value);
1613 /* Always annotate types in nested variants, because they are
1614 * (by nature) of variable type.
1616 g_string_append_c (string, '<');
1617 g_variant_print_string (child, string, TRUE);
1618 g_string_append_c (string, '>');
1620 g_variant_unref (child);
1624 case G_VARIANT_CLASS_BOOLEAN:
1625 if (g_variant_get_boolean (value))
1626 g_string_append (string, "true");
1628 g_string_append (string, "false");
1631 case G_VARIANT_CLASS_STRING:
1633 const gchar *str = g_variant_get_string (value, NULL);
1634 gchar *escaped = g_strescape (str, NULL);
1636 g_string_append_printf (string, "\'%s\'", escaped);
1642 case G_VARIANT_CLASS_BYTE:
1644 g_string_append (string, "byte ");
1645 g_string_append_printf (string, "0x%02x",
1646 g_variant_get_byte (value));
1649 case G_VARIANT_CLASS_INT16:
1651 g_string_append (string, "int16 ");
1652 g_string_append_printf (string, "%"G_GINT16_FORMAT,
1653 g_variant_get_int16 (value));
1656 case G_VARIANT_CLASS_UINT16:
1658 g_string_append (string, "uint16 ");
1659 g_string_append_printf (string, "%"G_GUINT16_FORMAT,
1660 g_variant_get_uint16 (value));
1663 case G_VARIANT_CLASS_INT32:
1664 /* Never annotate this type because it is the default for numbers
1665 * (and this is a *pretty* printer)
1667 g_string_append_printf (string, "%"G_GINT32_FORMAT,
1668 g_variant_get_int32 (value));
1671 case G_VARIANT_CLASS_HANDLE:
1673 g_string_append (string, "handle ");
1674 g_string_append_printf (string, "%"G_GINT32_FORMAT,
1675 g_variant_get_handle (value));
1678 case G_VARIANT_CLASS_UINT32:
1680 g_string_append (string, "uint32 ");
1681 g_string_append_printf (string, "%"G_GUINT32_FORMAT,
1682 g_variant_get_uint32 (value));
1685 case G_VARIANT_CLASS_INT64:
1687 g_string_append (string, "int64 ");
1688 g_string_append_printf (string, "%"G_GINT64_FORMAT,
1689 g_variant_get_int64 (value));
1692 case G_VARIANT_CLASS_UINT64:
1694 g_string_append (string, "uint64 ");
1695 g_string_append_printf (string, "%"G_GUINT64_FORMAT,
1696 g_variant_get_uint64 (value));
1699 case G_VARIANT_CLASS_DOUBLE:
1704 g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
1706 for (i = 0; buffer[i]; i++)
1707 if (buffer[i] == '.' || buffer[i] == 'e' ||
1708 buffer[i] == 'n' || buffer[i] == 'N')
1711 /* if there is no '.' or 'e' in the float then add one */
1712 if (buffer[i] == '\0')
1719 g_string_append (string, buffer);
1723 case G_VARIANT_CLASS_OBJECT_PATH:
1725 g_string_append (string, "objectpath ");
1726 g_string_append_printf (string, "\'%s\'",
1727 g_variant_get_string (value, NULL));
1730 case G_VARIANT_CLASS_SIGNATURE:
1732 g_string_append (string, "signature ");
1733 g_string_append_printf (string, "\'%s\'",
1734 g_variant_get_string (value, NULL));
1738 g_assert_not_reached ();
1746 * @value: a #GVariant
1747 * @type_annotate: %TRUE if type information should be included in
1749 * @returns: a newly-allocated string holding the result.
1751 * Pretty-prints @value in the format understood by g_variant_parse().
1753 * If @type_annotate is %TRUE, then type information is included in
1757 g_variant_print (GVariant *value,
1758 gboolean type_annotate)
1760 return g_string_free (g_variant_print_string (value, NULL, type_annotate),
1764 /* Hash, Equal {{{1 */
1767 * @value: a basic #GVariant value as a #gconstpointer
1768 * @returns: a hash value corresponding to @value
1770 * Generates a hash value for a #GVariant instance.
1772 * The output of this function is guaranteed to be the same for a given
1773 * value only per-process. It may change between different processor
1774 * architectures or even different versions of GLib. Do not use this
1775 * function as a basis for building protocols or file formats.
1777 * The type of @value is #gconstpointer only to allow use of this
1778 * function with #GHashTable. @value must be a #GVariant.
1783 g_variant_hash (gconstpointer value_)
1785 GVariant *value = (GVariant *) value_;
1787 switch (g_variant_classify (value))
1789 case G_VARIANT_CLASS_STRING:
1790 case G_VARIANT_CLASS_OBJECT_PATH:
1791 case G_VARIANT_CLASS_SIGNATURE:
1792 return g_str_hash (g_variant_get_string (value, NULL));
1794 case G_VARIANT_CLASS_BOOLEAN:
1795 /* this is a very odd thing to hash... */
1796 return g_variant_get_boolean (value);
1798 case G_VARIANT_CLASS_BYTE:
1799 return g_variant_get_byte (value);
1801 case G_VARIANT_CLASS_INT16:
1802 case G_VARIANT_CLASS_UINT16:
1806 ptr = g_variant_get_data (value);
1814 case G_VARIANT_CLASS_INT32:
1815 case G_VARIANT_CLASS_UINT32:
1816 case G_VARIANT_CLASS_HANDLE:
1820 ptr = g_variant_get_data (value);
1828 case G_VARIANT_CLASS_INT64:
1829 case G_VARIANT_CLASS_UINT64:
1830 case G_VARIANT_CLASS_DOUBLE:
1831 /* need a separate case for these guys because otherwise
1832 * performance could be quite bad on big endian systems
1837 ptr = g_variant_get_data (value);
1840 return ptr[0] + ptr[1];
1846 g_return_val_if_fail (!g_variant_is_container (value), 0);
1847 g_assert_not_reached ();
1853 * @one: a #GVariant instance
1854 * @two: a #GVariant instance
1855 * @returns: %TRUE if @one and @two are equal
1857 * Checks if @one and @two have the same type and value.
1859 * The types of @one and @two are #gconstpointer only to allow use of
1860 * this function with #GHashTable. They must each be a #GVariant.
1865 g_variant_equal (gconstpointer one,
1870 g_return_val_if_fail (one != NULL && two != NULL, FALSE);
1872 if (g_variant_get_type_info ((GVariant *) one) !=
1873 g_variant_get_type_info ((GVariant *) two))
1876 /* if both values are trusted to be in their canonical serialised form
1877 * then a simple memcmp() of their serialised data will answer the
1880 * if not, then this might generate a false negative (since it is
1881 * possible for two different byte sequences to represent the same
1882 * value). for now we solve this by pretty-printing both values and
1883 * comparing the result.
1885 if (g_variant_is_trusted ((GVariant *) one) &&
1886 g_variant_is_trusted ((GVariant *) two))
1888 gconstpointer data_one, data_two;
1889 gsize size_one, size_two;
1891 size_one = g_variant_get_size ((GVariant *) one);
1892 size_two = g_variant_get_size ((GVariant *) two);
1894 if (size_one != size_two)
1897 data_one = g_variant_get_data ((GVariant *) one);
1898 data_two = g_variant_get_data ((GVariant *) two);
1900 equal = memcmp (data_one, data_two, size_one) == 0;
1904 gchar *strone, *strtwo;
1906 strone = g_variant_print ((GVariant *) one, FALSE);
1907 strtwo = g_variant_print ((GVariant *) two, FALSE);
1908 equal = strcmp (strone, strtwo) == 0;
1916 /* GVariantIter {{{1 */
1920 * #GVariantIter is an opaque data structure and can only be accessed
1921 * using the following functions.
1928 const gchar *loop_format;
1934 G_STATIC_ASSERT (sizeof (struct stack_iter) <= sizeof (GVariantIter));
1938 struct stack_iter iter;
1940 GVariant *value_ref;
1944 #define GVSI(i) ((struct stack_iter *) (i))
1945 #define GVHI(i) ((struct heap_iter *) (i))
1946 #define GVSI_MAGIC ((gsize) 3579507750u)
1947 #define GVHI_MAGIC ((gsize) 1450270775u)
1948 #define is_valid_iter(i) (i != NULL && \
1949 GVSI(i)->magic == GVSI_MAGIC)
1950 #define is_valid_heap_iter(i) (GVHI(i)->magic == GVHI_MAGIC && \
1954 * g_variant_iter_new:
1955 * @value: a container #GVariant
1956 * @returns: a new heap-allocated #GVariantIter
1958 * Creates a heap-allocated #GVariantIter for iterating over the items
1961 * Use g_variant_iter_free() to free the return value when you no longer
1964 * A reference is taken to @value and will be released only when
1965 * g_variant_iter_free() is called.
1970 g_variant_iter_new (GVariant *value)
1974 iter = (GVariantIter *) g_slice_new (struct heap_iter);
1975 GVHI(iter)->value_ref = g_variant_ref (value);
1976 GVHI(iter)->magic = GVHI_MAGIC;
1978 g_variant_iter_init (iter, value);
1984 * g_variant_iter_init:
1985 * @iter: a pointer to a #GVariantIter
1986 * @value: a container #GVariant
1987 * @returns: the number of items in @value
1989 * Initialises (without allocating) a #GVariantIter. @iter may be
1990 * completely uninitialised prior to this call; its old value is
1993 * The iterator remains valid for as long as @value exists, and need not
1994 * be freed in any way.
1999 g_variant_iter_init (GVariantIter *iter,
2002 GVSI(iter)->magic = GVSI_MAGIC;
2003 GVSI(iter)->value = value;
2004 GVSI(iter)->n = g_variant_n_children (value);
2006 GVSI(iter)->loop_format = NULL;
2008 return GVSI(iter)->n;
2012 * g_variant_iter_copy:
2013 * @iter: a #GVariantIter
2014 * @returns: a new heap-allocated #GVariantIter
2016 * Creates a new heap-allocated #GVariantIter to iterate over the
2017 * container that was being iterated over by @iter. Iteration begins on
2018 * the new iterator from the current position of the old iterator but
2019 * the two copies are independent past that point.
2021 * Use g_variant_iter_free() to free the return value when you no longer
2024 * A reference is taken to the container that @iter is iterating over
2025 * and will be releated only when g_variant_iter_free() is called.
2030 g_variant_iter_copy (GVariantIter *iter)
2034 g_return_val_if_fail (is_valid_iter (iter), 0);
2036 copy = g_variant_iter_new (GVSI(iter)->value);
2037 GVSI(copy)->i = GVSI(iter)->i;
2043 * g_variant_iter_n_children:
2044 * @iter: a #GVariantIter
2045 * @returns: the number of children in the container
2047 * Queries the number of child items in the container that we are
2048 * iterating over. This is the total number of items -- not the number
2049 * of items remaining.
2051 * This function might be useful for preallocation of arrays.
2056 g_variant_iter_n_children (GVariantIter *iter)
2058 g_return_val_if_fail (is_valid_iter (iter), 0);
2060 return GVSI(iter)->n;
2064 * g_variant_iter_free:
2065 * @iter: a heap-allocated #GVariantIter
2067 * Frees a heap-allocated #GVariantIter. Only call this function on
2068 * iterators that were returned by g_variant_iter_new() or
2069 * g_variant_iter_copy().
2074 g_variant_iter_free (GVariantIter *iter)
2076 g_return_if_fail (is_valid_heap_iter (iter));
2078 g_variant_unref (GVHI(iter)->value_ref);
2079 GVHI(iter)->magic = 0;
2081 g_slice_free (struct heap_iter, GVHI(iter));
2085 * g_variant_iter_next_value:
2086 * @iter: a #GVariantIter
2087 * @returns: a #GVariant, or %NULL
2089 * Gets the next item in the container. If no more items remain then
2090 * %NULL is returned.
2092 * Use g_variant_unref() to drop your reference on the return value when
2093 * you no longer need it.
2096 * <title>Iterating with g_variant_iter_next_value()</title>
2098 * /<!-- -->* recursively iterate a container *<!-- -->/
2100 * iterate_container_recursive (GVariant *container)
2102 * GVariantIter iter;
2105 * g_variant_iter_init (&iter, dictionary);
2106 * while ((child = g_variant_iter_next_value (&iter)))
2108 * g_print ("type '%s'\n", g_variant_get_type_string (child));
2110 * if (g_variant_is_container (child))
2111 * iterate_container_recursive (child);
2113 * g_variant_unref (child);
2122 g_variant_iter_next_value (GVariantIter *iter)
2124 g_return_val_if_fail (is_valid_iter (iter), FALSE);
2126 if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n)
2128 g_critical ("g_variant_iter_next_value: must not be called again "
2129 "after NULL has already been returned.");
2135 if (GVSI(iter)->i < GVSI(iter)->n)
2136 return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i);
2141 /* GVariantBuilder {{{1 */
2145 * A utility type for constructing container-type #GVariant instances.
2147 * This is an opaque structure and may only be accessed using the
2148 * following functions.
2150 * #GVariantBuilder is not threadsafe in any way. Do not attempt to
2151 * access it from more than one thread.
2154 struct stack_builder
2156 GVariantBuilder *parent;
2159 /* type constraint explicitly specified by 'type'.
2160 * for tuple types, this moves along as we add more items.
2162 const GVariantType *expected_type;
2164 /* type constraint implied by previous array item.
2166 const GVariantType *prev_item_type;
2168 /* constraints on the number of children. max = -1 for unlimited. */
2172 /* dynamically-growing pointer array */
2173 GVariant **children;
2174 gsize allocated_children;
2177 /* set to '1' if all items in the container will have the same type
2178 * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
2180 guint uniform_item_types : 1;
2182 /* set to '1' initially and changed to '0' if an untrusted value is
2190 G_STATIC_ASSERT (sizeof (struct stack_builder) <= sizeof (GVariantBuilder));
2194 GVariantBuilder builder;
2200 #define GVSB(b) ((struct stack_builder *) (b))
2201 #define GVHB(b) ((struct heap_builder *) (b))
2202 #define GVSB_MAGIC ((gsize) 1033660112u)
2203 #define GVHB_MAGIC ((gsize) 3087242682u)
2204 #define is_valid_builder(b) (b != NULL && \
2205 GVSB(b)->magic == GVSB_MAGIC)
2206 #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
2209 * g_variant_builder_new:
2210 * @type: a container type
2211 * @returns: a #GVariantBuilder
2213 * Allocates and initialises a new #GVariantBuilder.
2215 * You should call g_variant_builder_unref() on the return value when it
2216 * is no longer needed. The memory will not be automatically freed by
2219 * In most cases it is easier to place a #GVariantBuilder directly on
2220 * the stack of the calling function and initialise it with
2221 * g_variant_builder_init().
2226 g_variant_builder_new (const GVariantType *type)
2228 GVariantBuilder *builder;
2230 builder = (GVariantBuilder *) g_slice_new (struct heap_builder);
2231 g_variant_builder_init (builder, type);
2232 GVHB(builder)->magic = GVHB_MAGIC;
2233 GVHB(builder)->ref_count = 1;
2239 * g_variant_builder_unref:
2240 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
2242 * Decreases the reference count on @builder.
2244 * In the event that there are no more references, releases all memory
2245 * associated with the #GVariantBuilder.
2247 * Don't call this on stack-allocated #GVariantBuilder instances or bad
2248 * things will happen.
2253 g_variant_builder_unref (GVariantBuilder *builder)
2255 g_return_if_fail (is_valid_heap_builder (builder));
2257 if (--GVHB(builder)->ref_count)
2260 g_variant_builder_clear (builder);
2261 GVHB(builder)->magic = 0;
2263 g_slice_free (struct heap_builder, GVHB(builder));
2267 * g_variant_builder_ref:
2268 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
2269 * @returns: a new reference to @builder
2271 * Increases the reference count on @builder.
2273 * Don't call this on stack-allocated #GVariantBuilder instances or bad
2274 * things will happen.
2279 g_variant_builder_ref (GVariantBuilder *builder)
2281 g_return_val_if_fail (is_valid_heap_builder (builder), NULL);
2283 GVHB(builder)->ref_count++;
2289 * g_variant_builder_clear:
2290 * @builder: a #GVariantBuilder
2292 * Releases all memory associated with a #GVariantBuilder without
2293 * freeing the #GVariantBuilder structure itself.
2295 * It typically only makes sense to do this on a stack-allocated
2296 * #GVariantBuilder if you want to abort building the value part-way
2297 * through. This function need not be called if you call
2298 * g_variant_builder_end() and it also doesn't need to be called on
2299 * builders allocated with g_variant_builder_new (see
2300 * g_variant_builder_free() for that).
2302 * This function leaves the #GVariantBuilder structure set to all-zeros.
2303 * It is valid to call this function on either an initialised
2304 * #GVariantBuilder or one that is set to all-zeros but it is not valid
2305 * to call this function on uninitialised memory.
2310 g_variant_builder_clear (GVariantBuilder *builder)
2314 if (GVSB(builder)->magic == 0)
2315 /* all-zeros case */
2318 g_return_if_fail (is_valid_builder (builder));
2320 g_variant_type_free (GVSB(builder)->type);
2322 for (i = 0; i < GVSB(builder)->offset; i++)
2323 g_variant_unref (GVSB(builder)->children[i]);
2325 g_free (GVSB(builder)->children);
2327 if (GVSB(builder)->parent)
2329 g_variant_builder_clear (GVSB(builder)->parent);
2330 g_slice_free (GVariantBuilder, GVSB(builder)->parent);
2333 memset (builder, 0, sizeof (GVariantBuilder));
2337 * g_variant_builder_init:
2338 * @builder: a #GVariantBuilder
2339 * @type: a container type
2341 * Initialises a #GVariantBuilder structure.
2343 * @type must be non-%NULL. It specifies the type of container to
2344 * construct. It can be an indefinite type such as
2345 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
2346 * Maybe, array, tuple, dictionary entry and variant-typed values may be
2349 * After the builder is initialised, values are added using
2350 * g_variant_builder_add_value() or g_variant_builder_add().
2352 * After all the child values are added, g_variant_builder_end() frees
2353 * the memory associated with the builder and returns the #GVariant that
2356 * This function completely ignores the previous contents of @builder.
2357 * On one hand this means that it is valid to pass in completely
2358 * uninitialised memory. On the other hand, this means that if you are
2359 * initialising over top of an existing #GVariantBuilder you need to
2360 * first call g_variant_builder_clear() in order to avoid leaking
2363 * You must not call g_variant_builder_ref() or
2364 * g_variant_builder_unref() on a #GVariantBuilder that was initialised
2365 * with this function. If you ever pass a reference to a
2366 * #GVariantBuilder outside of the control of your own code then you
2367 * should assume that the person receiving that reference may try to use
2368 * reference counting; you should use g_variant_builder_new() instead of
2374 g_variant_builder_init (GVariantBuilder *builder,
2375 const GVariantType *type)
2377 g_return_if_fail (type != NULL);
2378 g_return_if_fail (g_variant_type_is_container (type));
2380 memset (builder, 0, sizeof (GVariantBuilder));
2382 GVSB(builder)->type = g_variant_type_copy (type);
2383 GVSB(builder)->magic = GVSB_MAGIC;
2384 GVSB(builder)->trusted = TRUE;
2386 switch (*(const gchar *) type)
2388 case G_VARIANT_CLASS_VARIANT:
2389 GVSB(builder)->uniform_item_types = TRUE;
2390 GVSB(builder)->allocated_children = 1;
2391 GVSB(builder)->expected_type = NULL;
2392 GVSB(builder)->min_items = 1;
2393 GVSB(builder)->max_items = 1;
2396 case G_VARIANT_CLASS_ARRAY:
2397 GVSB(builder)->uniform_item_types = TRUE;
2398 GVSB(builder)->allocated_children = 8;
2399 GVSB(builder)->expected_type =
2400 g_variant_type_element (GVSB(builder)->type);
2401 GVSB(builder)->min_items = 0;
2402 GVSB(builder)->max_items = -1;
2405 case G_VARIANT_CLASS_MAYBE:
2406 GVSB(builder)->uniform_item_types = TRUE;
2407 GVSB(builder)->allocated_children = 1;
2408 GVSB(builder)->expected_type =
2409 g_variant_type_element (GVSB(builder)->type);
2410 GVSB(builder)->min_items = 0;
2411 GVSB(builder)->max_items = 1;
2414 case G_VARIANT_CLASS_DICT_ENTRY:
2415 GVSB(builder)->uniform_item_types = FALSE;
2416 GVSB(builder)->allocated_children = 2;
2417 GVSB(builder)->expected_type =
2418 g_variant_type_key (GVSB(builder)->type);
2419 GVSB(builder)->min_items = 2;
2420 GVSB(builder)->max_items = 2;
2423 case 'r': /* G_VARIANT_TYPE_TUPLE was given */
2424 GVSB(builder)->uniform_item_types = FALSE;
2425 GVSB(builder)->allocated_children = 8;
2426 GVSB(builder)->expected_type = NULL;
2427 GVSB(builder)->min_items = 0;
2428 GVSB(builder)->max_items = -1;
2431 case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */
2432 GVSB(builder)->allocated_children = g_variant_type_n_items (type);
2433 GVSB(builder)->expected_type =
2434 g_variant_type_first (GVSB(builder)->type);
2435 GVSB(builder)->min_items = GVSB(builder)->allocated_children;
2436 GVSB(builder)->max_items = GVSB(builder)->allocated_children;
2437 GVSB(builder)->uniform_item_types = FALSE;
2441 g_assert_not_reached ();
2444 GVSB(builder)->children = g_new (GVariant *,
2445 GVSB(builder)->allocated_children);
2449 g_variant_builder_make_room (struct stack_builder *builder)
2451 if (builder->offset == builder->allocated_children)
2453 builder->allocated_children *= 2;
2454 builder->children = g_renew (GVariant *, builder->children,
2455 builder->allocated_children);
2460 * g_variant_builder_add_value:
2461 * @builder: a #GVariantBuilder
2462 * @value: a #GVariant
2464 * Adds @value to @builder.
2466 * It is an error to call this function in any way that would create an
2467 * inconsistent value to be constructed. Some examples of this are
2468 * putting different types of items into an array, putting the wrong
2469 * types or number of items in a tuple, putting more than one value into
2475 g_variant_builder_add_value (GVariantBuilder *builder,
2478 g_return_if_fail (is_valid_builder (builder));
2479 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
2480 g_return_if_fail (!GVSB(builder)->expected_type ||
2481 g_variant_is_of_type (value,
2482 GVSB(builder)->expected_type));
2483 g_return_if_fail (!GVSB(builder)->prev_item_type ||
2484 g_variant_is_of_type (value,
2485 GVSB(builder)->prev_item_type));
2487 GVSB(builder)->trusted &= g_variant_is_trusted (value);
2489 if (!GVSB(builder)->uniform_item_types)
2491 /* advance our expected type pointers */
2492 if (GVSB(builder)->expected_type)
2493 GVSB(builder)->expected_type =
2494 g_variant_type_next (GVSB(builder)->expected_type);
2496 if (GVSB(builder)->prev_item_type)
2497 GVSB(builder)->prev_item_type =
2498 g_variant_type_next (GVSB(builder)->prev_item_type);
2501 GVSB(builder)->prev_item_type = g_variant_get_type (value);
2503 g_variant_builder_make_room (GVSB(builder));
2505 GVSB(builder)->children[GVSB(builder)->offset++] =
2506 g_variant_ref_sink (value);
2510 * g_variant_builder_open:
2511 * @builder: a #GVariantBuilder
2512 * @type: a #GVariantType
2514 * Opens a subcontainer inside the given @builder. When done adding
2515 * items to the subcontainer, g_variant_builder_close() must be called.
2517 * It is an error to call this function in any way that would cause an
2518 * inconsistent value to be constructed (ie: adding too many values or
2519 * a value of an incorrect type).
2524 g_variant_builder_open (GVariantBuilder *builder,
2525 const GVariantType *type)
2527 GVariantBuilder *parent;
2529 g_return_if_fail (is_valid_builder (builder));
2530 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
2531 g_return_if_fail (!GVSB(builder)->expected_type ||
2532 g_variant_type_is_subtype_of (type,
2533 GVSB(builder)->expected_type));
2534 g_return_if_fail (!GVSB(builder)->prev_item_type ||
2535 g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type,
2538 parent = g_slice_dup (GVariantBuilder, builder);
2539 g_variant_builder_init (builder, type);
2540 GVSB(builder)->parent = parent;
2542 /* push the prev_item_type down into the subcontainer */
2543 if (GVSB(parent)->prev_item_type)
2545 if (!GVSB(builder)->uniform_item_types)
2546 /* tuples and dict entries */
2547 GVSB(builder)->prev_item_type =
2548 g_variant_type_first (GVSB(parent)->prev_item_type);
2550 else if (!g_variant_type_is_variant (GVSB(builder)->type))
2551 /* maybes and arrays */
2552 GVSB(builder)->prev_item_type =
2553 g_variant_type_element (GVSB(parent)->prev_item_type);
2558 * g_variant_builder_close:
2559 * @builder: a #GVariantBuilder
2561 * Closes the subcontainer inside the given @builder that was opened by
2562 * the most recent call to g_variant_builder_open().
2564 * It is an error to call this function in any way that would create an
2565 * inconsistent value to be constructed (ie: too few values added to the
2571 g_variant_builder_close (GVariantBuilder *builder)
2573 GVariantBuilder *parent;
2575 g_return_if_fail (is_valid_builder (builder));
2576 g_return_if_fail (GVSB(builder)->parent != NULL);
2578 parent = GVSB(builder)->parent;
2579 GVSB(builder)->parent = NULL;
2581 g_variant_builder_add_value (parent, g_variant_builder_end (builder));
2584 g_slice_free (GVariantBuilder, parent);
2588 * g_variant_make_maybe_type:
2589 * @element: a #GVariant
2591 * Return the type of a maybe containing @element.
2593 static GVariantType *
2594 g_variant_make_maybe_type (GVariant *element)
2596 return g_variant_type_new_maybe (g_variant_get_type (element));
2600 * g_variant_make_array_type:
2601 * @element: a #GVariant
2603 * Return the type of an array containing @element.
2605 static GVariantType *
2606 g_variant_make_array_type (GVariant *element)
2608 return g_variant_type_new_array (g_variant_get_type (element));
2612 * g_variant_builder_end:
2613 * @builder: a #GVariantBuilder
2614 * @returns: a new, floating, #GVariant
2616 * Ends the builder process and returns the constructed value.
2618 * This call automatically reduces the reference count on @builder by
2619 * one, unless it has previously had g_variant_builder_no_autofree()
2620 * called on it. Unless you've taken other actions, this is usually
2621 * sufficient to free @builder.
2623 * Even if additional references are held, it is not permissible to use
2624 * @builder in any way after this call except for further reference
2625 * counting operations.
2627 * It is an error to call this function in any way that would create an
2628 * inconsistent value to be constructed (ie: insufficient number of
2629 * items added to a container with a specific number of children
2630 * required). It is also an error to call this function if the builder
2631 * was created with an indefinite array or maybe type and no children
2632 * have been added; in this case it is impossible to infer the type of
2638 g_variant_builder_end (GVariantBuilder *builder)
2640 GVariantType *my_type;
2643 g_return_val_if_fail (is_valid_builder (builder), NULL);
2644 g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items,
2646 g_return_val_if_fail (!GVSB(builder)->uniform_item_types ||
2647 GVSB(builder)->prev_item_type != NULL ||
2648 g_variant_type_is_definite (GVSB(builder)->type),
2651 if (g_variant_type_is_definite (GVSB(builder)->type))
2652 my_type = g_variant_type_copy (GVSB(builder)->type);
2654 else if (g_variant_type_is_maybe (GVSB(builder)->type))
2655 my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]);
2657 else if (g_variant_type_is_array (GVSB(builder)->type))
2658 my_type = g_variant_make_array_type (GVSB(builder)->children[0]);
2660 else if (g_variant_type_is_tuple (GVSB(builder)->type))
2661 my_type = g_variant_make_tuple_type (GVSB(builder)->children,
2662 GVSB(builder)->offset);
2664 else if (g_variant_type_is_dict_entry (GVSB(builder)->type))
2665 my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0],
2666 GVSB(builder)->children[1]);
2668 g_assert_not_reached ();
2670 value = g_variant_new_from_children (my_type,
2671 g_renew (GVariant *,
2672 GVSB(builder)->children,
2673 GVSB(builder)->offset),
2674 GVSB(builder)->offset,
2675 GVSB(builder)->trusted);
2676 GVSB(builder)->children = NULL;
2677 GVSB(builder)->offset = 0;
2679 g_variant_builder_clear (builder);
2680 g_variant_type_free (my_type);
2685 /* Format strings {{{1 */
2687 * g_variant_format_string_scan:
2688 * @string: a string that may be prefixed with a format string
2689 * @limit: a pointer to the end of @string, or %NULL
2690 * @endptr: location to store the end pointer, or %NULL
2691 * @returns: %TRUE if there was a valid format string
2693 * Checks the string pointed to by @string for starting with a properly
2694 * formed #GVariant varargs format string. If no valid format string is
2695 * found then %FALSE is returned.
2697 * If @string does start with a valid format string then %TRUE is
2698 * returned. If @endptr is non-%NULL then it is updated to point to the
2699 * first character after the format string.
2701 * If @limit is non-%NULL then @limit (and any charater after it) will
2702 * not be accessed and the effect is otherwise equivalent to if the
2703 * character at @limit were nul.
2705 * See the section on <link linkend='gvariant-format-strings'>GVariant
2706 * Format Strings</link>.
2711 g_variant_format_string_scan (const gchar *string,
2713 const gchar **endptr)
2715 #define next_char() (string == limit ? '\0' : *string++)
2716 #define peek_char() (string == limit ? '\0' : *string)
2719 switch (next_char())
2721 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
2722 case 'x': case 't': case 'h': case 'd': case 's': case 'o':
2723 case 'g': case 'v': case '*': case '?': case 'r':
2727 return g_variant_format_string_scan (string, limit, endptr);
2731 return g_variant_type_string_scan (string, limit, endptr);
2734 while (peek_char() != ')')
2735 if (!g_variant_format_string_scan (string, limit, &string))
2738 next_char(); /* consume ')' */
2747 /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
2748 * The terminating null character is considered to be
2749 * part of the string.
2751 if (c != '\0' && strchr ("bynqiuxthdsog?", c) == NULL)
2754 if (!g_variant_format_string_scan (string, limit, &string))
2757 if (next_char() != '}')
2762 case '^': /* '^as' or '^a&s' only */
2763 if (next_char() != 'a')
2766 if (peek_char() == '&')
2771 if (c != 's' && c != 'o' && c != 'g')
2779 if (c != 's' && c != 'o' && c != 'g')
2798 * g_variant_format_string_scan_type:
2799 * @string: a string that may be prefixed with a format string
2800 * @limit: a pointer to the end of @string
2801 * @endptr: location to store the end pointer, or %NULL
2802 * @returns: a #GVariantType if there was a valid format string
2804 * If @string starts with a valid format string then this function will
2805 * return the type that the format string corresponds to. Otherwise
2806 * this function returns %NULL.
2808 * Use g_variant_type_free() to free the return value when you no longer
2811 * This function is otherwise exactly like
2812 * g_variant_format_string_scan().
2817 g_variant_format_string_scan_type (const gchar *string,
2819 const gchar **endptr)
2821 const gchar *my_end;
2828 if (!g_variant_format_string_scan (string, limit, endptr))
2831 dest = new = g_malloc (*endptr - string + 1);
2832 while (string != *endptr)
2834 if (*string != '@' && *string != '&' && *string != '^')
2840 return (GVariantType *) G_VARIANT_TYPE (new);
2844 valid_format_string (const gchar *format_string,
2848 const gchar *endptr;
2851 type = g_variant_format_string_scan_type (format_string, NULL, &endptr);
2853 if G_UNLIKELY (type == NULL || (single && *endptr != '\0'))
2856 g_critical ("`%s' is not a valid GVariant format string",
2859 g_critical ("`%s' does not have a valid GVariant format "
2860 "string as a prefix", format_string);
2863 g_variant_type_free (type);
2868 if G_UNLIKELY (value && !g_variant_is_of_type (value, type))
2873 fragment = g_strndup (format_string, endptr - format_string);
2874 typestr = g_variant_type_dup_string (type);
2876 g_critical ("the GVariant format string `%s' has a type of "
2877 "`%s' but the given value has a type of `%s'",
2878 fragment, typestr, g_variant_get_type_string (value));
2880 g_variant_type_free (type);
2885 g_variant_type_free (type);
2890 /* Variable Arguments {{{1 */
2891 /* We consider 2 main classes of format strings:
2893 * - recursive format strings
2894 * these are ones that result in recursion and the collection of
2895 * possibly more than one argument. Maybe types, tuples,
2896 * dictionary entries.
2898 * - leaf format string
2899 * these result in the collection of a single argument.
2901 * Leaf format strings are further subdivided into two categories:
2903 * - single non-null pointer ("nnp")
2904 * these either collect or return a single non-null pointer.
2907 * these collect or return something else (bool, number, etc).
2909 * Based on the above, the varargs handling code is split into 4 main parts:
2911 * - nnp handling code
2912 * - leaf handling code (which may invoke nnp code)
2913 * - generic handling code (may be recursive, may invoke leaf code)
2914 * - user-facing API (which invokes the generic code)
2916 * Each section implements some of the following functions:
2919 * collect the arguments for the format string as if
2920 * g_variant_new() had been called, but do nothing with them. used
2921 * for skipping over arguments when constructing a Nothing maybe
2925 * create a GVariant *
2928 * unpack a GVariant *
2930 * - free (nnp only):
2931 * free a previously allocated item
2935 g_variant_format_string_is_leaf (const gchar *str)
2937 return str[0] != 'm' && str[0] != '(' && str[0] != '{';
2941 g_variant_format_string_is_nnp (const gchar *str)
2943 return str[0] == 'a' || str[0] == 's' || str[0] == 'o' || str[0] == 'g' ||
2944 str[0] == '^' || str[0] == '@' || str[0] == '*' || str[0] == '?' ||
2945 str[0] == 'r' || str[0] == 'v' || str[0] == '&';
2948 /* Single non-null pointer ("nnp") {{{2 */
2950 g_variant_valist_free_nnp (const gchar *str,
2956 g_variant_iter_free (ptr);
2960 if (str[2] != '&') /* '^as' */
2976 g_variant_unref (ptr);
2983 g_assert_not_reached ();
2988 g_variant_valist_new_nnp (const gchar **str,
2998 const GVariantType *type;
3001 value = g_variant_builder_end (ptr);
3002 type = g_variant_get_type (value);
3004 if G_UNLIKELY (!g_variant_type_is_array (type))
3005 g_error ("g_variant_new: expected array GVariantBuilder but "
3006 "the built value has type `%s'",
3007 g_variant_get_type_string (value));
3009 type = g_variant_type_element (type);
3011 if G_UNLIKELY (!g_variant_type_is_subtype_of (type, (GVariantType *) *str))
3012 g_error ("g_variant_new: expected GVariantBuilder array element "
3013 "type `%s' but the built value has element type `%s'",
3014 g_variant_type_dup_string ((GVariantType *) *str),
3015 g_variant_get_type_string (value) + 1);
3017 g_variant_type_string_scan (*str, NULL, str);
3023 return g_variant_new_string (ptr);
3026 return g_variant_new_object_path (ptr);
3029 return g_variant_new_signature (ptr);
3033 const GVariantType *type;
3034 GVariantType *array_type;
3035 GVariant **children;
3040 if ((*str)[1] == '&') /* '^a&s' */
3045 type = (GVariantType *) (*str)++;
3046 array_type = g_variant_type_new_array (type);
3047 length = g_strv_length (strv);
3048 children = g_new (GVariant *, length);
3049 for (i = 0; i < length; i++)
3050 children[i] = g_variant_ref_sink (
3051 g_variant_new_from_trusted (type, strv[i], strlen (strv[i]) + 1));
3053 value = g_variant_new_from_children (array_type, children,
3055 g_variant_type_free (array_type);
3061 if G_UNLIKELY (!g_variant_is_of_type (ptr, (GVariantType *) *str))
3062 g_error ("g_variant_new: expected GVariant of type `%s' but "
3063 "received value has type `%s'",
3064 g_variant_type_dup_string ((GVariantType *) *str),
3065 g_variant_get_type_string (ptr));
3067 g_variant_type_string_scan (*str, NULL, str);
3075 if G_UNLIKELY (!g_variant_type_is_basic (g_variant_get_type (ptr)))
3076 g_error ("g_variant_new: format string `?' expects basic-typed "
3077 "GVariant, but received value has type `%s'",
3078 g_variant_get_type_string (ptr));
3083 if G_UNLIKELY (!g_variant_type_is_tuple (g_variant_get_type (ptr)))
3084 g_error ("g_variant_new: format string `r` expects tuple-typed "
3085 "GVariant, but received value has type `%s'",
3086 g_variant_get_type_string (ptr));
3091 return g_variant_new_variant (ptr);
3094 g_assert_not_reached ();
3099 g_variant_valist_get_nnp (const gchar **str,
3105 g_variant_type_string_scan (*str, NULL, str);
3106 return g_variant_iter_new (value);
3110 return (gchar *) g_variant_get_string (value, NULL);
3115 return g_variant_dup_string (value, NULL);
3118 if ((*str)[1] == '&') /* '^a&s' */
3121 return g_variant_get_strv (value, NULL);
3126 return g_variant_dup_strv (value, NULL);
3130 g_variant_type_string_scan (*str, NULL, str);
3136 return g_variant_ref (value);
3139 return g_variant_get_variant (value);
3142 g_assert_not_reached ();
3148 g_variant_valist_skip_leaf (const gchar **str,
3151 if (g_variant_format_string_is_nnp (*str))
3153 g_variant_format_string_scan (*str, NULL, str);
3154 va_arg (*app, gpointer);
3172 va_arg (*app, guint64);
3176 va_arg (*app, gdouble);
3180 g_assert_not_reached ();
3185 g_variant_valist_new_leaf (const gchar **str,
3188 if (g_variant_format_string_is_nnp (*str))
3189 return g_variant_valist_new_nnp (str, va_arg (*app, gpointer));
3194 return g_variant_new_boolean (va_arg (*app, gboolean));
3197 return g_variant_new_byte (va_arg (*app, guint));
3200 return g_variant_new_int16 (va_arg (*app, gint));
3203 return g_variant_new_uint16 (va_arg (*app, guint));
3206 return g_variant_new_int32 (va_arg (*app, gint));
3209 return g_variant_new_uint32 (va_arg (*app, guint));
3212 return g_variant_new_int64 (va_arg (*app, gint64));
3215 return g_variant_new_uint64 (va_arg (*app, guint64));
3218 return g_variant_new_handle (va_arg (*app, gint));
3221 return g_variant_new_double (va_arg (*app, gdouble));
3224 g_assert_not_reached ();
3228 /* The code below assumes this */
3229 G_STATIC_ASSERT (sizeof (gboolean) == sizeof (guint32));
3230 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
3233 g_variant_valist_get_leaf (const gchar **str,
3238 gpointer ptr = va_arg (*app, gpointer);
3242 g_variant_format_string_scan (*str, NULL, str);
3246 if (g_variant_format_string_is_nnp (*str))
3248 gpointer *nnp = (gpointer *) ptr;
3250 if (free && *nnp != NULL)
3251 g_variant_valist_free_nnp (*str, *nnp);
3256 *nnp = g_variant_valist_get_nnp (str, value);
3258 g_variant_format_string_scan (*str, NULL, str);
3268 *(gboolean *) ptr = g_variant_get_boolean (value);
3272 *(guchar *) ptr = g_variant_get_byte (value);
3276 *(gint16 *) ptr = g_variant_get_int16 (value);
3280 *(guint16 *) ptr = g_variant_get_uint16 (value);
3284 *(gint32 *) ptr = g_variant_get_int32 (value);
3288 *(guint32 *) ptr = g_variant_get_uint32 (value);
3292 *(gint64 *) ptr = g_variant_get_int64 (value);
3296 *(guint64 *) ptr = g_variant_get_uint64 (value);
3300 *(gint32 *) ptr = g_variant_get_handle (value);
3304 *(gdouble *) ptr = g_variant_get_double (value);
3313 *(guchar *) ptr = 0;
3318 *(guint16 *) ptr = 0;
3325 *(guint32 *) ptr = 0;
3331 *(guint64 *) ptr = 0;
3336 g_assert_not_reached ();
3339 /* Generic (recursive) {{{2 */
3341 g_variant_valist_skip (const gchar **str,
3344 if (g_variant_format_string_is_leaf (*str))
3345 g_variant_valist_skip_leaf (str, app);
3347 else if (**str == 'm') /* maybe */
3351 if (!g_variant_format_string_is_nnp (*str))
3352 va_arg (*app, gboolean);
3354 g_variant_valist_skip (str, app);
3356 else /* tuple, dictionary entry */
3358 g_assert (**str == '(' || **str == '{');
3360 while (**str != ')' && **str != '}')
3361 g_variant_valist_skip (str, app);
3367 g_variant_valist_new (const gchar **str,
3370 if (g_variant_format_string_is_leaf (*str))
3371 return g_variant_valist_new_leaf (str, app);
3373 if (**str == 'm') /* maybe */
3375 GVariantType *type = NULL;
3376 GVariant *value = NULL;
3380 if (g_variant_format_string_is_nnp (*str))
3382 gpointer nnp = va_arg (*app, gpointer);
3385 value = g_variant_valist_new_nnp (str, nnp);
3387 type = g_variant_format_string_scan_type (*str, NULL, str);
3391 gboolean just = va_arg (*app, gboolean);
3394 value = g_variant_valist_new (str, app);
3397 type = g_variant_format_string_scan_type (*str, NULL, NULL);
3398 g_variant_valist_skip (str, app);
3402 value = g_variant_new_maybe (type, value);
3405 g_variant_type_free (type);
3409 else /* tuple, dictionary entry */
3414 g_variant_builder_init (&b, G_VARIANT_TYPE_TUPLE);
3417 g_assert (**str == '{');
3418 g_variant_builder_init (&b, G_VARIANT_TYPE_DICT_ENTRY);
3422 while (**str != ')' && **str != '}')
3423 g_variant_builder_add_value (&b, g_variant_valist_new (str, app));
3426 return g_variant_builder_end (&b);
3431 g_variant_valist_get (const gchar **str,
3436 if (g_variant_format_string_is_leaf (*str))
3437 g_variant_valist_get_leaf (str, value, free, app);
3439 else if (**str == 'm')
3444 value = g_variant_get_maybe (value);
3446 if (!g_variant_format_string_is_nnp (*str))
3448 gboolean *ptr = va_arg (*app, gboolean *);
3451 *ptr = value != NULL;
3454 g_variant_valist_get (str, value, free, app);
3457 g_variant_unref (value);
3460 else /* tuple, dictionary entry */
3464 g_assert (**str == '(' || **str == '{');
3465 g_variant_iter_init (&iter, value);
3468 while (**str != ')' && **str != '}')
3470 value = g_variant_iter_next_value (&iter);
3471 g_variant_valist_get (str, value, free, app);
3472 g_variant_unref (value);
3478 /* User-facing API {{{2 */
3481 * @format_string: a #GVariant format string
3482 * @...: arguments, as per @format_string
3483 * @returns: a new floating #GVariant instance
3485 * Creates a new #GVariant instance.
3487 * Think of this function as an analogue to g_strdup_printf().
3489 * The type of the created instance and the arguments that are
3490 * expected by this function are determined by @format_string. See the
3491 * section on <link linkend='gvariant-format-strings'>GVariant Format
3492 * Strings</link>. Please note that the syntax of the format string is
3493 * very likely to be extended in the future.
3495 * The first character of the format string must not be '*' '?' '@' or
3496 * 'r'; in essence, a new #GVariant must always be constructed by this
3497 * function (and not merely passed through it unmodified).
3502 g_variant_new (const gchar *format_string,
3508 g_return_val_if_fail (valid_format_string (format_string, TRUE, NULL) &&
3509 format_string[0] != '?' && format_string[0] != '@' &&
3510 format_string[0] != '*' && format_string[0] != 'r',
3513 va_start (ap, format_string);
3514 value = g_variant_new_va (format_string, NULL, &ap);
3522 * @format_string: a string that is prefixed with a format string
3523 * @endptr: location to store the end pointer, or %NULL
3524 * @app: a pointer to a #va_list
3525 * @returns: a new, usually floating, #GVariant
3527 * This function is intended to be used by libraries based on
3528 * #GVariant that want to provide g_variant_new()-like functionality
3531 * The API is more general than g_variant_new() to allow a wider range
3534 * @format_string must still point to a valid format string, but it only
3535 * needs to be nul-terminated if @endptr is %NULL. If @endptr is
3536 * non-%NULL then it is updated to point to the first character past the
3537 * end of the format string.
3539 * @app is a pointer to a #va_list. The arguments, according to
3540 * @format_string, are collected from this #va_list and the list is left
3541 * pointing to the argument following the last.
3543 * These two generalisations allow mixing of multiple calls to
3544 * g_variant_new_va() and g_variant_get_va() within a single actual
3545 * varargs call by the user.
3547 * The return value will be floating if it was a newly created GVariant
3548 * instance (for example, if the format string was "(ii)"). In the case
3549 * that the format_string was '*', '?', 'r', or a format starting with
3550 * '@' then the collected #GVariant pointer will be returned unmodified,
3551 * without adding any additional references.
3553 * In order to behave correctly in all cases it is necessary for the
3554 * calling function to g_variant_ref_sink() the return result before
3555 * returning control to the user that originally provided the pointer.
3556 * At this point, the caller will have their own full reference to the
3557 * result. This can also be done by adding the result to a container,
3558 * or by passing it to another g_variant_new() call.
3563 g_variant_new_va (const gchar *format_string,
3564 const gchar **endptr,
3569 g_return_val_if_fail (valid_format_string (format_string, !endptr, NULL),
3571 g_return_val_if_fail (app != NULL, NULL);
3573 value = g_variant_valist_new (&format_string, app);
3576 *endptr = format_string;
3583 * @value: a #GVariant instance
3584 * @format_string: a #GVariant format string
3585 * @...: arguments, as per @format_string
3587 * Deconstructs a #GVariant instance.
3589 * Think of this function as an analogue to scanf().
3591 * The arguments that are expected by this function are entirely
3592 * determined by @format_string. @format_string also restricts the
3593 * permissible types of @value. It is an error to give a value with
3594 * an incompatible type. See the section on <link
3595 * linkend='gvariant-format-strings'>GVariant Format Strings</link>.
3596 * Please note that the syntax of the format string is very likely to be
3597 * extended in the future.
3602 g_variant_get (GVariant *value,
3603 const gchar *format_string,
3608 g_return_if_fail (valid_format_string (format_string, TRUE, value));
3610 /* if any direct-pointer-access formats are in use, flatten first */
3611 if (strchr (format_string, '&'))
3612 g_variant_get_data (value);
3614 va_start (ap, format_string);
3615 g_variant_get_va (value, format_string, NULL, &ap);
3621 * @value: a #GVariant
3622 * @format_string: a string that is prefixed with a format string
3623 * @endptr: location to store the end pointer, or %NULL
3624 * @app: a pointer to a #va_list
3626 * This function is intended to be used by libraries based on #GVariant
3627 * that want to provide g_variant_get()-like functionality to their
3630 * The API is more general than g_variant_get() to allow a wider range
3633 * @format_string must still point to a valid format string, but it only
3634 * need to be nul-terminated if @endptr is %NULL. If @endptr is
3635 * non-%NULL then it is updated to point to the first character past the
3636 * end of the format string.
3638 * @app is a pointer to a #va_list. The arguments, according to
3639 * @format_string, are collected from this #va_list and the list is left
3640 * pointing to the argument following the last.
3642 * These two generalisations allow mixing of multiple calls to
3643 * g_variant_new_va() and g_variant_get_va() within a single actual
3644 * varargs call by the user.
3649 g_variant_get_va (GVariant *value,
3650 const gchar *format_string,
3651 const gchar **endptr,
3654 g_return_if_fail (valid_format_string (format_string, !endptr, value));
3655 g_return_if_fail (value != NULL);
3656 g_return_if_fail (app != NULL);
3658 /* if any direct-pointer-access formats are in use, flatten first */
3659 if (strchr (format_string, '&'))
3660 g_variant_get_data (value);
3662 g_variant_valist_get (&format_string, value, FALSE, app);
3665 *endptr = format_string;
3668 /* Varargs-enabled Utility Functions {{{1 */
3671 * g_variant_builder_add:
3672 * @builder: a #GVariantBuilder
3673 * @format_string: a #GVariant varargs format string
3674 * @...: arguments, as per @format_string
3676 * Adds to a #GVariantBuilder.
3678 * This call is a convenience wrapper that is exactly equivalent to
3679 * calling g_variant_new() followed by g_variant_builder_add_value().
3681 * This function might be used as follows:
3685 * make_pointless_dictionary (void)
3687 * GVariantBuilder *builder;
3690 * builder = g_variant_builder_new (G_VARIANT_TYPE_CLASS_ARRAY,
3692 * for (i = 0; i < 16; i++)
3696 * sprintf (buf, "%d", i);
3697 * g_variant_builder_add (builder, "{is}", i, buf);
3700 * return g_variant_builder_end (builder);
3707 g_variant_builder_add (GVariantBuilder *builder,
3708 const gchar *format_string,
3714 va_start (ap, format_string);
3715 variant = g_variant_new_va (format_string, NULL, &ap);
3718 g_variant_builder_add_value (builder, variant);
3722 * g_variant_get_child:
3723 * @value: a container #GVariant
3724 * @index_: the index of the child to deconstruct
3725 * @format_string: a #GVariant format string
3726 * @...: arguments, as per @format_string
3728 * Reads a child item out of a container #GVariant instance and
3729 * deconstructs it according to @format_string. This call is
3730 * essentially a combination of g_variant_get_child_value() and
3736 g_variant_get_child (GVariant *value,
3738 const gchar *format_string,
3744 child = g_variant_get_child_value (value, index_);
3745 g_return_if_fail (valid_format_string (format_string, TRUE, child));
3747 va_start (ap, format_string);
3748 g_variant_get_va (child, format_string, NULL, &ap);
3751 g_variant_unref (child);
3755 * g_variant_iter_next:
3756 * @iter: a #GVariantIter
3757 * @format_string: a GVariant format string
3758 * @...: the arguments to unpack the value into
3759 * @returns: %TRUE if a value was unpacked, or %FALSE if there as no
3762 * Gets the next item in the container and unpacks it into the variable
3763 * argument list according to @format_string, returning %TRUE.
3765 * If no more items remain then %FALSE is returned.
3767 * All of the pointers given on the variable arguments list of this
3768 * function are assumed to point at uninitialised memory. It is the
3769 * responsibility of the caller to free all of the values returned by
3770 * the unpacking process.
3773 * <title>Memory management with g_variant_iter_next()</title>
3775 * /<!-- -->* Iterates a dictionary of type 'a{sv}' *<!-- -->/
3777 * iterate_dictionary (GVariant *dictionary)
3779 * GVariantIter iter;
3783 * g_variant_iter_init (&iter, dictionary);
3784 * while (g_variant_iter_next (&iter, "{sv}", &key, &value))
3786 * g_print ("Item '%s' has type '%s'\n", key,
3787 * g_variant_get_type_string (value));
3789 * /<!-- -->* must free data for ourselves *<!-- -->/
3790 * g_variant_unref (value);
3797 * For a solution that is likely to be more convenient to C programmers
3798 * when dealing with loops, see g_variant_iter_loop().
3803 g_variant_iter_next (GVariantIter *iter,
3804 const gchar *format_string,
3809 value = g_variant_iter_next_value (iter);
3811 g_return_val_if_fail (valid_format_string (format_string, TRUE, value),
3818 va_start (ap, format_string);
3819 g_variant_valist_get (&format_string, value, FALSE, &ap);
3822 g_variant_unref (value);
3825 return value != NULL;
3829 * g_variant_iter_loop:
3830 * @iter: a #GVariantIter
3831 * @format_string: a GVariant format string
3832 * @...: the arguments to unpack the value into
3833 * @returns: %TRUE if a value was unpacked, or %FALSE if there as no
3836 * Gets the next item in the container and unpacks it into the variable
3837 * argument list according to @format_string, returning %TRUE.
3839 * If no more items remain then %FALSE is returned.
3841 * On the first call to this function, the pointers appearing on the
3842 * variable argument list are assumed to point at uninitialised memory.
3843 * On the second and later calls, it is assumed that the same pointers
3844 * will be given and that they will point to the memory as set by the
3845 * previous call to this function. This allows the previous values to
3846 * be freed, as appropriate.
3848 * This function is intended to be used with a while loop as
3849 * demonstrated in the following example. This function can only be
3850 * used when iterating over an array. It is only valid to call this
3851 * function with a string constant for the format string and the same
3852 * string constant must be used each time. Mixing calls to this
3853 * function and g_variant_iter_next() or g_variant_iter_next_value() on
3854 * the same iterator is not recommended.
3857 * <title>Memory management with g_variant_iter_loop()</title>
3859 * /<!-- -->* Iterates a dictionary of type 'a{sv}' *<!-- -->/
3861 * iterate_dictionary (GVariant *dictionary)
3863 * GVariantIter iter;
3867 * g_variant_iter_init (&iter, dictionary);
3868 * while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
3870 * g_print ("Item '%s' has type '%s'\n", key,
3871 * g_variant_get_type_string (value));
3873 * /<!-- -->* no need to free 'key' and 'value' here *<!-- -->/
3879 * If you want a slightly less magical alternative that requires more
3880 * typing, see g_variant_iter_next().
3885 g_variant_iter_loop (GVariantIter *iter,
3886 const gchar *format_string,
3889 gboolean first_time = GVSI(iter)->loop_format == NULL;
3893 g_return_val_if_fail (first_time ||
3894 format_string == GVSI(iter)->loop_format,
3899 TYPE_CHECK (GVSI(iter)->value, G_VARIANT_TYPE_ARRAY, FALSE);
3900 GVSI(iter)->loop_format = format_string;
3902 if (strchr (format_string, '&'))
3903 g_variant_get_data (GVSI(iter)->value);
3906 value = g_variant_iter_next_value (iter);
3908 g_return_val_if_fail (!first_time ||
3909 valid_format_string (format_string, TRUE, value),
3912 va_start (ap, format_string);
3913 g_variant_valist_get (&format_string, value, !first_time, &ap);
3917 g_variant_unref (value);
3919 return value != NULL;
3922 /* Serialised data {{{1 */
3924 g_variant_deep_copy (GVariant *value)
3926 switch (g_variant_classify (value))
3928 case G_VARIANT_CLASS_MAYBE:
3929 case G_VARIANT_CLASS_ARRAY:
3930 case G_VARIANT_CLASS_TUPLE:
3931 case G_VARIANT_CLASS_DICT_ENTRY:
3932 case G_VARIANT_CLASS_VARIANT:
3934 GVariantBuilder builder;
3938 g_variant_builder_init (&builder, g_variant_get_type (value));
3939 g_variant_iter_init (&iter, value);
3941 while ((child = g_variant_iter_next_value (&iter)))
3943 g_variant_builder_add_value (&builder, g_variant_deep_copy (child));
3944 g_variant_unref (child);
3947 return g_variant_builder_end (&builder);
3950 case G_VARIANT_CLASS_BOOLEAN:
3951 return g_variant_new_boolean (g_variant_get_boolean (value));
3953 case G_VARIANT_CLASS_BYTE:
3954 return g_variant_new_byte (g_variant_get_byte (value));
3956 case G_VARIANT_CLASS_INT16:
3957 return g_variant_new_int16 (g_variant_get_int16 (value));
3959 case G_VARIANT_CLASS_UINT16:
3960 return g_variant_new_uint16 (g_variant_get_uint16 (value));
3962 case G_VARIANT_CLASS_INT32:
3963 return g_variant_new_int32 (g_variant_get_int32 (value));
3965 case G_VARIANT_CLASS_UINT32:
3966 return g_variant_new_uint32 (g_variant_get_uint32 (value));
3968 case G_VARIANT_CLASS_INT64:
3969 return g_variant_new_int64 (g_variant_get_int64 (value));
3971 case G_VARIANT_CLASS_UINT64:
3972 return g_variant_new_uint64 (g_variant_get_uint64 (value));
3974 case G_VARIANT_CLASS_HANDLE:
3975 return g_variant_new_handle (g_variant_get_handle (value));
3977 case G_VARIANT_CLASS_DOUBLE:
3978 return g_variant_new_double (g_variant_get_double (value));
3980 case G_VARIANT_CLASS_STRING:
3981 return g_variant_new_string (g_variant_get_string (value, NULL));
3983 case G_VARIANT_CLASS_OBJECT_PATH:
3984 return g_variant_new_object_path (g_variant_get_string (value, NULL));
3986 case G_VARIANT_CLASS_SIGNATURE:
3987 return g_variant_new_signature (g_variant_get_string (value, NULL));
3990 g_assert_not_reached ();
3994 * g_variant_get_normal_form:
3995 * @value: a #GVariant
3996 * @returns: a trusted #GVariant
3998 * Gets a #GVariant instance that has the same value as @value and is
3999 * trusted to be in normal form.
4001 * If @value is already trusted to be in normal form then a new
4002 * reference to @value is returned.
4004 * If @value is not already trusted, then it is scanned to check if it
4005 * is in normal form. If it is found to be in normal form then it is
4006 * marked as trusted and a new reference to it is returned.
4008 * If @value is found not to be in normal form then a new trusted
4009 * #GVariant is created with the same value as @value.
4011 * It makes sense to call this function if you've received #GVariant
4012 * data from untrusted sources and you want to ensure your serialised
4013 * output is definitely in normal form.
4018 g_variant_get_normal_form (GVariant *value)
4022 if (g_variant_is_normal_form (value))
4023 return g_variant_ref (value);
4025 trusted = g_variant_deep_copy (value);
4026 g_assert (g_variant_is_trusted (trusted));
4028 return g_variant_ref_sink (trusted);
4032 * g_variant_byteswap:
4033 * @value: a #GVariant
4034 * @returns: the byteswapped form of @value
4036 * Performs a byteswapping operation on the contents of @value. The
4037 * result is that all multi-byte numeric data contained in @value is
4038 * byteswapped. That includes 16, 32, and 64bit signed and unsigned
4039 * integers as well as file handles and double precision floating point
4042 * This function is an identity mapping on any value that does not
4043 * contain multi-byte numeric data. That include strings, booleans,
4044 * bytes and containers containing only these things (recursively).
4046 * The returned value is always in normal form and is marked as trusted.
4051 g_variant_byteswap (GVariant *value)
4053 GVariantSerialised serialised;
4058 trusted = g_variant_get_normal_form (value);
4059 serialised.type_info = g_variant_get_type_info (trusted);
4060 serialised.size = g_variant_get_size (trusted);
4061 serialised.data = g_malloc (serialised.size);
4062 g_variant_store (trusted, serialised.data);
4063 g_variant_unref (trusted);
4065 g_variant_serialised_byteswap (serialised);
4067 buffer = g_buffer_new_take_data (serialised.data, serialised.size);
4068 new = g_variant_new_from_buffer (g_variant_get_type (value), buffer, TRUE);
4069 g_buffer_unref (buffer);
4071 return g_variant_ref_sink (new);
4075 * g_variant_new_from_data:
4076 * @type: a #GVariantType
4077 * @data: the serialised data
4078 * @size: the size of @data
4079 * @trusted: %TRUE if @data is definitely in normal form
4080 * @notify: function to call when @data is no longer needed
4081 * @user_data: data for @notify
4082 * @returns: a new floating #GVariant of type @type
4084 * Creates a new #GVariant instance from serialised data.
4086 * @type is the type of #GVariant instance that will be constructed.
4087 * The interpretation of @data depends on knowing the type.
4089 * @data is not modified by this function and must remain valid with an
4090 * unchanging value until such a time as @notify is called with
4091 * @user_data. If the contents of @data change before that time then
4092 * the result is undefined.
4094 * If @data is trusted to be serialised data in normal form then
4095 * @trusted should be %TRUE. This applies to serialised data created
4096 * within this process or read from a trusted location on the disk (such
4097 * as a file installed in /usr/lib alongside your application). You
4098 * should set trusted to %FALSE if @data is read from the network, a
4099 * file in the user's home directory, etc.
4101 * @notify will be called with @user_data when @data is no longer
4102 * needed. The exact time of this call is unspecified and might even be
4103 * before this function returns.
4108 g_variant_new_from_data (const GVariantType *type,
4112 GDestroyNotify notify,
4119 buffer = g_buffer_new_from_pointer (data, size, notify, user_data);
4121 buffer = g_buffer_new_from_static_data (data, size);
4123 value = g_variant_new_from_buffer (type, buffer, trusted);
4124 g_buffer_unref (buffer);
4130 #define __G_VARIANT_C__
4131 #include "galiasdef.c"
4133 /* vim:set foldmethod=marker: */