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>
25 #include "gvarianttype.h"
27 #include <glib/gtestutils.h>
28 #include <glib/gstrfuncs.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 DBus type system, with two major changes and some minor
41 * lifting of restrictions. The <ulink
42 * url='http://dbus.freedesktop.org/doc/dbus-specification.html'>DBus
43 * specification</ulink>, therefore, provides a significant amount of
44 * information that is useful when working with GVariant.
46 * The first major change with respect to the DBus 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 "<literal>m</literal>" 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 DBus. For example, it is possible to speak
55 * of "an array of any type" in GVariant, where the DBus 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 "<literal>*</literal>", "<literal>?</literal>" and
59 * "<literal>r</literal>" to type strings.
61 * Finally, all arbitrary restrictions relating to the complexity of
62 * types are lifted along with the restriction that dictionary entries
63 * may only appear nested inside of arrays.
65 * Just as in DBus, GVariant types are described with strings ("type
66 * strings"). Subject to the differences mentioned above, these strings
67 * are of the same form as those found in DBus. Note, however: DBus
68 * always works in terms of messages and therefore individual type
69 * strings appear nowhere in its interface. Instead, "signatures"
70 * are a concatenation of the strings of the type of each argument in a
71 * message. GVariant deals with single values directly so GVariant type
72 * strings always describe the type of exactly one value. This means
73 * that a DBus signature string is generally not a valid GVariant type
74 * string -- except in the case that it is the signature of a message
75 * containing exactly one argument.
77 * An indefinite type is similar in spirit to what may be called an
78 * abstract type in other type systems. No value can exist that has an
79 * indefinite type as its type, but values can exist that have types
80 * that are subtypes of indefinite types. That is to say,
81 * g_variant_get_type() will never return an indefinite type, but
82 * calling g_variant_is_a() with an indefinite type may return %TRUE.
83 * For example, you can not have a value that represents "an array of no
84 * particular type", but you can have an "array of integers" which
85 * certainly matches the type of "an array of no particular type", since
86 * "array of integers" is a subtype of "array of no particular type".
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 * A detailed description of GVariant type strings is given here:
98 * <refsect2 id='gvariant-typestrings'>
99 * <title>GVariant Type Strings</title>
101 * A GVariant type string can be any of the following:
106 * any basic type string (listed below)
111 * "<literal>v</literal>", "<literal>r</literal>" or
112 * "<literal>*</literal>"
117 * one of the characters '<literal>a</literal>' or
118 * '<literal>m</literal>', followed by another type string
123 * the character '<literal>(</literal>', followed by a concatenation
124 * of zero or more other type strings, followed by the character
125 * '<literal>)</literal>'
130 * the character '<literal>{</literal>', followed by a basic type
131 * string (see below), followed by another type string, followed by
132 * the character '<literal>}</literal>'
137 * A basic type string describes a basic type (as per
138 * g_variant_type_is_basic()) and is always a single
139 * character in length. The valid basic type strings are
140 * "<literal>b</literal>", "<literal>y</literal>",
141 * "<literal>n</literal>", "<literal>q</literal>",
142 * "<literal>i</literal>", "<literal>u</literal>",
143 * "<literal>x</literal>", "<literal>t</literal>",
144 * "<literal>h</literal>", "<literal>d</literal>",
145 * "<literal>s</literal>", "<literal>o</literal>",
146 * "<literal>g</literal>" and "<literal>?</literal>".
149 * The above definition is recursive to arbitrary depth.
150 * "<literal>aaaaai</literal>" and "<literal>(ui(nq((y)))s)</literal>"
151 * are both valid type strings, as is
152 * "<literal>a(aa(ui)(qna{ya(yd)}))</literal>".
155 * The meaning of each of the characters is as follows:
163 * <emphasis role='strong'>Character</emphasis>
168 * <emphasis role='strong'>Meaning</emphasis>
175 * <literal>b</literal>
180 * the type string of %G_VARIANT_TYPE_BOOLEAN; a boolean value.
187 * <literal>y</literal>
192 * the type string of %G_VARIANT_TYPE_BYTE; a byte.
199 * <literal>n</literal>
204 * the type string of %G_VARIANT_TYPE_INT16; a signed 16 bit
212 * <literal>q</literal>
217 * the type string of %G_VARIANT_TYPE_UINT16; an unsigned 16 bit
225 * <literal>i</literal>
230 * the type string of %G_VARIANT_TYPE_INT32; a signed 32 bit
238 * <literal>u</literal>
243 * the type string of %G_VARIANT_TYPE_UINT32; an unsigned 32 bit
251 * <literal>x</literal>
256 * the type string of %G_VARIANT_TYPE_INT64; a signed 64 bit
264 * <literal>t</literal>
269 * the type string of %G_VARIANT_TYPE_UINT64; an unsigned 64 bit
277 * <literal>h</literal>
282 * the type string of %G_VARIANT_TYPE_HANDLE; a signed 32 bit
283 * value that, by convention, is used as an index into an array
284 * of file descriptors that are sent alongside a DBus message.
291 * <literal>d</literal>
296 * the type string of %G_VARIANT_TYPE_DOUBLE; a double precision
297 * floating point value.
304 * <literal>s</literal>
309 * the type string of %G_VARIANT_TYPE_STRING; a string.
316 * <literal>o</literal>
321 * the type string of %G_VARIANT_TYPE_OBJECT_PATH; a string in
322 * the form of a DBus object path.
329 * <literal>g</literal>
334 * the type string of %G_VARIANT_TYPE_STRING; a string in the
335 * form of a DBus type signature.
342 * <literal>?</literal>
347 * the type string of %G_VARIANT_TYPE_BASIC; an indefinite type
348 * that is a supertype of any of the basic types.
355 * <literal>v</literal>
360 * the type string of %G_VARIANT_TYPE_VARIANT; a container type
361 * that contain any other type of value.
368 * <literal>a</literal>
373 * used as a prefix on another type string to mean an array of
374 * that type; the type string "<literal>ai</literal>", for
375 * example, is the type of an array of 32 bit signed integers.
382 * <literal>m</literal>
387 * used as a prefix on another type string to mean a "maybe", or
388 * "nullable", version of that type; the type string
389 * "<literal>ms</literal>", for example, is the type of a value
390 * that maybe contains a string, or maybe contains nothing.
397 * <literal>()</literal>
402 * used to enclose zero or more other concatenated type strings
403 * to create a tuple type; the type string
404 * "<literal>(is)</literal>", for example, is the type of a pair
405 * of an integer and a string.
412 * <literal>r</literal>
417 * the type string of %G_VARIANT_TYPE_TUPLE; an indefinite type
418 * that is a supertype of any tuple type, regardless of the
426 * <literal>{}</literal>
431 * used to enclose a basic type string concatenated with another
432 * type string to create a dictionary entry type, which usually
433 * appears inside of an array to form a dictionary; the type
434 * string "<literal>a{sd}</literal>", for example, is the type of
435 * a dictionary that maps strings to double precision floating
439 * The first type (the basic type) is the key type and the second
440 * type is the value type. The reason that the first type is
441 * restricted to being a basic type is so that it can easily be
449 * <literal>*</literal>
454 * the type string of %G_VARIANT_TYPE_ANY; the indefinite type
455 * that is a supertype of all types. Note that, as with all type
456 * strings, this character represents exactly one type. It
457 * cannot be used inside of tuples to mean "any number of items".
465 * Any type string of a container that contains an indefinite type is,
466 * itself, an indefinite type. For example, the type string
467 * "<literal>a*</literal>" (corresponding to %G_VARIANT_TYPE_ARRAY) is
468 * an indefinite type that is a supertype of every array type.
469 * "<literal>(*s)</literal>" is a supertype of all tuples that
470 * contain exactly two items where the second item is a string.
473 * "<literal>a{?*}</literal>" is an indefinite type that is a
474 * supertype of all arrays containing dictionary entries where the key
475 * is any basic type and the value is any type at all. This is, by
476 * definition, a dictionary, so this type string corresponds to
477 * %G_VARIANT_TYPE_DICTIONARY. Note that, due to the restriction that
478 * the key of a dictionary entry must be a basic type,
479 * "<literal>{**}</literal>" is not a valid type string.
486 g_variant_type_check (const GVariantType
*type
)
488 const gchar
*type_string
;
493 type_string
= (const gchar
*) type
;
494 #ifndef G_DISABLE_CHECKS
495 return g_variant_type_string_scan (type_string
, NULL
, NULL
);
502 * g_variant_type_string_scan:
503 * @string: a pointer to any string
504 * @limit: the end of @string, or %NULL
505 * @endptr: location to store the end pointer, or %NULL
506 * @returns: %TRUE if a valid type string was found
508 * Scan for a single complete and valid GVariant type string in @string.
509 * The memory pointed to by @limit (or bytes beyond it) is never
512 * If a valid type string is found, @endptr is updated to point to the
513 * first character past the end of the string that was found and %TRUE
516 * If there is no valid type string starting at @string, or if the type
517 * string does not end before @limit then %FALSE is returned.
519 * For the simple case of checking if a string is a valid type string,
520 * see g_variant_type_string_is_valid().
525 g_variant_type_string_scan (const gchar
*string
,
527 const gchar
**endptr
)
529 g_return_val_if_fail (string
!= NULL
, FALSE
);
531 if (string
== limit
|| *string
== '\0')
537 while (string
== limit
|| *string
!= ')')
538 if (!g_variant_type_string_scan (string
, limit
, &string
))
545 if (string
== limit
|| *string
== '\0' || /* { */
546 !strchr ("bynqihuxtdsog?", *string
++) || /* key */
547 !g_variant_type_string_scan (string
, limit
, &string
) || /* value */
548 string
== limit
|| *string
++ != '}') /* } */
554 return g_variant_type_string_scan (string
, limit
, endptr
);
556 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
557 case 'x': case 't': case 'd': case 's': case 'o': case 'g':
558 case 'v': case 'r': case '*': case '?': case 'h':
572 * g_variant_type_string_is_valid:
573 * @type_string: a pointer to any string
574 * @returns: %TRUE if @type_string is exactly one valid type string
576 * Checks if @type_string is a valid GVariant type string. This call is
577 * equivalent to calling g_variant_type_string_scan() and confirming
578 * that the following character is a nul terminator.
583 g_variant_type_string_is_valid (const gchar
*type_string
)
587 g_return_val_if_fail (type_string
!= NULL
, FALSE
);
589 if (!g_variant_type_string_scan (type_string
, NULL
, &endptr
))
592 return *endptr
== '\0';
596 * g_variant_type_free:
597 * @type: a #GVariantType, or %NULL
599 * Frees a #GVariantType that was allocated with
600 * g_variant_type_copy(), g_variant_type_new() or one of the container
601 * type constructor functions.
603 * In the case that @type is %NULL, this function does nothing.
608 g_variant_type_free (GVariantType
*type
)
610 g_return_if_fail (type
== NULL
|| g_variant_type_check (type
));
616 * g_variant_type_copy:
617 * @type: a #GVariantType
618 * @returns: a new #GVariantType
620 * Makes a copy of a #GVariantType. It is appropriate to call
621 * g_variant_type_free() on the return value. @type may not be %NULL.
626 g_variant_type_copy (const GVariantType
*type
)
631 g_return_val_if_fail (g_variant_type_check (type
), NULL
);
633 length
= g_variant_type_get_string_length (type
);
634 new = g_malloc (length
+ 1);
636 memcpy (new, type
, length
);
639 return (GVariantType
*) new;
643 * g_variant_type_new:
644 * @type_string: a valid GVariant type string
645 * @returns: a new #GVariantType
647 * Creates a new #GVariantType corresponding to the type string given
648 * by @type_string. It is appropriate to call g_variant_type_free() on
651 * It is a programmer error to call this function with an invalid type
652 * string. Use g_variant_type_string_is_valid() if you are unsure.
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
667 * @returns: the length of the corresponding type string
669 * Returns the length of the type string corresponding to the given
670 * @type. This function must be used to determine the valid extent of
671 * the memory region returned by g_variant_type_peek_string().
676 g_variant_type_get_string_length (const GVariantType
*type
)
678 const gchar
*type_string
= (const gchar
*) type
;
682 g_return_val_if_fail (g_variant_type_check (type
), 0);
686 while (type_string
[index
] == 'a' || type_string
[index
] == 'm')
689 if (type_string
[index
] == '(' || type_string
[index
] == '{')
692 else if (type_string
[index
] == ')' || type_string
[index
] == '}')
703 * g_variant_type_peek_string:
704 * @type: a #GVariantType
705 * @returns: the corresponding type string (not nul-terminated)
707 * Returns the type string corresponding to the given @type. The
708 * result is not nul-terminated; in order to determine its length you
709 * must call g_variant_type_get_string_length().
711 * To get a nul-terminated string, see g_variant_type_dup_string().
716 g_variant_type_peek_string (const GVariantType
*type
)
718 g_return_val_if_fail (g_variant_type_check (type
), NULL
);
720 return (const gchar
*) type
;
724 * g_variant_type_dup_string:
725 * @type: a #GVariantType
726 * @returns: the corresponding type string
728 * Returns a newly-allocated copy of the type string corresponding to
729 * @type. The returned string is nul-terminated. It is appropriate to
730 * call g_free() on the return value.
735 g_variant_type_dup_string (const GVariantType
*type
)
737 g_return_val_if_fail (g_variant_type_check (type
), NULL
);
739 return g_strndup (g_variant_type_peek_string (type
),
740 g_variant_type_get_string_length (type
));
744 * g_variant_type_is_definite:
745 * @type: a #GVariantType
746 * @returns: %TRUE if @type is definite
748 * Determines if the given @type is definite (ie: not indefinite).
750 * A type is definite if its type string does not contain any indefinite
751 * type characters ('*', '?', or 'r').
753 * A #GVariant instance may not have an indefinite type, so calling
754 * this function on the result of g_variant_get_type() will always
755 * result in %TRUE being returned. Calling this function on an
756 * indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in
757 * %FALSE being returned.
762 g_variant_type_is_definite (const GVariantType
*type
)
764 const gchar
*type_string
;
768 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
770 type_length
= g_variant_type_get_string_length (type
);
771 type_string
= g_variant_type_peek_string (type
);
773 for (i
= 0; i
< type_length
; i
++)
774 if (type_string
[i
] == '*' ||
775 type_string
[i
] == '?' ||
776 type_string
[i
] == 'r')
783 * g_variant_type_is_container:
784 * @type: a #GVariantType
785 * @returns: %TRUE if @type is a container type
787 * Determines if the given @type is a container type.
789 * Container types are any array, maybe, tuple, or dictionary
790 * entry types plus the variant type.
792 * This function returns %TRUE for any indefinite type for which every
793 * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
799 g_variant_type_is_container (const GVariantType
*type
)
803 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
805 first_char
= g_variant_type_peek_string (type
)[0];
822 * g_variant_type_is_basic:
823 * @type: a #GVariantType
824 * @returns: %TRUE if @type is a basic type
826 * Determines if the given @type is a basic type.
828 * Basic types are booleans, bytes, integers, doubles, strings, object
829 * paths and signatures.
831 * Only a basic type may be used as the key of a dictionary entry.
833 * This function returns %FALSE for all indefinite types except
834 * %G_VARIANT_TYPE_BASIC.
839 g_variant_type_is_basic (const GVariantType
*type
)
843 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
845 first_char
= g_variant_type_peek_string (type
)[0];
870 * g_variant_type_is_maybe:
871 * @type: a #GVariantType
872 * @returns: %TRUE if @type is a maybe type
874 * Determines if the given @type is a maybe type. This is true if the
875 * type string for @type starts with an 'm'.
877 * This function returns %TRUE for any indefinite type for which every
878 * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
884 g_variant_type_is_maybe (const GVariantType
*type
)
886 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
888 return g_variant_type_peek_string (type
)[0] == 'm';
892 * g_variant_type_is_array:
893 * @type: a #GVariantType
894 * @returns: %TRUE if @type is an array type
896 * Determines if the given @type is an array type. This is true if the
897 * type string for @type starts with an 'a'.
899 * This function returns %TRUE for any indefinite type for which every
900 * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
906 g_variant_type_is_array (const GVariantType
*type
)
908 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
910 return g_variant_type_peek_string (type
)[0] == 'a';
914 * g_variant_type_is_tuple:
915 * @type: a #GVariantType
916 * @returns: %TRUE if @type is a tuple type
918 * Determines if the given @type is a tuple type. This is true if the
919 * type string for @type starts with a '(' or if @type is
920 * %G_VARIANT_TYPE_TUPLE.
922 * This function returns %TRUE for any indefinite type for which every
923 * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
929 g_variant_type_is_tuple (const GVariantType
*type
)
933 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
935 type_char
= g_variant_type_peek_string (type
)[0];
936 return type_char
== 'r' || type_char
== '(';
940 * g_variant_type_is_dict_entry:
941 * @type: a #GVariantType
942 * @returns: %TRUE if @type is a dictionary entry type
944 * Determines if the given @type is a dictionary entry type. This is
945 * true if the type string for @type starts with a '{'.
947 * This function returns %TRUE for any indefinite type for which every
948 * definite subtype is a dictionary entry type --
949 * %G_VARIANT_TYPE_DICT_ENTRY, for example.
954 g_variant_type_is_dict_entry (const GVariantType
*type
)
956 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
958 return g_variant_type_peek_string (type
)[0] == '{';
962 * g_variant_type_is_variant:
963 * @type: a #GVariantType
964 * @returns: %TRUE if @type is the variant type
966 * Determines if the given @type is the variant type.
971 g_variant_type_is_variant (const GVariantType
*type
)
973 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
975 return g_variant_type_peek_string (type
)[0] == 'v';
979 * g_variant_type_hash:
980 * @type: a #GVariantType
981 * @returns: the hash value
985 * The argument type of @type is only #gconstpointer to allow use with
986 * #GHashTable without function pointer casting. A valid
987 * #GVariantType must be provided.
992 g_variant_type_hash (gconstpointer type
)
994 const gchar
*type_string
;
999 g_return_val_if_fail (g_variant_type_check (type
), 0);
1001 type_string
= g_variant_type_peek_string (type
);
1002 length
= g_variant_type_get_string_length (type
);
1004 for (i
= 0; i
< length
; i
++)
1005 value
= (value
<< 5) - value
+ type_string
[i
];
1011 * g_variant_type_equal:
1012 * @type1: a #GVariantType
1013 * @type2: a #GVariantType
1014 * @returns: %TRUE if @type1 and @type2 are exactly equal
1016 * Compares @type1 and @type2 for equality.
1018 * Only returns %TRUE if the types are exactly equal. Even if one type
1019 * is an indefinite type and the other is a subtype of it, %FALSE will
1020 * be returned if they are not exactly equal. If you want to check for
1021 * subtypes, use g_variant_type_is_subtype_of().
1023 * The argument types of @type1 and @type2 are only #gconstpointer to
1024 * allow use with #GHashTable without function pointer casting. For
1025 * both arguments, a valid #GVariantType must be provided.
1030 g_variant_type_equal (gconstpointer type1
,
1031 gconstpointer type2
)
1033 const gchar
*string1
, *string2
;
1036 g_return_val_if_fail (g_variant_type_check (type1
), FALSE
);
1037 g_return_val_if_fail (g_variant_type_check (type2
), FALSE
);
1042 size1
= g_variant_type_get_string_length (type1
);
1043 size2
= g_variant_type_get_string_length (type2
);
1048 string1
= g_variant_type_peek_string (type1
);
1049 string2
= g_variant_type_peek_string (type2
);
1051 return memcmp (string1
, string2
, size1
) == 0;
1055 * g_variant_type_is_subtype_of:
1056 * @type: a #GVariantType
1057 * @supertype: a #GVariantType
1058 * @returns: %TRUE if @type is a subtype of @supertype
1060 * Checks if @type is a subtype of @supertype.
1062 * This function returns %TRUE if @type is a subtype of @supertype. All
1063 * types are considered to be subtypes of themselves. Aside from that,
1064 * only indefinite types can have subtypes.
1069 g_variant_type_is_subtype_of (const GVariantType
*type
,
1070 const GVariantType
*supertype
)
1072 const gchar
*supertype_string
;
1073 const gchar
*supertype_end
;
1074 const gchar
*type_string
;
1076 g_return_val_if_fail (g_variant_type_check (type
), FALSE
);
1077 g_return_val_if_fail (g_variant_type_check (supertype
), FALSE
);
1079 supertype_string
= g_variant_type_peek_string (supertype
);
1080 type_string
= g_variant_type_peek_string (type
);
1082 supertype_end
= supertype_string
+
1083 g_variant_type_get_string_length (supertype
);
1085 /* we know that type and supertype are both well-formed, so it's
1086 * safe to treat this merely as a text processing problem.
1088 while (supertype_string
< supertype_end
)
1090 char supertype_char
= *supertype_string
++;
1092 if (supertype_char
== *type_string
)
1095 else if (*type_string
== ')')
1100 const GVariantType
*target_type
= (GVariantType
*) type_string
;
1102 switch (supertype_char
)
1105 if (!g_variant_type_is_tuple (target_type
))
1113 if (!g_variant_type_is_basic (target_type
))
1121 type_string
+= g_variant_type_get_string_length (target_type
);
1129 * g_variant_type_element:
1130 * @type: an array or maybe #GVariantType
1131 * @returns: the element type of @type
1133 * Determines the element type of an array or maybe type.
1135 * This function may only be used with array or maybe types.
1139 const GVariantType
*
1140 g_variant_type_element (const GVariantType
*type
)
1142 const gchar
*type_string
;
1144 g_return_val_if_fail (g_variant_type_check (type
), NULL
);
1146 type_string
= g_variant_type_peek_string (type
);
1148 g_assert (type_string
[0] == 'a' || type_string
[0] == 'm');
1150 return (const GVariantType
*) &type_string
[1];
1154 * g_variant_type_first:
1155 * @type: a tuple or dictionary entry #GVariantType
1156 * @returns: the first item type of @type, or %NULL
1158 * Determines the first item type of a tuple or dictionary entry
1161 * This function may only be used with tuple or dictionary entry types,
1162 * but must not be used with the generic tuple type
1163 * %G_VARIANT_TYPE_TUPLE.
1165 * In the case of a dictionary entry type, this returns the type of
1168 * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
1170 * This call, together with g_variant_type_next() provides an iterator
1171 * interface over tuple and dictionary entry types.
1175 const GVariantType
*
1176 g_variant_type_first (const GVariantType
*type
)
1178 const gchar
*type_string
;
1180 g_return_val_if_fail (g_variant_type_check (type
), NULL
);
1182 type_string
= g_variant_type_peek_string (type
);
1183 g_assert (type_string
[0] == '(' || type_string
[0] == '{');
1185 if (type_string
[1] == ')')
1188 return (const GVariantType
*) &type_string
[1];
1192 * g_variant_type_next:
1193 * @type: a #GVariantType from a previous call
1194 * @returns: the next #GVariantType after @type, or %NULL
1196 * Determines the next item type of a tuple or dictionary entry
1199 * @type must be the result of a previous call to
1200 * g_variant_type_first() or g_variant_type_next().
1202 * If called on the key type of a dictionary entry then this call
1203 * returns the value type. If called on the value type of a dictionary
1204 * entry then this call returns %NULL.
1206 * For tuples, %NULL is returned when @type is the last item in a tuple.
1210 const GVariantType
*
1211 g_variant_type_next (const GVariantType
*type
)
1213 const gchar
*type_string
;
1215 g_return_val_if_fail (g_variant_type_check (type
), NULL
);
1217 type_string
= g_variant_type_peek_string (type
);
1218 type_string
+= g_variant_type_get_string_length (type
);
1220 if (*type_string
== ')' || *type_string
== '}')
1223 return (const GVariantType
*) type_string
;
1227 * g_variant_type_n_items:
1228 * @type: a tuple or dictionary entry #GVariantType
1229 * @returns: the number of items in @type
1231 * Determines the number of items contained in a tuple or
1232 * dictionary entry type.
1234 * This function may only be used with tuple or dictionary entry types,
1235 * but must not be used with the generic tuple type
1236 * %G_VARIANT_TYPE_TUPLE.
1238 * In the case of a dictionary entry type, this function will always
1244 g_variant_type_n_items (const GVariantType
*type
)
1248 g_return_val_if_fail (g_variant_type_check (type
), 0);
1250 for (type
= g_variant_type_first (type
);
1252 type
= g_variant_type_next (type
))
1259 * g_variant_type_key:
1260 * @type: a dictionary entry #GVariantType
1261 * @returns: the key type of the dictionary entry
1263 * Determines the key type of a dictionary entry type.
1265 * This function may only be used with a dictionary entry type. Other
1266 * than the additional restriction, this call is equivalent to
1267 * g_variant_type_first().
1271 const GVariantType
*
1272 g_variant_type_key (const GVariantType
*type
)
1274 const gchar
*type_string
;
1276 g_return_val_if_fail (g_variant_type_check (type
), NULL
);
1278 type_string
= g_variant_type_peek_string (type
);
1279 g_assert (type_string
[0] == '{');
1281 return (const GVariantType
*) &type_string
[1];
1285 * g_variant_type_value:
1286 * @type: a dictionary entry #GVariantType
1287 * @returns: the value type of the dictionary entry
1289 * Determines the value type of a dictionary entry type.
1291 * This function may only be used with a dictionary entry type.
1295 const GVariantType
*
1296 g_variant_type_value (const GVariantType
*type
)
1298 const gchar
*type_string
;
1300 g_return_val_if_fail (g_variant_type_check (type
), NULL
);
1302 type_string
= g_variant_type_peek_string (type
);
1303 g_assert (type_string
[0] == '{');
1305 return g_variant_type_next (g_variant_type_key (type
));
1309 * g_variant_type_new_tuple:
1310 * @items: an array of #GVariantTypes, one for each item
1311 * @length: the length of @items, or -1
1312 * @returns: a new tuple #GVariantType
1314 * Constructs a new tuple type, from @items.
1316 * @length is the number of items in @items, or -1 to indicate that
1317 * @items is %NULL-terminated.
1319 * It is appropriate to call g_variant_type_free() on the return value.
1323 static GVariantType
*
1324 g_variant_type_new_tuple_slow (const GVariantType
* const *items
,
1327 /* the "slow" version is needed in case the static buffer of 1024
1328 * bytes is exceeded when running the normal version. this will
1329 * happen only in truly insane code, so it can be slow.
1334 string
= g_string_new ("(");
1335 for (i
= 0; i
< length
; i
++)
1337 const GVariantType
*type
;
1340 g_return_val_if_fail (g_variant_type_check (items
[i
]), NULL
);
1343 size
= g_variant_type_get_string_length (type
);
1344 g_string_append_len (string
, (const gchar
*) type
, size
);
1346 g_string_append_c (string
, ')');
1348 return (GVariantType
*) g_string_free (string
, FALSE
);
1352 g_variant_type_new_tuple (const GVariantType
* const *items
,
1359 g_return_val_if_fail (length
== 0 || items
!= NULL
, NULL
);
1362 for (length
= 0; items
[length
] != NULL
; length
++);
1365 buffer
[offset
++] = '(';
1367 for (i
= 0; i
< length
; i
++)
1369 const GVariantType
*type
;
1372 g_return_val_if_fail (g_variant_type_check (items
[i
]), NULL
);
1375 size
= g_variant_type_get_string_length (type
);
1377 if (offset
+ size
>= sizeof buffer
) /* leave room for ')' */
1378 return g_variant_type_new_tuple_slow (items
, length
);
1380 memcpy (&buffer
[offset
], type
, size
);
1384 g_assert (offset
< sizeof buffer
);
1385 buffer
[offset
++] = ')';
1387 return (GVariantType
*) g_memdup (buffer
, offset
);
1391 * g_variant_type_new_array:
1392 * @element: a #GVariantType
1393 * @returns: a new array #GVariantType
1395 * Constructs the type corresponding to an array of elements of the
1398 * It is appropriate to call g_variant_type_free() on the return value.
1403 g_variant_type_new_array (const GVariantType
*element
)
1408 g_return_val_if_fail (g_variant_type_check (element
), NULL
);
1410 size
= g_variant_type_get_string_length (element
);
1411 new = g_malloc (size
+ 1);
1414 memcpy (new + 1, element
, size
);
1416 return (GVariantType
*) new;
1420 * g_variant_type_new_maybe:
1421 * @element: a #GVariantType
1422 * @returns: a new maybe #GVariantType
1424 * Constructs the type corresponding to a maybe instance containing
1425 * type @type or Nothing.
1427 * It is appropriate to call g_variant_type_free() on the return value.
1432 g_variant_type_new_maybe (const GVariantType
*element
)
1437 g_return_val_if_fail (g_variant_type_check (element
), NULL
);
1439 size
= g_variant_type_get_string_length (element
);
1440 new = g_malloc (size
+ 1);
1443 memcpy (new + 1, element
, size
);
1445 return (GVariantType
*) new;
1449 * g_variant_type_new_dict_entry:
1450 * @key: a basic #GVariantType
1451 * @value: a #GVariantType
1452 * @returns: a new dictionary entry #GVariantType
1454 * Constructs the type corresponding to a dictionary entry with a key
1455 * of type @key and a value of type @value.
1457 * It is appropriate to call g_variant_type_free() on the return value.
1462 g_variant_type_new_dict_entry (const GVariantType
*key
,
1463 const GVariantType
*value
)
1465 gsize keysize
, valsize
;
1468 g_return_val_if_fail (g_variant_type_check (key
), NULL
);
1469 g_return_val_if_fail (g_variant_type_check (value
), NULL
);
1471 keysize
= g_variant_type_get_string_length (key
);
1472 valsize
= g_variant_type_get_string_length (value
);
1474 new = g_malloc (1 + keysize
+ valsize
+ 1);
1477 memcpy (new + 1, key
, keysize
);
1478 memcpy (new + 1 + keysize
, value
, valsize
);
1479 new[1 + keysize
+ valsize
] = '}';
1481 return (GVariantType
*) new;
1485 const GVariantType
*
1486 g_variant_type_checked_ (const gchar
*type_string
)
1488 g_return_val_if_fail (g_variant_type_string_is_valid (type_string
), NULL
);
1489 return (const GVariantType
*) type_string
;