Merge remote-tracking branch 'gvdb/master'
[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 "config.h"
24
25 #include "gvarianttype.h"
26
27 #include <glib/gtestutils.h>
28 #include <glib/gstrfuncs.h>
29
30 #include <string.h>
31
32
33 /**
34  * SECTION:gvarianttype
35  * @title: GVariantType
36  * @short_description: introduction to the GVariant type system
37  * @see_also: #GVariantType, #GVariant
38  *
39  * This section introduces the GVariant type system.  It is based, in
40  * large part, on the D-Bus 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.
45  *
46  * The first major change with respect to the D-Bus 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.
51  *
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 D-Bus.  For example, it is possible to speak
55  * of "an array of any type" in GVariant, where the D-Bus 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.
60  *
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.
64  *
65  * Just as in D-Bus, 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: D-Bus
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 D-Bus 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.
76  *
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_of_type() with an indefinite type may return
83  * %TRUE.  For example, you cannot have a value that represents "an
84  * array of no particular type", but you can have an "array of integers"
85  * which certainly matches the type of "an array of no particular type",
86  * since "array of integers" is a subtype of "array of no particular
87  * type".
88  *
89  * This is similar to how instances of abstract classes may not
90  * directly exist in other type systems, but instances of their
91  * non-abstract subtypes may.  For example, in GTK, no object that has
92  * the type of #GtkBin can exist (since #GtkBin is an abstract class),
93  * but a #GtkWindow can certainly be instantiated, and you would say
94  * that the #GtkWindow is a #GtkBin (since #GtkWindow is a subclass of
95  * #GtkBin).
96  *
97  * A detailed description of GVariant type strings is given here:
98  *
99  * <refsect2 id='gvariant-typestrings'>
100  *  <title>GVariant Type Strings</title>
101  *  <para>
102  *   A GVariant type string can be any of the following:
103  *  </para>
104  *  <itemizedlist>
105  *   <listitem>
106  *    <para>
107  *     any basic type string (listed below)
108  *    </para>
109  *   </listitem>
110  *   <listitem>
111  *    <para>
112  *     "<literal>v</literal>", "<literal>r</literal>" or
113  *     "<literal>*</literal>"
114  *    </para>
115  *   </listitem>
116  *   <listitem>
117  *    <para>
118  *     one of the characters '<literal>a</literal>' or
119  *     '<literal>m</literal>', followed by another type string
120  *    </para>
121  *   </listitem>
122  *   <listitem>
123  *    <para>
124  *     the character '<literal>(</literal>', followed by a concatenation
125  *     of zero or more other type strings, followed by the character
126  *     '<literal>)</literal>'
127  *    </para>
128  *   </listitem>
129  *   <listitem>
130  *    <para>
131  *     the character '<literal>{</literal>', followed by a basic type
132  *     string (see below), followed by another type string, followed by
133  *     the character '<literal>}</literal>'
134  *    </para>
135  *   </listitem>
136  *  </itemizedlist>
137  *  <para>
138  *   A basic type string describes a basic type (as per
139  *   g_variant_type_is_basic()) and is always a single
140  *   character in length.  The valid basic type strings are
141  *   "<literal>b</literal>", "<literal>y</literal>",
142  *   "<literal>n</literal>", "<literal>q</literal>",
143  *   "<literal>i</literal>", "<literal>u</literal>",
144  *   "<literal>x</literal>", "<literal>t</literal>",
145  *   "<literal>h</literal>", "<literal>d</literal>",
146  *   "<literal>s</literal>", "<literal>o</literal>",
147  *   "<literal>g</literal>" and "<literal>?</literal>".
148  *  </para>
149  *  <para>
150  *   The above definition is recursive to arbitrary depth.
151  *   "<literal>aaaaai</literal>" and "<literal>(ui(nq((y)))s)</literal>"
152  *   are both valid type strings, as is
153  *   "<literal>a(aa(ui)(qna{ya(yd)}))</literal>".
154  *  </para>
155  *  <para>
156  *   The meaning of each of the characters is as follows:
157  *  </para>
158  *  <informaltable>
159  *   <tgroup cols='2'>
160  *    <tbody>
161  *     <row>
162  *      <entry>
163  *       <para>
164  *        <emphasis role='strong'>Character</emphasis>
165  *       </para>
166  *      </entry>
167  *      <entry>
168  *       <para>
169  *        <emphasis role='strong'>Meaning</emphasis>
170  *       </para>
171  *      </entry>
172  *     </row>
173  *     <row>
174  *      <entry>
175  *       <para>
176  *        <literal>b</literal>
177  *       </para>
178  *      </entry>
179  *      <entry>
180  *       <para>
181  *        the type string of %G_VARIANT_TYPE_BOOLEAN; a boolean value.
182  *       </para>
183  *      </entry>
184  *     </row>
185  *     <row>
186  *      <entry>
187  *       <para>
188  *        <literal>y</literal>
189  *       </para>
190  *      </entry>
191  *      <entry>
192  *       <para>
193  *        the type string of %G_VARIANT_TYPE_BYTE; a byte.
194  *       </para>
195  *      </entry>
196  *     </row>
197  *     <row>
198  *      <entry>
199  *       <para>
200  *        <literal>n</literal>
201  *       </para>
202  *      </entry>
203  *      <entry>
204  *       <para>
205  *        the type string of %G_VARIANT_TYPE_INT16; a signed 16 bit
206  *        integer.
207  *       </para>
208  *      </entry>
209  *     </row>
210  *     <row>
211  *      <entry>
212  *       <para>
213  *        <literal>q</literal>
214  *       </para>
215  *      </entry>
216  *      <entry>
217  *       <para>
218  *        the type string of %G_VARIANT_TYPE_UINT16; an unsigned 16 bit
219  *        integer.
220  *       </para>
221  *      </entry>
222  *     </row>
223  *     <row>
224  *      <entry>
225  *       <para>
226  *        <literal>i</literal>
227  *       </para>
228  *      </entry>
229  *      <entry>
230  *       <para>
231  *        the type string of %G_VARIANT_TYPE_INT32; a signed 32 bit
232  *        integer.
233  *       </para>
234  *      </entry>
235  *     </row>
236  *     <row>
237  *      <entry>
238  *       <para>
239  *        <literal>u</literal>
240  *       </para>
241  *      </entry>
242  *      <entry>
243  *       <para>
244  *        the type string of %G_VARIANT_TYPE_UINT32; an unsigned 32 bit
245  *        integer.
246  *       </para>
247  *      </entry>
248  *     </row>
249  *     <row>
250  *      <entry>
251  *       <para>
252  *        <literal>x</literal>
253  *       </para>
254  *      </entry>
255  *      <entry>
256  *       <para>
257  *        the type string of %G_VARIANT_TYPE_INT64; a signed 64 bit
258  *        integer.
259  *       </para>
260  *      </entry>
261  *     </row>
262  *     <row>
263  *      <entry>
264  *       <para>
265  *        <literal>t</literal>
266  *       </para>
267  *      </entry>
268  *      <entry>
269  *       <para>
270  *        the type string of %G_VARIANT_TYPE_UINT64; an unsigned 64 bit
271  *        integer.
272  *       </para>
273  *      </entry>
274  *     </row>
275  *     <row>
276  *      <entry>
277  *       <para>
278  *        <literal>h</literal>
279  *       </para>
280  *      </entry>
281  *      <entry>
282  *       <para>
283  *        the type string of %G_VARIANT_TYPE_HANDLE; a signed 32 bit
284  *        value that, by convention, is used as an index into an array
285  *        of file descriptors that are sent alongside a D-Bus message.
286  *       </para>
287  *      </entry>
288  *     </row>
289  *     <row>
290  *      <entry>
291  *       <para>
292  *        <literal>d</literal>
293  *       </para>
294  *      </entry>
295  *      <entry>
296  *       <para>
297  *        the type string of %G_VARIANT_TYPE_DOUBLE; a double precision
298  *        floating point value.
299  *       </para>
300  *      </entry>
301  *     </row>
302  *     <row>
303  *      <entry>
304  *       <para>
305  *        <literal>s</literal>
306  *       </para>
307  *      </entry>
308  *      <entry>
309  *       <para>
310  *        the type string of %G_VARIANT_TYPE_STRING; a string.
311  *       </para>
312  *      </entry>
313  *     </row>
314  *     <row>
315  *      <entry>
316  *       <para>
317  *        <literal>o</literal>
318  *       </para>
319  *      </entry>
320  *      <entry>
321  *       <para>
322  *        the type string of %G_VARIANT_TYPE_OBJECT_PATH; a string in
323  *        the form of a D-Bus object path.
324  *       </para>
325  *      </entry>
326  *     </row>
327  *     <row>
328  *      <entry>
329  *       <para>
330  *        <literal>g</literal>
331  *       </para>
332  *      </entry>
333  *      <entry>
334  *       <para>
335  *        the type string of %G_VARIANT_TYPE_STRING; a string in the
336  *        form of a D-Bus type signature.
337  *       </para>
338  *      </entry>
339  *     </row>
340  *     <row>
341  *      <entry>
342  *       <para>
343  *        <literal>?</literal>
344  *       </para>
345  *      </entry>
346  *      <entry>
347  *       <para>
348  *        the type string of %G_VARIANT_TYPE_BASIC; an indefinite type
349  *        that is a supertype of any of the basic types.
350  *       </para>
351  *      </entry>
352  *     </row>
353  *     <row>
354  *      <entry>
355  *       <para>
356  *        <literal>v</literal>
357  *       </para>
358  *      </entry>
359  *      <entry>
360  *       <para>
361  *        the type string of %G_VARIANT_TYPE_VARIANT; a container type
362  *        that contain any other type of value.
363  *       </para>
364  *      </entry>
365  *     </row>
366  *     <row>
367  *      <entry>
368  *       <para>
369  *        <literal>a</literal>
370  *       </para>
371  *      </entry>
372  *      <entry>
373  *       <para>
374  *        used as a prefix on another type string to mean an array of
375  *        that type; the type string "<literal>ai</literal>", for
376  *        example, is the type of an array of 32 bit signed integers.
377  *       </para>
378  *      </entry>
379  *     </row>
380  *     <row>
381  *      <entry>
382  *       <para>
383  *        <literal>m</literal>
384  *       </para>
385  *      </entry>
386  *      <entry>
387  *       <para>
388  *        used as a prefix on another type string to mean a "maybe", or
389  *        "nullable", version of that type; the type string
390  *        "<literal>ms</literal>", for example, is the type of a value
391  *        that maybe contains a string, or maybe contains nothing.
392  *       </para>
393  *      </entry>
394  *     </row>
395  *     <row>
396  *      <entry>
397  *       <para>
398  *        <literal>()</literal>
399  *       </para>
400  *      </entry>
401  *      <entry>
402  *       <para>
403  *        used to enclose zero or more other concatenated type strings
404  *        to create a tuple type; the type string
405  *        "<literal>(is)</literal>", for example, is the type of a pair
406  *        of an integer and a string.
407  *       </para>
408  *      </entry>
409  *     </row>
410  *     <row>
411  *      <entry>
412  *       <para>
413  *        <literal>r</literal>
414  *       </para>
415  *      </entry>
416  *      <entry>
417  *       <para>
418  *        the type string of %G_VARIANT_TYPE_TUPLE; an indefinite type
419  *        that is a supertype of any tuple type, regardless of the
420  *        number of items.
421  *       </para>
422  *      </entry>
423  *     </row>
424  *     <row>
425  *      <entry>
426  *       <para>
427  *        <literal>{}</literal>
428  *       </para>
429  *      </entry>
430  *      <entry>
431  *       <para>
432  *        used to enclose a basic type string concatenated with another
433  *        type string to create a dictionary entry type, which usually
434  *        appears inside of an array to form a dictionary; the type
435  *        string "<literal>a{sd}</literal>", for example, is the type of
436  *        a dictionary that maps strings to double precision floating
437  *        point values.
438  *       </para>
439  *       <para>
440  *        The first type (the basic type) is the key type and the second
441  *        type is the value type.  The reason that the first type is
442  *        restricted to being a basic type is so that it can easily be
443  *        hashed.
444  *       </para>
445  *      </entry>
446  *     </row>
447  *     <row>
448  *      <entry>
449  *       <para>
450  *        <literal>*</literal>
451  *       </para>
452  *      </entry>
453  *      <entry>
454  *       <para>
455  *        the type string of %G_VARIANT_TYPE_ANY; the indefinite type
456  *        that is a supertype of all types.  Note that, as with all type
457  *        strings, this character represents exactly one type.  It
458  *        cannot be used inside of tuples to mean "any number of items".
459  *       </para>
460  *      </entry>
461  *     </row>
462  *    </tbody>
463  *   </tgroup>
464  *  </informaltable>
465  *  <para>
466  *   Any type string of a container that contains an indefinite type is,
467  *   itself, an indefinite type.  For example, the type string
468  *   "<literal>a*</literal>" (corresponding to %G_VARIANT_TYPE_ARRAY) is
469  *   an indefinite type that is a supertype of every array type.
470  *   "<literal>(*s)</literal>" is a supertype of all tuples that
471  *   contain exactly two items where the second item is a string.
472  *  </para>
473  *  <para>
474  *   "<literal>a{?*}</literal>" is an indefinite type that is a
475  *   supertype of all arrays containing dictionary entries where the key
476  *   is any basic type and the value is any type at all.  This is, by
477  *   definition, a dictionary, so this type string corresponds to
478  *   %G_VARIANT_TYPE_DICTIONARY.  Note that, due to the restriction that
479  *   the key of a dictionary entry must be a basic type,
480  *   "<literal>{**}</literal>" is not a valid type string.
481  *  </para>
482  * </refsect2>
483  */
484
485
486 static gboolean
487 g_variant_type_check (const GVariantType *type)
488 {
489   const gchar *type_string;
490
491   if (type == NULL)
492     return FALSE;
493
494   type_string = (const gchar *) type;
495 #ifndef G_DISABLE_CHECKS
496   return g_variant_type_string_scan (type_string, NULL, NULL);
497 #else
498   return TRUE;
499 #endif
500 }
501
502 /**
503  * g_variant_type_string_scan:
504  * @string: a pointer to any string
505  * @limit: (allow-none): the end of @string, or %NULL
506  * @endptr: (out) (allow-none): location to store the end pointer, or %NULL
507  * @returns: %TRUE if a valid type string was found
508  *
509  * Scan for a single complete and valid GVariant type string in @string.
510  * The memory pointed to by @limit (or bytes beyond it) is never
511  * accessed.
512  *
513  * If a valid type string is found, @endptr is updated to point to the
514  * first character past the end of the string that was found and %TRUE
515  * is returned.
516  *
517  * If there is no valid type string starting at @string, or if the type
518  * string does not end before @limit then %FALSE is returned.
519  *
520  * For the simple case of checking if a string is a valid type string,
521  * see g_variant_type_string_is_valid().
522  *
523  * Since: 2.24
524  **/
525 gboolean
526 g_variant_type_string_scan (const gchar  *string,
527                             const gchar  *limit,
528                             const gchar **endptr)
529 {
530   g_return_val_if_fail (string != NULL, FALSE);
531
532   if (string == limit || *string == '\0')
533     return FALSE;
534
535   switch (*string++)
536     {
537     case '(':
538       while (string == limit || *string != ')')
539         if (!g_variant_type_string_scan (string, limit, &string))
540           return FALSE;
541
542       string++;
543       break;
544
545     case '{':
546       if (string == limit || *string == '\0' ||                    /* { */
547           !strchr ("bynqihuxtdsog?", *string++) ||                 /* key */
548           !g_variant_type_string_scan (string, limit, &string) ||  /* value */
549           string == limit || *string++ != '}')                     /* } */
550         return FALSE;
551
552       break;
553
554     case 'm': case 'a':
555       return g_variant_type_string_scan (string, limit, endptr);
556
557     case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
558     case 'x': case 't': case 'd': case 's': case 'o': case 'g':
559     case 'v': case 'r': case '*': case '?': case 'h':
560       break;
561
562     default:
563       return FALSE;
564     }
565
566   if (endptr != NULL)
567     *endptr = string;
568
569   return TRUE;
570 }
571
572 /**
573  * g_variant_type_string_is_valid:
574  * @type_string: a pointer to any string
575  * @returns: %TRUE if @type_string is exactly one valid type string
576  *
577  * Checks if @type_string is a valid GVariant type string.  This call is
578  * equivalent to calling g_variant_type_string_scan() and confirming
579  * that the following character is a nul terminator.
580  *
581  * Since 2.24
582  **/
583 gboolean
584 g_variant_type_string_is_valid (const gchar *type_string)
585 {
586   const gchar *endptr;
587
588   g_return_val_if_fail (type_string != NULL, FALSE);
589
590   if (!g_variant_type_string_scan (type_string, NULL, &endptr))
591     return FALSE;
592
593   return *endptr == '\0';
594 }
595
596 /**
597  * g_variant_type_free:
598  * @type: a #GVariantType, or %NULL
599  *
600  * Frees a #GVariantType that was allocated with
601  * g_variant_type_copy(), g_variant_type_new() or one of the container
602  * type constructor functions.
603  *
604  * In the case that @type is %NULL, this function does nothing.
605  *
606  * Since 2.24
607  **/
608 void
609 g_variant_type_free (GVariantType *type)
610 {
611   g_return_if_fail (type == NULL || g_variant_type_check (type));
612
613   g_free (type);
614 }
615
616 /**
617  * g_variant_type_copy:
618  * @type: a #GVariantType
619  * @returns: (transfer full): a new #GVariantType
620  *
621  * Makes a copy of a #GVariantType.  It is appropriate to call
622  * g_variant_type_free() on the return value.  @type may not be %NULL.
623  *
624  * Since 2.24
625  **/
626 GVariantType *
627 g_variant_type_copy (const GVariantType *type)
628 {
629   gsize length;
630   gchar *new;
631
632   g_return_val_if_fail (g_variant_type_check (type), NULL);
633
634   length = g_variant_type_get_string_length (type);
635   new = g_malloc (length + 1);
636
637   memcpy (new, type, length);
638   new[length] = '\0';
639
640   return (GVariantType *) new;
641 }
642
643 /**
644  * g_variant_type_new:
645  * @type_string: a valid GVariant type string
646  * @returns: (transfer full): a new #GVariantType
647  *
648  * Creates a new #GVariantType corresponding to the type string given
649  * by @type_string.  It is appropriate to call g_variant_type_free() on
650  * the return value.
651  *
652  * It is a programmer error to call this function with an invalid type
653  * string.  Use g_variant_type_string_is_valid() if you are unsure.
654  *
655  * Since: 2.24
656  */
657 GVariantType *
658 g_variant_type_new (const gchar *type_string)
659 {
660   g_return_val_if_fail (type_string != NULL, NULL);
661
662   return g_variant_type_copy (G_VARIANT_TYPE (type_string));
663 }
664
665 /**
666  * g_variant_type_get_string_length:
667  * @type: a #GVariantType
668  * @returns: the length of the corresponding type string
669  *
670  * Returns the length of the type string corresponding to the given
671  * @type.  This function must be used to determine the valid extent of
672  * the memory region returned by g_variant_type_peek_string().
673  *
674  * Since 2.24
675  **/
676 gsize
677 g_variant_type_get_string_length (const GVariantType *type)
678 {
679   const gchar *type_string = (const gchar *) type;
680   gint brackets = 0;
681   gsize index = 0;
682
683   g_return_val_if_fail (g_variant_type_check (type), 0);
684
685   do
686     {
687       while (type_string[index] == 'a' || type_string[index] == 'm')
688         index++;
689
690       if (type_string[index] == '(' || type_string[index] == '{')
691         brackets++;
692
693       else if (type_string[index] == ')' || type_string[index] == '}')
694         brackets--;
695
696       index++;
697     }
698   while (brackets);
699
700   return index;
701 }
702
703 /*
704   This function is not introspectable, it returns something that
705   is not an array and neither a string
706 */
707 /**
708  * g_variant_type_peek_string: (skip)
709  * @type: a #GVariantType
710  * @returns: the corresponding type string (not nul-terminated)
711  *
712  * Returns the type string corresponding to the given @type.  The
713  * result is not nul-terminated; in order to determine its length you
714  * must call g_variant_type_get_string_length().
715  *
716  * To get a nul-terminated string, see g_variant_type_dup_string().
717  *
718  * Since 2.24
719  **/
720 const gchar *
721 g_variant_type_peek_string (const GVariantType *type)
722 {
723   g_return_val_if_fail (g_variant_type_check (type), NULL);
724
725   return (const gchar *) type;
726 }
727
728 /**
729  * g_variant_type_dup_string:
730  * @type: a #GVariantType
731  * @returns: (transfer full): the corresponding type string
732  *
733  * Returns a newly-allocated copy of the type string corresponding to
734  * @type.  The returned string is nul-terminated.  It is appropriate to
735  * call g_free() on the return value.
736  *
737  * Since 2.24
738  **/
739 gchar *
740 g_variant_type_dup_string (const GVariantType *type)
741 {
742   g_return_val_if_fail (g_variant_type_check (type), NULL);
743
744   return g_strndup (g_variant_type_peek_string (type),
745                     g_variant_type_get_string_length (type));
746 }
747
748 /**
749  * g_variant_type_is_definite:
750  * @type: a #GVariantType
751  * @returns: %TRUE if @type is definite
752  *
753  * Determines if the given @type is definite (ie: not indefinite).
754  *
755  * A type is definite if its type string does not contain any indefinite
756  * type characters ('*', '?', or 'r').
757  *
758  * A #GVariant instance may not have an indefinite type, so calling
759  * this function on the result of g_variant_get_type() will always
760  * result in %TRUE being returned.  Calling this function on an
761  * indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in
762  * %FALSE being returned.
763  *
764  * Since 2.24
765  **/
766 gboolean
767 g_variant_type_is_definite (const GVariantType *type)
768 {
769   const gchar *type_string;
770   gsize type_length;
771   gsize i;
772
773   g_return_val_if_fail (g_variant_type_check (type), FALSE);
774
775   type_length = g_variant_type_get_string_length (type);
776   type_string = g_variant_type_peek_string (type);
777
778   for (i = 0; i < type_length; i++)
779     if (type_string[i] == '*' ||
780         type_string[i] == '?' ||
781         type_string[i] == 'r')
782       return FALSE;
783
784   return TRUE;
785 }
786
787 /**
788  * g_variant_type_is_container:
789  * @type: a #GVariantType
790  * @returns: %TRUE if @type is a container type
791  *
792  * Determines if the given @type is a container type.
793  *
794  * Container types are any array, maybe, tuple, or dictionary
795  * entry types plus the variant type.
796  *
797  * This function returns %TRUE for any indefinite type for which every
798  * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
799  * example.
800  *
801  * Since 2.24
802  **/
803 gboolean
804 g_variant_type_is_container (const GVariantType *type)
805 {
806   gchar first_char;
807
808   g_return_val_if_fail (g_variant_type_check (type), FALSE);
809
810   first_char = g_variant_type_peek_string (type)[0];
811   switch (first_char)
812   {
813     case 'a':
814     case 'm':
815     case 'r':
816     case '(':
817     case '{':
818     case 'v':
819       return TRUE;
820
821     default:
822       return FALSE;
823   }
824 }
825
826 /**
827  * g_variant_type_is_basic:
828  * @type: a #GVariantType
829  * @returns: %TRUE if @type is a basic type
830  *
831  * Determines if the given @type is a basic type.
832  *
833  * Basic types are booleans, bytes, integers, doubles, strings, object
834  * paths and signatures.
835  *
836  * Only a basic type may be used as the key of a dictionary entry.
837  *
838  * This function returns %FALSE for all indefinite types except
839  * %G_VARIANT_TYPE_BASIC.
840  *
841  * Since 2.24
842  **/
843 gboolean
844 g_variant_type_is_basic (const GVariantType *type)
845 {
846   gchar first_char;
847
848   g_return_val_if_fail (g_variant_type_check (type), FALSE);
849
850   first_char = g_variant_type_peek_string (type)[0];
851   switch (first_char)
852   {
853     case 'b':
854     case 'y':
855     case 'n':
856     case 'q':
857     case 'i':
858     case 'h':
859     case 'u':
860     case 't':
861     case 'x':
862     case 'd':
863     case 's':
864     case 'o':
865     case 'g':
866     case '?':
867       return TRUE;
868
869     default:
870       return FALSE;
871   }
872 }
873
874 /**
875  * g_variant_type_is_maybe:
876  * @type: a #GVariantType
877  * @returns: %TRUE if @type is a maybe type
878  *
879  * Determines if the given @type is a maybe type.  This is true if the
880  * type string for @type starts with an 'm'.
881  *
882  * This function returns %TRUE for any indefinite type for which every
883  * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
884  * example.
885  *
886  * Since 2.24
887  **/
888 gboolean
889 g_variant_type_is_maybe (const GVariantType *type)
890 {
891   g_return_val_if_fail (g_variant_type_check (type), FALSE);
892
893   return g_variant_type_peek_string (type)[0] == 'm';
894 }
895
896 /**
897  * g_variant_type_is_array:
898  * @type: a #GVariantType
899  * @returns: %TRUE if @type is an array type
900  *
901  * Determines if the given @type is an array type.  This is true if the
902  * type string for @type starts with an 'a'.
903  *
904  * This function returns %TRUE for any indefinite type for which every
905  * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
906  * example.
907  *
908  * Since 2.24
909  **/
910 gboolean
911 g_variant_type_is_array (const GVariantType *type)
912 {
913   g_return_val_if_fail (g_variant_type_check (type), FALSE);
914
915   return g_variant_type_peek_string (type)[0] == 'a';
916 }
917
918 /**
919  * g_variant_type_is_tuple:
920  * @type: a #GVariantType
921  * @returns: %TRUE if @type is a tuple type
922  *
923  * Determines if the given @type is a tuple type.  This is true if the
924  * type string for @type starts with a '(' or if @type is
925  * %G_VARIANT_TYPE_TUPLE.
926  *
927  * This function returns %TRUE for any indefinite type for which every
928  * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
929  * example.
930  *
931  * Since 2.24
932  **/
933 gboolean
934 g_variant_type_is_tuple (const GVariantType *type)
935 {
936   gchar type_char;
937
938   g_return_val_if_fail (g_variant_type_check (type), FALSE);
939
940   type_char = g_variant_type_peek_string (type)[0];
941   return type_char == 'r' || type_char == '(';
942 }
943
944 /**
945  * g_variant_type_is_dict_entry:
946  * @type: a #GVariantType
947  * @returns: %TRUE if @type is a dictionary entry type
948  *
949  * Determines if the given @type is a dictionary entry type.  This is
950  * true if the type string for @type starts with a '{'.
951  *
952  * This function returns %TRUE for any indefinite type for which every
953  * definite subtype is a dictionary entry type --
954  * %G_VARIANT_TYPE_DICT_ENTRY, for example.
955  *
956  * Since 2.24
957  **/
958 gboolean
959 g_variant_type_is_dict_entry (const GVariantType *type)
960 {
961   g_return_val_if_fail (g_variant_type_check (type), FALSE);
962
963   return g_variant_type_peek_string (type)[0] == '{';
964 }
965
966 /**
967  * g_variant_type_is_variant:
968  * @type: a #GVariantType
969  * @returns: %TRUE if @type is the variant type
970  *
971  * Determines if the given @type is the variant type.
972  *
973  * Since 2.24
974  **/
975 gboolean
976 g_variant_type_is_variant (const GVariantType *type)
977 {
978   g_return_val_if_fail (g_variant_type_check (type), FALSE);
979
980   return g_variant_type_peek_string (type)[0] == 'v';
981 }
982
983 /**
984  * g_variant_type_hash:
985  * @type: (type GVariantType): a #GVariantType
986  * @returns: the hash value
987  *
988  * Hashes @type.
989  *
990  * The argument type of @type is only #gconstpointer to allow use with
991  * #GHashTable without function pointer casting.  A valid
992  * #GVariantType must be provided.
993  *
994  * Since 2.24
995  **/
996 guint
997 g_variant_type_hash (gconstpointer type)
998 {
999   const gchar *type_string;
1000   guint value = 0;
1001   gsize length;
1002   gsize i;
1003
1004   g_return_val_if_fail (g_variant_type_check (type), 0);
1005
1006   type_string = g_variant_type_peek_string (type);
1007   length = g_variant_type_get_string_length (type);
1008
1009   for (i = 0; i < length; i++)
1010     value = (value << 5) - value + type_string[i];
1011
1012   return value;
1013 }
1014
1015 /**
1016  * g_variant_type_equal:
1017  * @type1: (type GVariantType): a #GVariantType
1018  * @type2: (type GVariantType): a #GVariantType
1019  * @returns: %TRUE if @type1 and @type2 are exactly equal
1020  *
1021  * Compares @type1 and @type2 for equality.
1022  *
1023  * Only returns %TRUE if the types are exactly equal.  Even if one type
1024  * is an indefinite type and the other is a subtype of it, %FALSE will
1025  * be returned if they are not exactly equal.  If you want to check for
1026  * subtypes, use g_variant_type_is_subtype_of().
1027  *
1028  * The argument types of @type1 and @type2 are only #gconstpointer to
1029  * allow use with #GHashTable without function pointer casting.  For
1030  * both arguments, a valid #GVariantType must be provided.
1031  *
1032  * Since 2.24
1033  **/
1034 gboolean
1035 g_variant_type_equal (gconstpointer type1,
1036                       gconstpointer type2)
1037 {
1038   const gchar *string1, *string2;
1039   gsize size1, size2;
1040
1041   g_return_val_if_fail (g_variant_type_check (type1), FALSE);
1042   g_return_val_if_fail (g_variant_type_check (type2), FALSE);
1043
1044   if (type1 == type2)
1045     return TRUE;
1046
1047   size1 = g_variant_type_get_string_length (type1);
1048   size2 = g_variant_type_get_string_length (type2);
1049
1050   if (size1 != size2)
1051     return FALSE;
1052
1053   string1 = g_variant_type_peek_string (type1);
1054   string2 = g_variant_type_peek_string (type2);
1055
1056   return memcmp (string1, string2, size1) == 0;
1057 }
1058
1059 /**
1060  * g_variant_type_is_subtype_of:
1061  * @type: a #GVariantType
1062  * @supertype: a #GVariantType
1063  * @returns: %TRUE if @type is a subtype of @supertype
1064  *
1065  * Checks if @type is a subtype of @supertype.
1066  *
1067  * This function returns %TRUE if @type is a subtype of @supertype.  All
1068  * types are considered to be subtypes of themselves.  Aside from that,
1069  * only indefinite types can have subtypes.
1070  *
1071  * Since 2.24
1072  **/
1073 gboolean
1074 g_variant_type_is_subtype_of (const GVariantType *type,
1075                               const GVariantType *supertype)
1076 {
1077   const gchar *supertype_string;
1078   const gchar *supertype_end;
1079   const gchar *type_string;
1080
1081   g_return_val_if_fail (g_variant_type_check (type), FALSE);
1082   g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
1083
1084   supertype_string = g_variant_type_peek_string (supertype);
1085   type_string = g_variant_type_peek_string (type);
1086
1087   supertype_end = supertype_string +
1088                   g_variant_type_get_string_length (supertype);
1089
1090   /* we know that type and supertype are both well-formed, so it's
1091    * safe to treat this merely as a text processing problem.
1092    */
1093   while (supertype_string < supertype_end)
1094     {
1095       char supertype_char = *supertype_string++;
1096
1097       if (supertype_char == *type_string)
1098         type_string++;
1099
1100       else if (*type_string == ')')
1101         return FALSE;
1102
1103       else
1104         {
1105           const GVariantType *target_type = (GVariantType *) type_string;
1106
1107           switch (supertype_char)
1108             {
1109             case 'r':
1110               if (!g_variant_type_is_tuple (target_type))
1111                 return FALSE;
1112               break;
1113
1114             case '*':
1115               break;
1116
1117             case '?':
1118               if (!g_variant_type_is_basic (target_type))
1119                 return FALSE;
1120               break;
1121
1122             default:
1123               return FALSE;
1124             }
1125
1126           type_string += g_variant_type_get_string_length (target_type);
1127         }
1128     }
1129
1130   return TRUE;
1131 }
1132
1133 /**
1134  * g_variant_type_element:
1135  * @type: an array or maybe #GVariantType
1136  * @returns: (transfer none): the element type of @type
1137  *
1138  * Determines the element type of an array or maybe type.
1139  *
1140  * This function may only be used with array or maybe types.
1141  *
1142  * Since 2.24
1143  **/
1144 const GVariantType *
1145 g_variant_type_element (const GVariantType *type)
1146 {
1147   const gchar *type_string;
1148
1149   g_return_val_if_fail (g_variant_type_check (type), NULL);
1150
1151   type_string = g_variant_type_peek_string (type);
1152
1153   g_assert (type_string[0] == 'a' || type_string[0] == 'm');
1154
1155   return (const GVariantType *) &type_string[1];
1156 }
1157
1158 /**
1159  * g_variant_type_first:
1160  * @type: a tuple or dictionary entry #GVariantType
1161  * @returns: (transfer none): the first item type of @type, or %NULL
1162  *
1163  * Determines the first item type of a tuple or dictionary entry
1164  * type.
1165  *
1166  * This function may only be used with tuple or dictionary entry types,
1167  * but must not be used with the generic tuple type
1168  * %G_VARIANT_TYPE_TUPLE.
1169  *
1170  * In the case of a dictionary entry type, this returns the type of
1171  * the key.
1172  *
1173  * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
1174  *
1175  * This call, together with g_variant_type_next() provides an iterator
1176  * interface over tuple and dictionary entry types.
1177  *
1178  * Since 2.24
1179  **/
1180 const GVariantType *
1181 g_variant_type_first (const GVariantType *type)
1182 {
1183   const gchar *type_string;
1184
1185   g_return_val_if_fail (g_variant_type_check (type), NULL);
1186
1187   type_string = g_variant_type_peek_string (type);
1188   g_assert (type_string[0] == '(' || type_string[0] == '{');
1189
1190   if (type_string[1] == ')')
1191     return NULL;
1192
1193   return (const GVariantType *) &type_string[1];
1194 }
1195
1196 /**
1197  * g_variant_type_next:
1198  * @type: a #GVariantType from a previous call
1199  * @returns: (transfer none): the next #GVariantType after @type, or %NULL
1200  *
1201  * Determines the next item type of a tuple or dictionary entry
1202  * type.
1203  *
1204  * @type must be the result of a previous call to
1205  * g_variant_type_first() or g_variant_type_next().
1206  *
1207  * If called on the key type of a dictionary entry then this call
1208  * returns the value type.  If called on the value type of a dictionary
1209  * entry then this call returns %NULL.
1210  *
1211  * For tuples, %NULL is returned when @type is the last item in a tuple.
1212  *
1213  * Since 2.24
1214  **/
1215 const GVariantType *
1216 g_variant_type_next (const GVariantType *type)
1217 {
1218   const gchar *type_string;
1219
1220   g_return_val_if_fail (g_variant_type_check (type), NULL);
1221
1222   type_string = g_variant_type_peek_string (type);
1223   type_string += g_variant_type_get_string_length (type);
1224
1225   if (*type_string == ')' || *type_string == '}')
1226     return NULL;
1227
1228   return (const GVariantType *) type_string;
1229 }
1230
1231 /**
1232  * g_variant_type_n_items:
1233  * @type: a tuple or dictionary entry #GVariantType
1234  * @returns: the number of items in @type
1235  *
1236  * Determines the number of items contained in a tuple or
1237  * dictionary entry type.
1238  *
1239  * This function may only be used with tuple or dictionary entry types,
1240  * but must not be used with the generic tuple type
1241  * %G_VARIANT_TYPE_TUPLE.
1242  *
1243  * In the case of a dictionary entry type, this function will always
1244  * return 2.
1245  *
1246  * Since 2.24
1247  **/
1248 gsize
1249 g_variant_type_n_items (const GVariantType *type)
1250 {
1251   gsize count = 0;
1252
1253   g_return_val_if_fail (g_variant_type_check (type), 0);
1254
1255   for (type = g_variant_type_first (type);
1256        type;
1257        type = g_variant_type_next (type))
1258     count++;
1259
1260   return count;
1261 }
1262
1263 /**
1264  * g_variant_type_key:
1265  * @type: a dictionary entry #GVariantType
1266  * @returns: (transfer none): the key type of the dictionary entry
1267  *
1268  * Determines the key type of a dictionary entry type.
1269  *
1270  * This function may only be used with a dictionary entry type.  Other
1271  * than the additional restriction, this call is equivalent to
1272  * g_variant_type_first().
1273  *
1274  * Since 2.24
1275  **/
1276 const GVariantType *
1277 g_variant_type_key (const GVariantType *type)
1278 {
1279   const gchar *type_string;
1280
1281   g_return_val_if_fail (g_variant_type_check (type), NULL);
1282
1283   type_string = g_variant_type_peek_string (type);
1284   g_assert (type_string[0] == '{');
1285
1286   return (const GVariantType *) &type_string[1];
1287 }
1288
1289 /**
1290  * g_variant_type_value:
1291  * @type: a dictionary entry #GVariantType
1292  * @returns: (transfer none): the value type of the dictionary entry
1293  *
1294  * Determines the value type of a dictionary entry type.
1295  *
1296  * This function may only be used with a dictionary entry type.
1297  *
1298  * Since 2.24
1299  **/
1300 const GVariantType *
1301 g_variant_type_value (const GVariantType *type)
1302 {
1303   const gchar *type_string;
1304
1305   g_return_val_if_fail (g_variant_type_check (type), NULL);
1306
1307   type_string = g_variant_type_peek_string (type);
1308   g_assert (type_string[0] == '{');
1309
1310   return g_variant_type_next (g_variant_type_key (type));
1311 }
1312
1313 /**
1314  * g_variant_type_new_tuple:
1315  * @items: (array length=length): an array of #GVariantTypes, one for each item
1316  * @length: the length of @items, or -1
1317  * @returns: (transfer full): a new tuple #GVariantType
1318  *
1319  * Constructs a new tuple type, from @items.
1320  *
1321  * @length is the number of items in @items, or -1 to indicate that
1322  * @items is %NULL-terminated.
1323  *
1324  * It is appropriate to call g_variant_type_free() on the return value.
1325  *
1326  * Since 2.24
1327  **/
1328 static GVariantType *
1329 g_variant_type_new_tuple_slow (const GVariantType * const *items,
1330                                gint                        length)
1331 {
1332   /* the "slow" version is needed in case the static buffer of 1024
1333    * bytes is exceeded when running the normal version.  this will
1334    * happen only in truly insane code, so it can be slow.
1335    */
1336   GString *string;
1337   gsize i;
1338
1339   string = g_string_new ("(");
1340   for (i = 0; i < length; i++)
1341     {
1342       const GVariantType *type;
1343       gsize size;
1344
1345       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1346
1347       type = items[i];
1348       size = g_variant_type_get_string_length (type);
1349       g_string_append_len (string, (const gchar *) type, size);
1350     }
1351   g_string_append_c (string, ')');
1352
1353   return (GVariantType *) g_string_free (string, FALSE);
1354 }
1355
1356 GVariantType *
1357 g_variant_type_new_tuple (const GVariantType * const *items,
1358                           gint                        length)
1359 {
1360   char buffer[1024];
1361   gsize offset;
1362   gsize i;
1363
1364   g_return_val_if_fail (length == 0 || items != NULL, NULL);
1365
1366   if (length < 0)
1367     for (length = 0; items[length] != NULL; length++);
1368
1369   offset = 0;
1370   buffer[offset++] = '(';
1371
1372   for (i = 0; i < length; i++)
1373     {
1374       const GVariantType *type;
1375       gsize size;
1376
1377       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1378
1379       type = items[i];
1380       size = g_variant_type_get_string_length (type);
1381
1382       if (offset + size >= sizeof buffer) /* leave room for ')' */
1383         return g_variant_type_new_tuple_slow (items, length);
1384
1385       memcpy (&buffer[offset], type, size);
1386       offset += size;
1387     }
1388
1389   g_assert (offset < sizeof buffer);
1390   buffer[offset++] = ')';
1391
1392   return (GVariantType *) g_memdup (buffer, offset);
1393 }
1394
1395 /**
1396  * g_variant_type_new_array:
1397  * @element: a #GVariantType
1398  * @returns: (transfer full): a new array #GVariantType
1399  *
1400  * Constructs the type corresponding to an array of elements of the
1401  * type @type.
1402  *
1403  * It is appropriate to call g_variant_type_free() on the return value.
1404  *
1405  * Since 2.24
1406  **/
1407 GVariantType *
1408 g_variant_type_new_array (const GVariantType *element)
1409 {
1410   gsize size;
1411   gchar *new;
1412
1413   g_return_val_if_fail (g_variant_type_check (element), NULL);
1414
1415   size = g_variant_type_get_string_length (element);
1416   new = g_malloc (size + 1);
1417
1418   new[0] = 'a';
1419   memcpy (new + 1, element, size);
1420
1421   return (GVariantType *) new;
1422 }
1423
1424 /**
1425  * g_variant_type_new_maybe:
1426  * @element: a #GVariantType
1427  * @returns: (transfer full): a new maybe #GVariantType
1428  *
1429  * Constructs the type corresponding to a maybe instance containing
1430  * type @type or Nothing.
1431  *
1432  * It is appropriate to call g_variant_type_free() on the return value.
1433  *
1434  * Since 2.24
1435  **/
1436 GVariantType *
1437 g_variant_type_new_maybe (const GVariantType *element)
1438 {
1439   gsize size;
1440   gchar *new;
1441
1442   g_return_val_if_fail (g_variant_type_check (element), NULL);
1443
1444   size = g_variant_type_get_string_length (element);
1445   new = g_malloc (size + 1);
1446
1447   new[0] = 'm';
1448   memcpy (new + 1, element, size);
1449
1450   return (GVariantType *) new;
1451 }
1452
1453 /**
1454  * g_variant_type_new_dict_entry:
1455  * @key: a basic #GVariantType
1456  * @value: a #GVariantType
1457  * @returns: (transfer full): a new dictionary entry #GVariantType
1458  *
1459  * Constructs the type corresponding to a dictionary entry with a key
1460  * of type @key and a value of type @value.
1461  *
1462  * It is appropriate to call g_variant_type_free() on the return value.
1463  *
1464  * Since 2.24
1465  **/
1466 GVariantType *
1467 g_variant_type_new_dict_entry (const GVariantType *key,
1468                                const GVariantType *value)
1469 {
1470   gsize keysize, valsize;
1471   gchar *new;
1472
1473   g_return_val_if_fail (g_variant_type_check (key), NULL);
1474   g_return_val_if_fail (g_variant_type_check (value), NULL);
1475
1476   keysize = g_variant_type_get_string_length (key);
1477   valsize = g_variant_type_get_string_length (value);
1478
1479   new = g_malloc (1 + keysize + valsize + 1);
1480
1481   new[0] = '{';
1482   memcpy (new + 1, key, keysize);
1483   memcpy (new + 1 + keysize, value, valsize);
1484   new[1 + keysize + valsize] = '}';
1485
1486   return (GVariantType *) new;
1487 }
1488
1489 /* private */
1490 const GVariantType *
1491 g_variant_type_checked_ (const gchar *type_string)
1492 {
1493   g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1494   return (const GVariantType *) type_string;
1495 }