don't #include <glib.h> from other public headers
[platform/upstream/glib.git] / glib / gvarianttype.c
1 /*
2  * Copyright © 2007, 2008 Ryan Lortie
3  * Copyright © 2009, 2010 Codethink Limited
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Ryan Lortie <desrt@desrt.ca>
21  */
22
23 #include "gvarianttype.h"
24
25 #include <glib/gtestutils.h>
26 #include <glib/gstrfuncs.h>
27
28 #include <string.h>
29
30 #include "galias.h"
31
32 /**
33  * SECTION: gvarianttype
34  * @title: GVariantType
35  * @short_description: introduction to the GVariant type system
36  * @see_also: #GVariantType, #GVariant
37  *
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.
44  *
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.
50  *
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.
59  *
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.
63  *
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.
75  *
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".
86  *
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
93  * #GtkBin).
94  *
95  * A detailed description of GVariant type strings is given here:
96  *
97  * <refsect2 id='gvariant-typestrings'>
98  *  <title>GVariant Type Strings</title>
99  *  <para>
100  *   A GVariant type string can be any of the following:
101  *  </para>
102  *  <itemizedlist>
103  *   <listitem>
104  *    <para>
105  *     any basic type string (listed below)
106  *    </para>
107  *   </listitem>
108  *   <listitem>
109  *    <para>
110  *     "<literal>v</literal>", "<literal>r</literal>" or
111  *     "<literal>*</literal>"
112  *    </para>
113  *   </listitem>
114  *   <listitem>
115  *    <para>
116  *     one of the characters '<literal>a</literal>' or
117  *     '<literal>m</literal>', followed by another type string
118  *    </para>
119  *   </listitem>
120  *   <listitem>
121  *    <para>
122  *     the character '<literal>(</literal>', followed by a concatenation
123  *     of zero or more other type strings, followed by the character
124  *     '<literal>)</literal>'
125  *    </para>
126  *   </listitem>
127  *   <listitem>
128  *    <para>
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>'
132  *    </para>
133  *   </listitem>
134  *  </itemizedlist>
135  *  <para>
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>".
146  *  </para>
147  *  <para>
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>".
152  *  </para>
153  *  <para>
154  *   The meaning of each of the characters is as follows:
155  *  </para>
156  *  <informaltable>
157  *   <tgroup cols='2'>
158  *    <tbody>
159  *     <row>
160  *      <entry>
161  *       <para>
162  *        <emphasis role='strong'>Character</emphasis>
163  *       </para>
164  *      </entry>
165  *      <entry>
166  *       <para>
167  *        <emphasis role='strong'>Meaning</emphasis>
168  *       </para>
169  *      </entry>
170  *     </row>
171  *     <row>
172  *      <entry>
173  *       <para>
174  *        <literal>b</literal>
175  *       </para>
176  *      </entry>
177  *      <entry>
178  *       <para>
179  *        the type string of %G_VARIANT_TYPE_BOOLEAN; a boolean value.
180  *       </para>
181  *      </entry>
182  *     </row>
183  *     <row>
184  *      <entry>
185  *       <para>
186  *        <literal>y</literal>
187  *       </para>
188  *      </entry>
189  *      <entry>
190  *       <para>
191  *        the type string of %G_VARIANT_TYPE_BYTE; a byte.
192  *       </para>
193  *      </entry>
194  *     </row>
195  *     <row>
196  *      <entry>
197  *       <para>
198  *        <literal>n</literal>
199  *       </para>
200  *      </entry>
201  *      <entry>
202  *       <para>
203  *        the type string of %G_VARIANT_TYPE_INT16; a signed 16 bit
204  *        integer.
205  *       </para>
206  *      </entry>
207  *     </row>
208  *     <row>
209  *      <entry>
210  *       <para>
211  *        <literal>q</literal>
212  *       </para>
213  *      </entry>
214  *      <entry>
215  *       <para>
216  *        the type string of %G_VARIANT_TYPE_UINT16; an unsigned 16 bit
217  *        integer.
218  *       </para>
219  *      </entry>
220  *     </row>
221  *     <row>
222  *      <entry>
223  *       <para>
224  *        <literal>i</literal>
225  *       </para>
226  *      </entry>
227  *      <entry>
228  *       <para>
229  *        the type string of %G_VARIANT_TYPE_INT32; a signed 32 bit
230  *        integer.
231  *       </para>
232  *      </entry>
233  *     </row>
234  *     <row>
235  *      <entry>
236  *       <para>
237  *        <literal>u</literal>
238  *       </para>
239  *      </entry>
240  *      <entry>
241  *       <para>
242  *        the type string of %G_VARIANT_TYPE_UINT32; an unsigned 32 bit
243  *        integer.
244  *       </para>
245  *      </entry>
246  *     </row>
247  *     <row>
248  *      <entry>
249  *       <para>
250  *        <literal>x</literal>
251  *       </para>
252  *      </entry>
253  *      <entry>
254  *       <para>
255  *        the type string of %G_VARIANT_TYPE_INT64; a signed 64 bit
256  *        integer.
257  *       </para>
258  *      </entry>
259  *     </row>
260  *     <row>
261  *      <entry>
262  *       <para>
263  *        <literal>t</literal>
264  *       </para>
265  *      </entry>
266  *      <entry>
267  *       <para>
268  *        the type string of %G_VARIANT_TYPE_UINT64; an unsigned 64 bit
269  *        integer.
270  *       </para>
271  *      </entry>
272  *     </row>
273  *     <row>
274  *      <entry>
275  *       <para>
276  *        <literal>h</literal>
277  *       </para>
278  *      </entry>
279  *      <entry>
280  *       <para>
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.
284  *       </para>
285  *      </entry>
286  *     </row>
287  *     <row>
288  *      <entry>
289  *       <para>
290  *        <literal>d</literal>
291  *       </para>
292  *      </entry>
293  *      <entry>
294  *       <para>
295  *        the type string of %G_VARIANT_TYPE_DOUBLE; a double precision
296  *        floating point value.
297  *       </para>
298  *      </entry>
299  *     </row>
300  *     <row>
301  *      <entry>
302  *       <para>
303  *        <literal>s</literal>
304  *       </para>
305  *      </entry>
306  *      <entry>
307  *       <para>
308  *        the type string of %G_VARIANT_TYPE_STRING; a string.
309  *       </para>
310  *      </entry>
311  *     </row>
312  *     <row>
313  *      <entry>
314  *       <para>
315  *        <literal>o</literal>
316  *       </para>
317  *      </entry>
318  *      <entry>
319  *       <para>
320  *        the type string of %G_VARIANT_TYPE_OBJECT_PATH; a string in
321  *        the form of a DBus object path.
322  *       </para>
323  *      </entry>
324  *     </row>
325  *     <row>
326  *      <entry>
327  *       <para>
328  *        <literal>g</literal>
329  *       </para>
330  *      </entry>
331  *      <entry>
332  *       <para>
333  *        the type string of %G_VARIANT_TYPE_STRING; a string in the
334  *        form of a DBus type signature.
335  *       </para>
336  *      </entry>
337  *     </row>
338  *     <row>
339  *      <entry>
340  *       <para>
341  *        <literal>?</literal>
342  *       </para>
343  *      </entry>
344  *      <entry>
345  *       <para>
346  *        the type string of %G_VARIANT_TYPE_BASIC; an indefinite type
347  *        that is a supertype of any of the basic types.
348  *       </para>
349  *      </entry>
350  *     </row>
351  *     <row>
352  *      <entry>
353  *       <para>
354  *        <literal>v</literal>
355  *       </para>
356  *      </entry>
357  *      <entry>
358  *       <para>
359  *        the type string of %G_VARIANT_TYPE_VARIANT; a container type
360  *        that contain any other type of value.
361  *       </para>
362  *      </entry>
363  *     </row>
364  *     <row>
365  *      <entry>
366  *       <para>
367  *        <literal>a</literal>
368  *       </para>
369  *      </entry>
370  *      <entry>
371  *       <para>
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.
375  *       </para>
376  *      </entry>
377  *     </row>
378  *     <row>
379  *      <entry>
380  *       <para>
381  *        <literal>m</literal>
382  *       </para>
383  *      </entry>
384  *      <entry>
385  *       <para>
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.
390  *       </para>
391  *      </entry>
392  *     </row>
393  *     <row>
394  *      <entry>
395  *       <para>
396  *        <literal>()</literal>
397  *       </para>
398  *      </entry>
399  *      <entry>
400  *       <para>
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.
405  *       </para>
406  *      </entry>
407  *     </row>
408  *     <row>
409  *      <entry>
410  *       <para>
411  *        <literal>r</literal>
412  *       </para>
413  *      </entry>
414  *      <entry>
415  *       <para>
416  *        the type string of %G_VARIANT_TYPE_TUPLE; an indefinite type
417  *        that is a supertype of any tuple type, regardless of the
418  *        number of items.
419  *       </para>
420  *      </entry>
421  *     </row>
422  *     <row>
423  *      <entry>
424  *       <para>
425  *        <literal>{}</literal>
426  *       </para>
427  *      </entry>
428  *      <entry>
429  *       <para>
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
435  *        point values.
436  *       </para>
437  *       <para>
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
441  *        hashed.
442  *       </para>
443  *      </entry>
444  *     </row>
445  *     <row>
446  *      <entry>
447  *       <para>
448  *        <literal>*</literal>
449  *       </para>
450  *      </entry>
451  *      <entry>
452  *       <para>
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".
457  *       </para>
458  *      </entry>
459  *     </row>
460  *    </tbody>
461  *   </tgroup>
462  *  </informaltable>
463  *  <para>
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.
470  *  </para>
471  *  <para>
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.
479  *  </para>
480  * </refsect2>
481  */
482
483
484 static gboolean
485 g_variant_type_check (const GVariantType *type)
486 {
487   const gchar *type_string;
488
489   if (type == NULL)
490     return FALSE;
491
492   type_string = (const gchar *) type;
493 #ifndef G_DISABLE_CHECKS
494   return g_variant_type_string_scan (type_string, NULL, NULL);
495 #else
496   return TRUE;
497 #endif
498 }
499
500 /**
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
506  *
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
509  * accessed.
510  *
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
513  * is returned.
514  *
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.
517  *
518  * For the simple case of checking if a string is a valid type string,
519  * see g_variant_type_string_is_valid().
520  *
521  * Since: 2.24
522  **/
523 gboolean
524 g_variant_type_string_scan (const gchar  *string,
525                             const gchar  *limit,
526                             const gchar **endptr)
527 {
528   g_return_val_if_fail (string != NULL, FALSE);
529
530   if (string == limit || *string == '\0')
531     return FALSE;
532
533   switch (*string++)
534     {
535     case '(':
536       while (string == limit || *string != ')')
537         if (!g_variant_type_string_scan (string, limit, &string))
538           return FALSE;
539
540       string++;
541       break;
542
543     case '{':
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++ != '}')                     /* } */
548         return FALSE;
549
550       break;
551
552     case 'm': case 'a':
553       return g_variant_type_string_scan (string, limit, endptr);
554
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':
558       break;
559
560     default:
561       return FALSE;
562     }
563
564   if (endptr != NULL)
565     *endptr = string;
566
567   return TRUE;
568 }
569
570 /**
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
574  *
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.
578  *
579  * Since 2.24
580  **/
581 gboolean
582 g_variant_type_string_is_valid (const gchar *type_string)
583 {
584   gchar *endptr;
585
586   g_return_val_if_fail (type_string != NULL, FALSE);
587
588   if (!g_variant_type_string_scan (type_string, NULL, &endptr))
589     return FALSE;
590
591   return *endptr == '\0';
592 }
593
594 /**
595  * g_variant_type_free:
596  * @type: a #GVariantType, or %NULL
597  *
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.
601  *
602  * In the case that @type is %NULL, this function does nothing.
603  *
604  * Since 2.24
605  **/
606 void
607 g_variant_type_free (GVariantType *type)
608 {
609   g_return_if_fail (type == NULL || g_variant_type_check (type));
610
611   g_free (type);
612 }
613
614 /**
615  * g_variant_type_copy:
616  * @type: a #GVariantType
617  * @returns: a new #GVariantType
618  *
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.
621  *
622  * Since 2.24
623  **/
624 GVariantType *
625 g_variant_type_copy (const GVariantType *type)
626 {
627   gsize length;
628   gchar *new;
629
630   g_return_val_if_fail (g_variant_type_check (type), NULL);
631
632   length = g_variant_type_get_string_length (type);
633   new = g_malloc (length + 1);
634
635   memcpy (new, type, length);
636   new[length] = '\0';
637
638   return (GVariantType *) new;
639 }
640
641 /**
642  * g_variant_type_new:
643  * @type_string: a valid GVariant type string
644  * @returns: a new #GVariantType
645  *
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
648  * the return value.
649  *
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.
652  *
653  * Since: 2.24
654  */
655 GVariantType *
656 g_variant_type_new (const gchar *type_string)
657 {
658   g_return_val_if_fail (type_string != NULL, NULL);
659
660   return g_variant_type_copy (G_VARIANT_TYPE (type_string));
661 }
662
663 /**
664  * g_variant_type_get_string_length:
665  * @type: a #GVariantType
666  * @returns: the length of the corresponding type string
667  *
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().
671  *
672  * Since 2.24
673  **/
674 gsize
675 g_variant_type_get_string_length (const GVariantType *type)
676 {
677   const gchar *type_string = (const gchar *) type;
678   gint brackets = 0;
679   gsize index = 0;
680
681   g_return_val_if_fail (g_variant_type_check (type), 0);
682
683   do
684     {
685       while (type_string[index] == 'a' || type_string[index] == 'm')
686         index++;
687
688       if (type_string[index] == '(' || type_string[index] == '{')
689         brackets++;
690
691       else if (type_string[index] == ')' || type_string[index] == '}')
692         brackets--;
693
694       index++;
695     }
696   while (brackets);
697
698   return index;
699 }
700
701 /**
702  * g_variant_type_peek_string:
703  * @type: a #GVariantType
704  * @returns: the corresponding type string (not nul-terminated)
705  *
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().
709  *
710  * To get a nul-terminated string, see g_variant_type_dup_string().
711  *
712  * Since 2.24
713  **/
714 const gchar *
715 g_variant_type_peek_string (const GVariantType *type)
716 {
717   g_return_val_if_fail (g_variant_type_check (type), NULL);
718
719   return (const gchar *) type;
720 }
721
722 /**
723  * g_variant_type_dup_string:
724  * @type: a #GVariantType
725  * @returns: the corresponding type string
726  *
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.
730  *
731  * Since 2.24
732  **/
733 gchar *
734 g_variant_type_dup_string (const GVariantType *type)
735 {
736   g_return_val_if_fail (g_variant_type_check (type), NULL);
737
738   return g_strndup (g_variant_type_peek_string (type),
739                     g_variant_type_get_string_length (type));
740 }
741
742 /**
743  * g_variant_type_is_definite:
744  * @type: a #GVariantType
745  * @returns: %TRUE if @type is definite
746  *
747  * Determines if the given @type is definite (ie: not indefinite).
748  *
749  * A type is definite if its type string does not contain any indefinite
750  * type characters ('*', '?', or 'r').
751  *
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.
757  *
758  * Since 2.24
759  **/
760 gboolean
761 g_variant_type_is_definite (const GVariantType *type)
762 {
763   const gchar *type_string;
764   gsize type_length;
765   gsize i;
766
767   g_return_val_if_fail (g_variant_type_check (type), FALSE);
768
769   type_length = g_variant_type_get_string_length (type);
770   type_string = g_variant_type_peek_string (type);
771
772   for (i = 0; i < type_length; i++)
773     if (type_string[i] == '*' ||
774         type_string[i] == '?' ||
775         type_string[i] == 'r')
776       return FALSE;
777
778   return TRUE;
779 }
780
781 /**
782  * g_variant_type_is_container:
783  * @type: a #GVariantType
784  * @returns: %TRUE if @type is a container type
785  *
786  * Determines if the given @type is a container type.
787  *
788  * Container types are any array, maybe, tuple, or dictionary
789  * entry types plus the variant type.
790  *
791  * This function returns %TRUE for any indefinite type for which every
792  * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
793  * example.
794  *
795  * Since 2.24
796  **/
797 gboolean
798 g_variant_type_is_container (const GVariantType *type)
799 {
800   gchar first_char;
801
802   g_return_val_if_fail (g_variant_type_check (type), FALSE);
803
804   first_char = g_variant_type_peek_string (type)[0];
805   switch (first_char)
806   {
807     case 'a':
808     case 'm':
809     case 'r':
810     case '(':
811     case '{':
812     case 'v':
813       return TRUE;
814
815     default:
816       return FALSE;
817   }
818 }
819
820 /**
821  * g_variant_type_is_basic:
822  * @type: a #GVariantType
823  * @returns: %TRUE if @type is a basic type
824  *
825  * Determines if the given @type is a basic type.
826  *
827  * Basic types are booleans, bytes, integers, doubles, strings, object
828  * paths and signatures.
829  *
830  * Only a basic type may be used as the key of a dictionary entry.
831  *
832  * This function returns %FALSE for all indefinite types except
833  * %G_VARIANT_TYPE_BASIC.
834  *
835  * Since 2.24
836  **/
837 gboolean
838 g_variant_type_is_basic (const GVariantType *type)
839 {
840   gchar first_char;
841
842   g_return_val_if_fail (g_variant_type_check (type), FALSE);
843
844   first_char = g_variant_type_peek_string (type)[0];
845   switch (first_char)
846   {
847     case 'b':
848     case 'y':
849     case 'n':
850     case 'q':
851     case 'i':
852     case 'h':
853     case 'u':
854     case 't':
855     case 'x':
856     case 'd':
857     case 's':
858     case 'o':
859     case 'g':
860     case '?':
861       return TRUE;
862
863     default:
864       return FALSE;
865   }
866 }
867
868 /**
869  * g_variant_type_is_maybe:
870  * @type: a #GVariantType
871  * @returns: %TRUE if @type is a maybe type
872  *
873  * Determines if the given @type is a maybe type.  This is true if the
874  * type string for @type starts with an 'm'.
875  *
876  * This function returns %TRUE for any indefinite type for which every
877  * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
878  * example.
879  *
880  * Since 2.24
881  **/
882 gboolean
883 g_variant_type_is_maybe (const GVariantType *type)
884 {
885   g_return_val_if_fail (g_variant_type_check (type), FALSE);
886
887   return g_variant_type_peek_string (type)[0] == 'm';
888 }
889
890 /**
891  * g_variant_type_is_array:
892  * @type: a #GVariantType
893  * @returns: %TRUE if @type is an array type
894  *
895  * Determines if the given @type is an array type.  This is true if the
896  * type string for @type starts with an 'a'.
897  *
898  * This function returns %TRUE for any indefinite type for which every
899  * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
900  * example.
901  *
902  * Since 2.24
903  **/
904 gboolean
905 g_variant_type_is_array (const GVariantType *type)
906 {
907   g_return_val_if_fail (g_variant_type_check (type), FALSE);
908
909   return g_variant_type_peek_string (type)[0] == 'a';
910 }
911
912 /**
913  * g_variant_type_is_tuple:
914  * @type: a #GVariantType
915  * @returns: %TRUE if @type is a tuple type
916  *
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.
920  *
921  * This function returns %TRUE for any indefinite type for which every
922  * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
923  * example.
924  *
925  * Since 2.24
926  **/
927 gboolean
928 g_variant_type_is_tuple (const GVariantType *type)
929 {
930   gchar type_char;
931
932   g_return_val_if_fail (g_variant_type_check (type), FALSE);
933
934   type_char = g_variant_type_peek_string (type)[0];
935   return type_char == 'r' || type_char == '(';
936 }
937
938 /**
939  * g_variant_type_is_dict_entry:
940  * @type: a #GVariantType
941  * @returns: %TRUE if @type is a dictionary entry type
942  *
943  * Determines if the given @type is a dictionary entry type.  This is
944  * true if the type string for @type starts with a '{'.
945  *
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.
949  *
950  * Since 2.24
951  **/
952 gboolean
953 g_variant_type_is_dict_entry (const GVariantType *type)
954 {
955   g_return_val_if_fail (g_variant_type_check (type), FALSE);
956
957   return g_variant_type_peek_string (type)[0] == '{';
958 }
959
960 /**
961  * g_variant_type_hash:
962  * @type: a #GVariantType
963  * @returns: the hash value
964  *
965  * Hashes @type.
966  *
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.
970  *
971  * Since 2.24
972  **/
973 guint
974 g_variant_type_hash (gconstpointer type)
975 {
976   const gchar *type_string;
977   guint value = 0;
978   gsize length;
979   gsize i;
980
981   g_return_val_if_fail (g_variant_type_check (type), 0);
982
983   type_string = g_variant_type_peek_string (type);
984   length = g_variant_type_get_string_length (type);
985
986   for (i = 0; i < length; i++)
987     value = (value << 5) - value + type_string[i];
988
989   return value;
990 }
991
992 /**
993  * g_variant_type_equal:
994  * @type1: a #GVariantType
995  * @type2: a #GVariantType
996  * @returns: %TRUE if @type1 and @type2 are exactly equal
997  *
998  * Compares @type1 and @type2 for equality.
999  *
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().
1004  *
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.
1008  *
1009  * Since 2.24
1010  **/
1011 gboolean
1012 g_variant_type_equal (gconstpointer type1,
1013                       gconstpointer type2)
1014 {
1015   const gchar *string1, *string2;
1016   gsize size1, size2;
1017
1018   g_return_val_if_fail (g_variant_type_check (type1), FALSE);
1019   g_return_val_if_fail (g_variant_type_check (type2), FALSE);
1020
1021   if (type1 == type2)
1022     return TRUE;
1023
1024   size1 = g_variant_type_get_string_length (type1);
1025   size2 = g_variant_type_get_string_length (type2);
1026
1027   if (size1 != size2)
1028     return FALSE;
1029
1030   string1 = g_variant_type_peek_string (type1);
1031   string2 = g_variant_type_peek_string (type2);
1032
1033   return memcmp (string1, string2, size1) == 0;
1034 }
1035
1036 /**
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
1041  *
1042  * Checks if @type is a subtype of @supertype.
1043  *
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.
1047  *
1048  * Since 2.24
1049  **/
1050 gboolean
1051 g_variant_type_is_subtype_of (const GVariantType *type,
1052                               const GVariantType *supertype)
1053 {
1054   const gchar *supertype_string;
1055   const gchar *supertype_end;
1056   const gchar *type_string;
1057
1058   g_return_val_if_fail (g_variant_type_check (type), FALSE);
1059   g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
1060
1061   supertype_string = g_variant_type_peek_string (supertype);
1062   type_string = g_variant_type_peek_string (type);
1063
1064   supertype_end = supertype_string +
1065                   g_variant_type_get_string_length (supertype);
1066
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.
1069    */
1070   while (supertype_string < supertype_end)
1071     {
1072       char supertype_char = *supertype_string++;
1073
1074       if (supertype_char == *type_string)
1075         type_string++;
1076
1077       else if (*type_string == ')')
1078         return FALSE;
1079
1080       else
1081         {
1082           const GVariantType *target_type = (GVariantType *) type_string;
1083
1084           switch (supertype_char)
1085             {
1086             case 'r':
1087               if (!g_variant_type_is_tuple (target_type))
1088                 return FALSE;
1089               break;
1090
1091             case '*':
1092               break;
1093
1094             case '?':
1095               if (!g_variant_type_is_basic (target_type))
1096                 return FALSE;
1097               break;
1098
1099             default:
1100               return FALSE;
1101             }
1102
1103           type_string += g_variant_type_get_string_length (target_type);
1104         }
1105     }
1106
1107   return TRUE;
1108 }
1109
1110 /**
1111  * g_variant_type_element:
1112  * @type: an array or maybe #GVariantType
1113  * @returns: the element type of @type
1114  *
1115  * Determines the element type of an array or maybe type.
1116  *
1117  * This function may only be used with array or maybe types.
1118  *
1119  * Since 2.24
1120  **/
1121 const GVariantType *
1122 g_variant_type_element (const GVariantType *type)
1123 {
1124   const gchar *type_string;
1125
1126   g_return_val_if_fail (g_variant_type_check (type), NULL);
1127
1128   type_string = g_variant_type_peek_string (type);
1129
1130   g_assert (type_string[0] == 'a' || type_string[0] == 'm');
1131
1132   return (const GVariantType *) &type_string[1];
1133 }
1134
1135 /**
1136  * g_variant_type_first:
1137  * @type: a tuple or dictionary entry #GVariantType
1138  * @returns: the first item type of @type, or %NULL
1139  *
1140  * Determines the first item type of a tuple or dictionary entry
1141  * type.
1142  *
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.
1146  *
1147  * In the case of a dictionary entry type, this returns the type of
1148  * the key.
1149  *
1150  * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
1151  *
1152  * This call, together with g_variant_type_next() provides an iterator
1153  * interface over tuple and dictionary entry types.
1154  *
1155  * Since 2.24
1156  **/
1157 const GVariantType *
1158 g_variant_type_first (const GVariantType *type)
1159 {
1160   const gchar *type_string;
1161
1162   g_return_val_if_fail (g_variant_type_check (type), NULL);
1163
1164   type_string = g_variant_type_peek_string (type);
1165   g_assert (type_string[0] == '(' || type_string[0] == '{');
1166
1167   if (type_string[1] == ')')
1168     return NULL;
1169
1170   return (const GVariantType *) &type_string[1];
1171 }
1172
1173 /**
1174  * g_variant_type_next:
1175  * @type: a #GVariantType from a previous call
1176  * @returns: the next #GVariantType after @type, or %NULL
1177  *
1178  * Determines the next item type of a tuple or dictionary entry
1179  * type.
1180  *
1181  * @type must be the result of a previous call to
1182  * g_variant_type_first() or g_variant_type_next().
1183  *
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.
1187  *
1188  * For tuples, %NULL is returned when @type is the last item in a tuple.
1189  *
1190  * Since 2.24
1191  **/
1192 const GVariantType *
1193 g_variant_type_next (const GVariantType *type)
1194 {
1195   const gchar *type_string;
1196
1197   g_return_val_if_fail (g_variant_type_check (type), NULL);
1198
1199   type_string = g_variant_type_peek_string (type);
1200   type_string += g_variant_type_get_string_length (type);
1201
1202   if (*type_string == ')' || *type_string == '}')
1203     return NULL;
1204
1205   return (const GVariantType *) type_string;
1206 }
1207
1208 /**
1209  * g_variant_type_n_items:
1210  * @type: a tuple or dictionary entry #GVariantType
1211  * @returns: the number of items in @type
1212  *
1213  * Determines the number of items contained in a tuple or
1214  * dictionary entry type.
1215  *
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.
1219  *
1220  * In the case of a dictionary entry type, this function will always
1221  * return 2.
1222  *
1223  * Since 2.24
1224  **/
1225 gsize
1226 g_variant_type_n_items (const GVariantType *type)
1227 {
1228   gsize count = 0;
1229
1230   g_return_val_if_fail (g_variant_type_check (type), 0);
1231
1232   for (type = g_variant_type_first (type);
1233        type;
1234        type = g_variant_type_next (type))
1235     count++;
1236
1237   return count;
1238 }
1239
1240 /**
1241  * g_variant_type_key:
1242  * @type: a dictionary entry #GVariantType
1243  * @returns: the key type of the dictionary entry
1244  *
1245  * Determines the key type of a dictionary entry type.
1246  *
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().
1250  *
1251  * Since 2.24
1252  **/
1253 const GVariantType *
1254 g_variant_type_key (const GVariantType *type)
1255 {
1256   const gchar *type_string;
1257
1258   g_return_val_if_fail (g_variant_type_check (type), NULL);
1259
1260   type_string = g_variant_type_peek_string (type);
1261   g_assert (type_string[0] == '{');
1262
1263   return (const GVariantType *) &type_string[1];
1264 }
1265
1266 /**
1267  * g_variant_type_value:
1268  * @type: a dictionary entry #GVariantType
1269  * @returns: the value type of the dictionary entry
1270  *
1271  * Determines the value type of a dictionary entry type.
1272  *
1273  * This function may only be used with a dictionary entry type.
1274  *
1275  * Since 2.24
1276  **/
1277 const GVariantType *
1278 g_variant_type_value (const GVariantType *type)
1279 {
1280   const gchar *type_string;
1281
1282   g_return_val_if_fail (g_variant_type_check (type), NULL);
1283
1284   type_string = g_variant_type_peek_string (type);
1285   g_assert (type_string[0] == '{');
1286
1287   return g_variant_type_next (g_variant_type_key (type));
1288 }
1289
1290 /**
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
1295  *
1296  * Constructs a new tuple type, from @items.
1297  *
1298  * @length is the number of items in @items, or -1 to indicate that
1299  * @items is %NULL-terminated.
1300  *
1301  * It is appropriate to call g_variant_type_free() on the return value.
1302  *
1303  * Since 2.24
1304  **/
1305 static GVariantType *
1306 g_variant_type_new_tuple_slow (const GVariantType * const *items,
1307                                gint                        length)
1308 {
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.
1312    */
1313   GString *string;
1314   gsize i;
1315
1316   string = g_string_new ("(");
1317   for (i = 0; i < length; i++)
1318     {
1319       const GVariantType *type;
1320       gsize size;
1321
1322       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1323
1324       type = items[i];
1325       size = g_variant_type_get_string_length (type);
1326       g_string_append_len (string, (const gchar *) type, size);
1327     }
1328   g_string_append_c (string, ')');
1329
1330   return (GVariantType *) g_string_free (string, FALSE);
1331 }
1332
1333 GVariantType *
1334 g_variant_type_new_tuple (const GVariantType * const *items,
1335                           gint                        length)
1336 {
1337   char buffer[1024];
1338   gsize offset;
1339   gsize i;
1340
1341   g_return_val_if_fail (length == 0 || items != NULL, NULL);
1342
1343   if (length < 0)
1344     for (length = 0; items[length] != NULL; length++);
1345
1346   offset = 0;
1347   buffer[offset++] = '(';
1348
1349   for (i = 0; i < length; i++)
1350     {
1351       const GVariantType *type;
1352       gsize size;
1353
1354       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1355
1356       type = items[i];
1357       size = g_variant_type_get_string_length (type);
1358
1359       if (offset + size >= sizeof buffer) /* leave room for ')' */
1360         return g_variant_type_new_tuple_slow (items, length);
1361
1362       memcpy (&buffer[offset], type, size);
1363       offset += size;
1364     }
1365
1366   g_assert (offset < sizeof buffer);
1367   buffer[offset++] = ')';
1368
1369   return (GVariantType *) g_memdup (buffer, offset);
1370 }
1371
1372 /**
1373  * g_variant_type_new_array:
1374  * @element: a #GVariantType
1375  * @returns: a new array #GVariantType
1376  *
1377  * Constructs the type corresponding to an array of elements of the
1378  * type @type.
1379  *
1380  * It is appropriate to call g_variant_type_free() on the return value.
1381  *
1382  * Since 2.24
1383  **/
1384 GVariantType *
1385 g_variant_type_new_array (const GVariantType *element)
1386 {
1387   gsize size;
1388   gchar *new;
1389
1390   g_return_val_if_fail (g_variant_type_check (element), NULL);
1391
1392   size = g_variant_type_get_string_length (element);
1393   new = g_malloc (size + 1);
1394
1395   new[0] = 'a';
1396   memcpy (new + 1, element, size);
1397
1398   return (GVariantType *) new;
1399 }
1400
1401 /**
1402  * g_variant_type_new_maybe:
1403  * @element: a #GVariantType
1404  * @returns: a new maybe #GVariantType
1405  *
1406  * Constructs the type corresponding to a maybe instance containing
1407  * type @type or Nothing.
1408  *
1409  * It is appropriate to call g_variant_type_free() on the return value.
1410  *
1411  * Since 2.24
1412  **/
1413 GVariantType *
1414 g_variant_type_new_maybe (const GVariantType *element)
1415 {
1416   gsize size;
1417   gchar *new;
1418
1419   g_return_val_if_fail (g_variant_type_check (element), NULL);
1420
1421   size = g_variant_type_get_string_length (element);
1422   new = g_malloc (size + 1);
1423
1424   new[0] = 'm';
1425   memcpy (new + 1, element, size);
1426
1427   return (GVariantType *) new;
1428 }
1429
1430 /**
1431  * g_variant_type_new_dict_entry:
1432  * @key: a basic #GVariantType
1433  * @value: a #GVariantType
1434  * @returns: a new dictionary entry #GVariantType
1435  *
1436  * Constructs the type corresponding to a dictionary entry with a key
1437  * of type @key and a value of type @value.
1438  *
1439  * It is appropriate to call g_variant_type_free() on the return value.
1440  *
1441  * Since 2.24
1442  **/
1443 GVariantType *
1444 g_variant_type_new_dict_entry (const GVariantType *key,
1445                                const GVariantType *value)
1446 {
1447   gsize keysize, valsize;
1448   gchar *new;
1449
1450   g_return_val_if_fail (g_variant_type_check (key), NULL);
1451   g_return_val_if_fail (g_variant_type_check (value), NULL);
1452
1453   keysize = g_variant_type_get_string_length (key);
1454   valsize = g_variant_type_get_string_length (value);
1455
1456   new = g_malloc (1 + keysize + valsize + 1);
1457
1458   new[0] = '{';
1459   memcpy (new + 1, key, keysize);
1460   memcpy (new + 1 + keysize, value, valsize);
1461   new[1 + keysize + valsize] = '}';
1462
1463   return (GVariantType *) new;
1464 }
1465
1466 /* private */
1467 const GVariantType *
1468 g_variant_type_checked_ (const gchar *type_string)
1469 {
1470   g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1471   return (const GVariantType *) type_string;
1472 }
1473
1474 #define __G_VARIANT_TYPE_C__
1475 #include "galiasdef.c"