Use macros for refcount types API
[glib.git] / glib / gvarianttype.c
blob9910dee93cb181d121f4dd874c3dcb4800506721
1 /*
2 * Copyright © 2007, 2008 Ryan Lortie
3 * Copyright © 2009, 2010 Codethink Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Ryan Lortie <desrt@desrt.ca>
21 #include "config.h"
23 #include "gvarianttype.h"
25 #include <glib/gtestutils.h>
26 #include <glib/gstrfuncs.h>
28 #include <string.h>
31 /**
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
39 * some minor lifting of restrictions. The
40 * [D-Bus specification](http://dbus.freedesktop.org/doc/dbus-specification.html),
41 * 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 "m" 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 "*", "?" and "r" to type strings.
58 * Finally, all arbitrary restrictions relating to the complexity of
59 * types are lifted along with the restriction that dictionary entries
60 * may only appear nested inside of arrays.
62 * Just as in D-Bus, GVariant types are described with strings ("type
63 * strings"). Subject to the differences mentioned above, these strings
64 * are of the same form as those found in DBus. Note, however: D-Bus
65 * always works in terms of messages and therefore individual type
66 * strings appear nowhere in its interface. Instead, "signatures"
67 * are a concatenation of the strings of the type of each argument in a
68 * message. GVariant deals with single values directly so GVariant type
69 * strings always describe the type of exactly one value. This means
70 * that a D-Bus signature string is generally not a valid GVariant type
71 * string -- except in the case that it is the signature of a message
72 * containing exactly one argument.
74 * An indefinite type is similar in spirit to what may be called an
75 * abstract type in other type systems. No value can exist that has an
76 * indefinite type as its type, but values can exist that have types
77 * that are subtypes of indefinite types. That is to say,
78 * g_variant_get_type() will never return an indefinite type, but
79 * calling g_variant_is_of_type() with an indefinite type may return
80 * %TRUE. For example, you cannot have a value that represents "an
81 * array of no particular type", but you can have an "array of integers"
82 * which certainly matches the type of "an array of no particular type",
83 * since "array of integers" is a subtype of "array of no particular
84 * type".
86 * This is similar to how instances of abstract classes may not
87 * directly exist in other type systems, but instances of their
88 * non-abstract subtypes may. For example, in GTK, no object that has
89 * the type of #GtkBin can exist (since #GtkBin is an abstract class),
90 * but a #GtkWindow can certainly be instantiated, and you would say
91 * that the #GtkWindow is a #GtkBin (since #GtkWindow is a subclass of
92 * #GtkBin).
94 * ## GVariant Type Strings
96 * A GVariant type string can be any of the following:
98 * - any basic type string (listed below)
100 * - "v", "r" or "*"
102 * - one of the characters 'a' or 'm', followed by another type string
104 * - the character '(', followed by a concatenation of zero or more other
105 * type strings, followed by the character ')'
107 * - the character '{', followed by a basic type string (see below),
108 * followed by another type string, followed by the character '}'
110 * A basic type string describes a basic type (as per
111 * g_variant_type_is_basic()) and is always a single character in length.
112 * The valid basic type strings are "b", "y", "n", "q", "i", "u", "x", "t",
113 * "h", "d", "s", "o", "g" and "?".
115 * The above definition is recursive to arbitrary depth. "aaaaai" and
116 * "(ui(nq((y)))s)" are both valid type strings, as is
117 * "a(aa(ui)(qna{ya(yd)}))".
119 * The meaning of each of the characters is as follows:
120 * - `b`: the type string of %G_VARIANT_TYPE_BOOLEAN; a boolean value.
121 * - `y`: the type string of %G_VARIANT_TYPE_BYTE; a byte.
122 * - `n`: the type string of %G_VARIANT_TYPE_INT16; a signed 16 bit integer.
123 * - `q`: the type string of %G_VARIANT_TYPE_UINT16; an unsigned 16 bit integer.
124 * - `i`: the type string of %G_VARIANT_TYPE_INT32; a signed 32 bit integer.
125 * - `u`: the type string of %G_VARIANT_TYPE_UINT32; an unsigned 32 bit integer.
126 * - `x`: the type string of %G_VARIANT_TYPE_INT64; a signed 64 bit integer.
127 * - `t`: the type string of %G_VARIANT_TYPE_UINT64; an unsigned 64 bit integer.
128 * - `h`: the type string of %G_VARIANT_TYPE_HANDLE; a signed 32 bit value
129 * that, by convention, is used as an index into an array of file
130 * descriptors that are sent alongside a D-Bus message.
131 * - `d`: the type string of %G_VARIANT_TYPE_DOUBLE; a double precision
132 * floating point value.
133 * - `s`: the type string of %G_VARIANT_TYPE_STRING; a string.
134 * - `o`: the type string of %G_VARIANT_TYPE_OBJECT_PATH; a string in the form
135 * of a D-Bus object path.
136 * - `g`: the type string of %G_VARIANT_TYPE_SIGNATURE; a string in the form of
137 * a D-Bus type signature.
138 * - `?`: the type string of %G_VARIANT_TYPE_BASIC; an indefinite type that
139 * is a supertype of any of the basic types.
140 * - `v`: the type string of %G_VARIANT_TYPE_VARIANT; a container type that
141 * contain any other type of value.
142 * - `a`: used as a prefix on another type string to mean an array of that
143 * type; the type string "ai", for example, is the type of an array of
144 * signed 32-bit integers.
145 * - `m`: used as a prefix on another type string to mean a "maybe", or
146 * "nullable", version of that type; the type string "ms", for example,
147 * is the type of a value that maybe contains a string, or maybe contains
148 * nothing.
149 * - `()`: used to enclose zero or more other concatenated type strings to
150 * create a tuple type; the type string "(is)", for example, is the type of
151 * a pair of an integer and a string.
152 * - `r`: the type string of %G_VARIANT_TYPE_TUPLE; an indefinite type that is
153 * a supertype of any tuple type, regardless of the number of items.
154 * - `{}`: used to enclose a basic type string concatenated with another type
155 * string to create a dictionary entry type, which usually appears inside of
156 * an array to form a dictionary; the type string "a{sd}", for example, is
157 * the type of a dictionary that maps strings to double precision floating
158 * point values.
160 * The first type (the basic type) is the key type and the second type is
161 * the value type. The reason that the first type is restricted to being a
162 * basic type is so that it can easily be hashed.
163 * - `*`: the type string of %G_VARIANT_TYPE_ANY; the indefinite type that is
164 * a supertype of all types. Note that, as with all type strings, this
165 * character represents exactly one type. It cannot be used inside of tuples
166 * to mean "any number of items".
168 * Any type string of a container that contains an indefinite type is,
169 * itself, an indefinite type. For example, the type string "a*"
170 * (corresponding to %G_VARIANT_TYPE_ARRAY) is an indefinite type
171 * that is a supertype of every array type. "(*s)" is a supertype
172 * of all tuples that contain exactly two items where the second
173 * item is a string.
175 * "a{?*}" is an indefinite type that is a supertype of all arrays
176 * containing dictionary entries where the key is any basic type and
177 * the value is any type at all. This is, by definition, a dictionary,
178 * so this type string corresponds to %G_VARIANT_TYPE_DICTIONARY. Note
179 * that, due to the restriction that the key of a dictionary entry must
180 * be a basic type, "{**}" is not a valid type string.
184 static gboolean
185 g_variant_type_check (const GVariantType *type)
187 if (type == NULL)
188 return FALSE;
190 #if 0
191 return g_variant_type_string_scan ((const gchar *) type, NULL, NULL);
192 #else
193 return TRUE;
194 #endif
198 * g_variant_type_string_scan:
199 * @string: a pointer to any string
200 * @limit: (nullable): the end of @string, or %NULL
201 * @endptr: (out) (optional): location to store the end pointer, or %NULL
203 * Scan for a single complete and valid GVariant type string in @string.
204 * The memory pointed to by @limit (or bytes beyond it) is never
205 * accessed.
207 * If a valid type string is found, @endptr is updated to point to the
208 * first character past the end of the string that was found and %TRUE
209 * is returned.
211 * If there is no valid type string starting at @string, or if the type
212 * string does not end before @limit then %FALSE is returned.
214 * For the simple case of checking if a string is a valid type string,
215 * see g_variant_type_string_is_valid().
217 * Returns: %TRUE if a valid type string was found
219 * Since: 2.24
221 gboolean
222 g_variant_type_string_scan (const gchar *string,
223 const gchar *limit,
224 const gchar **endptr)
226 g_return_val_if_fail (string != NULL, FALSE);
228 if (string == limit || *string == '\0')
229 return FALSE;
231 switch (*string++)
233 case '(':
234 while (string == limit || *string != ')')
235 if (!g_variant_type_string_scan (string, limit, &string))
236 return FALSE;
238 string++;
239 break;
241 case '{':
242 if (string == limit || *string == '\0' || /* { */
243 !strchr ("bynqihuxtdsog?", *string++) || /* key */
244 !g_variant_type_string_scan (string, limit, &string) || /* value */
245 string == limit || *string++ != '}') /* } */
246 return FALSE;
248 break;
250 case 'm': case 'a':
251 return g_variant_type_string_scan (string, limit, endptr);
253 case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
254 case 'x': case 't': case 'd': case 's': case 'o': case 'g':
255 case 'v': case 'r': case '*': case '?': case 'h':
256 break;
258 default:
259 return FALSE;
262 if (endptr != NULL)
263 *endptr = string;
265 return TRUE;
269 * g_variant_type_string_is_valid:
270 * @type_string: a pointer to any string
272 * Checks if @type_string is a valid GVariant type string. This call is
273 * equivalent to calling g_variant_type_string_scan() and confirming
274 * that the following character is a nul terminator.
276 * Returns: %TRUE if @type_string is exactly one valid type string
278 * Since 2.24
280 gboolean
281 g_variant_type_string_is_valid (const gchar *type_string)
283 const gchar *endptr;
285 g_return_val_if_fail (type_string != NULL, FALSE);
287 if (!g_variant_type_string_scan (type_string, NULL, &endptr))
288 return FALSE;
290 return *endptr == '\0';
294 * g_variant_type_free:
295 * @type: (nullable): a #GVariantType, or %NULL
297 * Frees a #GVariantType that was allocated with
298 * g_variant_type_copy(), g_variant_type_new() or one of the container
299 * type constructor functions.
301 * In the case that @type is %NULL, this function does nothing.
303 * Since 2.24
305 void
306 g_variant_type_free (GVariantType *type)
308 g_return_if_fail (type == NULL || g_variant_type_check (type));
310 g_free (type);
314 * g_variant_type_copy:
315 * @type: a #GVariantType
317 * Makes a copy of a #GVariantType. It is appropriate to call
318 * g_variant_type_free() on the return value. @type may not be %NULL.
320 * Returns: (transfer full): a new #GVariantType
322 * Since 2.24
324 GVariantType *
325 g_variant_type_copy (const GVariantType *type)
327 gsize length;
328 gchar *new;
330 g_return_val_if_fail (g_variant_type_check (type), NULL);
332 length = g_variant_type_get_string_length (type);
333 new = g_malloc (length + 1);
335 memcpy (new, type, length);
336 new[length] = '\0';
338 return (GVariantType *) new;
342 * g_variant_type_new:
343 * @type_string: a valid GVariant type string
345 * Creates a new #GVariantType corresponding to the type string given
346 * by @type_string. It is appropriate to call g_variant_type_free() on
347 * the return value.
349 * It is a programmer error to call this function with an invalid type
350 * string. Use g_variant_type_string_is_valid() if you are unsure.
352 * Returns: (transfer full): a new #GVariantType
354 * Since: 2.24
356 GVariantType *
357 g_variant_type_new (const gchar *type_string)
359 g_return_val_if_fail (type_string != NULL, NULL);
361 return g_variant_type_copy (G_VARIANT_TYPE (type_string));
365 * g_variant_type_get_string_length:
366 * @type: a #GVariantType
368 * Returns the length of the type string corresponding to the given
369 * @type. This function must be used to determine the valid extent of
370 * the memory region returned by g_variant_type_peek_string().
372 * Returns: the length of the corresponding type string
374 * Since 2.24
376 gsize
377 g_variant_type_get_string_length (const GVariantType *type)
379 const gchar *type_string = (const gchar *) type;
380 gint brackets = 0;
381 gsize index = 0;
383 g_return_val_if_fail (g_variant_type_check (type), 0);
387 while (type_string[index] == 'a' || type_string[index] == 'm')
388 index++;
390 if (type_string[index] == '(' || type_string[index] == '{')
391 brackets++;
393 else if (type_string[index] == ')' || type_string[index] == '}')
394 brackets--;
396 index++;
398 while (brackets);
400 return index;
404 This function is not introspectable, it returns something that
405 is not an array and neither a string
408 * g_variant_type_peek_string: (skip)
409 * @type: a #GVariantType
411 * Returns the type string corresponding to the given @type. The
412 * result is not nul-terminated; in order to determine its length you
413 * must call g_variant_type_get_string_length().
415 * To get a nul-terminated string, see g_variant_type_dup_string().
417 * Returns: the corresponding type string (not nul-terminated)
419 * Since 2.24
421 const gchar *
422 g_variant_type_peek_string (const GVariantType *type)
424 g_return_val_if_fail (g_variant_type_check (type), NULL);
426 return (const gchar *) type;
430 * g_variant_type_dup_string:
431 * @type: a #GVariantType
433 * Returns a newly-allocated copy of the type string corresponding to
434 * @type. The returned string is nul-terminated. It is appropriate to
435 * call g_free() on the return value.
437 * Returns: (transfer full): the corresponding type string
439 * Since 2.24
441 gchar *
442 g_variant_type_dup_string (const GVariantType *type)
444 g_return_val_if_fail (g_variant_type_check (type), NULL);
446 return g_strndup (g_variant_type_peek_string (type),
447 g_variant_type_get_string_length (type));
451 * g_variant_type_is_definite:
452 * @type: a #GVariantType
454 * Determines if the given @type is definite (ie: not indefinite).
456 * A type is definite if its type string does not contain any indefinite
457 * type characters ('*', '?', or 'r').
459 * A #GVariant instance may not have an indefinite type, so calling
460 * this function on the result of g_variant_get_type() will always
461 * result in %TRUE being returned. Calling this function on an
462 * indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in
463 * %FALSE being returned.
465 * Returns: %TRUE if @type is definite
467 * Since 2.24
469 gboolean
470 g_variant_type_is_definite (const GVariantType *type)
472 const gchar *type_string;
473 gsize type_length;
474 gsize i;
476 g_return_val_if_fail (g_variant_type_check (type), FALSE);
478 type_length = g_variant_type_get_string_length (type);
479 type_string = g_variant_type_peek_string (type);
481 for (i = 0; i < type_length; i++)
482 if (type_string[i] == '*' ||
483 type_string[i] == '?' ||
484 type_string[i] == 'r')
485 return FALSE;
487 return TRUE;
491 * g_variant_type_is_container:
492 * @type: a #GVariantType
494 * Determines if the given @type is a container type.
496 * Container types are any array, maybe, tuple, or dictionary
497 * entry types plus the variant type.
499 * This function returns %TRUE for any indefinite type for which every
500 * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
501 * example.
503 * Returns: %TRUE if @type is a container type
505 * Since 2.24
507 gboolean
508 g_variant_type_is_container (const GVariantType *type)
510 gchar first_char;
512 g_return_val_if_fail (g_variant_type_check (type), FALSE);
514 first_char = g_variant_type_peek_string (type)[0];
515 switch (first_char)
517 case 'a':
518 case 'm':
519 case 'r':
520 case '(':
521 case '{':
522 case 'v':
523 return TRUE;
525 default:
526 return FALSE;
531 * g_variant_type_is_basic:
532 * @type: a #GVariantType
534 * Determines if the given @type is a basic type.
536 * Basic types are booleans, bytes, integers, doubles, strings, object
537 * paths and signatures.
539 * Only a basic type may be used as the key of a dictionary entry.
541 * This function returns %FALSE for all indefinite types except
542 * %G_VARIANT_TYPE_BASIC.
544 * Returns: %TRUE if @type is a basic type
546 * Since 2.24
548 gboolean
549 g_variant_type_is_basic (const GVariantType *type)
551 gchar first_char;
553 g_return_val_if_fail (g_variant_type_check (type), FALSE);
555 first_char = g_variant_type_peek_string (type)[0];
556 switch (first_char)
558 case 'b':
559 case 'y':
560 case 'n':
561 case 'q':
562 case 'i':
563 case 'h':
564 case 'u':
565 case 't':
566 case 'x':
567 case 'd':
568 case 's':
569 case 'o':
570 case 'g':
571 case '?':
572 return TRUE;
574 default:
575 return FALSE;
580 * g_variant_type_is_maybe:
581 * @type: a #GVariantType
583 * Determines if the given @type is a maybe type. This is true if the
584 * type string for @type starts with an 'm'.
586 * This function returns %TRUE for any indefinite type for which every
587 * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
588 * example.
590 * Returns: %TRUE if @type is a maybe type
592 * Since 2.24
594 gboolean
595 g_variant_type_is_maybe (const GVariantType *type)
597 g_return_val_if_fail (g_variant_type_check (type), FALSE);
599 return g_variant_type_peek_string (type)[0] == 'm';
603 * g_variant_type_is_array:
604 * @type: a #GVariantType
606 * Determines if the given @type is an array type. This is true if the
607 * type string for @type starts with an 'a'.
609 * This function returns %TRUE for any indefinite type for which every
610 * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
611 * example.
613 * Returns: %TRUE if @type is an array type
615 * Since 2.24
617 gboolean
618 g_variant_type_is_array (const GVariantType *type)
620 g_return_val_if_fail (g_variant_type_check (type), FALSE);
622 return g_variant_type_peek_string (type)[0] == 'a';
626 * g_variant_type_is_tuple:
627 * @type: a #GVariantType
629 * Determines if the given @type is a tuple type. This is true if the
630 * type string for @type starts with a '(' or if @type is
631 * %G_VARIANT_TYPE_TUPLE.
633 * This function returns %TRUE for any indefinite type for which every
634 * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
635 * example.
637 * Returns: %TRUE if @type is a tuple type
639 * Since 2.24
641 gboolean
642 g_variant_type_is_tuple (const GVariantType *type)
644 gchar type_char;
646 g_return_val_if_fail (g_variant_type_check (type), FALSE);
648 type_char = g_variant_type_peek_string (type)[0];
649 return type_char == 'r' || type_char == '(';
653 * g_variant_type_is_dict_entry:
654 * @type: a #GVariantType
656 * Determines if the given @type is a dictionary entry type. This is
657 * true if the type string for @type starts with a '{'.
659 * This function returns %TRUE for any indefinite type for which every
660 * definite subtype is a dictionary entry type --
661 * %G_VARIANT_TYPE_DICT_ENTRY, for example.
663 * Returns: %TRUE if @type is a dictionary entry type
665 * Since 2.24
667 gboolean
668 g_variant_type_is_dict_entry (const GVariantType *type)
670 g_return_val_if_fail (g_variant_type_check (type), FALSE);
672 return g_variant_type_peek_string (type)[0] == '{';
676 * g_variant_type_is_variant:
677 * @type: a #GVariantType
679 * Determines if the given @type is the variant type.
681 * Returns: %TRUE if @type is the variant type
683 * Since 2.24
685 gboolean
686 g_variant_type_is_variant (const GVariantType *type)
688 g_return_val_if_fail (g_variant_type_check (type), FALSE);
690 return g_variant_type_peek_string (type)[0] == 'v';
694 * g_variant_type_hash:
695 * @type: (type GVariantType): a #GVariantType
697 * Hashes @type.
699 * The argument type of @type is only #gconstpointer to allow use with
700 * #GHashTable without function pointer casting. A valid
701 * #GVariantType must be provided.
703 * Returns: the hash value
705 * Since 2.24
707 guint
708 g_variant_type_hash (gconstpointer type)
710 const gchar *type_string;
711 guint value = 0;
712 gsize length;
713 gsize i;
715 g_return_val_if_fail (g_variant_type_check (type), 0);
717 type_string = g_variant_type_peek_string (type);
718 length = g_variant_type_get_string_length (type);
720 for (i = 0; i < length; i++)
721 value = (value << 5) - value + type_string[i];
723 return value;
727 * g_variant_type_equal:
728 * @type1: (type GVariantType): a #GVariantType
729 * @type2: (type GVariantType): a #GVariantType
731 * Compares @type1 and @type2 for equality.
733 * Only returns %TRUE if the types are exactly equal. Even if one type
734 * is an indefinite type and the other is a subtype of it, %FALSE will
735 * be returned if they are not exactly equal. If you want to check for
736 * subtypes, use g_variant_type_is_subtype_of().
738 * The argument types of @type1 and @type2 are only #gconstpointer to
739 * allow use with #GHashTable without function pointer casting. For
740 * both arguments, a valid #GVariantType must be provided.
742 * Returns: %TRUE if @type1 and @type2 are exactly equal
744 * Since 2.24
746 gboolean
747 g_variant_type_equal (gconstpointer type1,
748 gconstpointer type2)
750 const gchar *string1, *string2;
751 gsize size1, size2;
753 g_return_val_if_fail (g_variant_type_check (type1), FALSE);
754 g_return_val_if_fail (g_variant_type_check (type2), FALSE);
756 if (type1 == type2)
757 return TRUE;
759 size1 = g_variant_type_get_string_length (type1);
760 size2 = g_variant_type_get_string_length (type2);
762 if (size1 != size2)
763 return FALSE;
765 string1 = g_variant_type_peek_string (type1);
766 string2 = g_variant_type_peek_string (type2);
768 return memcmp (string1, string2, size1) == 0;
772 * g_variant_type_is_subtype_of:
773 * @type: a #GVariantType
774 * @supertype: a #GVariantType
776 * Checks if @type is a subtype of @supertype.
778 * This function returns %TRUE if @type is a subtype of @supertype. All
779 * types are considered to be subtypes of themselves. Aside from that,
780 * only indefinite types can have subtypes.
782 * Returns: %TRUE if @type is a subtype of @supertype
784 * Since 2.24
786 gboolean
787 g_variant_type_is_subtype_of (const GVariantType *type,
788 const GVariantType *supertype)
790 const gchar *supertype_string;
791 const gchar *supertype_end;
792 const gchar *type_string;
794 g_return_val_if_fail (g_variant_type_check (type), FALSE);
795 g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
797 supertype_string = g_variant_type_peek_string (supertype);
798 type_string = g_variant_type_peek_string (type);
800 supertype_end = supertype_string +
801 g_variant_type_get_string_length (supertype);
803 /* we know that type and supertype are both well-formed, so it's
804 * safe to treat this merely as a text processing problem.
806 while (supertype_string < supertype_end)
808 char supertype_char = *supertype_string++;
810 if (supertype_char == *type_string)
811 type_string++;
813 else if (*type_string == ')')
814 return FALSE;
816 else
818 const GVariantType *target_type = (GVariantType *) type_string;
820 switch (supertype_char)
822 case 'r':
823 if (!g_variant_type_is_tuple (target_type))
824 return FALSE;
825 break;
827 case '*':
828 break;
830 case '?':
831 if (!g_variant_type_is_basic (target_type))
832 return FALSE;
833 break;
835 default:
836 return FALSE;
839 type_string += g_variant_type_get_string_length (target_type);
843 return TRUE;
847 * g_variant_type_element:
848 * @type: an array or maybe #GVariantType
850 * Determines the element type of an array or maybe type.
852 * This function may only be used with array or maybe types.
854 * Returns: (transfer none): the element type of @type
856 * Since 2.24
858 const GVariantType *
859 g_variant_type_element (const GVariantType *type)
861 const gchar *type_string;
863 g_return_val_if_fail (g_variant_type_check (type), NULL);
865 type_string = g_variant_type_peek_string (type);
867 g_assert (type_string[0] == 'a' || type_string[0] == 'm');
869 return (const GVariantType *) &type_string[1];
873 * g_variant_type_first:
874 * @type: a tuple or dictionary entry #GVariantType
876 * Determines the first item type of a tuple or dictionary entry
877 * type.
879 * This function may only be used with tuple or dictionary entry types,
880 * but must not be used with the generic tuple type
881 * %G_VARIANT_TYPE_TUPLE.
883 * In the case of a dictionary entry type, this returns the type of
884 * the key.
886 * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
888 * This call, together with g_variant_type_next() provides an iterator
889 * interface over tuple and dictionary entry types.
891 * Returns: (transfer none): the first item type of @type, or %NULL
893 * Since 2.24
895 const GVariantType *
896 g_variant_type_first (const GVariantType *type)
898 const gchar *type_string;
900 g_return_val_if_fail (g_variant_type_check (type), NULL);
902 type_string = g_variant_type_peek_string (type);
903 g_assert (type_string[0] == '(' || type_string[0] == '{');
905 if (type_string[1] == ')')
906 return NULL;
908 return (const GVariantType *) &type_string[1];
912 * g_variant_type_next:
913 * @type: a #GVariantType from a previous call
915 * Determines the next item type of a tuple or dictionary entry
916 * type.
918 * @type must be the result of a previous call to
919 * g_variant_type_first() or g_variant_type_next().
921 * If called on the key type of a dictionary entry then this call
922 * returns the value type. If called on the value type of a dictionary
923 * entry then this call returns %NULL.
925 * For tuples, %NULL is returned when @type is the last item in a tuple.
927 * Returns: (transfer none): the next #GVariantType after @type, or %NULL
929 * Since 2.24
931 const GVariantType *
932 g_variant_type_next (const GVariantType *type)
934 const gchar *type_string;
936 g_return_val_if_fail (g_variant_type_check (type), NULL);
938 type_string = g_variant_type_peek_string (type);
939 type_string += g_variant_type_get_string_length (type);
941 if (*type_string == ')' || *type_string == '}')
942 return NULL;
944 return (const GVariantType *) type_string;
948 * g_variant_type_n_items:
949 * @type: a tuple or dictionary entry #GVariantType
951 * Determines the number of items contained in a tuple or
952 * dictionary entry type.
954 * This function may only be used with tuple or dictionary entry types,
955 * but must not be used with the generic tuple type
956 * %G_VARIANT_TYPE_TUPLE.
958 * In the case of a dictionary entry type, this function will always
959 * return 2.
961 * Returns: the number of items in @type
963 * Since 2.24
965 gsize
966 g_variant_type_n_items (const GVariantType *type)
968 gsize count = 0;
970 g_return_val_if_fail (g_variant_type_check (type), 0);
972 for (type = g_variant_type_first (type);
973 type;
974 type = g_variant_type_next (type))
975 count++;
977 return count;
981 * g_variant_type_key:
982 * @type: a dictionary entry #GVariantType
984 * Determines the key type of a dictionary entry type.
986 * This function may only be used with a dictionary entry type. Other
987 * than the additional restriction, this call is equivalent to
988 * g_variant_type_first().
990 * Returns: (transfer none): the key type of the dictionary entry
992 * Since 2.24
994 const GVariantType *
995 g_variant_type_key (const GVariantType *type)
997 const gchar *type_string;
999 g_return_val_if_fail (g_variant_type_check (type), NULL);
1001 type_string = g_variant_type_peek_string (type);
1002 g_assert (type_string[0] == '{');
1004 return (const GVariantType *) &type_string[1];
1008 * g_variant_type_value:
1009 * @type: a dictionary entry #GVariantType
1011 * Determines the value type of a dictionary entry type.
1013 * This function may only be used with a dictionary entry type.
1015 * Returns: (transfer none): the value type of the dictionary entry
1017 * Since 2.24
1019 const GVariantType *
1020 g_variant_type_value (const GVariantType *type)
1022 const gchar *type_string;
1024 g_return_val_if_fail (g_variant_type_check (type), NULL);
1026 type_string = g_variant_type_peek_string (type);
1027 g_assert (type_string[0] == '{');
1029 return g_variant_type_next (g_variant_type_key (type));
1033 * g_variant_type_new_tuple:
1034 * @items: (array length=length): an array of #GVariantTypes, one for each item
1035 * @length: the length of @items, or -1
1037 * Constructs a new tuple type, from @items.
1039 * @length is the number of items in @items, or -1 to indicate that
1040 * @items is %NULL-terminated.
1042 * It is appropriate to call g_variant_type_free() on the return value.
1044 * Returns: (transfer full): a new tuple #GVariantType
1046 * Since 2.24
1048 static GVariantType *
1049 g_variant_type_new_tuple_slow (const GVariantType * const *items,
1050 gint length)
1052 /* the "slow" version is needed in case the static buffer of 1024
1053 * bytes is exceeded when running the normal version. this will
1054 * happen only in truly insane code, so it can be slow.
1056 GString *string;
1057 gsize i;
1059 string = g_string_new ("(");
1060 for (i = 0; i < length; i++)
1062 const GVariantType *type;
1063 gsize size;
1065 g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1067 type = items[i];
1068 size = g_variant_type_get_string_length (type);
1069 g_string_append_len (string, (const gchar *) type, size);
1071 g_string_append_c (string, ')');
1073 return (GVariantType *) g_string_free (string, FALSE);
1076 GVariantType *
1077 g_variant_type_new_tuple (const GVariantType * const *items,
1078 gint length)
1080 char buffer[1024];
1081 gsize offset;
1082 gsize i;
1084 g_return_val_if_fail (length == 0 || items != NULL, NULL);
1086 if (length < 0)
1087 for (length = 0; items[length] != NULL; length++);
1089 offset = 0;
1090 buffer[offset++] = '(';
1092 for (i = 0; i < length; i++)
1094 const GVariantType *type;
1095 gsize size;
1097 g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1099 type = items[i];
1100 size = g_variant_type_get_string_length (type);
1102 if (offset + size >= sizeof buffer) /* leave room for ')' */
1103 return g_variant_type_new_tuple_slow (items, length);
1105 memcpy (&buffer[offset], type, size);
1106 offset += size;
1109 g_assert (offset < sizeof buffer);
1110 buffer[offset++] = ')';
1112 return (GVariantType *) g_memdup (buffer, offset);
1116 * g_variant_type_new_array: (constructor)
1117 * @element: a #GVariantType
1119 * Constructs the type corresponding to an array of elements of the
1120 * type @type.
1122 * It is appropriate to call g_variant_type_free() on the return value.
1124 * Returns: (transfer full): a new array #GVariantType
1126 * Since 2.24
1128 GVariantType *
1129 g_variant_type_new_array (const GVariantType *element)
1131 gsize size;
1132 gchar *new;
1134 g_return_val_if_fail (g_variant_type_check (element), NULL);
1136 size = g_variant_type_get_string_length (element);
1137 new = g_malloc (size + 1);
1139 new[0] = 'a';
1140 memcpy (new + 1, element, size);
1142 return (GVariantType *) new;
1146 * g_variant_type_new_maybe: (constructor)
1147 * @element: a #GVariantType
1149 * Constructs the type corresponding to a maybe instance containing
1150 * type @type or Nothing.
1152 * It is appropriate to call g_variant_type_free() on the return value.
1154 * Returns: (transfer full): a new maybe #GVariantType
1156 * Since 2.24
1158 GVariantType *
1159 g_variant_type_new_maybe (const GVariantType *element)
1161 gsize size;
1162 gchar *new;
1164 g_return_val_if_fail (g_variant_type_check (element), NULL);
1166 size = g_variant_type_get_string_length (element);
1167 new = g_malloc (size + 1);
1169 new[0] = 'm';
1170 memcpy (new + 1, element, size);
1172 return (GVariantType *) new;
1176 * g_variant_type_new_dict_entry: (constructor)
1177 * @key: a basic #GVariantType
1178 * @value: a #GVariantType
1180 * Constructs the type corresponding to a dictionary entry with a key
1181 * of type @key and a value of type @value.
1183 * It is appropriate to call g_variant_type_free() on the return value.
1185 * Returns: (transfer full): a new dictionary entry #GVariantType
1187 * Since 2.24
1189 GVariantType *
1190 g_variant_type_new_dict_entry (const GVariantType *key,
1191 const GVariantType *value)
1193 gsize keysize, valsize;
1194 gchar *new;
1196 g_return_val_if_fail (g_variant_type_check (key), NULL);
1197 g_return_val_if_fail (g_variant_type_check (value), NULL);
1199 keysize = g_variant_type_get_string_length (key);
1200 valsize = g_variant_type_get_string_length (value);
1202 new = g_malloc (1 + keysize + valsize + 1);
1204 new[0] = '{';
1205 memcpy (new + 1, key, keysize);
1206 memcpy (new + 1 + keysize, value, valsize);
1207 new[1 + keysize + valsize] = '}';
1209 return (GVariantType *) new;
1212 /* private */
1213 const GVariantType *
1214 g_variant_type_checked_ (const gchar *type_string)
1216 g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1217 return (const GVariantType *) type_string;