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 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Ryan Lortie <desrt@desrt.ca>
23 #include "gvarianttype.h"
25 #include <glib/gtestutils.h>
26 #include <glib/gstrfuncs.h>
32 * SECTION:gvarianttype
33 * @title: GVariantType
34 * @short_description: introduction to the GVariant type system
35 * @see_also: #GVariantType, #GVariant
37 * This section introduces the GVariant type system. It is based, in
38 * large part, on the D-Bus type system, with two major changes and some minor
39 * lifting of restrictions. The <ulink
40 * url='http://dbus.freedesktop.org/doc/dbus-specification.html'>DBus
41 * specification</ulink>, therefore, provides a significant amount of
42 * information that is useful when working with GVariant.
44 * The first major change with respect to the D-Bus type system is the
45 * introduction of maybe (or "nullable") types. Any type in GVariant can be
46 * converted to a maybe type, in which case, "nothing" (or "null") becomes a
47 * valid value. Maybe types have been added by introducing the
48 * character "<literal>m</literal>" to type strings.
50 * The second major change is that the GVariant type system supports the
51 * concept of "indefinite types" -- types that are less specific than
52 * the normal types found in D-Bus. For example, it is possible to speak
53 * of "an array of any type" in GVariant, where the D-Bus type system
54 * would require you to speak of "an array of integers" or "an array of
55 * strings". Indefinite types have been added by introducing the
56 * characters "<literal>*</literal>", "<literal>?</literal>" and
57 * "<literal>r</literal>" to type strings.
59 * Finally, all arbitrary restrictions relating to the complexity of
60 * types are lifted along with the restriction that dictionary entries
61 * may only appear nested inside of arrays.
63 * Just as in D-Bus, GVariant types are described with strings ("type
64 * strings"). Subject to the differences mentioned above, these strings
65 * are of the same form as those found in DBus. Note, however: D-Bus
66 * always works in terms of messages and therefore individual type
67 * strings appear nowhere in its interface. Instead, "signatures"
68 * are a concatenation of the strings of the type of each argument in a
69 * message. GVariant deals with single values directly so GVariant type
70 * strings always describe the type of exactly one value. This means
71 * that a D-Bus signature string is generally not a valid GVariant type
72 * string -- except in the case that it is the signature of a message
73 * containing exactly one argument.
75 * An indefinite type is similar in spirit to what may be called an
76 * abstract type in other type systems. No value can exist that has an
77 * indefinite type as its type, but values can exist that have types
78 * that are subtypes of indefinite types. That is to say,
79 * g_variant_get_type() will never return an indefinite type, but
80 * calling g_variant_is_of_type() with an indefinite type may return
81 * %TRUE. For example, you cannot have a value that represents "an
82 * array of no particular type", but you can have an "array of integers"
83 * which certainly matches the type of "an array of no particular type",
84 * since "array of integers" is a subtype of "array of no particular
87 * This is similar to how instances of abstract classes may not
88 * directly exist in other type systems, but instances of their
89 * non-abstract subtypes may. For example, in GTK, no object that has
90 * the type of #GtkBin can exist (since #GtkBin is an abstract class),
91 * but a #GtkWindow can certainly be instantiated, and you would say
92 * that the #GtkWindow is a #GtkBin (since #GtkWindow is a subclass of
95 * A detailed description of GVariant type strings is given here:
97 * <refsect2 id='gvariant-typestrings'>
98 * <title>GVariant Type Strings</title>
100 * A GVariant type string can be any of the following:
105 * any basic type string (listed below)
110 * "<literal>v</literal>", "<literal>r</literal>" or
111 * "<literal>*</literal>"
116 * one of the characters '<literal>a</literal>' or
117 * '<literal>m</literal>', followed by another type string
122 * the character '<literal>(</literal>', followed by a concatenation
123 * of zero or more other type strings, followed by the character
124 * '<literal>)</literal>'
129 * the character '<literal>{</literal>', followed by a basic type
130 * string (see below), followed by another type string, followed by
131 * the character '<literal>}</literal>'
136 * A basic type string describes a basic type (as per
137 * g_variant_type_is_basic()) and is always a single
138 * character in length. The valid basic type strings are
139 * "<literal>b</literal>", "<literal>y</literal>",
140 * "<literal>n</literal>", "<literal>q</literal>",
141 * "<literal>i</literal>", "<literal>u</literal>",
142 * "<literal>x</literal>", "<literal>t</literal>",
143 * "<literal>h</literal>", "<literal>d</literal>",
144 * "<literal>s</literal>", "<literal>o</literal>",
145 * "<literal>g</literal>" and "<literal>?</literal>".
148 * The above definition is recursive to arbitrary depth.
149 * "<literal>aaaaai</literal>" and "<literal>(ui(nq((y)))s)</literal>"
150 * are both valid type strings, as is
151 * "<literal>a(aa(ui)(qna{ya(yd)}))</literal>".
154 * The meaning of each of the characters is as follows:
162 * <emphasis role='strong'>Character</emphasis>
167 * <emphasis role='strong'>Meaning</emphasis>
174 * <literal>b</literal>
179 * the type string of %G_VARIANT_TYPE_BOOLEAN; a boolean value.
186 * <literal>y</literal>
191 * the type string of %G_VARIANT_TYPE_BYTE; a byte.
198 * <literal>n</literal>
203 * the type string of %G_VARIANT_TYPE_INT16; a signed 16 bit
211 * <literal>q</literal>
216 * the type string of %G_VARIANT_TYPE_UINT16; an unsigned 16 bit
224 * <literal>i</literal>
229 * the type string of %G_VARIANT_TYPE_INT32; a signed 32 bit
237 * <literal>u</literal>
242 * the type string of %G_VARIANT_TYPE_UINT32; an unsigned 32 bit
250 * <literal>x</literal>
255 * the type string of %G_VARIANT_TYPE_INT64; a signed 64 bit
263 * <literal>t</literal>
268 * the type string of %G_VARIANT_TYPE_UINT64; an unsigned 64 bit
276 * <literal>h</literal>
281 * the type string of %G_VARIANT_TYPE_HANDLE; a signed 32 bit
282 * value that, by convention, is used as an index into an array
283 * of file descriptors that are sent alongside a D-Bus message.
290 * <literal>d</literal>
295 * the type string of %G_VARIANT_TYPE_DOUBLE; a double precision
296 * floating point value.
303 * <literal>s</literal>
308 * the type string of %G_VARIANT_TYPE_STRING; a string.
315 * <literal>o</literal>
320 * the type string of %G_VARIANT_TYPE_OBJECT_PATH; a string in
321 * the form of a D-Bus object path.
328 * <literal>g</literal>
333 * the type string of %G_VARIANT_TYPE_STRING; a string in the
334 * form of a D-Bus type signature.
341 * <literal>?</literal>
346 * the type string of %G_VARIANT_TYPE_BASIC; an indefinite type
347 * that is a supertype of any of the basic types.
354 * <literal>v</literal>
359 * the type string of %G_VARIANT_TYPE_VARIANT; a container type
360 * that contain any other type of value.
367 * <literal>a</literal>
372 * used as a prefix on another type string to mean an array of
373 * that type; the type string "<literal>ai</literal>", for
374 * example, is the type of an array of 32 bit signed integers.
381 * <literal>m</literal>
386 * used as a prefix on another type string to mean a "maybe", or
387 * "nullable", version of that type; the type string
388 * "<literal>ms</literal>", for example, is the type of a value
389 * that maybe contains a string, or maybe contains nothing.
396 * <literal>()</literal>
401 * used to enclose zero or more other concatenated type strings
402 * to create a tuple type; the type string
403 * "<literal>(is)</literal>", for example, is the type of a pair
404 * of an integer and a string.
411 * <literal>r</literal>
416 * the type string of %G_VARIANT_TYPE_TUPLE; an indefinite type
417 * that is a supertype of any tuple type, regardless of the
425 * <literal>{}</literal>
430 * used to enclose a basic type string concatenated with another
431 * type string to create a dictionary entry type, which usually
432 * appears inside of an array to form a dictionary; the type
433 * string "<literal>a{sd}</literal>", for example, is the type of
434 * a dictionary that maps strings to double precision floating
438 * The first type (the basic type) is the key type and the second
439 * type is the value type. The reason that the first type is
440 * restricted to being a basic type is so that it can easily be
448 * <literal>*</literal>
453 * the type string of %G_VARIANT_TYPE_ANY; the indefinite type
454 * that is a supertype of all types. Note that, as with all type
455 * strings, this character represents exactly one type. It
456 * cannot be used inside of tuples to mean "any number of items".
464 * Any type string of a container that contains an indefinite type is,
465 * itself, an indefinite type. For example, the type string
466 * "<literal>a*</literal>" (corresponding to %G_VARIANT_TYPE_ARRAY) is
467 * an indefinite type that is a supertype of every array type.
468 * "<literal>(*s)</literal>" is a supertype of all tuples that
469 * contain exactly two items where the second item is a string.
472 * "<literal>a{?*}</literal>" is an indefinite type that is a
473 * supertype of all arrays containing dictionary entries where the key
474 * is any basic type and the value is any type at all. This is, by
475 * definition, a dictionary, so this type string corresponds to
476 * %G_VARIANT_TYPE_DICTIONARY. Note that, due to the restriction that
477 * the key of a dictionary entry must be a basic type,
478 * "<literal>{**}</literal>" is not a valid type string.
485 g_variant_type_check (const GVariantType *type)
491 return g_variant_type_string_scan ((const gchar *) type, NULL, NULL);
498 * g_variant_type_string_scan:
499 * @string: a pointer to any string
500 * @limit: (allow-none): the end of @string, or %NULL
501 * @endptr: (out) (allow-none): location to store the end pointer, or %NULL
503 * Scan for a single complete and valid GVariant type string in @string.
504 * The memory pointed to by @limit (or bytes beyond it) is never
507 * If a valid type string is found, @endptr is updated to point to the
508 * first character past the end of the string that was found and %TRUE
511 * If there is no valid type string starting at @string, or if the type
512 * string does not end before @limit then %FALSE is returned.
514 * For the simple case of checking if a string is a valid type string,
515 * see g_variant_type_string_is_valid().
517 * Returns: %TRUE if a valid type string was found
522 g_variant_type_string_scan (const gchar *string,
524 const gchar **endptr)
526 g_return_val_if_fail (string != NULL, FALSE);
528 if (string == limit || *string == '\0')
534 while (string == limit || *string != ')')
535 if (!g_variant_type_string_scan (string, limit, &string))
542 if (string == limit || *string == '\0' || /* { */
543 !strchr ("bynqihuxtdsog?", *string++) || /* key */
544 !g_variant_type_string_scan (string, limit, &string) || /* value */
545 string == limit || *string++ != '}') /* } */
551 return g_variant_type_string_scan (string, limit, endptr);
553 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
554 case 'x': case 't': case 'd': case 's': case 'o': case 'g':
555 case 'v': case 'r': case '*': case '?': case 'h':
569 * g_variant_type_string_is_valid:
570 * @type_string: a pointer to any string
572 * Checks if @type_string is a valid GVariant type string. This call is
573 * equivalent to calling g_variant_type_string_scan() and confirming
574 * that the following character is a nul terminator.
576 * Returns: %TRUE if @type_string is exactly one valid type string
581 g_variant_type_string_is_valid (const gchar *type_string)
585 g_return_val_if_fail (type_string != NULL, FALSE);
587 if (!g_variant_type_string_scan (type_string, NULL, &endptr))
590 return *endptr == '\0';
594 * g_variant_type_free:
595 * @type: (allow-none): a #GVariantType, or %NULL
597 * Frees a #GVariantType that was allocated with
598 * g_variant_type_copy(), g_variant_type_new() or one of the container
599 * type constructor functions.
601 * In the case that @type is %NULL, this function does nothing.
606 g_variant_type_free (GVariantType *type)
608 g_return_if_fail (type == NULL || g_variant_type_check (type));
614 * g_variant_type_copy:
615 * @type: a #GVariantType
617 * Makes a copy of a #GVariantType. It is appropriate to call
618 * g_variant_type_free() on the return value. @type may not be %NULL.
620 * Returns: (transfer full): a new #GVariantType
625 g_variant_type_copy (const GVariantType *type)
630 g_return_val_if_fail (g_variant_type_check (type), NULL);
632 length = g_variant_type_get_string_length (type);
633 new = g_malloc (length + 1);
635 memcpy (new, type, length);
638 return (GVariantType *) new;
642 * g_variant_type_new:
643 * @type_string: a valid GVariant type string
645 * Creates a new #GVariantType corresponding to the type string given
646 * by @type_string. It is appropriate to call g_variant_type_free() on
649 * It is a programmer error to call this function with an invalid type
650 * string. Use g_variant_type_string_is_valid() if you are unsure.
652 * Returns: (transfer full): a new #GVariantType
657 g_variant_type_new (const gchar *type_string)
659 g_return_val_if_fail (type_string != NULL, NULL);
661 return g_variant_type_copy (G_VARIANT_TYPE (type_string));
665 * g_variant_type_get_string_length:
666 * @type: a #GVariantType
668 * Returns the length of the type string corresponding to the given
669 * @type. This function must be used to determine the valid extent of
670 * the memory region returned by g_variant_type_peek_string().
672 * Returns: the length of the corresponding type string
677 g_variant_type_get_string_length (const GVariantType *type)
679 const gchar *type_string = (const gchar *) type;
683 g_return_val_if_fail (g_variant_type_check (type), 0);
687 while (type_string[index] == 'a' || type_string[index] == 'm')
690 if (type_string[index] == '(' || type_string[index] == '{')
693 else if (type_string[index] == ')' || type_string[index] == '}')
704 This function is not introspectable, it returns something that
705 is not an array and neither a string
708 * g_variant_type_peek_string: (skip)
709 * @type: a #GVariantType
711 * Returns the type string corresponding to the given @type. The
712 * result is not nul-terminated; in order to determine its length you
713 * must call g_variant_type_get_string_length().
715 * To get a nul-terminated string, see g_variant_type_dup_string().
717 * Returns: the corresponding type string (not nul-terminated)
722 g_variant_type_peek_string (const GVariantType *type)
724 g_return_val_if_fail (g_variant_type_check (type), NULL);
726 return (const gchar *) type;
730 * g_variant_type_dup_string:
731 * @type: a #GVariantType
733 * Returns a newly-allocated copy of the type string corresponding to
734 * @type. The returned string is nul-terminated. It is appropriate to
735 * call g_free() on the return value.
737 * Returns: (transfer full): the corresponding type string
742 g_variant_type_dup_string (const GVariantType *type)
744 g_return_val_if_fail (g_variant_type_check (type), NULL);
746 return g_strndup (g_variant_type_peek_string (type),
747 g_variant_type_get_string_length (type));
751 * g_variant_type_is_definite:
752 * @type: a #GVariantType
754 * Determines if the given @type is definite (ie: not indefinite).
756 * A type is definite if its type string does not contain any indefinite
757 * type characters ('*', '?', or 'r').
759 * A #GVariant instance may not have an indefinite type, so calling
760 * this function on the result of g_variant_get_type() will always
761 * result in %TRUE being returned. Calling this function on an
762 * indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in
763 * %FALSE being returned.
765 * Returns: %TRUE if @type is definite
770 g_variant_type_is_definite (const GVariantType *type)
772 const gchar *type_string;
776 g_return_val_if_fail (g_variant_type_check (type), FALSE);
778 type_length = g_variant_type_get_string_length (type);
779 type_string = g_variant_type_peek_string (type);
781 for (i = 0; i < type_length; i++)
782 if (type_string[i] == '*' ||
783 type_string[i] == '?' ||
784 type_string[i] == 'r')
791 * g_variant_type_is_container:
792 * @type: a #GVariantType
794 * Determines if the given @type is a container type.
796 * Container types are any array, maybe, tuple, or dictionary
797 * entry types plus the variant type.
799 * This function returns %TRUE for any indefinite type for which every
800 * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
803 * Returns: %TRUE if @type is a container type
808 g_variant_type_is_container (const GVariantType *type)
812 g_return_val_if_fail (g_variant_type_check (type), FALSE);
814 first_char = g_variant_type_peek_string (type)[0];
831 * g_variant_type_is_basic:
832 * @type: a #GVariantType
834 * Determines if the given @type is a basic type.
836 * Basic types are booleans, bytes, integers, doubles, strings, object
837 * paths and signatures.
839 * Only a basic type may be used as the key of a dictionary entry.
841 * This function returns %FALSE for all indefinite types except
842 * %G_VARIANT_TYPE_BASIC.
844 * Returns: %TRUE if @type is a basic type
849 g_variant_type_is_basic (const GVariantType *type)
853 g_return_val_if_fail (g_variant_type_check (type), FALSE);
855 first_char = g_variant_type_peek_string (type)[0];
880 * g_variant_type_is_maybe:
881 * @type: a #GVariantType
883 * Determines if the given @type is a maybe type. This is true if the
884 * type string for @type starts with an 'm'.
886 * This function returns %TRUE for any indefinite type for which every
887 * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
890 * Returns: %TRUE if @type is a maybe type
895 g_variant_type_is_maybe (const GVariantType *type)
897 g_return_val_if_fail (g_variant_type_check (type), FALSE);
899 return g_variant_type_peek_string (type)[0] == 'm';
903 * g_variant_type_is_array:
904 * @type: a #GVariantType
906 * Determines if the given @type is an array type. This is true if the
907 * type string for @type starts with an 'a'.
909 * This function returns %TRUE for any indefinite type for which every
910 * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
913 * Returns: %TRUE if @type is an array type
918 g_variant_type_is_array (const GVariantType *type)
920 g_return_val_if_fail (g_variant_type_check (type), FALSE);
922 return g_variant_type_peek_string (type)[0] == 'a';
926 * g_variant_type_is_tuple:
927 * @type: a #GVariantType
929 * Determines if the given @type is a tuple type. This is true if the
930 * type string for @type starts with a '(' or if @type is
931 * %G_VARIANT_TYPE_TUPLE.
933 * This function returns %TRUE for any indefinite type for which every
934 * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
937 * Returns: %TRUE if @type is a tuple type
942 g_variant_type_is_tuple (const GVariantType *type)
946 g_return_val_if_fail (g_variant_type_check (type), FALSE);
948 type_char = g_variant_type_peek_string (type)[0];
949 return type_char == 'r' || type_char == '(';
953 * g_variant_type_is_dict_entry:
954 * @type: a #GVariantType
956 * Determines if the given @type is a dictionary entry type. This is
957 * true if the type string for @type starts with a '{'.
959 * This function returns %TRUE for any indefinite type for which every
960 * definite subtype is a dictionary entry type --
961 * %G_VARIANT_TYPE_DICT_ENTRY, for example.
963 * Returns: %TRUE if @type is a dictionary entry type
968 g_variant_type_is_dict_entry (const GVariantType *type)
970 g_return_val_if_fail (g_variant_type_check (type), FALSE);
972 return g_variant_type_peek_string (type)[0] == '{';
976 * g_variant_type_is_variant:
977 * @type: a #GVariantType
979 * Determines if the given @type is the variant type.
981 * Returns: %TRUE if @type is the variant type
986 g_variant_type_is_variant (const GVariantType *type)
988 g_return_val_if_fail (g_variant_type_check (type), FALSE);
990 return g_variant_type_peek_string (type)[0] == 'v';
994 * g_variant_type_hash:
995 * @type: (type GVariantType): a #GVariantType
999 * The argument type of @type is only #gconstpointer to allow use with
1000 * #GHashTable without function pointer casting. A valid
1001 * #GVariantType must be provided.
1003 * Returns: the hash value
1008 g_variant_type_hash (gconstpointer type)
1010 const gchar *type_string;
1015 g_return_val_if_fail (g_variant_type_check (type), 0);
1017 type_string = g_variant_type_peek_string (type);
1018 length = g_variant_type_get_string_length (type);
1020 for (i = 0; i < length; i++)
1021 value = (value << 5) - value + type_string[i];
1027 * g_variant_type_equal:
1028 * @type1: (type GVariantType): a #GVariantType
1029 * @type2: (type GVariantType): a #GVariantType
1031 * Compares @type1 and @type2 for equality.
1033 * Only returns %TRUE if the types are exactly equal. Even if one type
1034 * is an indefinite type and the other is a subtype of it, %FALSE will
1035 * be returned if they are not exactly equal. If you want to check for
1036 * subtypes, use g_variant_type_is_subtype_of().
1038 * The argument types of @type1 and @type2 are only #gconstpointer to
1039 * allow use with #GHashTable without function pointer casting. For
1040 * both arguments, a valid #GVariantType must be provided.
1042 * Returns: %TRUE if @type1 and @type2 are exactly equal
1047 g_variant_type_equal (gconstpointer type1,
1048 gconstpointer type2)
1050 const gchar *string1, *string2;
1053 g_return_val_if_fail (g_variant_type_check (type1), FALSE);
1054 g_return_val_if_fail (g_variant_type_check (type2), FALSE);
1059 size1 = g_variant_type_get_string_length (type1);
1060 size2 = g_variant_type_get_string_length (type2);
1065 string1 = g_variant_type_peek_string (type1);
1066 string2 = g_variant_type_peek_string (type2);
1068 return memcmp (string1, string2, size1) == 0;
1072 * g_variant_type_is_subtype_of:
1073 * @type: a #GVariantType
1074 * @supertype: a #GVariantType
1076 * Checks if @type is a subtype of @supertype.
1078 * This function returns %TRUE if @type is a subtype of @supertype. All
1079 * types are considered to be subtypes of themselves. Aside from that,
1080 * only indefinite types can have subtypes.
1082 * Returns: %TRUE if @type is a subtype of @supertype
1087 g_variant_type_is_subtype_of (const GVariantType *type,
1088 const GVariantType *supertype)
1090 const gchar *supertype_string;
1091 const gchar *supertype_end;
1092 const gchar *type_string;
1094 g_return_val_if_fail (g_variant_type_check (type), FALSE);
1095 g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
1097 supertype_string = g_variant_type_peek_string (supertype);
1098 type_string = g_variant_type_peek_string (type);
1100 supertype_end = supertype_string +
1101 g_variant_type_get_string_length (supertype);
1103 /* we know that type and supertype are both well-formed, so it's
1104 * safe to treat this merely as a text processing problem.
1106 while (supertype_string < supertype_end)
1108 char supertype_char = *supertype_string++;
1110 if (supertype_char == *type_string)
1113 else if (*type_string == ')')
1118 const GVariantType *target_type = (GVariantType *) type_string;
1120 switch (supertype_char)
1123 if (!g_variant_type_is_tuple (target_type))
1131 if (!g_variant_type_is_basic (target_type))
1139 type_string += g_variant_type_get_string_length (target_type);
1147 * g_variant_type_element:
1148 * @type: an array or maybe #GVariantType
1150 * Determines the element type of an array or maybe type.
1152 * This function may only be used with array or maybe types.
1154 * Returns: (transfer none): the element type of @type
1158 const GVariantType *
1159 g_variant_type_element (const GVariantType *type)
1161 const gchar *type_string;
1163 g_return_val_if_fail (g_variant_type_check (type), NULL);
1165 type_string = g_variant_type_peek_string (type);
1167 g_assert (type_string[0] == 'a' || type_string[0] == 'm');
1169 return (const GVariantType *) &type_string[1];
1173 * g_variant_type_first:
1174 * @type: a tuple or dictionary entry #GVariantType
1176 * Determines the first item type of a tuple or dictionary entry
1179 * This function may only be used with tuple or dictionary entry types,
1180 * but must not be used with the generic tuple type
1181 * %G_VARIANT_TYPE_TUPLE.
1183 * In the case of a dictionary entry type, this returns the type of
1186 * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
1188 * This call, together with g_variant_type_next() provides an iterator
1189 * interface over tuple and dictionary entry types.
1191 * Returns: (transfer none): the first item type of @type, or %NULL
1195 const GVariantType *
1196 g_variant_type_first (const GVariantType *type)
1198 const gchar *type_string;
1200 g_return_val_if_fail (g_variant_type_check (type), NULL);
1202 type_string = g_variant_type_peek_string (type);
1203 g_assert (type_string[0] == '(' || type_string[0] == '{');
1205 if (type_string[1] == ')')
1208 return (const GVariantType *) &type_string[1];
1212 * g_variant_type_next:
1213 * @type: a #GVariantType from a previous call
1215 * Determines the next item type of a tuple or dictionary entry
1218 * @type must be the result of a previous call to
1219 * g_variant_type_first() or g_variant_type_next().
1221 * If called on the key type of a dictionary entry then this call
1222 * returns the value type. If called on the value type of a dictionary
1223 * entry then this call returns %NULL.
1225 * For tuples, %NULL is returned when @type is the last item in a tuple.
1227 * Returns: (transfer none): the next #GVariantType after @type, or %NULL
1231 const GVariantType *
1232 g_variant_type_next (const GVariantType *type)
1234 const gchar *type_string;
1236 g_return_val_if_fail (g_variant_type_check (type), NULL);
1238 type_string = g_variant_type_peek_string (type);
1239 type_string += g_variant_type_get_string_length (type);
1241 if (*type_string == ')' || *type_string == '}')
1244 return (const GVariantType *) type_string;
1248 * g_variant_type_n_items:
1249 * @type: a tuple or dictionary entry #GVariantType
1251 * Determines the number of items contained in a tuple or
1252 * dictionary entry type.
1254 * This function may only be used with tuple or dictionary entry types,
1255 * but must not be used with the generic tuple type
1256 * %G_VARIANT_TYPE_TUPLE.
1258 * In the case of a dictionary entry type, this function will always
1261 * Returns: the number of items in @type
1266 g_variant_type_n_items (const GVariantType *type)
1270 g_return_val_if_fail (g_variant_type_check (type), 0);
1272 for (type = g_variant_type_first (type);
1274 type = g_variant_type_next (type))
1281 * g_variant_type_key:
1282 * @type: a dictionary entry #GVariantType
1284 * Determines the key type of a dictionary entry type.
1286 * This function may only be used with a dictionary entry type. Other
1287 * than the additional restriction, this call is equivalent to
1288 * g_variant_type_first().
1290 * Returns: (transfer none): the key type of the dictionary entry
1294 const GVariantType *
1295 g_variant_type_key (const GVariantType *type)
1297 const gchar *type_string;
1299 g_return_val_if_fail (g_variant_type_check (type), NULL);
1301 type_string = g_variant_type_peek_string (type);
1302 g_assert (type_string[0] == '{');
1304 return (const GVariantType *) &type_string[1];
1308 * g_variant_type_value:
1309 * @type: a dictionary entry #GVariantType
1311 * Determines the value type of a dictionary entry type.
1313 * This function may only be used with a dictionary entry type.
1315 * Returns: (transfer none): the value type of the dictionary entry
1319 const GVariantType *
1320 g_variant_type_value (const GVariantType *type)
1322 const gchar *type_string;
1324 g_return_val_if_fail (g_variant_type_check (type), NULL);
1326 type_string = g_variant_type_peek_string (type);
1327 g_assert (type_string[0] == '{');
1329 return g_variant_type_next (g_variant_type_key (type));
1333 * g_variant_type_new_tuple:
1334 * @items: (array length=length): an array of #GVariantTypes, one for each item
1335 * @length: the length of @items, or -1
1337 * Constructs a new tuple type, from @items.
1339 * @length is the number of items in @items, or -1 to indicate that
1340 * @items is %NULL-terminated.
1342 * It is appropriate to call g_variant_type_free() on the return value.
1344 * Returns: (transfer full): a new tuple #GVariantType
1348 static GVariantType *
1349 g_variant_type_new_tuple_slow (const GVariantType * const *items,
1352 /* the "slow" version is needed in case the static buffer of 1024
1353 * bytes is exceeded when running the normal version. this will
1354 * happen only in truly insane code, so it can be slow.
1359 string = g_string_new ("(");
1360 for (i = 0; i < length; i++)
1362 const GVariantType *type;
1365 g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1368 size = g_variant_type_get_string_length (type);
1369 g_string_append_len (string, (const gchar *) type, size);
1371 g_string_append_c (string, ')');
1373 return (GVariantType *) g_string_free (string, FALSE);
1377 g_variant_type_new_tuple (const GVariantType * const *items,
1384 g_return_val_if_fail (length == 0 || items != NULL, NULL);
1387 for (length = 0; items[length] != NULL; length++);
1390 buffer[offset++] = '(';
1392 for (i = 0; i < length; i++)
1394 const GVariantType *type;
1397 g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1400 size = g_variant_type_get_string_length (type);
1402 if (offset + size >= sizeof buffer) /* leave room for ')' */
1403 return g_variant_type_new_tuple_slow (items, length);
1405 memcpy (&buffer[offset], type, size);
1409 g_assert (offset < sizeof buffer);
1410 buffer[offset++] = ')';
1412 return (GVariantType *) g_memdup (buffer, offset);
1416 * g_variant_type_new_array: (constructor)
1417 * @element: a #GVariantType
1419 * Constructs the type corresponding to an array of elements of the
1422 * It is appropriate to call g_variant_type_free() on the return value.
1424 * Returns: (transfer full): a new array #GVariantType
1429 g_variant_type_new_array (const GVariantType *element)
1434 g_return_val_if_fail (g_variant_type_check (element), NULL);
1436 size = g_variant_type_get_string_length (element);
1437 new = g_malloc (size + 1);
1440 memcpy (new + 1, element, size);
1442 return (GVariantType *) new;
1446 * g_variant_type_new_maybe: (constructor)
1447 * @element: a #GVariantType
1449 * Constructs the type corresponding to a maybe instance containing
1450 * type @type or Nothing.
1452 * It is appropriate to call g_variant_type_free() on the return value.
1454 * Returns: (transfer full): a new maybe #GVariantType
1459 g_variant_type_new_maybe (const GVariantType *element)
1464 g_return_val_if_fail (g_variant_type_check (element), NULL);
1466 size = g_variant_type_get_string_length (element);
1467 new = g_malloc (size + 1);
1470 memcpy (new + 1, element, size);
1472 return (GVariantType *) new;
1476 * g_variant_type_new_dict_entry: (constructor)
1477 * @key: a basic #GVariantType
1478 * @value: a #GVariantType
1480 * Constructs the type corresponding to a dictionary entry with a key
1481 * of type @key and a value of type @value.
1483 * It is appropriate to call g_variant_type_free() on the return value.
1485 * Returns: (transfer full): a new dictionary entry #GVariantType
1490 g_variant_type_new_dict_entry (const GVariantType *key,
1491 const GVariantType *value)
1493 gsize keysize, valsize;
1496 g_return_val_if_fail (g_variant_type_check (key), NULL);
1497 g_return_val_if_fail (g_variant_type_check (value), NULL);
1499 keysize = g_variant_type_get_string_length (key);
1500 valsize = g_variant_type_get_string_length (value);
1502 new = g_malloc (1 + keysize + valsize + 1);
1505 memcpy (new + 1, key, keysize);
1506 memcpy (new + 1 + keysize, value, valsize);
1507 new[1 + keysize + valsize] = '}';
1509 return (GVariantType *) new;
1513 const GVariantType *
1514 g_variant_type_checked_ (const gchar *type_string)
1516 g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1517 return (const GVariantType *) type_string;