2 * Copyright © 2007, 2008 Ryan Lortie
3 * Copyright © 2009, 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>
23 #include "gvarianttype.h"
25 #include <glib/gtestutils.h>
26 #include <glib/gstrfuncs.h>
27 #include <glib/gvariant-internal.h>
31 #include "gstrfuncsprivate.h"
34 * SECTION:gvarianttype
35 * @title: GVariantType
36 * @short_description: introduction to the GVariant type system
37 * @see_also: #GVariantType, #GVariant
39 * This section introduces the GVariant type system. It is based, in
40 * large part, on the D-Bus type system, with two major changes and
41 * some minor lifting of restrictions. The
42 * [D-Bus specification](http://dbus.freedesktop.org/doc/dbus-specification.html),
43 * therefore, provides a significant amount of
44 * information that is useful when working with GVariant.
46 * The first major change with respect to the D-Bus type system is the
47 * introduction of maybe (or "nullable") types. Any type in GVariant can be
48 * converted to a maybe type, in which case, "nothing" (or "null") becomes a
49 * valid value. Maybe types have been added by introducing the
50 * character "m" to type strings.
52 * The second major change is that the GVariant type system supports the
53 * concept of "indefinite types" -- types that are less specific than
54 * the normal types found in D-Bus. For example, it is possible to speak
55 * of "an array of any type" in GVariant, where the D-Bus type system
56 * would require you to speak of "an array of integers" or "an array of
57 * strings". Indefinite types have been added by introducing the
58 * characters "*", "?" and "r" to type strings.
60 * Finally, all arbitrary restrictions relating to the complexity of
61 * types are lifted along with the restriction that dictionary entries
62 * may only appear nested inside of arrays.
64 * Just as in D-Bus, GVariant types are described with strings ("type
65 * strings"). Subject to the differences mentioned above, these strings
66 * are of the same form as those found in DBus. Note, however: D-Bus
67 * always works in terms of messages and therefore individual type
68 * strings appear nowhere in its interface. Instead, "signatures"
69 * are a concatenation of the strings of the type of each argument in a
70 * message. GVariant deals with single values directly so GVariant type
71 * strings always describe the type of exactly one value. This means
72 * that a D-Bus signature string is generally not a valid GVariant type
73 * string -- except in the case that it is the signature of a message
74 * containing exactly one argument.
76 * An indefinite type is similar in spirit to what may be called an
77 * abstract type in other type systems. No value can exist that has an
78 * indefinite type as its type, but values can exist that have types
79 * that are subtypes of indefinite types. That is to say,
80 * g_variant_get_type() will never return an indefinite type, but
81 * calling g_variant_is_of_type() with an indefinite type may return
82 * %TRUE. For example, you cannot have a value that represents "an
83 * array of no particular type", but you can have an "array of integers"
84 * which certainly matches the type of "an array of no particular type",
85 * since "array of integers" is a subtype of "array of no particular
88 * This is similar to how instances of abstract classes may not
89 * directly exist in other type systems, but instances of their
90 * non-abstract subtypes may. For example, in GTK, no object that has
91 * the type of #GtkBin can exist (since #GtkBin is an abstract class),
92 * but a #GtkWindow can certainly be instantiated, and you would say
93 * that the #GtkWindow is a #GtkBin (since #GtkWindow is a subclass of
96 * ## GVariant Type Strings
98 * A GVariant type string can be any of the following:
100 * - any basic type string (listed below)
104 * - one of the characters 'a' or 'm', followed by another type string
106 * - the character '(', followed by a concatenation of zero or more other
107 * type strings, followed by the character ')'
109 * - the character '{', followed by a basic type string (see below),
110 * followed by another type string, followed by the character '}'
112 * A basic type string describes a basic type (as per
113 * g_variant_type_is_basic()) and is always a single character in length.
114 * The valid basic type strings are "b", "y", "n", "q", "i", "u", "x", "t",
115 * "h", "d", "s", "o", "g" and "?".
117 * The above definition is recursive to arbitrary depth. "aaaaai" and
118 * "(ui(nq((y)))s)" are both valid type strings, as is
119 * "a(aa(ui)(qna{ya(yd)}))". In order to not hit memory limits, #GVariant
120 * imposes a limit on recursion depth of 65 nested containers. This is the
121 * limit in the D-Bus specification (64) plus one to allow a #GDBusMessage to
122 * be nested in a top-level tuple.
124 * The meaning of each of the characters is as follows:
125 * - `b`: the type string of %G_VARIANT_TYPE_BOOLEAN; a boolean value.
126 * - `y`: the type string of %G_VARIANT_TYPE_BYTE; a byte.
127 * - `n`: the type string of %G_VARIANT_TYPE_INT16; a signed 16 bit integer.
128 * - `q`: the type string of %G_VARIANT_TYPE_UINT16; an unsigned 16 bit integer.
129 * - `i`: the type string of %G_VARIANT_TYPE_INT32; a signed 32 bit integer.
130 * - `u`: the type string of %G_VARIANT_TYPE_UINT32; an unsigned 32 bit integer.
131 * - `x`: the type string of %G_VARIANT_TYPE_INT64; a signed 64 bit integer.
132 * - `t`: the type string of %G_VARIANT_TYPE_UINT64; an unsigned 64 bit integer.
133 * - `h`: the type string of %G_VARIANT_TYPE_HANDLE; a signed 32 bit value
134 * that, by convention, is used as an index into an array of file
135 * descriptors that are sent alongside a D-Bus message.
136 * - `d`: the type string of %G_VARIANT_TYPE_DOUBLE; a double precision
137 * floating point value.
138 * - `s`: the type string of %G_VARIANT_TYPE_STRING; a string.
139 * - `o`: the type string of %G_VARIANT_TYPE_OBJECT_PATH; a string in the form
140 * of a D-Bus object path.
141 * - `g`: the type string of %G_VARIANT_TYPE_SIGNATURE; a string in the form of
142 * a D-Bus type signature.
143 * - `?`: the type string of %G_VARIANT_TYPE_BASIC; an indefinite type that
144 * is a supertype of any of the basic types.
145 * - `v`: the type string of %G_VARIANT_TYPE_VARIANT; a container type that
146 * contain any other type of value.
147 * - `a`: used as a prefix on another type string to mean an array of that
148 * type; the type string "ai", for example, is the type of an array of
149 * signed 32-bit integers.
150 * - `m`: used as a prefix on another type string to mean a "maybe", or
151 * "nullable", version of that type; the type string "ms", for example,
152 * is the type of a value that maybe contains a string, or maybe contains
154 * - `()`: used to enclose zero or more other concatenated type strings to
155 * create a tuple type; the type string "(is)", for example, is the type of
156 * a pair of an integer and a string.
157 * - `r`: the type string of %G_VARIANT_TYPE_TUPLE; an indefinite type that is
158 * a supertype of any tuple type, regardless of the number of items.
159 * - `{}`: used to enclose a basic type string concatenated with another type
160 * string to create a dictionary entry type, which usually appears inside of
161 * an array to form a dictionary; the type string "a{sd}", for example, is
162 * the type of a dictionary that maps strings to double precision floating
165 * The first type (the basic type) is the key type and the second type is
166 * the value type. The reason that the first type is restricted to being a
167 * basic type is so that it can easily be hashed.
168 * - `*`: the type string of %G_VARIANT_TYPE_ANY; the indefinite type that is
169 * a supertype of all types. Note that, as with all type strings, this
170 * character represents exactly one type. It cannot be used inside of tuples
171 * to mean "any number of items".
173 * Any type string of a container that contains an indefinite type is,
174 * itself, an indefinite type. For example, the type string "a*"
175 * (corresponding to %G_VARIANT_TYPE_ARRAY) is an indefinite type
176 * that is a supertype of every array type. "(*s)" is a supertype
177 * of all tuples that contain exactly two items where the second
180 * "a{?*}" is an indefinite type that is a supertype of all arrays
181 * containing dictionary entries where the key is any basic type and
182 * the value is any type at all. This is, by definition, a dictionary,
183 * so this type string corresponds to %G_VARIANT_TYPE_DICTIONARY. Note
184 * that, due to the restriction that the key of a dictionary entry must
185 * be a basic type, "{**}" is not a valid type string.
190 g_variant_type_check (const GVariantType *type)
196 return g_variant_type_string_scan ((const gchar *) type, NULL, NULL);
203 variant_type_string_scan_internal (const gchar *string,
205 const gchar **endptr,
209 gsize max_depth = 0, child_depth;
211 g_return_val_if_fail (string != NULL, FALSE);
213 if (string == limit || *string == '\0')
219 while (string == limit || *string != ')')
221 if (depth_limit == 0 ||
222 !variant_type_string_scan_internal (string, limit, &string,
227 max_depth = MAX (max_depth, child_depth + 1);
234 if (depth_limit == 0 ||
235 string == limit || *string == '\0' || /* { */
236 !strchr ("bynqihuxtdsog?", *string++) || /* key */
237 !variant_type_string_scan_internal (string, limit, &string,
238 &child_depth, depth_limit - 1) || /* value */
239 string == limit || *string++ != '}') /* } */
242 max_depth = MAX (max_depth, child_depth + 1);
246 if (depth_limit == 0 ||
247 !variant_type_string_scan_internal (string, limit, &string,
248 &child_depth, depth_limit - 1))
251 max_depth = MAX (max_depth, child_depth + 1);
254 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
255 case 'x': case 't': case 'd': case 's': case 'o': case 'g':
256 case 'v': case 'r': case '*': case '?': case 'h':
257 max_depth = MAX (max_depth, 1);
273 * g_variant_type_string_scan:
274 * @string: a pointer to any string
275 * @limit: (nullable): the end of @string, or %NULL
276 * @endptr: (out) (optional): location to store the end pointer, or %NULL
278 * Scan for a single complete and valid GVariant type string in @string.
279 * The memory pointed to by @limit (or bytes beyond it) is never
282 * If a valid type string is found, @endptr is updated to point to the
283 * first character past the end of the string that was found and %TRUE
286 * If there is no valid type string starting at @string, or if the type
287 * string does not end before @limit then %FALSE is returned.
289 * For the simple case of checking if a string is a valid type string,
290 * see g_variant_type_string_is_valid().
292 * Returns: %TRUE if a valid type string was found
297 g_variant_type_string_scan (const gchar *string,
299 const gchar **endptr)
301 return variant_type_string_scan_internal (string, limit, endptr, NULL,
302 G_VARIANT_MAX_RECURSION_DEPTH);
306 * g_variant_type_string_get_depth_:
307 * @type_string: a pointer to any string
309 * Get the maximum depth of the nested types in @type_string. A basic type will
310 * return depth 1, and a container type will return a greater value. The depth
311 * of a tuple is 1 plus the depth of its deepest child type.
313 * If @type_string is not a valid #GVariant type string, 0 will be returned.
315 * Returns: depth of @type_string, or 0 on error
319 g_variant_type_string_get_depth_ (const gchar *type_string)
324 g_return_val_if_fail (type_string != NULL, 0);
326 if (!variant_type_string_scan_internal (type_string, NULL, &endptr, &depth,
327 G_VARIANT_MAX_RECURSION_DEPTH) ||
335 * g_variant_type_string_is_valid:
336 * @type_string: a pointer to any string
338 * Checks if @type_string is a valid GVariant type string. This call is
339 * equivalent to calling g_variant_type_string_scan() and confirming
340 * that the following character is a nul terminator.
342 * Returns: %TRUE if @type_string is exactly one valid type string
347 g_variant_type_string_is_valid (const gchar *type_string)
351 g_return_val_if_fail (type_string != NULL, FALSE);
353 if (!g_variant_type_string_scan (type_string, NULL, &endptr))
356 return *endptr == '\0';
360 * g_variant_type_free:
361 * @type: (nullable): a #GVariantType, or %NULL
363 * Frees a #GVariantType that was allocated with
364 * g_variant_type_copy(), g_variant_type_new() or one of the container
365 * type constructor functions.
367 * In the case that @type is %NULL, this function does nothing.
372 g_variant_type_free (GVariantType *type)
374 g_return_if_fail (type == NULL || g_variant_type_check (type));
380 * g_variant_type_copy:
381 * @type: a #GVariantType
383 * Makes a copy of a #GVariantType. It is appropriate to call
384 * g_variant_type_free() on the return value. @type may not be %NULL.
386 * Returns: (transfer full): a new #GVariantType
391 g_variant_type_copy (const GVariantType *type)
396 g_return_val_if_fail (g_variant_type_check (type), NULL);
398 length = g_variant_type_get_string_length (type);
399 new = g_malloc (length + 1);
401 memcpy (new, type, length);
404 return (GVariantType *) new;
408 * g_variant_type_new:
409 * @type_string: a valid GVariant type string
411 * Creates a new #GVariantType corresponding to the type string given
412 * by @type_string. It is appropriate to call g_variant_type_free() on
415 * It is a programmer error to call this function with an invalid type
416 * string. Use g_variant_type_string_is_valid() if you are unsure.
418 * Returns: (transfer full): a new #GVariantType
423 g_variant_type_new (const gchar *type_string)
425 g_return_val_if_fail (type_string != NULL, NULL);
427 return g_variant_type_copy (G_VARIANT_TYPE (type_string));
431 * g_variant_type_get_string_length:
432 * @type: a #GVariantType
434 * Returns the length of the type string corresponding to the given
435 * @type. This function must be used to determine the valid extent of
436 * the memory region returned by g_variant_type_peek_string().
438 * Returns: the length of the corresponding type string
443 g_variant_type_get_string_length (const GVariantType *type)
445 const gchar *type_string = (const gchar *) type;
449 g_return_val_if_fail (g_variant_type_check (type), 0);
453 while (type_string[index] == 'a' || type_string[index] == 'm')
456 if (type_string[index] == '(' || type_string[index] == '{')
459 else if (type_string[index] == ')' || type_string[index] == '}')
470 This function is not introspectable, it returns something that
471 is not an array and neither a string
474 * g_variant_type_peek_string: (skip)
475 * @type: a #GVariantType
477 * Returns the type string corresponding to the given @type. The
478 * result is not nul-terminated; in order to determine its length you
479 * must call g_variant_type_get_string_length().
481 * To get a nul-terminated string, see g_variant_type_dup_string().
483 * Returns: the corresponding type string (not nul-terminated)
488 g_variant_type_peek_string (const GVariantType *type)
490 g_return_val_if_fail (g_variant_type_check (type), NULL);
492 return (const gchar *) type;
496 * g_variant_type_dup_string:
497 * @type: a #GVariantType
499 * Returns a newly-allocated copy of the type string corresponding to
500 * @type. The returned string is nul-terminated. It is appropriate to
501 * call g_free() on the return value.
503 * Returns: (transfer full): the corresponding type string
508 g_variant_type_dup_string (const GVariantType *type)
510 g_return_val_if_fail (g_variant_type_check (type), NULL);
512 return g_strndup (g_variant_type_peek_string (type),
513 g_variant_type_get_string_length (type));
517 * g_variant_type_is_definite:
518 * @type: a #GVariantType
520 * Determines if the given @type is definite (ie: not indefinite).
522 * A type is definite if its type string does not contain any indefinite
523 * type characters ('*', '?', or 'r').
525 * A #GVariant instance may not have an indefinite type, so calling
526 * this function on the result of g_variant_get_type() will always
527 * result in %TRUE being returned. Calling this function on an
528 * indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in
529 * %FALSE being returned.
531 * Returns: %TRUE if @type is definite
536 g_variant_type_is_definite (const GVariantType *type)
538 const gchar *type_string;
542 g_return_val_if_fail (g_variant_type_check (type), FALSE);
544 type_length = g_variant_type_get_string_length (type);
545 type_string = g_variant_type_peek_string (type);
547 for (i = 0; i < type_length; i++)
548 if (type_string[i] == '*' ||
549 type_string[i] == '?' ||
550 type_string[i] == 'r')
557 * g_variant_type_is_container:
558 * @type: a #GVariantType
560 * Determines if the given @type is a container type.
562 * Container types are any array, maybe, tuple, or dictionary
563 * entry types plus the variant type.
565 * This function returns %TRUE for any indefinite type for which every
566 * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
569 * Returns: %TRUE if @type is a container type
574 g_variant_type_is_container (const GVariantType *type)
578 g_return_val_if_fail (g_variant_type_check (type), FALSE);
580 first_char = g_variant_type_peek_string (type)[0];
597 * g_variant_type_is_basic:
598 * @type: a #GVariantType
600 * Determines if the given @type is a basic type.
602 * Basic types are booleans, bytes, integers, doubles, strings, object
603 * paths and signatures.
605 * Only a basic type may be used as the key of a dictionary entry.
607 * This function returns %FALSE for all indefinite types except
608 * %G_VARIANT_TYPE_BASIC.
610 * Returns: %TRUE if @type is a basic type
615 g_variant_type_is_basic (const GVariantType *type)
619 g_return_val_if_fail (g_variant_type_check (type), FALSE);
621 first_char = g_variant_type_peek_string (type)[0];
646 * g_variant_type_is_maybe:
647 * @type: a #GVariantType
649 * Determines if the given @type is a maybe type. This is true if the
650 * type string for @type starts with an 'm'.
652 * This function returns %TRUE for any indefinite type for which every
653 * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
656 * Returns: %TRUE if @type is a maybe type
661 g_variant_type_is_maybe (const GVariantType *type)
663 g_return_val_if_fail (g_variant_type_check (type), FALSE);
665 return g_variant_type_peek_string (type)[0] == 'm';
669 * g_variant_type_is_array:
670 * @type: a #GVariantType
672 * Determines if the given @type is an array type. This is true if the
673 * type string for @type starts with an 'a'.
675 * This function returns %TRUE for any indefinite type for which every
676 * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
679 * Returns: %TRUE if @type is an array type
684 g_variant_type_is_array (const GVariantType *type)
686 g_return_val_if_fail (g_variant_type_check (type), FALSE);
688 return g_variant_type_peek_string (type)[0] == 'a';
692 * g_variant_type_is_tuple:
693 * @type: a #GVariantType
695 * Determines if the given @type is a tuple type. This is true if the
696 * type string for @type starts with a '(' or if @type is
697 * %G_VARIANT_TYPE_TUPLE.
699 * This function returns %TRUE for any indefinite type for which every
700 * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
703 * Returns: %TRUE if @type is a tuple type
708 g_variant_type_is_tuple (const GVariantType *type)
712 g_return_val_if_fail (g_variant_type_check (type), FALSE);
714 type_char = g_variant_type_peek_string (type)[0];
715 return type_char == 'r' || type_char == '(';
719 * g_variant_type_is_dict_entry:
720 * @type: a #GVariantType
722 * Determines if the given @type is a dictionary entry type. This is
723 * true if the type string for @type starts with a '{'.
725 * This function returns %TRUE for any indefinite type for which every
726 * definite subtype is a dictionary entry type --
727 * %G_VARIANT_TYPE_DICT_ENTRY, for example.
729 * Returns: %TRUE if @type is a dictionary entry type
734 g_variant_type_is_dict_entry (const GVariantType *type)
736 g_return_val_if_fail (g_variant_type_check (type), FALSE);
738 return g_variant_type_peek_string (type)[0] == '{';
742 * g_variant_type_is_variant:
743 * @type: a #GVariantType
745 * Determines if the given @type is the variant type.
747 * Returns: %TRUE if @type is the variant type
752 g_variant_type_is_variant (const GVariantType *type)
754 g_return_val_if_fail (g_variant_type_check (type), FALSE);
756 return g_variant_type_peek_string (type)[0] == 'v';
760 * g_variant_type_hash:
761 * @type: (type GVariantType): a #GVariantType
765 * The argument type of @type is only #gconstpointer to allow use with
766 * #GHashTable without function pointer casting. A valid
767 * #GVariantType must be provided.
769 * Returns: the hash value
774 g_variant_type_hash (gconstpointer type)
776 const gchar *type_string;
781 g_return_val_if_fail (g_variant_type_check (type), 0);
783 type_string = g_variant_type_peek_string (type);
784 length = g_variant_type_get_string_length (type);
786 for (i = 0; i < length; i++)
787 value = (value << 5) - value + type_string[i];
793 * g_variant_type_equal:
794 * @type1: (type GVariantType): a #GVariantType
795 * @type2: (type GVariantType): a #GVariantType
797 * Compares @type1 and @type2 for equality.
799 * Only returns %TRUE if the types are exactly equal. Even if one type
800 * is an indefinite type and the other is a subtype of it, %FALSE will
801 * be returned if they are not exactly equal. If you want to check for
802 * subtypes, use g_variant_type_is_subtype_of().
804 * The argument types of @type1 and @type2 are only #gconstpointer to
805 * allow use with #GHashTable without function pointer casting. For
806 * both arguments, a valid #GVariantType must be provided.
808 * Returns: %TRUE if @type1 and @type2 are exactly equal
813 g_variant_type_equal (gconstpointer type1,
816 const gchar *string1, *string2;
819 g_return_val_if_fail (g_variant_type_check (type1), FALSE);
820 g_return_val_if_fail (g_variant_type_check (type2), FALSE);
825 size1 = g_variant_type_get_string_length (type1);
826 size2 = g_variant_type_get_string_length (type2);
831 string1 = g_variant_type_peek_string (type1);
832 string2 = g_variant_type_peek_string (type2);
834 return memcmp (string1, string2, size1) == 0;
838 * g_variant_type_is_subtype_of:
839 * @type: a #GVariantType
840 * @supertype: a #GVariantType
842 * Checks if @type is a subtype of @supertype.
844 * This function returns %TRUE if @type is a subtype of @supertype. All
845 * types are considered to be subtypes of themselves. Aside from that,
846 * only indefinite types can have subtypes.
848 * Returns: %TRUE if @type is a subtype of @supertype
853 g_variant_type_is_subtype_of (const GVariantType *type,
854 const GVariantType *supertype)
856 const gchar *supertype_string;
857 const gchar *supertype_end;
858 const gchar *type_string;
860 g_return_val_if_fail (g_variant_type_check (type), FALSE);
861 g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
863 supertype_string = g_variant_type_peek_string (supertype);
864 type_string = g_variant_type_peek_string (type);
866 supertype_end = supertype_string +
867 g_variant_type_get_string_length (supertype);
869 /* we know that type and supertype are both well-formed, so it's
870 * safe to treat this merely as a text processing problem.
872 while (supertype_string < supertype_end)
874 char supertype_char = *supertype_string++;
876 if (supertype_char == *type_string)
879 else if (*type_string == ')')
884 const GVariantType *target_type = (GVariantType *) type_string;
886 switch (supertype_char)
889 if (!g_variant_type_is_tuple (target_type))
897 if (!g_variant_type_is_basic (target_type))
905 type_string += g_variant_type_get_string_length (target_type);
913 * g_variant_type_element:
914 * @type: an array or maybe #GVariantType
916 * Determines the element type of an array or maybe type.
918 * This function may only be used with array or maybe types.
920 * Returns: (transfer none): the element type of @type
925 g_variant_type_element (const GVariantType *type)
927 const gchar *type_string;
929 g_return_val_if_fail (g_variant_type_check (type), NULL);
931 type_string = g_variant_type_peek_string (type);
933 g_assert (type_string[0] == 'a' || type_string[0] == 'm');
935 return (const GVariantType *) &type_string[1];
939 * g_variant_type_first:
940 * @type: a tuple or dictionary entry #GVariantType
942 * Determines the first item type of a tuple or dictionary entry
945 * This function may only be used with tuple or dictionary entry types,
946 * but must not be used with the generic tuple type
947 * %G_VARIANT_TYPE_TUPLE.
949 * In the case of a dictionary entry type, this returns the type of
952 * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
954 * This call, together with g_variant_type_next() provides an iterator
955 * interface over tuple and dictionary entry types.
957 * Returns: (transfer none): the first item type of @type, or %NULL
962 g_variant_type_first (const GVariantType *type)
964 const gchar *type_string;
966 g_return_val_if_fail (g_variant_type_check (type), NULL);
968 type_string = g_variant_type_peek_string (type);
969 g_assert (type_string[0] == '(' || type_string[0] == '{');
971 if (type_string[1] == ')')
974 return (const GVariantType *) &type_string[1];
978 * g_variant_type_next:
979 * @type: a #GVariantType from a previous call
981 * Determines the next item type of a tuple or dictionary entry
984 * @type must be the result of a previous call to
985 * g_variant_type_first() or g_variant_type_next().
987 * If called on the key type of a dictionary entry then this call
988 * returns the value type. If called on the value type of a dictionary
989 * entry then this call returns %NULL.
991 * For tuples, %NULL is returned when @type is the last item in a tuple.
993 * Returns: (transfer none): the next #GVariantType after @type, or %NULL
998 g_variant_type_next (const GVariantType *type)
1000 const gchar *type_string;
1002 g_return_val_if_fail (g_variant_type_check (type), NULL);
1004 type_string = g_variant_type_peek_string (type);
1005 type_string += g_variant_type_get_string_length (type);
1007 if (*type_string == ')' || *type_string == '}')
1010 return (const GVariantType *) type_string;
1014 * g_variant_type_n_items:
1015 * @type: a tuple or dictionary entry #GVariantType
1017 * Determines the number of items contained in a tuple or
1018 * dictionary entry type.
1020 * This function may only be used with tuple or dictionary entry types,
1021 * but must not be used with the generic tuple type
1022 * %G_VARIANT_TYPE_TUPLE.
1024 * In the case of a dictionary entry type, this function will always
1027 * Returns: the number of items in @type
1032 g_variant_type_n_items (const GVariantType *type)
1036 g_return_val_if_fail (g_variant_type_check (type), 0);
1038 for (type = g_variant_type_first (type);
1040 type = g_variant_type_next (type))
1047 * g_variant_type_key:
1048 * @type: a dictionary entry #GVariantType
1050 * Determines the key type of a dictionary entry type.
1052 * This function may only be used with a dictionary entry type. Other
1053 * than the additional restriction, this call is equivalent to
1054 * g_variant_type_first().
1056 * Returns: (transfer none): the key type of the dictionary entry
1060 const GVariantType *
1061 g_variant_type_key (const GVariantType *type)
1063 const gchar *type_string;
1065 g_return_val_if_fail (g_variant_type_check (type), NULL);
1067 type_string = g_variant_type_peek_string (type);
1068 g_assert (type_string[0] == '{');
1070 return (const GVariantType *) &type_string[1];
1074 * g_variant_type_value:
1075 * @type: a dictionary entry #GVariantType
1077 * Determines the value type of a dictionary entry type.
1079 * This function may only be used with a dictionary entry type.
1081 * Returns: (transfer none): the value type of the dictionary entry
1085 const GVariantType *
1086 g_variant_type_value (const GVariantType *type)
1088 #ifndef G_DISABLE_ASSERT
1089 const gchar *type_string;
1092 g_return_val_if_fail (g_variant_type_check (type), NULL);
1094 #ifndef G_DISABLE_ASSERT
1095 type_string = g_variant_type_peek_string (type);
1096 g_assert (type_string[0] == '{');
1099 return g_variant_type_next (g_variant_type_key (type));
1103 * g_variant_type_new_tuple:
1104 * @items: (array length=length): an array of #GVariantTypes, one for each item
1105 * @length: the length of @items, or -1
1107 * Constructs a new tuple type, from @items.
1109 * @length is the number of items in @items, or -1 to indicate that
1110 * @items is %NULL-terminated.
1112 * It is appropriate to call g_variant_type_free() on the return value.
1114 * Returns: (transfer full): a new tuple #GVariantType
1118 static GVariantType *
1119 g_variant_type_new_tuple_slow (const GVariantType * const *items,
1122 /* the "slow" version is needed in case the static buffer of 1024
1123 * bytes is exceeded when running the normal version. this will
1124 * happen only with very unusually large types, so it can be slow.
1129 string = g_string_new ("(");
1130 for (i = 0; i < length; i++)
1132 const GVariantType *type;
1135 g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1138 size = g_variant_type_get_string_length (type);
1139 g_string_append_len (string, (const gchar *) type, size);
1141 g_string_append_c (string, ')');
1143 return (GVariantType *) g_string_free (string, FALSE);
1147 g_variant_type_new_tuple (const GVariantType * const *items,
1153 gsize length_unsigned;
1155 g_return_val_if_fail (length == 0 || items != NULL, NULL);
1158 for (length_unsigned = 0; items[length_unsigned] != NULL; length_unsigned++);
1160 length_unsigned = (gsize) length;
1163 buffer[offset++] = '(';
1165 for (i = 0; i < length_unsigned; i++)
1167 const GVariantType *type;
1170 g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1173 size = g_variant_type_get_string_length (type);
1175 if (offset + size >= sizeof buffer) /* leave room for ')' */
1176 return g_variant_type_new_tuple_slow (items, length_unsigned);
1178 memcpy (&buffer[offset], type, size);
1182 g_assert (offset < sizeof buffer);
1183 buffer[offset++] = ')';
1185 return (GVariantType *) g_memdup2 (buffer, offset);
1189 * g_variant_type_new_array: (constructor)
1190 * @element: a #GVariantType
1192 * Constructs the type corresponding to an array of elements of the
1195 * It is appropriate to call g_variant_type_free() on the return value.
1197 * Returns: (transfer full): a new array #GVariantType
1202 g_variant_type_new_array (const GVariantType *element)
1207 g_return_val_if_fail (g_variant_type_check (element), NULL);
1209 size = g_variant_type_get_string_length (element);
1210 new = g_malloc (size + 1);
1213 memcpy (new + 1, element, size);
1215 return (GVariantType *) new;
1219 * g_variant_type_new_maybe: (constructor)
1220 * @element: a #GVariantType
1222 * Constructs the type corresponding to a maybe instance containing
1223 * type @type or Nothing.
1225 * It is appropriate to call g_variant_type_free() on the return value.
1227 * Returns: (transfer full): a new maybe #GVariantType
1232 g_variant_type_new_maybe (const GVariantType *element)
1237 g_return_val_if_fail (g_variant_type_check (element), NULL);
1239 size = g_variant_type_get_string_length (element);
1240 new = g_malloc (size + 1);
1243 memcpy (new + 1, element, size);
1245 return (GVariantType *) new;
1249 * g_variant_type_new_dict_entry: (constructor)
1250 * @key: a basic #GVariantType
1251 * @value: a #GVariantType
1253 * Constructs the type corresponding to a dictionary entry with a key
1254 * of type @key and a value of type @value.
1256 * It is appropriate to call g_variant_type_free() on the return value.
1258 * Returns: (transfer full): a new dictionary entry #GVariantType
1263 g_variant_type_new_dict_entry (const GVariantType *key,
1264 const GVariantType *value)
1266 gsize keysize, valsize;
1269 g_return_val_if_fail (g_variant_type_check (key), NULL);
1270 g_return_val_if_fail (g_variant_type_check (value), NULL);
1272 keysize = g_variant_type_get_string_length (key);
1273 valsize = g_variant_type_get_string_length (value);
1275 new = g_malloc (1 + keysize + valsize + 1);
1278 memcpy (new + 1, key, keysize);
1279 memcpy (new + 1 + keysize, value, valsize);
1280 new[1 + keysize + valsize] = '}';
1282 return (GVariantType *) new;
1286 const GVariantType *
1287 g_variant_type_checked_ (const gchar *type_string)
1289 g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1290 return (const GVariantType *) type_string;