1 <?xml version='1.0' encoding='utf-8'?>
3 <refentry id='gvariant-format-strings'>
5 <refentrytitle>GVariant Format Strings</refentrytitle>
8 <refname>GVariant Format Strings</refname>
9 <refpurpose>varargs conversion of GVariants</refpurpose>
13 <title>Variable Argument Conversions</title>
16 This page attempts to document how to perform variable argument
17 conversions with GVariant.
20 Conversions occur according to format strings. A format string is a two-way mapping between a single
21 <link linkend='GVariant'>GVariant</link> value and one or more C values.
24 A conversion from C values into a <link linkend='GVariant'>GVariant</link> value is made using the
25 <link linkend='g-variant-new'><function>g_variant_new()</function></link> function. A conversion from a
26 <link linkend='GVariant'>GVariant</link> into C values is made using the
27 <link linkend='g-variant-get'><function>g_variant_get()</function></link> function.
35 This section exhaustively describes all possibilities for GVariant format strings. There are no valid forms of
36 format strings other than those described here. Please note that the format string syntax is likely to expand in the
40 Valid format strings have one of the following forms:
44 <para>any type string</para>
48 a type string prefixed with a '<literal>@</literal>'
53 '<literal>&s</literal>' '<literal>&o</literal>', '<literal>&g</literal>', '<literal>^as</literal>',
54 '<literal>^a&s</literal>', '<literal>^ao</literal>', '<literal>^a&o</literal>','<literal>^ay</literal>',
55 '<literal>^&ay</literal>', '<literal>^aay</literal>' or '<literal>^a&ay</literal>'.
60 any format string, prefixed with an '<literal>m</literal>'
65 a sequence of zero or more format strings, concatenated and enclosed in parentheses
70 an opening brace, followed by two format strings, followed by a closing brace (subject to the constraint that the
71 first format string correspond to a type valid for use as the key type of a dictionary)
77 <title>Symbols</title>
80 The following table describes the rough meaning of symbols that may appear inside a GVariant format string. Each
81 symbol is described in detail in its own section, including usage examples.
86 <colspec colname='col_0'/>
87 <colspec colname='col_1'/>
91 <entry colsep='1' rowsep='1'>
93 <emphasis role='strong'>Symbol</emphasis>
96 <entry colsep='1' rowsep='1'>
98 <emphasis role='strong'>Meaning</emphasis>
104 <entry colsep='1' rowsep='1'>
106 <emphasis role='strong'>
107 <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>, <literal>i</literal>,
108 <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>, <literal>d</literal>
112 <entry colsep='1' rowsep='1'>
114 Used for building or deconstructing boolean, byte and numeric types. See
115 <link linkend='gvariant-format-strings-numeric-types'>Numeric Types</link> below.
121 <entry colsep='1' rowsep='1'>
123 <emphasis role='strong'>
124 <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
128 <entry colsep='1' rowsep='1'>
130 Used for building or deconstructing string types. See
131 <link linkend='gvariant-format-strings-strings'>Strings</link> below.
137 <entry colsep='1' rowsep='1'>
139 <emphasis role='strong'><literal>v</literal></emphasis>
142 <entry colsep='1' rowsep='1'>
144 Used for building or deconstructing variant types. See
145 <link linkend='gvariant-format-strings-variants'>Variants</link> below.
151 <entry colsep='1' rowsep='1'>
153 <emphasis role='strong'>
158 <entry colsep='1' rowsep='1'>
160 Used for building or deconstructing arrays. See
161 <link linkend='gvariant-format-strings-arrays'>Arrays</link> below.
167 <entry colsep='1' rowsep='1'>
169 <emphasis role='strong'>
174 <entry colsep='1' rowsep='1'>
176 Used for building or deconstructing maybe types. See
177 <link linkend='gvariant-format-strings-maybe-types'>Maybe Types</link> below.
183 <entry colsep='1' rowsep='1'>
185 <emphasis role='strong'>
186 <literal>()</literal>
190 <entry colsep='1' rowsep='1'>
192 Used for building or deconstructing tuples. See
193 <link linkend='gvariant-format-strings-tuples'>Tuples</link> below.
199 <entry colsep='1' rowsep='1'>
201 <emphasis role='strong'>
202 <literal>{}</literal>
206 <entry colsep='1' rowsep='1'>
208 Used for building or deconstructing dictionary entries. See
209 <link linkend='gvariant-format-strings-dictionaries'>Dictionaries</link> below.
215 <entry colsep='1' rowsep='1'>
217 <emphasis role='strong'>
222 <entry colsep='1' rowsep='1'>
224 Used as a prefix for a GVariant type string (not a prefix for a format string, so <literal>@as</literal> is
225 a valid format string but <literal>@^as</literal> is not). Denotes that a pointer to a
226 <link linkend='GVariant'>GVariant</link> should be used in place of the normal C type or types. For
227 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this means that you must pass a
228 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> <code>(<link linkend='GVariant'>GVariant</link>
229 *)</code>; if it is a floating reference, ownership will be taken, as
230 if by using <link linkend="g-variant-ref-sink"><function>g_variant_ref_sink()</function></link>.
231 For <link linkend='g-variant-get'><function>g_variant_get()</function></link> this means that you
232 must pass a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> for the value to be returned
233 by reference or <link linkend='NULL:CAPS'><literal>NULL</literal></link> to ignore the value. See
234 <link linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
240 <entry colsep='1' rowsep='1'>
242 <emphasis role='strong'>
243 <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
247 <entry colsep='1' rowsep='1'>
249 Exactly equivalent to <literal>@*</literal>, <literal>@?</literal> and <literal>@r</literal>. Provided only for
250 completeness so that all GVariant type strings can be used also as format strings. See <link
251 linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
257 <entry colsep='1' rowsep='1'>
259 <emphasis role='strong'><literal>&</literal></emphasis>
262 <entry colsep='1' rowsep='1'>
264 Used as a prefix for a GVariant type string (not a prefix for a format string, so <literal>&s</literal> is
265 a valid format string but <literal>&@s</literal> is not).
266 Denotes that a C pointer to serialised data
267 should be used in place of the normal C type. See
268 <link linkend='gvariant-format-strings-pointers'>Pointers</link> below.
274 <entry colsep='1' rowsep='1'>
276 <emphasis role='strong'><literal>^</literal></emphasis>
279 <entry colsep='1' rowsep='1'>
281 Used as a prefix on some specific types of format strings. See
282 <link linkend='gvariant-format-strings-convenience'>Convenience Conversions</link> below.
291 <refsect2 id='gvariant-format-strings-numeric-types'>
292 <title>Numeric Types</title>
294 <emphasis role='strong'>
295 Characters: <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>,
296 <literal>i</literal>, <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>,
302 Variable argument conversions from numeric types work in the most obvious way possible. Upon encountering one of
303 these characters, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes the equivalent C
304 type as an argument. <link linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to
305 the equivalent C type (or <link linkend='NULL:CAPS'><literal>NULL</literal></link> to ignore the value).
309 The equivalent C types are as follows:
314 <colspec colname='col_0'/><colspec colname='col_1'/>
317 <entry colsep='1' rowsep='1'>
319 <emphasis role='strong'>Character</emphasis>
322 <entry colsep='1' rowsep='1'>
324 <emphasis role='strong'>Equivalent C type</emphasis>
329 <entry colsep='1' rowsep='1'>
331 <emphasis role='strong'>
336 <entry colsep='1' rowsep='1'>
338 <link linkend='gboolean'><type>gboolean</type></link>
343 <entry colsep='1' rowsep='1'>
345 <emphasis role='strong'>
350 <entry colsep='1' rowsep='1'>
352 <link linkend='guchar'><type>guchar</type></link>
357 <entry colsep='1' rowsep='1'>
359 <emphasis role='strong'>
364 <entry colsep='1' rowsep='1'>
366 <link linkend='gint16'><type>gint16</type></link>
371 <entry colsep='1' rowsep='1'>
373 <emphasis role='strong'>
378 <entry colsep='1' rowsep='1'>
380 <link linkend='guint16'><type>guint16</type></link>
385 <entry colsep='1' rowsep='1'>
387 <emphasis role='strong'>
392 <entry colsep='1' rowsep='1'>
394 <link linkend='gint32'><type>gint32</type></link>
399 <entry colsep='1' rowsep='1'>
401 <emphasis role='strong'>
406 <entry colsep='1' rowsep='1'>
408 <link linkend='guint32'><type>guint32</type></link>
413 <entry colsep='1' rowsep='1'>
415 <emphasis role='strong'>
420 <entry colsep='1' rowsep='1'>
422 <link linkend='gint64'><type>gint64</type></link>
427 <entry colsep='1' rowsep='1'>
429 <emphasis role='strong'>
434 <entry colsep='1' rowsep='1'>
436 <link linkend='guint64'><type>guint64</type></link>
441 <entry colsep='1' rowsep='1'>
443 <emphasis role='strong'>
448 <entry colsep='1' rowsep='1'>
450 <link linkend='gint32'><type>gint32</type></link>
455 <entry colsep='1' rowsep='1'>
457 <emphasis role='strong'>
462 <entry colsep='1' rowsep='1'>
464 <link linkend='gdouble'><type>gdouble</type></link>
472 <anchor id='gvariant-varargs'/>
474 Note that in C, small integer types in variable argument lists are promoted up to <link
475 linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
476 read back accordingly. <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
477 currently supported. This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
478 with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
479 '<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
480 '<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'. Specifically, you can use integer
481 literals with these characters.
485 When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
486 provide is 64 bit. This means that you should use a cast or make use of the
487 <link linkend='G-GINT64-CONSTANT:CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
488 <link linkend='G-GUINT64-CONSTANT:CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
492 No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
493 it operates with pointers. The pointers must always point to a memory region of exactly the correct size.
497 <title>Examples</title>
498 <informalexample><programlisting>
499 <![CDATA[GVariant *value1, *value2, *value3, *value4;
501 value1 = g_variant_new ("y", 200);
502 value2 = g_variant_new ("b", TRUE);
503 value3 = g_variant_new ("d", 37.5):
504 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
512 g_variant_get (value1, "y", NULL); /* ignore the value. */
513 g_variant_get (value2, "b", &truth);
514 g_variant_get (value3, "d", &floating);
515 g_variant_get (value4, "x", &bignum);
516 }]]></programlisting></informalexample>
520 <refsect2 id='gvariant-format-strings-strings'>
521 <title>Strings</title>
523 <emphasis role='strong'>
524 Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
529 String conversions occur to and from standard nul-terminated C strings. Upon encountering an
530 '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
531 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
532 <link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
533 <link linkend='NULL:CAPS'><literal>NULL</literal></link> is not a valid string. If the '<literal>o</literal>' or
534 '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
535 object path or DBus type signature, respectively.
538 Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
539 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
540 <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
541 sets it to a newly-allocated copy of the string. It is appropriate to free this copy using
542 <link linkend='g-free'><function>g_free()</function></link>.
543 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
544 string should be ignored (in which case no copy is made).
548 <title>Examples</title>
549 <informalexample><programlisting>
550 <![CDATA[GVariant *value1, *value2, *value3;
552 value1 = g_variant_new ("s", "hello world!");
553 value2 = g_variant_new ("o", "/must/be/a/valid/path");
554 value3 = g_variant_new ("g", "iias");
557 g_variant_new ("s", NULL); /* not valid: NULL is not a string. */
563 g_variant_get (value1, "s", &result);
564 g_print ("It was '%s'\n", result);
566 }]]></programlisting></informalexample>
570 <refsect2 id='gvariant-format-strings-variants'>
571 <title>Variants</title>
573 <emphasis role='strong'>
574 Characters: <literal>v</literal>
579 Upon encountering a '<literal>v</literal>',
580 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
581 linkend='GVariant'>GVariant</link> *)</code>. The value of the
582 <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
585 Upon encountering a '<literal>v</literal>', <link
586 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
587 <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
588 </code>). It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
589 containing the contents of the variant value. It is appropriate to free this reference using
590 <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
591 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
592 ignored (in which case no new reference is created).
596 <title>Examples</title>
597 <informalexample><programlisting>
598 <![CDATA[GVariant *x, *y;
600 /* the following two lines are equivalent: */
601 x = g_variant_new ("v", y);
602 x = g_variant_new_variant (y);
605 g_variant_get (x, "v", &y);
606 y = g_variant_get_variant (x);]]></programlisting></informalexample>
611 <refsect2 id='gvariant-format-strings-arrays'>
612 <title>Arrays</title>
614 <emphasis role='strong'>
615 Characters: <literal>a</literal>
620 Upon encountering an '<literal>a</literal>' character followed by a type string,
621 <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
622 <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
623 for an array of the type given in the type string. The builder will have
624 <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
625 result will be used as the value. As a special exception, if the given type string is a definite type, then
626 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
630 Upon encountering an '<literal>a</literal>' character followed by a type string,
631 <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
632 <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
633 <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
634 A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
635 This iterator should be freed when you are done with it, using
636 <link linkend='g-variant-iter-free'><function>g_variant_iter_free()</function></link>.
637 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
642 <title>Examples</title>
643 <informalexample><programlisting>
644 <![CDATA[GVariantBuilder *builder;
647 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
648 g_variant_builder_add (builder, "s", "when");
649 g_variant_builder_add (builder, "s", "in");
650 g_variant_builder_add (builder, "s", "the");
651 g_variant_builder_add (builder, "s", "course");
652 value = g_variant_new ("as", builder);
653 g_variant_builder_unref (builder);
659 g_variant_get (value, "as", &iter);
660 while (g_variant_iter_loop (iter, "s", &str))
661 g_print ("%s\n", str);
662 g_variant_iter_free (iter);
665 g_variant_unref (value);]]></programlisting></informalexample>
669 <refsect2 id='gvariant-format-strings-maybe-types'>
670 <title>Maybe Types</title>
672 <emphasis role='strong'>
673 Characters: <literal>m</literal>
677 Maybe types are handled in two separate ways depending on the format string that follows the
678 '<literal>m</literal>'. The method that is used currently depends entirely on the character immediately following the
679 '<literal>m</literal>'.
683 The first way is used with format strings starting with '<literal>a</literal>', '<literal>s</literal>',
684 '<literal>o</literal>', '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>',
685 '<literal>*</literal>', '<literal>?</literal>', '<literal>r</literal>', '<literal>&</literal>', or
686 '<literal>^</literal>'. In all of these cases, for non-maybe types,
687 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a pointer to a
688 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> value and
689 <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
690 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer. When any of these format strings are
691 prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
692 <link linkend='NULL:CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
695 Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
696 here. Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
697 the Nothing value -- not an empty array.
700 The second way is used with all other format strings. For
701 <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
702 <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
703 <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
704 <code>(<link linkend='gboolean'>gboolean</link> *)</code>. Following this argument, the arguments that are normally
705 collected for the equivalent non-maybe type will be collected.
708 If <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> is given to
709 <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
710 the collected arguments are ignored. Otherwise (if <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> was
711 given), the arguments are used in the normal way to create the Just value.
714 If <link linkend='NULL:CAPS'><literal>NULL</literal></link> is given to
715 <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored. If a
716 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
717 whether the value was Just. In the case that the value was Just, the
718 <link linkend='gboolean'><type>gboolean</type></link> will be set to
719 <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
720 way. In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
721 <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
722 but have their values set to binary zero.
726 <title>Examples</title>
727 <informalexample><programlisting>
728 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
729 value1 = g_variant_new ("ms", "Hello world");
730 value2 = g_variant_new ("ms", NULL);
731 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
732 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done"); /* both '-1' are ignored. */
733 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
742 g_variant_get (value1, "ms", &str);
744 g_print ("str: %s\n", str);
746 g_print ("it was null\n");
750 g_variant_get (value2, "m&s", &cstr);
752 g_print ("str: %s\n", cstr);
754 g_print ("it was null\n");
755 /* don't free 'cstr' */
758 /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
759 g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
760 g_print ("string is %s\n", str);
763 /* note: &s used, so g_free() not needed */
764 g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
766 g_print ("it was (%d, %d)\n", x, y);
768 g_print ("it was null\n");
769 g_print ("string is %s\n", cstr);
770 /* don't free 'cstr' */
773 g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
774 if (contents != NULL)
776 g_variant_get (contents, "(ii)", &x, &y);
777 g_print ("it was (%d, %d)\n", x, y);
778 g_variant_unref (contents);
781 g_print ("it was null\n");
782 }]]></programlisting></informalexample>
786 <refsect2 id='gvariant-format-strings-tuples'>
787 <title>Tuples</title>
789 <emphasis role='strong'>
790 Characters: <code>()</code>
795 Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
799 <title>Examples</title>
800 <informalexample><programlisting>
801 <![CDATA[GVariant *value1, *value2;
803 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
804 value2 = g_variant_new ("()");
810 g_variant_get (value1, "(s(ii))", &string, &x, &y);
811 g_print ("%s, %d, %d\n", string, x, y);
814 g_variant_get (value2, "()"); /* do nothing... */
815 }]]></programlisting></informalexample>
819 <refsect2 id='gvariant-format-strings-dictionaries'>
820 <title>Dictionaries</title>
822 <emphasis role='strong'>
823 Characters: <code>{}</code>
828 Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
832 <title>Examples</title>
833 <informalexample><programlisting>
834 <![CDATA[GVariantBuilder *b;
837 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
838 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
839 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
840 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
844 <refsect2 id='gvariant-format-strings-gvariant'>
845 <title>GVariant *</title>
847 <emphasis role='strong'>
848 Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
853 Upon encountering a '<literal>@</literal>' in front of a type string,
854 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
855 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer to a
856 <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
857 create the value. The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
858 type string following the '<literal>@</literal>'. '<literal>*</literal>' is
859 the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
860 '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
861 <link linkend='GVariant'><type>GVariant</type></link> of any basic type). '<literal>r</literal>' is the same as
862 '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
865 Upon encountering a '<literal>@</literal>' in front of a type string,
866 <link linkend='g-variant-get'><function>g_variant_get()</function></link>
867 takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
868 <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
869 <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
870 C types in the usual way). <link linkend='NULL:CAPS'><literal>NULL</literal></link> can be given to ignore the
871 value. '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
872 what is stated above.
875 You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
876 use of '<literal>@</literal>'. Using the other characters where possible is recommended, however, due to the
877 improvements in type safety and code self-documentation.
881 <title>Examples</title>
882 <informalexample><programlisting>
883 <![CDATA[GVariant *value1, *value2;
885 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
887 /* note: consumes floating reference count on 'value1' */
888 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
896 g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
897 string = g_variant_get_string (tmp, &length);
898 g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
899 g_variant_unref (tmp);
901 /* quick way to skip all the values in a tuple */
902 g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
903 g_print ("i only got the string: %s\n", string);
905 }]]></programlisting></informalexample>
909 <refsect2 id='gvariant-format-strings-pointers'>
910 <title>Pointers</title>
912 <emphasis role='strong'>
913 Characters: <code>&</code>
918 The '<code>&</code>' character is used to indicate that serialised data should be directly exchanged via a
922 Currently, the only use for this character is when it is applied to a string (ie: '<literal>&s</literal>',
923 '<literal>&o</literal>' or '<code>&g</code>'). For
924 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect. The string
925 is collected and duplicated normally. For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
926 it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
927 returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
928 always be properly nul-terminated.
932 <title>Examples</title>
933 <informalexample><programlisting>
938 value = g_variant_new ("&s", "hello world");
939 str = g_variant_get (value, "&s", &str);
940 g_print ("string is: %s\n", str);
941 /* no need to free str */
942 }]]></programlisting></informalexample>
946 <refsect2 id='gvariant-format-strings-convenience'>
947 <title>Convenience Conversions</title>
949 <emphasis role='strong'>
950 Characters: <literal>^</literal>
955 The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
956 of strings or bytestrings. It has a number of forms.
960 In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
961 pointer value is collected from the variable arguments and passed to a function (as given in the table below).
962 The result of that function is used as the value for this position. When used with
963 <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
964 the function (given in the table) and returned by reference.
969 <colspec colname='col_0'/>
970 <colspec colname='col_1'/>
971 <colspec colname='col_2'/>
975 <entry colsep='1' rowsep='1'>
977 <emphasis role='strong'>Conversion</emphasis>
980 <entry colsep='1' rowsep='1'>
982 <emphasis role='strong'>
983 Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
987 <entry colsep='1' rowsep='1'>
989 <emphasis role='strong'>
990 Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
997 <entry colsep='1' rowsep='1'>
999 <emphasis role='strong'>
1000 <literal>^as</literal>
1004 <entry colsep='1' rowsep='1' morerows='1'>
1006 equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
1009 <entry colsep='1' rowsep='1'>
1011 equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1017 <entry colsep='1' rowsep='1'>
1019 <emphasis role='strong'>
1020 <literal>^a&s</literal>
1024 <entry colsep='1' rowsep='1'>
1026 equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1032 <entry colsep='1' rowsep='1'>
1034 <emphasis role='strong'>
1035 <literal>^ao</literal>
1039 <entry colsep='1' rowsep='1' morerows='1'>
1041 equivalent to <link linkend='g-variant-new-objv'><function>g_variant_new_objv()</function></link>
1044 <entry colsep='1' rowsep='1'>
1046 equivalent to <link linkend='g-variant-dup-objv'><function>g_variant_dup_objv()</function></link>
1052 <entry colsep='1' rowsep='1'>
1054 <emphasis role='strong'>
1055 <literal>^a&o</literal>
1059 <entry colsep='1' rowsep='1'>
1061 equivalent to <link linkend='g-variant-get-objv'><function>g_variant_get_objv()</function></link>
1067 <entry colsep='1' rowsep='1'>
1069 <emphasis role='strong'>
1070 <literal>^ay</literal>
1074 <entry colsep='1' rowsep='1' morerows='1'>
1076 equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1079 <entry colsep='1' rowsep='1'>
1081 equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1087 <entry colsep='1' rowsep='1'>
1089 <emphasis role='strong'>
1090 <literal>^&ay</literal>
1094 <entry colsep='1' rowsep='1'>
1096 equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1102 <entry colsep='1' rowsep='1'>
1104 <emphasis role='strong'>
1105 <literal>^aay</literal>
1109 <entry colsep='1' rowsep='1' morerows='1'>
1111 equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1114 <entry colsep='1' rowsep='1'>
1116 equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1122 <entry colsep='1' rowsep='1'>
1124 <emphasis role='strong'>
1125 <literal>^a&ay</literal>
1129 <entry colsep='1' rowsep='1'>
1131 equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>