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.1 of the License, 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 can contain one or more values
44 * along with information about the type of the values.
46 * A #GVariant may contain simple types, like an integer, or a boolean value;
47 * or complex types, like an array of two strings, or a dictionary of key
48 * value pairs. A #GVariant is also immutable: once it's been created neither
49 * its type nor its content can be modified further.
51 * GVariant is useful whenever data needs to be serialized, for example when
52 * sending method parameters in DBus, or when saving settings using GSettings.
54 * When creating a new #GVariant, you pass the data you want to store in it
55 * along with a string representing the type of data you wish to pass to it.
57 * For instance, if you want to create a #GVariant holding an integer value you
60 * |[<!-- language="C" -->
61 * GVariant *v = g_variant_new ("u", 40);
64 * The string "u" in the first argument tells #GVariant that the data passed to
65 * the constructor (40) is going to be an unsigned integer.
67 * More advanced examples of #GVariant in use can be found in documentation for
68 * [GVariant format strings][gvariant-format-strings-pointers].
70 * The range of possible values is determined by the type.
72 * The type system used by #GVariant is #GVariantType.
74 * #GVariant instances always have a type and a value (which are given
75 * at construction time). The type and value of a #GVariant instance
76 * can never change other than by the #GVariant itself being
77 * destroyed. A #GVariant cannot contain a pointer.
79 * #GVariant is reference counted using g_variant_ref() and
80 * g_variant_unref(). #GVariant also has floating reference counts --
81 * see g_variant_ref_sink().
83 * #GVariant is completely threadsafe. A #GVariant instance can be
84 * concurrently accessed in any way from any number of threads without
87 * #GVariant is heavily optimised for dealing with data in serialised
88 * form. It works particularly well with data located in memory-mapped
89 * files. It can perform nearly all deserialisation operations in a
90 * small constant time, usually touching only a single memory page.
91 * Serialised #GVariant data can also be sent over the network.
93 * #GVariant is largely compatible with D-Bus. Almost all types of
94 * #GVariant instances can be sent over D-Bus. See #GVariantType for
95 * exceptions. (However, #GVariant's serialisation format is not the same
96 * as the serialisation format of a D-Bus message body: use #GDBusMessage,
97 * in the gio library, for those.)
99 * For space-efficiency, the #GVariant serialisation format does not
100 * automatically include the variant's length, type or endianness,
101 * which must either be implied from context (such as knowledge that a
102 * particular file format always contains a little-endian
103 * %G_VARIANT_TYPE_VARIANT which occupies the whole length of the file)
104 * or supplied out-of-band (for instance, a length, type and/or endianness
105 * indicator could be placed at the beginning of a file, network message
106 * or network stream).
108 * A #GVariant's size is limited mainly by any lower level operating
109 * system constraints, such as the number of bits in #gsize. For
110 * example, it is reasonable to have a 2GB file mapped into memory
111 * with #GMappedFile, and call g_variant_new_from_data() on it.
113 * For convenience to C programmers, #GVariant features powerful
114 * varargs-based value construction and destruction. This feature is
115 * designed to be embedded in other libraries.
117 * There is a Python-inspired text language for describing #GVariant
118 * values. #GVariant includes a printer for this language and a parser
119 * with type inferencing.
123 * #GVariant tries to be quite efficient with respect to memory use.
124 * This section gives a rough idea of how much memory is used by the
125 * current implementation. The information here is subject to change
128 * The memory allocated by #GVariant can be grouped into 4 broad
129 * purposes: memory for serialised data, memory for the type
130 * information cache, buffer management memory and memory for the
131 * #GVariant structure itself.
133 * ## Serialised Data Memory
135 * This is the memory that is used for storing GVariant data in
136 * serialised form. This is what would be sent over the network or
137 * what would end up on disk, not counting any indicator of the
138 * endianness, or of the length or type of the top-level variant.
140 * The amount of memory required to store a boolean is 1 byte. 16,
141 * 32 and 64 bit integers and double precision floating point numbers
142 * use their "natural" size. Strings (including object path and
143 * signature strings) are stored with a nul terminator, and as such
144 * use the length of the string plus 1 byte.
146 * Maybe types use no space at all to represent the null value and
147 * use the same amount of space (sometimes plus one byte) as the
148 * equivalent non-maybe-typed value to represent the non-null case.
150 * Arrays use the amount of space required to store each of their
151 * members, concatenated. Additionally, if the items stored in an
152 * array are not of a fixed-size (ie: strings, other arrays, etc)
153 * then an additional framing offset is stored for each item. The
154 * size of this offset is either 1, 2 or 4 bytes depending on the
155 * overall size of the container. Additionally, extra padding bytes
156 * are added as required for alignment of child values.
158 * Tuples (including dictionary entries) use the amount of space
159 * required to store each of their members, concatenated, plus one
160 * framing offset (as per arrays) for each non-fixed-sized item in
161 * the tuple, except for the last one. Additionally, extra padding
162 * bytes are added as required for alignment of child values.
164 * Variants use the same amount of space as the item inside of the
165 * variant, plus 1 byte, plus the length of the type string for the
166 * item inside the variant.
168 * As an example, consider a dictionary mapping strings to variants.
169 * In the case that the dictionary is empty, 0 bytes are required for
172 * If we add an item "width" that maps to the int32 value of 500 then
173 * we will use 4 byte to store the int32 (so 6 for the variant
174 * containing it) and 6 bytes for the string. The variant must be
175 * aligned to 8 after the 6 bytes of the string, so that's 2 extra
176 * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
177 * for the dictionary entry. An additional 1 byte is added to the
178 * array as a framing offset making a total of 15 bytes.
180 * If we add another entry, "title" that maps to a nullable string
181 * that happens to have a value of null, then we use 0 bytes for the
182 * null value (and 3 bytes for the variant to contain it along with
183 * its type string) plus 6 bytes for the string. Again, we need 2
184 * padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
186 * We now require extra padding between the two items in the array.
187 * After the 14 bytes of the first item, that's 2 bytes required.
188 * We now require 2 framing offsets for an extra two
189 * bytes. 14 + 2 + 11 + 2 = 29 bytes to encode the entire two-item
192 * ## Type Information Cache
194 * For each GVariant type that currently exists in the program a type
195 * information structure is kept in the type information cache. The
196 * type information structure is required for rapid deserialisation.
198 * Continuing with the above example, if a #GVariant exists with the
199 * type "a{sv}" then a type information struct will exist for
200 * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type
201 * will share the same type information. Additionally, all
202 * single-digit types are stored in read-only static memory and do
203 * not contribute to the writable memory footprint of a program using
206 * Aside from the type information structures stored in read-only
207 * memory, there are two forms of type information. One is used for
208 * container types where there is a single element type: arrays and
209 * maybe types. The other is used for container types where there
210 * are multiple element types: tuples and dictionary entries.
212 * Array type info structures are 6 * sizeof (void *), plus the
213 * memory required to store the type string itself. This means that
214 * on 32-bit systems, the cache entry for "a{sv}" would require 30
215 * bytes of memory (plus malloc overhead).
217 * Tuple type info structures are 6 * sizeof (void *), plus 4 *
218 * sizeof (void *) for each item in the tuple, plus the memory
219 * required to store the type string itself. A 2-item tuple, for
220 * example, would have a type information structure that consumed
221 * writable memory in the size of 14 * sizeof (void *) (plus type
222 * string) This means that on 32-bit systems, the cache entry for
223 * "{sv}" would require 61 bytes of memory (plus malloc overhead).
225 * This means that in total, for our "a{sv}" example, 91 bytes of
226 * type information would be allocated.
228 * The type information cache, additionally, uses a #GHashTable to
229 * store and lookup the cached items and stores a pointer to this
230 * hash table in static storage. The hash table is freed when there
231 * are zero items in the type cache.
233 * Although these sizes may seem large it is important to remember
234 * that a program will probably only have a very small number of
235 * different types of values in it and that only one type information
236 * structure is required for many different values of the same type.
238 * ## Buffer Management Memory
240 * #GVariant uses an internal buffer management structure to deal
241 * with the various different possible sources of serialised data
242 * that it uses. The buffer is responsible for ensuring that the
243 * correct call is made when the data is no longer in use by
244 * #GVariant. This may involve a g_free() or a g_slice_free() or
245 * even g_mapped_file_unref().
247 * One buffer management structure is used for each chunk of
248 * serialised data. The size of the buffer management structure
249 * is 4 * (void *). On 32-bit systems, that's 16 bytes.
251 * ## GVariant structure
253 * The size of a #GVariant structure is 6 * (void *). On 32-bit
254 * systems, that's 24 bytes.
256 * #GVariant structures only exist if they are explicitly created
257 * with API calls. For example, if a #GVariant is constructed out of
258 * serialised data for the example given above (with the dictionary)
259 * then although there are 9 individual values that comprise the
260 * entire dictionary (two keys, two values, two variants containing
261 * the values, two dictionary entries, plus the dictionary itself),
262 * only 1 #GVariant instance exists -- the one referring to the
265 * If calls are made to start accessing the other values then
266 * #GVariant instances will exist for those values only for as long
267 * as they are in use (ie: until you call g_variant_unref()). The
268 * type information is shared. The serialised data and the buffer
269 * management structure for that serialised data is shared by the
274 * To put the entire example together, for our dictionary mapping
275 * strings to variants (with two entries, as given above), we are
276 * using 91 bytes of memory for type information, 29 bytes of memory
277 * for the serialised data, 16 bytes for buffer management and 24
278 * bytes for the #GVariant instance, or a total of 160 bytes, plus
279 * malloc overhead. If we were to use g_variant_get_child_value() to
280 * access the two dictionary entries, we would use an additional 48
281 * bytes. If we were to have other dictionaries of the same type, we
282 * would use more memory for the serialised data and buffer
283 * management for those dictionaries, but the type information would
287 /* definition of GVariant structure is in gvariant-core.c */
289 /* this is a g_return_val_if_fail() for making
290 * sure a (GVariant *) has the required type.
292 #define TYPE_CHECK(value, TYPE, val) \
293 if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \
294 g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, \
295 "g_variant_is_of_type (" #value \
300 /* Numeric Type Constructor/Getters {{{1 */
302 * g_variant_new_from_trusted:
303 * @type: the #GVariantType
304 * @data: the data to use
305 * @size: the size of @data
307 * Constructs a new trusted #GVariant instance from the provided data.
308 * This is used to implement g_variant_new_* for all the basic types.
310 * Returns: a new floating #GVariant
313 g_variant_new_from_trusted (const GVariantType
*type
,
320 bytes
= g_bytes_new (data
, size
);
321 value
= g_variant_new_from_bytes (type
, bytes
, TRUE
);
322 g_bytes_unref (bytes
);
328 * g_variant_new_boolean:
329 * @value: a #gboolean value
331 * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
333 * Returns: (transfer none): a floating reference to a new boolean #GVariant instance
338 g_variant_new_boolean (gboolean value
)
342 return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN
, &v
, 1);
346 * g_variant_get_boolean:
347 * @value: a boolean #GVariant instance
349 * Returns the boolean value of @value.
351 * It is an error to call this function with a @value of any type
352 * other than %G_VARIANT_TYPE_BOOLEAN.
354 * Returns: %TRUE or %FALSE
359 g_variant_get_boolean (GVariant
*value
)
363 TYPE_CHECK (value
, G_VARIANT_TYPE_BOOLEAN
, FALSE
);
365 data
= g_variant_get_data (value
);
367 return data
!= NULL
? *data
!= 0 : FALSE
;
370 /* the constructors and accessors for byte, int{16,32,64}, handles and
371 * doubles all look pretty much exactly the same, so we reduce
374 #define NUMERIC_TYPE(TYPE, type, ctype) \
375 GVariant *g_variant_new_##type (ctype value) { \
376 return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \
377 &value, sizeof value); \
379 ctype g_variant_get_##type (GVariant *value) { \
381 TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \
382 data = g_variant_get_data (value); \
383 return data != NULL ? *data : 0; \
388 * g_variant_new_byte:
389 * @value: a #guint8 value
391 * Creates a new byte #GVariant instance.
393 * Returns: (transfer none): a floating reference to a new byte #GVariant instance
398 * g_variant_get_byte:
399 * @value: a byte #GVariant instance
401 * Returns the byte value of @value.
403 * It is an error to call this function with a @value of any type
404 * other than %G_VARIANT_TYPE_BYTE.
410 NUMERIC_TYPE (BYTE
, byte
, guchar
)
413 * g_variant_new_int16:
414 * @value: a #gint16 value
416 * Creates a new int16 #GVariant instance.
418 * Returns: (transfer none): a floating reference to a new int16 #GVariant instance
423 * g_variant_get_int16:
424 * @value: a int16 #GVariant instance
426 * Returns the 16-bit signed integer value of @value.
428 * It is an error to call this function with a @value of any type
429 * other than %G_VARIANT_TYPE_INT16.
435 NUMERIC_TYPE (INT16
, int16
, gint16
)
438 * g_variant_new_uint16:
439 * @value: a #guint16 value
441 * Creates a new uint16 #GVariant instance.
443 * Returns: (transfer none): a floating reference to a new uint16 #GVariant instance
448 * g_variant_get_uint16:
449 * @value: a uint16 #GVariant instance
451 * Returns the 16-bit unsigned integer value of @value.
453 * It is an error to call this function with a @value of any type
454 * other than %G_VARIANT_TYPE_UINT16.
456 * Returns: a #guint16
460 NUMERIC_TYPE (UINT16
, uint16
, guint16
)
463 * g_variant_new_int32:
464 * @value: a #gint32 value
466 * Creates a new int32 #GVariant instance.
468 * Returns: (transfer none): a floating reference to a new int32 #GVariant instance
473 * g_variant_get_int32:
474 * @value: a int32 #GVariant instance
476 * Returns the 32-bit signed integer value of @value.
478 * It is an error to call this function with a @value of any type
479 * other than %G_VARIANT_TYPE_INT32.
485 NUMERIC_TYPE (INT32
, int32
, gint32
)
488 * g_variant_new_uint32:
489 * @value: a #guint32 value
491 * Creates a new uint32 #GVariant instance.
493 * Returns: (transfer none): a floating reference to a new uint32 #GVariant instance
498 * g_variant_get_uint32:
499 * @value: a uint32 #GVariant instance
501 * Returns the 32-bit unsigned integer value of @value.
503 * It is an error to call this function with a @value of any type
504 * other than %G_VARIANT_TYPE_UINT32.
506 * Returns: a #guint32
510 NUMERIC_TYPE (UINT32
, uint32
, guint32
)
513 * g_variant_new_int64:
514 * @value: a #gint64 value
516 * Creates a new int64 #GVariant instance.
518 * Returns: (transfer none): a floating reference to a new int64 #GVariant instance
523 * g_variant_get_int64:
524 * @value: a int64 #GVariant instance
526 * Returns the 64-bit signed integer value of @value.
528 * It is an error to call this function with a @value of any type
529 * other than %G_VARIANT_TYPE_INT64.
535 NUMERIC_TYPE (INT64
, int64
, gint64
)
538 * g_variant_new_uint64:
539 * @value: a #guint64 value
541 * Creates a new uint64 #GVariant instance.
543 * Returns: (transfer none): a floating reference to a new uint64 #GVariant instance
548 * g_variant_get_uint64:
549 * @value: a uint64 #GVariant instance
551 * Returns the 64-bit unsigned integer value of @value.
553 * It is an error to call this function with a @value of any type
554 * other than %G_VARIANT_TYPE_UINT64.
556 * Returns: a #guint64
560 NUMERIC_TYPE (UINT64
, uint64
, guint64
)
563 * g_variant_new_handle:
564 * @value: a #gint32 value
566 * Creates a new handle #GVariant instance.
568 * By convention, handles are indexes into an array of file descriptors
569 * that are sent alongside a D-Bus message. If you're not interacting
570 * with D-Bus, you probably don't need them.
572 * Returns: (transfer none): a floating reference to a new handle #GVariant instance
577 * g_variant_get_handle:
578 * @value: a handle #GVariant instance
580 * Returns the 32-bit signed integer value of @value.
582 * It is an error to call this function with a @value of any type other
583 * than %G_VARIANT_TYPE_HANDLE.
585 * By convention, handles are indexes into an array of file descriptors
586 * that are sent alongside a D-Bus message. If you're not interacting
587 * with D-Bus, you probably don't need them.
593 NUMERIC_TYPE (HANDLE
, handle
, gint32
)
596 * g_variant_new_double:
597 * @value: a #gdouble floating point value
599 * Creates a new double #GVariant instance.
601 * Returns: (transfer none): a floating reference to a new double #GVariant instance
606 * g_variant_get_double:
607 * @value: a double #GVariant instance
609 * Returns the double precision floating point value of @value.
611 * It is an error to call this function with a @value of any type
612 * other than %G_VARIANT_TYPE_DOUBLE.
614 * Returns: a #gdouble
618 NUMERIC_TYPE (DOUBLE
, double, gdouble
)
620 /* Container type Constructor / Deconstructors {{{1 */
622 * g_variant_new_maybe:
623 * @child_type: (nullable): the #GVariantType of the child, or %NULL
624 * @child: (nullable): the child value, or %NULL
626 * Depending on if @child is %NULL, either wraps @child inside of a
627 * maybe container or creates a Nothing instance for the given @type.
629 * At least one of @child_type and @child must be non-%NULL.
630 * If @child_type is non-%NULL then it must be a definite type.
631 * If they are both non-%NULL then @child_type must be the type
634 * If @child is a floating reference (see g_variant_ref_sink()), the new
635 * instance takes ownership of @child.
637 * Returns: (transfer none): a floating reference to a new #GVariant maybe instance
642 g_variant_new_maybe (const GVariantType
*child_type
,
645 GVariantType
*maybe_type
;
648 g_return_val_if_fail (child_type
== NULL
|| g_variant_type_is_definite
650 g_return_val_if_fail (child_type
!= NULL
|| child
!= NULL
, NULL
);
651 g_return_val_if_fail (child_type
== NULL
|| child
== NULL
||
652 g_variant_is_of_type (child
, child_type
),
655 if (child_type
== NULL
)
656 child_type
= g_variant_get_type (child
);
658 maybe_type
= g_variant_type_new_maybe (child_type
);
665 children
= g_new (GVariant
*, 1);
666 children
[0] = g_variant_ref_sink (child
);
667 trusted
= g_variant_is_trusted (children
[0]);
669 value
= g_variant_new_from_children (maybe_type
, children
, 1, trusted
);
672 value
= g_variant_new_from_children (maybe_type
, NULL
, 0, TRUE
);
674 g_variant_type_free (maybe_type
);
680 * g_variant_get_maybe:
681 * @value: a maybe-typed value
683 * Given a maybe-typed #GVariant instance, extract its value. If the
684 * value is Nothing, then this function returns %NULL.
686 * Returns: (nullable) (transfer full): the contents of @value, or %NULL
691 g_variant_get_maybe (GVariant
*value
)
693 TYPE_CHECK (value
, G_VARIANT_TYPE_MAYBE
, NULL
);
695 if (g_variant_n_children (value
))
696 return g_variant_get_child_value (value
, 0);
702 * g_variant_new_variant: (constructor)
703 * @value: a #GVariant instance
705 * Boxes @value. The result is a #GVariant instance representing a
706 * variant containing the original value.
708 * If @child is a floating reference (see g_variant_ref_sink()), the new
709 * instance takes ownership of @child.
711 * Returns: (transfer none): a floating reference to a new variant #GVariant instance
716 g_variant_new_variant (GVariant
*value
)
718 g_return_val_if_fail (value
!= NULL
, NULL
);
720 g_variant_ref_sink (value
);
722 return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT
,
723 g_memdup (&value
, sizeof value
),
724 1, g_variant_is_trusted (value
));
728 * g_variant_get_variant:
729 * @value: a variant #GVariant instance
731 * Unboxes @value. The result is the #GVariant instance that was
732 * contained in @value.
734 * Returns: (transfer full): the item contained in the variant
739 g_variant_get_variant (GVariant
*value
)
741 TYPE_CHECK (value
, G_VARIANT_TYPE_VARIANT
, NULL
);
743 return g_variant_get_child_value (value
, 0);
747 * g_variant_new_array:
748 * @child_type: (nullable): the element type of the new array
749 * @children: (nullable) (array length=n_children): an array of
750 * #GVariant pointers, the children
751 * @n_children: the length of @children
753 * Creates a new #GVariant array from @children.
755 * @child_type must be non-%NULL if @n_children is zero. Otherwise, the
756 * child type is determined by inspecting the first element of the
757 * @children array. If @child_type is non-%NULL then it must be a
760 * The items of the array are taken from the @children array. No entry
761 * in the @children array may be %NULL.
763 * All items in the array must have the same type, which must be the
764 * same as @child_type, if given.
766 * If the @children are floating references (see g_variant_ref_sink()), the
767 * new instance takes ownership of them as if via g_variant_ref_sink().
769 * Returns: (transfer none): a floating reference to a new #GVariant array
774 g_variant_new_array (const GVariantType
*child_type
,
775 GVariant
* const *children
,
778 GVariantType
*array_type
;
779 GVariant
**my_children
;
784 g_return_val_if_fail (n_children
> 0 || child_type
!= NULL
, NULL
);
785 g_return_val_if_fail (n_children
== 0 || children
!= NULL
, NULL
);
786 g_return_val_if_fail (child_type
== NULL
||
787 g_variant_type_is_definite (child_type
), NULL
);
789 my_children
= g_new (GVariant
*, n_children
);
792 if (child_type
== NULL
)
793 child_type
= g_variant_get_type (children
[0]);
794 array_type
= g_variant_type_new_array (child_type
);
796 for (i
= 0; i
< n_children
; i
++)
798 TYPE_CHECK (children
[i
], child_type
, NULL
);
799 my_children
[i
] = g_variant_ref_sink (children
[i
]);
800 trusted
&= g_variant_is_trusted (children
[i
]);
803 value
= g_variant_new_from_children (array_type
, my_children
,
804 n_children
, trusted
);
805 g_variant_type_free (array_type
);
811 * g_variant_make_tuple_type:
812 * @children: (array length=n_children): an array of GVariant *
813 * @n_children: the length of @children
815 * Return the type of a tuple containing @children as its items.
817 static GVariantType
*
818 g_variant_make_tuple_type (GVariant
* const *children
,
821 const GVariantType
**types
;
825 types
= g_new (const GVariantType
*, n_children
);
827 for (i
= 0; i
< n_children
; i
++)
828 types
[i
] = g_variant_get_type (children
[i
]);
830 type
= g_variant_type_new_tuple (types
, n_children
);
837 * g_variant_new_tuple:
838 * @children: (array length=n_children): the items to make the tuple out of
839 * @n_children: the length of @children
841 * Creates a new tuple #GVariant out of the items in @children. The
842 * type is determined from the types of @children. No entry in the
843 * @children array may be %NULL.
845 * If @n_children is 0 then the unit tuple is constructed.
847 * If the @children are floating references (see g_variant_ref_sink()), the
848 * new instance takes ownership of them as if via g_variant_ref_sink().
850 * Returns: (transfer none): a floating reference to a new #GVariant tuple
855 g_variant_new_tuple (GVariant
* const *children
,
858 GVariantType
*tuple_type
;
859 GVariant
**my_children
;
864 g_return_val_if_fail (n_children
== 0 || children
!= NULL
, NULL
);
866 my_children
= g_new (GVariant
*, n_children
);
869 for (i
= 0; i
< n_children
; i
++)
871 my_children
[i
] = g_variant_ref_sink (children
[i
]);
872 trusted
&= g_variant_is_trusted (children
[i
]);
875 tuple_type
= g_variant_make_tuple_type (children
, n_children
);
876 value
= g_variant_new_from_children (tuple_type
, my_children
,
877 n_children
, trusted
);
878 g_variant_type_free (tuple_type
);
884 * g_variant_make_dict_entry_type:
885 * @key: a #GVariant, the key
886 * @val: a #GVariant, the value
888 * Return the type of a dictionary entry containing @key and @val as its
891 static GVariantType
*
892 g_variant_make_dict_entry_type (GVariant
*key
,
895 return g_variant_type_new_dict_entry (g_variant_get_type (key
),
896 g_variant_get_type (val
));
900 * g_variant_new_dict_entry: (constructor)
901 * @key: a basic #GVariant, the key
902 * @value: a #GVariant, the value
904 * Creates a new dictionary entry #GVariant. @key and @value must be
905 * non-%NULL. @key must be a value of a basic type (ie: not a container).
907 * If the @key or @value are floating references (see g_variant_ref_sink()),
908 * the new instance takes ownership of them as if via g_variant_ref_sink().
910 * Returns: (transfer none): a floating reference to a new dictionary entry #GVariant
915 g_variant_new_dict_entry (GVariant
*key
,
918 GVariantType
*dict_type
;
922 g_return_val_if_fail (key
!= NULL
&& value
!= NULL
, NULL
);
923 g_return_val_if_fail (!g_variant_is_container (key
), NULL
);
925 children
= g_new (GVariant
*, 2);
926 children
[0] = g_variant_ref_sink (key
);
927 children
[1] = g_variant_ref_sink (value
);
928 trusted
= g_variant_is_trusted (key
) && g_variant_is_trusted (value
);
930 dict_type
= g_variant_make_dict_entry_type (key
, value
);
931 value
= g_variant_new_from_children (dict_type
, children
, 2, trusted
);
932 g_variant_type_free (dict_type
);
938 * g_variant_lookup: (skip)
939 * @dictionary: a dictionary #GVariant
940 * @key: the key to lookup in the dictionary
941 * @format_string: a GVariant format string
942 * @...: the arguments to unpack the value into
944 * Looks up a value in a dictionary #GVariant.
946 * This function is a wrapper around g_variant_lookup_value() and
947 * g_variant_get(). In the case that %NULL would have been returned,
948 * this function returns %FALSE. Otherwise, it unpacks the returned
949 * value and returns %TRUE.
951 * @format_string determines the C types that are used for unpacking
952 * the values and also determines if the values are copied or borrowed,
954 * [GVariant format strings][gvariant-format-strings-pointers].
956 * This function is currently implemented with a linear scan. If you
957 * plan to do many lookups then #GVariantDict may be more efficient.
959 * Returns: %TRUE if a value was unpacked
964 g_variant_lookup (GVariant
*dictionary
,
966 const gchar
*format_string
,
973 g_variant_get_data (dictionary
);
975 type
= g_variant_format_string_scan_type (format_string
, NULL
, NULL
);
976 value
= g_variant_lookup_value (dictionary
, key
, type
);
977 g_variant_type_free (type
);
983 va_start (ap
, format_string
);
984 g_variant_get_va (value
, format_string
, NULL
, &ap
);
985 g_variant_unref (value
);
996 * g_variant_lookup_value:
997 * @dictionary: a dictionary #GVariant
998 * @key: the key to lookup in the dictionary
999 * @expected_type: (nullable): a #GVariantType, or %NULL
1001 * Looks up a value in a dictionary #GVariant.
1003 * This function works with dictionaries of the type a{s*} (and equally
1004 * well with type a{o*}, but we only further discuss the string case
1005 * for sake of clarity).
1007 * In the event that @dictionary has the type a{sv}, the @expected_type
1008 * string specifies what type of value is expected to be inside of the
1009 * variant. If the value inside the variant has a different type then
1010 * %NULL is returned. In the event that @dictionary has a value type other
1011 * than v then @expected_type must directly match the key type and it is
1012 * used to unpack the value directly or an error occurs.
1014 * In either case, if @key is not found in @dictionary, %NULL is returned.
1016 * If the key is found and the value has the correct type, it is
1017 * returned. If @expected_type was specified then any non-%NULL return
1018 * value will have this type.
1020 * This function is currently implemented with a linear scan. If you
1021 * plan to do many lookups then #GVariantDict may be more efficient.
1023 * Returns: (transfer full): the value of the dictionary key, or %NULL
1028 g_variant_lookup_value (GVariant
*dictionary
,
1030 const GVariantType
*expected_type
)
1036 g_return_val_if_fail (g_variant_is_of_type (dictionary
,
1037 G_VARIANT_TYPE ("a{s*}")) ||
1038 g_variant_is_of_type (dictionary
,
1039 G_VARIANT_TYPE ("a{o*}")),
1042 g_variant_iter_init (&iter
, dictionary
);
1044 while ((entry
= g_variant_iter_next_value (&iter
)))
1046 GVariant
*entry_key
;
1049 entry_key
= g_variant_get_child_value (entry
, 0);
1050 matches
= strcmp (g_variant_get_string (entry_key
, NULL
), key
) == 0;
1051 g_variant_unref (entry_key
);
1056 g_variant_unref (entry
);
1062 value
= g_variant_get_child_value (entry
, 1);
1063 g_variant_unref (entry
);
1065 if (g_variant_is_of_type (value
, G_VARIANT_TYPE_VARIANT
))
1069 tmp
= g_variant_get_variant (value
);
1070 g_variant_unref (value
);
1072 if (expected_type
&& !g_variant_is_of_type (tmp
, expected_type
))
1074 g_variant_unref (tmp
);
1081 g_return_val_if_fail (expected_type
== NULL
|| value
== NULL
||
1082 g_variant_is_of_type (value
, expected_type
), NULL
);
1088 * g_variant_get_fixed_array:
1089 * @value: a #GVariant array with fixed-sized elements
1090 * @n_elements: (out): a pointer to the location to store the number of items
1091 * @element_size: the size of each element
1093 * Provides access to the serialised data for an array of fixed-sized
1096 * @value must be an array with fixed-sized elements. Numeric types are
1097 * fixed-size, as are tuples containing only other fixed-sized types.
1099 * @element_size must be the size of a single element in the array,
1100 * as given by the section on
1101 * [serialized data memory][gvariant-serialised-data-memory].
1103 * In particular, arrays of these fixed-sized types can be interpreted
1104 * as an array of the given C type, with @element_size set to the size
1105 * the appropriate type:
1106 * - %G_VARIANT_TYPE_INT16 (etc.): #gint16 (etc.)
1107 * - %G_VARIANT_TYPE_BOOLEAN: #guchar (not #gboolean!)
1108 * - %G_VARIANT_TYPE_BYTE: #guchar
1109 * - %G_VARIANT_TYPE_HANDLE: #guint32
1110 * - %G_VARIANT_TYPE_DOUBLE: #gdouble
1112 * For example, if calling this function for an array of 32-bit integers,
1113 * you might say `sizeof(gint32)`. This value isn't used except for the purpose
1114 * of a double-check that the form of the serialised data matches the caller's
1117 * @n_elements, which must be non-%NULL, is set equal to the number of
1118 * items in the array.
1120 * Returns: (array length=n_elements) (transfer none): a pointer to
1126 g_variant_get_fixed_array (GVariant
*value
,
1130 GVariantTypeInfo
*array_info
;
1131 gsize array_element_size
;
1135 TYPE_CHECK (value
, G_VARIANT_TYPE_ARRAY
, NULL
);
1137 g_return_val_if_fail (n_elements
!= NULL
, NULL
);
1138 g_return_val_if_fail (element_size
> 0, NULL
);
1140 array_info
= g_variant_get_type_info (value
);
1141 g_variant_type_info_query_element (array_info
, NULL
, &array_element_size
);
1143 g_return_val_if_fail (array_element_size
, NULL
);
1145 if G_UNLIKELY (array_element_size
!= element_size
)
1147 if (array_element_size
)
1148 g_critical ("g_variant_get_fixed_array: assertion "
1149 "'g_variant_array_has_fixed_size (value, element_size)' "
1150 "failed: array size %"G_GSIZE_FORMAT
" does not match "
1151 "given element_size %"G_GSIZE_FORMAT
".",
1152 array_element_size
, element_size
);
1154 g_critical ("g_variant_get_fixed_array: assertion "
1155 "'g_variant_array_has_fixed_size (value, element_size)' "
1156 "failed: array does not have fixed size.");
1159 data
= g_variant_get_data (value
);
1160 size
= g_variant_get_size (value
);
1162 if (size
% element_size
)
1165 *n_elements
= size
/ element_size
;
1174 * g_variant_new_fixed_array:
1175 * @element_type: the #GVariantType of each element
1176 * @elements: a pointer to the fixed array of contiguous elements
1177 * @n_elements: the number of elements
1178 * @element_size: the size of each element
1180 * Constructs a new array #GVariant instance, where the elements are
1181 * of @element_type type.
1183 * @elements must be an array with fixed-sized elements. Numeric types are
1184 * fixed-size as are tuples containing only other fixed-sized types.
1186 * @element_size must be the size of a single element in the array.
1187 * For example, if calling this function for an array of 32-bit integers,
1188 * you might say sizeof(gint32). This value isn't used except for the purpose
1189 * of a double-check that the form of the serialised data matches the caller's
1192 * @n_elements must be the length of the @elements array.
1194 * Returns: (transfer none): a floating reference to a new array #GVariant instance
1199 g_variant_new_fixed_array (const GVariantType
*element_type
,
1200 gconstpointer elements
,
1204 GVariantType
*array_type
;
1205 gsize array_element_size
;
1206 GVariantTypeInfo
*array_info
;
1210 g_return_val_if_fail (g_variant_type_is_definite (element_type
), NULL
);
1211 g_return_val_if_fail (element_size
> 0, NULL
);
1213 array_type
= g_variant_type_new_array (element_type
);
1214 array_info
= g_variant_type_info_get (array_type
);
1215 g_variant_type_info_query_element (array_info
, NULL
, &array_element_size
);
1216 if G_UNLIKELY (array_element_size
!= element_size
)
1218 if (array_element_size
)
1219 g_critical ("g_variant_new_fixed_array: array size %" G_GSIZE_FORMAT
1220 " does not match given element_size %" G_GSIZE_FORMAT
".",
1221 array_element_size
, element_size
);
1223 g_critical ("g_variant_get_fixed_array: array does not have fixed size.");
1227 data
= g_memdup (elements
, n_elements
* element_size
);
1228 value
= g_variant_new_from_data (array_type
, data
,
1229 n_elements
* element_size
,
1230 FALSE
, g_free
, data
);
1232 g_variant_type_free (array_type
);
1233 g_variant_type_info_unref (array_info
);
1238 /* String type constructor/getters/validation {{{1 */
1240 * g_variant_new_string:
1241 * @string: a normal UTF-8 nul-terminated string
1243 * Creates a string #GVariant with the contents of @string.
1245 * @string must be valid UTF-8, and must not be %NULL. To encode
1246 * potentially-%NULL strings, use g_variant_new() with `ms` as the
1247 * [format string][gvariant-format-strings-maybe-types].
1249 * Returns: (transfer none): a floating reference to a new string #GVariant instance
1254 g_variant_new_string (const gchar
*string
)
1256 g_return_val_if_fail (string
!= NULL
, NULL
);
1257 g_return_val_if_fail (g_utf8_validate (string
, -1, NULL
), NULL
);
1259 return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING
,
1260 string
, strlen (string
) + 1);
1264 * g_variant_new_take_string: (skip)
1265 * @string: a normal UTF-8 nul-terminated string
1267 * Creates a string #GVariant with the contents of @string.
1269 * @string must be valid UTF-8, and must not be %NULL. To encode
1270 * potentially-%NULL strings, use this with g_variant_new_maybe().
1272 * This function consumes @string. g_free() will be called on @string
1273 * when it is no longer required.
1275 * You must not modify or access @string in any other way after passing
1276 * it to this function. It is even possible that @string is immediately
1279 * Returns: (transfer none): a floating reference to a new string
1280 * #GVariant instance
1285 g_variant_new_take_string (gchar
*string
)
1290 g_return_val_if_fail (string
!= NULL
, NULL
);
1291 g_return_val_if_fail (g_utf8_validate (string
, -1, NULL
), NULL
);
1293 bytes
= g_bytes_new_take (string
, strlen (string
) + 1);
1294 value
= g_variant_new_from_bytes (G_VARIANT_TYPE_STRING
, bytes
, TRUE
);
1295 g_bytes_unref (bytes
);
1301 * g_variant_new_printf: (skip)
1302 * @format_string: a printf-style format string
1303 * @...: arguments for @format_string
1305 * Creates a string-type GVariant using printf formatting.
1307 * This is similar to calling g_strdup_printf() and then
1308 * g_variant_new_string() but it saves a temporary variable and an
1311 * Returns: (transfer none): a floating reference to a new string
1312 * #GVariant instance
1317 g_variant_new_printf (const gchar
*format_string
,
1325 g_return_val_if_fail (format_string
!= NULL
, NULL
);
1327 va_start (ap
, format_string
);
1328 string
= g_strdup_vprintf (format_string
, ap
);
1331 bytes
= g_bytes_new_take (string
, strlen (string
) + 1);
1332 value
= g_variant_new_from_bytes (G_VARIANT_TYPE_STRING
, bytes
, TRUE
);
1333 g_bytes_unref (bytes
);
1339 * g_variant_new_object_path:
1340 * @object_path: a normal C nul-terminated string
1342 * Creates a D-Bus object path #GVariant with the contents of @string.
1343 * @string must be a valid D-Bus object path. Use
1344 * g_variant_is_object_path() if you're not sure.
1346 * Returns: (transfer none): a floating reference to a new object path #GVariant instance
1351 g_variant_new_object_path (const gchar
*object_path
)
1353 g_return_val_if_fail (g_variant_is_object_path (object_path
), NULL
);
1355 return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH
,
1356 object_path
, strlen (object_path
) + 1);
1360 * g_variant_is_object_path:
1361 * @string: a normal C nul-terminated string
1363 * Determines if a given string is a valid D-Bus object path. You
1364 * should ensure that a string is a valid D-Bus object path before
1365 * passing it to g_variant_new_object_path().
1367 * A valid object path starts with '/' followed by zero or more
1368 * sequences of characters separated by '/' characters. Each sequence
1369 * must contain only the characters "[A-Z][a-z][0-9]_". No sequence
1370 * (including the one following the final '/' character) may be empty.
1372 * Returns: %TRUE if @string is a D-Bus object path
1377 g_variant_is_object_path (const gchar
*string
)
1379 g_return_val_if_fail (string
!= NULL
, FALSE
);
1381 return g_variant_serialiser_is_object_path (string
, strlen (string
) + 1);
1385 * g_variant_new_signature:
1386 * @signature: a normal C nul-terminated string
1388 * Creates a D-Bus type signature #GVariant with the contents of
1389 * @string. @string must be a valid D-Bus type signature. Use
1390 * g_variant_is_signature() if you're not sure.
1392 * Returns: (transfer none): a floating reference to a new signature #GVariant instance
1397 g_variant_new_signature (const gchar
*signature
)
1399 g_return_val_if_fail (g_variant_is_signature (signature
), NULL
);
1401 return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE
,
1402 signature
, strlen (signature
) + 1);
1406 * g_variant_is_signature:
1407 * @string: a normal C nul-terminated string
1409 * Determines if a given string is a valid D-Bus type signature. You
1410 * should ensure that a string is a valid D-Bus type signature before
1411 * passing it to g_variant_new_signature().
1413 * D-Bus type signatures consist of zero or more definite #GVariantType
1414 * strings in sequence.
1416 * Returns: %TRUE if @string is a D-Bus type signature
1421 g_variant_is_signature (const gchar
*string
)
1423 g_return_val_if_fail (string
!= NULL
, FALSE
);
1425 return g_variant_serialiser_is_signature (string
, strlen (string
) + 1);
1429 * g_variant_get_string:
1430 * @value: a string #GVariant instance
1431 * @length: (optional) (default 0) (out): a pointer to a #gsize,
1432 * to store the length
1434 * Returns the string value of a #GVariant instance with a string
1435 * type. This includes the types %G_VARIANT_TYPE_STRING,
1436 * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
1438 * The string will always be UTF-8 encoded, and will never be %NULL.
1440 * If @length is non-%NULL then the length of the string (in bytes) is
1441 * returned there. For trusted values, this information is already
1442 * known. For untrusted values, a strlen() will be performed.
1444 * It is an error to call this function with a @value of any type
1445 * other than those three.
1447 * The return value remains valid as long as @value exists.
1449 * Returns: (transfer none): the constant string, UTF-8 encoded
1454 g_variant_get_string (GVariant
*value
,
1460 g_return_val_if_fail (value
!= NULL
, NULL
);
1461 g_return_val_if_fail (
1462 g_variant_is_of_type (value
, G_VARIANT_TYPE_STRING
) ||
1463 g_variant_is_of_type (value
, G_VARIANT_TYPE_OBJECT_PATH
) ||
1464 g_variant_is_of_type (value
, G_VARIANT_TYPE_SIGNATURE
), NULL
);
1466 data
= g_variant_get_data (value
);
1467 size
= g_variant_get_size (value
);
1469 if (!g_variant_is_trusted (value
))
1471 switch (g_variant_classify (value
))
1473 case G_VARIANT_CLASS_STRING
:
1474 if (g_variant_serialiser_is_string (data
, size
))
1481 case G_VARIANT_CLASS_OBJECT_PATH
:
1482 if (g_variant_serialiser_is_object_path (data
, size
))
1489 case G_VARIANT_CLASS_SIGNATURE
:
1490 if (g_variant_serialiser_is_signature (data
, size
))
1498 g_assert_not_reached ();
1509 * g_variant_dup_string:
1510 * @value: a string #GVariant instance
1511 * @length: (out): a pointer to a #gsize, to store the length
1513 * Similar to g_variant_get_string() except that instead of returning
1514 * a constant string, the string is duplicated.
1516 * The string will always be UTF-8 encoded.
1518 * The return value must be freed using g_free().
1520 * Returns: (transfer full): a newly allocated string, UTF-8 encoded
1525 g_variant_dup_string (GVariant
*value
,
1528 return g_strdup (g_variant_get_string (value
, length
));
1532 * g_variant_new_strv:
1533 * @strv: (array length=length) (element-type utf8): an array of strings
1534 * @length: the length of @strv, or -1
1536 * Constructs an array of strings #GVariant from the given array of
1539 * If @length is -1 then @strv is %NULL-terminated.
1541 * Returns: (transfer none): a new floating #GVariant instance
1546 g_variant_new_strv (const gchar
* const *strv
,
1552 g_return_val_if_fail (length
== 0 || strv
!= NULL
, NULL
);
1555 length
= g_strv_length ((gchar
**) strv
);
1557 strings
= g_new (GVariant
*, length
);
1558 for (i
= 0; i
< length
; i
++)
1559 strings
[i
] = g_variant_ref_sink (g_variant_new_string (strv
[i
]));
1561 return g_variant_new_from_children (G_VARIANT_TYPE_STRING_ARRAY
,
1562 strings
, length
, TRUE
);
1566 * g_variant_get_strv:
1567 * @value: an array of strings #GVariant
1568 * @length: (out) (optional): the length of the result, or %NULL
1570 * Gets the contents of an array of strings #GVariant. This call
1571 * makes a shallow copy; the return result should be released with
1572 * g_free(), but the individual strings must not be modified.
1574 * If @length is non-%NULL then the number of elements in the result
1575 * is stored there. In any case, the resulting array will be
1578 * For an empty array, @length will be set to 0 and a pointer to a
1579 * %NULL pointer will be returned.
1581 * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings
1586 g_variant_get_strv (GVariant
*value
,
1593 TYPE_CHECK (value
, G_VARIANT_TYPE_STRING_ARRAY
, NULL
);
1595 g_variant_get_data (value
);
1596 n
= g_variant_n_children (value
);
1597 strv
= g_new (const gchar
*, n
+ 1);
1599 for (i
= 0; i
< n
; i
++)
1603 string
= g_variant_get_child_value (value
, i
);
1604 strv
[i
] = g_variant_get_string (string
, NULL
);
1605 g_variant_unref (string
);
1616 * g_variant_dup_strv:
1617 * @value: an array of strings #GVariant
1618 * @length: (out) (optional): the length of the result, or %NULL
1620 * Gets the contents of an array of strings #GVariant. This call
1621 * makes a deep copy; the return result should be released with
1624 * If @length is non-%NULL then the number of elements in the result
1625 * is stored there. In any case, the resulting array will be
1628 * For an empty array, @length will be set to 0 and a pointer to a
1629 * %NULL pointer will be returned.
1631 * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1636 g_variant_dup_strv (GVariant
*value
,
1643 TYPE_CHECK (value
, G_VARIANT_TYPE_STRING_ARRAY
, NULL
);
1645 n
= g_variant_n_children (value
);
1646 strv
= g_new (gchar
*, n
+ 1);
1648 for (i
= 0; i
< n
; i
++)
1652 string
= g_variant_get_child_value (value
, i
);
1653 strv
[i
] = g_variant_dup_string (string
, NULL
);
1654 g_variant_unref (string
);
1665 * g_variant_new_objv:
1666 * @strv: (array length=length) (element-type utf8): an array of strings
1667 * @length: the length of @strv, or -1
1669 * Constructs an array of object paths #GVariant from the given array of
1672 * Each string must be a valid #GVariant object path; see
1673 * g_variant_is_object_path().
1675 * If @length is -1 then @strv is %NULL-terminated.
1677 * Returns: (transfer none): a new floating #GVariant instance
1682 g_variant_new_objv (const gchar
* const *strv
,
1688 g_return_val_if_fail (length
== 0 || strv
!= NULL
, NULL
);
1691 length
= g_strv_length ((gchar
**) strv
);
1693 strings
= g_new (GVariant
*, length
);
1694 for (i
= 0; i
< length
; i
++)
1695 strings
[i
] = g_variant_ref_sink (g_variant_new_object_path (strv
[i
]));
1697 return g_variant_new_from_children (G_VARIANT_TYPE_OBJECT_PATH_ARRAY
,
1698 strings
, length
, TRUE
);
1702 * g_variant_get_objv:
1703 * @value: an array of object paths #GVariant
1704 * @length: (out) (optional): the length of the result, or %NULL
1706 * Gets the contents of an array of object paths #GVariant. This call
1707 * makes a shallow copy; the return result should be released with
1708 * g_free(), but the individual strings must not be modified.
1710 * If @length is non-%NULL then the number of elements in the result
1711 * is stored there. In any case, the resulting array will be
1714 * For an empty array, @length will be set to 0 and a pointer to a
1715 * %NULL pointer will be returned.
1717 * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings
1722 g_variant_get_objv (GVariant
*value
,
1729 TYPE_CHECK (value
, G_VARIANT_TYPE_OBJECT_PATH_ARRAY
, NULL
);
1731 g_variant_get_data (value
);
1732 n
= g_variant_n_children (value
);
1733 strv
= g_new (const gchar
*, n
+ 1);
1735 for (i
= 0; i
< n
; i
++)
1739 string
= g_variant_get_child_value (value
, i
);
1740 strv
[i
] = g_variant_get_string (string
, NULL
);
1741 g_variant_unref (string
);
1752 * g_variant_dup_objv:
1753 * @value: an array of object paths #GVariant
1754 * @length: (out) (optional): the length of the result, or %NULL
1756 * Gets the contents of an array of object paths #GVariant. This call
1757 * makes a deep copy; the return result should be released with
1760 * If @length is non-%NULL then the number of elements in the result
1761 * is stored there. In any case, the resulting array will be
1764 * For an empty array, @length will be set to 0 and a pointer to a
1765 * %NULL pointer will be returned.
1767 * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1772 g_variant_dup_objv (GVariant
*value
,
1779 TYPE_CHECK (value
, G_VARIANT_TYPE_OBJECT_PATH_ARRAY
, NULL
);
1781 n
= g_variant_n_children (value
);
1782 strv
= g_new (gchar
*, n
+ 1);
1784 for (i
= 0; i
< n
; i
++)
1788 string
= g_variant_get_child_value (value
, i
);
1789 strv
[i
] = g_variant_dup_string (string
, NULL
);
1790 g_variant_unref (string
);
1802 * g_variant_new_bytestring:
1803 * @string: (array zero-terminated=1) (element-type guint8): a normal
1804 * nul-terminated string in no particular encoding
1806 * Creates an array-of-bytes #GVariant with the contents of @string.
1807 * This function is just like g_variant_new_string() except that the
1808 * string need not be valid UTF-8.
1810 * The nul terminator character at the end of the string is stored in
1813 * Returns: (transfer none): a floating reference to a new bytestring #GVariant instance
1818 g_variant_new_bytestring (const gchar
*string
)
1820 g_return_val_if_fail (string
!= NULL
, NULL
);
1822 return g_variant_new_from_trusted (G_VARIANT_TYPE_BYTESTRING
,
1823 string
, strlen (string
) + 1);
1827 * g_variant_get_bytestring:
1828 * @value: an array-of-bytes #GVariant instance
1830 * Returns the string value of a #GVariant instance with an
1831 * array-of-bytes type. The string has no particular encoding.
1833 * If the array does not end with a nul terminator character, the empty
1834 * string is returned. For this reason, you can always trust that a
1835 * non-%NULL nul-terminated string will be returned by this function.
1837 * If the array contains a nul terminator character somewhere other than
1838 * the last byte then the returned string is the string, up to the first
1839 * such nul character.
1841 * g_variant_get_fixed_array() should be used instead if the array contains
1842 * arbitrary data that could not be nul-terminated or could contain nul bytes.
1844 * It is an error to call this function with a @value that is not an
1847 * The return value remains valid as long as @value exists.
1849 * Returns: (transfer none) (array zero-terminated=1) (element-type guint8):
1850 * the constant string
1855 g_variant_get_bytestring (GVariant
*value
)
1857 const gchar
*string
;
1860 TYPE_CHECK (value
, G_VARIANT_TYPE_BYTESTRING
, NULL
);
1862 /* Won't be NULL since this is an array type */
1863 string
= g_variant_get_data (value
);
1864 size
= g_variant_get_size (value
);
1866 if (size
&& string
[size
- 1] == '\0')
1873 * g_variant_dup_bytestring:
1874 * @value: an array-of-bytes #GVariant instance
1875 * @length: (out) (optional) (default NULL): a pointer to a #gsize, to store
1876 * the length (not including the nul terminator)
1878 * Similar to g_variant_get_bytestring() except that instead of
1879 * returning a constant string, the string is duplicated.
1881 * The return value must be freed using g_free().
1883 * Returns: (transfer full) (array zero-terminated=1 length=length) (element-type guint8):
1884 * a newly allocated string
1889 g_variant_dup_bytestring (GVariant
*value
,
1892 const gchar
*original
= g_variant_get_bytestring (value
);
1895 /* don't crash in case get_bytestring() had an assert failure */
1896 if (original
== NULL
)
1899 size
= strlen (original
);
1904 return g_memdup (original
, size
+ 1);
1908 * g_variant_new_bytestring_array:
1909 * @strv: (array length=length): an array of strings
1910 * @length: the length of @strv, or -1
1912 * Constructs an array of bytestring #GVariant from the given array of
1915 * If @length is -1 then @strv is %NULL-terminated.
1917 * Returns: (transfer none): a new floating #GVariant instance
1922 g_variant_new_bytestring_array (const gchar
* const *strv
,
1928 g_return_val_if_fail (length
== 0 || strv
!= NULL
, NULL
);
1931 length
= g_strv_length ((gchar
**) strv
);
1933 strings
= g_new (GVariant
*, length
);
1934 for (i
= 0; i
< length
; i
++)
1935 strings
[i
] = g_variant_ref_sink (g_variant_new_bytestring (strv
[i
]));
1937 return g_variant_new_from_children (G_VARIANT_TYPE_BYTESTRING_ARRAY
,
1938 strings
, length
, TRUE
);
1942 * g_variant_get_bytestring_array:
1943 * @value: an array of array of bytes #GVariant ('aay')
1944 * @length: (out) (optional): the length of the result, or %NULL
1946 * Gets the contents of an array of array of bytes #GVariant. This call
1947 * makes a shallow copy; the return result should be released with
1948 * g_free(), but the individual strings must not be modified.
1950 * If @length is non-%NULL then the number of elements in the result is
1951 * stored there. In any case, the resulting array will be
1954 * For an empty array, @length will be set to 0 and a pointer to a
1955 * %NULL pointer will be returned.
1957 * Returns: (array length=length) (transfer container): an array of constant strings
1962 g_variant_get_bytestring_array (GVariant
*value
,
1969 TYPE_CHECK (value
, G_VARIANT_TYPE_BYTESTRING_ARRAY
, NULL
);
1971 g_variant_get_data (value
);
1972 n
= g_variant_n_children (value
);
1973 strv
= g_new (const gchar
*, n
+ 1);
1975 for (i
= 0; i
< n
; i
++)
1979 string
= g_variant_get_child_value (value
, i
);
1980 strv
[i
] = g_variant_get_bytestring (string
);
1981 g_variant_unref (string
);
1992 * g_variant_dup_bytestring_array:
1993 * @value: an array of array of bytes #GVariant ('aay')
1994 * @length: (out) (optional): the length of the result, or %NULL
1996 * Gets the contents of an array of array of bytes #GVariant. This call
1997 * makes a deep copy; the return result should be released with
2000 * If @length is non-%NULL then the number of elements in the result is
2001 * stored there. In any case, the resulting array will be
2004 * For an empty array, @length will be set to 0 and a pointer to a
2005 * %NULL pointer will be returned.
2007 * Returns: (array length=length) (transfer full): an array of strings
2012 g_variant_dup_bytestring_array (GVariant
*value
,
2019 TYPE_CHECK (value
, G_VARIANT_TYPE_BYTESTRING_ARRAY
, NULL
);
2021 g_variant_get_data (value
);
2022 n
= g_variant_n_children (value
);
2023 strv
= g_new (gchar
*, n
+ 1);
2025 for (i
= 0; i
< n
; i
++)
2029 string
= g_variant_get_child_value (value
, i
);
2030 strv
[i
] = g_variant_dup_bytestring (string
, NULL
);
2031 g_variant_unref (string
);
2041 /* Type checking and querying {{{1 */
2043 * g_variant_get_type:
2044 * @value: a #GVariant
2046 * Determines the type of @value.
2048 * The return value is valid for the lifetime of @value and must not
2051 * Returns: a #GVariantType
2055 const GVariantType
*
2056 g_variant_get_type (GVariant
*value
)
2058 GVariantTypeInfo
*type_info
;
2060 g_return_val_if_fail (value
!= NULL
, NULL
);
2062 type_info
= g_variant_get_type_info (value
);
2064 return (GVariantType
*) g_variant_type_info_get_type_string (type_info
);
2068 * g_variant_get_type_string:
2069 * @value: a #GVariant
2071 * Returns the type string of @value. Unlike the result of calling
2072 * g_variant_type_peek_string(), this string is nul-terminated. This
2073 * string belongs to #GVariant and must not be freed.
2075 * Returns: the type string for the type of @value
2080 g_variant_get_type_string (GVariant
*value
)
2082 GVariantTypeInfo
*type_info
;
2084 g_return_val_if_fail (value
!= NULL
, NULL
);
2086 type_info
= g_variant_get_type_info (value
);
2088 return g_variant_type_info_get_type_string (type_info
);
2092 * g_variant_is_of_type:
2093 * @value: a #GVariant instance
2094 * @type: a #GVariantType
2096 * Checks if a value has a type matching the provided type.
2098 * Returns: %TRUE if the type of @value matches @type
2103 g_variant_is_of_type (GVariant
*value
,
2104 const GVariantType
*type
)
2106 return g_variant_type_is_subtype_of (g_variant_get_type (value
), type
);
2110 * g_variant_is_container:
2111 * @value: a #GVariant instance
2113 * Checks if @value is a container.
2115 * Returns: %TRUE if @value is a container
2120 g_variant_is_container (GVariant
*value
)
2122 return g_variant_type_is_container (g_variant_get_type (value
));
2127 * g_variant_classify:
2128 * @value: a #GVariant
2130 * Classifies @value according to its top-level type.
2132 * Returns: the #GVariantClass of @value
2138 * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
2139 * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
2140 * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
2141 * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
2142 * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
2143 * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
2144 * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
2145 * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
2146 * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
2147 * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating
2149 * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
2150 * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a D-Bus object path
2152 * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a D-Bus signature string.
2153 * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
2154 * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
2155 * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
2156 * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
2157 * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
2159 * The range of possible top-level types of #GVariant instances.
2164 g_variant_classify (GVariant
*value
)
2166 g_return_val_if_fail (value
!= NULL
, 0);
2168 return *g_variant_get_type_string (value
);
2171 /* Pretty printer {{{1 */
2172 /* This function is not introspectable because if @string is NULL,
2173 @returns is (transfer full), otherwise it is (transfer none), which
2174 is not supported by GObjectIntrospection */
2176 * g_variant_print_string: (skip)
2177 * @value: a #GVariant
2178 * @string: (nullable) (default NULL): a #GString, or %NULL
2179 * @type_annotate: %TRUE if type information should be included in
2182 * Behaves as g_variant_print(), but operates on a #GString.
2184 * If @string is non-%NULL then it is appended to and returned. Else,
2185 * a new empty #GString is allocated and it is returned.
2187 * Returns: a #GString containing the string
2192 g_variant_print_string (GVariant
*value
,
2194 gboolean type_annotate
)
2196 if G_UNLIKELY (string
== NULL
)
2197 string
= g_string_new (NULL
);
2199 switch (g_variant_classify (value
))
2201 case G_VARIANT_CLASS_MAYBE
:
2203 g_string_append_printf (string
, "@%s ",
2204 g_variant_get_type_string (value
));
2206 if (g_variant_n_children (value
))
2208 gchar
*printed_child
;
2213 * Consider the case of the type "mmi". In this case we could
2214 * write "just just 4", but "4" alone is totally unambiguous,
2215 * so we try to drop "just" where possible.
2217 * We have to be careful not to always drop "just", though,
2218 * since "nothing" needs to be distinguishable from "just
2219 * nothing". The case where we need to ensure we keep the
2220 * "just" is actually exactly the case where we have a nested
2223 * Instead of searching for that nested Nothing, we just print
2224 * the contained value into a separate string and see if we
2225 * end up with "nothing" at the end of it. If so, we need to
2226 * add "just" at our level.
2228 element
= g_variant_get_child_value (value
, 0);
2229 printed_child
= g_variant_print (element
, FALSE
);
2230 g_variant_unref (element
);
2232 if (g_str_has_suffix (printed_child
, "nothing"))
2233 g_string_append (string
, "just ");
2234 g_string_append (string
, printed_child
);
2235 g_free (printed_child
);
2238 g_string_append (string
, "nothing");
2242 case G_VARIANT_CLASS_ARRAY
:
2243 /* it's an array so the first character of the type string is 'a'
2245 * if the first two characters are 'ay' then it's a bytestring.
2246 * under certain conditions we print those as strings.
2248 if (g_variant_get_type_string (value
)[1] == 'y')
2254 /* first determine if it is a byte string.
2255 * that's when there's a single nul character: at the end.
2257 str
= g_variant_get_data (value
);
2258 size
= g_variant_get_size (value
);
2260 for (i
= 0; i
< size
; i
++)
2264 /* first nul byte is the last byte -> it's a byte string. */
2267 gchar
*escaped
= g_strescape (str
, NULL
);
2269 /* use double quotes only if a ' is in the string */
2270 if (strchr (str
, '\''))
2271 g_string_append_printf (string
, "b\"%s\"", escaped
);
2273 g_string_append_printf (string
, "b'%s'", escaped
);
2281 /* fall through and handle normally... */
2286 * if the first two characters are 'a{' then it's an array of
2287 * dictionary entries (ie: a dictionary) so we print that
2290 if (g_variant_get_type_string (value
)[1] == '{')
2293 const gchar
*comma
= "";
2296 if ((n
= g_variant_n_children (value
)) == 0)
2299 g_string_append_printf (string
, "@%s ",
2300 g_variant_get_type_string (value
));
2301 g_string_append (string
, "{}");
2305 g_string_append_c (string
, '{');
2306 for (i
= 0; i
< n
; i
++)
2308 GVariant
*entry
, *key
, *val
;
2310 g_string_append (string
, comma
);
2313 entry
= g_variant_get_child_value (value
, i
);
2314 key
= g_variant_get_child_value (entry
, 0);
2315 val
= g_variant_get_child_value (entry
, 1);
2316 g_variant_unref (entry
);
2318 g_variant_print_string (key
, string
, type_annotate
);
2319 g_variant_unref (key
);
2320 g_string_append (string
, ": ");
2321 g_variant_print_string (val
, string
, type_annotate
);
2322 g_variant_unref (val
);
2323 type_annotate
= FALSE
;
2325 g_string_append_c (string
, '}');
2328 /* normal (non-dictionary) array */
2330 const gchar
*comma
= "";
2333 if ((n
= g_variant_n_children (value
)) == 0)
2336 g_string_append_printf (string
, "@%s ",
2337 g_variant_get_type_string (value
));
2338 g_string_append (string
, "[]");
2342 g_string_append_c (string
, '[');
2343 for (i
= 0; i
< n
; i
++)
2347 g_string_append (string
, comma
);
2350 element
= g_variant_get_child_value (value
, i
);
2352 g_variant_print_string (element
, string
, type_annotate
);
2353 g_variant_unref (element
);
2354 type_annotate
= FALSE
;
2356 g_string_append_c (string
, ']');
2361 case G_VARIANT_CLASS_TUPLE
:
2365 n
= g_variant_n_children (value
);
2367 g_string_append_c (string
, '(');
2368 for (i
= 0; i
< n
; i
++)
2372 element
= g_variant_get_child_value (value
, i
);
2373 g_variant_print_string (element
, string
, type_annotate
);
2374 g_string_append (string
, ", ");
2375 g_variant_unref (element
);
2378 /* for >1 item: remove final ", "
2379 * for 1 item: remove final " ", but leave the ","
2380 * for 0 items: there is only "(", so remove nothing
2382 g_string_truncate (string
, string
->len
- (n
> 0) - (n
> 1));
2383 g_string_append_c (string
, ')');
2387 case G_VARIANT_CLASS_DICT_ENTRY
:
2391 g_string_append_c (string
, '{');
2393 element
= g_variant_get_child_value (value
, 0);
2394 g_variant_print_string (element
, string
, type_annotate
);
2395 g_variant_unref (element
);
2397 g_string_append (string
, ", ");
2399 element
= g_variant_get_child_value (value
, 1);
2400 g_variant_print_string (element
, string
, type_annotate
);
2401 g_variant_unref (element
);
2403 g_string_append_c (string
, '}');
2407 case G_VARIANT_CLASS_VARIANT
:
2409 GVariant
*child
= g_variant_get_variant (value
);
2411 /* Always annotate types in nested variants, because they are
2412 * (by nature) of variable type.
2414 g_string_append_c (string
, '<');
2415 g_variant_print_string (child
, string
, TRUE
);
2416 g_string_append_c (string
, '>');
2418 g_variant_unref (child
);
2422 case G_VARIANT_CLASS_BOOLEAN
:
2423 if (g_variant_get_boolean (value
))
2424 g_string_append (string
, "true");
2426 g_string_append (string
, "false");
2429 case G_VARIANT_CLASS_STRING
:
2431 const gchar
*str
= g_variant_get_string (value
, NULL
);
2432 gunichar quote
= strchr (str
, '\'') ? '"' : '\'';
2434 g_string_append_c (string
, quote
);
2438 gunichar c
= g_utf8_get_char (str
);
2440 if (c
== quote
|| c
== '\\')
2441 g_string_append_c (string
, '\\');
2443 if (g_unichar_isprint (c
))
2444 g_string_append_unichar (string
, c
);
2448 g_string_append_c (string
, '\\');
2453 g_string_append_c (string
, 'a');
2457 g_string_append_c (string
, 'b');
2461 g_string_append_c (string
, 'f');
2465 g_string_append_c (string
, 'n');
2469 g_string_append_c (string
, 'r');
2473 g_string_append_c (string
, 't');
2477 g_string_append_c (string
, 'v');
2481 g_string_append_printf (string
, "u%04x", c
);
2485 g_string_append_printf (string
, "U%08x", c
);
2488 str
= g_utf8_next_char (str
);
2491 g_string_append_c (string
, quote
);
2495 case G_VARIANT_CLASS_BYTE
:
2497 g_string_append (string
, "byte ");
2498 g_string_append_printf (string
, "0x%02x",
2499 g_variant_get_byte (value
));
2502 case G_VARIANT_CLASS_INT16
:
2504 g_string_append (string
, "int16 ");
2505 g_string_append_printf (string
, "%"G_GINT16_FORMAT
,
2506 g_variant_get_int16 (value
));
2509 case G_VARIANT_CLASS_UINT16
:
2511 g_string_append (string
, "uint16 ");
2512 g_string_append_printf (string
, "%"G_GUINT16_FORMAT
,
2513 g_variant_get_uint16 (value
));
2516 case G_VARIANT_CLASS_INT32
:
2517 /* Never annotate this type because it is the default for numbers
2518 * (and this is a *pretty* printer)
2520 g_string_append_printf (string
, "%"G_GINT32_FORMAT
,
2521 g_variant_get_int32 (value
));
2524 case G_VARIANT_CLASS_HANDLE
:
2526 g_string_append (string
, "handle ");
2527 g_string_append_printf (string
, "%"G_GINT32_FORMAT
,
2528 g_variant_get_handle (value
));
2531 case G_VARIANT_CLASS_UINT32
:
2533 g_string_append (string
, "uint32 ");
2534 g_string_append_printf (string
, "%"G_GUINT32_FORMAT
,
2535 g_variant_get_uint32 (value
));
2538 case G_VARIANT_CLASS_INT64
:
2540 g_string_append (string
, "int64 ");
2541 g_string_append_printf (string
, "%"G_GINT64_FORMAT
,
2542 g_variant_get_int64 (value
));
2545 case G_VARIANT_CLASS_UINT64
:
2547 g_string_append (string
, "uint64 ");
2548 g_string_append_printf (string
, "%"G_GUINT64_FORMAT
,
2549 g_variant_get_uint64 (value
));
2552 case G_VARIANT_CLASS_DOUBLE
:
2557 g_ascii_dtostr (buffer
, sizeof buffer
, g_variant_get_double (value
));
2559 for (i
= 0; buffer
[i
]; i
++)
2560 if (buffer
[i
] == '.' || buffer
[i
] == 'e' ||
2561 buffer
[i
] == 'n' || buffer
[i
] == 'N')
2564 /* if there is no '.' or 'e' in the float then add one */
2565 if (buffer
[i
] == '\0')
2572 g_string_append (string
, buffer
);
2576 case G_VARIANT_CLASS_OBJECT_PATH
:
2578 g_string_append (string
, "objectpath ");
2579 g_string_append_printf (string
, "\'%s\'",
2580 g_variant_get_string (value
, NULL
));
2583 case G_VARIANT_CLASS_SIGNATURE
:
2585 g_string_append (string
, "signature ");
2586 g_string_append_printf (string
, "\'%s\'",
2587 g_variant_get_string (value
, NULL
));
2591 g_assert_not_reached ();
2599 * @value: a #GVariant
2600 * @type_annotate: %TRUE if type information should be included in
2603 * Pretty-prints @value in the format understood by g_variant_parse().
2605 * The format is described [here][gvariant-text].
2607 * If @type_annotate is %TRUE, then type information is included in
2610 * Returns: (transfer full): a newly-allocated string holding the result.
2615 g_variant_print (GVariant
*value
,
2616 gboolean type_annotate
)
2618 return g_string_free (g_variant_print_string (value
, NULL
, type_annotate
),
2622 /* Hash, Equal, Compare {{{1 */
2625 * @value: (type GVariant): a basic #GVariant value as a #gconstpointer
2627 * Generates a hash value for a #GVariant instance.
2629 * The output of this function is guaranteed to be the same for a given
2630 * value only per-process. It may change between different processor
2631 * architectures or even different versions of GLib. Do not use this
2632 * function as a basis for building protocols or file formats.
2634 * The type of @value is #gconstpointer only to allow use of this
2635 * function with #GHashTable. @value must be a #GVariant.
2637 * Returns: a hash value corresponding to @value
2642 g_variant_hash (gconstpointer value_
)
2644 GVariant
*value
= (GVariant
*) value_
;
2646 switch (g_variant_classify (value
))
2648 case G_VARIANT_CLASS_STRING
:
2649 case G_VARIANT_CLASS_OBJECT_PATH
:
2650 case G_VARIANT_CLASS_SIGNATURE
:
2651 return g_str_hash (g_variant_get_string (value
, NULL
));
2653 case G_VARIANT_CLASS_BOOLEAN
:
2654 /* this is a very odd thing to hash... */
2655 return g_variant_get_boolean (value
);
2657 case G_VARIANT_CLASS_BYTE
:
2658 return g_variant_get_byte (value
);
2660 case G_VARIANT_CLASS_INT16
:
2661 case G_VARIANT_CLASS_UINT16
:
2665 ptr
= g_variant_get_data (value
);
2673 case G_VARIANT_CLASS_INT32
:
2674 case G_VARIANT_CLASS_UINT32
:
2675 case G_VARIANT_CLASS_HANDLE
:
2679 ptr
= g_variant_get_data (value
);
2687 case G_VARIANT_CLASS_INT64
:
2688 case G_VARIANT_CLASS_UINT64
:
2689 case G_VARIANT_CLASS_DOUBLE
:
2690 /* need a separate case for these guys because otherwise
2691 * performance could be quite bad on big endian systems
2696 ptr
= g_variant_get_data (value
);
2699 return ptr
[0] + ptr
[1];
2705 g_return_val_if_fail (!g_variant_is_container (value
), 0);
2706 g_assert_not_reached ();
2712 * @one: (type GVariant): a #GVariant instance
2713 * @two: (type GVariant): a #GVariant instance
2715 * Checks if @one and @two have the same type and value.
2717 * The types of @one and @two are #gconstpointer only to allow use of
2718 * this function with #GHashTable. They must each be a #GVariant.
2720 * Returns: %TRUE if @one and @two are equal
2725 g_variant_equal (gconstpointer one
,
2730 g_return_val_if_fail (one
!= NULL
&& two
!= NULL
, FALSE
);
2732 if (g_variant_get_type_info ((GVariant
*) one
) !=
2733 g_variant_get_type_info ((GVariant
*) two
))
2736 /* if both values are trusted to be in their canonical serialised form
2737 * then a simple memcmp() of their serialised data will answer the
2740 * if not, then this might generate a false negative (since it is
2741 * possible for two different byte sequences to represent the same
2742 * value). for now we solve this by pretty-printing both values and
2743 * comparing the result.
2745 if (g_variant_is_trusted ((GVariant
*) one
) &&
2746 g_variant_is_trusted ((GVariant
*) two
))
2748 gconstpointer data_one
, data_two
;
2749 gsize size_one
, size_two
;
2751 size_one
= g_variant_get_size ((GVariant
*) one
);
2752 size_two
= g_variant_get_size ((GVariant
*) two
);
2754 if (size_one
!= size_two
)
2757 data_one
= g_variant_get_data ((GVariant
*) one
);
2758 data_two
= g_variant_get_data ((GVariant
*) two
);
2760 equal
= memcmp (data_one
, data_two
, size_one
) == 0;
2764 gchar
*strone
, *strtwo
;
2766 strone
= g_variant_print ((GVariant
*) one
, FALSE
);
2767 strtwo
= g_variant_print ((GVariant
*) two
, FALSE
);
2768 equal
= strcmp (strone
, strtwo
) == 0;
2777 * g_variant_compare:
2778 * @one: (type GVariant): a basic-typed #GVariant instance
2779 * @two: (type GVariant): a #GVariant instance of the same type
2781 * Compares @one and @two.
2783 * The types of @one and @two are #gconstpointer only to allow use of
2784 * this function with #GTree, #GPtrArray, etc. They must each be a
2787 * Comparison is only defined for basic types (ie: booleans, numbers,
2788 * strings). For booleans, %FALSE is less than %TRUE. Numbers are
2789 * ordered in the usual way. Strings are in ASCII lexographical order.
2791 * It is a programmer error to attempt to compare container values or
2792 * two values that have types that are not exactly equal. For example,
2793 * you cannot compare a 32-bit signed integer with a 32-bit unsigned
2794 * integer. Also note that this function is not particularly
2795 * well-behaved when it comes to comparison of doubles; in particular,
2796 * the handling of incomparable values (ie: NaN) is undefined.
2798 * If you only require an equality comparison, g_variant_equal() is more
2801 * Returns: negative value if a < b;
2803 * positive value if a > b.
2808 g_variant_compare (gconstpointer one
,
2811 GVariant
*a
= (GVariant
*) one
;
2812 GVariant
*b
= (GVariant
*) two
;
2814 g_return_val_if_fail (g_variant_classify (a
) == g_variant_classify (b
), 0);
2816 switch (g_variant_classify (a
))
2818 case G_VARIANT_CLASS_BOOLEAN
:
2819 return g_variant_get_boolean (a
) -
2820 g_variant_get_boolean (b
);
2822 case G_VARIANT_CLASS_BYTE
:
2823 return ((gint
) g_variant_get_byte (a
)) -
2824 ((gint
) g_variant_get_byte (b
));
2826 case G_VARIANT_CLASS_INT16
:
2827 return ((gint
) g_variant_get_int16 (a
)) -
2828 ((gint
) g_variant_get_int16 (b
));
2830 case G_VARIANT_CLASS_UINT16
:
2831 return ((gint
) g_variant_get_uint16 (a
)) -
2832 ((gint
) g_variant_get_uint16 (b
));
2834 case G_VARIANT_CLASS_INT32
:
2836 gint32 a_val
= g_variant_get_int32 (a
);
2837 gint32 b_val
= g_variant_get_int32 (b
);
2839 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2842 case G_VARIANT_CLASS_UINT32
:
2844 guint32 a_val
= g_variant_get_uint32 (a
);
2845 guint32 b_val
= g_variant_get_uint32 (b
);
2847 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2850 case G_VARIANT_CLASS_INT64
:
2852 gint64 a_val
= g_variant_get_int64 (a
);
2853 gint64 b_val
= g_variant_get_int64 (b
);
2855 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2858 case G_VARIANT_CLASS_UINT64
:
2860 guint64 a_val
= g_variant_get_uint64 (a
);
2861 guint64 b_val
= g_variant_get_uint64 (b
);
2863 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2866 case G_VARIANT_CLASS_DOUBLE
:
2868 gdouble a_val
= g_variant_get_double (a
);
2869 gdouble b_val
= g_variant_get_double (b
);
2871 return (a_val
== b_val
) ? 0 : (a_val
> b_val
) ? 1 : -1;
2874 case G_VARIANT_CLASS_STRING
:
2875 case G_VARIANT_CLASS_OBJECT_PATH
:
2876 case G_VARIANT_CLASS_SIGNATURE
:
2877 return strcmp (g_variant_get_string (a
, NULL
),
2878 g_variant_get_string (b
, NULL
));
2881 g_return_val_if_fail (!g_variant_is_container (a
), 0);
2882 g_assert_not_reached ();
2886 /* GVariantIter {{{1 */
2888 * GVariantIter: (skip)
2890 * #GVariantIter is an opaque data structure and can only be accessed
2891 * using the following functions.
2898 const gchar
*loop_format
;
2904 G_STATIC_ASSERT (sizeof (struct stack_iter
) <= sizeof (GVariantIter
));
2908 struct stack_iter iter
;
2910 GVariant
*value_ref
;
2914 #define GVSI(i) ((struct stack_iter *) (i))
2915 #define GVHI(i) ((struct heap_iter *) (i))
2916 #define GVSI_MAGIC ((gsize) 3579507750u)
2917 #define GVHI_MAGIC ((gsize) 1450270775u)
2918 #define is_valid_iter(i) (i != NULL && \
2919 GVSI(i)->magic == GVSI_MAGIC)
2920 #define is_valid_heap_iter(i) (is_valid_iter(i) && \
2921 GVHI(i)->magic == GVHI_MAGIC)
2924 * g_variant_iter_new:
2925 * @value: a container #GVariant
2927 * Creates a heap-allocated #GVariantIter for iterating over the items
2930 * Use g_variant_iter_free() to free the return value when you no longer
2933 * A reference is taken to @value and will be released only when
2934 * g_variant_iter_free() is called.
2936 * Returns: (transfer full): a new heap-allocated #GVariantIter
2941 g_variant_iter_new (GVariant
*value
)
2945 iter
= (GVariantIter
*) g_slice_new (struct heap_iter
);
2946 GVHI(iter
)->value_ref
= g_variant_ref (value
);
2947 GVHI(iter
)->magic
= GVHI_MAGIC
;
2949 g_variant_iter_init (iter
, value
);
2955 * g_variant_iter_init: (skip)
2956 * @iter: a pointer to a #GVariantIter
2957 * @value: a container #GVariant
2959 * Initialises (without allocating) a #GVariantIter. @iter may be
2960 * completely uninitialised prior to this call; its old value is
2963 * The iterator remains valid for as long as @value exists, and need not
2964 * be freed in any way.
2966 * Returns: the number of items in @value
2971 g_variant_iter_init (GVariantIter
*iter
,
2974 GVSI(iter
)->magic
= GVSI_MAGIC
;
2975 GVSI(iter
)->value
= value
;
2976 GVSI(iter
)->n
= g_variant_n_children (value
);
2978 GVSI(iter
)->loop_format
= NULL
;
2980 return GVSI(iter
)->n
;
2984 * g_variant_iter_copy:
2985 * @iter: a #GVariantIter
2987 * Creates a new heap-allocated #GVariantIter to iterate over the
2988 * container that was being iterated over by @iter. Iteration begins on
2989 * the new iterator from the current position of the old iterator but
2990 * the two copies are independent past that point.
2992 * Use g_variant_iter_free() to free the return value when you no longer
2995 * A reference is taken to the container that @iter is iterating over
2996 * and will be releated only when g_variant_iter_free() is called.
2998 * Returns: (transfer full): a new heap-allocated #GVariantIter
3003 g_variant_iter_copy (GVariantIter
*iter
)
3007 g_return_val_if_fail (is_valid_iter (iter
), 0);
3009 copy
= g_variant_iter_new (GVSI(iter
)->value
);
3010 GVSI(copy
)->i
= GVSI(iter
)->i
;
3016 * g_variant_iter_n_children:
3017 * @iter: a #GVariantIter
3019 * Queries the number of child items in the container that we are
3020 * iterating over. This is the total number of items -- not the number
3021 * of items remaining.
3023 * This function might be useful for preallocation of arrays.
3025 * Returns: the number of children in the container
3030 g_variant_iter_n_children (GVariantIter
*iter
)
3032 g_return_val_if_fail (is_valid_iter (iter
), 0);
3034 return GVSI(iter
)->n
;
3038 * g_variant_iter_free:
3039 * @iter: (transfer full): a heap-allocated #GVariantIter
3041 * Frees a heap-allocated #GVariantIter. Only call this function on
3042 * iterators that were returned by g_variant_iter_new() or
3043 * g_variant_iter_copy().
3048 g_variant_iter_free (GVariantIter
*iter
)
3050 g_return_if_fail (is_valid_heap_iter (iter
));
3052 g_variant_unref (GVHI(iter
)->value_ref
);
3053 GVHI(iter
)->magic
= 0;
3055 g_slice_free (struct heap_iter
, GVHI(iter
));
3059 * g_variant_iter_next_value:
3060 * @iter: a #GVariantIter
3062 * Gets the next item in the container. If no more items remain then
3063 * %NULL is returned.
3065 * Use g_variant_unref() to drop your reference on the return value when
3066 * you no longer need it.
3068 * Here is an example for iterating with g_variant_iter_next_value():
3069 * |[<!-- language="C" -->
3070 * // recursively iterate a container
3072 * iterate_container_recursive (GVariant *container)
3074 * GVariantIter iter;
3077 * g_variant_iter_init (&iter, container);
3078 * while ((child = g_variant_iter_next_value (&iter)))
3080 * g_print ("type '%s'\n", g_variant_get_type_string (child));
3082 * if (g_variant_is_container (child))
3083 * iterate_container_recursive (child);
3085 * g_variant_unref (child);
3090 * Returns: (nullable) (transfer full): a #GVariant, or %NULL
3095 g_variant_iter_next_value (GVariantIter
*iter
)
3097 g_return_val_if_fail (is_valid_iter (iter
), FALSE
);
3099 if G_UNLIKELY (GVSI(iter
)->i
>= GVSI(iter
)->n
)
3101 g_critical ("g_variant_iter_next_value: must not be called again "
3102 "after NULL has already been returned.");
3108 if (GVSI(iter
)->i
< GVSI(iter
)->n
)
3109 return g_variant_get_child_value (GVSI(iter
)->value
, GVSI(iter
)->i
);
3114 /* GVariantBuilder {{{1 */
3118 * A utility type for constructing container-type #GVariant instances.
3120 * This is an opaque structure and may only be accessed using the
3121 * following functions.
3123 * #GVariantBuilder is not threadsafe in any way. Do not attempt to
3124 * access it from more than one thread.
3127 struct stack_builder
3129 GVariantBuilder
*parent
;
3132 /* type constraint explicitly specified by 'type'.
3133 * for tuple types, this moves along as we add more items.
3135 const GVariantType
*expected_type
;
3137 /* type constraint implied by previous array item.
3139 const GVariantType
*prev_item_type
;
3141 /* constraints on the number of children. max = -1 for unlimited. */
3145 /* dynamically-growing pointer array */
3146 GVariant
**children
;
3147 gsize allocated_children
;
3150 /* set to '1' if all items in the container will have the same type
3151 * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
3153 guint uniform_item_types
: 1;
3155 /* set to '1' initially and changed to '0' if an untrusted value is
3163 G_STATIC_ASSERT (sizeof (struct stack_builder
) <= sizeof (GVariantBuilder
));
3167 GVariantBuilder builder
;
3173 #define GVSB(b) ((struct stack_builder *) (b))
3174 #define GVHB(b) ((struct heap_builder *) (b))
3175 #define GVSB_MAGIC ((gsize) 1033660112u)
3176 #define GVSB_MAGIC_PARTIAL ((gsize) 2942751021u)
3177 #define GVHB_MAGIC ((gsize) 3087242682u)
3178 #define is_valid_builder(b) (b != NULL && \
3179 GVSB(b)->magic == GVSB_MAGIC)
3180 #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
3182 /* Just to make sure that by adding a union to GVariantBuilder, we
3183 * didn't accidentally change ABI. */
3184 G_STATIC_ASSERT (sizeof (GVariantBuilder
) == sizeof (gsize
[16]));
3187 ensure_valid_builder (GVariantBuilder
*builder
)
3189 if (is_valid_builder (builder
))
3191 if (builder
->u
.s
.partial_magic
== GVSB_MAGIC_PARTIAL
)
3193 static GVariantBuilder cleared_builder
;
3195 /* Make sure that only first two fields were set and the rest is
3196 * zeroed to avoid messing up the builder that had parent
3197 * address equal to GVSB_MAGIC_PARTIAL. */
3198 if (memcmp (cleared_builder
.u
.s
.y
, builder
->u
.s
.y
, sizeof cleared_builder
.u
.s
.y
))
3201 g_variant_builder_init (builder
, builder
->u
.s
.type
);
3203 return is_valid_builder (builder
);
3207 * g_variant_builder_new:
3208 * @type: a container type
3210 * Allocates and initialises a new #GVariantBuilder.
3212 * You should call g_variant_builder_unref() on the return value when it
3213 * is no longer needed. The memory will not be automatically freed by
3216 * In most cases it is easier to place a #GVariantBuilder directly on
3217 * the stack of the calling function and initialise it with
3218 * g_variant_builder_init().
3220 * Returns: (transfer full): a #GVariantBuilder
3225 g_variant_builder_new (const GVariantType
*type
)
3227 GVariantBuilder
*builder
;
3229 builder
= (GVariantBuilder
*) g_slice_new (struct heap_builder
);
3230 g_variant_builder_init (builder
, type
);
3231 GVHB(builder
)->magic
= GVHB_MAGIC
;
3232 GVHB(builder
)->ref_count
= 1;
3238 * g_variant_builder_unref:
3239 * @builder: (transfer full): a #GVariantBuilder allocated by g_variant_builder_new()
3241 * Decreases the reference count on @builder.
3243 * In the event that there are no more references, releases all memory
3244 * associated with the #GVariantBuilder.
3246 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3247 * things will happen.
3252 g_variant_builder_unref (GVariantBuilder
*builder
)
3254 g_return_if_fail (is_valid_heap_builder (builder
));
3256 if (--GVHB(builder
)->ref_count
)
3259 g_variant_builder_clear (builder
);
3260 GVHB(builder
)->magic
= 0;
3262 g_slice_free (struct heap_builder
, GVHB(builder
));
3266 * g_variant_builder_ref:
3267 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
3269 * Increases the reference count on @builder.
3271 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3272 * things will happen.
3274 * Returns: (transfer full): a new reference to @builder
3279 g_variant_builder_ref (GVariantBuilder
*builder
)
3281 g_return_val_if_fail (is_valid_heap_builder (builder
), NULL
);
3283 GVHB(builder
)->ref_count
++;
3289 * g_variant_builder_clear: (skip)
3290 * @builder: a #GVariantBuilder
3292 * Releases all memory associated with a #GVariantBuilder without
3293 * freeing the #GVariantBuilder structure itself.
3295 * It typically only makes sense to do this on a stack-allocated
3296 * #GVariantBuilder if you want to abort building the value part-way
3297 * through. This function need not be called if you call
3298 * g_variant_builder_end() and it also doesn't need to be called on
3299 * builders allocated with g_variant_builder_new() (see
3300 * g_variant_builder_unref() for that).
3302 * This function leaves the #GVariantBuilder structure set to all-zeros.
3303 * It is valid to call this function on either an initialised
3304 * #GVariantBuilder or one that is set to all-zeros but it is not valid
3305 * to call this function on uninitialised memory.
3310 g_variant_builder_clear (GVariantBuilder
*builder
)
3314 if (GVSB(builder
)->magic
== 0)
3315 /* all-zeros or partial case */
3318 g_return_if_fail (ensure_valid_builder (builder
));
3320 g_variant_type_free (GVSB(builder
)->type
);
3322 for (i
= 0; i
< GVSB(builder
)->offset
; i
++)
3323 g_variant_unref (GVSB(builder
)->children
[i
]);
3325 g_free (GVSB(builder
)->children
);
3327 if (GVSB(builder
)->parent
)
3329 g_variant_builder_clear (GVSB(builder
)->parent
);
3330 g_slice_free (GVariantBuilder
, GVSB(builder
)->parent
);
3333 memset (builder
, 0, sizeof (GVariantBuilder
));
3337 * g_variant_builder_init: (skip)
3338 * @builder: a #GVariantBuilder
3339 * @type: a container type
3341 * Initialises a #GVariantBuilder structure.
3343 * @type must be non-%NULL. It specifies the type of container to
3344 * construct. It can be an indefinite type such as
3345 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
3346 * Maybe, array, tuple, dictionary entry and variant-typed values may be
3349 * After the builder is initialised, values are added using
3350 * g_variant_builder_add_value() or g_variant_builder_add().
3352 * After all the child values are added, g_variant_builder_end() frees
3353 * the memory associated with the builder and returns the #GVariant that
3356 * This function completely ignores the previous contents of @builder.
3357 * On one hand this means that it is valid to pass in completely
3358 * uninitialised memory. On the other hand, this means that if you are
3359 * initialising over top of an existing #GVariantBuilder you need to
3360 * first call g_variant_builder_clear() in order to avoid leaking
3363 * You must not call g_variant_builder_ref() or
3364 * g_variant_builder_unref() on a #GVariantBuilder that was initialised
3365 * with this function. If you ever pass a reference to a
3366 * #GVariantBuilder outside of the control of your own code then you
3367 * should assume that the person receiving that reference may try to use
3368 * reference counting; you should use g_variant_builder_new() instead of
3374 g_variant_builder_init (GVariantBuilder
*builder
,
3375 const GVariantType
*type
)
3377 g_return_if_fail (type
!= NULL
);
3378 g_return_if_fail (g_variant_type_is_container (type
));
3380 memset (builder
, 0, sizeof (GVariantBuilder
));
3382 GVSB(builder
)->type
= g_variant_type_copy (type
);
3383 GVSB(builder
)->magic
= GVSB_MAGIC
;
3384 GVSB(builder
)->trusted
= TRUE
;
3386 switch (*(const gchar
*) type
)
3388 case G_VARIANT_CLASS_VARIANT
:
3389 GVSB(builder
)->uniform_item_types
= TRUE
;
3390 GVSB(builder
)->allocated_children
= 1;
3391 GVSB(builder
)->expected_type
= NULL
;
3392 GVSB(builder
)->min_items
= 1;
3393 GVSB(builder
)->max_items
= 1;
3396 case G_VARIANT_CLASS_ARRAY
:
3397 GVSB(builder
)->uniform_item_types
= TRUE
;
3398 GVSB(builder
)->allocated_children
= 8;
3399 GVSB(builder
)->expected_type
=
3400 g_variant_type_element (GVSB(builder
)->type
);
3401 GVSB(builder
)->min_items
= 0;
3402 GVSB(builder
)->max_items
= -1;
3405 case G_VARIANT_CLASS_MAYBE
:
3406 GVSB(builder
)->uniform_item_types
= TRUE
;
3407 GVSB(builder
)->allocated_children
= 1;
3408 GVSB(builder
)->expected_type
=
3409 g_variant_type_element (GVSB(builder
)->type
);
3410 GVSB(builder
)->min_items
= 0;
3411 GVSB(builder
)->max_items
= 1;
3414 case G_VARIANT_CLASS_DICT_ENTRY
:
3415 GVSB(builder
)->uniform_item_types
= FALSE
;
3416 GVSB(builder
)->allocated_children
= 2;
3417 GVSB(builder
)->expected_type
=
3418 g_variant_type_key (GVSB(builder
)->type
);
3419 GVSB(builder
)->min_items
= 2;
3420 GVSB(builder
)->max_items
= 2;
3423 case 'r': /* G_VARIANT_TYPE_TUPLE was given */
3424 GVSB(builder
)->uniform_item_types
= FALSE
;
3425 GVSB(builder
)->allocated_children
= 8;
3426 GVSB(builder
)->expected_type
= NULL
;
3427 GVSB(builder
)->min_items
= 0;
3428 GVSB(builder
)->max_items
= -1;
3431 case G_VARIANT_CLASS_TUPLE
: /* a definite tuple type was given */
3432 GVSB(builder
)->allocated_children
= g_variant_type_n_items (type
);
3433 GVSB(builder
)->expected_type
=
3434 g_variant_type_first (GVSB(builder
)->type
);
3435 GVSB(builder
)->min_items
= GVSB(builder
)->allocated_children
;
3436 GVSB(builder
)->max_items
= GVSB(builder
)->allocated_children
;
3437 GVSB(builder
)->uniform_item_types
= FALSE
;
3441 g_assert_not_reached ();
3444 GVSB(builder
)->children
= g_new (GVariant
*,
3445 GVSB(builder
)->allocated_children
);
3449 g_variant_builder_make_room (struct stack_builder
*builder
)
3451 if (builder
->offset
== builder
->allocated_children
)
3453 builder
->allocated_children
*= 2;
3454 builder
->children
= g_renew (GVariant
*, builder
->children
,
3455 builder
->allocated_children
);
3460 * g_variant_builder_add_value:
3461 * @builder: a #GVariantBuilder
3462 * @value: a #GVariant
3464 * Adds @value to @builder.
3466 * It is an error to call this function in any way that would create an
3467 * inconsistent value to be constructed. Some examples of this are
3468 * putting different types of items into an array, putting the wrong
3469 * types or number of items in a tuple, putting more than one value into
3472 * If @value is a floating reference (see g_variant_ref_sink()),
3473 * the @builder instance takes ownership of @value.
3478 g_variant_builder_add_value (GVariantBuilder
*builder
,
3481 g_return_if_fail (ensure_valid_builder (builder
));
3482 g_return_if_fail (GVSB(builder
)->offset
< GVSB(builder
)->max_items
);
3483 g_return_if_fail (!GVSB(builder
)->expected_type
||
3484 g_variant_is_of_type (value
,
3485 GVSB(builder
)->expected_type
));
3486 g_return_if_fail (!GVSB(builder
)->prev_item_type
||
3487 g_variant_is_of_type (value
,
3488 GVSB(builder
)->prev_item_type
));
3490 GVSB(builder
)->trusted
&= g_variant_is_trusted (value
);
3492 if (!GVSB(builder
)->uniform_item_types
)
3494 /* advance our expected type pointers */
3495 if (GVSB(builder
)->expected_type
)
3496 GVSB(builder
)->expected_type
=
3497 g_variant_type_next (GVSB(builder
)->expected_type
);
3499 if (GVSB(builder
)->prev_item_type
)
3500 GVSB(builder
)->prev_item_type
=
3501 g_variant_type_next (GVSB(builder
)->prev_item_type
);
3504 GVSB(builder
)->prev_item_type
= g_variant_get_type (value
);
3506 g_variant_builder_make_room (GVSB(builder
));
3508 GVSB(builder
)->children
[GVSB(builder
)->offset
++] =
3509 g_variant_ref_sink (value
);
3513 * g_variant_builder_open:
3514 * @builder: a #GVariantBuilder
3515 * @type: the #GVariantType of the container
3517 * Opens a subcontainer inside the given @builder. When done adding
3518 * items to the subcontainer, g_variant_builder_close() must be called. @type
3519 * is the type of the container: so to build a tuple of several values, @type
3520 * must include the tuple itself.
3522 * It is an error to call this function in any way that would cause an
3523 * inconsistent value to be constructed (ie: adding too many values or
3524 * a value of an incorrect type).
3526 * Example of building a nested variant:
3527 * |[<!-- language="C" -->
3528 * GVariantBuilder builder;
3529 * guint32 some_number = get_number ();
3530 * g_autoptr (GHashTable) some_dict = get_dict ();
3531 * GHashTableIter iter;
3533 * const GVariant *value;
3534 * g_autoptr (GVariant) output = NULL;
3536 * g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ua{sv})"));
3537 * g_variant_builder_add (&builder, "u", some_number);
3538 * g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
3540 * g_hash_table_iter_init (&iter, some_dict);
3541 * while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value))
3543 * g_variant_builder_open (&builder, G_VARIANT_TYPE ("{sv}"));
3544 * g_variant_builder_add (&builder, "s", key);
3545 * g_variant_builder_add (&builder, "v", value);
3546 * g_variant_builder_close (&builder);
3549 * g_variant_builder_close (&builder);
3551 * output = g_variant_builder_end (&builder);
3557 g_variant_builder_open (GVariantBuilder
*builder
,
3558 const GVariantType
*type
)
3560 GVariantBuilder
*parent
;
3562 g_return_if_fail (ensure_valid_builder (builder
));
3563 g_return_if_fail (GVSB(builder
)->offset
< GVSB(builder
)->max_items
);
3564 g_return_if_fail (!GVSB(builder
)->expected_type
||
3565 g_variant_type_is_subtype_of (type
,
3566 GVSB(builder
)->expected_type
));
3567 g_return_if_fail (!GVSB(builder
)->prev_item_type
||
3568 g_variant_type_is_subtype_of (GVSB(builder
)->prev_item_type
,
3571 parent
= g_slice_dup (GVariantBuilder
, builder
);
3572 g_variant_builder_init (builder
, type
);
3573 GVSB(builder
)->parent
= parent
;
3575 /* push the prev_item_type down into the subcontainer */
3576 if (GVSB(parent
)->prev_item_type
)
3578 if (!GVSB(builder
)->uniform_item_types
)
3579 /* tuples and dict entries */
3580 GVSB(builder
)->prev_item_type
=
3581 g_variant_type_first (GVSB(parent
)->prev_item_type
);
3583 else if (!g_variant_type_is_variant (GVSB(builder
)->type
))
3584 /* maybes and arrays */
3585 GVSB(builder
)->prev_item_type
=
3586 g_variant_type_element (GVSB(parent
)->prev_item_type
);
3591 * g_variant_builder_close:
3592 * @builder: a #GVariantBuilder
3594 * Closes the subcontainer inside the given @builder that was opened by
3595 * the most recent call to g_variant_builder_open().
3597 * It is an error to call this function in any way that would create an
3598 * inconsistent value to be constructed (ie: too few values added to the
3604 g_variant_builder_close (GVariantBuilder
*builder
)
3606 GVariantBuilder
*parent
;
3608 g_return_if_fail (ensure_valid_builder (builder
));
3609 g_return_if_fail (GVSB(builder
)->parent
!= NULL
);
3611 parent
= GVSB(builder
)->parent
;
3612 GVSB(builder
)->parent
= NULL
;
3614 g_variant_builder_add_value (parent
, g_variant_builder_end (builder
));
3617 g_slice_free (GVariantBuilder
, parent
);
3621 * g_variant_make_maybe_type:
3622 * @element: a #GVariant
3624 * Return the type of a maybe containing @element.
3626 static GVariantType
*
3627 g_variant_make_maybe_type (GVariant
*element
)
3629 return g_variant_type_new_maybe (g_variant_get_type (element
));
3633 * g_variant_make_array_type:
3634 * @element: a #GVariant
3636 * Return the type of an array containing @element.
3638 static GVariantType
*
3639 g_variant_make_array_type (GVariant
*element
)
3641 return g_variant_type_new_array (g_variant_get_type (element
));
3645 * g_variant_builder_end:
3646 * @builder: a #GVariantBuilder
3648 * Ends the builder process and returns the constructed value.
3650 * It is not permissible to use @builder in any way after this call
3651 * except for reference counting operations (in the case of a
3652 * heap-allocated #GVariantBuilder) or by reinitialising it with
3653 * g_variant_builder_init() (in the case of stack-allocated). This
3654 * means that for the stack-allocated builders there is no need to
3655 * call g_variant_builder_clear() after the call to
3656 * g_variant_builder_end().
3658 * It is an error to call this function in any way that would create an
3659 * inconsistent value to be constructed (ie: insufficient number of
3660 * items added to a container with a specific number of children
3661 * required). It is also an error to call this function if the builder
3662 * was created with an indefinite array or maybe type and no children
3663 * have been added; in this case it is impossible to infer the type of
3666 * Returns: (transfer none): a new, floating, #GVariant
3671 g_variant_builder_end (GVariantBuilder
*builder
)
3673 GVariantType
*my_type
;
3676 g_return_val_if_fail (ensure_valid_builder (builder
), NULL
);
3677 g_return_val_if_fail (GVSB(builder
)->offset
>= GVSB(builder
)->min_items
,
3679 g_return_val_if_fail (!GVSB(builder
)->uniform_item_types
||
3680 GVSB(builder
)->prev_item_type
!= NULL
||
3681 g_variant_type_is_definite (GVSB(builder
)->type
),
3684 if (g_variant_type_is_definite (GVSB(builder
)->type
))
3685 my_type
= g_variant_type_copy (GVSB(builder
)->type
);
3687 else if (g_variant_type_is_maybe (GVSB(builder
)->type
))
3688 my_type
= g_variant_make_maybe_type (GVSB(builder
)->children
[0]);
3690 else if (g_variant_type_is_array (GVSB(builder
)->type
))
3691 my_type
= g_variant_make_array_type (GVSB(builder
)->children
[0]);
3693 else if (g_variant_type_is_tuple (GVSB(builder
)->type
))
3694 my_type
= g_variant_make_tuple_type (GVSB(builder
)->children
,
3695 GVSB(builder
)->offset
);
3697 else if (g_variant_type_is_dict_entry (GVSB(builder
)->type
))
3698 my_type
= g_variant_make_dict_entry_type (GVSB(builder
)->children
[0],
3699 GVSB(builder
)->children
[1]);
3701 g_assert_not_reached ();
3703 value
= g_variant_new_from_children (my_type
,
3704 g_renew (GVariant
*,
3705 GVSB(builder
)->children
,
3706 GVSB(builder
)->offset
),
3707 GVSB(builder
)->offset
,
3708 GVSB(builder
)->trusted
);
3709 GVSB(builder
)->children
= NULL
;
3710 GVSB(builder
)->offset
= 0;
3712 g_variant_builder_clear (builder
);
3713 g_variant_type_free (my_type
);
3718 /* GVariantDict {{{1 */
3723 * #GVariantDict is a mutable interface to #GVariant dictionaries.
3725 * It can be used for doing a sequence of dictionary lookups in an
3726 * efficient way on an existing #GVariant dictionary or it can be used
3727 * to construct new dictionaries with a hashtable-like interface. It
3728 * can also be used for taking existing dictionaries and modifying them
3729 * in order to create new ones.
3731 * #GVariantDict can only be used with %G_VARIANT_TYPE_VARDICT
3734 * It is possible to use #GVariantDict allocated on the stack or on the
3735 * heap. When using a stack-allocated #GVariantDict, you begin with a
3736 * call to g_variant_dict_init() and free the resources with a call to
3737 * g_variant_dict_clear().
3739 * Heap-allocated #GVariantDict follows normal refcounting rules: you
3740 * allocate it with g_variant_dict_new() and use g_variant_dict_ref()
3741 * and g_variant_dict_unref().
3743 * g_variant_dict_end() is used to convert the #GVariantDict back into a
3744 * dictionary-type #GVariant. When used with stack-allocated instances,
3745 * this also implicitly frees all associated memory, but for
3746 * heap-allocated instances, you must still call g_variant_dict_unref()
3749 * You will typically want to use a heap-allocated #GVariantDict when
3750 * you expose it as part of an API. For most other uses, the
3751 * stack-allocated form will be more convenient.
3753 * Consider the following two examples that do the same thing in each
3754 * style: take an existing dictionary and look up the "count" uint32
3755 * key, adding 1 to it if it is found, or returning an error if the
3756 * key is not found. Each returns the new dictionary as a floating
3759 * ## Using a stack-allocated GVariantDict
3761 * |[<!-- language="C" -->
3763 * add_to_count (GVariant *orig,
3766 * GVariantDict dict;
3769 * g_variant_dict_init (&dict, orig);
3770 * if (!g_variant_dict_lookup (&dict, "count", "u", &count))
3772 * g_set_error (...);
3773 * g_variant_dict_clear (&dict);
3777 * g_variant_dict_insert (&dict, "count", "u", count + 1);
3779 * return g_variant_dict_end (&dict);
3783 * ## Using heap-allocated GVariantDict
3785 * |[<!-- language="C" -->
3787 * add_to_count (GVariant *orig,
3790 * GVariantDict *dict;
3794 * dict = g_variant_dict_new (orig);
3796 * if (g_variant_dict_lookup (dict, "count", "u", &count))
3798 * g_variant_dict_insert (dict, "count", "u", count + 1);
3799 * result = g_variant_dict_end (dict);
3803 * g_set_error (...);
3807 * g_variant_dict_unref (dict);
3821 G_STATIC_ASSERT (sizeof (struct stack_dict
) <= sizeof (GVariantDict
));
3825 struct stack_dict dict
;
3830 #define GVSD(d) ((struct stack_dict *) (d))
3831 #define GVHD(d) ((struct heap_dict *) (d))
3832 #define GVSD_MAGIC ((gsize) 2579507750u)
3833 #define GVSD_MAGIC_PARTIAL ((gsize) 3488698669u)
3834 #define GVHD_MAGIC ((gsize) 2450270775u)
3835 #define is_valid_dict(d) (d != NULL && \
3836 GVSD(d)->magic == GVSD_MAGIC)
3837 #define is_valid_heap_dict(d) (GVHD(d)->magic == GVHD_MAGIC)
3839 /* Just to make sure that by adding a union to GVariantDict, we didn't
3840 * accidentally change ABI. */
3841 G_STATIC_ASSERT (sizeof (GVariantDict
) == sizeof (gsize
[16]));
3844 ensure_valid_dict (GVariantDict
*dict
)
3846 if (is_valid_dict (dict
))
3848 if (dict
->u
.s
.partial_magic
== GVSD_MAGIC_PARTIAL
)
3850 static GVariantDict cleared_dict
;
3852 /* Make sure that only first two fields were set and the rest is
3853 * zeroed to avoid messing up the builder that had parent
3854 * address equal to GVSB_MAGIC_PARTIAL. */
3855 if (memcmp (cleared_dict
.u
.s
.y
, dict
->u
.s
.y
, sizeof cleared_dict
.u
.s
.y
))
3858 g_variant_dict_init (dict
, dict
->u
.s
.asv
);
3860 return is_valid_dict (dict
);
3864 * g_variant_dict_new:
3865 * @from_asv: (nullable): the #GVariant with which to initialise the
3868 * Allocates and initialises a new #GVariantDict.
3870 * You should call g_variant_dict_unref() on the return value when it
3871 * is no longer needed. The memory will not be automatically freed by
3874 * In some cases it may be easier to place a #GVariantDict directly on
3875 * the stack of the calling function and initialise it with
3876 * g_variant_dict_init(). This is particularly useful when you are
3877 * using #GVariantDict to construct a #GVariant.
3879 * Returns: (transfer full): a #GVariantDict
3884 g_variant_dict_new (GVariant
*from_asv
)
3888 dict
= g_slice_alloc (sizeof (struct heap_dict
));
3889 g_variant_dict_init (dict
, from_asv
);
3890 GVHD(dict
)->magic
= GVHD_MAGIC
;
3891 GVHD(dict
)->ref_count
= 1;
3897 * g_variant_dict_init: (skip)
3898 * @dict: a #GVariantDict
3899 * @from_asv: (nullable): the initial value for @dict
3901 * Initialises a #GVariantDict structure.
3903 * If @from_asv is given, it is used to initialise the dictionary.
3905 * This function completely ignores the previous contents of @dict. On
3906 * one hand this means that it is valid to pass in completely
3907 * uninitialised memory. On the other hand, this means that if you are
3908 * initialising over top of an existing #GVariantDict you need to first
3909 * call g_variant_dict_clear() in order to avoid leaking memory.
3911 * You must not call g_variant_dict_ref() or g_variant_dict_unref() on a
3912 * #GVariantDict that was initialised with this function. If you ever
3913 * pass a reference to a #GVariantDict outside of the control of your
3914 * own code then you should assume that the person receiving that
3915 * reference may try to use reference counting; you should use
3916 * g_variant_dict_new() instead of this function.
3921 g_variant_dict_init (GVariantDict
*dict
,
3928 GVSD(dict
)->values
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_variant_unref
);
3929 GVSD(dict
)->magic
= GVSD_MAGIC
;
3933 g_variant_iter_init (&iter
, from_asv
);
3934 while (g_variant_iter_next (&iter
, "{sv}", &key
, &value
))
3935 g_hash_table_insert (GVSD(dict
)->values
, key
, value
);
3940 * g_variant_dict_lookup:
3941 * @dict: a #GVariantDict
3942 * @key: the key to lookup in the dictionary
3943 * @format_string: a GVariant format string
3944 * @...: the arguments to unpack the value into
3946 * Looks up a value in a #GVariantDict.
3948 * This function is a wrapper around g_variant_dict_lookup_value() and
3949 * g_variant_get(). In the case that %NULL would have been returned,
3950 * this function returns %FALSE. Otherwise, it unpacks the returned
3951 * value and returns %TRUE.
3953 * @format_string determines the C types that are used for unpacking the
3954 * values and also determines if the values are copied or borrowed, see the
3955 * section on [GVariant format strings][gvariant-format-strings-pointers].
3957 * Returns: %TRUE if a value was unpacked
3962 g_variant_dict_lookup (GVariantDict
*dict
,
3964 const gchar
*format_string
,
3970 g_return_val_if_fail (ensure_valid_dict (dict
), FALSE
);
3971 g_return_val_if_fail (key
!= NULL
, FALSE
);
3972 g_return_val_if_fail (format_string
!= NULL
, FALSE
);
3974 value
= g_hash_table_lookup (GVSD(dict
)->values
, key
);
3976 if (value
== NULL
|| !g_variant_check_format_string (value
, format_string
, FALSE
))
3979 va_start (ap
, format_string
);
3980 g_variant_get_va (value
, format_string
, NULL
, &ap
);
3987 * g_variant_dict_lookup_value:
3988 * @dict: a #GVariantDict
3989 * @key: the key to lookup in the dictionary
3990 * @expected_type: (nullable): a #GVariantType, or %NULL
3992 * Looks up a value in a #GVariantDict.
3994 * If @key is not found in @dictionary, %NULL is returned.
3996 * The @expected_type string specifies what type of value is expected.
3997 * If the value associated with @key has a different type then %NULL is
4000 * If the key is found and the value has the correct type, it is
4001 * returned. If @expected_type was specified then any non-%NULL return
4002 * value will have this type.
4004 * Returns: (transfer full): the value of the dictionary key, or %NULL
4009 g_variant_dict_lookup_value (GVariantDict
*dict
,
4011 const GVariantType
*expected_type
)
4015 g_return_val_if_fail (ensure_valid_dict (dict
), NULL
);
4016 g_return_val_if_fail (key
!= NULL
, NULL
);
4018 result
= g_hash_table_lookup (GVSD(dict
)->values
, key
);
4020 if (result
&& (!expected_type
|| g_variant_is_of_type (result
, expected_type
)))
4021 return g_variant_ref (result
);
4027 * g_variant_dict_contains:
4028 * @dict: a #GVariantDict
4029 * @key: the key to lookup in the dictionary
4031 * Checks if @key exists in @dict.
4033 * Returns: %TRUE if @key is in @dict
4038 g_variant_dict_contains (GVariantDict
*dict
,
4041 g_return_val_if_fail (ensure_valid_dict (dict
), FALSE
);
4042 g_return_val_if_fail (key
!= NULL
, FALSE
);
4044 return g_hash_table_contains (GVSD(dict
)->values
, key
);
4048 * g_variant_dict_insert:
4049 * @dict: a #GVariantDict
4050 * @key: the key to insert a value for
4051 * @format_string: a #GVariant varargs format string
4052 * @...: arguments, as per @format_string
4054 * Inserts a value into a #GVariantDict.
4056 * This call is a convenience wrapper that is exactly equivalent to
4057 * calling g_variant_new() followed by g_variant_dict_insert_value().
4062 g_variant_dict_insert (GVariantDict
*dict
,
4064 const gchar
*format_string
,
4069 g_return_if_fail (ensure_valid_dict (dict
));
4070 g_return_if_fail (key
!= NULL
);
4071 g_return_if_fail (format_string
!= NULL
);
4073 va_start (ap
, format_string
);
4074 g_variant_dict_insert_value (dict
, key
, g_variant_new_va (format_string
, NULL
, &ap
));
4079 * g_variant_dict_insert_value:
4080 * @dict: a #GVariantDict
4081 * @key: the key to insert a value for
4082 * @value: the value to insert
4084 * Inserts (or replaces) a key in a #GVariantDict.
4086 * @value is consumed if it is floating.
4091 g_variant_dict_insert_value (GVariantDict
*dict
,
4095 g_return_if_fail (ensure_valid_dict (dict
));
4096 g_return_if_fail (key
!= NULL
);
4097 g_return_if_fail (value
!= NULL
);
4099 g_hash_table_insert (GVSD(dict
)->values
, g_strdup (key
), g_variant_ref_sink (value
));
4103 * g_variant_dict_remove:
4104 * @dict: a #GVariantDict
4105 * @key: the key to remove
4107 * Removes a key and its associated value from a #GVariantDict.
4109 * Returns: %TRUE if the key was found and removed
4114 g_variant_dict_remove (GVariantDict
*dict
,
4117 g_return_val_if_fail (ensure_valid_dict (dict
), FALSE
);
4118 g_return_val_if_fail (key
!= NULL
, FALSE
);
4120 return g_hash_table_remove (GVSD(dict
)->values
, key
);
4124 * g_variant_dict_clear:
4125 * @dict: a #GVariantDict
4127 * Releases all memory associated with a #GVariantDict without freeing
4128 * the #GVariantDict structure itself.
4130 * It typically only makes sense to do this on a stack-allocated
4131 * #GVariantDict if you want to abort building the value part-way
4132 * through. This function need not be called if you call
4133 * g_variant_dict_end() and it also doesn't need to be called on dicts
4134 * allocated with g_variant_dict_new (see g_variant_dict_unref() for
4137 * It is valid to call this function on either an initialised
4138 * #GVariantDict or one that was previously cleared by an earlier call
4139 * to g_variant_dict_clear() but it is not valid to call this function
4140 * on uninitialised memory.
4145 g_variant_dict_clear (GVariantDict
*dict
)
4147 if (GVSD(dict
)->magic
== 0)
4148 /* all-zeros case */
4151 g_return_if_fail (ensure_valid_dict (dict
));
4153 g_hash_table_unref (GVSD(dict
)->values
);
4154 GVSD(dict
)->values
= NULL
;
4156 GVSD(dict
)->magic
= 0;
4160 * g_variant_dict_end:
4161 * @dict: a #GVariantDict
4163 * Returns the current value of @dict as a #GVariant of type
4164 * %G_VARIANT_TYPE_VARDICT, clearing it in the process.
4166 * It is not permissible to use @dict in any way after this call except
4167 * for reference counting operations (in the case of a heap-allocated
4168 * #GVariantDict) or by reinitialising it with g_variant_dict_init() (in
4169 * the case of stack-allocated).
4171 * Returns: (transfer none): a new, floating, #GVariant
4176 g_variant_dict_end (GVariantDict
*dict
)
4178 GVariantBuilder builder
;
4179 GHashTableIter iter
;
4180 gpointer key
, value
;
4182 g_return_val_if_fail (ensure_valid_dict (dict
), NULL
);
4184 g_variant_builder_init (&builder
, G_VARIANT_TYPE_VARDICT
);
4186 g_hash_table_iter_init (&iter
, GVSD(dict
)->values
);
4187 while (g_hash_table_iter_next (&iter
, &key
, &value
))
4188 g_variant_builder_add (&builder
, "{sv}", (const gchar
*) key
, (GVariant
*) value
);
4190 g_variant_dict_clear (dict
);
4192 return g_variant_builder_end (&builder
);
4196 * g_variant_dict_ref:
4197 * @dict: a heap-allocated #GVariantDict
4199 * Increases the reference count on @dict.
4201 * Don't call this on stack-allocated #GVariantDict instances or bad
4202 * things will happen.
4204 * Returns: (transfer full): a new reference to @dict
4209 g_variant_dict_ref (GVariantDict
*dict
)
4211 g_return_val_if_fail (is_valid_heap_dict (dict
), NULL
);
4213 GVHD(dict
)->ref_count
++;
4219 * g_variant_dict_unref:
4220 * @dict: (transfer full): a heap-allocated #GVariantDict
4222 * Decreases the reference count on @dict.
4224 * In the event that there are no more references, releases all memory
4225 * associated with the #GVariantDict.
4227 * Don't call this on stack-allocated #GVariantDict instances or bad
4228 * things will happen.
4233 g_variant_dict_unref (GVariantDict
*dict
)
4235 g_return_if_fail (is_valid_heap_dict (dict
));
4237 if (--GVHD(dict
)->ref_count
== 0)
4239 g_variant_dict_clear (dict
);
4240 g_slice_free (struct heap_dict
, (struct heap_dict
*) dict
);
4245 /* Format strings {{{1 */
4247 * g_variant_format_string_scan:
4248 * @string: a string that may be prefixed with a format string
4249 * @limit: (nullable) (default NULL): a pointer to the end of @string,
4251 * @endptr: (nullable) (default NULL): location to store the end pointer,
4254 * Checks the string pointed to by @string for starting with a properly
4255 * formed #GVariant varargs format string. If no valid format string is
4256 * found then %FALSE is returned.
4258 * If @string does start with a valid format string then %TRUE is
4259 * returned. If @endptr is non-%NULL then it is updated to point to the
4260 * first character after the format string.
4262 * If @limit is non-%NULL then @limit (and any character after it) will
4263 * not be accessed and the effect is otherwise equivalent to if the
4264 * character at @limit were nul.
4266 * See the section on [GVariant format strings][gvariant-format-strings].
4268 * Returns: %TRUE if there was a valid format string
4273 g_variant_format_string_scan (const gchar
*string
,
4275 const gchar
**endptr
)
4277 #define next_char() (string == limit ? '\0' : *string++)
4278 #define peek_char() (string == limit ? '\0' : *string)
4281 switch (next_char())
4283 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
4284 case 'x': case 't': case 'h': case 'd': case 's': case 'o':
4285 case 'g': case 'v': case '*': case '?': case 'r':
4289 return g_variant_format_string_scan (string
, limit
, endptr
);
4293 return g_variant_type_string_scan (string
, limit
, endptr
);
4296 while (peek_char() != ')')
4297 if (!g_variant_format_string_scan (string
, limit
, &string
))
4300 next_char(); /* consume ')' */
4310 if (c
!= 's' && c
!= 'o' && c
!= 'g')
4318 /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
4319 * The terminating null character is considered to be
4320 * part of the string.
4322 if (c
!= '\0' && strchr ("bynqiuxthdsog?", c
) == NULL
)
4326 if (!g_variant_format_string_scan (string
, limit
, &string
))
4329 if (next_char() != '}')
4335 if ((c
= next_char()) == 'a')
4337 if ((c
= next_char()) == '&')
4339 if ((c
= next_char()) == 'a')
4341 if ((c
= next_char()) == 'y')
4342 break; /* '^a&ay' */
4345 else if (c
== 's' || c
== 'o')
4346 break; /* '^a&s', '^a&o' */
4351 if ((c
= next_char()) == 'y')
4355 else if (c
== 's' || c
== 'o')
4356 break; /* '^as', '^ao' */
4363 if ((c
= next_char()) == 'a')
4365 if ((c
= next_char()) == 'y')
4375 if (c
!= 's' && c
!= 'o' && c
!= 'g')
4394 * g_variant_check_format_string:
4395 * @value: a #GVariant
4396 * @format_string: a valid #GVariant format string
4397 * @copy_only: %TRUE to ensure the format string makes deep copies
4399 * Checks if calling g_variant_get() with @format_string on @value would
4400 * be valid from a type-compatibility standpoint. @format_string is
4401 * assumed to be a valid format string (from a syntactic standpoint).
4403 * If @copy_only is %TRUE then this function additionally checks that it
4404 * would be safe to call g_variant_unref() on @value immediately after
4405 * the call to g_variant_get() without invalidating the result. This is
4406 * only possible if deep copies are made (ie: there are no pointers to
4407 * the data inside of the soon-to-be-freed #GVariant instance). If this
4408 * check fails then a g_critical() is printed and %FALSE is returned.
4410 * This function is meant to be used by functions that wish to provide
4411 * varargs accessors to #GVariant values of uncertain values (eg:
4412 * g_variant_lookup() or g_menu_model_get_item_attribute()).
4414 * Returns: %TRUE if @format_string is safe to use
4419 g_variant_check_format_string (GVariant
*value
,
4420 const gchar
*format_string
,
4423 const gchar
*original_format
= format_string
;
4424 const gchar
*type_string
;
4426 /* Interesting factoid: assuming a format string is valid, it can be
4427 * converted to a type string by removing all '@' '&' and '^'
4430 * Instead of doing that, we can just skip those characters when
4431 * comparing it to the type string of @value.
4433 * For the copy-only case we can just drop the '&' from the list of
4434 * characters to skip over. A '&' will never appear in a type string
4435 * so we know that it won't be possible to return %TRUE if it is in a
4438 type_string
= g_variant_get_type_string (value
);
4440 while (*type_string
|| *format_string
)
4442 gchar format
= *format_string
++;
4447 if G_UNLIKELY (copy_only
)
4449 /* for the love of all that is good, please don't mark this string for translation... */
4450 g_critical ("g_variant_check_format_string() is being called by a function with a GVariant varargs "
4451 "interface to validate the passed format string for type safety. The passed format "
4452 "(%s) contains a '&' character which would result in a pointer being returned to the "
4453 "data inside of a GVariant instance that may no longer exist by the time the function "
4454 "returns. Modify your code to use a format string without '&'.", original_format
);
4461 /* ignore these 2 (or 3) */
4465 /* attempt to consume one of 'bynqiuxthdsog' */
4467 char s
= *type_string
++;
4469 if (s
== '\0' || strchr ("bynqiuxthdsog", s
) == NULL
)
4475 /* ensure it's a tuple */
4476 if (*type_string
!= '(')
4481 /* consume a full type string for the '*' or 'r' */
4482 if (!g_variant_type_string_scan (type_string
, NULL
, &type_string
))
4488 /* attempt to consume exactly one character equal to the format */
4489 if (format
!= *type_string
++)
4498 * g_variant_format_string_scan_type:
4499 * @string: a string that may be prefixed with a format string
4500 * @limit: (nullable) (default NULL): a pointer to the end of @string,
4502 * @endptr: (nullable) (default NULL): location to store the end pointer,
4505 * If @string starts with a valid format string then this function will
4506 * return the type that the format string corresponds to. Otherwise
4507 * this function returns %NULL.
4509 * Use g_variant_type_free() to free the return value when you no longer
4512 * This function is otherwise exactly like
4513 * g_variant_format_string_scan().
4515 * Returns: (nullable): a #GVariantType if there was a valid format string
4520 g_variant_format_string_scan_type (const gchar
*string
,
4522 const gchar
**endptr
)
4524 const gchar
*my_end
;
4531 if (!g_variant_format_string_scan (string
, limit
, endptr
))
4534 dest
= new = g_malloc (*endptr
- string
+ 1);
4535 while (string
!= *endptr
)
4537 if (*string
!= '@' && *string
!= '&' && *string
!= '^')
4543 return (GVariantType
*) G_VARIANT_TYPE (new);
4547 valid_format_string (const gchar
*format_string
,
4551 const gchar
*endptr
;
4554 type
= g_variant_format_string_scan_type (format_string
, NULL
, &endptr
);
4556 if G_UNLIKELY (type
== NULL
|| (single
&& *endptr
!= '\0'))
4559 g_critical ("'%s' is not a valid GVariant format string",
4562 g_critical ("'%s' does not have a valid GVariant format "
4563 "string as a prefix", format_string
);
4566 g_variant_type_free (type
);
4571 if G_UNLIKELY (value
&& !g_variant_is_of_type (value
, type
))
4576 fragment
= g_strndup (format_string
, endptr
- format_string
);
4577 typestr
= g_variant_type_dup_string (type
);
4579 g_critical ("the GVariant format string '%s' has a type of "
4580 "'%s' but the given value has a type of '%s'",
4581 fragment
, typestr
, g_variant_get_type_string (value
));
4583 g_variant_type_free (type
);
4590 g_variant_type_free (type
);
4595 /* Variable Arguments {{{1 */
4596 /* We consider 2 main classes of format strings:
4598 * - recursive format strings
4599 * these are ones that result in recursion and the collection of
4600 * possibly more than one argument. Maybe types, tuples,
4601 * dictionary entries.
4603 * - leaf format string
4604 * these result in the collection of a single argument.
4606 * Leaf format strings are further subdivided into two categories:
4608 * - single non-null pointer ("nnp")
4609 * these either collect or return a single non-null pointer.
4612 * these collect or return something else (bool, number, etc).
4614 * Based on the above, the varargs handling code is split into 4 main parts:
4616 * - nnp handling code
4617 * - leaf handling code (which may invoke nnp code)
4618 * - generic handling code (may be recursive, may invoke leaf code)
4619 * - user-facing API (which invokes the generic code)
4621 * Each section implements some of the following functions:
4624 * collect the arguments for the format string as if
4625 * g_variant_new() had been called, but do nothing with them. used
4626 * for skipping over arguments when constructing a Nothing maybe
4630 * create a GVariant *
4633 * unpack a GVariant *
4635 * - free (nnp only):
4636 * free a previously allocated item
4640 g_variant_format_string_is_leaf (const gchar
*str
)
4642 return str
[0] != 'm' && str
[0] != '(' && str
[0] != '{';
4646 g_variant_format_string_is_nnp (const gchar
*str
)
4648 return str
[0] == 'a' || str
[0] == 's' || str
[0] == 'o' || str
[0] == 'g' ||
4649 str
[0] == '^' || str
[0] == '@' || str
[0] == '*' || str
[0] == '?' ||
4650 str
[0] == 'r' || str
[0] == 'v' || str
[0] == '&';
4653 /* Single non-null pointer ("nnp") {{{2 */
4655 g_variant_valist_free_nnp (const gchar
*str
,
4661 g_variant_iter_free (ptr
);
4665 if (str
[2] != '&') /* '^as', '^ao' */
4667 else /* '^a&s', '^a&o' */
4681 g_variant_unref (ptr
);
4688 g_assert_not_reached ();
4693 g_variant_scan_convenience (const gchar
**str
,
4716 g_variant_valist_new_nnp (const gchar
**str
,
4727 const GVariantType
*type
;
4730 value
= g_variant_builder_end (ptr
);
4731 type
= g_variant_get_type (value
);
4733 if G_UNLIKELY (!g_variant_type_is_array (type
))
4734 g_error ("g_variant_new: expected array GVariantBuilder but "
4735 "the built value has type '%s'",
4736 g_variant_get_type_string (value
));
4738 type
= g_variant_type_element (type
);
4740 if G_UNLIKELY (!g_variant_type_is_subtype_of (type
, (GVariantType
*) *str
))
4741 g_error ("g_variant_new: expected GVariantBuilder array element "
4742 "type '%s' but the built value has element type '%s'",
4743 g_variant_type_dup_string ((GVariantType
*) *str
),
4744 g_variant_get_type_string (value
) + 1);
4746 g_variant_type_string_scan (*str
, NULL
, str
);
4752 /* special case: NULL pointer for empty array */
4754 const GVariantType
*type
= (GVariantType
*) *str
;
4756 g_variant_type_string_scan (*str
, NULL
, str
);
4758 if G_UNLIKELY (!g_variant_type_is_definite (type
))
4759 g_error ("g_variant_new: NULL pointer given with indefinite "
4760 "array type; unable to determine which type of empty "
4761 "array to construct.");
4763 return g_variant_new_array (type
, NULL
, 0);
4770 value
= g_variant_new_string (ptr
);
4773 value
= g_variant_new_string ("[Invalid UTF-8]");
4779 return g_variant_new_object_path (ptr
);
4782 return g_variant_new_signature (ptr
);
4790 type
= g_variant_scan_convenience (str
, &constant
, &arrays
);
4793 return g_variant_new_strv (ptr
, -1);
4796 return g_variant_new_objv (ptr
, -1);
4799 return g_variant_new_bytestring_array (ptr
, -1);
4801 return g_variant_new_bytestring (ptr
);
4805 if G_UNLIKELY (!g_variant_is_of_type (ptr
, (GVariantType
*) *str
))
4806 g_error ("g_variant_new: expected GVariant of type '%s' but "
4807 "received value has type '%s'",
4808 g_variant_type_dup_string ((GVariantType
*) *str
),
4809 g_variant_get_type_string (ptr
));
4811 g_variant_type_string_scan (*str
, NULL
, str
);
4819 if G_UNLIKELY (!g_variant_type_is_basic (g_variant_get_type (ptr
)))
4820 g_error ("g_variant_new: format string '?' expects basic-typed "
4821 "GVariant, but received value has type '%s'",
4822 g_variant_get_type_string (ptr
));
4827 if G_UNLIKELY (!g_variant_type_is_tuple (g_variant_get_type (ptr
)))
4828 g_error ("g_variant_new: format string 'r' expects tuple-typed "
4829 "GVariant, but received value has type '%s'",
4830 g_variant_get_type_string (ptr
));
4835 return g_variant_new_variant (ptr
);
4838 g_assert_not_reached ();
4843 g_variant_valist_get_nnp (const gchar
**str
,
4849 g_variant_type_string_scan (*str
, NULL
, str
);
4850 return g_variant_iter_new (value
);
4854 return (gchar
*) g_variant_get_string (value
, NULL
);
4859 return g_variant_dup_string (value
, NULL
);
4867 type
= g_variant_scan_convenience (str
, &constant
, &arrays
);
4872 return g_variant_get_strv (value
, NULL
);
4874 return g_variant_dup_strv (value
, NULL
);
4877 else if (type
== 'o')
4880 return g_variant_get_objv (value
, NULL
);
4882 return g_variant_dup_objv (value
, NULL
);
4885 else if (arrays
> 1)
4888 return g_variant_get_bytestring_array (value
, NULL
);
4890 return g_variant_dup_bytestring_array (value
, NULL
);
4896 return (gchar
*) g_variant_get_bytestring (value
);
4898 return g_variant_dup_bytestring (value
, NULL
);
4903 g_variant_type_string_scan (*str
, NULL
, str
);
4909 return g_variant_ref (value
);
4912 return g_variant_get_variant (value
);
4915 g_assert_not_reached ();
4921 g_variant_valist_skip_leaf (const gchar
**str
,
4924 if (g_variant_format_string_is_nnp (*str
))
4926 g_variant_format_string_scan (*str
, NULL
, str
);
4927 va_arg (*app
, gpointer
);
4945 va_arg (*app
, guint64
);
4949 va_arg (*app
, gdouble
);
4953 g_assert_not_reached ();
4958 g_variant_valist_new_leaf (const gchar
**str
,
4961 if (g_variant_format_string_is_nnp (*str
))
4962 return g_variant_valist_new_nnp (str
, va_arg (*app
, gpointer
));
4967 return g_variant_new_boolean (va_arg (*app
, gboolean
));
4970 return g_variant_new_byte (va_arg (*app
, guint
));
4973 return g_variant_new_int16 (va_arg (*app
, gint
));
4976 return g_variant_new_uint16 (va_arg (*app
, guint
));
4979 return g_variant_new_int32 (va_arg (*app
, gint
));
4982 return g_variant_new_uint32 (va_arg (*app
, guint
));
4985 return g_variant_new_int64 (va_arg (*app
, gint64
));
4988 return g_variant_new_uint64 (va_arg (*app
, guint64
));
4991 return g_variant_new_handle (va_arg (*app
, gint
));
4994 return g_variant_new_double (va_arg (*app
, gdouble
));
4997 g_assert_not_reached ();
5001 /* The code below assumes this */
5002 G_STATIC_ASSERT (sizeof (gboolean
) == sizeof (guint32
));
5003 G_STATIC_ASSERT (sizeof (gdouble
) == sizeof (guint64
));
5006 g_variant_valist_get_leaf (const gchar
**str
,
5011 gpointer ptr
= va_arg (*app
, gpointer
);
5015 g_variant_format_string_scan (*str
, NULL
, str
);
5019 if (g_variant_format_string_is_nnp (*str
))
5021 gpointer
*nnp
= (gpointer
*) ptr
;
5023 if (free
&& *nnp
!= NULL
)
5024 g_variant_valist_free_nnp (*str
, *nnp
);
5029 *nnp
= g_variant_valist_get_nnp (str
, value
);
5031 g_variant_format_string_scan (*str
, NULL
, str
);
5041 *(gboolean
*) ptr
= g_variant_get_boolean (value
);
5045 *(guchar
*) ptr
= g_variant_get_byte (value
);
5049 *(gint16
*) ptr
= g_variant_get_int16 (value
);
5053 *(guint16
*) ptr
= g_variant_get_uint16 (value
);
5057 *(gint32
*) ptr
= g_variant_get_int32 (value
);
5061 *(guint32
*) ptr
= g_variant_get_uint32 (value
);
5065 *(gint64
*) ptr
= g_variant_get_int64 (value
);
5069 *(guint64
*) ptr
= g_variant_get_uint64 (value
);
5073 *(gint32
*) ptr
= g_variant_get_handle (value
);
5077 *(gdouble
*) ptr
= g_variant_get_double (value
);
5086 *(guchar
*) ptr
= 0;
5091 *(guint16
*) ptr
= 0;
5098 *(guint32
*) ptr
= 0;
5104 *(guint64
*) ptr
= 0;
5109 g_assert_not_reached ();
5112 /* Generic (recursive) {{{2 */
5114 g_variant_valist_skip (const gchar
**str
,
5117 if (g_variant_format_string_is_leaf (*str
))
5118 g_variant_valist_skip_leaf (str
, app
);
5120 else if (**str
== 'm') /* maybe */
5124 if (!g_variant_format_string_is_nnp (*str
))
5125 va_arg (*app
, gboolean
);
5127 g_variant_valist_skip (str
, app
);
5129 else /* tuple, dictionary entry */
5131 g_assert (**str
== '(' || **str
== '{');
5133 while (**str
!= ')' && **str
!= '}')
5134 g_variant_valist_skip (str
, app
);
5140 g_variant_valist_new (const gchar
**str
,
5143 if (g_variant_format_string_is_leaf (*str
))
5144 return g_variant_valist_new_leaf (str
, app
);
5146 if (**str
== 'm') /* maybe */
5148 GVariantType
*type
= NULL
;
5149 GVariant
*value
= NULL
;
5153 if (g_variant_format_string_is_nnp (*str
))
5155 gpointer nnp
= va_arg (*app
, gpointer
);
5158 value
= g_variant_valist_new_nnp (str
, nnp
);
5160 type
= g_variant_format_string_scan_type (*str
, NULL
, str
);
5164 gboolean just
= va_arg (*app
, gboolean
);
5167 value
= g_variant_valist_new (str
, app
);
5170 type
= g_variant_format_string_scan_type (*str
, NULL
, NULL
);
5171 g_variant_valist_skip (str
, app
);
5175 value
= g_variant_new_maybe (type
, value
);
5178 g_variant_type_free (type
);
5182 else /* tuple, dictionary entry */
5187 g_variant_builder_init (&b
, G_VARIANT_TYPE_TUPLE
);
5190 g_assert (**str
== '{');
5191 g_variant_builder_init (&b
, G_VARIANT_TYPE_DICT_ENTRY
);
5195 while (**str
!= ')' && **str
!= '}')
5196 g_variant_builder_add_value (&b
, g_variant_valist_new (str
, app
));
5199 return g_variant_builder_end (&b
);
5204 g_variant_valist_get (const gchar
**str
,
5209 if (g_variant_format_string_is_leaf (*str
))
5210 g_variant_valist_get_leaf (str
, value
, free
, app
);
5212 else if (**str
== 'm')
5217 value
= g_variant_get_maybe (value
);
5219 if (!g_variant_format_string_is_nnp (*str
))
5221 gboolean
*ptr
= va_arg (*app
, gboolean
*);
5224 *ptr
= value
!= NULL
;
5227 g_variant_valist_get (str
, value
, free
, app
);
5230 g_variant_unref (value
);
5233 else /* tuple, dictionary entry */
5237 g_assert (**str
== '(' || **str
== '{');
5240 while (**str
!= ')' && **str
!= '}')
5244 GVariant
*child
= g_variant_get_child_value (value
, index
++);
5245 g_variant_valist_get (str
, child
, free
, app
);
5246 g_variant_unref (child
);
5249 g_variant_valist_get (str
, NULL
, free
, app
);
5255 /* User-facing API {{{2 */
5257 * g_variant_new: (skip)
5258 * @format_string: a #GVariant format string
5259 * @...: arguments, as per @format_string
5261 * Creates a new #GVariant instance.
5263 * Think of this function as an analogue to g_strdup_printf().
5265 * The type of the created instance and the arguments that are expected
5266 * by this function are determined by @format_string. See the section on
5267 * [GVariant format strings][gvariant-format-strings]. Please note that
5268 * the syntax of the format string is very likely to be extended in the
5271 * The first character of the format string must not be '*' '?' '@' or
5272 * 'r'; in essence, a new #GVariant must always be constructed by this
5273 * function (and not merely passed through it unmodified).
5275 * Note that the arguments must be of the correct width for their types
5276 * specified in @format_string. This can be achieved by casting them. See
5277 * the [GVariant varargs documentation][gvariant-varargs].
5279 * |[<!-- language="C" -->
5280 * MyFlags some_flags = FLAG_ONE | FLAG_TWO;
5281 * const gchar *some_strings[] = { "a", "b", "c", NULL };
5282 * GVariant *new_variant;
5284 * new_variant = g_variant_new ("(t^as)",
5285 * // This cast is required.
5286 * (guint64) some_flags,
5290 * Returns: a new floating #GVariant instance
5295 g_variant_new (const gchar
*format_string
,
5301 g_return_val_if_fail (valid_format_string (format_string
, TRUE
, NULL
) &&
5302 format_string
[0] != '?' && format_string
[0] != '@' &&
5303 format_string
[0] != '*' && format_string
[0] != 'r',
5306 va_start (ap
, format_string
);
5307 value
= g_variant_new_va (format_string
, NULL
, &ap
);
5314 * g_variant_new_va: (skip)
5315 * @format_string: a string that is prefixed with a format string
5316 * @endptr: (nullable) (default NULL): location to store the end pointer,
5318 * @app: a pointer to a #va_list
5320 * This function is intended to be used by libraries based on
5321 * #GVariant that want to provide g_variant_new()-like functionality
5324 * The API is more general than g_variant_new() to allow a wider range
5327 * @format_string must still point to a valid format string, but it only
5328 * needs to be nul-terminated if @endptr is %NULL. If @endptr is
5329 * non-%NULL then it is updated to point to the first character past the
5330 * end of the format string.
5332 * @app is a pointer to a #va_list. The arguments, according to
5333 * @format_string, are collected from this #va_list and the list is left
5334 * pointing to the argument following the last.
5336 * Note that the arguments in @app must be of the correct width for their
5337 * types specified in @format_string when collected into the #va_list.
5338 * See the [GVariant varargs documentation][gvariant-varargs].
5340 * These two generalisations allow mixing of multiple calls to
5341 * g_variant_new_va() and g_variant_get_va() within a single actual
5342 * varargs call by the user.
5344 * The return value will be floating if it was a newly created GVariant
5345 * instance (for example, if the format string was "(ii)"). In the case
5346 * that the format_string was '*', '?', 'r', or a format starting with
5347 * '@' then the collected #GVariant pointer will be returned unmodified,
5348 * without adding any additional references.
5350 * In order to behave correctly in all cases it is necessary for the
5351 * calling function to g_variant_ref_sink() the return result before
5352 * returning control to the user that originally provided the pointer.
5353 * At this point, the caller will have their own full reference to the
5354 * result. This can also be done by adding the result to a container,
5355 * or by passing it to another g_variant_new() call.
5357 * Returns: a new, usually floating, #GVariant
5362 g_variant_new_va (const gchar
*format_string
,
5363 const gchar
**endptr
,
5368 g_return_val_if_fail (valid_format_string (format_string
, !endptr
, NULL
),
5370 g_return_val_if_fail (app
!= NULL
, NULL
);
5372 value
= g_variant_valist_new (&format_string
, app
);
5375 *endptr
= format_string
;
5381 * g_variant_get: (skip)
5382 * @value: a #GVariant instance
5383 * @format_string: a #GVariant format string
5384 * @...: arguments, as per @format_string
5386 * Deconstructs a #GVariant instance.
5388 * Think of this function as an analogue to scanf().
5390 * The arguments that are expected by this function are entirely
5391 * determined by @format_string. @format_string also restricts the
5392 * permissible types of @value. It is an error to give a value with
5393 * an incompatible type. See the section on
5394 * [GVariant format strings][gvariant-format-strings].
5395 * Please note that the syntax of the format string is very likely to be
5396 * extended in the future.
5398 * @format_string determines the C types that are used for unpacking
5399 * the values and also determines if the values are copied or borrowed,
5400 * see the section on
5401 * [GVariant format strings][gvariant-format-strings-pointers].
5406 g_variant_get (GVariant
*value
,
5407 const gchar
*format_string
,
5412 g_return_if_fail (valid_format_string (format_string
, TRUE
, value
));
5414 /* if any direct-pointer-access formats are in use, flatten first */
5415 if (strchr (format_string
, '&'))
5416 g_variant_get_data (value
);
5418 va_start (ap
, format_string
);
5419 g_variant_get_va (value
, format_string
, NULL
, &ap
);
5424 * g_variant_get_va: (skip)
5425 * @value: a #GVariant
5426 * @format_string: a string that is prefixed with a format string
5427 * @endptr: (nullable) (default NULL): location to store the end pointer,
5429 * @app: a pointer to a #va_list
5431 * This function is intended to be used by libraries based on #GVariant
5432 * that want to provide g_variant_get()-like functionality to their
5435 * The API is more general than g_variant_get() to allow a wider range
5438 * @format_string must still point to a valid format string, but it only
5439 * need to be nul-terminated if @endptr is %NULL. If @endptr is
5440 * non-%NULL then it is updated to point to the first character past the
5441 * end of the format string.
5443 * @app is a pointer to a #va_list. The arguments, according to
5444 * @format_string, are collected from this #va_list and the list is left
5445 * pointing to the argument following the last.
5447 * These two generalisations allow mixing of multiple calls to
5448 * g_variant_new_va() and g_variant_get_va() within a single actual
5449 * varargs call by the user.
5451 * @format_string determines the C types that are used for unpacking
5452 * the values and also determines if the values are copied or borrowed,
5453 * see the section on
5454 * [GVariant format strings][gvariant-format-strings-pointers].
5459 g_variant_get_va (GVariant
*value
,
5460 const gchar
*format_string
,
5461 const gchar
**endptr
,
5464 g_return_if_fail (valid_format_string (format_string
, !endptr
, value
));
5465 g_return_if_fail (value
!= NULL
);
5466 g_return_if_fail (app
!= NULL
);
5468 /* if any direct-pointer-access formats are in use, flatten first */
5469 if (strchr (format_string
, '&'))
5470 g_variant_get_data (value
);
5472 g_variant_valist_get (&format_string
, value
, FALSE
, app
);
5475 *endptr
= format_string
;
5478 /* Varargs-enabled Utility Functions {{{1 */
5481 * g_variant_builder_add: (skip)
5482 * @builder: a #GVariantBuilder
5483 * @format_string: a #GVariant varargs format string
5484 * @...: arguments, as per @format_string
5486 * Adds to a #GVariantBuilder.
5488 * This call is a convenience wrapper that is exactly equivalent to
5489 * calling g_variant_new() followed by g_variant_builder_add_value().
5491 * Note that the arguments must be of the correct width for their types
5492 * specified in @format_string. This can be achieved by casting them. See
5493 * the [GVariant varargs documentation][gvariant-varargs].
5495 * This function might be used as follows:
5497 * |[<!-- language="C" -->
5499 * make_pointless_dictionary (void)
5501 * GVariantBuilder builder;
5504 * g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
5505 * for (i = 0; i < 16; i++)
5509 * sprintf (buf, "%d", i);
5510 * g_variant_builder_add (&builder, "{is}", i, buf);
5513 * return g_variant_builder_end (&builder);
5520 g_variant_builder_add (GVariantBuilder
*builder
,
5521 const gchar
*format_string
,
5527 va_start (ap
, format_string
);
5528 variant
= g_variant_new_va (format_string
, NULL
, &ap
);
5531 g_variant_builder_add_value (builder
, variant
);
5535 * g_variant_get_child: (skip)
5536 * @value: a container #GVariant
5537 * @index_: the index of the child to deconstruct
5538 * @format_string: a #GVariant format string
5539 * @...: arguments, as per @format_string
5541 * Reads a child item out of a container #GVariant instance and
5542 * deconstructs it according to @format_string. This call is
5543 * essentially a combination of g_variant_get_child_value() and
5546 * @format_string determines the C types that are used for unpacking
5547 * the values and also determines if the values are copied or borrowed,
5548 * see the section on
5549 * [GVariant format strings][gvariant-format-strings-pointers].
5554 g_variant_get_child (GVariant
*value
,
5556 const gchar
*format_string
,
5562 /* if any direct-pointer-access formats are in use, flatten first */
5563 if (strchr (format_string
, '&'))
5564 g_variant_get_data (value
);
5566 child
= g_variant_get_child_value (value
, index_
);
5567 g_return_if_fail (valid_format_string (format_string
, TRUE
, child
));
5569 va_start (ap
, format_string
);
5570 g_variant_get_va (child
, format_string
, NULL
, &ap
);
5573 g_variant_unref (child
);
5577 * g_variant_iter_next: (skip)
5578 * @iter: a #GVariantIter
5579 * @format_string: a GVariant format string
5580 * @...: the arguments to unpack the value into
5582 * Gets the next item in the container and unpacks it into the variable
5583 * argument list according to @format_string, returning %TRUE.
5585 * If no more items remain then %FALSE is returned.
5587 * All of the pointers given on the variable arguments list of this
5588 * function are assumed to point at uninitialised memory. It is the
5589 * responsibility of the caller to free all of the values returned by
5590 * the unpacking process.
5592 * Here is an example for memory management with g_variant_iter_next():
5593 * |[<!-- language="C" -->
5594 * // Iterates a dictionary of type 'a{sv}'
5596 * iterate_dictionary (GVariant *dictionary)
5598 * GVariantIter iter;
5602 * g_variant_iter_init (&iter, dictionary);
5603 * while (g_variant_iter_next (&iter, "{sv}", &key, &value))
5605 * g_print ("Item '%s' has type '%s'\n", key,
5606 * g_variant_get_type_string (value));
5608 * // must free data for ourselves
5609 * g_variant_unref (value);
5615 * For a solution that is likely to be more convenient to C programmers
5616 * when dealing with loops, see g_variant_iter_loop().
5618 * @format_string determines the C types that are used for unpacking
5619 * the values and also determines if the values are copied or borrowed.
5621 * See the section on
5622 * [GVariant format strings][gvariant-format-strings-pointers].
5624 * Returns: %TRUE if a value was unpacked, or %FALSE if there as no value
5629 g_variant_iter_next (GVariantIter
*iter
,
5630 const gchar
*format_string
,
5635 value
= g_variant_iter_next_value (iter
);
5637 g_return_val_if_fail (valid_format_string (format_string
, TRUE
, value
),
5644 va_start (ap
, format_string
);
5645 g_variant_valist_get (&format_string
, value
, FALSE
, &ap
);
5648 g_variant_unref (value
);
5651 return value
!= NULL
;
5655 * g_variant_iter_loop: (skip)
5656 * @iter: a #GVariantIter
5657 * @format_string: a GVariant format string
5658 * @...: the arguments to unpack the value into
5660 * Gets the next item in the container and unpacks it into the variable
5661 * argument list according to @format_string, returning %TRUE.
5663 * If no more items remain then %FALSE is returned.
5665 * On the first call to this function, the pointers appearing on the
5666 * variable argument list are assumed to point at uninitialised memory.
5667 * On the second and later calls, it is assumed that the same pointers
5668 * will be given and that they will point to the memory as set by the
5669 * previous call to this function. This allows the previous values to
5670 * be freed, as appropriate.
5672 * This function is intended to be used with a while loop as
5673 * demonstrated in the following example. This function can only be
5674 * used when iterating over an array. It is only valid to call this
5675 * function with a string constant for the format string and the same
5676 * string constant must be used each time. Mixing calls to this
5677 * function and g_variant_iter_next() or g_variant_iter_next_value() on
5678 * the same iterator causes undefined behavior.
5680 * If you break out of a such a while loop using g_variant_iter_loop() then
5681 * you must free or unreference all the unpacked values as you would with
5682 * g_variant_get(). Failure to do so will cause a memory leak.
5684 * Here is an example for memory management with g_variant_iter_loop():
5685 * |[<!-- language="C" -->
5686 * // Iterates a dictionary of type 'a{sv}'
5688 * iterate_dictionary (GVariant *dictionary)
5690 * GVariantIter iter;
5694 * g_variant_iter_init (&iter, dictionary);
5695 * while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
5697 * g_print ("Item '%s' has type '%s'\n", key,
5698 * g_variant_get_type_string (value));
5700 * // no need to free 'key' and 'value' here
5701 * // unless breaking out of this loop
5706 * For most cases you should use g_variant_iter_next().
5708 * This function is really only useful when unpacking into #GVariant or
5709 * #GVariantIter in order to allow you to skip the call to
5710 * g_variant_unref() or g_variant_iter_free().
5712 * For example, if you are only looping over simple integer and string
5713 * types, g_variant_iter_next() is definitely preferred. For string
5714 * types, use the '&' prefix to avoid allocating any memory at all (and
5715 * thereby avoiding the need to free anything as well).
5717 * @format_string determines the C types that are used for unpacking
5718 * the values and also determines if the values are copied or borrowed.
5720 * See the section on
5721 * [GVariant format strings][gvariant-format-strings-pointers].
5723 * Returns: %TRUE if a value was unpacked, or %FALSE if there was no
5729 g_variant_iter_loop (GVariantIter
*iter
,
5730 const gchar
*format_string
,
5733 gboolean first_time
= GVSI(iter
)->loop_format
== NULL
;
5737 g_return_val_if_fail (first_time
||
5738 format_string
== GVSI(iter
)->loop_format
,
5743 TYPE_CHECK (GVSI(iter
)->value
, G_VARIANT_TYPE_ARRAY
, FALSE
);
5744 GVSI(iter
)->loop_format
= format_string
;
5746 if (strchr (format_string
, '&'))
5747 g_variant_get_data (GVSI(iter
)->value
);
5750 value
= g_variant_iter_next_value (iter
);
5752 g_return_val_if_fail (!first_time
||
5753 valid_format_string (format_string
, TRUE
, value
),
5756 va_start (ap
, format_string
);
5757 g_variant_valist_get (&format_string
, value
, !first_time
, &ap
);
5761 g_variant_unref (value
);
5763 return value
!= NULL
;
5766 /* Serialised data {{{1 */
5768 g_variant_deep_copy (GVariant
*value
)
5770 switch (g_variant_classify (value
))
5772 case G_VARIANT_CLASS_MAYBE
:
5773 case G_VARIANT_CLASS_ARRAY
:
5774 case G_VARIANT_CLASS_TUPLE
:
5775 case G_VARIANT_CLASS_DICT_ENTRY
:
5776 case G_VARIANT_CLASS_VARIANT
:
5778 GVariantBuilder builder
;
5782 g_variant_builder_init (&builder
, g_variant_get_type (value
));
5783 g_variant_iter_init (&iter
, value
);
5785 while ((child
= g_variant_iter_next_value (&iter
)))
5787 g_variant_builder_add_value (&builder
, g_variant_deep_copy (child
));
5788 g_variant_unref (child
);
5791 return g_variant_builder_end (&builder
);
5794 case G_VARIANT_CLASS_BOOLEAN
:
5795 return g_variant_new_boolean (g_variant_get_boolean (value
));
5797 case G_VARIANT_CLASS_BYTE
:
5798 return g_variant_new_byte (g_variant_get_byte (value
));
5800 case G_VARIANT_CLASS_INT16
:
5801 return g_variant_new_int16 (g_variant_get_int16 (value
));
5803 case G_VARIANT_CLASS_UINT16
:
5804 return g_variant_new_uint16 (g_variant_get_uint16 (value
));
5806 case G_VARIANT_CLASS_INT32
:
5807 return g_variant_new_int32 (g_variant_get_int32 (value
));
5809 case G_VARIANT_CLASS_UINT32
:
5810 return g_variant_new_uint32 (g_variant_get_uint32 (value
));
5812 case G_VARIANT_CLASS_INT64
:
5813 return g_variant_new_int64 (g_variant_get_int64 (value
));
5815 case G_VARIANT_CLASS_UINT64
:
5816 return g_variant_new_uint64 (g_variant_get_uint64 (value
));
5818 case G_VARIANT_CLASS_HANDLE
:
5819 return g_variant_new_handle (g_variant_get_handle (value
));
5821 case G_VARIANT_CLASS_DOUBLE
:
5822 return g_variant_new_double (g_variant_get_double (value
));
5824 case G_VARIANT_CLASS_STRING
:
5825 return g_variant_new_string (g_variant_get_string (value
, NULL
));
5827 case G_VARIANT_CLASS_OBJECT_PATH
:
5828 return g_variant_new_object_path (g_variant_get_string (value
, NULL
));
5830 case G_VARIANT_CLASS_SIGNATURE
:
5831 return g_variant_new_signature (g_variant_get_string (value
, NULL
));
5834 g_assert_not_reached ();
5838 * g_variant_get_normal_form:
5839 * @value: a #GVariant
5841 * Gets a #GVariant instance that has the same value as @value and is
5842 * trusted to be in normal form.
5844 * If @value is already trusted to be in normal form then a new
5845 * reference to @value is returned.
5847 * If @value is not already trusted, then it is scanned to check if it
5848 * is in normal form. If it is found to be in normal form then it is
5849 * marked as trusted and a new reference to it is returned.
5851 * If @value is found not to be in normal form then a new trusted
5852 * #GVariant is created with the same value as @value.
5854 * It makes sense to call this function if you've received #GVariant
5855 * data from untrusted sources and you want to ensure your serialised
5856 * output is definitely in normal form.
5858 * If @value is already in normal form, a new reference will be returned
5859 * (which will be floating if @value is floating). If it is not in normal form,
5860 * the newly created #GVariant will be returned with a single non-floating
5861 * reference. Typically, g_variant_take_ref() should be called on the return
5862 * value from this function to guarantee ownership of a single non-floating
5865 * Returns: (transfer full): a trusted #GVariant
5870 g_variant_get_normal_form (GVariant
*value
)
5874 if (g_variant_is_normal_form (value
))
5875 return g_variant_ref (value
);
5877 trusted
= g_variant_deep_copy (value
);
5878 g_assert (g_variant_is_trusted (trusted
));
5880 return g_variant_ref_sink (trusted
);
5884 * g_variant_byteswap:
5885 * @value: a #GVariant
5887 * Performs a byteswapping operation on the contents of @value. The
5888 * result is that all multi-byte numeric data contained in @value is
5889 * byteswapped. That includes 16, 32, and 64bit signed and unsigned
5890 * integers as well as file handles and double precision floating point
5893 * This function is an identity mapping on any value that does not
5894 * contain multi-byte numeric data. That include strings, booleans,
5895 * bytes and containers containing only these things (recursively).
5897 * The returned value is always in normal form and is marked as trusted.
5899 * Returns: (transfer full): the byteswapped form of @value
5904 g_variant_byteswap (GVariant
*value
)
5906 GVariantTypeInfo
*type_info
;
5910 type_info
= g_variant_get_type_info (value
);
5912 g_variant_type_info_query (type_info
, &alignment
, NULL
);
5915 /* (potentially) contains multi-byte numeric data */
5917 GVariantSerialised serialised
;
5921 trusted
= g_variant_get_normal_form (value
);
5922 serialised
.type_info
= g_variant_get_type_info (trusted
);
5923 serialised
.size
= g_variant_get_size (trusted
);
5924 serialised
.data
= g_malloc (serialised
.size
);
5925 g_variant_store (trusted
, serialised
.data
);
5926 g_variant_unref (trusted
);
5928 g_variant_serialised_byteswap (serialised
);
5930 bytes
= g_bytes_new_take (serialised
.data
, serialised
.size
);
5931 new = g_variant_new_from_bytes (g_variant_get_type (value
), bytes
, TRUE
);
5932 g_bytes_unref (bytes
);
5935 /* contains no multi-byte data */
5938 return g_variant_ref_sink (new);
5942 * g_variant_new_from_data:
5943 * @type: a definite #GVariantType
5944 * @data: (array length=size) (element-type guint8): the serialised data
5945 * @size: the size of @data
5946 * @trusted: %TRUE if @data is definitely in normal form
5947 * @notify: (scope async): function to call when @data is no longer needed
5948 * @user_data: data for @notify
5950 * Creates a new #GVariant instance from serialised data.
5952 * @type is the type of #GVariant instance that will be constructed.
5953 * The interpretation of @data depends on knowing the type.
5955 * @data is not modified by this function and must remain valid with an
5956 * unchanging value until such a time as @notify is called with
5957 * @user_data. If the contents of @data change before that time then
5958 * the result is undefined.
5960 * If @data is trusted to be serialised data in normal form then
5961 * @trusted should be %TRUE. This applies to serialised data created
5962 * within this process or read from a trusted location on the disk (such
5963 * as a file installed in /usr/lib alongside your application). You
5964 * should set trusted to %FALSE if @data is read from the network, a
5965 * file in the user's home directory, etc.
5967 * If @data was not stored in this machine's native endianness, any multi-byte
5968 * numeric values in the returned variant will also be in non-native
5969 * endianness. g_variant_byteswap() can be used to recover the original values.
5971 * @notify will be called with @user_data when @data is no longer
5972 * needed. The exact time of this call is unspecified and might even be
5973 * before this function returns.
5975 * Returns: (transfer none): a new floating #GVariant of type @type
5980 g_variant_new_from_data (const GVariantType
*type
,
5984 GDestroyNotify notify
,
5990 g_return_val_if_fail (g_variant_type_is_definite (type
), NULL
);
5991 g_return_val_if_fail (data
!= NULL
|| size
== 0, NULL
);
5994 bytes
= g_bytes_new_with_free_func (data
, size
, notify
, user_data
);
5996 bytes
= g_bytes_new_static (data
, size
);
5998 value
= g_variant_new_from_bytes (type
, bytes
, trusted
);
5999 g_bytes_unref (bytes
);
6005 /* vim:set foldmethod=marker: */