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 g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("as")) ||
1215 g_variant_is_of_type (value, G_VARIANT_TYPE ("ao")) ||
1216 g_variant_is_of_type (value, G_VARIANT_TYPE ("ag")),
1219 g_variant_get_data (value);
1220 n = g_variant_n_children (value);
1221 strv = g_new (const gchar *, n + 1);
1223 for (i = 0; i < n; i++)
1227 string = g_variant_get_child_value (value, i);
1228 strv[i] = g_variant_get_string (string, NULL);
1229 g_variant_unref (string);
1240 * g_variant_dup_strv:
1241 * @value: an array of strings #GVariant
1242 * @length: the length of the result, or %NULL
1243 * @returns: an array of constant strings
1245 * Gets the contents of an array of strings #GVariant. This call
1246 * makes a deep copy; the return result should be released with
1249 * If @length is non-%NULL then the number of elements in the result
1250 * is stored there. In any case, the resulting array will be
1253 * For an empty array, @length will be set to 0 and a pointer to a
1254 * %NULL pointer will be returned.
1259 g_variant_dup_strv (GVariant *value,
1266 g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("as")) ||
1267 g_variant_is_of_type (value, G_VARIANT_TYPE ("ao")) ||
1268 g_variant_is_of_type (value, G_VARIANT_TYPE ("ag")),
1271 n = g_variant_n_children (value);
1272 strv = g_new (gchar *, n + 1);
1274 for (i = 0; i < n; i++)
1278 string = g_variant_get_child_value (value, i);
1279 strv[i] = g_variant_dup_string (string, NULL);
1280 g_variant_unref (string);
1290 /* Type checking and querying {{{1 */
1292 * g_variant_get_type:
1293 * @value: a #GVariant
1294 * @returns: a #GVariantType
1296 * Determines the type of @value.
1298 * The return value is valid for the lifetime of @value and must not
1303 const GVariantType *
1304 g_variant_get_type (GVariant *value)
1306 GVariantTypeInfo *type_info;
1308 g_return_val_if_fail (value != NULL, NULL);
1310 type_info = g_variant_get_type_info (value);
1312 return (GVariantType *) g_variant_type_info_get_type_string (type_info);
1316 * g_variant_get_type_string:
1317 * @value: a #GVariant
1318 * @returns: the type string for the type of @value
1320 * Returns the type string of @value. Unlike the result of calling
1321 * g_variant_type_peek_string(), this string is nul-terminated. This
1322 * string belongs to #GVariant and must not be freed.
1327 g_variant_get_type_string (GVariant *value)
1329 GVariantTypeInfo *type_info;
1331 g_return_val_if_fail (value != NULL, NULL);
1333 type_info = g_variant_get_type_info (value);
1335 return g_variant_type_info_get_type_string (type_info);
1339 * g_variant_is_of_type:
1340 * @value: a #GVariant instance
1341 * @type: a #GVariantType
1342 * @returns: %TRUE if the type of @value matches @type
1344 * Checks if a value has a type matching the provided type.
1349 g_variant_is_of_type (GVariant *value,
1350 const GVariantType *type)
1352 return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
1356 * g_variant_is_container:
1357 * @value: a #GVariant instance
1358 * @returns: %TRUE if @value is a container
1360 * Checks if @value is a container.
1363 g_variant_is_container (GVariant *value)
1365 return g_variant_type_is_container (g_variant_get_type (value));
1370 * g_variant_classify:
1371 * @value: a #GVariant
1372 * @returns: the #GVariantClass of @value
1374 * Classifies @value according to its top-level type.
1380 * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
1381 * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
1382 * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
1383 * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
1384 * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
1385 * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
1386 * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
1387 * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
1388 * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
1389 * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating
1391 * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
1392 * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a DBus object path
1394 * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a DBus signature string.
1395 * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
1396 * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
1397 * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
1398 * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
1399 * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
1401 * The range of possible top-level types of #GVariant instances.
1406 g_variant_classify (GVariant *value)
1408 g_return_val_if_fail (value != NULL, 0);
1410 return *g_variant_get_type_string (value);
1413 /* Pretty printer {{{1 */
1415 * g_variant_print_string:
1416 * @value: a #GVariant
1417 * @string: a #GString, or %NULL
1418 * @type_annotate: %TRUE if type information should be included in
1420 * @returns: a #GString containing the string
1422 * Behaves as g_variant_print(), but operates on a #GString.
1424 * If @string is non-%NULL then it is appended to and returned. Else,
1425 * a new empty #GString is allocated and it is returned.
1430 g_variant_print_string (GVariant *value,
1432 gboolean type_annotate)
1434 if G_UNLIKELY (string == NULL)
1435 string = g_string_new (NULL);
1437 switch (g_variant_classify (value))
1439 case G_VARIANT_CLASS_MAYBE:
1441 g_string_append_printf (string, "@%s ",
1442 g_variant_get_type_string (value));
1444 if (g_variant_n_children (value))
1446 gchar *printed_child;
1451 * Consider the case of the type "mmi". In this case we could
1452 * write "Just Just 4", but "4" alone is totally unambiguous,
1453 * so we try to drop "Just" where possible.
1455 * We have to be careful not to always drop "Just", though,
1456 * since "Nothing" needs to be distinguishable from "Just
1457 * Nothing". The case where we need to ensure we keep the
1458 * "Just" is actually exactly the case where we have a nested
1461 * Instead of searching for that nested Nothing, we just print
1462 * the contained value into a separate string and see if we
1463 * end up with "Nothing" at the end of it. If so, we need to
1464 * add "Just" at our level.
1466 element = g_variant_get_child_value (value, 0);
1467 printed_child = g_variant_print (element, FALSE);
1468 g_variant_unref (element);
1470 if (g_str_has_suffix (printed_child, "Nothing"))
1471 g_string_append (string, "Just ");
1472 g_string_append (string, printed_child);
1473 g_free (printed_child);
1476 g_string_append (string, "Nothing");
1480 case G_VARIANT_CLASS_ARRAY:
1481 /* it's an array so the first character of the type string is 'a'
1483 * if the first two characters are 'a{' then it's an array of
1484 * dictionary entries (ie: a dictionary) so we print that
1487 if (g_variant_get_type_string (value)[1] == '{')
1490 const gchar *comma = "";
1493 if ((n = g_variant_n_children (value)) == 0)
1496 g_string_append_printf (string, "@%s ",
1497 g_variant_get_type_string (value));
1498 g_string_append (string, "{}");
1502 g_string_append_c (string, '{');
1503 for (i = 0; i < n; i++)
1505 GVariant *entry, *key, *val;
1507 g_string_append (string, comma);
1510 entry = g_variant_get_child_value (value, i);
1511 key = g_variant_get_child_value (entry, 0);
1512 val = g_variant_get_child_value (entry, 1);
1513 g_variant_unref (entry);
1515 g_variant_print_string (key, string, type_annotate);
1516 g_variant_unref (key);
1517 g_string_append (string, ": ");
1518 g_variant_print_string (val, string, type_annotate);
1519 g_variant_unref (val);
1520 type_annotate = FALSE;
1522 g_string_append_c (string, '}');
1525 /* normal (non-dictionary) array */
1527 const gchar *comma = "";
1530 if ((n = g_variant_n_children (value)) == 0)
1533 g_string_append_printf (string, "@%s ",
1534 g_variant_get_type_string (value));
1535 g_string_append (string, "[]");
1539 g_string_append_c (string, '[');
1540 for (i = 0; i < n; i++)
1544 g_string_append (string, comma);
1547 element = g_variant_get_child_value (value, i);
1549 g_variant_print_string (element, string, type_annotate);
1550 g_variant_unref (element);
1551 type_annotate = FALSE;
1553 g_string_append_c (string, ']');
1558 case G_VARIANT_CLASS_TUPLE:
1562 n = g_variant_n_children (value);
1564 g_string_append_c (string, '(');
1565 for (i = 0; i < n; i++)
1569 element = g_variant_get_child_value (value, i);
1570 g_variant_print_string (element, string, type_annotate);
1571 g_string_append (string, ", ");
1572 g_variant_unref (element);
1575 /* for >1 item: remove final ", "
1576 * for 1 item: remove final " ", but leave the ","
1577 * for 0 items: there is only "(", so remove nothing
1579 g_string_truncate (string, string->len - (n > 0) - (n > 1));
1580 g_string_append_c (string, ')');
1584 case G_VARIANT_CLASS_DICT_ENTRY:
1588 g_string_append_c (string, '{');
1590 element = g_variant_get_child_value (value, 0);
1591 g_variant_print_string (element, string, type_annotate);
1592 g_variant_unref (element);
1594 g_string_append (string, ", ");
1596 element = g_variant_get_child_value (value, 1);
1597 g_variant_print_string (element, string, type_annotate);
1598 g_variant_unref (element);
1600 g_string_append_c (string, '}');
1604 case G_VARIANT_CLASS_VARIANT:
1606 GVariant *child = g_variant_get_variant (value);
1608 /* Always annotate types in nested variants, because they are
1609 * (by nature) of variable type.
1611 g_string_append_c (string, '<');
1612 g_variant_print_string (child, string, TRUE);
1613 g_string_append_c (string, '>');
1615 g_variant_unref (child);
1619 case G_VARIANT_CLASS_BOOLEAN:
1620 if (g_variant_get_boolean (value))
1621 g_string_append (string, "true");
1623 g_string_append (string, "false");
1626 case G_VARIANT_CLASS_STRING:
1628 const gchar *str = g_variant_get_string (value, NULL);
1629 gchar *escaped = g_strescape (str, NULL);
1631 g_string_append_printf (string, "\'%s\'", escaped);
1637 case G_VARIANT_CLASS_BYTE:
1639 g_string_append (string, "byte ");
1640 g_string_append_printf (string, "0x%02x",
1641 g_variant_get_byte (value));
1644 case G_VARIANT_CLASS_INT16:
1646 g_string_append (string, "int16 ");
1647 g_string_append_printf (string, "%"G_GINT16_FORMAT,
1648 g_variant_get_int16 (value));
1651 case G_VARIANT_CLASS_UINT16:
1653 g_string_append (string, "uint16 ");
1654 g_string_append_printf (string, "%"G_GUINT16_FORMAT,
1655 g_variant_get_uint16 (value));
1658 case G_VARIANT_CLASS_INT32:
1659 /* Never annotate this type because it is the default for numbers
1660 * (and this is a *pretty* printer)
1662 g_string_append_printf (string, "%"G_GINT32_FORMAT,
1663 g_variant_get_int32 (value));
1666 case G_VARIANT_CLASS_HANDLE:
1668 g_string_append (string, "handle ");
1669 g_string_append_printf (string, "%"G_GINT32_FORMAT,
1670 g_variant_get_handle (value));
1673 case G_VARIANT_CLASS_UINT32:
1675 g_string_append (string, "uint32 ");
1676 g_string_append_printf (string, "%"G_GUINT32_FORMAT,
1677 g_variant_get_uint32 (value));
1680 case G_VARIANT_CLASS_INT64:
1682 g_string_append (string, "int64 ");
1683 g_string_append_printf (string, "%"G_GINT64_FORMAT,
1684 g_variant_get_int64 (value));
1687 case G_VARIANT_CLASS_UINT64:
1689 g_string_append (string, "uint64 ");
1690 g_string_append_printf (string, "%"G_GUINT64_FORMAT,
1691 g_variant_get_uint64 (value));
1694 case G_VARIANT_CLASS_DOUBLE:
1699 g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
1701 for (i = 0; buffer[i]; i++)
1702 if (buffer[i] == '.' || buffer[i] == 'e' ||
1703 buffer[i] == 'n' || buffer[i] == 'N')
1706 /* if there is no '.' or 'e' in the float then add one */
1707 if (buffer[i] == '\0')
1714 g_string_append (string, buffer);
1718 case G_VARIANT_CLASS_OBJECT_PATH:
1720 g_string_append (string, "objectpath ");
1721 g_string_append_printf (string, "\'%s\'",
1722 g_variant_get_string (value, NULL));
1725 case G_VARIANT_CLASS_SIGNATURE:
1727 g_string_append (string, "signature ");
1728 g_string_append_printf (string, "\'%s\'",
1729 g_variant_get_string (value, NULL));
1733 g_assert_not_reached ();
1741 * @value: a #GVariant
1742 * @type_annotate: %TRUE if type information should be included in
1744 * @returns: a newly-allocated string holding the result.
1746 * Pretty-prints @value in the format understood by g_variant_parse().
1748 * If @type_annotate is %TRUE, then type information is included in
1752 g_variant_print (GVariant *value,
1753 gboolean type_annotate)
1755 return g_string_free (g_variant_print_string (value, NULL, type_annotate),
1759 /* Hash, Equal {{{1 */
1762 * @value: a basic #GVariant value as a #gconstpointer
1763 * @returns: a hash value corresponding to @value
1765 * Generates a hash value for a #GVariant instance.
1767 * The output of this function is guaranteed to be the same for a given
1768 * value only per-process. It may change between different processor
1769 * architectures or even different versions of GLib. Do not use this
1770 * function as a basis for building protocols or file formats.
1772 * The type of @value is #gconstpointer only to allow use of this
1773 * function with #GHashTable. @value must be a #GVariant.
1778 g_variant_hash (gconstpointer value_)
1780 GVariant *value = (GVariant *) value_;
1782 switch (g_variant_classify (value))
1784 case G_VARIANT_CLASS_STRING:
1785 case G_VARIANT_CLASS_OBJECT_PATH:
1786 case G_VARIANT_CLASS_SIGNATURE:
1787 return g_str_hash (g_variant_get_string (value, NULL));
1789 case G_VARIANT_CLASS_BOOLEAN:
1790 /* this is a very odd thing to hash... */
1791 return g_variant_get_boolean (value);
1793 case G_VARIANT_CLASS_BYTE:
1794 return g_variant_get_byte (value);
1796 case G_VARIANT_CLASS_INT16:
1797 case G_VARIANT_CLASS_UINT16:
1801 ptr = g_variant_get_data (value);
1809 case G_VARIANT_CLASS_INT32:
1810 case G_VARIANT_CLASS_UINT32:
1811 case G_VARIANT_CLASS_HANDLE:
1815 ptr = g_variant_get_data (value);
1823 case G_VARIANT_CLASS_INT64:
1824 case G_VARIANT_CLASS_UINT64:
1825 case G_VARIANT_CLASS_DOUBLE:
1826 /* need a separate case for these guys because otherwise
1827 * performance could be quite bad on big endian systems
1832 ptr = g_variant_get_data (value);
1835 return ptr[0] + ptr[1];
1841 g_return_val_if_fail (!g_variant_is_container (value), 0);
1842 g_assert_not_reached ();
1848 * @one: a #GVariant instance
1849 * @two: a #GVariant instance
1850 * @returns: %TRUE if @one and @two are equal
1852 * Checks if @one and @two have the same type and value.
1854 * The types of @one and @two are #gconstpointer only to allow use of
1855 * this function with #GHashTable. They must each be a #GVariant.
1860 g_variant_equal (gconstpointer one,
1865 g_return_val_if_fail (one != NULL && two != NULL, FALSE);
1867 if (g_variant_get_type_info ((GVariant *) one) !=
1868 g_variant_get_type_info ((GVariant *) two))
1871 /* if both values are trusted to be in their canonical serialised form
1872 * then a simple memcmp() of their serialised data will answer the
1875 * if not, then this might generate a false negative (since it is
1876 * possible for two different byte sequences to represent the same
1877 * value). for now we solve this by pretty-printing both values and
1878 * comparing the result.
1880 if (g_variant_is_trusted ((GVariant *) one) &&
1881 g_variant_is_trusted ((GVariant *) two))
1883 gconstpointer data_one, data_two;
1884 gsize size_one, size_two;
1886 size_one = g_variant_get_size ((GVariant *) one);
1887 size_two = g_variant_get_size ((GVariant *) two);
1889 if (size_one != size_two)
1892 data_one = g_variant_get_data ((GVariant *) one);
1893 data_two = g_variant_get_data ((GVariant *) two);
1895 equal = memcmp (data_one, data_two, size_one) == 0;
1899 gchar *strone, *strtwo;
1901 strone = g_variant_print ((GVariant *) one, FALSE);
1902 strtwo = g_variant_print ((GVariant *) two, FALSE);
1903 equal = strcmp (strone, strtwo) == 0;
1911 /* GVariantIter {{{1 */
1915 * #GVariantIter is an opaque data structure and can only be accessed
1916 * using the following functions.
1923 const gchar *loop_format;
1931 struct stack_iter iter;
1933 GVariant *value_ref;
1937 #define GVSI(i) ((struct stack_iter *) (i))
1938 #define GVHI(i) ((struct heap_iter *) (i))
1939 #define GVSI_MAGIC ((gsize) 3579507750u)
1940 #define GVHI_MAGIC ((gsize) 1450270775u)
1941 #define is_valid_iter(i) (i != NULL && \
1942 GVSI(i)->magic == GVSI_MAGIC)
1943 #define is_valid_heap_iter(i) (GVHI(i)->magic == GVHI_MAGIC && \
1947 * g_variant_iter_new:
1948 * @value: a container #GVariant
1949 * @returns: a new heap-allocated #GVariantIter
1951 * Creates a heap-allocated #GVariantIter for iterating over the items
1954 * Use g_variant_iter_free() to free the return value when you no longer
1957 * A reference is taken to @value and will be released only when
1958 * g_variant_iter_free() is called.
1963 g_variant_iter_new (GVariant *value)
1967 iter = (GVariantIter *) g_slice_new (struct heap_iter);
1968 GVHI(iter)->value_ref = g_variant_ref (value);
1969 GVHI(iter)->magic = GVHI_MAGIC;
1971 g_variant_iter_init (iter, value);
1977 * g_variant_iter_init:
1978 * @iter: a pointer to a #GVariantIter
1979 * @value: a container #GVariant
1980 * @returns: the number of items in @value
1982 * Initialises (without allocating) a #GVariantIter. @iter may be
1983 * completely uninitialised prior to this call; its old value is
1986 * The iterator remains valid for as long as @value exists, and need not
1987 * be freed in any way.
1992 g_variant_iter_init (GVariantIter *iter,
1995 g_assert (sizeof (GVariantIter) == sizeof (struct stack_iter));
1997 GVSI(iter)->magic = GVSI_MAGIC;
1998 GVSI(iter)->value = value;
1999 GVSI(iter)->n = g_variant_n_children (value);
2001 GVSI(iter)->loop_format = NULL;
2003 return GVSI(iter)->n;
2007 * g_variant_iter_copy:
2008 * @iter: a #GVariantIter
2009 * @returns: a new heap-allocated #GVariantIter
2011 * Creates a new heap-allocated #GVariantIter to iterate over the
2012 * container that was being iterated over by @iter. Iteration begins on
2013 * the new iterator from the current position of the old iterator but
2014 * the two copies are independent past that point.
2016 * Use g_variant_iter_free() to free the return value when you no longer
2019 * A reference is taken to the container that @iter is iterating over
2020 * and will be releated only when g_variant_iter_free() is called.
2025 g_variant_iter_copy (GVariantIter *iter)
2029 g_return_val_if_fail (is_valid_iter (iter), 0);
2031 copy = g_variant_iter_new (GVSI(iter)->value);
2032 GVSI(copy)->i = GVSI(iter)->i;
2038 * g_variant_iter_n_children:
2039 * @iter: a #GVariantIter
2040 * @returns: the number of children in the container
2042 * Queries the number of child items in the container that we are
2043 * iterating over. This is the total number of items -- not the number
2044 * of items remaining.
2046 * This function might be useful for preallocation of arrays.
2051 g_variant_iter_n_children (GVariantIter *iter)
2053 g_return_val_if_fail (is_valid_iter (iter), 0);
2055 return GVSI(iter)->n;
2059 * g_variant_iter_free:
2060 * @iter: a heap-allocated #GVariantIter
2062 * Frees a heap-allocated #GVariantIter. Only call this function on
2063 * iterators that were returned by g_variant_iter_new() or
2064 * g_variant_iter_copy().
2069 g_variant_iter_free (GVariantIter *iter)
2071 g_return_if_fail (is_valid_heap_iter (iter));
2073 g_variant_unref (GVHI(iter)->value_ref);
2074 GVHI(iter)->magic = 0;
2076 g_slice_free (struct heap_iter, GVHI(iter));
2080 * g_variant_iter_next_value:
2081 * @iter: a #GVariantIter
2082 * @returns: a #GVariant, or %NULL
2084 * Gets the next item in the container. If no more items remain then
2085 * %NULL is returned.
2087 * Use g_variant_unref() to drop your reference on the return value when
2088 * you no longer need it.
2091 * <title>Iterating with g_variant_iter_next_value()</title>
2093 * /<!-- -->* recursively iterate a container *<!-- -->/
2095 * iterate_container_recursive (GVariant *container)
2097 * GVariantIter iter;
2100 * g_variant_iter_init (&iter, dictionary);
2101 * while ((child = g_variant_iter_next_value (&iter)))
2103 * g_print ("type '%s'\n", g_variant_get_type_string (child));
2105 * if (g_variant_is_container (child))
2106 * iterate_container_recursive (child);
2108 * g_variant_unref (child);
2117 g_variant_iter_next_value (GVariantIter *iter)
2119 g_return_val_if_fail (is_valid_iter (iter), FALSE);
2121 if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n)
2123 g_critical ("g_variant_iter_next_value: must not be called again "
2124 "after NULL has already been returned.");
2130 if (GVSI(iter)->i < GVSI(iter)->n)
2131 return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i);
2137 * g_variant_iter_loop:
2138 * @iter: a #GVariantIter
2139 * @format_string: a GVariant format string
2140 * @...: the arguments to unpack the value into
2141 * @returns: %TRUE if a value was unpacked, or %FALSE if there as no
2144 * Gets the next item in the container and unpacks it into the variable
2145 * argument list according to @format_string, returning %TRUE.
2147 * If no more items remain then %FALSE is returned.
2149 * On the first call to this function, the pointers appearing on the
2150 * variable argument list are assumed to point at uninitialised memory.
2151 * On the second and later calls, it is assumed that the same pointers
2152 * will be given and that they will point to the memory as set by the
2153 * previous call to this function. This allows the previous values to
2154 * be freed, as appropriate.
2156 * This function is intended to be used with a while loop as
2157 * demonstrated in the following example. This function can only be
2158 * used when iterating over an array. It is only valid to call this
2159 * function with a string constant for the format string and the same
2160 * string constant must be used each time. Mixing calls to this
2161 * function and g_variant_iter_next() or g_variant_iter_next_value() on
2162 * the same iterator is not recommended.
2165 * <title>Memory management with g_variant_iter_loop()</title>
2167 * /<!-- -->* Iterates a dictionary of type 'a{sv}' *<!-- -->/
2169 * iterate_dictionary (GVariant *dictionary)
2171 * GVariantIter iter;
2175 * g_variant_iter_init (&iter, dictionary);
2176 * while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
2178 * g_print ("Item '%s' has type '%s'\n", key,
2179 * g_variant_get_type_string (value));
2181 * /<!-- -->* no need to free 'key' and 'value' here *<!-- -->/
2187 * If you want a slightly less magical alternative that requires more
2188 * typing, see g_variant_iter_next().
2193 g_variant_iter_loop (GVariantIter *iter,
2194 const gchar *format_string,
2197 gboolean first_time = GVSI(iter)->loop_format == NULL;
2200 g_return_val_if_fail (first_time ||
2201 format_string == GVSI(iter)->loop_format,
2206 TYPE_CHECK (GVSI(iter)->value, G_VARIANT_TYPE_ARRAY, FALSE);
2207 GVSI(iter)->loop_format = format_string;
2210 value = g_variant_iter_next_value (iter);
2216 va_start (ap, format_string);
2217 /* varargs get stuff */
2220 g_variant_unref (value);
2223 return value != NULL;
2227 * g_variant_iter_next:
2228 * @iter: a #GVariantIter
2229 * @format_string: a GVariant format string
2230 * @...: the arguments to unpack the value into
2231 * @returns: %TRUE if a value was unpacked, or %FALSE if there as no
2234 * Gets the next item in the container and unpacks it into the variable
2235 * argument list according to @format_string, returning %TRUE.
2237 * If no more items remain then %FALSE is returned.
2239 * All of the pointers given on the variable arguments list of this
2240 * function are assumed to point at uninitialised memory. It is the
2241 * responsibility of the caller to free all of the values returned by
2242 * the unpacking process.
2245 * <title>Memory management with g_variant_iter_next()</title>
2247 * /<!-- -->* Iterates a dictionary of type 'a{sv}' *<!-- -->/
2249 * iterate_dictionary (GVariant *dictionary)
2251 * GVariantIter iter;
2255 * g_variant_iter_init (&iter, dictionary);
2256 * while (g_variant_iter_next (&iter, "{sv}", &key, &value))
2258 * g_print ("Item '%s' has type '%s'\n", key,
2259 * g_variant_get_type_string (value));
2261 * /<!-- -->* must free data for ourselves *<!-- -->/
2262 * g_variant_unref (value);
2269 * For a solution that is likely to be more convenient to C programmers,
2270 * see g_variant_iter_loop().
2275 g_variant_iter_next (GVariantIter *iter,
2276 const gchar *format_string,
2281 value = g_variant_iter_next_value (iter);
2287 va_start (ap, format_string);
2288 /* varargs get stuff */
2291 g_variant_unref (value);
2294 return value != NULL;
2297 /* GVariantBuilder {{{1 */
2301 * A utility type for constructing container-type #GVariant instances.
2303 * This is an opaque structure and may only be accessed using the
2304 * following functions.
2306 * #GVariantBuilder is not threadsafe in any way. Do not attempt to
2307 * access it from more than one thread.
2310 struct stack_builder
2312 GVariantBuilder *parent;
2315 /* type constraint explicitly specified by 'type'.
2316 * for tuple types, this moves along as we add more items.
2318 const GVariantType *expected_type;
2320 /* type constraint implied by previous array item.
2322 const GVariantType *prev_item_type;
2324 /* constraints on the number of children. max = -1 for unlimited. */
2328 /* dynamically-growing pointer array */
2329 GVariant **children;
2330 gsize allocated_children;
2333 /* set to '1' if all items in the container will have the same type
2334 * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
2336 guint uniform_item_types : 1;
2338 /* set to '1' initially and changed to '0' if an untrusted value is
2348 GVariantBuilder builder;
2354 #define GVSB(b) ((struct stack_builder *) (b))
2355 #define GVHB(b) ((struct heap_builder *) (b))
2356 #define GVSB_MAGIC ((gsize) 1033660112u)
2357 #define GVHB_MAGIC ((gsize) 3087242682u)
2358 #define is_valid_builder(b) (b != NULL && \
2359 GVSB(b)->magic == GVSB_MAGIC)
2360 #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
2363 * g_variant_builder_new:
2364 * @type: a container type
2365 * @returns: a #GVariantBuilder
2367 * Allocates and initialises a new #GVariantBuilder.
2369 * You should call g_variant_builder_unref() on the return value when it
2370 * is no longer needed. The memory will not be automatically freed by
2373 * In most cases it is easier to place a #GVariantBuilder directly on
2374 * the stack of the calling function and initialise it with
2375 * g_variant_builder_init().
2380 g_variant_builder_new (const GVariantType *type)
2382 GVariantBuilder *builder;
2384 builder = (GVariantBuilder *) g_slice_new (struct heap_builder);
2385 g_variant_builder_init (builder, type);
2386 GVHB(builder)->magic = GVHB_MAGIC;
2387 GVHB(builder)->ref_count = 1;
2393 * g_variant_builder_unref:
2394 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
2396 * Decreases the reference count on @builder.
2398 * In the event that there are no more references, releases all memory
2399 * associated with the #GVariantBuilder.
2401 * Don't call this on stack-allocated #GVariantBuilder instances or bad
2402 * things will happen.
2407 g_variant_builder_unref (GVariantBuilder *builder)
2409 g_return_if_fail (is_valid_heap_builder (builder));
2411 if (--GVHB(builder)->ref_count)
2414 g_variant_builder_clear (builder);
2415 GVHB(builder)->magic = 0;
2417 g_slice_free (struct heap_builder, GVHB(builder));
2421 * g_variant_builder_ref:
2422 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
2423 * @returns: a new reference to @builder
2425 * Increases the reference count on @builder.
2427 * Don't call this on stack-allocated #GVariantBuilder instances or bad
2428 * things will happen.
2433 g_variant_builder_ref (GVariantBuilder *builder)
2435 g_return_val_if_fail (is_valid_heap_builder (builder), NULL);
2437 GVHB(builder)->ref_count++;
2443 * g_variant_builder_clear:
2444 * @builder: a #GVariantBuilder
2446 * Releases all memory associated with a #GVariantBuilder without
2447 * freeing the #GVariantBuilder structure itself.
2449 * It typically only makes sense to do this on a stack-allocated
2450 * #GVariantBuilder if you want to abort building the value part-way
2451 * through. This function need not be called if you call
2452 * g_variant_builder_end() and it also doesn't need to be called on
2453 * builders allocated with g_variant_builder_new (see
2454 * g_variant_builder_free() for that).
2456 * This function leaves the #GVariantBuilder structure set to all-zeros.
2457 * It is valid to call this function on either an initialised
2458 * #GVariantBuilder or one that is set to all-zeros but it is not valid
2459 * to call this function on uninitialised memory.
2464 g_variant_builder_clear (GVariantBuilder *builder)
2468 if (GVSB(builder)->magic == 0)
2469 /* all-zeros case */
2472 g_return_if_fail (is_valid_builder (builder));
2474 g_variant_type_free (GVSB(builder)->type);
2476 for (i = 0; i < GVSB(builder)->offset; i++)
2477 g_variant_unref (GVSB(builder)->children[i]);
2479 g_free (GVSB(builder)->children);
2481 if (GVSB(builder)->parent)
2483 g_variant_builder_clear (GVSB(builder)->parent);
2484 g_slice_free (GVariantBuilder, GVSB(builder)->parent);
2487 memset (builder, 0, sizeof (GVariantBuilder));
2491 * g_variant_builder_init:
2492 * @builder: a #GVariantBuilder
2493 * @type: a container type
2495 * Initialises a #GVariantBuilder structure.
2497 * @type must be non-%NULL. It specifies the type of container to
2498 * construct. It can be an indefinite type such as
2499 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
2500 * Maybe, array, tuple, dictionary entry and variant-typed values may be
2503 * After the builder is initialised, values are added using
2504 * g_variant_builder_add_value() or g_variant_builder_add().
2506 * After all the child values are added, g_variant_builder_end() frees
2507 * the memory associated with the builder and returns the #GVariant that
2510 * This function completely ignores the previous contents of @builder.
2511 * On one hand this means that it is valid to pass in completely
2512 * uninitialised memory. On the other hand, this means that if you are
2513 * initialising over top of an existing #GVariantBuilder you need to
2514 * first call g_variant_builder_clear() in order to avoid leaking
2517 * You must not call g_variant_builder_ref() or
2518 * g_variant_builder_unref() on a #GVariantBuilder that was initialised
2519 * with this function. If you ever pass a reference to a
2520 * #GVariantBuilder outside of the control of your own code then you
2521 * should assume that the person receiving that reference may try to use
2522 * reference counting; you should use g_variant_builder_new() instead of
2528 g_variant_builder_init (GVariantBuilder *builder,
2529 const GVariantType *type)
2531 g_return_if_fail (type != NULL);
2532 g_return_if_fail (g_variant_type_is_container (type));
2534 g_assert (sizeof (struct stack_builder) < sizeof (GVariantBuilder));
2535 memset (builder, 0, sizeof (GVariantBuilder));
2537 GVSB(builder)->type = g_variant_type_copy (type);
2538 GVSB(builder)->magic = GVSB_MAGIC;
2539 GVSB(builder)->trusted = TRUE;
2541 switch (*(const gchar *) type)
2543 case G_VARIANT_CLASS_VARIANT:
2544 GVSB(builder)->uniform_item_types = TRUE;
2545 GVSB(builder)->allocated_children = 1;
2546 GVSB(builder)->expected_type = NULL;
2547 GVSB(builder)->min_items = 1;
2548 GVSB(builder)->max_items = 1;
2551 case G_VARIANT_CLASS_ARRAY:
2552 GVSB(builder)->uniform_item_types = TRUE;
2553 GVSB(builder)->allocated_children = 8;
2554 GVSB(builder)->expected_type =
2555 g_variant_type_element (GVSB(builder)->type);
2556 GVSB(builder)->min_items = 0;
2557 GVSB(builder)->max_items = -1;
2560 case G_VARIANT_CLASS_MAYBE:
2561 GVSB(builder)->uniform_item_types = TRUE;
2562 GVSB(builder)->allocated_children = 1;
2563 GVSB(builder)->expected_type =
2564 g_variant_type_element (GVSB(builder)->type);
2565 GVSB(builder)->min_items = 0;
2566 GVSB(builder)->max_items = 1;
2569 case G_VARIANT_CLASS_DICT_ENTRY:
2570 GVSB(builder)->uniform_item_types = FALSE;
2571 GVSB(builder)->allocated_children = 2;
2572 GVSB(builder)->expected_type =
2573 g_variant_type_key (GVSB(builder)->type);
2574 GVSB(builder)->min_items = 2;
2575 GVSB(builder)->max_items = 2;
2578 case 'r': /* G_VARIANT_TYPE_TUPLE was given */
2579 GVSB(builder)->uniform_item_types = FALSE;
2580 GVSB(builder)->allocated_children = 8;
2581 GVSB(builder)->expected_type = NULL;
2582 GVSB(builder)->min_items = 0;
2583 GVSB(builder)->max_items = -1;
2586 case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */
2587 GVSB(builder)->allocated_children = g_variant_type_n_items (type);
2588 GVSB(builder)->expected_type =
2589 g_variant_type_first (GVSB(builder)->type);
2590 GVSB(builder)->min_items = GVSB(builder)->allocated_children;
2591 GVSB(builder)->max_items = GVSB(builder)->allocated_children;
2592 GVSB(builder)->uniform_item_types = FALSE;
2596 g_assert_not_reached ();
2599 GVSB(builder)->children = g_new (GVariant *,
2600 GVSB(builder)->allocated_children);
2604 g_variant_builder_make_room (struct stack_builder *builder)
2606 if (builder->offset == builder->allocated_children)
2608 builder->allocated_children *= 2;
2609 builder->children = g_renew (GVariant *, builder->children,
2610 builder->allocated_children);
2615 * g_variant_builder_add_value:
2616 * @builder: a #GVariantBuilder
2617 * @value: a #GVariant
2619 * Adds @value to @builder.
2621 * It is an error to call this function in any way that would create an
2622 * inconsistent value to be constructed. Some examples of this are
2623 * putting different types of items into an array, putting the wrong
2624 * types or number of items in a tuple, putting more than one value into
2630 g_variant_builder_add_value (GVariantBuilder *builder,
2633 g_return_if_fail (is_valid_builder (builder));
2634 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
2635 g_return_if_fail (!GVSB(builder)->expected_type ||
2636 g_variant_is_of_type (value,
2637 GVSB(builder)->expected_type));
2638 g_return_if_fail (!GVSB(builder)->prev_item_type ||
2639 g_variant_is_of_type (value,
2640 GVSB(builder)->prev_item_type));
2642 GVSB(builder)->trusted &= g_variant_is_trusted (value);
2644 if (!GVSB(builder)->uniform_item_types)
2646 /* advance our expected type pointers */
2647 if (GVSB(builder)->expected_type)
2648 GVSB(builder)->expected_type =
2649 g_variant_type_next (GVSB(builder)->expected_type);
2651 if (GVSB(builder)->prev_item_type)
2652 GVSB(builder)->prev_item_type =
2653 g_variant_type_next (GVSB(builder)->prev_item_type);
2656 GVSB(builder)->prev_item_type = g_variant_get_type (value);
2658 g_variant_builder_make_room (GVSB(builder));
2660 GVSB(builder)->children[GVSB(builder)->offset++] =
2661 g_variant_ref_sink (value);
2665 * g_variant_builder_open:
2666 * @builder: a #GVariantBuilder
2667 * @type: a #GVariantType
2669 * Opens a subcontainer inside the given @builder. When done adding
2670 * items to the subcontainer, g_variant_builder_close() must be called.
2672 * It is an error to call this function in any way that would cause an
2673 * inconsistent value to be constructed (ie: adding too many values or
2674 * a value of an incorrect type).
2679 g_variant_builder_open (GVariantBuilder *builder,
2680 const GVariantType *type)
2682 GVariantBuilder *parent;
2684 g_return_if_fail (is_valid_builder (builder));
2685 g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
2686 g_return_if_fail (!GVSB(builder)->expected_type ||
2687 g_variant_type_is_subtype_of (type,
2688 GVSB(builder)->expected_type));
2689 g_return_if_fail (!GVSB(builder)->prev_item_type ||
2690 g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type,
2693 parent = g_slice_dup (GVariantBuilder, builder);
2694 g_variant_builder_init (builder, type);
2695 GVSB(builder)->parent = parent;
2697 /* push the prev_item_type down into the subcontainer */
2698 if (GVSB(parent)->prev_item_type)
2700 if (!GVSB(builder)->uniform_item_types)
2701 /* tuples and dict entries */
2702 GVSB(builder)->prev_item_type =
2703 g_variant_type_first (GVSB(parent)->prev_item_type);
2705 else if (!g_variant_type_is_variant (GVSB(builder)->type))
2706 /* maybes and arrays */
2707 GVSB(builder)->prev_item_type =
2708 g_variant_type_element (GVSB(parent)->prev_item_type);
2713 * g_variant_builder_close:
2714 * @builder: a #GVariantBuilder
2716 * Closes the subcontainer inside the given @builder that was opened by
2717 * the most recent call to g_variant_builder_open().
2719 * It is an error to call this function in any way that would create an
2720 * inconsistent value to be constructed (ie: too few values added to the
2726 g_variant_builder_close (GVariantBuilder *builder)
2728 GVariantBuilder *parent;
2730 g_return_if_fail (is_valid_builder (builder));
2731 g_return_if_fail (GVSB(builder)->parent != NULL);
2733 parent = GVSB(builder)->parent;
2734 GVSB(builder)->parent = NULL;
2736 g_variant_builder_add_value (parent, g_variant_builder_end (builder));
2739 g_slice_free (GVariantBuilder, parent);
2743 * g_variant_make_maybe_type:
2744 * @element: a #GVariant
2746 * Return the type of a maybe containing @element.
2748 static GVariantType *
2749 g_variant_make_maybe_type (GVariant *element)
2751 return g_variant_type_new_maybe (g_variant_get_type (element));
2755 * g_variant_make_array_type:
2756 * @element: a #GVariant
2758 * Return the type of an array containing @element.
2760 static GVariantType *
2761 g_variant_make_array_type (GVariant *element)
2763 return g_variant_type_new_array (g_variant_get_type (element));
2767 * g_variant_builder_end:
2768 * @builder: a #GVariantBuilder
2769 * @returns: a new, floating, #GVariant
2771 * Ends the builder process and returns the constructed value.
2773 * This call automatically reduces the reference count on @builder by
2774 * one, unless it has previously had g_variant_builder_no_autofree()
2775 * called on it. Unless you've taken other actions, this is usually
2776 * sufficient to free @builder.
2778 * Even if additional references are held, it is not permissible to use
2779 * @builder in any way after this call except for further reference
2780 * counting operations.
2782 * It is an error to call this function in any way that would create an
2783 * inconsistent value to be constructed (ie: insufficient number of
2784 * items added to a container with a specific number of children
2785 * required). It is also an error to call this function if the builder
2786 * was created with an indefinite array or maybe type and no children
2787 * have been added; in this case it is impossible to infer the type of
2793 g_variant_builder_end (GVariantBuilder *builder)
2795 GVariantType *my_type;
2798 g_return_val_if_fail (is_valid_builder (builder), NULL);
2799 g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items,
2801 g_return_val_if_fail (!GVSB(builder)->uniform_item_types ||
2802 GVSB(builder)->prev_item_type != NULL ||
2803 g_variant_type_is_definite (GVSB(builder)->type),
2806 if (g_variant_type_is_definite (GVSB(builder)->type))
2807 my_type = g_variant_type_copy (GVSB(builder)->type);
2809 else if (g_variant_type_is_maybe (GVSB(builder)->type))
2810 my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]);
2812 else if (g_variant_type_is_array (GVSB(builder)->type))
2813 my_type = g_variant_make_array_type (GVSB(builder)->children[0]);
2815 else if (g_variant_type_is_tuple (GVSB(builder)->type))
2816 my_type = g_variant_make_tuple_type (GVSB(builder)->children,
2817 GVSB(builder)->offset);
2819 else if (g_variant_type_is_dict_entry (GVSB(builder)->type))
2820 my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0],
2821 GVSB(builder)->children[1]);
2823 g_assert_not_reached ();
2825 value = g_variant_new_from_children (my_type,
2826 g_renew (GVariant *,
2827 GVSB(builder)->children,
2828 GVSB(builder)->offset),
2829 GVSB(builder)->offset,
2830 GVSB(builder)->trusted);
2831 GVSB(builder)->children = NULL;
2832 GVSB(builder)->offset = 0;
2834 g_variant_builder_clear (builder);
2835 g_variant_type_free (my_type);
2841 #define __G_VARIANT_C__
2842 #include "galiasdef.c"
2844 /* vim:set foldmethod=marker: */