2 * Copyright © 2007, 2008 Ryan Lortie
3 * Copyright © 2010 Codethink Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Ryan Lortie <desrt@desrt.ca>
25 #include <glib/gvariant-serialiser.h>
26 #include "gvariant-internal.h"
27 #include <glib/gvariant-core.h>
28 #include <glib/gtestutils.h>
29 #include <glib/gstrfuncs.h>
30 #include <glib/gslice.h>
31 #include <glib/ghash.h>
32 #include <glib/gmem.h>
40 * @short_description: strongly typed value datatype
41 * @see_also: GVariantType
43 * #GVariant is a variant datatype; it stores a value along with
44 * information about the type of that value. The range of possible
45 * values is determined by the type. The type system used by #GVariant
48 * #GVariant instances always have a type and a value (which are given
49 * at construction time). The type and value of a #GVariant instance
50 * can never change other than by the #GVariant itself being
51 * destroyed. A #GVariant cannot contain a pointer.
53 * #GVariant is reference counted using g_variant_ref() and
54 * g_variant_unref(). #GVariant also has floating reference counts --
55 * see g_variant_ref_sink().
57 * #GVariant is completely threadsafe. A #GVariant instance can be
58 * concurrently accessed in any way from any number of threads without
61 * #GVariant is heavily optimised for dealing with data in serialised
62 * form. It works particularly well with data located in memory-mapped
63 * files. It can perform nearly all deserialisation operations in a
64 * small constant time, usually touching only a single memory page.
65 * Serialised #GVariant data can also be sent over the network.
67 * #GVariant is largely compatible with D-Bus. Almost all types of
68 * #GVariant instances can be sent over D-Bus. See #GVariantType for
69 * exceptions. (However, #GVariant's serialisation format is not the same
70 * as the serialisation format of a D-Bus message body: use #GDBusMessage,
71 * in the gio library, for those.)
73 * For space-efficiency, the #GVariant serialisation format does not
74 * automatically include the variant's length, type or endianness,
75 * which must either be implied from context (such as knowledge that a
76 * particular file format always contains a little-endian
77 * %G_VARIANT_TYPE_VARIANT which occupies the whole length of the file)
78 * or supplied out-of-band (for instance, a length, type and/or endianness
79 * indicator could be placed at the beginning of a file, network message
82 * A #GVariant's size is limited mainly by any lower level operating
83 * system constraints, such as the number of bits in #gsize. For
84 * example, it is reasonable to have a 2GB file mapped into memory
85 * with #GMappedFile, and call g_variant_new_from_data() on it.
87 * For convenience to C programmers, #GVariant features powerful
88 * varargs-based value construction and destruction. This feature is
89 * designed to be embedded in other libraries.
91 * There is a Python-inspired text language for describing #GVariant
92 * values. #GVariant includes a printer for this language and a parser
93 * with type inferencing.
97 * #GVariant tries to be quite efficient with respect to memory use.
98 * This section gives a rough idea of how much memory is used by the
99 * current implementation. The information here is subject to change
102 * The memory allocated by #GVariant can be grouped into 4 broad
103 * purposes: memory for serialised data, memory for the type
104 * information cache, buffer management memory and memory for the
105 * #GVariant structure itself.
107 * ## Serialised Data Memory
109 * This is the memory that is used for storing GVariant data in
110 * serialised form. This is what would be sent over the network or
111 * what would end up on disk, not counting any indicator of the
112 * endianness, or of the length or type of the top-level variant.
114 * The amount of memory required to store a boolean is 1 byte. 16,
115 * 32 and 64 bit integers and double precision floating point numbers
116 * use their "natural" size. Strings (including object path and
117 * signature strings) are stored with a nul terminator, and as such
118 * use the length of the string plus 1 byte.
120 * Maybe types use no space at all to represent the null value and
121 * use the same amount of space (sometimes plus one byte) as the
122 * equivalent non-maybe-typed value to represent the non-null case.
124 * Arrays use the amount of space required to store each of their
125 * members, concatenated. Additionally, if the items stored in an
126 * array are not of a fixed-size (ie: strings, other arrays, etc)
127 * then an additional framing offset is stored for each item. The
128 * size of this offset is either 1, 2 or 4 bytes depending on the
129 * overall size of the container. Additionally, extra padding bytes
130 * are added as required for alignment of child values.
132 * Tuples (including dictionary entries) use the amount of space
133 * required to store each of their members, concatenated, plus one
134 * framing offset (as per arrays) for each non-fixed-sized item in
135 * the tuple, except for the last one. Additionally, extra padding
136 * bytes are added as required for alignment of child values.
138 * Variants use the same amount of space as the item inside of the
139 * variant, plus 1 byte, plus the length of the type string for the
140 * item inside the variant.
142 * As an example, consider a dictionary mapping strings to variants.
143 * In the case that the dictionary is empty, 0 bytes are required for
146 * If we add an item "width" that maps to the int32 value of 500 then
147 * we will use 4 byte to store the int32 (so 6 for the variant
148 * containing it) and 6 bytes for the string. The variant must be
149 * aligned to 8 after the 6 bytes of the string, so that's 2 extra
150 * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
151 * for the dictionary entry. An additional 1 byte is added to the
152 * array as a framing offset making a total of 15 bytes.
154 * If we add another entry, "title" that maps to a nullable string
155 * that happens to have a value of null, then we use 0 bytes for the
156 * null value (and 3 bytes for the variant to contain it along with
157 * its type string) plus 6 bytes for the string. Again, we need 2
158 * padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
160 * We now require extra padding between the two items in the array.
161 * After the 14 bytes of the first item, that's 2 bytes required.
162 * We now require 2 framing offsets for an extra two
163 * bytes. 14 + 2 + 11 + 2 = 29 bytes to encode the entire two-item
166 * ## Type Information Cache
168 * For each GVariant type that currently exists in the program a type
169 * information structure is kept in the type information cache. The
170 * type information structure is required for rapid deserialisation.
172 * Continuing with the above example, if a #GVariant exists with the
173 * type "a{sv}" then a type information struct will exist for
174 * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type
175 * will share the same type information. Additionally, all
176 * single-digit types are stored in read-only static memory and do
177 * not contribute to the writable memory footprint of a program using
180 * Aside from the type information structures stored in read-only
181 * memory, there are two forms of type information. One is used for
182 * container types where there is a single element type: arrays and
183 * maybe types. The other is used for container types where there
184 * are multiple element types: tuples and dictionary entries.
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 32-bit systems, the cache entry for "a{sv}" would require 30
189 * bytes of memory (plus malloc overhead).
191 * Tuple type info structures are 6 * sizeof (void *), plus 4 *
192 * sizeof (void *) for each item in the tuple, plus the memory
193 * required to store the type string itself. A 2-item tuple, for
194 * example, would have a type information structure that consumed
195 * writable memory in the size of 14 * sizeof (void *) (plus type
196 * string) This means that on 32-bit systems, the cache entry for
197 * "{sv}" would require 61 bytes of memory (plus malloc overhead).
199 * This means that in total, for our "a{sv}" example, 91 bytes of
200 * type information would be allocated.
202 * The type information cache, additionally, uses a #GHashTable to
203 * store and lookup the cached items and stores a pointer to this
204 * hash table in static storage. The hash table is freed when there
205 * are zero items in the type cache.
207 * Although these sizes may seem large it is important to remember
208 * that a program will probably only have a very small number of
209 * different types of values in it and that only one type information
210 * structure is required for many different values of the same type.
212 * ## Buffer Management Memory
214 * #GVariant uses an internal buffer management structure to deal
215 * with the various different possible sources of serialised data
216 * that it uses. The buffer is responsible for ensuring that the
217 * correct call is made when the data is no longer in use by
218 * #GVariant. This may involve a g_free() or a g_slice_free() or
219 * even g_mapped_file_unref().
221 * One buffer management structure is used for each chunk of
222 * serialised data. The size of the buffer management structure
223 * is 4 * (void *). On 32-bit systems, that's 16 bytes.
225 * ## GVariant structure
227 * The size of a #GVariant structure is 6 * (void *). On 32-bit
228 * systems, that's 24 bytes.
230 * #GVariant structures only exist if they are explicitly created
231 * with API calls. For example, if a #GVariant is constructed out of
232 * serialised data for the example given above (with the dictionary)
233 * then although there are 9 individual values that comprise the
234 * entire dictionary (two keys, two values, two variants containing
235 * the values, two dictionary entries, plus the dictionary itself),
236 * only 1 #GVariant instance exists -- the one referring to the
239 * If calls are made to start accessing the other values then
240 * #GVariant instances will exist for those values only for as long
241 * as they are in use (ie: until you call g_variant_unref()). The
242 * type information is shared. The serialised data and the buffer
243 * management structure for that serialised data is shared by the
248 * To put the entire example together, for our dictionary mapping
249 * strings to variants (with two entries, as given above), we are
250 * using 91 bytes of memory for type information, 29 byes of memory
251 * for the serialised data, 16 bytes for buffer management and 24
252 * bytes for the #GVariant instance, or a total of 160 bytes, plus
253 * malloc overhead. If we were to use g_variant_get_child_value() to
254 * access the two dictionary entries, we would use an additional 48
255 * bytes. If we were to have other dictionaries of the same type, we
256 * would use more memory for the serialised data and buffer
257 * management for those dictionaries, but the type information would
261 /* definition of GVariant structure is in gvariant-core.c */
263 /* this is a g_return_val_if_fail() for making
264 * sure a (GVariant *) has the required type.
266 #define TYPE_CHECK(value, TYPE, val) \
267 if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \
268 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, \
269 "g_variant_is_of_type (" #value \
274 /* Numeric Type Constructor/Getters {{{1 */
276 * g_variant_new_from_trusted:
277 * @type: the #GVariantType
278 * @data: the data to use
279 * @size: the size of @data
281 * Constructs a new trusted #GVariant instance from the provided data.
282 * This is used to implement g_variant_new_* for all the basic types.
284 * Returns: a new floating #GVariant
287 g_variant_new_from_trusted (const GVariantType
*type
,
294 bytes
= g_bytes_new (data
, size
);
295 value
= g_variant_new_from_bytes (type
, bytes
, TRUE
);
296 g_bytes_unref (bytes
);
302 * g_variant_new_boolean:
303 * @value: a #gboolean value
305 * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
307 * Returns: (transfer none): a floating reference to a new boolean #GVariant instance
312 g_variant_new_boolean (gboolean value
)
316 return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN
, &v
, 1);
320 * g_variant_get_boolean:
321 * @value: a boolean #GVariant instance
323 * Returns the boolean value of @value.
325 * It is an error to call this function with a @value of any type
326 * other than %G_VARIANT_TYPE_BOOLEAN.
328 * Returns: %TRUE or %FALSE
333 g_variant_get_boolean (GVariant
*value
)
337 TYPE_CHECK (value
, G_VARIANT_TYPE_BOOLEAN
, FALSE
);
339 data
= g_variant_get_data (value
);
341 return data
!= NULL
? *data
!= 0 : FALSE
;
344 /* the constructors and accessors for byte, int{16,32,64}, handles and
345 * doubles all look pretty much exactly the same, so we reduce
348 #define NUMERIC_TYPE(TYPE, type, ctype) \
349 GVariant *g_variant_new_##type (ctype value) { \
350 return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \
351 &value, sizeof value); \
353 ctype g_variant_get_##type (GVariant *value) { \
355 TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \
356 data = g_variant_get_data (value); \
357 return data != NULL ? *data : 0; \
362 * g_variant_new_byte:
363 * @value: a #guint8 value
365 * Creates a new byte #GVariant instance.
367 * Returns: (transfer none): a floating reference to a new byte #GVariant instance
372 * g_variant_get_byte:
373 * @value: a byte #GVariant instance
375 * Returns the byte value of @value.
377 * It is an error to call this function with a @value of any type
378 * other than %G_VARIANT_TYPE_BYTE.
384 NUMERIC_TYPE (BYTE
, byte
, guchar
)
387 * g_variant_new_int16:
388 * @value: a #gint16 value
390 * Creates a new int16 #GVariant instance.
392 * Returns: (transfer none): a floating reference to a new int16 #GVariant instance
397 * g_variant_get_int16:
398 * @value: a int16 #GVariant instance
400 * Returns the 16-bit signed integer value of @value.
402 * It is an error to call this function with a @value of any type
403 * other than %G_VARIANT_TYPE_INT16.
409 NUMERIC_TYPE (INT16
, int16
, gint16
)
412 * g_variant_new_uint16:
413 * @value: a #guint16 value
415 * Creates a new uint16 #GVariant instance.
417 * Returns: (transfer none): a floating reference to a new uint16 #GVariant instance
422 * g_variant_get_uint16:
423 * @value: a uint16 #GVariant instance
425 * Returns the 16-bit unsigned integer value of @value.
427 * It is an error to call this function with a @value of any type
428 * other than %G_VARIANT_TYPE_UINT16.
430 * Returns: a #guint16
434 NUMERIC_TYPE (UINT16
, uint16
, guint16
)
437 * g_variant_new_int32:
438 * @value: a #gint32 value
440 * Creates a new int32 #GVariant instance.
442 * Returns: (transfer none): a floating reference to a new int32 #GVariant instance
447 * g_variant_get_int32:
448 * @value: a int32 #GVariant instance
450 * Returns the 32-bit signed integer value of @value.
452 * It is an error to call this function with a @value of any type
453 * other than %G_VARIANT_TYPE_INT32.
459 NUMERIC_TYPE (INT32
, int32
, gint32
)
462 * g_variant_new_uint32:
463 * @value: a #guint32 value
465 * Creates a new uint32 #GVariant instance.
467 * Returns: (transfer none): a floating reference to a new uint32 #GVariant instance
472 * g_variant_get_uint32:
473 * @value: a uint32 #GVariant instance
475 * Returns the 32-bit unsigned integer value of @value.
477 * It is an error to call this function with a @value of any type
478 * other than %G_VARIANT_TYPE_UINT32.
480 * Returns: a #guint32
484 NUMERIC_TYPE (UINT32
, uint32
, guint32
)
487 * g_variant_new_int64:
488 * @value: a #gint64 value
490 * Creates a new int64 #GVariant instance.
492 * Returns: (transfer none): a floating reference to a new int64 #GVariant instance
497 * g_variant_get_int64:
498 * @value: a int64 #GVariant instance
500 * Returns the 64-bit signed integer value of @value.
502 * It is an error to call this function with a @value of any type
503 * other than %G_VARIANT_TYPE_INT64.
509 NUMERIC_TYPE (INT64
, int64
, gint64
)
512 * g_variant_new_uint64:
513 * @value: a #guint64 value
515 * Creates a new uint64 #GVariant instance.
517 * Returns: (transfer none): a floating reference to a new uint64 #GVariant instance
522 * g_variant_get_uint64:
523 * @value: a uint64 #GVariant instance
525 * Returns the 64-bit unsigned integer value of @value.
527 * It is an error to call this function with a @value of any type
528 * other than %G_VARIANT_TYPE_UINT64.
530 * Returns: a #guint64
534 NUMERIC_TYPE (UINT64
, uint64
, guint64
)
537 * g_variant_new_handle:
538 * @value: a #gint32 value
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 D-Bus message. If you're not interacting
544 * with D-Bus, you probably don't need them.
546 * Returns: (transfer none): a floating reference to a new handle #GVariant instance
551 * g_variant_get_handle:
552 * @value: a handle #GVariant instance
554 * Returns the 32-bit signed integer value of @value.
556 * It is an error to call this function with a @value of any type other
557 * than %G_VARIANT_TYPE_HANDLE.
559 * By convention, handles are indexes into an array of file descriptors
560 * that are sent alongside a D-Bus message. If you're not interacting
561 * with D-Bus, you probably don't need them.
567 NUMERIC_TYPE (HANDLE
, handle
, gint32
)
570 * g_variant_new_double:
571 * @value: a #gdouble floating point value
573 * Creates a new double #GVariant instance.
575 * Returns: (transfer none): a floating reference to a new double #GVariant instance
580 * g_variant_get_double:
581 * @value: a double #GVariant instance
583 * Returns the double precision floating point value of @value.
585 * It is an error to call this function with a @value of any type
586 * other than %G_VARIANT_TYPE_DOUBLE.
588 * Returns: a #gdouble
592 NUMERIC_TYPE (DOUBLE
, double, gdouble
)
594 /* Container type Constructor / Deconstructors {{{1 */
596 * g_variant_new_maybe:
597 * @child_type: (allow-none): the #GVariantType of the child, or %NULL
598 * @child: (allow-none): the child value, or %NULL
600 * Depending on if @child is %NULL, either wraps @child inside of a
601 * maybe container or creates a Nothing instance for the given @type.
603 * At least one of @child_type and @child must be non-%NULL.
604 * If @child_type is non-%NULL then it must be a definite type.
605 * If they are both non-%NULL then @child_type must be the type
608 * If @child is a floating reference (see g_variant_ref_sink()), the new
609 * instance takes ownership of @child.
611 * Returns: (transfer none): a floating reference to a new #GVariant maybe instance
616 g_variant_new_maybe (const GVariantType
*child_type
,
619 GVariantType
*maybe_type
;
622 g_return_val_if_fail (child_type
== NULL
|| g_variant_type_is_definite
624 g_return_val_if_fail (child_type
!= NULL
|| child
!= NULL
, NULL
);
625 g_return_val_if_fail (child_type
== NULL
|| child
== NULL
||
626 g_variant_is_of_type (child
, child_type
),
629 if (child_type
== NULL
)
630 child_type
= g_variant_get_type (child
);
632 maybe_type
= g_variant_type_new_maybe (child_type
);
639 children
= g_new (GVariant
*, 1);
640 children
[0] = g_variant_ref_sink (child
);
641 trusted
= g_variant_is_trusted (children
[0]);
643 value
= g_variant_new_from_children (maybe_type
, children
, 1, trusted
);
646 value
= g_variant_new_from_children (maybe_type
, NULL
, 0, TRUE
);
648 g_variant_type_free (maybe_type
);
654 * g_variant_get_maybe:
655 * @value: a maybe-typed value
657 * Given a maybe-typed #GVariant instance, extract its value. If the
658 * value is Nothing, then this function returns %NULL.
660 * Returns: (allow-none) (transfer full): the contents of @value, or %NULL
665 g_variant_get_maybe (GVariant
*value
)
667 TYPE_CHECK (value
, G_VARIANT_TYPE_MAYBE
, NULL
);
669 if (g_variant_n_children (value
))
670 return g_variant_get_child_value (value
, 0);
676 * g_variant_new_variant: (constructor)
677 * @value: a #GVariant instance
679 * Boxes @value. The result is a #GVariant instance representing a
680 * variant containing the original value.
682 * If @child is a floating reference (see g_variant_ref_sink()), the new
683 * instance takes ownership of @child.
685 * Returns: (transfer none): a floating reference to a new variant #GVariant instance
690 g_variant_new_variant (GVariant
*value
)
692 g_return_val_if_fail (value
!= NULL
, NULL
);
694 g_variant_ref_sink (value
);
696 return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT
,
697 g_memdup (&value
, sizeof value
),
698 1, g_variant_is_trusted (value
));
702 * g_variant_get_variant:
703 * @value: a variant #GVariant instance
705 * Unboxes @value. The result is the #GVariant instance that was
706 * contained in @value.
708 * Returns: (transfer full): the item contained in the variant
713 g_variant_get_variant (GVariant
*value
)
715 TYPE_CHECK (value
, G_VARIANT_TYPE_VARIANT
, NULL
);
717 return g_variant_get_child_value (value
, 0);
721 * g_variant_new_array:
722 * @child_type: (allow-none): the element type of the new array
723 * @children: (allow-none) (array length=n_children): an array of
724 * #GVariant pointers, the children
725 * @n_children: the length of @children
727 * Creates a new #GVariant array from @children.
729 * @child_type must be non-%NULL if @n_children is zero. Otherwise, the
730 * child type is determined by inspecting the first element of the
731 * @children array. If @child_type is non-%NULL then it must be a
734 * The items of the array are taken from the @children array. No entry
735 * in the @children array may be %NULL.
737 * All items in the array must have the same type, which must be the
738 * same as @child_type, if given.
740 * If the @children are floating references (see g_variant_ref_sink()), the
741 * new instance takes ownership of them as if via g_variant_ref_sink().
743 * Returns: (transfer none): a floating reference to a new #GVariant array
748 g_variant_new_array (const GVariantType
*child_type
,
749 GVariant
* const *children
,
752 GVariantType
*array_type
;
753 GVariant
**my_children
;
758 g_return_val_if_fail (n_children
> 0 || child_type
!= NULL
, NULL
);
759 g_return_val_if_fail (n_children
== 0 || children
!= NULL
, NULL
);
760 g_return_val_if_fail (child_type
== NULL
||
761 g_variant_type_is_definite (child_type
), NULL
);
763 my_children
= g_new (GVariant
*, n_children
);
766 if (child_type
== NULL
)
767 child_type
= g_variant_get_type (children
[0]);
768 array_type
= g_variant_type_new_array (child_type
);
770 for (i
= 0; i
< n_children
; i
++)
772 TYPE_CHECK (children
[i
], child_type
, NULL
);
773 my_children
[i
] = g_variant_ref_sink (children
[i
]);
774 trusted
&= g_variant_is_trusted (children
[i
]);
777 value
= g_variant_new_from_children (array_type
, my_children
,
778 n_children
, trusted
);
779 g_variant_type_free (array_type
);
785 * g_variant_make_tuple_type:
786 * @children: (array length=n_children): an array of GVariant *
787 * @n_children: the length of @children
789 * Return the type of a tuple containing @children as its items.
791 static GVariantType
*
792 g_variant_make_tuple_type (GVariant
* const *children
,
795 const GVariantType
**types
;
799 types
= g_new (const GVariantType
*, n_children
);
801 for (i
= 0; i
< n_children
; i
++)
802 types
[i
] = g_variant_get_type (children
[i
]);
804 type
= g_variant_type_new_tuple (types
, n_children
);
811 * g_variant_new_tuple:
812 * @children: (array length=n_children): the items to make the tuple out of
813 * @n_children: the length of @children
815 * Creates a new tuple #GVariant out of the items in @children. The
816 * type is determined from the types of @children. No entry in the
817 * @children array may be %NULL.
819 * If @n_children is 0 then the unit tuple is constructed.
821 * If the @children are floating references (see g_variant_ref_sink()), the
822 * new instance takes ownership of them as if via g_variant_ref_sink().
824 * Returns: (transfer none): a floating reference to a new #GVariant tuple
829 g_variant_new_tuple (GVariant
* const *children
,
832 GVariantType
*tuple_type
;
833 GVariant
**my_children
;
838 g_return_val_if_fail (n_children
== 0 || children
!= NULL
, NULL
);
840 my_children
= g_new (GVariant
*, n_children
);
843 for (i
= 0; i
< n_children
; i
++)
845 my_children
[i
] = g_variant_ref_sink (children
[i
]);
846 trusted
&= g_variant_is_trusted (children
[i
]);
849 tuple_type
= g_variant_make_tuple_type (children
, n_children
);
850 value
= g_variant_new_from_children (tuple_type
, my_children
,
851 n_children
, trusted
);
852 g_variant_type_free (tuple_type
);
858 * g_variant_make_dict_entry_type:
859 * @key: a #GVariant, the key
860 * @val: a #GVariant, the value
862 * Return the type of a dictionary entry containing @key and @val as its
865 static GVariantType
*
866 g_variant_make_dict_entry_type (GVariant
*key
,
869 return g_variant_type_new_dict_entry (g_variant_get_type (key
),
870 g_variant_get_type (val
));
874 * g_variant_new_dict_entry: (constructor)
875 * @key: a basic #GVariant, the key
876 * @value: a #GVariant, the value
878 * Creates a new dictionary entry #GVariant. @key and @value must be
879 * non-%NULL. @key must be a value of a basic type (ie: not a container).
881 * If the @key or @value are floating references (see g_variant_ref_sink()),
882 * the new instance takes ownership of them as if via g_variant_ref_sink().
884 * Returns: (transfer none): a floating reference to a new dictionary entry #GVariant
889 g_variant_new_dict_entry (GVariant
*key
,
892 GVariantType
*dict_type
;
896 g_return_val_if_fail (key
!= NULL
&& value
!= NULL
, NULL
);
897 g_return_val_if_fail (!g_variant_is_container (key
), NULL
);
899 children
= g_new (GVariant
*, 2);
900 children
[0] = g_variant_ref_sink (key
);
901 children
[1] = g_variant_ref_sink (value
);
902 trusted
= g_variant_is_trusted (key
) && g_variant_is_trusted (value
);
904 dict_type
= g_variant_make_dict_entry_type (key
, value
);
905 value
= g_variant_new_from_children (dict_type
, children
, 2, trusted
);
906 g_variant_type_free (dict_type
);
912 * g_variant_lookup: (skip)
913 * @dictionary: a dictionary #GVariant
914 * @key: the key to lookup in the dictionary
915 * @format_string: a GVariant format string
916 * @...: the arguments to unpack the value into
918 * Looks up a value in a dictionary #GVariant.
920 * This function is a wrapper around g_variant_lookup_value() and
921 * g_variant_get(). In the case that %NULL would have been returned,
922 * this function returns %FALSE. Otherwise, it unpacks the returned
923 * value and returns %TRUE.
925 * @format_string determines the C types that are used for unpacking
926 * the values and also determines if the values are copied or borrowed,
928 * [GVariant format strings][gvariant-format-strings-pointers].
930 * This function is currently implemented with a linear scan. If you
931 * plan to do many lookups then #GVariantDict may be more efficient.
933 * Returns: %TRUE if a value was unpacked
938 g_variant_lookup (GVariant
*dictionary
,
940 const gchar
*format_string
,
947 g_variant_get_data (dictionary
);
949 type
= g_variant_format_string_scan_type (format_string
, NULL
, NULL
);
950 value
= g_variant_lookup_value (dictionary
, key
, type
);
951 g_variant_type_free (type
);
957 va_start (ap
, format_string
);
958 g_variant_get_va (value
, format_string
, NULL
, &ap
);
959 g_variant_unref (value
);
970 * g_variant_lookup_value:
971 * @dictionary: a dictionary #GVariant
972 * @key: the key to lookup in the dictionary
973 * @expected_type: (allow-none): a #GVariantType, or %NULL
975 * Looks up a value in a dictionary #GVariant.
977 * This function works with dictionaries of the type a{s*} (and equally
978 * well with type a{o*}, but we only further discuss the string case
979 * for sake of clarity).
981 * In the event that @dictionary has the type a{sv}, the @expected_type
982 * string specifies what type of value is expected to be inside of the
983 * variant. If the value inside the variant has a different type then
984 * %NULL is returned. In the event that @dictionary has a value type other
985 * than v then @expected_type must directly match the key type and it is
986 * used to unpack the value directly or an error occurs.
988 * In either case, if @key is not found in @dictionary, %NULL is returned.
990 * If the key is found and the value has the correct type, it is
991 * returned. If @expected_type was specified then any non-%NULL return
992 * value will have this type.
994 * This function is currently implemented with a linear scan. If you
995 * plan to do many lookups then #GVariantDict may be more efficient.
997 * Returns: (transfer full): the value of the dictionary key, or %NULL
1002 g_variant_lookup_value (GVariant
*dictionary
,
1004 const GVariantType
*expected_type
)
1010 g_return_val_if_fail (g_variant_is_of_type (dictionary
,
1011 G_VARIANT_TYPE ("a{s*}")) ||
1012 g_variant_is_of_type (dictionary
,
1013 G_VARIANT_TYPE ("a{o*}")),
1016 g_variant_iter_init (&iter
, dictionary
);
1018 while ((entry
= g_variant_iter_next_value (&iter
)))
1020 GVariant
*entry_key
;
1023 entry_key
= g_variant_get_child_value (entry
, 0);
1024 matches
= strcmp (g_variant_get_string (entry_key
, NULL
), key
) == 0;
1025 g_variant_unref (entry_key
);
1030 g_variant_unref (entry
);
1036 value
= g_variant_get_child_value (entry
, 1);
1037 g_variant_unref (entry
);
1039 if (g_variant_is_of_type (value
, G_VARIANT_TYPE_VARIANT
))
1043 tmp
= g_variant_get_variant (value
);
1044 g_variant_unref (value
);
1046 if (expected_type
&& !g_variant_is_of_type (tmp
, expected_type
))
1048 g_variant_unref (tmp
);
1055 g_return_val_if_fail (expected_type
== NULL
|| value
== NULL
||
1056 g_variant_is_of_type (value
, expected_type
), NULL
);
1062 * g_variant_get_fixed_array:
1063 * @value: a #GVariant array with fixed-sized elements
1064 * @n_elements: (out): a pointer to the location to store the number of items
1065 * @element_size: the size of each element
1067 * Provides access to the serialised data for an array of fixed-sized
1070 * @value must be an array with fixed-sized elements. Numeric types are
1071 * fixed-size, as are tuples containing only other fixed-sized types.
1073 * @element_size must be the size of a single element in the array,
1074 * as given by the section on
1075 * [serialized data memory][gvariant-serialised-data-memory].
1077 * In particular, arrays of these fixed-sized types can be interpreted
1078 * as an array of the given C type, with @element_size set to the size
1079 * the appropriate type:
1080 * - %G_VARIANT_TYPE_INT16 (etc.): #gint16 (etc.)
1081 * - %G_VARIANT_TYPE_BOOLEAN: #guchar (not #gboolean!)
1082 * - %G_VARIANT_TYPE_BYTE: #guchar
1083 * - %G_VARIANT_TYPE_HANDLE: #guint32
1084 * - %G_VARIANT_TYPE_DOUBLE: #gdouble
1086 * For example, if calling this function for an array of 32-bit integers,
1087 * you might say sizeof(gint32). This value isn't used except for the purpose
1088 * of a double-check that the form of the serialised data matches the caller's
1091 * @n_elements, which must be non-%NULL is set equal to the number of
1092 * items in the array.
1094 * Returns: (array length=n_elements) (transfer none): a pointer to
1100 g_variant_get_fixed_array (GVariant
*value
,
1104 GVariantTypeInfo
*array_info
;
1105 gsize array_element_size
;
1109 TYPE_CHECK (value
, G_VARIANT_TYPE_ARRAY
, NULL
);
1111 g_return_val_if_fail (n_elements
!= NULL
, NULL
);
1112 g_return_val_if_fail (element_size
> 0, NULL
);
1114 array_info
= g_variant_get_type_info (value
);
1115 g_variant_type_info_query_element (array_info
, NULL
, &array_element_size
);
1117 g_return_val_if_fail (array_element_size
, NULL
);
1119 if G_UNLIKELY (array_element_size
!= element_size
)
1121 if (array_element_size
)
1122 g_critical ("g_variant_get_fixed_array: assertion "
1123 "'g_variant_array_has_fixed_size (value, element_size)' "
1124 "failed: array size %"G_GSIZE_FORMAT
" does not match "
1125 "given element_size %"G_GSIZE_FORMAT
".",
1126 array_element_size
, element_size
);
1128 g_critical ("g_variant_get_fixed_array: assertion "
1129 "'g_variant_array_has_fixed_size (value, element_size)' "
1130 "failed: array does not have fixed size.");
1133 data
= g_variant_get_data (value
);
1134 size
= g_variant_get_size (value
);
1136 if (size
% element_size
)
1139 *n_elements
= size
/ element_size
;
1148 * g_variant_new_fixed_array:
1149 * @element_type: the #GVariantType of each element
1150 * @elements: a pointer to the fixed array of contiguous elements
1151 * @n_elements: the number of elements
1152 * @element_size: the size of each element
1154 * Provides access to the serialised data for an array of fixed-sized
1157 * @value must be an array with fixed-sized elements. Numeric types are
1158 * fixed-size as are tuples containing only other fixed-sized types.
1160 * @element_size must be the size of a single element in the array.
1161 * For example, if calling this function for an array of 32-bit integers,
1162 * you might say sizeof(gint32). This value isn't used except for the purpose
1163 * of a double-check that the form of the serialised data matches the caller's
1166 * @n_elements, which must be non-%NULL is set equal to the number of
1167 * items in the array.
1169 * Returns: (transfer none): a floating reference to a new array #GVariant instance
1174 g_variant_new_fixed_array (const GVariantType
*element_type
,
1175 gconstpointer elements
,
1179 GVariantType
*array_type
;
1180 gsize array_element_size
;
1181 GVariantTypeInfo
*array_info
;
1185 g_return_val_if_fail (g_variant_type_is_definite (element_type
), NULL
);
1186 g_return_val_if_fail (element_size
> 0, NULL
);
1188 array_type
= g_variant_type_new_array (element_type
);
1189 array_info
= g_variant_type_info_get (array_type
);
1190 g_variant_type_info_query_element (array_info
, NULL
, &array_element_size
);
1191 if G_UNLIKELY (array_element_size
!= element_size
)
1193 if (array_element_size
)
1194 g_critical ("g_variant_new_fixed_array: array size %" G_GSIZE_FORMAT
1195 " does not match given element_size %" G_GSIZE_FORMAT
".",
1196 array_element_size
, element_size
);
1198 g_critical ("g_variant_get_fixed_array: array does not have fixed size.");
1202 data
= g_memdup (elements
, n_elements
* element_size
);
1203 value
= g_variant_new_from_data (array_type
, data
,
1204 n_elements
* element_size
,
1205 FALSE
, g_free
, data
);
1207 g_variant_type_free (array_type
);
1208 g_variant_type_info_unref (array_info
);
1213 /* String type constructor/getters/validation {{{1 */
1215 * g_variant_new_string:
1216 * @string: a normal UTF-8 nul-terminated string
1218 * Creates a string #GVariant with the contents of @string.
1220 * @string must be valid UTF-8, and must not be %NULL. To encode
1221 * potentially-%NULL strings, use g_variant_new() with `ms` as the
1222 * [format string][gvariant-format-strings-maybe-types].
1224 * Returns: (transfer none): a floating reference to a new string #GVariant instance
1229 g_variant_new_string (const gchar
*string
)
1231 g_return_val_if_fail (string
!= NULL
, NULL
);
1232 g_return_val_if_fail (g_utf8_validate (string
, -1, NULL
), NULL
);
1234 return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING
,
1235 string
, strlen (string
) + 1);
1239 * g_variant_new_take_string: (skip)
1240 * @string: a normal UTF-8 nul-terminated string
1242 * Creates a string #GVariant with the contents of @string.
1244 * @string must be valid UTF-8, and must not be %NULL. To encode
1245 * potentially-%NULL strings, use this with g_variant_new_maybe().
1247 * This function consumes @string. g_free() will be called on @string
1248 * when it is no longer required.
1250 * You must not modify or access @string in any other way after passing
1251 * it to this function. It is even possible that @string is immediately
1254 * Returns: (transfer none): a floating reference to a new string
1255 * #GVariant instance
1260 g_variant_new_take_string (gchar
*string
)
1265 g_return_val_if_fail (string
!= NULL
, NULL
);
1266 g_return_val_if_fail (g_utf8_validate (string
, -1, NULL
), NULL
);
1268 bytes
= g_bytes_new_take (string
, strlen (string
) + 1);
1269 value
= g_variant_new_from_bytes (G_VARIANT_TYPE_STRING
, bytes
, TRUE
);
1270 g_bytes_unref (bytes
);
1276 * g_variant_new_printf: (skip)
1277 * @format_string: a printf-style format string
1278 * @...: arguments for @format_string
1280 * Creates a string-type GVariant using printf formatting.
1282 * This is similar to calling g_strdup_printf() and then
1283 * g_variant_new_string() but it saves a temporary variable and an
1286 * Returns: (transfer none): a floating reference to a new string
1287 * #GVariant instance
1292 g_variant_new_printf (const gchar
*format_string
,
1300 g_return_val_if_fail (format_string
!= NULL
, NULL
);
1302 va_start (ap
, format_string
);
1303 string
= g_strdup_vprintf (format_string
, ap
);
1306 bytes
= g_bytes_new_take (string
, strlen (string
) + 1);
1307 value
= g_variant_new_from_bytes (G_VARIANT_TYPE_STRING
, bytes
, TRUE
);
1308 g_bytes_unref (bytes
);
1314 * g_variant_new_object_path:
1315 * @object_path: a normal C nul-terminated string
1317 * Creates a D-Bus object path #GVariant with the contents of @string.
1318 * @string must be a valid D-Bus object path. Use
1319 * g_variant_is_object_path() if you're not sure.
1321 * Returns: (transfer none): a floating reference to a new object path #GVariant instance
1326 g_variant_new_object_path (const gchar
*object_path
)
1328 g_return_val_if_fail (g_variant_is_object_path (object_path
), NULL
);
1330 return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH
,
1331 object_path
, strlen (object_path
) + 1);
1335 * g_variant_is_object_path:
1336 * @string: a normal C nul-terminated string
1338 * Determines if a given string is a valid D-Bus object path. You
1339 * should ensure that a string is a valid D-Bus object path before
1340 * passing it to g_variant_new_object_path().
1342 * A valid object path starts with '/' followed by zero or more
1343 * sequences of characters separated by '/' characters. Each sequence
1344 * must contain only the characters "[A-Z][a-z][0-9]_". No sequence
1345 * (including the one following the final '/' character) may be empty.
1347 * Returns: %TRUE if @string is a D-Bus object path
1352 g_variant_is_object_path (const gchar
*string
)
1354 g_return_val_if_fail (string
!= NULL
, FALSE
);
1356 return g_variant_serialiser_is_object_path (string
, strlen (string
) + 1);
1360 * g_variant_new_signature:
1361 * @signature: a normal C nul-terminated string
1363 * Creates a D-Bus type signature #GVariant with the contents of
1364 * @string. @string must be a valid D-Bus type signature. Use
1365 * g_variant_is_signature() if you're not sure.
1367 * Returns: (transfer none): a floating reference to a new signature #GVariant instance
1372 g_variant_new_signature (const gchar
*signature
)
1374 g_return_val_if_fail (g_variant_is_signature (signature
), NULL
);
1376 return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE
,
1377 signature
, strlen (signature
) + 1);
1381 * g_variant_is_signature:
1382 * @string: a normal C nul-terminated string
1384 * Determines if a given string is a valid D-Bus type signature. You
1385 * should ensure that a string is a valid D-Bus type signature before
1386 * passing it to g_variant_new_signature().
1388 * D-Bus type signatures consist of zero or more definite #GVariantType
1389 * strings in sequence.
1391 * Returns: %TRUE if @string is a D-Bus type signature
1396 g_variant_is_signature (const gchar
*string
)
1398 g_return_val_if_fail (string
!= NULL
, FALSE
);
1400 return g_variant_serialiser_is_signature (string
, strlen (string
) + 1);
1404 * g_variant_get_string:
1405 * @value: a string #GVariant instance
1406 * @length: (allow-none) (default 0) (out): a pointer to a #gsize,
1407 * to store the length
1409 * Returns the string value of a #GVariant instance with a string
1410 * type. This includes the types %G_VARIANT_TYPE_STRING,
1411 * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
1413 * The string will always be UTF-8 encoded, and will never be %NULL.
1415 * If @length is non-%NULL then the length of the string (in bytes) is
1416 * returned there. For trusted values, this information is already
1417 * known. For untrusted values, a strlen() will be performed.
1419 * It is an error to call this function with a @value of any type
1420 * other than those three.
1422 * The return value remains valid as long as @value exists.
1424 * Returns: (transfer none): the constant string, UTF-8 encoded
1429 g_variant_get_string (GVariant
*value
,
1435 g_return_val_if_fail (value
!= NULL
, NULL
);
1436 g_return_val_if_fail (
1437 g_variant_is_of_type (value
, G_VARIANT_TYPE_STRING
) ||
1438 g_variant_is_of_type (value
, G_VARIANT_TYPE_OBJECT_PATH
) ||
1439 g_variant_is_of_type (value
, G_VARIANT_TYPE_SIGNATURE
), NULL
);
1441 data
= g_variant_get_data (value
);
1442 size
= g_variant_get_size (value
);
1444 if (!g_variant_is_trusted (value
))
1446 switch (g_variant_classify (value
))
1448 case G_VARIANT_CLASS_STRING
:
1449 if (g_variant_serialiser_is_string (data
, size
))
1456 case G_VARIANT_CLASS_OBJECT_PATH
:
1457 if (g_variant_serialiser_is_object_path (data
, size
))
1464 case G_VARIANT_CLASS_SIGNATURE
:
1465 if (g_variant_serialiser_is_signature (data
, size
))
1473 g_assert_not_reached ();
1484 * g_variant_dup_string:
1485 * @value: a string #GVariant instance
1486 * @length: (out): a pointer to a #gsize, to store the length
1488 * Similar to g_variant_get_string() except that instead of returning
1489 * a constant string, the string is duplicated.
1491 * The string will always be UTF-8 encoded.
1493 * The return value must be freed using g_free().
1495 * Returns: (transfer full): a newly allocated string, UTF-8 encoded
1500 g_variant_dup_string (GVariant
*value
,
1503 return g_strdup (g_variant_get_string (value
, length
));
1507 * g_variant_new_strv:
1508 * @strv: (array length=length) (element-type utf8): an array of strings
1509 * @length: the length of @strv, or -1
1511 * Constructs an array of strings #GVariant from the given array of
1514 * If @length is -1 then @strv is %NULL-terminated.
1516 * Returns: (transfer none): a new floating #GVariant instance
1521 g_variant_new_strv (const gchar
* const *strv
,
1527 g_return_val_if_fail (length
== 0 || strv
!= NULL
, NULL
);
1530 length
= g_strv_length ((gchar
**) strv
);
1532 strings
= g_new (GVariant
*, length
);
1533 for (i
= 0; i
< length
; i
++)
1534 strings
[i
] = g_variant_ref_sink (g_variant_new_string (strv
[i
]));
1536 return g_variant_new_from_children (G_VARIANT_TYPE_STRING_ARRAY
,
1537 strings
, length
, TRUE
);
1541 * g_variant_get_strv:
1542 * @value: an array of strings #GVariant
1543 * @length: (out) (allow-none): the length of the result, or %NULL
1545 * Gets the contents of an array of strings #GVariant. This call
1546 * makes a shallow copy; the return result should be released with
1547 * g_free(), but the individual strings must not be modified.
1549 * If @length is non-%NULL then the number of elements in the result
1550 * is stored there. In any case, the resulting array will be
1553 * For an empty array, @length will be set to 0 and a pointer to a
1554 * %NULL pointer will be returned.
1556 * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings
1561 g_variant_get_strv (GVariant
*value
,
1568 TYPE_CHECK (value
, G_VARIANT_TYPE_STRING_ARRAY
, NULL
);
1570 g_variant_get_data (value
);
1571 n
= g_variant_n_children (value
);
1572 strv
= g_new (const gchar
*, n
+ 1);
1574 for (i
= 0; i
< n
; i
++)
1578 string
= g_variant_get_child_value (value
, i
);
1579 strv
[i
] = g_variant_get_string (string
, NULL
);
1580 g_variant_unref (string
);
1591 * g_variant_dup_strv:
1592 * @value: an array of strings #GVariant
1593 * @length: (out) (allow-none): the length of the result, or %NULL
1595 * Gets the contents of an array of strings #GVariant. This call
1596 * makes a deep copy; the return result should be released with
1599 * If @length is non-%NULL then the number of elements in the result
1600 * is stored there. In any case, the resulting array will be
1603 * For an empty array, @length will be set to 0 and a pointer to a
1604 * %NULL pointer will be returned.
1606 * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1611 g_variant_dup_strv (GVariant
*value
,
1618 TYPE_CHECK (value
, G_VARIANT_TYPE_STRING_ARRAY
, NULL
);
1620 n
= g_variant_n_children (value
);
1621 strv
= g_new (gchar
*, n
+ 1);
1623 for (i
= 0; i
< n
; i
++)
1627 string
= g_variant_get_child_value (value
, i
);
1628 strv
[i
] = g_variant_dup_string (string
, NULL
);
1629 g_variant_unref (string
);
1640 * g_variant_new_objv:
1641 * @strv: (array length=length) (element-type utf8): an array of strings
1642 * @length: the length of @strv, or -1
1644 * Constructs an array of object paths #GVariant from the given array of
1647 * Each string must be a valid #GVariant object path; see
1648 * g_variant_is_object_path().
1650 * If @length is -1 then @strv is %NULL-terminated.
1652 * Returns: (transfer none): a new floating #GVariant instance
1657 g_variant_new_objv (const gchar
* const *strv
,
1663 g_return_val_if_fail (length
== 0 || strv
!= NULL
, NULL
);
1666 length
= g_strv_length ((gchar
**) strv
);
1668 strings
= g_new (GVariant
*, length
);
1669 for (i
= 0; i
< length
; i
++)
1670 strings
[i
] = g_variant_ref_sink (g_variant_new_object_path (strv
[i
]));
1672 return g_variant_new_from_children (G_VARIANT_TYPE_OBJECT_PATH_ARRAY
,
1673 strings
, length
, TRUE
);
1677 * g_variant_get_objv:
1678 * @value: an array of object paths #GVariant
1679 * @length: (out) (allow-none): the length of the result, or %NULL
1681 * Gets the contents of an array of object paths #GVariant. This call
1682 * makes a shallow copy; the return result should be released with
1683 * g_free(), but the individual strings must not be modified.
1685 * If @length is non-%NULL then the number of elements in the result
1686 * is stored there. In any case, the resulting array will be
1689 * For an empty array, @length will be set to 0 and a pointer to a
1690 * %NULL pointer will be returned.
1692 * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings
1697 g_variant_get_objv (GVariant
*value
,
1704 TYPE_CHECK (value
, G_VARIANT_TYPE_OBJECT_PATH_ARRAY
, NULL
);
1706 g_variant_get_data (value
);
1707 n
= g_variant_n_children (value
);
1708 strv
= g_new (const gchar
*, n
+ 1);
1710 for (i
= 0; i
< n
; i
++)
1714 string
= g_variant_get_child_value (value
, i
);
1715 strv
[i
] = g_variant_get_string (string
, NULL
);
1716 g_variant_unref (string
);
1727 * g_variant_dup_objv:
1728 * @value: an array of object paths #GVariant
1729 * @length: (out) (allow-none): the length of the result, or %NULL
1731 * Gets the contents of an array of object paths #GVariant. This call
1732 * makes a deep copy; the return result should be released with
1735 * If @length is non-%NULL then the number of elements in the result
1736 * is stored there. In any case, the resulting array will be
1739 * For an empty array, @length will be set to 0 and a pointer to a
1740 * %NULL pointer will be returned.
1742 * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1747 g_variant_dup_objv (GVariant
*value
,
1754 TYPE_CHECK (value
, G_VARIANT_TYPE_OBJECT_PATH_ARRAY
, NULL
);
1756 n
= g_variant_n_children (value
);
1757 strv
= g_new (gchar
*, n
+ 1);
1759 for (i
= 0; i
< n
; i
++)
1763 string
= g_variant_get_child_value (value
, i
);
1764 strv
[i
] = g_variant_dup_string (string
, NULL
);
1765 g_variant_unref (string
);
1777 * g_variant_new_bytestring:
1778 * @string: (array zero-terminated=1) (element-type guint8): a normal
1779 * nul-terminated string in no particular encoding
1781 * Creates an array-of-bytes #GVariant with the contents of @string.
1782 * This function is just like g_variant_new_string() except that the
1783 * string need not be valid UTF-8.
1785 * The nul terminator character at the end of the string is stored in
1788 * Returns: (transfer none): a floating reference to a new bytestring #GVariant instance
1793 g_variant_new_bytestring (const gchar
*string
)
1795 g_return_val_if_fail (string
!= NULL
, NULL
);
1797 return g_variant_new_from_trusted (G_VARIANT_TYPE_BYTESTRING
,
1798 string
, strlen (string
) + 1);
1802 * g_variant_get_bytestring:
1803 * @value: an array-of-bytes #GVariant instance
1805 * Returns the string value of a #GVariant instance with an
1806 * array-of-bytes type. The string has no particular encoding.
1808 * If the array does not end with a nul terminator character, the empty
1809 * string is returned. For this reason, you can always trust that a
1810 * non-%NULL nul-terminated string will be returned by this function.
1812 * If the array contains a nul terminator character somewhere other than
1813 * the last byte then the returned string is the string, up to the first
1814 * such nul character.
1816 * It is an error to call this function with a @value that is not an
1819 * The return value remains valid as long as @value exists.
1821 * Returns: (transfer none) (array zero-terminated=1) (element-type guint8):
1822 * the constant string
1827 g_variant_get_bytestring (GVariant
*value
)
1829 const gchar
*string
;
1832 TYPE_CHECK (value
, G_VARIANT_TYPE_BYTESTRING
, NULL
);
1834 /* Won't be NULL since this is an array type */
1835 string
= g_variant_get_data (value
);
1836 size
= g_variant_get_size (value
);
1838 if (size
&& string
[size
- 1] == '\0')
1845 * g_variant_dup_bytestring:
1846 * @value: an array-of-bytes #GVariant instance
1847 * @length: (out) (allow-none) (default NULL): a pointer to a #gsize, to store
1848 * the length (not including the nul terminator)
1850 * Similar to g_variant_get_bytestring() except that instead of
1851 * returning a constant string, the string is duplicated.
1853 * The return value must be freed using g_free().
1855 * Returns: (transfer full) (array zero-terminated=1 length=length) (element-type guint8):
1856 * a newly allocated string
1861 g_variant_dup_bytestring (GVariant
*value
,
1864 const gchar
*original
= g_variant_get_bytestring (value
);
1867 /* don't crash in case get_bytestring() had an assert failure */
1868 if (original
== NULL
)
1871 size
= strlen (original
);
1876 return g_memdup (original
, size
+ 1);
1880 * g_variant_new_bytestring_array:
1881 * @strv: (array length=length): an array of strings
1882 * @length: the length of @strv, or -1
1884 * Constructs an array of bytestring #GVariant from the given array of
1887 * If @length is -1 then @strv is %NULL-terminated.
1889 * Returns: (transfer none): a new floating #GVariant instance
1894 g_variant_new_bytestring_array (const gchar
* const *strv
,
1900 g_return_val_if_fail (length
== 0 || strv
!= NULL
, NULL
);
1903 length
= g_strv_length ((gchar
**) strv
);
1905 strings
= g_new (GVariant
*, length
);
1906 for (i
= 0; i
< length
; i
++)
1907 strings
[i
] = g_variant_ref_sink (g_variant_new_bytestring (strv
[i
]));
1909 return g_variant_new_from_children (G_VARIANT_TYPE_BYTESTRING_ARRAY
,
1910 strings
, length
, TRUE
);
1914 * g_variant_get_bytestring_array:
1915 * @value: an array of array of bytes #GVariant ('aay')
1916 * @length: (out) (allow-none): the length of the result, or %NULL
1918 * Gets the contents of an array of array of bytes #GVariant. This call
1919 * makes a shallow copy; the return result should be released with
1920 * g_free(), but the individual strings must not be modified.
1922 * If @length is non-%NULL then the number of elements in the result is
1923 * stored there. In any case, the resulting array will be
1926 * For an empty array, @length will be set to 0 and a pointer to a
1927 * %NULL pointer will be returned.
1929 * Returns: (array length=length) (transfer container): an array of constant strings
1934 g_variant_get_bytestring_array (GVariant
*value
,
1941 TYPE_CHECK (value
, G_VARIANT_TYPE_BYTESTRING_ARRAY
, NULL
);
1943 g_variant_get_data (value
);
1944 n
= g_variant_n_children (value
);
1945 strv
= g_new (const gchar
*, n
+ 1);
1947 for (i
= 0; i
< n
; i
++)
1951 string
= g_variant_get_child_value (value
, i
);
1952 strv
[i
] = g_variant_get_bytestring (string
);
1953 g_variant_unref (string
);
1964 * g_variant_dup_bytestring_array:
1965 * @value: an array of array of bytes #GVariant ('aay')
1966 * @length: (out) (allow-none): the length of the result, or %NULL
1968 * Gets the contents of an array of array of bytes #GVariant. This call
1969 * makes a deep copy; the return result should be released with
1972 * If @length is non-%NULL then the number of elements in the result is
1973 * stored there. In any case, the resulting array will be
1976 * For an empty array, @length will be set to 0 and a pointer to a
1977 * %NULL pointer will be returned.
1979 * Returns: (array length=length) (transfer full): an array of strings
1984 g_variant_dup_bytestring_array (GVariant
*value
,
1991 TYPE_CHECK (value
, G_VARIANT_TYPE_BYTESTRING_ARRAY
, NULL
);
1993 g_variant_get_data (value
);
1994 n
= g_variant_n_children (value
);
1995 strv
= g_new (gchar
*, n
+ 1);
1997 for (i
= 0; i
< n
; i
++)
2001 string
= g_variant_get_child_value (value
, i
);
2002 strv
[i
] = g_variant_dup_bytestring (string
, NULL
);
2003 g_variant_unref (string
);
2013 /* Type checking and querying {{{1 */
2015 * g_variant_get_type:
2016 * @value: a #GVariant
2018 * Determines the type of @value.
2020 * The return value is valid for the lifetime of @value and must not
2023 * Returns: a #GVariantType
2027 const GVariantType
*
2028 g_variant_get_type (GVariant
*value
)
2030 GVariantTypeInfo
*type_info
;
2032 g_return_val_if_fail (value
!= NULL
, NULL
);
2034 type_info
= g_variant_get_type_info (value
);
2036 return (GVariantType
*) g_variant_type_info_get_type_string (type_info
);
2040 * g_variant_get_type_string:
2041 * @value: a #GVariant
2043 * Returns the type string of @value. Unlike the result of calling
2044 * g_variant_type_peek_string(), this string is nul-terminated. This
2045 * string belongs to #GVariant and must not be freed.
2047 * Returns: the type string for the type of @value
2052 g_variant_get_type_string (GVariant
*value
)
2054 GVariantTypeInfo
*type_info
;
2056 g_return_val_if_fail (value
!= NULL
, NULL
);
2058 type_info
= g_variant_get_type_info (value
);
2060 return g_variant_type_info_get_type_string (type_info
);
2064 * g_variant_is_of_type:
2065 * @value: a #GVariant instance
2066 * @type: a #GVariantType
2068 * Checks if a value has a type matching the provided type.
2070 * Returns: %TRUE if the type of @value matches @type
2075 g_variant_is_of_type (GVariant
*value
,
2076 const GVariantType
*type
)
2078 return g_variant_type_is_subtype_of (g_variant_get_type (value
), type
);
2082 * g_variant_is_container:
2083 * @value: a #GVariant instance
2085 * Checks if @value is a container.
2087 * Returns: %TRUE if @value is a container
2092 g_variant_is_container (GVariant
*value
)
2094 return g_variant_type_is_container (g_variant_get_type (value
));
2099 * g_variant_classify:
2100 * @value: a #GVariant
2102 * Classifies @value according to its top-level type.
2104 * Returns: the #GVariantClass of @value
2110 * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
2111 * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
2112 * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
2113 * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
2114 * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
2115 * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
2116 * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
2117 * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
2118 * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
2119 * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating
2121 * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
2122 * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a D-Bus object path
2124 * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a D-Bus signature string.
2125 * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
2126 * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
2127 * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
2128 * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
2129 * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
2131 * The range of possible top-level types of #GVariant instances.
2136 g_variant_classify (GVariant
*value
)
2138 g_return_val_if_fail (value
!= NULL
, 0);
2140 return *g_variant_get_type_string (value
);
2143 /* Pretty printer {{{1 */
2144 /* This function is not introspectable because if @string is NULL,
2145 @returns is (transfer full), otherwise it is (transfer none), which
2146 is not supported by GObjectIntrospection */
2148 * g_variant_print_string: (skip)
2149 * @value: a #GVariant
2150 * @string: (allow-none) (default NULL): a #GString, or %NULL
2151 * @type_annotate: %TRUE if type information should be included in
2154 * Behaves as g_variant_print(), but operates on a #GString.
2156 * If @string is non-%NULL then it is appended to and returned. Else,
2157 * a new empty #GString is allocated and it is returned.
2159 * Returns: a #GString containing the string
2164 g_variant_print_string (GVariant
*value
,
2166 gboolean type_annotate
)
2168 if G_UNLIKELY (string
== NULL
)
2169 string
= g_string_new (NULL
);
2171 switch (g_variant_classify (value
))
2173 case G_VARIANT_CLASS_MAYBE
:
2175 g_string_append_printf (string
, "@%s ",
2176 g_variant_get_type_string (value
));
2178 if (g_variant_n_children (value
))
2180 gchar
*printed_child
;
2185 * Consider the case of the type "mmi". In this case we could
2186 * write "just just 4", but "4" alone is totally unambiguous,
2187 * so we try to drop "just" where possible.
2189 * We have to be careful not to always drop "just", though,
2190 * since "nothing" needs to be distinguishable from "just
2191 * nothing". The case where we need to ensure we keep the
2192 * "just" is actually exactly the case where we have a nested
2195 * Instead of searching for that nested Nothing, we just print
2196 * the contained value into a separate string and see if we
2197 * end up with "nothing" at the end of it. If so, we need to
2198 * add "just" at our level.
2200 element
= g_variant_get_child_value (value
, 0);
2201 printed_child
= g_variant_print (element
, FALSE
);
2202 g_variant_unref (element
);
2204 if (g_str_has_suffix (printed_child
, "nothing"))
2205 g_string_append (string
, "just ");
2206 g_string_append (string
, printed_child
);
2207 g_free (printed_child
);
2210 g_string_append (string
, "nothing");
2214 case G_VARIANT_CLASS_ARRAY
:
2215 /* it's an array so the first character of the type string is 'a'
2217 * if the first two characters are 'ay' then it's a bytestring.
2218 * under certain conditions we print those as strings.
2220 if (g_variant_get_type_string (value
)[1] == 'y')
2226 /* first determine if it is a byte string.
2227 * that's when there's a single nul character: at the end.
2229 str
= g_variant_get_data (value
);
2230 size
= g_variant_get_size (value
);
2232 for (i
= 0; i
< size
; i
++)
2236 /* first nul byte is the last byte -> it's a byte string. */
2239 gchar
*escaped
= g_strescape (str
, NULL
);
2241 /* use double quotes only if a ' is in the string */
2242 if (strchr (str
, '\''))
2243 g_string_append_printf (string
, "b\"%s\"", escaped
);
2245 g_string_append_printf (string
, "b'%s'", escaped
);
2252 /* fall through and handle normally... */;
2256 * if the first two characters are 'a{' then it's an array of
2257 * dictionary entries (ie: a dictionary) so we print that
2260 if (g_variant_get_type_string (value
)[1] == '{')
2263 const gchar
*comma
= "";
2266 if ((n
= g_variant_n_children (value
)) == 0)
2269 g_string_append_printf (string
, "@%s ",
2270 g_variant_get_type_string (value
));
2271 g_string_append (string
, "{}");
2275 g_string_append_c (string
, '{');
2276 for (i
= 0; i
< n
; i
++)
2278 GVariant
*entry
, *key
, *val
;
2280 g_string_append (string
, comma
);
2283 entry
= g_variant_get_child_value (value
, i
);
2284 key
= g_variant_get_child_value (entry
, 0);
2285 val
= g_variant_get_child_value (entry
, 1);
2286 g_variant_unref (entry
);
2288 g_variant_print_string (key
, string
, type_annotate
);
2289 g_variant_unref (key
);
2290 g_string_append (string
, ": ");
2291 g_variant_print_string (val
, string
, type_annotate
);
2292 g_variant_unref (val
);
2293 type_annotate
= FALSE
;
2295 g_string_append_c (string
, '}');
2298 /* normal (non-dictionary) array */
2300 const gchar
*comma
= "";
2303 if ((n
= g_variant_n_children (value
)) == 0)
2306 g_string_append_printf (string
, "@%s ",
2307 g_variant_get_type_string (value
));
2308 g_string_append (string
, "[]");
2312 g_string_append_c (string
, '[');
2313 for (i
= 0; i
< n
; i
++)
2317 g_string_append (string
, comma
);
2320 element
= g_variant_get_child_value (value
, i
);
2322 g_variant_print_string (element
, string
, type_annotate
);
2323 g_variant_unref (element
);
2324 type_annotate
= FALSE
;
2326 g_string_append_c (string
, ']');
2331 case G_VARIANT_CLASS_TUPLE
:
2335 n
= g_variant_n_children (value
);
2337 g_string_append_c (string
, '(');
2338 for (i
= 0; i
< n
; i
++)
2342 element
= g_variant_get_child_value (value
, i
);
2343 g_variant_print_string (element
, string
, type_annotate
);
2344 g_string_append (string
, ", ");
2345 g_variant_unref (element
);
2348 /* for >1 item: remove final ", "
2349 * for 1 item: remove final " ", but leave the ","
2350 * for 0 items: there is only "(", so remove nothing
2352 g_string_truncate (string
, string
->len
- (n
> 0) - (n
> 1));
2353 g_string_append_c (string
, ')');
2357 case G_VARIANT_CLASS_DICT_ENTRY
:
2361 g_string_append_c (string
, '{');
2363 element
= g_variant_get_child_value (value
, 0);
2364 g_variant_print_string (element
, string
, type_annotate
);
2365 g_variant_unref (element
);
2367 g_string_append (string
, ", ");
2369 element
= g_variant_get_child_value (value
, 1);
2370 g_variant_print_string (element
, string
, type_annotate
);
2371 g_variant_unref (element
);
2373 g_string_append_c (string
, '}');
2377 case G_VARIANT_CLASS_VARIANT
:
2379 GVariant
*child
= g_variant_get_variant (value
);
2381 /* Always annotate types in nested variants, because they are
2382 * (by nature) of variable type.
2384 g_string_append_c (string
, '<');
2385 g_variant_print_string (child
, string
, TRUE
);
2386 g_string_append_c (string
, '>');
2388 g_variant_unref (child
);
2392 case G_VARIANT_CLASS_BOOLEAN
:
2393 if (g_variant_get_boolean (value
))
2394 g_string_append (string
, "true");
2396 g_string_append (string
, "false");
2399 case G_VARIANT_CLASS_STRING
:
2401 const gchar
*str
= g_variant_get_string (value
, NULL
);
2402 gunichar quote
= strchr (str
, '\'') ? '"' : '\'';
2404 g_string_append_c (string
, quote
);
2408 gunichar c
= g_utf8_get_char (str
);
2410 if (c
== quote
|| c
== '\\')
2411 g_string_append_c (string
, '\\');
2413 if (g_unichar_isprint (c
))
2414 g_string_append_unichar (string
, c
);
2418 g_string_append_c (string
, '\\');
2423 g_string_append_c (string
, 'a');
2427 g_string_append_c (string
, 'b');
2431 g_string_append_c (string
, 'f');
2435 g_string_append_c (string
, 'n');
2439 g_string_append_c (string
, 'r');
2443 g_string_append_c (string
, 't');
2447 g_string_append_c (string
, 'v');
2451 g_string_append_printf (string
, "u%04x", c
);
2455 g_string_append_printf (string
, "U%08x", c
);
2458 str
= g_utf8_next_char (str
);
2461 g_string_append_c (string
, quote
);
2465 case G_VARIANT_CLASS_BYTE
:
2467 g_string_append (string
, "byte ");
2468 g_string_append_printf (string
, "0x%02x",
2469 g_variant_get_byte (value
));
2472 case G_VARIANT_CLASS_INT16
:
2474 g_string_append (string
, "int16 ");
2475 g_string_append_printf (string
, "%"G_GINT16_FORMAT
,
2476 g_variant_get_int16 (value
));
2479 case G_VARIANT_CLASS_UINT16
:
2481 g_string_append (string
, "uint16 ");
2482 g_string_append_printf (string
, "%"G_GUINT16_FORMAT
,
2483 g_variant_get_uint16 (value
));
2486 case G_VARIANT_CLASS_INT32
:
2487 /* Never annotate this type because it is the default for numbers
2488 * (and this is a *pretty* printer)
2490 g_string_append_printf (string
, "%"G_GINT32_FORMAT
,
2491 g_variant_get_int32 (value
));
2494 case G_VARIANT_CLASS_HANDLE
:
2496 g_string_append (string
, "handle ");
2497 g_string_append_printf (string
, "%"G_GINT32_FORMAT
,
2498 g_variant_get_handle (value
));
2501 case G_VARIANT_CLASS_UINT32
:
2503 g_string_append (string
, "uint32 ");
2504 g_string_append_printf (string
, "%"G_GUINT32_FORMAT
,
2505 g_variant_get_uint32 (value
));
2508 case G_VARIANT_CLASS_INT64
:
2510 g_string_append (string
, "int64 ");
2511 g_string_append_printf (string
, "%"G_GINT64_FORMAT
,
2512 g_variant_get_int64 (value
));
2515 case G_VARIANT_CLASS_UINT64
:
2517 g_string_append (string
, "uint64 ");
2518 g_string_append_printf (string
, "%"G_GUINT64_FORMAT
,
2519 g_variant_get_uint64 (value
));
2522 case G_VARIANT_CLASS_DOUBLE
:
2527 g_ascii_dtostr (buffer
, sizeof buffer
, g_variant_get_double (value
));
2529 for (i
= 0; buffer
[i
]; i
++)
2530 if (buffer
[i
] == '.' || buffer
[i
] == 'e' ||
2531 buffer
[i
] == 'n' || buffer
[i
] == 'N')
2534 /* if there is no '.' or 'e' in the float then add one */
2535 if (buffer
[i
] == '\0')
2542 g_string_append (string
, buffer
);
2546 case G_VARIANT_CLASS_OBJECT_PATH
:
2548 g_string_append (string
, "objectpath ");
2549 g_string_append_printf (string
, "\'%s\'",
2550 g_variant_get_string (value
, NULL
));
2553 case G_VARIANT_CLASS_SIGNATURE
:
2555 g_string_append (string
, "signature ");
2556 g_string_append_printf (string
, "\'%s\'",
2557 g_variant_get_string (value
, NULL
));
2561 g_assert_not_reached ();
2569 * @value: a #GVariant
2570 * @type_annotate: %TRUE if type information should be included in
2573 * Pretty-prints @value in the format understood by g_variant_parse().
2575 * The format is described [here][gvariant-text].
2577 * If @type_annotate is %TRUE, then type information is included in
2580 * Returns: (transfer full): a newly-allocated string holding the result.
2585 g_variant_print (GVariant
*value
,
2586 gboolean type_annotate
)
2588 return g_string_free (g_variant_print_string (value
, NULL
, type_annotate
),
2592 /* Hash, Equal, Compare {{{1 */
2595 * @value: (type GVariant): a basic #GVariant value as a #gconstpointer
2597 * Generates a hash value for a #GVariant instance.
2599 * The output of this function is guaranteed to be the same for a given
2600 * value only per-process. It may change between different processor
2601 * architectures or even different versions of GLib. Do not use this
2602 * function as a basis for building protocols or file formats.
2604 * The type of @value is #gconstpointer only to allow use of this
2605 * function with #GHashTable. @value must be a #GVariant.
2607 * Returns: a hash value corresponding to @value
2612 g_variant_hash (gconstpointer value_
)
2614 GVariant
*value
= (GVariant
*) value_
;
2616 switch (g_variant_classify (value
))
2618 case G_VARIANT_CLASS_STRING
:
2619 case G_VARIANT_CLASS_OBJECT_PATH
:
2620 case G_VARIANT_CLASS_SIGNATURE
:
2621 return g_str_hash (g_variant_get_string (value
, NULL
));
2623 case G_VARIANT_CLASS_BOOLEAN
:
2624 /* this is a very odd thing to hash... */
2625 return g_variant_get_boolean (value
);
2627 case G_VARIANT_CLASS_BYTE
:
2628 return g_variant_get_byte (value
);
2630 case G_VARIANT_CLASS_INT16
:
2631 case G_VARIANT_CLASS_UINT16
:
2635 ptr
= g_variant_get_data (value
);
2643 case G_VARIANT_CLASS_INT32
:
2644 case G_VARIANT_CLASS_UINT32
:
2645 case G_VARIANT_CLASS_HANDLE
:
2649 ptr
= g_variant_get_data (value
);
2657 case G_VARIANT_CLASS_INT64
:
2658 case G_VARIANT_CLASS_UINT64
:
2659 case G_VARIANT_CLASS_DOUBLE
:
2660 /* need a separate case for these guys because otherwise
2661 * performance could be quite bad on big endian systems
2666 ptr
= g_variant_get_data (value
);
2669 return ptr
[0] + ptr
[1];
2675 g_return_val_if_fail (!g_variant_is_container (value
), 0);
2676 g_assert_not_reached ();
2682 * @one: (type GVariant): a #GVariant instance
2683 * @two: (type GVariant): a #GVariant instance
2685 * Checks if @one and @two have the same type and value.
2687 * The types of @one and @two are #gconstpointer only to allow use of
2688 * this function with #GHashTable. They must each be a #GVariant.
2690 * Returns: %TRUE if @one and @two are equal
2695 g_variant_equal (gconstpointer one
,
2700 g_return_val_if_fail (one
!= NULL
&& two
!= NULL
, FALSE
);
2702 if (g_variant_get_type_info ((GVariant
*) one
) !=
2703 g_variant_get_type_info ((GVariant
*) two
))
2706 /* if both values are trusted to be in their canonical serialised form
2707 * then a simple memcmp() of their serialised data will answer the
2710 * if not, then this might generate a false negative (since it is
2711 * possible for two different byte sequences to represent the same
2712 * value). for now we solve this by pretty-printing both values and
2713 * comparing the result.
2715 if (g_variant_is_trusted ((GVariant
*) one
) &&
2716 g_variant_is_trusted ((GVariant
*) two
))
2718 gconstpointer data_one
, data_two
;
2719 gsize size_one
, size_two
;
2721 size_one
= g_variant_get_size ((GVariant
*) one
);
2722 size_two
= g_variant_get_size ((GVariant
*) two
);
2724 if (size_one
!= size_two
)
2727 data_one
= g_variant_get_data ((GVariant
*) one
);
2728 data_two
= g_variant_get_data ((GVariant
*) two
);
2730 equal
= memcmp (data_one
, data_two
, size_one
) == 0;
2734 gchar
*strone
, *strtwo
;
2736 strone
= g_variant_print ((GVariant
*) one
, FALSE
);
2737 strtwo
= g_variant_print ((GVariant
*) two
, FALSE
);
2738 equal
= strcmp (strone
, strtwo
) == 0;
2747 * g_variant_compare:
2748 * @one: (type GVariant): a basic-typed #GVariant instance
2749 * @two: (type GVariant): a #GVariant instance of the same type
2751 * Compares @one and @two.
2753 * The types of @one and @two are #gconstpointer only to allow use of
2754 * this function with #GTree, #GPtrArray, etc. They must each be a
2757 * Comparison is only defined for basic types (ie: booleans, numbers,
2758 * strings). For booleans, %FALSE is less than %TRUE. Numbers are
2759 * ordered in the usual way. Strings are in ASCII lexographical order.
2761 * It is a programmer error to attempt to compare container values or
2762 * two values that have types that are not exactly equal. For example,
2763 * you cannot compare a 32-bit signed integer with a 32-bit unsigned
2764 * integer. Also note that this function is not particularly
2765 * well-behaved when it comes to comparison of doubles; in particular,
2766 * the handling of incomparable values (ie: NaN) is undefined.
2768 * If you only require an equality comparison, g_variant_equal() is more
2771 * Returns: negative value if a < b;
2773 * positive value if a > b.
2778 g_variant_compare (gconstpointer one
,
2781 GVariant
*a
= (GVariant
*) one
;
2782 GVariant
*b
= (GVariant
*) two
;
2784 g_return_val_if_fail (g_variant_classify (a
) == g_variant_classify (b
), 0);
2786 switch (g_variant_classify (a
))
2788 case G_VARIANT_CLASS_BOOLEAN
:
2789 return g_variant_get_boolean (a
) -
2790 g_variant_get_boolean (b
);
2792 case G_VARIANT_CLASS_BYTE
:
2793 return ((gint
) g_variant_get_byte (a
)) -
2794 ((gint
) g_variant_get_byte (b
));
2796 case G_VARIANT_CLASS_INT16
:
2797 return ((gint
) g_variant_get_int16 (a
)) -
2798 ((gint
) g_variant_get_int16 (b
));
2800 case G_VARIANT_CLASS_UINT16
:
2801 return ((gint
) g_variant_get_uint16 (a
)) -
2802 ((gint
) g_variant_get_uint16 (b
));
2804 case G_VARIANT_CLASS_INT32
:
2806 gint32 a_val
= g_variant_get_int32 (a
);
2807 gint32 b_val
= g_variant_get_int32 (b
);
2809 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2812 case G_VARIANT_CLASS_UINT32
:
2814 guint32 a_val
= g_variant_get_uint32 (a
);
2815 guint32 b_val
= g_variant_get_uint32 (b
);
2817 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2820 case G_VARIANT_CLASS_INT64
:
2822 gint64 a_val
= g_variant_get_int64 (a
);
2823 gint64 b_val
= g_variant_get_int64 (b
);
2825 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2828 case G_VARIANT_CLASS_UINT64
:
2830 guint64 a_val
= g_variant_get_uint64 (a
);
2831 guint64 b_val
= g_variant_get_uint64 (b
);
2833 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2836 case G_VARIANT_CLASS_DOUBLE
:
2838 gdouble a_val
= g_variant_get_double (a
);
2839 gdouble b_val
= g_variant_get_double (b
);
2841 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2844 case G_VARIANT_CLASS_STRING
:
2845 case G_VARIANT_CLASS_OBJECT_PATH
:
2846 case G_VARIANT_CLASS_SIGNATURE
:
2847 return strcmp (g_variant_get_string (a
, NULL
),
2848 g_variant_get_string (b
, NULL
));
2851 g_return_val_if_fail (!g_variant_is_container (a
), 0);
2852 g_assert_not_reached ();
2856 /* GVariantIter {{{1 */
2858 * GVariantIter: (skip)
2860 * #GVariantIter is an opaque data structure and can only be accessed
2861 * using the following functions.
2868 const gchar
*loop_format
;
2874 G_STATIC_ASSERT (sizeof (struct stack_iter
) <= sizeof (GVariantIter
));
2878 struct stack_iter iter
;
2880 GVariant
*value_ref
;
2884 #define GVSI(i) ((struct stack_iter *) (i))
2885 #define GVHI(i) ((struct heap_iter *) (i))
2886 #define GVSI_MAGIC ((gsize) 3579507750u)
2887 #define GVHI_MAGIC ((gsize) 1450270775u)
2888 #define is_valid_iter(i) (i != NULL && \
2889 GVSI(i)->magic == GVSI_MAGIC)
2890 #define is_valid_heap_iter(i) (GVHI(i)->magic == GVHI_MAGIC && \
2894 * g_variant_iter_new:
2895 * @value: a container #GVariant
2897 * Creates a heap-allocated #GVariantIter for iterating over the items
2900 * Use g_variant_iter_free() to free the return value when you no longer
2903 * A reference is taken to @value and will be released only when
2904 * g_variant_iter_free() is called.
2906 * Returns: (transfer full): a new heap-allocated #GVariantIter
2911 g_variant_iter_new (GVariant
*value
)
2915 iter
= (GVariantIter
*) g_slice_new (struct heap_iter
);
2916 GVHI(iter
)->value_ref
= g_variant_ref (value
);
2917 GVHI(iter
)->magic
= GVHI_MAGIC
;
2919 g_variant_iter_init (iter
, value
);
2925 * g_variant_iter_init: (skip)
2926 * @iter: a pointer to a #GVariantIter
2927 * @value: a container #GVariant
2929 * Initialises (without allocating) a #GVariantIter. @iter may be
2930 * completely uninitialised prior to this call; its old value is
2933 * The iterator remains valid for as long as @value exists, and need not
2934 * be freed in any way.
2936 * Returns: the number of items in @value
2941 g_variant_iter_init (GVariantIter
*iter
,
2944 GVSI(iter
)->magic
= GVSI_MAGIC
;
2945 GVSI(iter
)->value
= value
;
2946 GVSI(iter
)->n
= g_variant_n_children (value
);
2948 GVSI(iter
)->loop_format
= NULL
;
2950 return GVSI(iter
)->n
;
2954 * g_variant_iter_copy:
2955 * @iter: a #GVariantIter
2957 * Creates a new heap-allocated #GVariantIter to iterate over the
2958 * container that was being iterated over by @iter. Iteration begins on
2959 * the new iterator from the current position of the old iterator but
2960 * the two copies are independent past that point.
2962 * Use g_variant_iter_free() to free the return value when you no longer
2965 * A reference is taken to the container that @iter is iterating over
2966 * and will be releated only when g_variant_iter_free() is called.
2968 * Returns: (transfer full): a new heap-allocated #GVariantIter
2973 g_variant_iter_copy (GVariantIter
*iter
)
2977 g_return_val_if_fail (is_valid_iter (iter
), 0);
2979 copy
= g_variant_iter_new (GVSI(iter
)->value
);
2980 GVSI(copy
)->i
= GVSI(iter
)->i
;
2986 * g_variant_iter_n_children:
2987 * @iter: a #GVariantIter
2989 * Queries the number of child items in the container that we are
2990 * iterating over. This is the total number of items -- not the number
2991 * of items remaining.
2993 * This function might be useful for preallocation of arrays.
2995 * Returns: the number of children in the container
3000 g_variant_iter_n_children (GVariantIter
*iter
)
3002 g_return_val_if_fail (is_valid_iter (iter
), 0);
3004 return GVSI(iter
)->n
;
3008 * g_variant_iter_free:
3009 * @iter: (transfer full): a heap-allocated #GVariantIter
3011 * Frees a heap-allocated #GVariantIter. Only call this function on
3012 * iterators that were returned by g_variant_iter_new() or
3013 * g_variant_iter_copy().
3018 g_variant_iter_free (GVariantIter
*iter
)
3020 g_return_if_fail (is_valid_heap_iter (iter
));
3022 g_variant_unref (GVHI(iter
)->value_ref
);
3023 GVHI(iter
)->magic
= 0;
3025 g_slice_free (struct heap_iter
, GVHI(iter
));
3029 * g_variant_iter_next_value:
3030 * @iter: a #GVariantIter
3032 * Gets the next item in the container. If no more items remain then
3033 * %NULL is returned.
3035 * Use g_variant_unref() to drop your reference on the return value when
3036 * you no longer need it.
3038 * Here is an example for iterating with g_variant_iter_next_value():
3039 * |[<!-- language="C" -->
3040 * // recursively iterate a container
3042 * iterate_container_recursive (GVariant *container)
3044 * GVariantIter iter;
3047 * g_variant_iter_init (&iter, container);
3048 * while ((child = g_variant_iter_next_value (&iter)))
3050 * g_print ("type '%s'\n", g_variant_get_type_string (child));
3052 * if (g_variant_is_container (child))
3053 * iterate_container_recursive (child);
3055 * g_variant_unref (child);
3060 * Returns: (allow-none) (transfer full): a #GVariant, or %NULL
3065 g_variant_iter_next_value (GVariantIter
*iter
)
3067 g_return_val_if_fail (is_valid_iter (iter
), FALSE
);
3069 if G_UNLIKELY (GVSI(iter
)->i
>= GVSI(iter
)->n
)
3071 g_critical ("g_variant_iter_next_value: must not be called again "
3072 "after NULL has already been returned.");
3078 if (GVSI(iter
)->i
< GVSI(iter
)->n
)
3079 return g_variant_get_child_value (GVSI(iter
)->value
, GVSI(iter
)->i
);
3084 /* GVariantBuilder {{{1 */
3088 * A utility type for constructing container-type #GVariant instances.
3090 * This is an opaque structure and may only be accessed using the
3091 * following functions.
3093 * #GVariantBuilder is not threadsafe in any way. Do not attempt to
3094 * access it from more than one thread.
3097 struct stack_builder
3099 GVariantBuilder
*parent
;
3102 /* type constraint explicitly specified by 'type'.
3103 * for tuple types, this moves along as we add more items.
3105 const GVariantType
*expected_type
;
3107 /* type constraint implied by previous array item.
3109 const GVariantType
*prev_item_type
;
3111 /* constraints on the number of children. max = -1 for unlimited. */
3115 /* dynamically-growing pointer array */
3116 GVariant
**children
;
3117 gsize allocated_children
;
3120 /* set to '1' if all items in the container will have the same type
3121 * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
3123 guint uniform_item_types
: 1;
3125 /* set to '1' initially and changed to '0' if an untrusted value is
3133 G_STATIC_ASSERT (sizeof (struct stack_builder
) <= sizeof (GVariantBuilder
));
3137 GVariantBuilder builder
;
3143 #define GVSB(b) ((struct stack_builder *) (b))
3144 #define GVHB(b) ((struct heap_builder *) (b))
3145 #define GVSB_MAGIC ((gsize) 1033660112u)
3146 #define GVHB_MAGIC ((gsize) 3087242682u)
3147 #define is_valid_builder(b) (b != NULL && \
3148 GVSB(b)->magic == GVSB_MAGIC)
3149 #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
3152 * g_variant_builder_new:
3153 * @type: a container type
3155 * Allocates and initialises a new #GVariantBuilder.
3157 * You should call g_variant_builder_unref() on the return value when it
3158 * is no longer needed. The memory will not be automatically freed by
3161 * In most cases it is easier to place a #GVariantBuilder directly on
3162 * the stack of the calling function and initialise it with
3163 * g_variant_builder_init().
3165 * Returns: (transfer full): a #GVariantBuilder
3170 g_variant_builder_new (const GVariantType
*type
)
3172 GVariantBuilder
*builder
;
3174 builder
= (GVariantBuilder
*) g_slice_new (struct heap_builder
);
3175 g_variant_builder_init (builder
, type
);
3176 GVHB(builder
)->magic
= GVHB_MAGIC
;
3177 GVHB(builder
)->ref_count
= 1;
3183 * g_variant_builder_unref:
3184 * @builder: (transfer full): a #GVariantBuilder allocated by g_variant_builder_new()
3186 * Decreases the reference count on @builder.
3188 * In the event that there are no more references, releases all memory
3189 * associated with the #GVariantBuilder.
3191 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3192 * things will happen.
3197 g_variant_builder_unref (GVariantBuilder
*builder
)
3199 g_return_if_fail (is_valid_heap_builder (builder
));
3201 if (--GVHB(builder
)->ref_count
)
3204 g_variant_builder_clear (builder
);
3205 GVHB(builder
)->magic
= 0;
3207 g_slice_free (struct heap_builder
, GVHB(builder
));
3211 * g_variant_builder_ref:
3212 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
3214 * Increases the reference count on @builder.
3216 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3217 * things will happen.
3219 * Returns: (transfer full): a new reference to @builder
3224 g_variant_builder_ref (GVariantBuilder
*builder
)
3226 g_return_val_if_fail (is_valid_heap_builder (builder
), NULL
);
3228 GVHB(builder
)->ref_count
++;
3234 * g_variant_builder_clear: (skip)
3235 * @builder: a #GVariantBuilder
3237 * Releases all memory associated with a #GVariantBuilder without
3238 * freeing the #GVariantBuilder structure itself.
3240 * It typically only makes sense to do this on a stack-allocated
3241 * #GVariantBuilder if you want to abort building the value part-way
3242 * through. This function need not be called if you call
3243 * g_variant_builder_end() and it also doesn't need to be called on
3244 * builders allocated with g_variant_builder_new (see
3245 * g_variant_builder_unref() for that).
3247 * This function leaves the #GVariantBuilder structure set to all-zeros.
3248 * It is valid to call this function on either an initialised
3249 * #GVariantBuilder or one that is set to all-zeros but it is not valid
3250 * to call this function on uninitialised memory.
3255 g_variant_builder_clear (GVariantBuilder
*builder
)
3259 if (GVSB(builder
)->magic
== 0)
3260 /* all-zeros case */
3263 g_return_if_fail (is_valid_builder (builder
));
3265 g_variant_type_free (GVSB(builder
)->type
);
3267 for (i
= 0; i
< GVSB(builder
)->offset
; i
++)
3268 g_variant_unref (GVSB(builder
)->children
[i
]);
3270 g_free (GVSB(builder
)->children
);
3272 if (GVSB(builder
)->parent
)
3274 g_variant_builder_clear (GVSB(builder
)->parent
);
3275 g_slice_free (GVariantBuilder
, GVSB(builder
)->parent
);
3278 memset (builder
, 0, sizeof (GVariantBuilder
));
3282 * g_variant_builder_init: (skip)
3283 * @builder: a #GVariantBuilder
3284 * @type: a container type
3286 * Initialises a #GVariantBuilder structure.
3288 * @type must be non-%NULL. It specifies the type of container to
3289 * construct. It can be an indefinite type such as
3290 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
3291 * Maybe, array, tuple, dictionary entry and variant-typed values may be
3294 * After the builder is initialised, values are added using
3295 * g_variant_builder_add_value() or g_variant_builder_add().
3297 * After all the child values are added, g_variant_builder_end() frees
3298 * the memory associated with the builder and returns the #GVariant that
3301 * This function completely ignores the previous contents of @builder.
3302 * On one hand this means that it is valid to pass in completely
3303 * uninitialised memory. On the other hand, this means that if you are
3304 * initialising over top of an existing #GVariantBuilder you need to
3305 * first call g_variant_builder_clear() in order to avoid leaking
3308 * You must not call g_variant_builder_ref() or
3309 * g_variant_builder_unref() on a #GVariantBuilder that was initialised
3310 * with this function. If you ever pass a reference to a
3311 * #GVariantBuilder outside of the control of your own code then you
3312 * should assume that the person receiving that reference may try to use
3313 * reference counting; you should use g_variant_builder_new() instead of
3319 g_variant_builder_init (GVariantBuilder
*builder
,
3320 const GVariantType
*type
)
3322 g_return_if_fail (type
!= NULL
);
3323 g_return_if_fail (g_variant_type_is_container (type
));
3325 memset (builder
, 0, sizeof (GVariantBuilder
));
3327 GVSB(builder
)->type
= g_variant_type_copy (type
);
3328 GVSB(builder
)->magic
= GVSB_MAGIC
;
3329 GVSB(builder
)->trusted
= TRUE
;
3331 switch (*(const gchar
*) type
)
3333 case G_VARIANT_CLASS_VARIANT
:
3334 GVSB(builder
)->uniform_item_types
= TRUE
;
3335 GVSB(builder
)->allocated_children
= 1;
3336 GVSB(builder
)->expected_type
= NULL
;
3337 GVSB(builder
)->min_items
= 1;
3338 GVSB(builder
)->max_items
= 1;
3341 case G_VARIANT_CLASS_ARRAY
:
3342 GVSB(builder
)->uniform_item_types
= TRUE
;
3343 GVSB(builder
)->allocated_children
= 8;
3344 GVSB(builder
)->expected_type
=
3345 g_variant_type_element (GVSB(builder
)->type
);
3346 GVSB(builder
)->min_items
= 0;
3347 GVSB(builder
)->max_items
= -1;
3350 case G_VARIANT_CLASS_MAYBE
:
3351 GVSB(builder
)->uniform_item_types
= TRUE
;
3352 GVSB(builder
)->allocated_children
= 1;
3353 GVSB(builder
)->expected_type
=
3354 g_variant_type_element (GVSB(builder
)->type
);
3355 GVSB(builder
)->min_items
= 0;
3356 GVSB(builder
)->max_items
= 1;
3359 case G_VARIANT_CLASS_DICT_ENTRY
:
3360 GVSB(builder
)->uniform_item_types
= FALSE
;
3361 GVSB(builder
)->allocated_children
= 2;
3362 GVSB(builder
)->expected_type
=
3363 g_variant_type_key (GVSB(builder
)->type
);
3364 GVSB(builder
)->min_items
= 2;
3365 GVSB(builder
)->max_items
= 2;
3368 case 'r': /* G_VARIANT_TYPE_TUPLE was given */
3369 GVSB(builder
)->uniform_item_types
= FALSE
;
3370 GVSB(builder
)->allocated_children
= 8;
3371 GVSB(builder
)->expected_type
= NULL
;
3372 GVSB(builder
)->min_items
= 0;
3373 GVSB(builder
)->max_items
= -1;
3376 case G_VARIANT_CLASS_TUPLE
: /* a definite tuple type was given */
3377 GVSB(builder
)->allocated_children
= g_variant_type_n_items (type
);
3378 GVSB(builder
)->expected_type
=
3379 g_variant_type_first (GVSB(builder
)->type
);
3380 GVSB(builder
)->min_items
= GVSB(builder
)->allocated_children
;
3381 GVSB(builder
)->max_items
= GVSB(builder
)->allocated_children
;
3382 GVSB(builder
)->uniform_item_types
= FALSE
;
3386 g_assert_not_reached ();
3389 GVSB(builder
)->children
= g_new (GVariant
*,
3390 GVSB(builder
)->allocated_children
);
3394 g_variant_builder_make_room (struct stack_builder
*builder
)
3396 if (builder
->offset
== builder
->allocated_children
)
3398 builder
->allocated_children
*= 2;
3399 builder
->children
= g_renew (GVariant
*, builder
->children
,
3400 builder
->allocated_children
);
3405 * g_variant_builder_add_value:
3406 * @builder: a #GVariantBuilder
3407 * @value: a #GVariant
3409 * Adds @value to @builder.
3411 * It is an error to call this function in any way that would create an
3412 * inconsistent value to be constructed. Some examples of this are
3413 * putting different types of items into an array, putting the wrong
3414 * types or number of items in a tuple, putting more than one value into
3417 * If @value is a floating reference (see g_variant_ref_sink()),
3418 * the @builder instance takes ownership of @value.
3423 g_variant_builder_add_value (GVariantBuilder
*builder
,
3426 g_return_if_fail (is_valid_builder (builder
));
3427 g_return_if_fail (GVSB(builder
)->offset
< GVSB(builder
)->max_items
);
3428 g_return_if_fail (!GVSB(builder
)->expected_type
||
3429 g_variant_is_of_type (value
,
3430 GVSB(builder
)->expected_type
));
3431 g_return_if_fail (!GVSB(builder
)->prev_item_type
||
3432 g_variant_is_of_type (value
,
3433 GVSB(builder
)->prev_item_type
));
3435 GVSB(builder
)->trusted
&= g_variant_is_trusted (value
);
3437 if (!GVSB(builder
)->uniform_item_types
)
3439 /* advance our expected type pointers */
3440 if (GVSB(builder
)->expected_type
)
3441 GVSB(builder
)->expected_type
=
3442 g_variant_type_next (GVSB(builder
)->expected_type
);
3444 if (GVSB(builder
)->prev_item_type
)
3445 GVSB(builder
)->prev_item_type
=
3446 g_variant_type_next (GVSB(builder
)->prev_item_type
);
3449 GVSB(builder
)->prev_item_type
= g_variant_get_type (value
);
3451 g_variant_builder_make_room (GVSB(builder
));
3453 GVSB(builder
)->children
[GVSB(builder
)->offset
++] =
3454 g_variant_ref_sink (value
);
3458 * g_variant_builder_open:
3459 * @builder: a #GVariantBuilder
3460 * @type: a #GVariantType
3462 * Opens a subcontainer inside the given @builder. When done adding
3463 * items to the subcontainer, g_variant_builder_close() must be called.
3465 * It is an error to call this function in any way that would cause an
3466 * inconsistent value to be constructed (ie: adding too many values or
3467 * a value of an incorrect type).
3472 g_variant_builder_open (GVariantBuilder
*builder
,
3473 const GVariantType
*type
)
3475 GVariantBuilder
*parent
;
3477 g_return_if_fail (is_valid_builder (builder
));
3478 g_return_if_fail (GVSB(builder
)->offset
< GVSB(builder
)->max_items
);
3479 g_return_if_fail (!GVSB(builder
)->expected_type
||
3480 g_variant_type_is_subtype_of (type
,
3481 GVSB(builder
)->expected_type
));
3482 g_return_if_fail (!GVSB(builder
)->prev_item_type
||
3483 g_variant_type_is_subtype_of (GVSB(builder
)->prev_item_type
,
3486 parent
= g_slice_dup (GVariantBuilder
, builder
);
3487 g_variant_builder_init (builder
, type
);
3488 GVSB(builder
)->parent
= parent
;
3490 /* push the prev_item_type down into the subcontainer */
3491 if (GVSB(parent
)->prev_item_type
)
3493 if (!GVSB(builder
)->uniform_item_types
)
3494 /* tuples and dict entries */
3495 GVSB(builder
)->prev_item_type
=
3496 g_variant_type_first (GVSB(parent
)->prev_item_type
);
3498 else if (!g_variant_type_is_variant (GVSB(builder
)->type
))
3499 /* maybes and arrays */
3500 GVSB(builder
)->prev_item_type
=
3501 g_variant_type_element (GVSB(parent
)->prev_item_type
);
3506 * g_variant_builder_close:
3507 * @builder: a #GVariantBuilder
3509 * Closes the subcontainer inside the given @builder that was opened by
3510 * the most recent call to g_variant_builder_open().
3512 * It is an error to call this function in any way that would create an
3513 * inconsistent value to be constructed (ie: too few values added to the
3519 g_variant_builder_close (GVariantBuilder
*builder
)
3521 GVariantBuilder
*parent
;
3523 g_return_if_fail (is_valid_builder (builder
));
3524 g_return_if_fail (GVSB(builder
)->parent
!= NULL
);
3526 parent
= GVSB(builder
)->parent
;
3527 GVSB(builder
)->parent
= NULL
;
3529 g_variant_builder_add_value (parent
, g_variant_builder_end (builder
));
3532 g_slice_free (GVariantBuilder
, parent
);
3536 * g_variant_make_maybe_type:
3537 * @element: a #GVariant
3539 * Return the type of a maybe containing @element.
3541 static GVariantType
*
3542 g_variant_make_maybe_type (GVariant
*element
)
3544 return g_variant_type_new_maybe (g_variant_get_type (element
));
3548 * g_variant_make_array_type:
3549 * @element: a #GVariant
3551 * Return the type of an array containing @element.
3553 static GVariantType
*
3554 g_variant_make_array_type (GVariant
*element
)
3556 return g_variant_type_new_array (g_variant_get_type (element
));
3560 * g_variant_builder_end:
3561 * @builder: a #GVariantBuilder
3563 * Ends the builder process and returns the constructed value.
3565 * It is not permissible to use @builder in any way after this call
3566 * except for reference counting operations (in the case of a
3567 * heap-allocated #GVariantBuilder) or by reinitialising it with
3568 * g_variant_builder_init() (in the case of stack-allocated).
3570 * It is an error to call this function in any way that would create an
3571 * inconsistent value to be constructed (ie: insufficient number of
3572 * items added to a container with a specific number of children
3573 * required). It is also an error to call this function if the builder
3574 * was created with an indefinite array or maybe type and no children
3575 * have been added; in this case it is impossible to infer the type of
3578 * Returns: (transfer none): a new, floating, #GVariant
3583 g_variant_builder_end (GVariantBuilder
*builder
)
3585 GVariantType
*my_type
;
3588 g_return_val_if_fail (is_valid_builder (builder
), NULL
);
3589 g_return_val_if_fail (GVSB(builder
)->offset
>= GVSB(builder
)->min_items
,
3591 g_return_val_if_fail (!GVSB(builder
)->uniform_item_types
||
3592 GVSB(builder
)->prev_item_type
!= NULL
||
3593 g_variant_type_is_definite (GVSB(builder
)->type
),
3596 if (g_variant_type_is_definite (GVSB(builder
)->type
))
3597 my_type
= g_variant_type_copy (GVSB(builder
)->type
);
3599 else if (g_variant_type_is_maybe (GVSB(builder
)->type
))
3600 my_type
= g_variant_make_maybe_type (GVSB(builder
)->children
[0]);
3602 else if (g_variant_type_is_array (GVSB(builder
)->type
))
3603 my_type
= g_variant_make_array_type (GVSB(builder
)->children
[0]);
3605 else if (g_variant_type_is_tuple (GVSB(builder
)->type
))
3606 my_type
= g_variant_make_tuple_type (GVSB(builder
)->children
,
3607 GVSB(builder
)->offset
);
3609 else if (g_variant_type_is_dict_entry (GVSB(builder
)->type
))
3610 my_type
= g_variant_make_dict_entry_type (GVSB(builder
)->children
[0],
3611 GVSB(builder
)->children
[1]);
3613 g_assert_not_reached ();
3615 value
= g_variant_new_from_children (my_type
,
3616 g_renew (GVariant
*,
3617 GVSB(builder
)->children
,
3618 GVSB(builder
)->offset
),
3619 GVSB(builder
)->offset
,
3620 GVSB(builder
)->trusted
);
3621 GVSB(builder
)->children
= NULL
;
3622 GVSB(builder
)->offset
= 0;
3624 g_variant_builder_clear (builder
);
3625 g_variant_type_free (my_type
);
3630 /* GVariantDict {{{1 */
3635 * #GVariantDict is a mutable interface to #GVariant dictionaries.
3637 * It can be used for doing a sequence of dictionary lookups in an
3638 * efficient way on an existing #GVariant dictionary or it can be used
3639 * to construct new dictionaries with a hashtable-like interface. It
3640 * can also be used for taking existing dictionaries and modifying them
3641 * in order to create new ones.
3643 * #GVariantDict can only be used with %G_VARIANT_TYPE_VARDICT
3646 * It is possible to use #GVariantDict allocated on the stack or on the
3647 * heap. When using a stack-allocated #GVariantDict, you begin with a
3648 * call to g_variant_dict_init() and free the resources with a call to
3649 * g_variant_dict_clear().
3651 * Heap-allocated #GVariantDict follows normal refcounting rules: you
3652 * allocate it with g_variant_dict_new() and use g_variant_dict_ref()
3653 * and g_variant_dict_unref().
3655 * g_variant_dict_end() is used to convert the #GVariantDict back into a
3656 * dictionary-type #GVariant. When used with stack-allocated instances,
3657 * this also implicitly frees all associated memory, but for
3658 * heap-allocated instances, you must still call g_variant_dict_unref()
3661 * You will typically want to use a heap-allocated #GVariantDict when
3662 * you expose it as part of an API. For most other uses, the
3663 * stack-allocated form will be more convenient.
3665 * Consider the following two examples that do the same thing in each
3666 * style: take an existing dictionary and look up the "count" uint32
3667 * key, adding 1 to it if it is found, or returning an error if the
3668 * key is not found. Each returns the new dictionary as a floating
3671 * ## Using a stack-allocated GVariantDict
3673 * |[<!-- language="C" -->
3675 * add_to_count (GVariant *orig,
3678 * GVariantDict dict;
3681 * g_variant_dict_init (&dict, orig);
3682 * if (!g_variant_dict_lookup (&dict, "count", "u", &count))
3684 * g_set_error (...);
3685 * g_variant_dict_clear (&dict);
3689 * g_variant_dict_insert (&dict, "count", "u", count + 1);
3691 * return g_variant_dict_end (&dict);
3695 * ## Using heap-allocated GVariantDict
3697 * |[<!-- language="C" -->
3699 * add_to_count (GVariant *orig,
3702 * GVariantDict *dict;
3706 * dict = g_variant_dict_new (orig);
3708 * if (g_variant_dict_lookup (dict, "count", "u", &count))
3710 * g_variant_dict_insert (dict, "count", "u", count + 1);
3711 * result = g_variant_dict_end (dict);
3715 * g_set_error (...);
3719 * g_variant_dict_unref (dict);
3733 G_STATIC_ASSERT (sizeof (struct stack_dict
) <= sizeof (GVariantDict
));
3737 struct stack_dict dict
;
3742 #define GVSD(d) ((struct stack_dict *) (d))
3743 #define GVHD(d) ((struct heap_dict *) (d))
3744 #define GVSD_MAGIC ((gsize) 2579507750u)
3745 #define GVHD_MAGIC ((gsize) 2450270775u)
3746 #define is_valid_dict(d) (d != NULL && \
3747 GVSD(d)->magic == GVSD_MAGIC)
3748 #define is_valid_heap_dict(d) (GVHD(d)->magic == GVHD_MAGIC)
3751 * g_variant_dict_new:
3752 * @from_asv: (allow-none): the #GVariant with which to initialise the
3755 * Allocates and initialises a new #GVariantDict.
3757 * You should call g_variant_dict_unref() on the return value when it
3758 * is no longer needed. The memory will not be automatically freed by
3761 * In some cases it may be easier to place a #GVariantDict directly on
3762 * the stack of the calling function and initialise it with
3763 * g_variant_dict_init(). This is particularly useful when you are
3764 * using #GVariantDict to construct a #GVariant.
3766 * Returns: (transfer full): a #GVariantDict
3771 g_variant_dict_new (GVariant
*from_asv
)
3775 dict
= g_slice_alloc (sizeof (struct heap_dict
));
3776 g_variant_dict_init (dict
, from_asv
);
3777 GVHD(dict
)->magic
= GVHD_MAGIC
;
3778 GVHD(dict
)->ref_count
= 1;
3784 * g_variant_dict_init: (skip)
3785 * @dict: a #GVariantDict
3786 * @from_asv: (allow-none): the initial value for @dict
3788 * Initialises a #GVariantDict structure.
3790 * If @from_asv is given, it is used to initialise the dictionary.
3792 * This function completely ignores the previous contents of @dict. On
3793 * one hand this means that it is valid to pass in completely
3794 * uninitialised memory. On the other hand, this means that if you are
3795 * initialising over top of an existing #GVariantDict you need to first
3796 * call g_variant_dict_clear() in order to avoid leaking memory.
3798 * You must not call g_variant_dict_ref() or g_variant_dict_unref() on a
3799 * #GVariantDict that was initialised with this function. If you ever
3800 * pass a reference to a #GVariantDict outside of the control of your
3801 * own code then you should assume that the person receiving that
3802 * reference may try to use reference counting; you should use
3803 * g_variant_dict_new() instead of this function.
3808 g_variant_dict_init (GVariantDict
*dict
,
3815 GVSD(dict
)->values
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_variant_unref
);
3816 GVSD(dict
)->magic
= GVSD_MAGIC
;
3820 g_variant_iter_init (&iter
, from_asv
);
3821 while (g_variant_iter_next (&iter
, "{sv}", &key
, &value
))
3822 g_hash_table_insert (GVSD(dict
)->values
, key
, value
);
3827 * g_variant_dict_lookup:
3828 * @dict: a #GVariantDict
3829 * @key: the key to lookup in the dictionary
3830 * @format_string: a GVariant format string
3831 * @...: the arguments to unpack the value into
3833 * Looks up a value in a #GVariantDict.
3835 * This function is a wrapper around g_variant_dict_lookup_value() and
3836 * g_variant_get(). In the case that %NULL would have been returned,
3837 * this function returns %FALSE. Otherwise, it unpacks the returned
3838 * value and returns %TRUE.
3840 * @format_string determines the C types that are used for unpacking the
3841 * values and also determines if the values are copied or borrowed, see the
3842 * section on [GVariant format strings][gvariant-format-strings-pointers].
3844 * Returns: %TRUE if a value was unpacked
3849 g_variant_dict_lookup (GVariantDict
*dict
,
3851 const gchar
*format_string
,
3857 g_return_val_if_fail (is_valid_dict (dict
), FALSE
);
3858 g_return_val_if_fail (key
!= NULL
, FALSE
);
3859 g_return_val_if_fail (format_string
!= NULL
, FALSE
);
3861 value
= g_hash_table_lookup (GVSD(dict
)->values
, key
);
3863 if (value
== NULL
|| !g_variant_check_format_string (value
, format_string
, FALSE
))
3866 va_start (ap
, format_string
);
3867 g_variant_get_va (value
, format_string
, NULL
, &ap
);
3874 * g_variant_dict_lookup_value:
3875 * @dict: a #GVariantDict
3876 * @key: the key to lookup in the dictionary
3877 * @expected_type: (allow-none): a #GVariantType, or %NULL
3879 * Looks up a value in a #GVariantDict.
3881 * If @key is not found in @dictionary, %NULL is returned.
3883 * The @expected_type string specifies what type of value is expected.
3884 * If the value associated with @key has a different type then %NULL is
3887 * If the key is found and the value has the correct type, it is
3888 * returned. If @expected_type was specified then any non-%NULL return
3889 * value will have this type.
3891 * Returns: (transfer full): the value of the dictionary key, or %NULL
3896 g_variant_dict_lookup_value (GVariantDict
*dict
,
3898 const GVariantType
*expected_type
)
3902 g_return_val_if_fail (is_valid_dict (dict
), NULL
);
3903 g_return_val_if_fail (key
!= NULL
, NULL
);
3905 result
= g_hash_table_lookup (GVSD(dict
)->values
, key
);
3907 if (result
&& (!expected_type
|| g_variant_is_of_type (result
, expected_type
)))
3908 return g_variant_ref (result
);
3914 * g_variant_dict_contains:
3915 * @dict: a #GVariantDict
3916 * @key: the key to lookup in the dictionary
3918 * Checks if @key exists in @dict.
3920 * Returns: %TRUE if @key is in @dict
3925 g_variant_dict_contains (GVariantDict
*dict
,
3928 g_return_val_if_fail (is_valid_dict (dict
), FALSE
);
3929 g_return_val_if_fail (key
!= NULL
, FALSE
);
3931 return g_hash_table_contains (GVSD(dict
)->values
, key
);
3935 * g_variant_dict_insert:
3936 * @dict: a #GVariantDict
3937 * @key: the key to insert a value for
3938 * @format_string: a #GVariant varargs format string
3939 * @...: arguments, as per @format_string
3941 * Inserts a value into a #GVariantDict.
3943 * This call is a convenience wrapper that is exactly equivalent to
3944 * calling g_variant_new() followed by g_variant_dict_insert_value().
3949 g_variant_dict_insert (GVariantDict
*dict
,
3951 const gchar
*format_string
,
3956 g_return_if_fail (is_valid_dict (dict
));
3957 g_return_if_fail (key
!= NULL
);
3958 g_return_if_fail (format_string
!= NULL
);
3960 va_start (ap
, format_string
);
3961 g_variant_dict_insert_value (dict
, key
, g_variant_new_va (format_string
, NULL
, &ap
));
3966 * g_variant_dict_insert_value:
3967 * @dict: a #GVariantDict
3968 * @key: the key to insert a value for
3969 * @value: the value to insert
3971 * Inserts (or replaces) a key in a #GVariantDict.
3973 * @value is consumed if it is floating.
3978 g_variant_dict_insert_value (GVariantDict
*dict
,
3982 g_return_if_fail (is_valid_dict (dict
));
3983 g_return_if_fail (key
!= NULL
);
3984 g_return_if_fail (value
!= NULL
);
3986 g_hash_table_insert (GVSD(dict
)->values
, g_strdup (key
), g_variant_ref_sink (value
));
3990 * g_variant_dict_remove:
3991 * @dict: a #GVariantDict
3992 * @key: the key to remove
3994 * Removes a key and its associated value from a #GVariantDict.
3996 * Returns: %TRUE if the key was found and removed
4001 g_variant_dict_remove (GVariantDict
*dict
,
4004 g_return_val_if_fail (is_valid_dict (dict
), FALSE
);
4005 g_return_val_if_fail (key
!= NULL
, FALSE
);
4007 return g_hash_table_remove (GVSD(dict
)->values
, key
);
4011 * g_variant_dict_clear:
4012 * @dict: a #GVariantDict
4014 * Releases all memory associated with a #GVariantDict without freeing
4015 * the #GVariantDict structure itself.
4017 * It typically only makes sense to do this on a stack-allocated
4018 * #GVariantDict if you want to abort building the value part-way
4019 * through. This function need not be called if you call
4020 * g_variant_dict_end() and it also doesn't need to be called on dicts
4021 * allocated with g_variant_dict_new (see g_variant_dict_unref() for
4024 * It is valid to call this function on either an initialised
4025 * #GVariantDict or one that was previously cleared by an earlier call
4026 * to g_variant_dict_clear() but it is not valid to call this function
4027 * on uninitialised memory.
4032 g_variant_dict_clear (GVariantDict
*dict
)
4034 if (GVSD(dict
)->magic
== 0)
4035 /* all-zeros case */
4038 g_return_if_fail (is_valid_dict (dict
));
4040 g_hash_table_unref (GVSD(dict
)->values
);
4041 GVSD(dict
)->values
= NULL
;
4043 GVSD(dict
)->magic
= 0;
4047 * g_variant_dict_end:
4048 * @dict: a #GVariantDict
4050 * Returns the current value of @dict as a #GVariant of type
4051 * %G_VARIANT_TYPE_VARDICT, clearing it in the process.
4053 * It is not permissible to use @dict in any way after this call except
4054 * for reference counting operations (in the case of a heap-allocated
4055 * #GVariantDict) or by reinitialising it with g_variant_dict_init() (in
4056 * the case of stack-allocated).
4058 * Returns: (transfer none): a new, floating, #GVariant
4063 g_variant_dict_end (GVariantDict
*dict
)
4065 GVariantBuilder builder
;
4066 GHashTableIter iter
;
4067 gpointer key
, value
;
4069 g_return_val_if_fail (is_valid_dict (dict
), NULL
);
4071 g_variant_builder_init (&builder
, G_VARIANT_TYPE_VARDICT
);
4073 g_hash_table_iter_init (&iter
, GVSD(dict
)->values
);
4074 while (g_hash_table_iter_next (&iter
, &key
, &value
))
4075 g_variant_builder_add (&builder
, "{sv}", (const gchar
*) key
, (GVariant
*) value
);
4077 g_variant_dict_clear (dict
);
4079 return g_variant_builder_end (&builder
);
4083 * g_variant_dict_ref:
4084 * @dict: a heap-allocated #GVariantDict
4086 * Increases the reference count on @dict.
4088 * Don't call this on stack-allocated #GVariantDict instances or bad
4089 * things will happen.
4091 * Returns: (transfer full): a new reference to @dict
4096 g_variant_dict_ref (GVariantDict
*dict
)
4098 g_return_val_if_fail (is_valid_heap_dict (dict
), NULL
);
4100 GVHD(dict
)->ref_count
++;
4106 * g_variant_dict_unref:
4107 * @dict: (transfer full): a heap-allocated #GVariantDict
4109 * Decreases the reference count on @dict.
4111 * In the event that there are no more references, releases all memory
4112 * associated with the #GVariantDict.
4114 * Don't call this on stack-allocated #GVariantDict instances or bad
4115 * things will happen.
4120 g_variant_dict_unref (GVariantDict
*dict
)
4122 g_return_if_fail (is_valid_heap_dict (dict
));
4124 if (--GVHD(dict
)->ref_count
== 0)
4126 g_variant_dict_clear (dict
);
4127 g_slice_free (struct heap_dict
, (struct heap_dict
*) dict
);
4132 /* Format strings {{{1 */
4134 * g_variant_format_string_scan:
4135 * @string: a string that may be prefixed with a format string
4136 * @limit: (allow-none) (default NULL): a pointer to the end of @string,
4138 * @endptr: (allow-none) (default NULL): location to store the end pointer,
4141 * Checks the string pointed to by @string for starting with a properly
4142 * formed #GVariant varargs format string. If no valid format string is
4143 * found then %FALSE is returned.
4145 * If @string does start with a valid format string then %TRUE is
4146 * returned. If @endptr is non-%NULL then it is updated to point to the
4147 * first character after the format string.
4149 * If @limit is non-%NULL then @limit (and any charater after it) will
4150 * not be accessed and the effect is otherwise equivalent to if the
4151 * character at @limit were nul.
4153 * See the section on [GVariant format strings][gvariant-format-strings].
4155 * Returns: %TRUE if there was a valid format string
4160 g_variant_format_string_scan (const gchar
*string
,
4162 const gchar
**endptr
)
4164 #define next_char() (string == limit ? '\0' : *string++)
4165 #define peek_char() (string == limit ? '\0' : *string)
4168 switch (next_char())
4170 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
4171 case 'x': case 't': case 'h': case 'd': case 's': case 'o':
4172 case 'g': case 'v': case '*': case '?': case 'r':
4176 return g_variant_format_string_scan (string
, limit
, endptr
);
4180 return g_variant_type_string_scan (string
, limit
, endptr
);
4183 while (peek_char() != ')')
4184 if (!g_variant_format_string_scan (string
, limit
, &string
))
4187 next_char(); /* consume ')' */
4197 if (c
!= 's' && c
!= 'o' && c
!= 'g')
4205 /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
4206 * The terminating null character is considered to be
4207 * part of the string.
4209 if (c
!= '\0' && strchr ("bynqiuxthdsog?", c
) == NULL
)
4213 if (!g_variant_format_string_scan (string
, limit
, &string
))
4216 if (next_char() != '}')
4222 if ((c
= next_char()) == 'a')
4224 if ((c
= next_char()) == '&')
4226 if ((c
= next_char()) == 'a')
4228 if ((c
= next_char()) == 'y')
4229 break; /* '^a&ay' */
4232 else if (c
== 's' || c
== 'o')
4233 break; /* '^a&s', '^a&o' */
4238 if ((c
= next_char()) == 'y')
4242 else if (c
== 's' || c
== 'o')
4243 break; /* '^as', '^ao' */
4250 if ((c
= next_char()) == 'a')
4252 if ((c
= next_char()) == 'y')
4262 if (c
!= 's' && c
!= 'o' && c
!= 'g')
4281 * g_variant_check_format_string:
4282 * @value: a #GVariant
4283 * @format_string: a valid #GVariant format string
4284 * @copy_only: %TRUE to ensure the format string makes deep copies
4286 * Checks if calling g_variant_get() with @format_string on @value would
4287 * be valid from a type-compatibility standpoint. @format_string is
4288 * assumed to be a valid format string (from a syntactic standpoint).
4290 * If @copy_only is %TRUE then this function additionally checks that it
4291 * would be safe to call g_variant_unref() on @value immediately after
4292 * the call to g_variant_get() without invalidating the result. This is
4293 * only possible if deep copies are made (ie: there are no pointers to
4294 * the data inside of the soon-to-be-freed #GVariant instance). If this
4295 * check fails then a g_critical() is printed and %FALSE is returned.
4297 * This function is meant to be used by functions that wish to provide
4298 * varargs accessors to #GVariant values of uncertain values (eg:
4299 * g_variant_lookup() or g_menu_model_get_item_attribute()).
4301 * Returns: %TRUE if @format_string is safe to use
4306 g_variant_check_format_string (GVariant
*value
,
4307 const gchar
*format_string
,
4310 const gchar
*original_format
= format_string
;
4311 const gchar
*type_string
;
4313 /* Interesting factoid: assuming a format string is valid, it can be
4314 * converted to a type string by removing all '@' '&' and '^'
4317 * Instead of doing that, we can just skip those characters when
4318 * comparing it to the type string of @value.
4320 * For the copy-only case we can just drop the '&' from the list of
4321 * characters to skip over. A '&' will never appear in a type string
4322 * so we know that it won't be possible to return %TRUE if it is in a
4325 type_string
= g_variant_get_type_string (value
);
4327 while (*type_string
|| *format_string
)
4329 gchar format
= *format_string
++;
4334 if G_UNLIKELY (copy_only
)
4336 /* for the love of all that is good, please don't mark this string for translation... */
4337 g_critical ("g_variant_check_format_string() is being called by a function with a GVariant varargs "
4338 "interface to validate the passed format string for type safety. The passed format "
4339 "(%s) contains a '&' character which would result in a pointer being returned to the "
4340 "data inside of a GVariant instance that may no longer exist by the time the function "
4341 "returns. Modify your code to use a format string without '&'.", original_format
);
4348 /* ignore these 2 (or 3) */
4352 /* attempt to consume one of 'bynqiuxthdsog' */
4354 char s
= *type_string
++;
4356 if (s
== '\0' || strchr ("bynqiuxthdsog", s
) == NULL
)
4362 /* ensure it's a tuple */
4363 if (*type_string
!= '(')
4368 /* consume a full type string for the '*' or 'r' */
4369 if (!g_variant_type_string_scan (type_string
, NULL
, &type_string
))
4375 /* attempt to consume exactly one character equal to the format */
4376 if (format
!= *type_string
++)
4385 * g_variant_format_string_scan_type:
4386 * @string: a string that may be prefixed with a format string
4387 * @limit: (allow-none) (default NULL): a pointer to the end of @string,
4389 * @endptr: (allow-none) (default NULL): location to store the end pointer,
4392 * If @string starts with a valid format string then this function will
4393 * return the type that the format string corresponds to. Otherwise
4394 * this function returns %NULL.
4396 * Use g_variant_type_free() to free the return value when you no longer
4399 * This function is otherwise exactly like
4400 * g_variant_format_string_scan().
4402 * Returns: (allow-none): a #GVariantType if there was a valid format string
4407 g_variant_format_string_scan_type (const gchar
*string
,
4409 const gchar
**endptr
)
4411 const gchar
*my_end
;
4418 if (!g_variant_format_string_scan (string
, limit
, endptr
))
4421 dest
= new = g_malloc (*endptr
- string
+ 1);
4422 while (string
!= *endptr
)
4424 if (*string
!= '@' && *string
!= '&' && *string
!= '^')
4430 return (GVariantType
*) G_VARIANT_TYPE (new);
4434 valid_format_string (const gchar
*format_string
,
4438 const gchar
*endptr
;
4441 type
= g_variant_format_string_scan_type (format_string
, NULL
, &endptr
);
4443 if G_UNLIKELY (type
== NULL
|| (single
&& *endptr
!= '\0'))
4446 g_critical ("'%s' is not a valid GVariant format string",
4449 g_critical ("'%s' does not have a valid GVariant format "
4450 "string as a prefix", format_string
);
4453 g_variant_type_free (type
);
4458 if G_UNLIKELY (value
&& !g_variant_is_of_type (value
, type
))
4463 fragment
= g_strndup (format_string
, endptr
- format_string
);
4464 typestr
= g_variant_type_dup_string (type
);
4466 g_critical ("the GVariant format string '%s' has a type of "
4467 "'%s' but the given value has a type of '%s'",
4468 fragment
, typestr
, g_variant_get_type_string (value
));
4470 g_variant_type_free (type
);
4477 g_variant_type_free (type
);
4482 /* Variable Arguments {{{1 */
4483 /* We consider 2 main classes of format strings:
4485 * - recursive format strings
4486 * these are ones that result in recursion and the collection of
4487 * possibly more than one argument. Maybe types, tuples,
4488 * dictionary entries.
4490 * - leaf format string
4491 * these result in the collection of a single argument.
4493 * Leaf format strings are further subdivided into two categories:
4495 * - single non-null pointer ("nnp")
4496 * these either collect or return a single non-null pointer.
4499 * these collect or return something else (bool, number, etc).
4501 * Based on the above, the varargs handling code is split into 4 main parts:
4503 * - nnp handling code
4504 * - leaf handling code (which may invoke nnp code)
4505 * - generic handling code (may be recursive, may invoke leaf code)
4506 * - user-facing API (which invokes the generic code)
4508 * Each section implements some of the following functions:
4511 * collect the arguments for the format string as if
4512 * g_variant_new() had been called, but do nothing with them. used
4513 * for skipping over arguments when constructing a Nothing maybe
4517 * create a GVariant *
4520 * unpack a GVariant *
4522 * - free (nnp only):
4523 * free a previously allocated item
4527 g_variant_format_string_is_leaf (const gchar
*str
)
4529 return str
[0] != 'm' && str
[0] != '(' && str
[0] != '{';
4533 g_variant_format_string_is_nnp (const gchar
*str
)
4535 return str
[0] == 'a' || str
[0] == 's' || str
[0] == 'o' || str
[0] == 'g' ||
4536 str
[0] == '^' || str
[0] == '@' || str
[0] == '*' || str
[0] == '?' ||
4537 str
[0] == 'r' || str
[0] == 'v' || str
[0] == '&';
4540 /* Single non-null pointer ("nnp") {{{2 */
4542 g_variant_valist_free_nnp (const gchar
*str
,
4548 g_variant_iter_free (ptr
);
4552 if (str
[2] != '&') /* '^as', '^ao' */
4554 else /* '^a&s', '^a&o' */
4568 g_variant_unref (ptr
);
4575 g_assert_not_reached ();
4580 g_variant_scan_convenience (const gchar
**str
,
4603 g_variant_valist_new_nnp (const gchar
**str
,
4614 const GVariantType
*type
;
4617 value
= g_variant_builder_end (ptr
);
4618 type
= g_variant_get_type (value
);
4620 if G_UNLIKELY (!g_variant_type_is_array (type
))
4621 g_error ("g_variant_new: expected array GVariantBuilder but "
4622 "the built value has type '%s'",
4623 g_variant_get_type_string (value
));
4625 type
= g_variant_type_element (type
);
4627 if G_UNLIKELY (!g_variant_type_is_subtype_of (type
, (GVariantType
*) *str
))
4628 g_error ("g_variant_new: expected GVariantBuilder array element "
4629 "type '%s' but the built value has element type '%s'",
4630 g_variant_type_dup_string ((GVariantType
*) *str
),
4631 g_variant_get_type_string (value
) + 1);
4633 g_variant_type_string_scan (*str
, NULL
, str
);
4639 /* special case: NULL pointer for empty array */
4641 const GVariantType
*type
= (GVariantType
*) *str
;
4643 g_variant_type_string_scan (*str
, NULL
, str
);
4645 if G_UNLIKELY (!g_variant_type_is_definite (type
))
4646 g_error ("g_variant_new: NULL pointer given with indefinite "
4647 "array type; unable to determine which type of empty "
4648 "array to construct.");
4650 return g_variant_new_array (type
, NULL
, 0);
4657 value
= g_variant_new_string (ptr
);
4660 value
= g_variant_new_string ("[Invalid UTF-8]");
4666 return g_variant_new_object_path (ptr
);
4669 return g_variant_new_signature (ptr
);
4677 type
= g_variant_scan_convenience (str
, &constant
, &arrays
);
4680 return g_variant_new_strv (ptr
, -1);
4683 return g_variant_new_objv (ptr
, -1);
4686 return g_variant_new_bytestring_array (ptr
, -1);
4688 return g_variant_new_bytestring (ptr
);
4692 if G_UNLIKELY (!g_variant_is_of_type (ptr
, (GVariantType
*) *str
))
4693 g_error ("g_variant_new: expected GVariant of type '%s' but "
4694 "received value has type '%s'",
4695 g_variant_type_dup_string ((GVariantType
*) *str
),
4696 g_variant_get_type_string (ptr
));
4698 g_variant_type_string_scan (*str
, NULL
, str
);
4706 if G_UNLIKELY (!g_variant_type_is_basic (g_variant_get_type (ptr
)))
4707 g_error ("g_variant_new: format string '?' expects basic-typed "
4708 "GVariant, but received value has type '%s'",
4709 g_variant_get_type_string (ptr
));
4714 if G_UNLIKELY (!g_variant_type_is_tuple (g_variant_get_type (ptr
)))
4715 g_error ("g_variant_new: format string 'r' expects tuple-typed "
4716 "GVariant, but received value has type '%s'",
4717 g_variant_get_type_string (ptr
));
4722 return g_variant_new_variant (ptr
);
4725 g_assert_not_reached ();
4730 g_variant_valist_get_nnp (const gchar
**str
,
4736 g_variant_type_string_scan (*str
, NULL
, str
);
4737 return g_variant_iter_new (value
);
4741 return (gchar
*) g_variant_get_string (value
, NULL
);
4746 return g_variant_dup_string (value
, NULL
);
4754 type
= g_variant_scan_convenience (str
, &constant
, &arrays
);
4759 return g_variant_get_strv (value
, NULL
);
4761 return g_variant_dup_strv (value
, NULL
);
4764 else if (type
== 'o')
4767 return g_variant_get_objv (value
, NULL
);
4769 return g_variant_dup_objv (value
, NULL
);
4772 else if (arrays
> 1)
4775 return g_variant_get_bytestring_array (value
, NULL
);
4777 return g_variant_dup_bytestring_array (value
, NULL
);
4783 return (gchar
*) g_variant_get_bytestring (value
);
4785 return g_variant_dup_bytestring (value
, NULL
);
4790 g_variant_type_string_scan (*str
, NULL
, str
);
4796 return g_variant_ref (value
);
4799 return g_variant_get_variant (value
);
4802 g_assert_not_reached ();
4808 g_variant_valist_skip_leaf (const gchar
**str
,
4811 if (g_variant_format_string_is_nnp (*str
))
4813 g_variant_format_string_scan (*str
, NULL
, str
);
4814 va_arg (*app
, gpointer
);
4832 va_arg (*app
, guint64
);
4836 va_arg (*app
, gdouble
);
4840 g_assert_not_reached ();
4845 g_variant_valist_new_leaf (const gchar
**str
,
4848 if (g_variant_format_string_is_nnp (*str
))
4849 return g_variant_valist_new_nnp (str
, va_arg (*app
, gpointer
));
4854 return g_variant_new_boolean (va_arg (*app
, gboolean
));
4857 return g_variant_new_byte (va_arg (*app
, guint
));
4860 return g_variant_new_int16 (va_arg (*app
, gint
));
4863 return g_variant_new_uint16 (va_arg (*app
, guint
));
4866 return g_variant_new_int32 (va_arg (*app
, gint
));
4869 return g_variant_new_uint32 (va_arg (*app
, guint
));
4872 return g_variant_new_int64 (va_arg (*app
, gint64
));
4875 return g_variant_new_uint64 (va_arg (*app
, guint64
));
4878 return g_variant_new_handle (va_arg (*app
, gint
));
4881 return g_variant_new_double (va_arg (*app
, gdouble
));
4884 g_assert_not_reached ();
4888 /* The code below assumes this */
4889 G_STATIC_ASSERT (sizeof (gboolean
) == sizeof (guint32
));
4890 G_STATIC_ASSERT (sizeof (gdouble
) == sizeof (guint64
));
4893 g_variant_valist_get_leaf (const gchar
**str
,
4898 gpointer ptr
= va_arg (*app
, gpointer
);
4902 g_variant_format_string_scan (*str
, NULL
, str
);
4906 if (g_variant_format_string_is_nnp (*str
))
4908 gpointer
*nnp
= (gpointer
*) ptr
;
4910 if (free
&& *nnp
!= NULL
)
4911 g_variant_valist_free_nnp (*str
, *nnp
);
4916 *nnp
= g_variant_valist_get_nnp (str
, value
);
4918 g_variant_format_string_scan (*str
, NULL
, str
);
4928 *(gboolean
*) ptr
= g_variant_get_boolean (value
);
4932 *(guchar
*) ptr
= g_variant_get_byte (value
);
4936 *(gint16
*) ptr
= g_variant_get_int16 (value
);
4940 *(guint16
*) ptr
= g_variant_get_uint16 (value
);
4944 *(gint32
*) ptr
= g_variant_get_int32 (value
);
4948 *(guint32
*) ptr
= g_variant_get_uint32 (value
);
4952 *(gint64
*) ptr
= g_variant_get_int64 (value
);
4956 *(guint64
*) ptr
= g_variant_get_uint64 (value
);
4960 *(gint32
*) ptr
= g_variant_get_handle (value
);
4964 *(gdouble
*) ptr
= g_variant_get_double (value
);
4973 *(guchar
*) ptr
= 0;
4978 *(guint16
*) ptr
= 0;
4985 *(guint32
*) ptr
= 0;
4991 *(guint64
*) ptr
= 0;
4996 g_assert_not_reached ();
4999 /* Generic (recursive) {{{2 */
5001 g_variant_valist_skip (const gchar
**str
,
5004 if (g_variant_format_string_is_leaf (*str
))
5005 g_variant_valist_skip_leaf (str
, app
);
5007 else if (**str
== 'm') /* maybe */
5011 if (!g_variant_format_string_is_nnp (*str
))
5012 va_arg (*app
, gboolean
);
5014 g_variant_valist_skip (str
, app
);
5016 else /* tuple, dictionary entry */
5018 g_assert (**str
== '(' || **str
== '{');
5020 while (**str
!= ')' && **str
!= '}')
5021 g_variant_valist_skip (str
, app
);
5027 g_variant_valist_new (const gchar
**str
,
5030 if (g_variant_format_string_is_leaf (*str
))
5031 return g_variant_valist_new_leaf (str
, app
);
5033 if (**str
== 'm') /* maybe */
5035 GVariantType
*type
= NULL
;
5036 GVariant
*value
= NULL
;
5040 if (g_variant_format_string_is_nnp (*str
))
5042 gpointer nnp
= va_arg (*app
, gpointer
);
5045 value
= g_variant_valist_new_nnp (str
, nnp
);
5047 type
= g_variant_format_string_scan_type (*str
, NULL
, str
);
5051 gboolean just
= va_arg (*app
, gboolean
);
5054 value
= g_variant_valist_new (str
, app
);
5057 type
= g_variant_format_string_scan_type (*str
, NULL
, NULL
);
5058 g_variant_valist_skip (str
, app
);
5062 value
= g_variant_new_maybe (type
, value
);
5065 g_variant_type_free (type
);
5069 else /* tuple, dictionary entry */
5074 g_variant_builder_init (&b
, G_VARIANT_TYPE_TUPLE
);
5077 g_assert (**str
== '{');
5078 g_variant_builder_init (&b
, G_VARIANT_TYPE_DICT_ENTRY
);
5082 while (**str
!= ')' && **str
!= '}')
5083 g_variant_builder_add_value (&b
, g_variant_valist_new (str
, app
));
5086 return g_variant_builder_end (&b
);
5091 g_variant_valist_get (const gchar
**str
,
5096 if (g_variant_format_string_is_leaf (*str
))
5097 g_variant_valist_get_leaf (str
, value
, free
, app
);
5099 else if (**str
== 'm')
5104 value
= g_variant_get_maybe (value
);
5106 if (!g_variant_format_string_is_nnp (*str
))
5108 gboolean
*ptr
= va_arg (*app
, gboolean
*);
5111 *ptr
= value
!= NULL
;
5114 g_variant_valist_get (str
, value
, free
, app
);
5117 g_variant_unref (value
);
5120 else /* tuple, dictionary entry */
5124 g_assert (**str
== '(' || **str
== '{');
5127 while (**str
!= ')' && **str
!= '}')
5131 GVariant
*child
= g_variant_get_child_value (value
, index
++);
5132 g_variant_valist_get (str
, child
, free
, app
);
5133 g_variant_unref (child
);
5136 g_variant_valist_get (str
, NULL
, free
, app
);
5142 /* User-facing API {{{2 */
5144 * g_variant_new: (skip)
5145 * @format_string: a #GVariant format string
5146 * @...: arguments, as per @format_string
5148 * Creates a new #GVariant instance.
5150 * Think of this function as an analogue to g_strdup_printf().
5152 * The type of the created instance and the arguments that are expected
5153 * by this function are determined by @format_string. See the section on
5154 * [GVariant format strings][gvariant-format-strings]. Please note that
5155 * the syntax of the format string is very likely to be extended in the
5158 * The first character of the format string must not be '*' '?' '@' or
5159 * 'r'; in essence, a new #GVariant must always be constructed by this
5160 * function (and not merely passed through it unmodified).
5162 * Note that the arguments must be of the correct width for their types
5163 * specified in @format_string. This can be achieved by casting them. See
5164 * the [GVariant varargs documentation][gvariant-varargs].
5166 * |[<!-- language="C" -->
5167 * MyFlags some_flags = FLAG_ONE | FLAG_TWO;
5168 * const gchar *some_strings[] = { "a", "b", "c", NULL };
5169 * GVariant *new_variant;
5171 * new_variant = g_variant_new ("(t^as)",
5172 * /<!-- -->* This cast is required. *<!-- -->/
5173 * (guint64) some_flags,
5177 * Returns: a new floating #GVariant instance
5182 g_variant_new (const gchar
*format_string
,
5188 g_return_val_if_fail (valid_format_string (format_string
, TRUE
, NULL
) &&
5189 format_string
[0] != '?' && format_string
[0] != '@' &&
5190 format_string
[0] != '*' && format_string
[0] != 'r',
5193 va_start (ap
, format_string
);
5194 value
= g_variant_new_va (format_string
, NULL
, &ap
);
5201 * g_variant_new_va: (skip)
5202 * @format_string: a string that is prefixed with a format string
5203 * @endptr: (allow-none) (default NULL): location to store the end pointer,
5205 * @app: a pointer to a #va_list
5207 * This function is intended to be used by libraries based on
5208 * #GVariant that want to provide g_variant_new()-like functionality
5211 * The API is more general than g_variant_new() to allow a wider range
5214 * @format_string must still point to a valid format string, but it only
5215 * needs to be nul-terminated if @endptr is %NULL. If @endptr is
5216 * non-%NULL then it is updated to point to the first character past the
5217 * end of the format string.
5219 * @app is a pointer to a #va_list. The arguments, according to
5220 * @format_string, are collected from this #va_list and the list is left
5221 * pointing to the argument following the last.
5223 * Note that the arguments in @app must be of the correct width for their
5224 * types specified in @format_string when collected into the #va_list.
5225 * See the [GVariant varargs documentation][gvariant-varargs.
5227 * These two generalisations allow mixing of multiple calls to
5228 * g_variant_new_va() and g_variant_get_va() within a single actual
5229 * varargs call by the user.
5231 * The return value will be floating if it was a newly created GVariant
5232 * instance (for example, if the format string was "(ii)"). In the case
5233 * that the format_string was '*', '?', 'r', or a format starting with
5234 * '@' then the collected #GVariant pointer will be returned unmodified,
5235 * without adding any additional references.
5237 * In order to behave correctly in all cases it is necessary for the
5238 * calling function to g_variant_ref_sink() the return result before
5239 * returning control to the user that originally provided the pointer.
5240 * At this point, the caller will have their own full reference to the
5241 * result. This can also be done by adding the result to a container,
5242 * or by passing it to another g_variant_new() call.
5244 * Returns: a new, usually floating, #GVariant
5249 g_variant_new_va (const gchar
*format_string
,
5250 const gchar
**endptr
,
5255 g_return_val_if_fail (valid_format_string (format_string
, !endptr
, NULL
),
5257 g_return_val_if_fail (app
!= NULL
, NULL
);
5259 value
= g_variant_valist_new (&format_string
, app
);
5262 *endptr
= format_string
;
5268 * g_variant_get: (skip)
5269 * @value: a #GVariant instance
5270 * @format_string: a #GVariant format string
5271 * @...: arguments, as per @format_string
5273 * Deconstructs a #GVariant instance.
5275 * Think of this function as an analogue to scanf().
5277 * The arguments that are expected by this function are entirely
5278 * determined by @format_string. @format_string also restricts the
5279 * permissible types of @value. It is an error to give a value with
5280 * an incompatible type. See the section on
5281 * [GVariant format strings][gvariant-format-strings].
5282 * Please note that the syntax of the format string is very likely to be
5283 * extended in the future.
5285 * @format_string determines the C types that are used for unpacking
5286 * the values and also determines if the values are copied or borrowed,
5287 * see the section on
5288 * [GVariant format strings][gvariant-format-strings-pointers].
5293 g_variant_get (GVariant
*value
,
5294 const gchar
*format_string
,
5299 g_return_if_fail (valid_format_string (format_string
, TRUE
, value
));
5301 /* if any direct-pointer-access formats are in use, flatten first */
5302 if (strchr (format_string
, '&'))
5303 g_variant_get_data (value
);
5305 va_start (ap
, format_string
);
5306 g_variant_get_va (value
, format_string
, NULL
, &ap
);
5311 * g_variant_get_va: (skip)
5312 * @value: a #GVariant
5313 * @format_string: a string that is prefixed with a format string
5314 * @endptr: (allow-none) (default NULL): location to store the end pointer,
5316 * @app: a pointer to a #va_list
5318 * This function is intended to be used by libraries based on #GVariant
5319 * that want to provide g_variant_get()-like functionality to their
5322 * The API is more general than g_variant_get() to allow a wider range
5325 * @format_string must still point to a valid format string, but it only
5326 * need to be nul-terminated if @endptr is %NULL. If @endptr is
5327 * non-%NULL then it is updated to point to the first character past the
5328 * end of the format string.
5330 * @app is a pointer to a #va_list. The arguments, according to
5331 * @format_string, are collected from this #va_list and the list is left
5332 * pointing to the argument following the last.
5334 * These two generalisations allow mixing of multiple calls to
5335 * g_variant_new_va() and g_variant_get_va() within a single actual
5336 * varargs call by the user.
5338 * @format_string determines the C types that are used for unpacking
5339 * the values and also determines if the values are copied or borrowed,
5340 * see the section on
5341 * [GVariant format strings][gvariant-format-strings-pointers].
5346 g_variant_get_va (GVariant
*value
,
5347 const gchar
*format_string
,
5348 const gchar
**endptr
,
5351 g_return_if_fail (valid_format_string (format_string
, !endptr
, value
));
5352 g_return_if_fail (value
!= NULL
);
5353 g_return_if_fail (app
!= NULL
);
5355 /* if any direct-pointer-access formats are in use, flatten first */
5356 if (strchr (format_string
, '&'))
5357 g_variant_get_data (value
);
5359 g_variant_valist_get (&format_string
, value
, FALSE
, app
);
5362 *endptr
= format_string
;
5365 /* Varargs-enabled Utility Functions {{{1 */
5368 * g_variant_builder_add: (skip)
5369 * @builder: a #GVariantBuilder
5370 * @format_string: a #GVariant varargs format string
5371 * @...: arguments, as per @format_string
5373 * Adds to a #GVariantBuilder.
5375 * This call is a convenience wrapper that is exactly equivalent to
5376 * calling g_variant_new() followed by g_variant_builder_add_value().
5378 * Note that the arguments must be of the correct width for their types
5379 * specified in @format_string. This can be achieved by casting them. See
5380 * the [GVariant varargs documentation][gvariant-varargs].
5382 * This function might be used as follows:
5384 * |[<!-- language="C" -->
5386 * make_pointless_dictionary (void)
5388 * GVariantBuilder builder;
5391 * g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
5392 * for (i = 0; i < 16; i++)
5396 * sprintf (buf, "%d", i);
5397 * g_variant_builder_add (&builder, "{is}", i, buf);
5400 * return g_variant_builder_end (&builder);
5407 g_variant_builder_add (GVariantBuilder
*builder
,
5408 const gchar
*format_string
,
5414 va_start (ap
, format_string
);
5415 variant
= g_variant_new_va (format_string
, NULL
, &ap
);
5418 g_variant_builder_add_value (builder
, variant
);
5422 * g_variant_get_child: (skip)
5423 * @value: a container #GVariant
5424 * @index_: the index of the child to deconstruct
5425 * @format_string: a #GVariant format string
5426 * @...: arguments, as per @format_string
5428 * Reads a child item out of a container #GVariant instance and
5429 * deconstructs it according to @format_string. This call is
5430 * essentially a combination of g_variant_get_child_value() and
5433 * @format_string determines the C types that are used for unpacking
5434 * the values and also determines if the values are copied or borrowed,
5435 * see the section on
5436 * [GVariant format strings][gvariant-format-strings-pointers].
5441 g_variant_get_child (GVariant
*value
,
5443 const gchar
*format_string
,
5449 /* if any direct-pointer-access formats are in use, flatten first */
5450 if (strchr (format_string
, '&'))
5451 g_variant_get_data (value
);
5453 child
= g_variant_get_child_value (value
, index_
);
5454 g_return_if_fail (valid_format_string (format_string
, TRUE
, child
));
5456 va_start (ap
, format_string
);
5457 g_variant_get_va (child
, format_string
, NULL
, &ap
);
5460 g_variant_unref (child
);
5464 * g_variant_iter_next: (skip)
5465 * @iter: a #GVariantIter
5466 * @format_string: a GVariant format string
5467 * @...: the arguments to unpack the value into
5469 * Gets the next item in the container and unpacks it into the variable
5470 * argument list according to @format_string, returning %TRUE.
5472 * If no more items remain then %FALSE is returned.
5474 * All of the pointers given on the variable arguments list of this
5475 * function are assumed to point at uninitialised memory. It is the
5476 * responsibility of the caller to free all of the values returned by
5477 * the unpacking process.
5479 * Here is an example for memory management with g_variant_iter_next():
5480 * |[<!-- language="C" -->
5481 * // Iterates a dictionary of type 'a{sv}'
5483 * iterate_dictionary (GVariant *dictionary)
5485 * GVariantIter iter;
5489 * g_variant_iter_init (&iter, dictionary);
5490 * while (g_variant_iter_next (&iter, "{sv}", &key, &value))
5492 * g_print ("Item '%s' has type '%s'\n", key,
5493 * g_variant_get_type_string (value));
5495 * // must free data for ourselves
5496 * g_variant_unref (value);
5502 * For a solution that is likely to be more convenient to C programmers
5503 * when dealing with loops, see g_variant_iter_loop().
5505 * @format_string determines the C types that are used for unpacking
5506 * the values and also determines if the values are copied or borrowed.
5508 * See the section on
5509 * [GVariant format strings][gvariant-format-strings-pointers].
5511 * Returns: %TRUE if a value was unpacked, or %FALSE if there as no value
5516 g_variant_iter_next (GVariantIter
*iter
,
5517 const gchar
*format_string
,
5522 value
= g_variant_iter_next_value (iter
);
5524 g_return_val_if_fail (valid_format_string (format_string
, TRUE
, value
),
5531 va_start (ap
, format_string
);
5532 g_variant_valist_get (&format_string
, value
, FALSE
, &ap
);
5535 g_variant_unref (value
);
5538 return value
!= NULL
;
5542 * g_variant_iter_loop: (skip)
5543 * @iter: a #GVariantIter
5544 * @format_string: a GVariant format string
5545 * @...: the arguments to unpack the value into
5547 * Gets the next item in the container and unpacks it into the variable
5548 * argument list according to @format_string, returning %TRUE.
5550 * If no more items remain then %FALSE is returned.
5552 * On the first call to this function, the pointers appearing on the
5553 * variable argument list are assumed to point at uninitialised memory.
5554 * On the second and later calls, it is assumed that the same pointers
5555 * will be given and that they will point to the memory as set by the
5556 * previous call to this function. This allows the previous values to
5557 * be freed, as appropriate.
5559 * This function is intended to be used with a while loop as
5560 * demonstrated in the following example. This function can only be
5561 * used when iterating over an array. It is only valid to call this
5562 * function with a string constant for the format string and the same
5563 * string constant must be used each time. Mixing calls to this
5564 * function and g_variant_iter_next() or g_variant_iter_next_value() on
5565 * the same iterator causes undefined behavior.
5567 * If you break out of a such a while loop using g_variant_iter_loop() then
5568 * you must free or unreference all the unpacked values as you would with
5569 * g_variant_get(). Failure to do so will cause a memory leak.
5571 * Here is an example for memory management with g_variant_iter_loop():
5572 * |[<!-- language="C" -->
5573 * // Iterates a dictionary of type 'a{sv}'
5575 * iterate_dictionary (GVariant *dictionary)
5577 * GVariantIter iter;
5581 * g_variant_iter_init (&iter, dictionary);
5582 * while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
5584 * g_print ("Item '%s' has type '%s'\n", key,
5585 * g_variant_get_type_string (value));
5587 * // no need to free 'key' and 'value' here
5588 * // unless breaking out of this loop
5593 * For most cases you should use g_variant_iter_next().
5595 * This function is really only useful when unpacking into #GVariant or
5596 * #GVariantIter in order to allow you to skip the call to
5597 * g_variant_unref() or g_variant_iter_free().
5599 * For example, if you are only looping over simple integer and string
5600 * types, g_variant_iter_next() is definitely preferred. For string
5601 * types, use the '&' prefix to avoid allocating any memory at all (and
5602 * thereby avoiding the need to free anything as well).
5604 * @format_string determines the C types that are used for unpacking
5605 * the values and also determines if the values are copied or borrowed.
5607 * See the section on
5608 * [GVariant format strings][gvariant-format-strings-pointers].
5610 * Returns: %TRUE if a value was unpacked, or %FALSE if there was no
5616 g_variant_iter_loop (GVariantIter
*iter
,
5617 const gchar
*format_string
,
5620 gboolean first_time
= GVSI(iter
)->loop_format
== NULL
;
5624 g_return_val_if_fail (first_time
||
5625 format_string
== GVSI(iter
)->loop_format
,
5630 TYPE_CHECK (GVSI(iter
)->value
, G_VARIANT_TYPE_ARRAY
, FALSE
);
5631 GVSI(iter
)->loop_format
= format_string
;
5633 if (strchr (format_string
, '&'))
5634 g_variant_get_data (GVSI(iter
)->value
);
5637 value
= g_variant_iter_next_value (iter
);
5639 g_return_val_if_fail (!first_time
||
5640 valid_format_string (format_string
, TRUE
, value
),
5643 va_start (ap
, format_string
);
5644 g_variant_valist_get (&format_string
, value
, !first_time
, &ap
);
5648 g_variant_unref (value
);
5650 return value
!= NULL
;
5653 /* Serialised data {{{1 */
5655 g_variant_deep_copy (GVariant
*value
)
5657 switch (g_variant_classify (value
))
5659 case G_VARIANT_CLASS_MAYBE
:
5660 case G_VARIANT_CLASS_ARRAY
:
5661 case G_VARIANT_CLASS_TUPLE
:
5662 case G_VARIANT_CLASS_DICT_ENTRY
:
5663 case G_VARIANT_CLASS_VARIANT
:
5665 GVariantBuilder builder
;
5669 g_variant_builder_init (&builder
, g_variant_get_type (value
));
5670 g_variant_iter_init (&iter
, value
);
5672 while ((child
= g_variant_iter_next_value (&iter
)))
5674 g_variant_builder_add_value (&builder
, g_variant_deep_copy (child
));
5675 g_variant_unref (child
);
5678 return g_variant_builder_end (&builder
);
5681 case G_VARIANT_CLASS_BOOLEAN
:
5682 return g_variant_new_boolean (g_variant_get_boolean (value
));
5684 case G_VARIANT_CLASS_BYTE
:
5685 return g_variant_new_byte (g_variant_get_byte (value
));
5687 case G_VARIANT_CLASS_INT16
:
5688 return g_variant_new_int16 (g_variant_get_int16 (value
));
5690 case G_VARIANT_CLASS_UINT16
:
5691 return g_variant_new_uint16 (g_variant_get_uint16 (value
));
5693 case G_VARIANT_CLASS_INT32
:
5694 return g_variant_new_int32 (g_variant_get_int32 (value
));
5696 case G_VARIANT_CLASS_UINT32
:
5697 return g_variant_new_uint32 (g_variant_get_uint32 (value
));
5699 case G_VARIANT_CLASS_INT64
:
5700 return g_variant_new_int64 (g_variant_get_int64 (value
));
5702 case G_VARIANT_CLASS_UINT64
:
5703 return g_variant_new_uint64 (g_variant_get_uint64 (value
));
5705 case G_VARIANT_CLASS_HANDLE
:
5706 return g_variant_new_handle (g_variant_get_handle (value
));
5708 case G_VARIANT_CLASS_DOUBLE
:
5709 return g_variant_new_double (g_variant_get_double (value
));
5711 case G_VARIANT_CLASS_STRING
:
5712 return g_variant_new_string (g_variant_get_string (value
, NULL
));
5714 case G_VARIANT_CLASS_OBJECT_PATH
:
5715 return g_variant_new_object_path (g_variant_get_string (value
, NULL
));
5717 case G_VARIANT_CLASS_SIGNATURE
:
5718 return g_variant_new_signature (g_variant_get_string (value
, NULL
));
5721 g_assert_not_reached ();
5725 * g_variant_get_normal_form:
5726 * @value: a #GVariant
5728 * Gets a #GVariant instance that has the same value as @value and is
5729 * trusted to be in normal form.
5731 * If @value is already trusted to be in normal form then a new
5732 * reference to @value is returned.
5734 * If @value is not already trusted, then it is scanned to check if it
5735 * is in normal form. If it is found to be in normal form then it is
5736 * marked as trusted and a new reference to it is returned.
5738 * If @value is found not to be in normal form then a new trusted
5739 * #GVariant is created with the same value as @value.
5741 * It makes sense to call this function if you've received #GVariant
5742 * data from untrusted sources and you want to ensure your serialised
5743 * output is definitely in normal form.
5745 * Returns: (transfer full): a trusted #GVariant
5750 g_variant_get_normal_form (GVariant
*value
)
5754 if (g_variant_is_normal_form (value
))
5755 return g_variant_ref (value
);
5757 trusted
= g_variant_deep_copy (value
);
5758 g_assert (g_variant_is_trusted (trusted
));
5760 return g_variant_ref_sink (trusted
);
5764 * g_variant_byteswap:
5765 * @value: a #GVariant
5767 * Performs a byteswapping operation on the contents of @value. The
5768 * result is that all multi-byte numeric data contained in @value is
5769 * byteswapped. That includes 16, 32, and 64bit signed and unsigned
5770 * integers as well as file handles and double precision floating point
5773 * This function is an identity mapping on any value that does not
5774 * contain multi-byte numeric data. That include strings, booleans,
5775 * bytes and containers containing only these things (recursively).
5777 * The returned value is always in normal form and is marked as trusted.
5779 * Returns: (transfer full): the byteswapped form of @value
5784 g_variant_byteswap (GVariant
*value
)
5786 GVariantTypeInfo
*type_info
;
5790 type_info
= g_variant_get_type_info (value
);
5792 g_variant_type_info_query (type_info
, &alignment
, NULL
);
5795 /* (potentially) contains multi-byte numeric data */
5797 GVariantSerialised serialised
;
5801 trusted
= g_variant_get_normal_form (value
);
5802 serialised
.type_info
= g_variant_get_type_info (trusted
);
5803 serialised
.size
= g_variant_get_size (trusted
);
5804 serialised
.data
= g_malloc (serialised
.size
);
5805 g_variant_store (trusted
, serialised
.data
);
5806 g_variant_unref (trusted
);
5808 g_variant_serialised_byteswap (serialised
);
5810 bytes
= g_bytes_new_take (serialised
.data
, serialised
.size
);
5811 new = g_variant_new_from_bytes (g_variant_get_type (value
), bytes
, TRUE
);
5812 g_bytes_unref (bytes
);
5815 /* contains no multi-byte data */
5818 return g_variant_ref_sink (new);
5822 * g_variant_new_from_data:
5823 * @type: a definite #GVariantType
5824 * @data: (array length=size) (element-type guint8): the serialised data
5825 * @size: the size of @data
5826 * @trusted: %TRUE if @data is definitely in normal form
5827 * @notify: (scope async): function to call when @data is no longer needed
5828 * @user_data: data for @notify
5830 * Creates a new #GVariant instance from serialised data.
5832 * @type is the type of #GVariant instance that will be constructed.
5833 * The interpretation of @data depends on knowing the type.
5835 * @data is not modified by this function and must remain valid with an
5836 * unchanging value until such a time as @notify is called with
5837 * @user_data. If the contents of @data change before that time then
5838 * the result is undefined.
5840 * If @data is trusted to be serialised data in normal form then
5841 * @trusted should be %TRUE. This applies to serialised data created
5842 * within this process or read from a trusted location on the disk (such
5843 * as a file installed in /usr/lib alongside your application). You
5844 * should set trusted to %FALSE if @data is read from the network, a
5845 * file in the user's home directory, etc.
5847 * If @data was not stored in this machine's native endianness, any multi-byte
5848 * numeric values in the returned variant will also be in non-native
5849 * endianness. g_variant_byteswap() can be used to recover the original values.
5851 * @notify will be called with @user_data when @data is no longer
5852 * needed. The exact time of this call is unspecified and might even be
5853 * before this function returns.
5855 * Returns: (transfer none): a new floating #GVariant of type @type
5860 g_variant_new_from_data (const GVariantType
*type
,
5864 GDestroyNotify notify
,
5870 g_return_val_if_fail (g_variant_type_is_definite (type
), NULL
);
5871 g_return_val_if_fail (data
!= NULL
|| size
== 0, NULL
);
5874 bytes
= g_bytes_new_with_free_func (data
, size
, notify
, user_data
);
5876 bytes
= g_bytes_new_static (data
, size
);
5878 value
= g_variant_new_from_bytes (type
, bytes
, trusted
);
5879 g_bytes_unref (bytes
);
5885 /* vim:set foldmethod=marker: */