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 format string (not type 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 format string (not type 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>
469 <anchor id='gvariant-varargs'/>
471 Note that in C, small integer types in variable argument lists are promoted up to <link
472 linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
473 read back accordingly. <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
474 currently suported. This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
475 with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
476 '<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
477 '<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'. Specifically, you can use integer
478 literals with these characters.
482 When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
483 provide is 64 bit. This means that you should use a cast or make use of the
484 <link linkend='G-GINT64-CONSTANT:CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
485 <link linkend='G-GUINT64-CONSTANT:CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
489 No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
490 it operates with pointers. The pointers must always point to a memory region of exactly the correct size.
494 <title>Examples</title>
495 <informalexample><programlisting>
496 <![CDATA[GVariant *value1, *value2, *value3, *value4;
498 value1 = g_variant_new ("y", 200);
499 value2 = g_variant_new ("b", TRUE);
500 value3 = g_variant_new ("d", 37.5):
501 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
509 g_variant_get (value1, "y", NULL); /* ignore the value. */
510 g_variant_get (value2, "b", &truth);
511 g_variant_get (value3, "d", &floating);
512 g_variant_get (value4, "x", &bignum);
513 }]]></programlisting></informalexample>
517 <refsect2 id='gvariant-format-strings-strings'>
518 <title>Strings</title>
520 <emphasis role='strong'>
521 Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
526 String conversions occur to and from standard nul-terminated C strings. Upon encountering an
527 '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
528 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
529 <link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
530 <link linkend='NULL:CAPS'><literal>NULL</literal></link> is not a valid string. If the '<literal>o</literal>' or
531 '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
532 object path or DBus type signature, respectively.
535 Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
536 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
537 <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
538 sets it to a newly-allocated copy of the string. It is appropriate to free this copy using
539 <link linkend='g-free'><function>g_free()</function></link>.
540 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
541 string should be ignored (in which case no copy is made).
545 <title>Examples</title>
546 <informalexample><programlisting>
547 <![CDATA[GVariant *value1, *value2, *value3;
549 value1 = g_variant_new ("s", "hello world!");
550 value2 = g_variant_new ("o", "/must/be/a/valid/path");
551 value3 = g_variant_new ("g", "iias");
554 g_variant_new ("s", NULL); /* not valid: NULL is not a string. */
560 g_variant_get (value1, "s", &result);
561 g_print ("It was '%s'\n", result);
563 }]]></programlisting></informalexample>
567 <refsect2 id='gvariant-format-strings-variants'>
568 <title>Variants</title>
570 <emphasis role='strong'>
571 Characters: <literal>v</literal>
576 Upon encountering a '<literal>v</literal>',
577 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
578 linkend='GVariant'>GVariant</link> *)</code>. The value of the
579 <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
582 Upon encountering a '<literal>v</literal>', <link
583 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
584 <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
585 </code>). It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
586 containing the contents of the variant value. It is appropriate to free this reference using
587 <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
588 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
589 ignored (in which case no new reference is created).
593 <title>Examples</title>
594 <informalexample><programlisting>
595 <![CDATA[GVariant *x, *y;
597 /* the following two lines are equivalent: */
598 x = g_variant_new ("v", y);
599 x = g_variant_new_variant (y);
602 g_variant_get (x, "v", &y);
603 y = g_variant_get_variant (x);]]></programlisting></informalexample>
608 <refsect2 id='gvariant-format-strings-arrays'>
609 <title>Arrays</title>
611 <emphasis role='strong'>
612 Characters: <literal>a</literal>
617 Upon encountering an '<literal>a</literal>' character followed by a type string,
618 <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
619 <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
620 for an array of the type given in the type string. The builder will have
621 <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
622 result will be used as the value. As a special exception, if the given type string is a definite type, then
623 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
627 Upon encountering an '<literal>a</literal>' character followed by a type string,
628 <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
629 <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
630 <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
631 A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
632 This iterator should be freed when you are done with it, using
633 <link linkend='g-variant-iter-free'><function>g_variant_iter_free()</function></link>.
634 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
639 <title>Examples</title>
640 <informalexample><programlisting>
641 <![CDATA[GVariantBuilder *builder;
644 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
645 g_variant_builder_add (builder, "s", "when");
646 g_variant_builder_add (builder, "s", "in");
647 g_variant_builder_add (builder, "s", "the");
648 g_variant_builder_add (builder, "s", "course");
649 value = g_variant_new ("as", builder);
650 g_variant_builder_unref (builder);
656 g_variant_get (value, "as", &iter);
657 while (g_variant_iter_loop (iter, "s", &str))
658 g_print ("%s\n", str);
659 g_variant_iter_free (iter);
662 g_variant_unref (value);]]></programlisting></informalexample>
666 <refsect2 id='gvariant-format-strings-maybe-types'>
667 <title>Maybe Types</title>
669 <emphasis role='strong'>
670 Characters: <literal>m</literal>
674 Maybe types are handled in two separate ways depending on the format string that follows the
675 '<literal>m</literal>'. The method that is used currently depends entirely on the character immediately following the
676 '<literal>m</literal>'.
680 The first way is used with format strings starting with '<literal>a</literal>', '<literal>s</literal>',
681 '<literal>o</literal>', '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>',
682 '<literal>*</literal>', '<literal>?</literal>', '<literal>r</literal>', '<literal>&</literal>', or
683 '<literal>^</literal>'. In all of these cases, for non-maybe types,
684 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a pointer to a
685 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> value and
686 <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
687 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer. When any of these format strings are
688 prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
689 <link linkend='NULL:CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
692 Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
693 here. Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
694 the Nothing value -- not an empty array.
697 The second way is used with all other format strings. For
698 <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
699 <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
700 <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
701 <code>(<link linkend='gboolean'>gboolean</link> *)</code>. Following this argument, the arguments that are normally
702 collected for the equivalent non-maybe type will be collected.
705 If <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> is given to
706 <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
707 the collected arguments are ignored. Otherwise (if <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> was
708 given), the arguments are used in the normal way to create the Just value.
711 If <link linkend='NULL:CAPS'><literal>NULL</literal></link> is given to
712 <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored. If a
713 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
714 whether the value was Just. In the case that the value was Just, the
715 <link linkend='gboolean'><type>gboolean</type></link> will be set to
716 <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
717 way. In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
718 <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
719 but have their values set to binary zero.
723 <title>Examples</title>
724 <informalexample><programlisting>
725 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
726 value1 = g_variant_new ("ms", "Hello world");
727 value2 = g_variant_new ("ms", NULL);
728 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
729 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done"); /* both '-1' are ignored. */
730 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
739 g_variant_get (value1, "ms", &str);
741 g_print ("str: %s\n", str);
743 g_print ("it was null\n");
747 g_variant_get (value2, "m&s", &cstr);
749 g_print ("str: %s\n", cstr);
751 g_print ("it was null\n");
752 /* don't free 'cstr' */
755 /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
756 g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
757 g_print ("string is %s\n", str);
760 /* note: &s used, so g_free() not needed */
761 g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
763 g_print ("it was (%d, %d)\n", x, y);
765 g_print ("it was null\n");
766 g_print ("string is %s\n", cstr);
767 /* don't free 'cstr' */
770 g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
771 if (contents != NULL)
773 g_variant_get (contents, "(ii)", &x, &y);
774 g_print ("it was (%d, %d)\n", x, y);
775 g_variant_unref (contents);
778 g_print ("it was null\n");
779 }]]></programlisting></informalexample>
783 <refsect2 id='gvariant-format-strings-tuples'>
784 <title>Tuples</title>
786 <emphasis role='strong'>
787 Characters: <code>()</code>
792 Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
796 <title>Examples</title>
797 <informalexample><programlisting>
798 <![CDATA[GVariant *value1, *value2;
800 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
801 value2 = g_variant_new ("()");
807 g_variant_get (value1, "(s(ii))", &string, &x, &y);
808 g_print ("%s, %d, %d\n", string, x, y);
811 g_variant_get (value2, "()"); /* do nothing... */
812 }]]></programlisting></informalexample>
816 <refsect2 id='gvariant-format-strings-dictionaries'>
817 <title>Dictionaries</title>
819 <emphasis role='strong'>
820 Characters: <code>{}</code>
825 Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
829 <title>Examples</title>
830 <informalexample><programlisting>
831 <![CDATA[GVariantBuilder *b;
834 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
835 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
836 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
837 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
841 <refsect2 id='gvariant-format-strings-gvariant'>
842 <title>GVariant *</title>
844 <emphasis role='strong'>
845 Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
850 Upon encountering a '<literal>@</literal>' in front of a type string,
851 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
852 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer to a
853 <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
854 create the value. The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
855 type string following the '<literal>@</literal>'. '<literal>*</literal>' is
856 the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
857 '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
858 <link linkend='GVariant'><type>GVariant</type></link> of any basic type). '<literal>r</literal>' is the same as
859 '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
862 Upon encountering a '<literal>@</literal>' in front of a type string,
863 <link linkend='g-variant-get'><function>g_variant_get()</function></link>
864 takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
865 <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
866 <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
867 C types in the usual way). <link linkend='NULL:CAPS'><literal>NULL</literal></link> can be given to ignore the
868 value. '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
869 what is stated above.
872 You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
873 use of '<literal>@</literal>'. Using the other characters where possible is recommended, however, due to the
874 improvements in type safety and code self-documentation.
878 <title>Examples</title>
879 <informalexample><programlisting>
880 <![CDATA[GVariant *value1, *value2;
882 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
884 /* note: consumes floating reference count on 'value1' */
885 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
893 g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
894 string = g_variant_get_string (tmp, &length);
895 g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
896 g_variant_unref (tmp);
898 /* quick way to skip all the values in a tuple */
899 g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
900 g_print ("i only got the string: %s\n", string);
902 }]]></programlisting></informalexample>
906 <refsect2 id='gvariant-format-strings-pointers'>
907 <title>Pointers</title>
909 <emphasis role='strong'>
910 Characters: <code>&</code>
915 The '<code>&</code>' character is used to indicate that serialised data should be directly exchanged via a
919 Currently, the only use for this character is when it is applied to a string (ie: '<literal>&s</literal>',
920 '<literal>&o</literal>' or '<code>&g</code>'). For
921 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect. The string
922 is collected and duplicated normally. For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
923 it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
924 returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
925 always be properly nul-terminated.
929 <title>Examples</title>
930 <informalexample><programlisting>
935 value = g_variant_new ("&s", "hello world");
936 str = g_variant_get (value, "&s", &str);
937 g_print ("string is: %s\n", str);
938 /* no need to free str */
939 }]]></programlisting></informalexample>
943 <refsect2 id='gvariant-format-strings-convenience'>
944 <title>Convenience Conversions</title>
946 <emphasis role='strong'>
947 Characters: <literal>^</literal>
952 The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
953 of strings or bytestrings. It has a number of forms.
957 In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
958 pointer value is collected from the variable arguments and passed to a function (as given in the table below).
959 The result of that function is used as the value for this position. When used with
960 <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
961 the function (given in the table) and returned by reference.
966 <colspec colname='col_0'/>
967 <colspec colname='col_1'/>
968 <colspec colname='col_2'/>
972 <entry colsep='1' rowsep='1'>
974 <emphasis role='strong'>Conversion</emphasis>
977 <entry colsep='1' rowsep='1'>
979 <emphasis role='strong'>
980 Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
984 <entry colsep='1' rowsep='1'>
986 <emphasis role='strong'>
987 Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
994 <entry colsep='1' rowsep='1'>
996 <emphasis role='strong'>
997 <literal>^as</literal>
1001 <entry colsep='1' rowsep='1' morerows='1'>
1003 equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
1006 <entry colsep='1' rowsep='1'>
1008 equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1014 <entry colsep='1' rowsep='1'>
1016 <emphasis role='strong'>
1017 <literal>^a&s</literal>
1021 <entry colsep='1' rowsep='1'>
1023 equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1029 <entry colsep='1' rowsep='1'>
1031 <emphasis role='strong'>
1032 <literal>^ao</literal>
1036 <entry colsep='1' rowsep='1' morerows='1'>
1038 equivalent to <link linkend='g-variant-new-objv'><function>g_variant_new_objv()</function></link>
1041 <entry colsep='1' rowsep='1'>
1043 equivalent to <link linkend='g-variant-dup-objv'><function>g_variant_dup_objv()</function></link>
1049 <entry colsep='1' rowsep='1'>
1051 <emphasis role='strong'>
1052 <literal>^a&o</literal>
1056 <entry colsep='1' rowsep='1'>
1058 equivalent to <link linkend='g-variant-get-objv'><function>g_variant_get_objv()</function></link>
1064 <entry colsep='1' rowsep='1'>
1066 <emphasis role='strong'>
1067 <literal>^ay</literal>
1071 <entry colsep='1' rowsep='1' morerows='1'>
1073 equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1076 <entry colsep='1' rowsep='1'>
1078 equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1084 <entry colsep='1' rowsep='1'>
1086 <emphasis role='strong'>
1087 <literal>^&ay</literal>
1091 <entry colsep='1' rowsep='1'>
1093 equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1099 <entry colsep='1' rowsep='1'>
1101 <emphasis role='strong'>
1102 <literal>^aay</literal>
1106 <entry colsep='1' rowsep='1' morerows='1'>
1108 equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1111 <entry colsep='1' rowsep='1'>
1113 equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1119 <entry colsep='1' rowsep='1'>
1121 <emphasis role='strong'>
1122 <literal>^a&ay</literal>
1126 <entry colsep='1' rowsep='1'>
1128 equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>