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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Ryan Lortie <desrt@desrt.ca>
23 #include "gvarianttype.h"
25 #include <glib/gtestutils.h>
26 #include <glib/gstrfuncs.h>
33 * SECTION: gvarianttype
34 * @title: GVariantType
35 * @short_description: introduction to the GVariant type system
36 * @see_also: #GVariantType, #GVariant
38 * This section introduces the GVariant type system. It is based, in
39 * large part, on the DBus type system, with two major changes and some minor
40 * lifting of restrictions. The <ulink
41 * url='http://dbus.freedesktop.org/doc/dbus-specification.html'>DBus
42 * specification</ulink>, therefore, provides a significant amount of
43 * information that is useful when working with GVariant.
45 * The first major change with respect to the DBus type system is the
46 * introduction of maybe (or "nullable") types. Any type in GVariant can be
47 * converted to a maybe type, in which case, "nothing" (or "null") becomes a
48 * valid value. Maybe types have been added by introducing the
49 * character "<literal>m</literal>" to type strings.
51 * The second major change is that the GVariant type system supports the
52 * concept of "indefinite types" -- types that are less specific than
53 * the normal types found in DBus. For example, it is possible to speak
54 * of "an array of any type" in GVariant, where the DBus type system
55 * would require you to speak of "an array of integers" or "an array of
56 * strings". Indefinite types have been added by introducing the
57 * characters "<literal>*</literal>", "<literal>?</literal>" and
58 * "<literal>r</literal>" 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 DBus, 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: DBus
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 DBus 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_a() with an indefinite type may return %TRUE.
82 * For example, you can not have a value that represents "an array of no
83 * particular type", but you can have an "array of integers" which
84 * certainly matches the type of "an array of no particular type", since
85 * "array of integers" is a subtype of "array of no particular type".
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 DBus 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 DBus object path.
328 * <literal>g</literal>
333 * the type string of %G_VARIANT_TYPE_STRING; a string in the
334 * form of a DBus 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)
487 const gchar *type_string;
492 type_string = (const gchar *) type;
493 #ifndef G_DISABLE_CHECKS
494 return g_variant_type_string_scan (type_string, NULL, NULL);
501 * g_variant_type_string_scan:
502 * @string: a pointer to any string
503 * @limit: the end of @string, or %NULL
504 * @endptr: location to store the end pointer, or %NULL
505 * @returns: %TRUE if a valid type string was found
507 * Scan for a single complete and valid GVariant type string in @string.
508 * The memory pointed to by @limit (or bytes beyond it) is never
511 * If a valid type string is found, @endptr is updated to point to the
512 * first character past the end of the string that was found and %TRUE
515 * If there is no valid type string starting at @string, or if the type
516 * string does not end before @limit then %FALSE is returned.
518 * For the simple case of checking if a string is a valid type string,
519 * see g_variant_type_string_is_valid().
524 g_variant_type_string_scan (const gchar *string,
526 const gchar **endptr)
528 g_return_val_if_fail (string != NULL, FALSE);
530 if (string == limit || *string == '\0')
536 while (string == limit || *string != ')')
537 if (!g_variant_type_string_scan (string, limit, &string))
544 if (string == limit || *string == '\0' || /* { */
545 !strchr ("bynqihuxtdsog?", *string++) || /* key */
546 !g_variant_type_string_scan (string, limit, &string) || /* value */
547 string == limit || *string++ != '}') /* } */
553 return g_variant_type_string_scan (string, limit, endptr);
555 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
556 case 'x': case 't': case 'd': case 's': case 'o': case 'g':
557 case 'v': case 'r': case '*': case '?': case 'h':
571 * g_variant_type_string_is_valid:
572 * @type_string: a pointer to any string
573 * @returns: %TRUE if @type_string is exactly one valid type string
575 * Checks if @type_string is a valid GVariant type string. This call is
576 * equivalent to calling g_variant_type_string_scan() and confirming
577 * that the following character is a nul terminator.
582 g_variant_type_string_is_valid (const gchar *type_string)
586 g_return_val_if_fail (type_string != NULL, FALSE);
588 if (!g_variant_type_string_scan (type_string, NULL, &endptr))
591 return *endptr == '\0';
595 * g_variant_type_free:
596 * @type: a #GVariantType, or %NULL
598 * Frees a #GVariantType that was allocated with
599 * g_variant_type_copy(), g_variant_type_new() or one of the container
600 * type constructor functions.
602 * In the case that @type is %NULL, this function does nothing.
607 g_variant_type_free (GVariantType *type)
609 g_return_if_fail (type == NULL || g_variant_type_check (type));
615 * g_variant_type_copy:
616 * @type: a #GVariantType
617 * @returns: a new #GVariantType
619 * Makes a copy of a #GVariantType. It is appropriate to call
620 * g_variant_type_free() on the return value. @type may not be %NULL.
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
644 * @returns: a new #GVariantType
646 * Creates a new #GVariantType corresponding to the type string given
647 * by @type_string. It is appropriate to call g_variant_type_free() on
650 * It is a programmer error to call this function with an invalid type
651 * string. Use g_variant_type_string_is_valid() if you are unsure.
656 g_variant_type_new (const gchar *type_string)
658 g_return_val_if_fail (type_string != NULL, NULL);
660 return g_variant_type_copy (G_VARIANT_TYPE (type_string));
664 * g_variant_type_get_string_length:
665 * @type: a #GVariantType
666 * @returns: the length of the corresponding type string
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().
675 g_variant_type_get_string_length (const GVariantType *type)
677 const gchar *type_string = (const gchar *) type;
681 g_return_val_if_fail (g_variant_type_check (type), 0);
685 while (type_string[index] == 'a' || type_string[index] == 'm')
688 if (type_string[index] == '(' || type_string[index] == '{')
691 else if (type_string[index] == ')' || type_string[index] == '}')
702 * g_variant_type_peek_string:
703 * @type: a #GVariantType
704 * @returns: the corresponding type string (not nul-terminated)
706 * Returns the type string corresponding to the given @type. The
707 * result is not nul-terminated; in order to determine its length you
708 * must call g_variant_type_get_string_length().
710 * To get a nul-terminated string, see g_variant_type_dup_string().
715 g_variant_type_peek_string (const GVariantType *type)
717 g_return_val_if_fail (g_variant_type_check (type), NULL);
719 return (const gchar *) type;
723 * g_variant_type_dup_string:
724 * @type: a #GVariantType
725 * @returns: the corresponding type string
727 * Returns a newly-allocated copy of the type string corresponding to
728 * @type. The returned string is nul-terminated. It is appropriate to
729 * call g_free() on the return value.
734 g_variant_type_dup_string (const GVariantType *type)
736 g_return_val_if_fail (g_variant_type_check (type), NULL);
738 return g_strndup (g_variant_type_peek_string (type),
739 g_variant_type_get_string_length (type));
743 * g_variant_type_is_definite:
744 * @type: a #GVariantType
745 * @returns: %TRUE if @type is definite
747 * Determines if the given @type is definite (ie: not indefinite).
749 * A type is definite if its type string does not contain any indefinite
750 * type characters ('*', '?', or 'r').
752 * A #GVariant instance may not have an indefinite type, so calling
753 * this function on the result of g_variant_get_type() will always
754 * result in %TRUE being returned. Calling this function on an
755 * indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in
756 * %FALSE being returned.
761 g_variant_type_is_definite (const GVariantType *type)
763 const gchar *type_string;
767 g_return_val_if_fail (g_variant_type_check (type), FALSE);
769 type_length = g_variant_type_get_string_length (type);
770 type_string = g_variant_type_peek_string (type);
772 for (i = 0; i < type_length; i++)
773 if (type_string[i] == '*' ||
774 type_string[i] == '?' ||
775 type_string[i] == 'r')
782 * g_variant_type_is_container:
783 * @type: a #GVariantType
784 * @returns: %TRUE if @type is a container type
786 * Determines if the given @type is a container type.
788 * Container types are any array, maybe, tuple, or dictionary
789 * entry types plus the variant type.
791 * This function returns %TRUE for any indefinite type for which every
792 * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
798 g_variant_type_is_container (const GVariantType *type)
802 g_return_val_if_fail (g_variant_type_check (type), FALSE);
804 first_char = g_variant_type_peek_string (type)[0];
821 * g_variant_type_is_basic:
822 * @type: a #GVariantType
823 * @returns: %TRUE if @type is a basic type
825 * Determines if the given @type is a basic type.
827 * Basic types are booleans, bytes, integers, doubles, strings, object
828 * paths and signatures.
830 * Only a basic type may be used as the key of a dictionary entry.
832 * This function returns %FALSE for all indefinite types except
833 * %G_VARIANT_TYPE_BASIC.
838 g_variant_type_is_basic (const GVariantType *type)
842 g_return_val_if_fail (g_variant_type_check (type), FALSE);
844 first_char = g_variant_type_peek_string (type)[0];
869 * g_variant_type_is_maybe:
870 * @type: a #GVariantType
871 * @returns: %TRUE if @type is a maybe type
873 * Determines if the given @type is a maybe type. This is true if the
874 * type string for @type starts with an 'm'.
876 * This function returns %TRUE for any indefinite type for which every
877 * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
883 g_variant_type_is_maybe (const GVariantType *type)
885 g_return_val_if_fail (g_variant_type_check (type), FALSE);
887 return g_variant_type_peek_string (type)[0] == 'm';
891 * g_variant_type_is_array:
892 * @type: a #GVariantType
893 * @returns: %TRUE if @type is an array type
895 * Determines if the given @type is an array type. This is true if the
896 * type string for @type starts with an 'a'.
898 * This function returns %TRUE for any indefinite type for which every
899 * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
905 g_variant_type_is_array (const GVariantType *type)
907 g_return_val_if_fail (g_variant_type_check (type), FALSE);
909 return g_variant_type_peek_string (type)[0] == 'a';
913 * g_variant_type_is_tuple:
914 * @type: a #GVariantType
915 * @returns: %TRUE if @type is a tuple type
917 * Determines if the given @type is a tuple type. This is true if the
918 * type string for @type starts with a '(' or if @type is
919 * %G_VARIANT_TYPE_TUPLE.
921 * This function returns %TRUE for any indefinite type for which every
922 * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
928 g_variant_type_is_tuple (const GVariantType *type)
932 g_return_val_if_fail (g_variant_type_check (type), FALSE);
934 type_char = g_variant_type_peek_string (type)[0];
935 return type_char == 'r' || type_char == '(';
939 * g_variant_type_is_dict_entry:
940 * @type: a #GVariantType
941 * @returns: %TRUE if @type is a dictionary entry type
943 * Determines if the given @type is a dictionary entry type. This is
944 * true if the type string for @type starts with a '{'.
946 * This function returns %TRUE for any indefinite type for which every
947 * definite subtype is a dictionary entry type --
948 * %G_VARIANT_TYPE_DICT_ENTRY, for example.
953 g_variant_type_is_dict_entry (const GVariantType *type)
955 g_return_val_if_fail (g_variant_type_check (type), FALSE);
957 return g_variant_type_peek_string (type)[0] == '{';
961 * g_variant_type_hash:
962 * @type: a #GVariantType
963 * @returns: the hash value
967 * The argument type of @type is only #gconstpointer to allow use with
968 * #GHashTable without function pointer casting. A valid
969 * #GVariantType must be provided.
974 g_variant_type_hash (gconstpointer type)
976 const gchar *type_string;
981 g_return_val_if_fail (g_variant_type_check (type), 0);
983 type_string = g_variant_type_peek_string (type);
984 length = g_variant_type_get_string_length (type);
986 for (i = 0; i < length; i++)
987 value = (value << 5) - value + type_string[i];
993 * g_variant_type_equal:
994 * @type1: a #GVariantType
995 * @type2: a #GVariantType
996 * @returns: %TRUE if @type1 and @type2 are exactly equal
998 * Compares @type1 and @type2 for equality.
1000 * Only returns %TRUE if the types are exactly equal. Even if one type
1001 * is an indefinite type and the other is a subtype of it, %FALSE will
1002 * be returned if they are not exactly equal. If you want to check for
1003 * subtypes, use g_variant_type_is_subtype_of().
1005 * The argument types of @type1 and @type2 are only #gconstpointer to
1006 * allow use with #GHashTable without function pointer casting. For
1007 * both arguments, a valid #GVariantType must be provided.
1012 g_variant_type_equal (gconstpointer type1,
1013 gconstpointer type2)
1015 const gchar *string1, *string2;
1018 g_return_val_if_fail (g_variant_type_check (type1), FALSE);
1019 g_return_val_if_fail (g_variant_type_check (type2), FALSE);
1024 size1 = g_variant_type_get_string_length (type1);
1025 size2 = g_variant_type_get_string_length (type2);
1030 string1 = g_variant_type_peek_string (type1);
1031 string2 = g_variant_type_peek_string (type2);
1033 return memcmp (string1, string2, size1) == 0;
1037 * g_variant_type_is_subtype_of:
1038 * @type: a #GVariantType
1039 * @supertype: a #GVariantType
1040 * @returns: %TRUE if @type is a subtype of @supertype
1042 * Checks if @type is a subtype of @supertype.
1044 * This function returns %TRUE if @type is a subtype of @supertype. All
1045 * types are considered to be subtypes of themselves. Aside from that,
1046 * only indefinite types can have subtypes.
1051 g_variant_type_is_subtype_of (const GVariantType *type,
1052 const GVariantType *supertype)
1054 const gchar *supertype_string;
1055 const gchar *supertype_end;
1056 const gchar *type_string;
1058 g_return_val_if_fail (g_variant_type_check (type), FALSE);
1059 g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
1061 supertype_string = g_variant_type_peek_string (supertype);
1062 type_string = g_variant_type_peek_string (type);
1064 supertype_end = supertype_string +
1065 g_variant_type_get_string_length (supertype);
1067 /* we know that type and supertype are both well-formed, so it's
1068 * safe to treat this merely as a text processing problem.
1070 while (supertype_string < supertype_end)
1072 char supertype_char = *supertype_string++;
1074 if (supertype_char == *type_string)
1077 else if (*type_string == ')')
1082 const GVariantType *target_type = (GVariantType *) type_string;
1084 switch (supertype_char)
1087 if (!g_variant_type_is_tuple (target_type))
1095 if (!g_variant_type_is_basic (target_type))
1103 type_string += g_variant_type_get_string_length (target_type);
1111 * g_variant_type_element:
1112 * @type: an array or maybe #GVariantType
1113 * @returns: the element type of @type
1115 * Determines the element type of an array or maybe type.
1117 * This function may only be used with array or maybe types.
1121 const GVariantType *
1122 g_variant_type_element (const GVariantType *type)
1124 const gchar *type_string;
1126 g_return_val_if_fail (g_variant_type_check (type), NULL);
1128 type_string = g_variant_type_peek_string (type);
1130 g_assert (type_string[0] == 'a' || type_string[0] == 'm');
1132 return (const GVariantType *) &type_string[1];
1136 * g_variant_type_first:
1137 * @type: a tuple or dictionary entry #GVariantType
1138 * @returns: the first item type of @type, or %NULL
1140 * Determines the first item type of a tuple or dictionary entry
1143 * This function may only be used with tuple or dictionary entry types,
1144 * but must not be used with the generic tuple type
1145 * %G_VARIANT_TYPE_TUPLE.
1147 * In the case of a dictionary entry type, this returns the type of
1150 * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
1152 * This call, together with g_variant_type_next() provides an iterator
1153 * interface over tuple and dictionary entry types.
1157 const GVariantType *
1158 g_variant_type_first (const GVariantType *type)
1160 const gchar *type_string;
1162 g_return_val_if_fail (g_variant_type_check (type), NULL);
1164 type_string = g_variant_type_peek_string (type);
1165 g_assert (type_string[0] == '(' || type_string[0] == '{');
1167 if (type_string[1] == ')')
1170 return (const GVariantType *) &type_string[1];
1174 * g_variant_type_next:
1175 * @type: a #GVariantType from a previous call
1176 * @returns: the next #GVariantType after @type, or %NULL
1178 * Determines the next item type of a tuple or dictionary entry
1181 * @type must be the result of a previous call to
1182 * g_variant_type_first() or g_variant_type_next().
1184 * If called on the key type of a dictionary entry then this call
1185 * returns the value type. If called on the value type of a dictionary
1186 * entry then this call returns %NULL.
1188 * For tuples, %NULL is returned when @type is the last item in a tuple.
1192 const GVariantType *
1193 g_variant_type_next (const GVariantType *type)
1195 const gchar *type_string;
1197 g_return_val_if_fail (g_variant_type_check (type), NULL);
1199 type_string = g_variant_type_peek_string (type);
1200 type_string += g_variant_type_get_string_length (type);
1202 if (*type_string == ')' || *type_string == '}')
1205 return (const GVariantType *) type_string;
1209 * g_variant_type_n_items:
1210 * @type: a tuple or dictionary entry #GVariantType
1211 * @returns: the number of items in @type
1213 * Determines the number of items contained in a tuple or
1214 * dictionary entry type.
1216 * This function may only be used with tuple or dictionary entry types,
1217 * but must not be used with the generic tuple type
1218 * %G_VARIANT_TYPE_TUPLE.
1220 * In the case of a dictionary entry type, this function will always
1226 g_variant_type_n_items (const GVariantType *type)
1230 g_return_val_if_fail (g_variant_type_check (type), 0);
1232 for (type = g_variant_type_first (type);
1234 type = g_variant_type_next (type))
1241 * g_variant_type_key:
1242 * @type: a dictionary entry #GVariantType
1243 * @returns: the key type of the dictionary entry
1245 * Determines the key type of a dictionary entry type.
1247 * This function may only be used with a dictionary entry type. Other
1248 * than the additional restriction, this call is equivalent to
1249 * g_variant_type_first().
1253 const GVariantType *
1254 g_variant_type_key (const GVariantType *type)
1256 const gchar *type_string;
1258 g_return_val_if_fail (g_variant_type_check (type), NULL);
1260 type_string = g_variant_type_peek_string (type);
1261 g_assert (type_string[0] == '{');
1263 return (const GVariantType *) &type_string[1];
1267 * g_variant_type_value:
1268 * @type: a dictionary entry #GVariantType
1269 * @returns: the value type of the dictionary entry
1271 * Determines the value type of a dictionary entry type.
1273 * This function may only be used with a dictionary entry type.
1277 const GVariantType *
1278 g_variant_type_value (const GVariantType *type)
1280 const gchar *type_string;
1282 g_return_val_if_fail (g_variant_type_check (type), NULL);
1284 type_string = g_variant_type_peek_string (type);
1285 g_assert (type_string[0] == '{');
1287 return g_variant_type_next (g_variant_type_key (type));
1291 * g_variant_type_new_tuple:
1292 * @items: an array of #GVariantTypes, one for each item
1293 * @length: the length of @items, or -1
1294 * @returns: a new tuple #GVariantType
1296 * Constructs a new tuple type, from @items.
1298 * @length is the number of items in @items, or -1 to indicate that
1299 * @items is %NULL-terminated.
1301 * It is appropriate to call g_variant_type_free() on the return value.
1305 static GVariantType *
1306 g_variant_type_new_tuple_slow (const GVariantType * const *items,
1309 /* the "slow" version is needed in case the static buffer of 1024
1310 * bytes is exceeded when running the normal version. this will
1311 * happen only in truly insane code, so it can be slow.
1316 string = g_string_new ("(");
1317 for (i = 0; i < length; i++)
1319 const GVariantType *type;
1322 g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1325 size = g_variant_type_get_string_length (type);
1326 g_string_append_len (string, (const gchar *) type, size);
1328 g_string_append_c (string, ')');
1330 return (GVariantType *) g_string_free (string, FALSE);
1334 g_variant_type_new_tuple (const GVariantType * const *items,
1341 g_return_val_if_fail (length == 0 || items != NULL, NULL);
1344 for (length = 0; items[length] != NULL; length++);
1347 buffer[offset++] = '(';
1349 for (i = 0; i < length; i++)
1351 const GVariantType *type;
1354 g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1357 size = g_variant_type_get_string_length (type);
1359 if (offset + size >= sizeof buffer) /* leave room for ')' */
1360 return g_variant_type_new_tuple_slow (items, length);
1362 memcpy (&buffer[offset], type, size);
1366 g_assert (offset < sizeof buffer);
1367 buffer[offset++] = ')';
1369 return (GVariantType *) g_memdup (buffer, offset);
1373 * g_variant_type_new_array:
1374 * @element: a #GVariantType
1375 * @returns: a new array #GVariantType
1377 * Constructs the type corresponding to an array of elements of the
1380 * It is appropriate to call g_variant_type_free() on the return value.
1385 g_variant_type_new_array (const GVariantType *element)
1390 g_return_val_if_fail (g_variant_type_check (element), NULL);
1392 size = g_variant_type_get_string_length (element);
1393 new = g_malloc (size + 1);
1396 memcpy (new + 1, element, size);
1398 return (GVariantType *) new;
1402 * g_variant_type_new_maybe:
1403 * @element: a #GVariantType
1404 * @returns: a new maybe #GVariantType
1406 * Constructs the type corresponding to a maybe instance containing
1407 * type @type or Nothing.
1409 * It is appropriate to call g_variant_type_free() on the return value.
1414 g_variant_type_new_maybe (const GVariantType *element)
1419 g_return_val_if_fail (g_variant_type_check (element), NULL);
1421 size = g_variant_type_get_string_length (element);
1422 new = g_malloc (size + 1);
1425 memcpy (new + 1, element, size);
1427 return (GVariantType *) new;
1431 * g_variant_type_new_dict_entry:
1432 * @key: a basic #GVariantType
1433 * @value: a #GVariantType
1434 * @returns: a new dictionary entry #GVariantType
1436 * Constructs the type corresponding to a dictionary entry with a key
1437 * of type @key and a value of type @value.
1439 * It is appropriate to call g_variant_type_free() on the return value.
1444 g_variant_type_new_dict_entry (const GVariantType *key,
1445 const GVariantType *value)
1447 gsize keysize, valsize;
1450 g_return_val_if_fail (g_variant_type_check (key), NULL);
1451 g_return_val_if_fail (g_variant_type_check (value), NULL);
1453 keysize = g_variant_type_get_string_length (key);
1454 valsize = g_variant_type_get_string_length (value);
1456 new = g_malloc (1 + keysize + valsize + 1);
1459 memcpy (new + 1, key, keysize);
1460 memcpy (new + 1 + keysize, value, valsize);
1461 new[1 + keysize + valsize] = '}';
1463 return (GVariantType *) new;
1467 const GVariantType *
1468 g_variant_type_checked_ (const gchar *type_string)
1470 g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1471 return (const GVariantType *) type_string;
1474 #define __G_VARIANT_TYPE_C__
1475 #include "galiasdef.c"