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 <glib/gvariant-core.h>
29 #include <glib/gtestutils.h>
30 #include <glib/gstrfuncs.h>
31 #include <glib/ghash.h>
32 #include <glib/gmem.h>
41 * @short_description: strongly typed value datatype
42 * @see_also: GVariantType
44 * #GVariant is a variant datatype; it stores a value along with
45 * information about the type of that value. The range of possible
46 * values is determined by the type. The type system used by #GVariant
49 * #GVariant instances always have a type and a value (which are given
50 * at construction time). The type and value of a #GVariant instance
51 * can never change other than by the #GVariant itself being
52 * destroyed. A #GVariant can not contain a pointer.
54 * #GVariant is reference counted using g_variant_ref() and
55 * g_variant_unref(). #GVariant also has floating reference counts --
56 * see g_variant_ref_sink().
58 * #GVariant is completely threadsafe. A #GVariant instance can be
59 * concurrently accessed in any way from any number of threads without
62 * #GVariant is heavily optimised for dealing with data in serialised
63 * form. It works particularly well with data located in memory-mapped
64 * files. It can perform nearly all deserialisation operations in a
65 * small constant time, usually touching only a single memory page.
66 * Serialised #GVariant data can also be sent over the network.
68 * #GVariant is largely compatible with DBus. Almost all types of
69 * #GVariant instances can be sent over DBus. See #GVariantType for
72 * For convenience to C programmers, #GVariant features powerful
73 * varargs-based value construction and destruction. This feature is
74 * designed to be embedded in other libraries.
76 * There is a Python-inspired text language for describing #GVariant
77 * values. #GVariant includes a printer for this language and a parser
78 * with type inferencing.
81 * <title>Memory Use</title>
83 * #GVariant tries to be quite efficient with respect to memory use.
84 * This section gives a rough idea of how much memory is used by the
85 * current implementation. The information here is subject to change
89 * The memory allocated by #GVariant can be grouped into 4 broad
90 * purposes: memory for serialised data, memory for the type
91 * information cache, buffer management memory and memory for the
92 * #GVariant structure itself.
95 * <title>Serialised Data Memory</title>
97 * This is the memory that is used for storing GVariant data in
98 * serialised form. This is what would be sent over the network or
99 * what would end up on disk.
102 * The amount of memory required to store a boolean is 1 byte. 16,
103 * 32 and 64 bit integers and double precision floating point numbers
104 * use their "natural" size. Strings (including object path and
105 * signature strings) are stored with a nul terminator, and as such
106 * use the length of the string plus 1 byte.
109 * Maybe types use no space at all to represent the null value and
110 * use the same amount of space (sometimes plus one byte) as the
111 * equivalent non-maybe-typed value to represent the non-null case.
114 * Arrays use the amount of space required to store each of their
115 * members, concatenated. Additionally, if the items stored in an
116 * array are not of a fixed-size (ie: strings, other arrays, etc)
117 * then an additional framing offset is stored for each item. The
118 * size of this offset is either 1, 2 or 4 bytes depending on the
119 * overall size of the container. Additionally, extra padding bytes
120 * are added as required for alignment of child values.
123 * Tuples (including dictionary entries) use the amount of space
124 * required to store each of their members, concatenated, plus one
125 * framing offset (as per arrays) for each non-fixed-sized item in
126 * the tuple, except for the last one. Additionally, extra padding
127 * bytes are added as required for alignment of child values.
130 * Variants use the same amount of space as the item inside of the
131 * variant, plus 1 byte, plus the length of the type string for the
132 * item inside the variant.
135 * As an example, consider a dictionary mapping strings to variants.
136 * In the case that the dictionary is empty, 0 bytes are required for
140 * If we add an item "width" that maps to the int32 value of 500 then
141 * we will use 4 byte to store the int32 (so 6 for the variant
142 * containing it) and 6 bytes for the string. The variant must be
143 * aligned to 8 after the 6 bytes of the string, so that's 2 extra
144 * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
145 * for the dictionary entry. An additional 1 byte is added to the
146 * array as a framing offset making a total of 15 bytes.
149 * If we add another entry, "title" that maps to a nullable string
150 * that happens to have a value of null, then we use 0 bytes for the
151 * null value (and 3 bytes for the variant to contain it along with
152 * its type string) plus 6 bytes for the string. Again, we need 2
153 * padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
156 * We now require extra padding between the two items in the array.
157 * After the 14 bytes of the first item, that's 2 bytes required. We
158 * now require 2 framing offsets for an extra two bytes. 14 + 2 + 11
159 * + 2 = 29 bytes to encode the entire two-item dictionary.
163 * <title>Type Information Cache</title>
165 * For each GVariant type that currently exists in the program a type
166 * information structure is kept in the type information cache. The
167 * type information structure is required for rapid deserialisation.
170 * Continuing with the above example, if a #GVariant exists with the
171 * type "a{sv}" then a type information struct will exist for
172 * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type
173 * will share the same type information. Additionally, all
174 * single-digit types are stored in read-only static memory and do
175 * not contribute to the writable memory footprint of a program using
179 * Aside from the type information structures stored in read-only
180 * memory, there are two forms of type information. One is used for
181 * container types where there is a single element type: arrays and
182 * maybe types. The other is used for container types where there
183 * are multiple element types: tuples and dictionary entries.
186 * Array type info structures are 6 * sizeof (void *), plus the
187 * memory required to store the type string itself. This means that
188 * on 32bit systems, the cache entry for "a{sv}" would require 30
189 * bytes of memory (plus malloc overhead).
192 * Tuple type info structures are 6 * sizeof (void *), plus 4 *
193 * sizeof (void *) for each item in the tuple, plus the memory
194 * required to store the type string itself. A 2-item tuple, for
195 * example, would have a type information structure that consumed
196 * writable memory in the size of 14 * sizeof (void *) (plus type
197 * string) This means that on 32bit systems, the cache entry for
198 * "{sv}" would require 61 bytes of memory (plus malloc overhead).
201 * This means that in total, for our "a{sv}" example, 91 bytes of
202 * type information would be allocated.
205 * The type information cache, additionally, uses a #GHashTable to
206 * store and lookup the cached items and stores a pointer to this
207 * hash table in static storage. The hash table is freed when there
208 * are zero items in the type cache.
211 * Although these sizes may seem large it is important to remember
212 * that a program will probably only have a very small number of
213 * different types of values in it and that only one type information
214 * structure is required for many different values of the same type.
218 * <title>Buffer Management Memory</title>
220 * #GVariant uses an internal buffer management structure to deal
221 * with the various different possible sources of serialised data
222 * that it uses. The buffer is responsible for ensuring that the
223 * correct call is made when the data is no longer in use by
224 * #GVariant. This may involve a g_free() or a g_slice_free() or
225 * even g_mapped_file_unref().
228 * One buffer management structure is used for each chunk of
229 * serialised data. The size of the buffer management structure is 4
230 * * (void *). On 32bit systems, that's 16 bytes.
234 * <title>GVariant structure</title>
236 * The size of a #GVariant structure is 6 * (void *). On 32 bit
237 * systems, that's 24 bytes.
240 * #GVariant structures only exist if they are explicitly created
241 * with API calls. For example, if a #GVariant is constructed out of
242 * serialised data for the example given above (with the dictionary)
243 * then although there are 9 individual values that comprise the
244 * entire dictionary (two keys, two values, two variants containing
245 * the values, two dictionary entries, plus the dictionary itself),
246 * only 1 #GVariant instance exists -- the one refering to the
250 * If calls are made to start accessing the other values then
251 * #GVariant instances will exist for those values only for as long
252 * as they are in use (ie: until you call g_variant_unref()). The
253 * type information is shared. The serialised data and the buffer
254 * management structure for that serialised data is shared by the
259 * <title>Summary</title>
261 * To put the entire example together, for our dictionary mapping
262 * strings to variants (with two entries, as given above), we are
263 * using 91 bytes of memory for type information, 29 byes of memory
264 * for the serialised data, 16 bytes for buffer management and 24
265 * bytes for the #GVariant instance, or a total of 160 bytes, plus
266 * malloc overhead. If we were to use g_variant_get_child_value() to
267 * access the two dictionary entries, we would use an additional 48
268 * bytes. If we were to have other dictionaries of the same type, we
269 * would use more memory for the serialised data and buffer
270 * management for those dictionaries, but the type information would
277 /* definition of GVariant structure is in gvariant-core.c */
279 /* this is a g_return_val_if_fail() for making
280 * sure a (GVariant *) has the required type.
282 #define TYPE_CHECK(value, TYPE, val) \
283 if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \
284 g_return_if_fail_warning (G_LOG_DOMAIN, __PRETTY_FUNCTION__, \
285 "g_variant_is_of_type (" #value \
290 /* Numeric Type Constructor/Getters {{{1 */
292 * g_variant_new_from_trusted:
293 * @type: the #GVariantType
294 * @data: the data to use
295 * @size: the size of @data
296 * @returns: a new floating #GVariant
298 * Constructs a new trusted #GVariant instance from the provided data.
299 * This is used to implement g_variant_new_* for all the basic types.
302 g_variant_new_from_trusted (const GVariantType *type,
309 buffer = g_buffer_new_from_data (data, size);
310 value = g_variant_new_from_buffer (type, buffer, TRUE);
311 g_buffer_unref (buffer);
317 * g_variant_new_boolean:
318 * @boolean: a #gboolean value
319 * @returns: a new boolean #GVariant instance
321 * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
326 g_variant_new_boolean (gboolean value)
330 return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1);
334 * g_variant_get_boolean:
335 * @value: a boolean #GVariant instance
336 * @returns: %TRUE or %FALSE
338 * Returns the boolean value of @value.
340 * It is an error to call this function with a @value of any type
341 * other than %G_VARIANT_TYPE_BOOLEAN.
346 g_variant_get_boolean (GVariant *value)
350 TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE);
352 data = g_variant_get_data (value);
354 return data != NULL ? *data != 0 : FALSE;
357 /* the constructors and accessors for byte, int{16,32,64}, handles and
358 * doubles all look pretty much exactly the same, so we reduce
361 #define NUMERIC_TYPE(TYPE, type, ctype) \
362 GVariant *g_variant_new_##type (ctype value) { \
363 return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \
364 &value, sizeof value); \
366 ctype g_variant_get_##type (GVariant *value) { \
368 TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \
369 data = g_variant_get_data (value); \
370 return data != NULL ? *data : 0; \
375 * g_variant_new_byte:
376 * @byte: a #guint8 value
377 * @returns: a new byte #GVariant instance
379 * Creates a new byte #GVariant instance.
384 * g_variant_get_byte:
385 * @value: a byte #GVariant instance
386 * @returns: a #guchar
388 * Returns the byte value of @value.
390 * It is an error to call this function with a @value of any type
391 * other than %G_VARIANT_TYPE_BYTE.
395 NUMERIC_TYPE (BYTE, byte, guchar)
398 * g_variant_new_int16:
399 * @int16: a #gint16 value
400 * @returns: a new int16 #GVariant instance
402 * Creates a new int16 #GVariant instance.
407 * g_variant_get_int16:
408 * @value: a int16 #GVariant instance
409 * @returns: a #gint16
411 * Returns the 16-bit signed integer value of @value.
413 * It is an error to call this function with a @value of any type
414 * other than %G_VARIANT_TYPE_INT16.
418 NUMERIC_TYPE (INT16, int16, gint16)
421 * g_variant_new_uint16:
422 * @uint16: a #guint16 value
423 * @returns: a new uint16 #GVariant instance
425 * Creates a new uint16 #GVariant instance.
430 * g_variant_get_uint16:
431 * @value: a uint16 #GVariant instance
432 * @returns: a #guint16
434 * Returns the 16-bit unsigned integer value of @value.
436 * It is an error to call this function with a @value of any type
437 * other than %G_VARIANT_TYPE_UINT16.
441 NUMERIC_TYPE (UINT16, uint16, guint16)
444 * g_variant_new_int32:
445 * @int32: a #gint32 value
446 * @returns: a new int32 #GVariant instance
448 * Creates a new int32 #GVariant instance.
453 * g_variant_get_int32:
454 * @value: a int32 #GVariant instance
455 * @returns: a #gint32
457 * Returns the 32-bit signed integer value of @value.
459 * It is an error to call this function with a @value of any type
460 * other than %G_VARIANT_TYPE_INT32.
464 NUMERIC_TYPE (INT32, int32, gint32)
467 * g_variant_new_uint32:
468 * @uint32: a #guint32 value
469 * @returns: a new uint32 #GVariant instance
471 * Creates a new uint32 #GVariant instance.
476 * g_variant_get_uint32:
477 * @value: a uint32 #GVariant instance
478 * @returns: a #guint32
480 * Returns the 32-bit unsigned integer value of @value.
482 * It is an error to call this function with a @value of any type
483 * other than %G_VARIANT_TYPE_UINT32.
487 NUMERIC_TYPE (UINT32, uint32, guint32)
490 * g_variant_new_int64:
491 * @int64: a #gint64 value
492 * @returns: a new int64 #GVariant instance
494 * Creates a new int64 #GVariant instance.
499 * g_variant_get_int64:
500 * @value: a int64 #GVariant instance
501 * @returns: a #gint64
503 * Returns the 64-bit signed integer value of @value.
505 * It is an error to call this function with a @value of any type
506 * other than %G_VARIANT_TYPE_INT64.
510 NUMERIC_TYPE (INT64, int64, gint64)
513 * g_variant_new_uint64:
514 * @uint64: a #guint64 value
515 * @returns: a new uint64 #GVariant instance
517 * Creates a new uint64 #GVariant instance.
522 * g_variant_get_uint64:
523 * @value: a uint64 #GVariant instance
524 * @returns: a #guint64
526 * Returns the 64-bit unsigned integer value of @value.
528 * It is an error to call this function with a @value of any type
529 * other than %G_VARIANT_TYPE_UINT64.
533 NUMERIC_TYPE (UINT64, uint64, guint64)
536 * g_variant_new_handle:
537 * @handle: a #gint32 value
538 * @returns: a new handle #GVariant instance
540 * Creates a new handle #GVariant instance.
542 * By convention, handles are indexes into an array of file descriptors
543 * that are sent alongside a DBus message. If you're not interacting
544 * with DBus, you probably don't need them.
549 * g_variant_get_handle:
550 * @value: a handle #GVariant instance
551 * @returns: a #gint32
553 * Returns the 32-bit signed integer value of @value.
555 * It is an error to call this function with a @value of any type other
556 * than %G_VARIANT_TYPE_HANDLE.
558 * By convention, handles are indexes into an array of file descriptors
559 * that are sent alongside a DBus message. If you're not interacting
560 * with DBus, you probably don't need them.
564 NUMERIC_TYPE (HANDLE, handle, gint32)
567 * g_variant_new_double:
568 * @floating: a #gdouble floating point value
569 * @returns: a new double #GVariant instance
571 * Creates a new double #GVariant instance.
576 * g_variant_get_double:
577 * @value: a double #GVariant instance
578 * @returns: a #gdouble
580 * Returns the double precision floating point value of @value.
582 * It is an error to call this function with a @value of any type
583 * other than %G_VARIANT_TYPE_DOUBLE.
587 NUMERIC_TYPE (DOUBLE, double, gdouble)
589 /* Container type Constructor / Deconstructors {{{1 */
591 * g_variant_new_maybe:
592 * @child_type: the #GVariantType of the child
593 * @child: the child value, or %NULL
594 * @returns: a new #GVariant maybe instance
596 * Depending on if @value is %NULL, either wraps @value inside of a
597 * maybe container or creates a Nothing instance for the given @type.
599 * At least one of @type and @value must be non-%NULL. If @type is
600 * non-%NULL then it must be a definite type. If they are both
601 * non-%NULL then @type must be the type of @value.
606 g_variant_new_maybe (const GVariantType *child_type,
609 GVariantType *maybe_type;
612 g_return_val_if_fail (child_type == NULL || g_variant_type_is_definite
614 g_return_val_if_fail (child_type != NULL || child != NULL, NULL);
615 g_return_val_if_fail (child_type == NULL || child == NULL ||
616 g_variant_is_of_type (child, child_type),
619 if (child_type == NULL)
620 child_type = g_variant_get_type (child);
622 maybe_type = g_variant_type_new_maybe (child_type);
629 children = g_new (GVariant *, 1);
630 children[0] = g_variant_ref_sink (child);
631 trusted = g_variant_is_trusted (children[0]);
633 value = g_variant_new_from_children (maybe_type, children, 1, trusted);
636 value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE);
638 g_variant_type_free (maybe_type);
644 * g_variant_get_maybe:
645 * @value: a maybe-typed value
646 * @returns: the contents of @value, or %NULL
648 * Given a maybe-typed #GVariant instance, extract its value. If the
649 * value is Nothing, then this function returns %NULL.
654 g_variant_get_maybe (GVariant *value)
656 TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL);
658 if (g_variant_n_children (value))
659 return g_variant_get_child_value (value, 0);
665 * g_variant_new_variant:
666 * @value: a #GVariance instance
667 * @returns: a new variant #GVariant instance
669 * Boxes @value. The result is a #GVariant instance representing a
670 * variant containing the original value.
675 g_variant_new_variant (GVariant *value)
677 g_return_val_if_fail (value != NULL, NULL);
679 g_variant_ref_sink (value);
681 return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
682 g_memdup (&value, sizeof value),
683 1, g_variant_is_trusted (value));
687 * g_variant_get_variant:
688 * @value: a variant #GVariance instance
689 * @returns: the item contained in the variant
691 * Unboxes @value. The result is the #GVariant instance that was
692 * contained in @value.
697 g_variant_get_variant (GVariant *value)
699 TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL);
701 return g_variant_get_child_value (value, 0);
705 * g_variant_new_array:
706 * @child_type: the element type of the new array
707 * @children: an array of #GVariant pointers, the children
708 * @n_children: the length of @children
709 * @returns: a new #GVariant array
711 * Creates a new #GVariant array from @children.
713 * @child_type must be non-%NULL if @n_children is zero. Otherwise, the
714 * child type is determined by inspecting the first element of the
715 * @children array. If @child_type is non-%NULL then it must be a
718 * The items of the array are taken from the @children array. No entry
719 * in the @children array may be %NULL.
721 * All items in the array must have the same type, which must be the
722 * same as @child_type, if given.
727 g_variant_new_array (const GVariantType *child_type,
728 GVariant * const *children,
731 GVariantType *array_type;
732 GVariant **my_children;
737 g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
738 g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
739 g_return_val_if_fail (child_type == NULL ||
740 g_variant_type_is_definite (child_type), NULL);
742 my_children = g_new (GVariant *, n_children);
745 if (child_type == NULL)
746 child_type = g_variant_get_type (children[0]);
747 array_type = g_variant_type_new_array (child_type);
749 for (i = 0; i < n_children; i++)
751 TYPE_CHECK (children[i], child_type, NULL);
752 my_children[i] = g_variant_ref_sink (children[i]);
753 trusted &= g_variant_is_trusted (children[i]);
756 value = g_variant_new_from_children (array_type, my_children,
757 n_children, trusted);
758 g_variant_type_free (array_type);
764 * g_variant_make_tuple_type:
765 * @children: an array of GVariant *
766 * @n_children: the length of @children
768 * Return the type of a tuple containing @children as its items.
770 static GVariantType *
771 g_variant_make_tuple_type (GVariant * const *children,
774 const GVariantType **types;
778 types = g_new (const GVariantType *, n_children);
780 for (i = 0; i < n_children; i++)
781 types[i] = g_variant_get_type (children[i]);
783 type = g_variant_type_new_tuple (types, n_children);
790 * g_variant_new_tuple:
791 * @children: the items to make the tuple out of
792 * @n_children: the length of @children
793 * @returns: a new #GVariant tuple
795 * Creates a new tuple #GVariant out of the items in @children. The
796 * type is determined from the types of @children. No entry in the
797 * @children array may be %NULL.
799 * If @n_children is 0 then the unit tuple is constructed.
804 g_variant_new_tuple (GVariant * const *children,
807 GVariantType *tuple_type;
808 GVariant **my_children;
813 g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
815 my_children = g_new (GVariant *, n_children);
818 for (i = 0; i < n_children; i++)
820 my_children[i] = g_variant_ref_sink (children[i]);
821 trusted &= g_variant_is_trusted (children[i]);
824 tuple_type = g_variant_make_tuple_type (children, n_children);
825 value = g_variant_new_from_children (tuple_type, my_children,
826 n_children, trusted);
827 g_variant_type_free (tuple_type);
833 * g_variant_make_dict_entry_type:
834 * @key: a #GVariant, the key
835 * @val: a #GVariant, the value
837 * Return the type of a dictionary entry containing @key and @val as its
840 static GVariantType *
841 g_variant_make_dict_entry_type (GVariant *key,
844 return g_variant_type_new_dict_entry (g_variant_get_type (key),
845 g_variant_get_type (val));
849 * g_variant_new_dict_entry:
850 * @key: a basic #GVariant, the key
851 * @value: a #GVariant, the value
852 * @returns: a new dictionary entry #GVariant
854 * Creates a new dictionary entry #GVariant. @key and @value must be
857 * @key must be a value of a basic type (ie: not a container).
862 g_variant_new_dict_entry (GVariant *key,
865 GVariantType *dict_type;
869 g_return_val_if_fail (key != NULL && value != NULL, NULL);
870 g_return_val_if_fail (!g_variant_is_container (key), NULL);
872 children = g_new (GVariant *, 2);
873 children[0] = g_variant_ref_sink (key);
874 children[1] = g_variant_ref_sink (value);
875 trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
877 dict_type = g_variant_make_dict_entry_type (key, value);
878 value = g_variant_new_from_children (dict_type, children, 2, trusted);
879 g_variant_type_free (dict_type);
885 * g_variant_get_fixed_array:
886 * @value: a #GVariant array with fixed-sized elements
887 * @n_elements: a pointer to the location to store the number of items
888 * @element_size: the size of each element
889 * @returns: a pointer to the fixed array
891 * Provides access to the serialised data for an array of fixed-sized
894 * @value must be an array with fixed-sized elements. Numeric types are
895 * fixed-size as are tuples containing only other fixed-sized types.
897 * @element_size must be the size of a single element in the array. For
898 * example, if calling this function for an array of 32 bit integers,
899 * you might say <code>sizeof (gint32)</code>. This value isn't used
900 * except for the purpose of a double-check that the form of the
901 * seralised data matches the caller's expectation.
903 * @n_elements, which must be non-%NULL is set equal to the number of
904 * items in the array.
909 g_variant_get_fixed_array (GVariant *value,
913 GVariantTypeInfo *array_info;
914 gsize array_element_size;
918 TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
920 g_return_val_if_fail (n_elements != NULL, NULL);
921 g_return_val_if_fail (element_size > 0, NULL);
923 array_info = g_variant_get_type_info (value);
924 g_variant_type_info_query_element (array_info, NULL, &array_element_size);
926 g_return_val_if_fail (array_element_size, NULL);
928 if G_UNLIKELY (array_element_size != element_size)
930 if (array_element_size)
931 g_critical ("g_variant_get_fixed_array: assertion "
932 "`g_variant_array_has_fixed_size (value, element_size)' "
933 "failed: array size %"G_GSIZE_FORMAT" does not match "
934 "given element_size %"G_GSIZE_FORMAT".",
935 array_element_size, element_size);
937 g_critical ("g_variant_get_fixed_array: assertion "
938 "`g_variant_array_has_fixed_size (value, element_size)' "
939 "failed: array does not have fixed size.");
942 data = g_variant_get_data (value);
943 size = g_variant_get_size (value);
945 if (size % element_size)
948 *n_elements = size / element_size;
956 /* String type constructor/getters/validation {{{1 */
958 * g_variant_new_string:
959 * @string: a normal C nul-terminated string
960 * @returns: a new string #GVariant instance
962 * Creates a string #GVariant with the contents of @string.
967 g_variant_new_string (const gchar *string)
969 g_return_val_if_fail (string != NULL, NULL);
971 return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING,
972 string, strlen (string) + 1);
976 * g_variant_new_object_path:
977 * @object_path: a normal C nul-terminated string
978 * @returns: a new object path #GVariant instance
980 * Creates a DBus object path #GVariant with the contents of @string.
981 * @string must be a valid DBus object path. Use
982 * g_variant_is_object_path() if you're not sure.
987 g_variant_new_object_path (const gchar *object_path)
989 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
991 return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH,
992 object_path, strlen (object_path) + 1);
996 * g_variant_is_object_path:
997 * @string: a normal C nul-terminated string
998 * @returns: %TRUE if @string is a DBus object path
1000 * Determines if a given string is a valid DBus object path. You
1001 * should ensure that a string is a valid DBus object path before
1002 * passing it to g_variant_new_object_path().
1004 * A valid object path starts with '/' followed by zero or more
1005 * sequences of characters separated by '/' characters. Each sequence
1006 * must contain only the characters "[A-Z][a-z][0-9]_". No sequence
1007 * (including the one following the final '/' character) may be empty.
1012 g_variant_is_object_path (const gchar *string)
1014 g_return_val_if_fail (string != NULL, FALSE);
1016 return g_variant_serialiser_is_object_path (string, strlen (string) + 1);
1020 * g_variant_new_signature:
1021 * @signature: a normal C nul-terminated string
1022 * @returns: a new signature #GVariant instance
1024 * Creates a DBus type signature #GVariant with the contents of
1025 * @string. @string must be a valid DBus type signature. Use
1026 * g_variant_is_signature() if you're not sure.
1031 g_variant_new_signature (const gchar *signature)
1033 g_return_val_if_fail (g_variant_is_signature (signature), NULL);
1035 return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE,
1036 signature, strlen (signature) + 1);
1040 * g_variant_is_signature:
1041 * @string: a normal C nul-terminated string
1042 * @returns: %TRUE if @string is a DBus type signature
1044 * Determines if a given string is a valid DBus type signature. You
1045 * should ensure that a string is a valid DBus object path before
1046 * passing it to g_variant_new_signature().
1048 * DBus type signatures consist of zero or more definite #GVariantType
1049 * strings in sequence.
1054 g_variant_is_signature (const gchar *string)
1056 g_return_val_if_fail (string != NULL, FALSE);
1058 return g_variant_serialiser_is_signature (string, strlen (string) + 1);
1062 * g_variant_get_string:
1063 * @value: a string #GVariant instance
1064 * @length: a pointer to a #gsize, to store the length
1065 * @returns: the constant string
1067 * Returns the string value of a #GVariant instance with a string
1068 * type. This includes the types %G_VARIANT_TYPE_STRING,
1069 * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
1071 * If @length is non-%NULL then the length of the string (in bytes) is
1072 * returned there. For trusted values, this information is already
1073 * known. For untrusted values, a strlen() will be performed.
1075 * It is an error to call this function with a @value of any type
1076 * other than those three.
1078 * The return value remains valid as long as @value exists.
1083 g_variant_get_string (GVariant *value,
1086 g_return_val_if_fail (value != NULL, NULL);
1087 g_return_val_if_fail (
1088 g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
1089 g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) ||
1090 g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL);
1091 gconstpointer data = g_variant_get_data (value);
1092 gsize size = g_variant_get_size (value);
1094 if (!g_variant_is_trusted (value))
1096 switch (g_variant_classify (value))
1098 case G_VARIANT_CLASS_STRING:
1099 if (g_variant_serialiser_is_string (data, size))
1106 case G_VARIANT_CLASS_OBJECT_PATH:
1107 if (g_variant_serialiser_is_object_path (data, size))
1114 case G_VARIANT_CLASS_SIGNATURE:
1115 if (g_variant_serialiser_is_signature (data, size))
1123 g_assert_not_reached ();
1134 * g_variant_dup_string:
1135 * @value: a string #GVariant instance
1136 * @length: a pointer to a #gsize, to store the length
1137 * @returns: a newly allocated string
1139 * Similar to g_variant_get_string() except that instead of returning
1140 * a constant string, the string is duplicated.
1142 * The return value must be freed using g_free().
1147 g_variant_dup_string (GVariant *value,
1150 return g_strdup (g_variant_get_string (value, length));
1154 * g_variant_new_strv:
1155 * @strv: an array of strings
1156 * @length: the length of @strv, or -1
1157 * @returns: a new floating #GVariant instance
1159 * Constructs an array of strings #GVariant from the given array of
1162 * If @length is not -1 then it gives the maximum length of @strv. In
1163 * any case, a %NULL pointer in @strv is taken as a terminator.
1168 g_variant_new_strv (const gchar * const *strv,
1174 g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1177 length = g_strv_length ((gchar **) strv);
1179 strings = g_new (GVariant *, length);
1180 for (i = 0; i < length; i++)
1181 strings[i] = g_variant_ref_sink (g_variant_new_string (strv[i]));
1183 return g_variant_new_from_children (G_VARIANT_TYPE ("as"),
1184 strings, length, TRUE);
1188 * g_variant_get_strv:
1189 * @value: an array of strings #GVariant
1190 * @length: the length of the result, or %NULL
1191 * @returns: an array of constant strings
1193 * Gets the contents of an array of strings #GVariant. This call
1194 * makes a shallow copy; the return result should be released with
1195 * g_free(), but the individual strings must not be modified.
1197 * If @length is non-%NULL then the number of elements in the result
1198 * is stored there. In any case, the resulting array will be
1201 * For an empty array, @length will be set to 0 and a pointer to a
1202 * %NULL pointer will be returned.
1207 g_variant_get_strv (GVariant *value,
1214 TYPE_CHECK (value, G_VARIANT_TYPE ("as"), NULL);
1216 g_variant_get_data (value);
1217 n = g_variant_n_children (value);
1218 strv = g_new (const gchar *, n + 1);
1220 for (i = 0; i < n; i++)
1224 string = g_variant_get_child_value (value, i);
1225 strv[i] = g_variant_get_string (string, NULL);
1226 g_variant_unref (string);
1237 * g_variant_dup_strv:
1238 * @value: an array of strings #GVariant
1239 * @length: the length of the result, or %NULL
1240 * @returns: an array of constant strings
1242 * Gets the contents of an array of strings #GVariant. This call
1243 * makes a deep copy; the return result should be released with
1246 * If @length is non-%NULL then the number of elements in the result
1247 * is stored there. In any case, the resulting array will be
1250 * For an empty array, @length will be set to 0 and a pointer to a
1251 * %NULL pointer will be returned.
1256 g_variant_dup_strv (GVariant *value,
1263 TYPE_CHECK (value, G_VARIANT_TYPE ("as"), NULL);
1265 n = g_variant_n_children (value);
1266 strv = g_new (gchar *, n + 1);
1268 for (i = 0; i < n; i++)
1272 string = g_variant_get_child_value (value, i);
1273 strv[i] = g_variant_dup_string (string, NULL);
1274 g_variant_unref (string);
1284 /* Type checking and querying {{{1 */
1286 * g_variant_get_type:
1287 * @value: a #GVariant
1288 * @returns: a #GVariantType
1290 * Determines the type of @value.
1292 * The return value is valid for the lifetime of @value and must not
1297 const GVariantType *
1298 g_variant_get_type (GVariant *value)
1300 GVariantTypeInfo *type_info;
1302 g_return_val_if_fail (value != NULL, NULL);
1304 type_info = g_variant_get_type_info (value);
1306 return (GVariantType *) g_variant_type_info_get_type_string (type_info);
1310 * g_variant_get_type_string:
1311 * @value: a #GVariant
1312 * @returns: the type string for the type of @value
1314 * Returns the type string of @value. Unlike the result of calling
1315 * g_variant_type_peek_string(), this string is nul-terminated. This
1316 * string belongs to #GVariant and must not be freed.
1321 g_variant_get_type_string (GVariant *value)
1323 GVariantTypeInfo *type_info;
1325 g_return_val_if_fail (value != NULL, NULL);
1327 type_info = g_variant_get_type_info (value);
1329 return g_variant_type_info_get_type_string (type_info);
1333 * g_variant_is_of_type:
1334 * @value: a #GVariant instance
1335 * @type: a #GVariantType
1336 * @returns: %TRUE if the type of @value matches @type
1338 * Checks if a value has a type matching the provided type.
1343 g_variant_is_of_type (GVariant *value,
1344 const GVariantType *type)
1346 return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
1350 * g_variant_is_container:
1351 * @value: a #GVariant instance
1352 * @returns: %TRUE if @value is a container
1354 * Checks if @value is a container.
1357 g_variant_is_container (GVariant *value)
1359 return g_variant_type_is_container (g_variant_get_type (value));
1364 * g_variant_classify:
1365 * @value: a #GVariant
1366 * @returns: the #GVariantClass of @value
1368 * Classifies @value according to its top-level type.
1374 * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
1375 * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
1376 * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
1377 * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
1378 * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
1379 * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
1380 * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
1381 * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
1382 * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
1383 * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating
1385 * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
1386 * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a DBus object path
1388 * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a DBus signature string.
1389 * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
1390 * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
1391 * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
1392 * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
1393 * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
1395 * The range of possible top-level types of #GVariant instances.
1400 g_variant_classify (GVariant *value)
1402 g_return_val_if_fail (value != NULL, 0);
1404 return *g_variant_get_type_string (value);
1407 /* Pretty printer {{{1 */
1409 * g_variant_print_string:
1410 * @value: a #GVariant
1411 * @string: a #GString, or %NULL
1412 * @type_annotate: %TRUE if type information should be included in
1414 * @returns: a #GString containing the string
1416 * Behaves as g_variant_print(), but operates on a #GString.
1418 * If @string is non-%NULL then it is appended to and returned. Else,
1419 * a new empty #GString is allocated and it is returned.
1424 g_variant_print_string (GVariant *value,
1426 gboolean type_annotate)
1428 if G_UNLIKELY (string == NULL)
1429 string = g_string_new (NULL);
1431 switch (g_variant_classify (value))
1433 case G_VARIANT_CLASS_MAYBE:
1435 g_string_append_printf (string, "@%s ",
1436 g_variant_get_type_string (value));
1438 if (g_variant_n_children (value))
1440 gchar *printed_child;
1445 * Consider the case of the type "mmi". In this case we could
1446 * write "Just Just 4", but "4" alone is totally unambiguous,
1447 * so we try to drop "Just" where possible.
1449 * We have to be careful not to always drop "Just", though,
1450 * since "Nothing" needs to be distinguishable from "Just
1451 * Nothing". The case where we need to ensure we keep the
1452 * "Just" is actually exactly the case where we have a nested
1455 * Instead of searching for that nested Nothing, we just print
1456 * the contained value into a separate string and see if we
1457 * end up with "Nothing" at the end of it. If so, we need to
1458 * add "Just" at our level.
1460 element = g_variant_get_child_value (value, 0);
1461 printed_child = g_variant_print (element, FALSE);
1462 g_variant_unref (element);
1464 if (g_str_has_suffix (printed_child, "Nothing"))
1465 g_string_append (string, "Just ");
1466 g_string_append (string, printed_child);
1467 g_free (printed_child);
1470 g_string_append (string, "Nothing");
1474 case G_VARIANT_CLASS_ARRAY:
1475 /* it's an array so the first character of the type string is 'a'
1477 * if the first two characters are 'a{' then it's an array of
1478 * dictionary entries (ie: a dictionary) so we print that
1481 if (g_variant_get_type_string (value)[1] == '{')
1484 const gchar *comma = "";
1487 if ((n = g_variant_n_children (value)) == 0)
1490 g_string_append_printf (string, "@%s ",
1491 g_variant_get_type_string (value));
1492 g_string_append (string, "{}");
1496 g_string_append_c (string, '{');
1497 for (i = 0; i < n; i++)
1499 GVariant *entry, *key, *val;
1501 g_string_append (string, comma);
1504 entry = g_variant_get_child_value (value, i);
1505 key = g_variant_get_child_value (entry, 0);
1506 val = g_variant_get_child_value (entry, 1);
1507 g_variant_unref (entry);
1509 g_variant_print_string (key, string, type_annotate);
1510 g_variant_unref (key);
1511 g_string_append (string, ": ");
1512 g_variant_print_string (val, string, type_annotate);
1513 g_variant_unref (val);
1514 type_annotate = FALSE;
1516 g_string_append_c (string, '}');
1519 /* normal (non-dictionary) array */
1521 const gchar *comma = "";
1524 if ((n = g_variant_n_children (value)) == 0)
1527 g_string_append_printf (string, "@%s ",
1528 g_variant_get_type_string (value));
1529 g_string_append (string, "[]");
1533 g_string_append_c (string, '[');
1534 for (i = 0; i < n; i++)
1538 g_string_append (string, comma);
1541 element = g_variant_get_child_value (value, i);
1543 g_variant_print_string (element, string, type_annotate);
1544 g_variant_unref (element);
1545 type_annotate = FALSE;
1547 g_string_append_c (string, ']');
1552 case G_VARIANT_CLASS_TUPLE:
1556 n = g_variant_n_children (value);
1558 g_string_append_c (string, '(');
1559 for (i = 0; i < n; i++)
1563 element = g_variant_get_child_value (value, i);
1564 g_variant_print_string (element, string, type_annotate);
1565 g_string_append (string, ", ");
1566 g_variant_unref (element);
1569 /* for >1 item: remove final ", "
1570 * for 1 item: remove final " ", but leave the ","
1571 * for 0 items: there is only "(", so remove nothing
1573 g_string_truncate (string, string->len - (n > 0) - (n > 1));
1574 g_string_append_c (string, ')');
1578 case G_VARIANT_CLASS_DICT_ENTRY:
1582 g_string_append_c (string, '{');
1584 element = g_variant_get_child_value (value, 0);
1585 g_variant_print_string (element, string, type_annotate);
1586 g_variant_unref (element);
1588 g_string_append (string, ", ");
1590 element = g_variant_get_child_value (value, 1);
1591 g_variant_print_string (element, string, type_annotate);
1592 g_variant_unref (element);
1594 g_string_append_c (string, '}');
1598 case G_VARIANT_CLASS_VARIANT:
1600 GVariant *child = g_variant_get_variant (value);
1602 /* Always annotate types in nested variants, because they are
1603 * (by nature) of variable type.
1605 g_string_append_c (string, '<');
1606 g_variant_print_string (child, string, TRUE);
1607 g_string_append_c (string, '>');
1609 g_variant_unref (child);
1613 case G_VARIANT_CLASS_BOOLEAN:
1614 if (g_variant_get_boolean (value))
1615 g_string_append (string, "true");
1617 g_string_append (string, "false");
1620 case G_VARIANT_CLASS_STRING:
1622 const gchar *str = g_variant_get_string (value, NULL);
1623 gchar *escaped = g_strescape (str, NULL);
1625 g_string_append_printf (string, "\'%s\'", escaped);
1631 case G_VARIANT_CLASS_BYTE:
1633 g_string_append (string, "byte ");
1634 g_string_append_printf (string, "0x%02x",
1635 g_variant_get_byte (value));
1638 case G_VARIANT_CLASS_INT16:
1640 g_string_append (string, "int16 ");
1641 g_string_append_printf (string, "%"G_GINT16_FORMAT,
1642 g_variant_get_int16 (value));
1645 case G_VARIANT_CLASS_UINT16:
1647 g_string_append (string, "uint16 ");
1648 g_string_append_printf (string, "%"G_GUINT16_FORMAT,
1649 g_variant_get_uint16 (value));
1652 case G_VARIANT_CLASS_INT32:
1653 /* Never annotate this type because it is the default for numbers
1654 * (and this is a *pretty* printer)
1656 g_string_append_printf (string, "%"G_GINT32_FORMAT,
1657 g_variant_get_int32 (value));
1660 case G_VARIANT_CLASS_HANDLE:
1662 g_string_append (string, "handle ");
1663 g_string_append_printf (string, "%"G_GINT32_FORMAT,
1664 g_variant_get_handle (value));
1667 case G_VARIANT_CLASS_UINT32:
1669 g_string_append (string, "uint32 ");
1670 g_string_append_printf (string, "%"G_GUINT32_FORMAT,
1671 g_variant_get_uint32 (value));
1674 case G_VARIANT_CLASS_INT64:
1676 g_string_append (string, "int64 ");
1677 g_string_append_printf (string, "%"G_GINT64_FORMAT,
1678 g_variant_get_int64 (value));
1681 case G_VARIANT_CLASS_UINT64:
1683 g_string_append (string, "uint64 ");
1684 g_string_append_printf (string, "%"G_GUINT64_FORMAT,
1685 g_variant_get_uint64 (value));
1688 case G_VARIANT_CLASS_DOUBLE:
1693 g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
1695 for (i = 0; buffer[i]; i++)
1696 if (buffer[i] == '.' || buffer[i] == 'e' ||
1697 buffer[i] == 'n' || buffer[i] == 'N')
1700 /* if there is no '.' or 'e' in the float then add one */
1701 if (buffer[i] == '\0')
1708 g_string_append (string, buffer);
1712 case G_VARIANT_CLASS_OBJECT_PATH:
1714 g_string_append (string, "objectpath ");
1715 g_string_append_printf (string, "\'%s\'",
1716 g_variant_get_string (value, NULL));
1719 case G_VARIANT_CLASS_SIGNATURE:
1721 g_string_append (string, "signature ");
1722 g_string_append_printf (string, "\'%s\'",
1723 g_variant_get_string (value, NULL));
1727 g_assert_not_reached ();
1735 * @value: a #GVariant
1736 * @type_annotate: %TRUE if type information should be included in
1738 * @returns: a newly-allocated string holding the result.
1740 * Pretty-prints @value in the format understood by g_variant_parse().
1742 * If @type_annotate is %TRUE, then type information is included in
1746 g_variant_print (GVariant *value,
1747 gboolean type_annotate)
1749 return g_string_free (g_variant_print_string (value, NULL, type_annotate),
1753 /* Hash, Equal {{{1 */
1756 * @value: a basic #GVariant value as a #gconstpointer
1757 * @returns: a hash value corresponding to @value
1759 * Generates a hash value for a #GVariant instance.
1761 * The output of this function is guaranteed to be the same for a given
1762 * value only per-process. It may change between different processor
1763 * architectures or even different versions of GLib. Do not use this
1764 * function as a basis for building protocols or file formats.
1766 * The type of @value is #gconstpointer only to allow use of this
1767 * function with #GHashTable. @value must be a #GVariant.
1772 g_variant_hash (gconstpointer value_)
1774 GVariant *value = (GVariant *) value_;
1776 switch (g_variant_classify (value))
1778 case G_VARIANT_CLASS_STRING:
1779 case G_VARIANT_CLASS_OBJECT_PATH:
1780 case G_VARIANT_CLASS_SIGNATURE:
1781 return g_str_hash (g_variant_get_string (value, NULL));
1783 case G_VARIANT_CLASS_BOOLEAN:
1784 /* this is a very odd thing to hash... */
1785 return g_variant_get_boolean (value);
1787 case G_VARIANT_CLASS_BYTE:
1788 return g_variant_get_byte (value);
1790 case G_VARIANT_CLASS_INT16:
1791 case G_VARIANT_CLASS_UINT16:
1795 ptr = g_variant_get_data (value);
1803 case G_VARIANT_CLASS_INT32:
1804 case G_VARIANT_CLASS_UINT32:
1805 case G_VARIANT_CLASS_HANDLE:
1809 ptr = g_variant_get_data (value);
1817 case G_VARIANT_CLASS_INT64:
1818 case G_VARIANT_CLASS_UINT64:
1819 case G_VARIANT_CLASS_DOUBLE:
1820 /* need a separate case for these guys because otherwise
1821 * performance could be quite bad on big endian systems
1826 ptr = g_variant_get_data (value);
1829 return ptr[0] + ptr[1];
1835 g_return_val_if_fail (!g_variant_is_container (value), 0);
1836 g_assert_not_reached ();
1842 * @one: a #GVariant instance
1843 * @two: a #GVariant instance
1844 * @returns: %TRUE if @one and @two are equal
1846 * Checks if @one and @two have the same type and value.
1848 * The types of @one and @two are #gconstpointer only to allow use of
1849 * this function with #GHashTable. They must each be a #GVariant.
1854 g_variant_equal (gconstpointer one,
1859 g_return_val_if_fail (one != NULL && two != NULL, FALSE);
1861 if (g_variant_get_type_info ((GVariant *) one) !=
1862 g_variant_get_type_info ((GVariant *) two))
1865 /* if both values are trusted to be in their canonical serialised form
1866 * then a simple memcmp() of their serialised data will answer the
1869 * if not, then this might generate a false negative (since it is
1870 * possible for two different byte sequences to represent the same
1871 * value). for now we solve this by pretty-printing both values and
1872 * comparing the result.
1874 if (g_variant_is_trusted ((GVariant *) one) &&
1875 g_variant_is_trusted ((GVariant *) two))
1877 gconstpointer data_one, data_two;
1878 gsize size_one, size_two;
1880 size_one = g_variant_get_size ((GVariant *) one);
1881 size_two = g_variant_get_size ((GVariant *) two);
1883 if (size_one != size_two)
1886 data_one = g_variant_get_data ((GVariant *) one);
1887 data_two = g_variant_get_data ((GVariant *) two);
1889 equal = memcmp (data_one, data_two, size_one) == 0;
1893 gchar *strone, *strtwo;
1895 strone = g_variant_print ((GVariant *) one, FALSE);
1896 strtwo = g_variant_print ((GVariant *) two, FALSE);
1897 equal = strcmp (strone, strtwo) == 0;
1905 /* GVariantIter {{{1 */
1909 * #GVariantIter is an opaque data structure and can only be accessed
1910 * using the following functions.
1917 const gchar *loop_format;
1925 struct stack_iter iter;
1927 GVariant *value_ref;
1931 #define GVSI(i) ((struct stack_iter *) (i))
1932 #define GVHI(i) ((struct heap_iter *) (i))
1933 #define GVSI_MAGIC ((gsize) 3579507750u)
1934 #define GVHI_MAGIC ((gsize) 1450270775u)
1935 #define is_valid_iter(i) (i != NULL && \
1936 GVSI(i)->magic == GVSI_MAGIC)
1937 #define is_valid_heap_iter(i) (GVHI(i)->magic == GVHI_MAGIC && \
1941 * g_variant_iter_new:
1942 * @value: a container #GVariant
1943 * @returns: a new heap-allocated #GVariantIter
1945 * Creates a heap-allocated #GVariantIter for iterating over the items
1948 * Use g_variant_iter_free() to free the return value when you no longer
1951 * A reference is taken to @value and will be released only when
1952 * g_variant_iter_free() is called.
1957 g_variant_iter_new (GVariant *value)
1961 iter = (GVariantIter *) g_slice_new (struct heap_iter);
1962 GVHI(iter)->value_ref = g_variant_ref (value);
1963 GVHI(iter)->magic = GVHI_MAGIC;
1965 g_variant_iter_init (iter, value);
1971 * g_variant_iter_init:
1972 * @iter: a pointer to a #GVariantIter
1973 * @value: a container #GVariant
1974 * @returns: the number of items in @value
1976 * Initialises (without allocating) a #GVariantIter. @iter may be
1977 * completely uninitialised prior to this call; its old value is
1980 * The iterator remains valid for as long as @value exists, and need not
1981 * be freed in any way.
1986 g_variant_iter_init (GVariantIter *iter,
1989 g_assert (sizeof (GVariantIter) == sizeof (struct stack_iter));
1991 GVSI(iter)->magic = GVSI_MAGIC;
1992 GVSI(iter)->value = value;
1993 GVSI(iter)->n = g_variant_n_children (value);
1995 GVSI(iter)->loop_format = NULL;
1997 return GVSI(iter)->n;
2001 * g_variant_iter_copy:
2002 * @iter: a #GVariantIter
2003 * @returns: a new heap-allocated #GVariantIter
2005 * Creates a new heap-allocated #GVariantIter to iterate over the
2006 * container that was being iterated over by @iter. Iteration begins on
2007 * the new iterator from the current position of the old iterator but
2008 * the two copies are independent past that point.
2010 * Use g_variant_iter_free() to free the return value when you no longer
2013 * A reference is taken to the container that @iter is iterating over
2014 * and will be releated only when g_variant_iter_free() is called.
2019 g_variant_iter_copy (GVariantIter *iter)
2023 g_return_val_if_fail (is_valid_iter (iter), 0);
2025 copy = g_variant_iter_new (GVSI(iter)->value);
2026 GVSI(copy)->i = GVSI(iter)->i;
2032 * g_variant_iter_n_children:
2033 * @iter: a #GVariantIter
2034 * @returns: the number of children in the container
2036 * Queries the number of child items in the container that we are
2037 * iterating over. This is the total number of items -- not the number
2038 * of items remaining.
2040 * This function might be useful for preallocation of arrays.
2045 g_variant_iter_n_children (GVariantIter *iter)
2047 g_return_val_if_fail (is_valid_iter (iter), 0);
2049 return GVSI(iter)->n;
2053 * g_variant_iter_free:
2054 * @iter: a heap-allocated #GVariantIter
2056 * Frees a heap-allocated #GVariantIter. Only call this function on
2057 * iterators that were returned by g_variant_iter_new() or
2058 * g_variant_iter_copy().
2063 g_variant_iter_free (GVariantIter *iter)
2065 g_return_if_fail (is_valid_heap_iter (iter));
2067 g_variant_unref (GVHI(iter)->value_ref);
2068 GVHI(iter)->magic = 0;
2070 g_slice_free (struct heap_iter, GVHI(iter));
2074 * g_variant_iter_next_value:
2075 * @iter: a #GVariantIter
2076 * @returns: a #GVariant, or %NULL
2078 * Gets the next item in the container. If no more items remain then
2079 * %NULL is returned.
2081 * Use g_variant_unref() to drop your reference on the return value when
2082 * you no longer need it.
2085 * <title>Iterating with g_variant_iter_next_value()</title>
2087 * /<!-- -->* recursively iterate a container *<!-- -->/
2089 * iterate_container_recursive (GVariant *container)
2091 * GVariantIter iter;
2094 * g_variant_iter_init (&iter, dictionary);
2095 * while ((child = g_variant_iter_next_value (&iter)))
2097 * g_print ("type '%s'\n", g_variant_get_type_string (child));
2099 * if (g_variant_is_container (child))
2100 * iterate_container_recursive (child);
2102 * g_variant_unref (child);
2111 g_variant_iter_next_value (GVariantIter *iter)
2113 g_return_val_if_fail (is_valid_iter (iter), FALSE);
2115 if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n)
2117 g_critical ("g_variant_iter_next_value: must not be called again "
2118 "after NULL has already been returned.");
2124 if (GVSI(iter)->i < GVSI(iter)->n)
2125 return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i);
2131 * g_variant_iter_loop:
2132 * @iter: a #GVariantIter
2133 * @format_string: a GVariant format string
2134 * @...: the arguments to unpack the value into
2135 * @returns: %TRUE if a value was unpacked, or %FALSE if there as no
2138 * Gets the next item in the container and unpacks it into the variable
2139 * argument list according to @format_string, returning %TRUE.
2141 * If no more items remain then %FALSE is returned.
2143 * On the first call to this function, the pointers appearing on the
2144 * variable argument list are assumed to point at uninitialised memory.
2145 * On the second and later calls, it is assumed that the same pointers
2146 * will be given and that they will point to the memory as set by the
2147 * previous call to this function. This allows the previous values to
2148 * be freed, as appropriate.
2150 * This function is intended to be used with a while loop as
2151 * demonstrated in the following example. This function can only be
2152 * used when iterating over an array. It is only valid to call this
2153 * function with a string constant for the format string and the same
2154 * string constant must be used each time. Mixing calls to this
2155 * function and g_variant_iter_next() or g_variant_iter_next_value() on
2156 * the same iterator is not recommended.
2159 * <title>Memory management with g_variant_iter_loop()</title>
2161 * /<!-- -->* Iterates a dictionary of type 'a{sv}' *<!-- -->/
2163 * iterate_dictionary (GVariant *dictionary)
2165 * GVariantIter iter;
2169 * g_variant_iter_init (&iter, dictionary);
2170 * while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
2172 * g_print ("Item '%s' has type '%s'\n", key,
2173 * g_variant_get_type_string (value));
2175 * /<!-- -->* no need to free 'key' and 'value' here *<!-- -->/
2181 * If you want a slightly less magical alternative that requires more
2182 * typing, see g_variant_iter_next().
2187 g_variant_iter_loop (GVariantIter *iter,
2188 const gchar *format_string,
2191 gboolean first_time = GVSI(iter)->loop_format == NULL;
2194 g_return_val_if_fail (first_time ||
2195 format_string == GVSI(iter)->loop_format,
2200 TYPE_CHECK (GVSI(iter)->value, G_VARIANT_TYPE_ARRAY, FALSE);
2201 GVSI(iter)->loop_format = format_string;
2204 value = g_variant_iter_next_value (iter);
2210 va_start (ap, format_string);
2211 /* varargs get stuff */
2214 g_variant_unref (value);
2217 return value != NULL;
2221 * g_variant_iter_next:
2222 * @iter: a #GVariantIter
2223 * @format_string: a GVariant format string
2224 * @...: the arguments to unpack the value into
2225 * @returns: %TRUE if a value was unpacked, or %FALSE if there as no
2228 * Gets the next item in the container and unpacks it into the variable
2229 * argument list according to @format_string, returning %TRUE.
2231 * If no more items remain then %FALSE is returned.
2233 * All of the pointers given on the variable arguments list of this
2234 * function are assumed to point at uninitialised memory. It is the
2235 * responsibility of the caller to free all of the values returned by
2236 * the unpacking process.
2239 * <title>Memory management with g_variant_iter_next()</title>
2241 * /<!-- -->* Iterates a dictionary of type 'a{sv}' *<!-- -->/
2243 * iterate_dictionary (GVariant *dictionary)
2245 * GVariantIter iter;
2249 * g_variant_iter_init (&iter, dictionary);
2250 * while (g_variant_iter_next (&iter, "{sv}", &key, &value))
2252 * g_print ("Item '%s' has type '%s'\n", key,
2253 * g_variant_get_type_string (value));
2255 * /<!-- -->* must free data for ourselves *<!-- -->/
2256 * g_variant_unref (value);
2263 * For a solution that is likely to be more convenient to C programmers,
2264 * see g_variant_iter_loop().
2269 g_variant_iter_next (GVariantIter *iter,
2270 const gchar *format_string,
2275 value = g_variant_iter_next_value (iter);
2281 va_start (ap, format_string);
2282 /* varargs get stuff */
2285 g_variant_unref (value);
2288 return value != NULL;
2291 /* GVariantBuilder {{{1 */
2295 * A utility type for constructing container-type #GVariant instances.
2297 * This is an opaque structure and may only be accessed using the
2298 * following functions.
2300 * #GVariantBuilder is not threadsafe in any way. Do not attempt to
2301 * access it from more than one thread.
2304 struct stack_builder
2306 GVariantBuilder *parent;
2309 /* type constraint explicitly specified by 'type'.
2310 * for tuple types, this moves along as we add more items.
2312 const GVariantType *expected_type;
2314 /* type constraint implied by previous array item.
2316 const GVariantType *prev_item_type;
2318 /* constraints on the number of children. max = -1 for unlimited. */
2322 /* dynamically-growing pointer array */
2323 GVariant **children;
2324 gsize allocated_children;
2327 /* set to '1' if all items in the container will have the same type
2328 * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
2330 guint uniform_item_types : 1;
2332 /* set to '1' initially and changed to '0' if an untrusted value is
2342 GVariantBuilder builder;
2348 #define GVSB(b) ((struct stack_builder *) (b))
2349 #define GVHB(b) ((struct heap_builder *) (b))
2350 #define GVSB_MAGIC ((gsize) 1033660112u)
2351 #define GVHB_MAGIC ((gsize) 3087242682u)
2352 #define is_valid_builder(b) (b != NULL && \
2353 GVSB(b)->magic == GVSB_MAGIC)
2354 #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
2357 * g_variant_builder_new:
2358 * @type: a container type
2359 * @returns: a #GVariantBuilder
2361 * Allocates and initialises a new #GVariantBuilder.
2363 * You should call g_variant_builder_unref() on the return value when it
2364 * is no longer needed. The memory will not be automatically freed by
2367 * In most cases it is easier to place a #GVariantBuilder directly on
2368 * the stack of the calling function and initialise it with
2369 * g_variant_builder_init().
2374 g_variant_builder_new (const GVariantType *type)
2376 GVariantBuilder *builder;
2378 builder = (GVariantBuilder *) g_slice_new (struct heap_builder);
2379 g_variant_builder_init (builder, type);
2380 GVHB(builder)->magic = GVHB_MAGIC;
2381 GVHB(builder)->ref_count = 1;
2387 * g_variant_builder_unref:
2388 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
2390 * Decreases the reference count on @builder.
2392 * In the event that there are no more references, releases all memory
2393 * associated with the #GVariantBuilder.
2395 * Don't call this on stack-allocated #GVariantBuilder instances or bad
2396 * things will happen.
2401 g_variant_builder_unref (GVariantBuilder *builder)
2403 g_return_if_fail (is_valid_heap_builder (builder));
2405 if (--GVHB(builder)->ref_count)
2408 g_variant_builder_clear (builder);
2409 GVHB(builder)->magic = 0;
2411 g_slice_free (struct heap_builder, GVHB(builder));
2415 * g_variant_builder_ref:
2416 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
2417 * @returns: a new reference to @builder
2419 * Increases the reference count on @builder.
2421 * Don't call this on stack-allocated #GVariantBuilder instances or bad
2422 * things will happen.
2427 g_variant_builder_ref (GVariantBuilder *builder)
2429 g_return_val_if_fail (is_valid_heap_builder (builder), NULL);
2431 GVHB(builder)->ref_count++;
2437 * g_variant_builder_clear:
2438 * @builder: a #GVariantBuilder
2440 * Releases all memory associated with a #GVariantBuilder without
2441 * freeing the #GVariantBuilder structure itself.
2443 * It typically only makes sense to do this on a stack-allocated
2444 * #GVariantBuilder if you want to abort building the value part-way
2445 * through. This function need not be called if you call
2446 * g_variant_builder_end() and it also doesn't need to be called on
2447 * builders allocated with g_variant_builder_new (see
2448 * g_variant_builder_free() for that).
2450 * This function leaves the #GVariantBuilder structure set to all-zeros.
2451 * It is valid to call this function on either an initialised
2452 * #GVariantBuilder or one that is set to all-zeros but it is not valid
2453 * to call this function on uninitialised memory.
2458 g_variant_builder_clear (GVariantBuilder *builder)
2462 if (GVSB(builder)->magic == 0)
2463 /* all-zeros case */
2466 g_return_if_fail (is_valid_builder (builder));
2468 g_variant_type_free (GVSB(builder)->type);
2470 for (i = 0; i < GVSB(builder)->offset; i++)
2471 g_variant_unref (GVSB(builder)->children[i]);
2473 g_free (GVSB(builder)->children);
2475 if (GVSB(builder)->parent)
2477 g_variant_builder_clear (GVSB(builder)->parent);
2478 g_slice_free (GVariantBuilder, GVSB(builder)->parent);
2481 memset (builder, 0, sizeof (GVariantBuilder));
2485 * g_variant_builder_init:
2486 * @builder: a #GVariantBuilder
2487 * @type: a container type
2489 * Initialises a #GVariantBuilder structure.
2491 * @type must be non-%NULL. It specifies the type of container to
2492 * construct. It can be an indefinite type such as
2493 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
2494 * Maybe, array, tuple, dictionary entry and variant-typed values may be
2497 * After the builder is initialised, values are added using
2498 * g_variant_builder_add_value() or g_variant_builder_add().
2500 * After all the child values are added, g_variant_builder_end() frees
2501 * the memory associated with the builder and returns the #GVariant that
2504 * This function completely ignores the previous contents of @builder.
2505 * On one hand this means that it is valid to pass in completely
2506 * uninitialised memory. On the other hand, this means that if you are
2507 * initialising over top of an existing #GVariantBuilder you need to
2508 * first call g_variant_builder_clear() in order to avoid leaking
2511 * You must not call g_variant_builder_ref() or
2512 * g_variant_builder_unref() on a #GVariantBuilder that was initialised
2513 * with this function. If you ever pass a reference to a
2514 * #GVariantBuilder outside of the control of your own code then you
2515 * should assume that the person receiving that reference may try to use
2516 * reference counting; you should use g_variant_builder_new() instead of
2522 g_variant_builder_init (GVariantBuilder *builder,
2523 const GVariantType *type)
2525 g_return_if_fail (type != NULL);
2526 g_return_if_fail (g_variant_type_is_container (type));
2528 g_assert (sizeof (struct stack_builder) < sizeof (GVariantBuilder));
2529 memset (builder, 0, sizeof (GVariantBuilder));
2531 GVSB(builder)->type = g_variant_type_copy (type);
2532 GVSB(builder)->magic = GVSB_MAGIC;
2533 GVSB(builder)->trusted = TRUE;
2535 switch (*(const gchar *) type)
2537 case G_VARIANT_CLASS_VARIANT:
2538 GVSB(builder)->uniform_item_types = TRUE;
2539 GVSB(builder)->allocated_children = 1;
2540 GVSB(builder)->expected_type = NULL;
2541 GVSB(builder)->min_items = 1;
2542 GVSB(builder)->max_items = 1;
2545 case G_VARIANT_CLASS_ARRAY:
2546 GVSB(builder)->uniform_item_types = TRUE;
2547 GVSB(builder)->allocated_children = 8;
2548 GVSB(builder)->expected_type =
2549 g_variant_type_element (GVSB(builder)->type);
2550 GVSB(builder)->min_items = 0;
2551 GVSB(builder)->max_items = -1;
2554 case G_VARIANT_CLASS_MAYBE:
2555 GVSB(builder)->uniform_item_types = TRUE;
2556 GVSB(builder)->allocated_children = 1;
2557 GVSB(builder)->expected_type =
2558 g_variant_type_element (GVSB(builder)->type);
2559 GVSB(builder)->min_items = 0;
2560 GVSB(builder)->max_items = 1;
2563 case G_VARIANT_CLASS_DICT_ENTRY:
2564 GVSB(builder)->uniform_item_types = FALSE;
2565 GVSB(builder)->allocated_children = 2;
2566 GVSB(builder)->expected_type =
2567 g_variant_type_key (GVSB(builder)->type);
2568 GVSB(builder)->min_items = 2;
2569 GVSB(builder)->max_items = 2;
2572 case 'r': /* G_VARIANT_TYPE_TUPLE was given */
2573 GVSB(builder)->uniform_item_types = FALSE;
2574 GVSB(builder)->allocated_children = 8;
2575 GVSB(builder)->expected_type = NULL;
2576 GVSB(builder)->min_items = 0;
2577 GVSB(builder)->max_items = -1;
2580 case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */
2581 GVSB(builder)->allocated_children = g_variant_type_n_items (type);
2582 GVSB(builder)->expected_type =
2583 g_variant_type_first (GVSB(builder)->type);
2584 GVSB(builder)->min_items = GVSB(builder)->allocated_children;
2585 GVSB(builder)->max_items = GVSB(builder)->allocated_children;
2586 GVSB(builder)->uniform_item_types = FALSE;
2590 g_assert_not_reached ();
2593 GVSB(builder)->children = g_new (GVariant *,
2594 GVSB(builder)->allocated_children);
2598 g_variant_builder_make_room (struct stack_builder *builder)
2600 if (builder->offset == builder->allocated_children)
2602 builder->allocated_children *= 2;
2603 builder->children = g_renew (GVariant *, builder->children,
2604 builder->allocated_children);
2609 * g_variant_builder_add_value:
2610 * @builder: a #GVariantBuilder
2611 * @value: a #GVariant
2613 * Adds @value to @builder.
2615 * It is an error to call this function in any way that would create an
2616 * inconsistent value to be constructed. Some examples of this are
2617 * putting different types of items into an array, putting the wrong
2618 * types or number of items in a tuple, putting more than one value into
2624 g_variant_builder_add_value (GVariantBuilder *builder,
2627 g_return_if_fail (is_valid_builder (builder));
2628 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
2629 g_return_if_fail (!GVSB(builder)->expected_type ||
2630 g_variant_is_of_type (value,
2631 GVSB(builder)->expected_type));
2632 g_return_if_fail (!GVSB(builder)->prev_item_type ||
2633 g_variant_is_of_type (value,
2634 GVSB(builder)->prev_item_type));
2636 GVSB(builder)->trusted &= g_variant_is_trusted (value);
2638 if (!GVSB(builder)->uniform_item_types)
2640 /* advance our expected type pointers */
2641 if (GVSB(builder)->expected_type)
2642 GVSB(builder)->expected_type =
2643 g_variant_type_next (GVSB(builder)->expected_type);
2645 if (GVSB(builder)->prev_item_type)
2646 GVSB(builder)->prev_item_type =
2647 g_variant_type_next (GVSB(builder)->prev_item_type);
2650 GVSB(builder)->prev_item_type = g_variant_get_type (value);
2652 g_variant_builder_make_room (GVSB(builder));
2654 GVSB(builder)->children[GVSB(builder)->offset++] =
2655 g_variant_ref_sink (value);
2659 * g_variant_builder_open:
2660 * @builder: a #GVariantBuilder
2661 * @type: a #GVariantType
2663 * Opens a subcontainer inside the given @builder. When done adding
2664 * items to the subcontainer, g_variant_builder_close() must be called.
2666 * It is an error to call this function in any way that would cause an
2667 * inconsistent value to be constructed (ie: adding too many values or
2668 * a value of an incorrect type).
2673 g_variant_builder_open (GVariantBuilder *builder,
2674 const GVariantType *type)
2676 GVariantBuilder *parent;
2678 g_return_if_fail (is_valid_builder (builder));
2679 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
2680 g_return_if_fail (!GVSB(builder)->expected_type ||
2681 g_variant_type_is_subtype_of (type,
2682 GVSB(builder)->expected_type));
2683 g_return_if_fail (!GVSB(builder)->prev_item_type ||
2684 g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type,
2687 parent = g_slice_dup (GVariantBuilder, builder);
2688 g_variant_builder_init (builder, type);
2689 GVSB(builder)->parent = parent;
2691 /* push the prev_item_type down into the subcontainer */
2692 if (GVSB(parent)->prev_item_type)
2694 if (!GVSB(builder)->uniform_item_types)
2695 /* tuples and dict entries */
2696 GVSB(builder)->prev_item_type =
2697 g_variant_type_first (GVSB(parent)->prev_item_type);
2699 else if (!g_variant_type_is_variant (GVSB(builder)->type))
2700 /* maybes and arrays */
2701 GVSB(builder)->prev_item_type =
2702 g_variant_type_element (GVSB(parent)->prev_item_type);
2707 * g_variant_builder_close:
2708 * @builder: a #GVariantBuilder
2710 * Closes the subcontainer inside the given @builder that was opened by
2711 * the most recent call to g_variant_builder_open().
2713 * It is an error to call this function in any way that would create an
2714 * inconsistent value to be constructed (ie: too few values added to the
2720 g_variant_builder_close (GVariantBuilder *builder)
2722 GVariantBuilder *parent;
2724 g_return_if_fail (is_valid_builder (builder));
2725 g_return_if_fail (GVSB(builder)->parent != NULL);
2727 parent = GVSB(builder)->parent;
2728 GVSB(builder)->parent = NULL;
2730 g_variant_builder_add_value (parent, g_variant_builder_end (builder));
2733 g_slice_free (GVariantBuilder, parent);
2737 * g_variant_make_maybe_type:
2738 * @element: a #GVariant
2740 * Return the type of a maybe containing @element.
2742 static GVariantType *
2743 g_variant_make_maybe_type (GVariant *element)
2745 return g_variant_type_new_maybe (g_variant_get_type (element));
2749 * g_variant_make_array_type:
2750 * @element: a #GVariant
2752 * Return the type of an array containing @element.
2754 static GVariantType *
2755 g_variant_make_array_type (GVariant *element)
2757 return g_variant_type_new_array (g_variant_get_type (element));
2761 * g_variant_builder_end:
2762 * @builder: a #GVariantBuilder
2763 * @returns: a new, floating, #GVariant
2765 * Ends the builder process and returns the constructed value.
2767 * This call automatically reduces the reference count on @builder by
2768 * one, unless it has previously had g_variant_builder_no_autofree()
2769 * called on it. Unless you've taken other actions, this is usually
2770 * sufficient to free @builder.
2772 * Even if additional references are held, it is not permissible to use
2773 * @builder in any way after this call except for further reference
2774 * counting operations.
2776 * It is an error to call this function in any way that would create an
2777 * inconsistent value to be constructed (ie: insufficient number of
2778 * items added to a container with a specific number of children
2779 * required). It is also an error to call this function if the builder
2780 * was created with an indefinite array or maybe type and no children
2781 * have been added; in this case it is impossible to infer the type of
2787 g_variant_builder_end (GVariantBuilder *builder)
2789 GVariantType *my_type;
2792 g_return_val_if_fail (is_valid_builder (builder), NULL);
2793 g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items,
2795 g_return_val_if_fail (!GVSB(builder)->uniform_item_types ||
2796 GVSB(builder)->prev_item_type != NULL ||
2797 g_variant_type_is_definite (GVSB(builder)->type),
2800 if (g_variant_type_is_definite (GVSB(builder)->type))
2801 my_type = g_variant_type_copy (GVSB(builder)->type);
2803 else if (g_variant_type_is_maybe (GVSB(builder)->type))
2804 my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]);
2806 else if (g_variant_type_is_array (GVSB(builder)->type))
2807 my_type = g_variant_make_array_type (GVSB(builder)->children[0]);
2809 else if (g_variant_type_is_tuple (GVSB(builder)->type))
2810 my_type = g_variant_make_tuple_type (GVSB(builder)->children,
2811 GVSB(builder)->offset);
2813 else if (g_variant_type_is_dict_entry (GVSB(builder)->type))
2814 my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0],
2815 GVSB(builder)->children[1]);
2817 g_assert_not_reached ();
2819 value = g_variant_new_from_children (my_type,
2820 g_renew (GVariant *,
2821 GVSB(builder)->children,
2822 GVSB(builder)->offset),
2823 GVSB(builder)->offset,
2824 GVSB(builder)->trusted);
2825 GVSB(builder)->children = NULL;
2826 GVSB(builder)->offset = 0;
2828 g_variant_builder_clear (builder);
2829 g_variant_type_free (my_type);
2835 #define __G_VARIANT_C__
2836 #include "galiasdef.c"
2838 /* vim:set foldmethod=marker: */