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