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 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 on a GVariant type string (not format string). Denotes that a pointer to a
225 <link linkend='GVariant'>GVariant</link> should be used in place of the normal C type or types. For
226 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this means that you must pass a
227 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> <code>(<link linkend='GVariant'>GVariant</link>
228 *)</code>; if it is a floating reference, ownership will be taken, as
229 if by using <link linkend="g-variant-ref-sink"><function>g_variant_ref_sink()</function></link>.
230 For <link linkend='g-variant-get'><function>g_variant_get()</function></link> this means that you
231 must pass a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> for the value to be returned
232 by reference or <link linkend='NULL:CAPS'><literal>NULL</literal></link> to ignore the value. See
233 <link linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
239 <entry colsep='1' rowsep='1'>
241 <emphasis role='strong'>
242 <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
246 <entry colsep='1' rowsep='1'>
248 Exactly equivalent to <literal>@*</literal>, <literal>@?</literal> and <literal>@r</literal>. Provided only for
249 completeness so that all GVariant type strings can be used also as format strings. See <link
250 linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
256 <entry colsep='1' rowsep='1'>
258 <emphasis role='strong'><literal>&</literal></emphasis>
261 <entry colsep='1' rowsep='1'>
263 Used as a prefix on a GVariant type string (not format string). Denotes that a C pointer to serialised data
264 should be used in place of the normal C type. See
265 <link linkend='gvariant-format-strings-pointers'>Pointers</link> below.
271 <entry colsep='1' rowsep='1'>
273 <emphasis role='strong'><literal>^</literal></emphasis>
276 <entry colsep='1' rowsep='1'>
278 Used as a prefix on some specific types of format strings. See
279 <link linkend='gvariant-format-strings-convenience'>Convenience Conversions</link> below.
288 <refsect2 id='gvariant-format-strings-numeric-types'>
289 <title>Numeric Types</title>
291 <emphasis role='strong'>
292 Characters: <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>,
293 <literal>i</literal>, <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>,
299 Variable argument conversions from numeric types work in the most obvious way possible. Upon encountering one of
300 these characters, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes the equivalent C
301 type as an argument. <link linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to
302 the equivalent C type (or <link linkend='NULL:CAPS'><literal>NULL</literal></link> to ignore the value).
306 The equivalent C types are as follows:
311 <colspec colname='col_0'/><colspec colname='col_1'/>
314 <entry colsep='1' rowsep='1'>
316 <emphasis role='strong'>Character</emphasis>
319 <entry colsep='1' rowsep='1'>
321 <emphasis role='strong'>Equivalent C type</emphasis>
326 <entry colsep='1' rowsep='1'>
328 <emphasis role='strong'>
333 <entry colsep='1' rowsep='1'>
335 <link linkend='gboolean'><type>gboolean</type></link>
340 <entry colsep='1' rowsep='1'>
342 <emphasis role='strong'>
347 <entry colsep='1' rowsep='1'>
349 <link linkend='guchar'><type>guchar</type></link>
354 <entry colsep='1' rowsep='1'>
356 <emphasis role='strong'>
361 <entry colsep='1' rowsep='1'>
363 <link linkend='gint16'><type>gint16</type></link>
368 <entry colsep='1' rowsep='1'>
370 <emphasis role='strong'>
375 <entry colsep='1' rowsep='1'>
377 <link linkend='guint16'><type>guint16</type></link>
382 <entry colsep='1' rowsep='1'>
384 <emphasis role='strong'>
389 <entry colsep='1' rowsep='1'>
391 <link linkend='gint32'><type>gint32</type></link>
396 <entry colsep='1' rowsep='1'>
398 <emphasis role='strong'>
403 <entry colsep='1' rowsep='1'>
405 <link linkend='guint32'><type>guint32</type></link>
410 <entry colsep='1' rowsep='1'>
412 <emphasis role='strong'>
417 <entry colsep='1' rowsep='1'>
419 <link linkend='gint64'><type>gint64</type></link>
424 <entry colsep='1' rowsep='1'>
426 <emphasis role='strong'>
431 <entry colsep='1' rowsep='1'>
433 <link linkend='guint64'><type>guint64</type></link>
438 <entry colsep='1' rowsep='1'>
440 <emphasis role='strong'>
445 <entry colsep='1' rowsep='1'>
447 <link linkend='gint32'><type>gint32</type></link>
452 <entry colsep='1' rowsep='1'>
454 <emphasis role='strong'>
459 <entry colsep='1' rowsep='1'>
461 <link linkend='gdouble'><type>gdouble</type></link>
470 Note that in C, small integer types in variable argument lists are promoted up to <link
471 linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
472 read back accordingly. <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
473 currently suported. This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
474 with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
475 '<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
476 '<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'. Specifically, you can use integer
477 literals with these characters.
481 When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
482 provide is 64 bit. This means that you should use a cast or make use of the
483 <link linkend='G-GINT64-CONSTANT:CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
484 <link linkend='G-GUINT64-CONSTANT:CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
488 No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
489 it operates with pointers. The pointers must always point to a memory region of exactly the correct size.
493 <title>Examples</title>
494 <informalexample><programlisting>
495 <![CDATA[GVariant *value1, *value2, *value3, *value4;
497 value1 = g_variant_new ("y", 200);
498 value2 = g_variant_new ("b", TRUE);
499 value3 = g_variant_new ("d", 37.5):
500 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
508 g_variant_get (value1, "y", NULL); /* ignore the value. */
509 g_variant_get (value2, "b", &truth);
510 g_variant_get (value3, "d", &floating);
511 g_variant_get (value4, "x", &bignum);
512 }]]></programlisting></informalexample>
516 <refsect2 id='gvariant-format-strings-strings'>
517 <title>Strings</title>
519 <emphasis role='strong'>
520 Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
525 String conversions occur to and from standard nul-terminated C strings. Upon encountering an
526 '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
527 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
528 <link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
529 <link linkend='NULL:CAPS'><literal>NULL</literal></link> is not a valid string. If the '<literal>o</literal>' or
530 '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
531 object path or DBus type signature, respectively.
534 Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
535 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
536 <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
537 sets it to a newly-allocated copy of the string. It is appropriate to free this copy using
538 <link linkend='g-free'><function>g_free()</function></link>.
539 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
540 string should be ignored (in which case no copy is made).
544 <title>Examples</title>
545 <informalexample><programlisting>
546 <![CDATA[GVariant *value1, *value2, *value3;
548 value1 = g_variant_new ("s", "hello world!");
549 value2 = g_variant_new ("o", "/must/be/a/valid/path");
550 value3 = g_variant_new ("g", "iias");
553 g_variant_new ("s", NULL); /* not valid: NULL is not a string. */
559 g_variant_get (value1, "s", &result);
560 g_print ("It was '%s'\n", result);
562 }]]></programlisting></informalexample>
566 <refsect2 id='gvariant-format-strings-variants'>
567 <title>Variants</title>
569 <emphasis role='strong'>
570 Characters: <literal>v</literal>
575 Upon encountering a '<literal>v</literal>',
576 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
577 linkend='GVariant'>GVariant</link> *)</code>. The value of the
578 <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
581 Upon encountering a '<literal>v</literal>', <link
582 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
583 <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
584 </code>). It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
585 containing the contents of the variant value. It is appropriate to free this reference using
586 <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
587 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
588 ignored (in which case no new reference is created).
592 <title>Examples</title>
593 <informalexample><programlisting>
594 <![CDATA[GVariant *x, *y;
596 /* the following two lines are equivalent: */
597 x = g_variant_new ("v", y);
598 x = g_variant_new_variant (y);
601 g_variant_get (x, "v", &y);
602 y = g_variant_get_variant (x);]]></programlisting></informalexample>
607 <refsect2 id='gvariant-format-strings-arrays'>
608 <title>Arrays</title>
610 <emphasis role='strong'>
611 Characters: <literal>a</literal>
616 Upon encountering an '<literal>a</literal>' character followed by a type string,
617 <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
618 <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
619 for an array of the type given in the type string. The builder will have
620 <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
621 result will be used as the value. As a special exception, if the given type string is a definite type, then
622 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
626 Upon encountering an '<literal>a</literal>' character followed by a type string,
627 <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
628 <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
629 <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
630 A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
631 This iterator should be freed when you are done with it, using
632 <link linkend='g-variant-iter-free'><function>g_variant_iter_free()</function></link>.
633 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
638 <title>Examples</title>
639 <informalexample><programlisting>
640 <![CDATA[GVariantBuilder *builder;
643 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
644 g_variant_builder_add (builder, "s", "when");
645 g_variant_builder_add (builder, "s", "in");
646 g_variant_builder_add (builder, "s", "the");
647 g_variant_builder_add (builder, "s", "course");
648 value = g_variant_new ("as", builder);
649 g_variant_builder_unref (builder);
655 g_variant_get (value, "as", &iter);
656 while (g_variant_iter_loop (iter, "s", &str))
657 g_print ("%s\n", str);
658 g_variant_iter_free (iter);
661 g_variant_unref (value);]]></programlisting></informalexample>
665 <refsect2 id='gvariant-format-strings-maybe-types'>
666 <title>Maybe Types</title>
668 <emphasis role='strong'>
669 Characters: <literal>m</literal>
673 Maybe types are handled in two separate ways depending on the format string that follows the
674 '<literal>m</literal>'. The method that is used currently depends entirely on the character immediately following the
675 '<literal>m</literal>'.
679 The first way is used with format strings starting with '<literal>a</literal>', '<literal>s</literal>',
680 '<literal>o</literal>', '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>',
681 '<literal>*</literal>', '<literal>?</literal>', '<literal>r</literal>', '<literal>&</literal>', or
682 '<literal>^</literal>'. In all of these cases, for non-maybe types,
683 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a pointer to a
684 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> value and
685 <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
686 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer. When any of these format strings are
687 prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
688 <link linkend='NULL:CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
691 Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
692 here. Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
693 the Nothing value -- not an empty array.
696 The second way is used with all other format strings. For
697 <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
698 <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
699 <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
700 <code>(<link linkend='gboolean'>gboolean</link> *)</code>. Following this argument, the arguments that are normally
701 collected for the equivalent non-maybe type will be collected.
704 If <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> is given to
705 <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
706 the collected arguments are ignored. Otherwise (if <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> was
707 given), the arguments are used in the normal way to create the Just value.
710 If <link linkend='NULL:CAPS'><literal>NULL</literal></link> is given to
711 <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored. If a
712 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
713 whether the value was Just. In the case that the value was Just, the
714 <link linkend='gboolean'><type>gboolean</type></link> will be set to
715 <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
716 way. In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
717 <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
718 but have their values set to binary zero.
722 <title>Examples</title>
723 <informalexample><programlisting>
724 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
725 value1 = g_variant_new ("ms", "Hello world");
726 value2 = g_variant_new ("ms", NULL);
727 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
728 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done"); /* both '-1' are ignored. */
729 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
738 g_variant_get (value1, "ms", &str);
740 g_print ("str: %s\n", str);
742 g_print ("it was null\n");
746 g_variant_get (value2, "m&s", &cstr);
748 g_print ("str: %s\n", cstr);
750 g_print ("it was null\n");
751 /* don't free 'cstr' */
754 /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
755 g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
756 g_print ("string is %s\n", str);
759 /* note: &s used, so g_free() not needed */
760 g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
762 g_print ("it was (%d, %d)\n", x, y);
764 g_print ("it was null\n");
765 g_print ("string is %s\n", cstr);
766 /* don't free 'cstr' */
769 g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
770 if (contents != NULL)
772 g_variant_get (contents, "(ii)", &x, &y);
773 g_print ("it was (%d, %d)\n", x, y);
774 g_variant_unref (contents);
777 g_print ("it was null\n");
778 }]]></programlisting></informalexample>
782 <refsect2 id='gvariant-format-strings-tuples'>
783 <title>Tuples</title>
785 <emphasis role='strong'>
786 Characters: <code>()</code>
791 Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
795 <title>Examples</title>
796 <informalexample><programlisting>
797 <![CDATA[GVariant *value1, *value2;
799 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
800 value2 = g_variant_new ("()");
806 g_variant_get (value1, "(s(ii))", &string, &x, &y);
807 g_print ("%s, %d, %d\n", string, x, y);
810 g_variant_get (value2, "()"); /* do nothing... */
811 }]]></programlisting></informalexample>
815 <refsect2 id='gvariant-format-strings-dictionaries'>
816 <title>Dictionaries</title>
818 <emphasis role='strong'>
819 Characters: <code>{}</code>
824 Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
828 <title>Examples</title>
829 <informalexample><programlisting>
830 <![CDATA[GVariantBuilder *b;
833 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
834 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
835 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
836 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
840 <refsect2 id='gvariant-format-strings-gvariant'>
841 <title>GVariant *</title>
843 <emphasis role='strong'>
844 Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
849 Upon encountering a '<literal>@</literal>' in front of a type string,
850 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
851 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer to a
852 <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
853 create the value. The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
854 type string following the '<literal>@</literal>'. '<literal>*</literal>' is
855 the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
856 '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
857 <link linkend='GVariant'><type>GVariant</type></link> of any basic type). '<literal>r</literal>' is the same as
858 '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
861 Upon encountering a '<literal>@</literal>' in front of a type string,
862 <link linkend='g-variant-get'><function>g_variant_get()</function></link>
863 takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
864 <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
865 <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
866 C types in the usual way). <link linkend='NULL:CAPS'><literal>NULL</literal></link> can be given to ignore the
867 value. '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
868 what is stated above.
871 You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
872 use of '<literal>@</literal>'. Using the other characters where possible is recommended, however, due to the
873 improvements in type safety and code self-documentation.
877 <title>Examples</title>
878 <informalexample><programlisting>
879 <![CDATA[GVariant *value1, *value2;
881 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
883 /* note: consumes floating reference count on 'value1' */
884 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
892 g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
893 string = g_variant_get_string (tmp, &length);
894 g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
895 g_variant_unref (tmp);
897 /* quick way to skip all the values in a tuple */
898 g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
899 g_print ("i only got the string: %s\n", string);
901 }]]></programlisting></informalexample>
905 <refsect2 id='gvariant-format-strings-pointers'>
906 <title>Pointers</title>
908 <emphasis role='strong'>
909 Characters: <code>&</code>
914 The '<code>&</code>' character is used to indicate that serialised data should be directly exchanged via a
918 Currently, the only use for this character is when it is applied to a string (ie: '<literal>&s</literal>',
919 '<literal>&o</literal>' or '<code>&g</code>'). For
920 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect. The string
921 is collected and duplicated normally. For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
922 it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
923 returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
924 always be properly nul-terminated.
928 <title>Examples</title>
929 <informalexample><programlisting>
934 value = g_variant_new ("&s", "hello world");
935 str = g_variant_get ("&s", &str);
936 g_print ("string is: %s\n", str);
937 /* no need to free str */
938 }]]></programlisting></informalexample>
942 <refsect2 id='gvariant-format-strings-convenience'>
943 <title>Convenience Conversions</title>
945 <emphasis role='strong'>
946 Characters: <literal>^</literal>
951 The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
952 of strings or bytestrings. It has a number of forms.
956 In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
957 pointer value is collected from the variable arguments and passed to a function (as given in the table below).
958 The result of that function is used as the value for this position. When used with
959 <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
960 the function (given in the table) and returned by reference.
965 <colspec colname='col_0'/>
966 <colspec colname='col_1'/>
967 <colspec colname='col_2'/>
971 <entry colsep='1' rowsep='1'>
973 <emphasis role='strong'>Conversion</emphasis>
976 <entry colsep='1' rowsep='1'>
978 <emphasis role='strong'>
979 Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
983 <entry colsep='1' rowsep='1'>
985 <emphasis role='strong'>
986 Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
993 <entry colsep='1' rowsep='1'>
995 <emphasis role='strong'>
996 <literal>^as</literal>
1000 <entry colsep='1' rowsep='1' morerows='1'>
1002 equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
1005 <entry colsep='1' rowsep='1'>
1007 equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1013 <entry colsep='1' rowsep='1'>
1015 <emphasis role='strong'>
1016 <literal>^a&s</literal>
1020 <entry colsep='1' rowsep='1'>
1022 equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1028 <entry colsep='1' rowsep='1'>
1030 <emphasis role='strong'>
1031 <literal>^ao</literal>
1035 <entry colsep='1' rowsep='1' morerows='1'>
1037 equivalent to <link linkend='g-variant-new-objv'><function>g_variant_new_objv()</function></link>
1040 <entry colsep='1' rowsep='1'>
1042 equivalent to <link linkend='g-variant-dup-objv'><function>g_variant_dup_objv()</function></link>
1048 <entry colsep='1' rowsep='1'>
1050 <emphasis role='strong'>
1051 <literal>^a&o</literal>
1055 <entry colsep='1' rowsep='1'>
1057 equivalent to <link linkend='g-variant-get-objv'><function>g_variant_get_objv()</function></link>
1063 <entry colsep='1' rowsep='1'>
1065 <emphasis role='strong'>
1066 <literal>^ay</literal>
1070 <entry colsep='1' rowsep='1' morerows='1'>
1072 equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1075 <entry colsep='1' rowsep='1'>
1077 equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1083 <entry colsep='1' rowsep='1'>
1085 <emphasis role='strong'>
1086 <literal>^&ay</literal>
1090 <entry colsep='1' rowsep='1'>
1092 equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1098 <entry colsep='1' rowsep='1'>
1100 <emphasis role='strong'>
1101 <literal>^aay</literal>
1105 <entry colsep='1' rowsep='1' morerows='1'>
1107 equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1110 <entry colsep='1' rowsep='1'>
1112 equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1118 <entry colsep='1' rowsep='1'>
1120 <emphasis role='strong'>
1121 <literal>^a&ay</literal>
1125 <entry colsep='1' rowsep='1'>
1127 equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>