1 <?xml version='1.0' encoding='utf-8'?>
3 <refentry id='gvariant-format-strings'>
5 <refentrytitle>GVariant Format Strings</refentrytitle>
9 <title>Variable Argument Conversions</title>
12 This page attempts to document how to perform variable argument conversions with GVariant.
15 Conversions occur according to format strings. A format string is a two-way mapping between a single
16 <link linkend='GVariant'>GVariant</link> value and one or more C values.
19 A conversion from C values into a <link linkend='GVariant'>GVariant</link> value is made using the
20 <link linkend='g-variant-new'><function>g_variant_new()</function></link> function. A conversion from a
21 <link linkend='GVariant'>GVariant</link> into C values is made using the
22 <link linkend='g-variant-get'><function>g_variant_get()</function></link> function.
30 This section exhaustively describes all possibilities for GVariant format strings. There are no valid forms of
31 format strings other than those described here. Please note that the format string syntax is likely to expand in the
35 Valid format strings have one of the following forms:
39 <para>any type string</para>
43 a type string prefixed with a '<literal>@</literal>'
48 '<literal>&s</literal>' '<literal>&o</literal>', '<literal>&g</literal>', '<literal>^as</literal>',
49 '<literal>^ao</literal>', '<literal>^ag</literal>', '<literal>^a&s</literal>', '<literal>^a&o</literal>' or
50 '<literal>^a&g</literal>'
55 any format string, prefixed with an '<literal>m</literal>'
60 a sequence of zero or more format strings strings, concatenated and enclosed in parentheses
65 an opening brace, followed by two format strings, followed by a closing brace (subject to the constraint that the
66 first format string correspond to a type valid for use as the key type of a dictionary)
72 <title>Symbols</title>
75 The following table describes the rough meaning of symbols that may appear inside a GVariant format string. Each
76 symbol is described in detail in its own section, including usage examples.
81 <colspec colname='col_0'/>
82 <colspec colname='col_1'/>
86 <entry colsep='1' rowsep='1'>
88 <emphasis role='strong'>Symbol</emphasis>
91 <entry colsep='1' rowsep='1'>
93 <emphasis role='strong'>Meaning</emphasis>
99 <entry colsep='1' rowsep='1'>
101 <emphasis role='strong'>
102 <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>, <literal>i</literal>,
103 <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>, <literal>d</literal>
107 <entry colsep='1' rowsep='1'>
109 Used for building or deconstructing boolean, byte and numeric types. See
110 <link linkend='gvariant-format-strings-numeric-types'>Numeric Types</link> below.
116 <entry colsep='1' rowsep='1'>
118 <emphasis role='strong'>
119 <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
123 <entry colsep='1' rowsep='1'>
125 Used for building or deconstructing string types. See
126 <link linkend='gvariant-format-strings-strings'>Strings</link> below.
132 <entry colsep='1' rowsep='1'>
134 <emphasis role='strong'><literal>v</literal></emphasis>
137 <entry colsep='1' rowsep='1'>
139 Used for building or deconstructing variant types. See
140 <link linkend='gvariant-format-strings-variant'>Variants</link> below.
146 <entry colsep='1' rowsep='1'>
148 <emphasis role='strong'>
153 <entry colsep='1' rowsep='1'>
155 Used for building or deconstructing arrays. See
156 <link linkend='gvariant-format-strings-arrays'>Arrays</link> below.
162 <entry colsep='1' rowsep='1'>
164 <emphasis role='strong'>
169 <entry colsep='1' rowsep='1'>
171 Used for building or deconstructing maybe types. See
172 <link linkend='gvariant-format-strings-maybe-types'>Maybe Types</link> below.
178 <entry colsep='1' rowsep='1'>
180 <emphasis role='strong'>
181 <literal>()</literal>
185 <entry colsep='1' rowsep='1'>
187 Used for building or deconstructing tuples. See
188 <link linkend='gvariant-format-strings-tuples'>Tuples</link> below.
194 <entry colsep='1' rowsep='1'>
196 <emphasis role='strong'>
197 <literal>{}</literal>
201 <entry colsep='1' rowsep='1'>
203 Used for building or deconstructing dictionary entries. See
204 <link linkend='gvariant-format-strings-dictionaries'>Dictionaries</link> below.
210 <entry colsep='1' rowsep='1'>
212 <emphasis role='strong'>
217 <entry colsep='1' rowsep='1'>
219 Used as a prefix on a GVariant type string (not format string). Denotes that a pointer to a
220 <link linkend='GVariant'>GVariant</link> should be used in place of the normal C type or types. For
221 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this means that you must pass a
222 non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> <code>(<link linkend='GVariant'>GVariant</link>
223 *)</code>. For <link linkend='g-variant-get'><function>g_variant_get()</function></link> this means that you
224 must pass a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> for the value to be returned
225 by reference or <link linkend='NULL--CAPS'><literal>NULL</literal></link> to ignore the value. See
226 <link linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
232 <entry colsep='1' rowsep='1'>
234 <emphasis role='strong'>
235 <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
239 <entry colsep='1' rowsep='1'>
241 Exactly equivalent to <literal>@*</literal>, <literal>@?</literal> and <literal>@r</literal>. Provided only for
242 completeness so that all GVariant type strings can be used also as format strings. See <link
243 linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
249 <entry colsep='1' rowsep='1'>
251 <emphasis role='strong'><literal>&</literal></emphasis>
254 <entry colsep='1' rowsep='1'>
256 Used as a prefix on a GVariant type string (not format string). Denotes that a C pointer to serialised data
257 should be used in place of the normal C type. See
258 <link linkend='gvariant-format-strings-pointers'>Pointers</link> below.
264 <entry colsep='1' rowsep='1'>
266 <emphasis role='strong'><literal>^</literal></emphasis>
269 <entry colsep='1' rowsep='1'>
271 Used as a prefix on some specific types of format strings. See
272 <link linkend='gvariant-format-strings-convenience'>Convenience Conversions</link> below.
281 <refsect2 id='gvariant-format-strings-numeric-types'>
282 <title>Numeric Types</title>
284 <emphasis role='strong'>
285 Characters: <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>,
286 <literal>i</literal>, <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>,
292 Variable argument conversions from numeric types work in the most obvious way possible. Upon encountering one of
293 these characters, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes the equivalent C
294 type as an argument. <link linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to
295 the equivalent C type (or <link linkend='NULL--CAPS'><literal>NULL</literal></link> to ignore the value).
299 The equivalent C types are as follows:
304 <colspec colname='col_0'/><colspec colname='col_1'/>
307 <entry colsep='1' rowsep='1'>
309 <emphasis role='strong'>Character</emphasis>
312 <entry colsep='1' rowsep='1'>
314 <emphasis role='strong'>Equivalent C type</emphasis>
319 <entry colsep='1' rowsep='1'>
321 <emphasis role='strong'>
326 <entry colsep='1' rowsep='1'>
328 <link linkend='gboolean'><type>gboolean</type></link>
333 <entry colsep='1' rowsep='1'>
335 <emphasis role='strong'>
340 <entry colsep='1' rowsep='1'>
342 <link linkend='guchar'><type>guchar</type></link>
347 <entry colsep='1' rowsep='1'>
349 <emphasis role='strong'>
354 <entry colsep='1' rowsep='1'>
356 <link linkend='gint16'><type>gint16</type></link>
361 <entry colsep='1' rowsep='1'>
363 <emphasis role='strong'>
368 <entry colsep='1' rowsep='1'>
370 <link linkend='guint16'><type>guint16</type></link>
375 <entry colsep='1' rowsep='1'>
377 <emphasis role='strong'>
382 <entry colsep='1' rowsep='1'>
384 <link linkend='gint32'><type>gint32</type></link>
389 <entry colsep='1' rowsep='1'>
391 <emphasis role='strong'>
396 <entry colsep='1' rowsep='1'>
398 <link linkend='guint32'><type>guint32</type></link>
403 <entry colsep='1' rowsep='1'>
405 <emphasis role='strong'>
410 <entry colsep='1' rowsep='1'>
412 <link linkend='gint64'><type>gint64</type></link>
417 <entry colsep='1' rowsep='1'>
419 <emphasis role='strong'>
424 <entry colsep='1' rowsep='1'>
426 <link linkend='guint64'><type>guint64</type></link>
431 <entry colsep='1' rowsep='1'>
433 <emphasis role='strong'>
438 <entry colsep='1' rowsep='1'>
440 <link linkend='gint32'><type>gint32</type></link>
445 <entry colsep='1' rowsep='1'>
447 <emphasis role='strong'>
452 <entry colsep='1' rowsep='1'>
454 <link linkend='gdouble'><type>gdouble</type></link>
463 Note that in C, small integer types in variable argument lists are promoted up to <link
464 linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
465 read back accordingly. <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
466 currently suported. This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
467 with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
468 '<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
469 '<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'. Specifically, you can use integer
470 literals with these characters.
474 When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
475 provide is 64 bit. This means that you should use a cast or make use of the
476 <link linkend='G-GINT64-CONSTANT--CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
477 <link linkend='G-GUINT64-CONSTANT--CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
481 No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
482 it operates with pointers. The pointers must always point to a memory region of exactly the correct size.
486 <title>Examples</title>
487 <informalexample><programlisting>
488 <![CDATA[GVariant *value1, *value2, *value3, *value4;
490 value1 = g_variant_new ("y", 200);
491 value2 = g_variant_new ("b", TRUE);
492 value3 = g_variant_new ("d", 37.5):
493 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
501 g_variant_get (value1, "y", NULL); /* ignore the value. */
502 g_variant_get (value2, "b", &truth);
503 g_variant_get (value3, "d", &floating);
504 g_variant_get (value4, "x", &bignum);
505 }]]></programlisting></informalexample>
509 <refsect2 id='gvariant-format-strings-strings'>
510 <title>Strings</title>
512 <emphasis role='strong'>
513 Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
518 String conversions occur to and from standard nul-terminated C strings. Upon encountering an
519 '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
520 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
521 <link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
522 <link linkend='NULL--CAPS'><literal>NULL</literal></link> is not a valid string. If the '<literal>o</literal>' or
523 '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
524 object path or DBus type signature, respectively.
527 Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
528 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
529 <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
530 sets it to a newly-allocated copy of the string. It is appropriate to free this copy using
531 <link linkend='g-free'><function>g_free()</function></link>.
532 <link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
533 string should be ignored (in which case no copy is made).
537 <title>Examples</title>
538 <informalexample><programlisting>
539 <![CDATA[GVariant *value1, *value2, *value3;
541 value1 = g_variant_new ("s", "hello world!");
542 value2 = g_variant_new ("o", "/must/be/a/valid/path");
543 value3 = g_variant_new ("g", "iias");
546 g_variant_new ("s", NULL); /* not valid: NULL is not a string. */
552 g_variant_get (value1, "s", &result);
553 g_print ("It was '%s'\n", result);
555 }]]></programlisting></informalexample>
559 <refsect2 id='gvariant-format-strings-variants'>
560 <title>Variants</title>
562 <emphasis role='strong'>
563 Characters: <literal>v</literal>
568 Upon encountering a '<literal>v</literal>',
569 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
570 linkend='GVariant'>GVariant</link> *)</code>. The value of the
571 <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
574 Upon encountering a '<literal>v</literal>', <link
575 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
576 <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
577 </code>). It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
578 containing the contents of the variant value. It is appropriate to free this reference using
579 <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
580 <link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
581 ignored (in which case no new reference is created).
585 <title>Examples</title>
586 <informalexample><programlisting>
587 <![CDATA[GVariant *x, *y;
589 /* the following two lines are equivalent: */
590 x = g_variant_new ("v", y);
591 x = g_variant_new_variant (y);
594 g_variant_get (x, "v", &y);
595 y = g_variant_get_variant (x);]]></programlisting></informalexample>
600 <refsect2 id='gvariant-format-strings-arrays'>
601 <title>Arrays</title>
603 <emphasis role='strong'>
604 Characters: <literal>a</literal>
609 Upon encountering an '<literal>a</literal>' character followed by a type string,
610 <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
611 <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
612 for an array of the type given in the type string. The builder will have
613 <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
614 result will be used as the value. As a special exception, if the given type string is a definite type, then
615 <link linkend='NULL--CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
619 Upon encountering an '<literal>a</literal>' character followed by a type string,
620 <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
621 <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
622 <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
623 A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
624 This iterator should be freed when you are done with it, using
625 <link linkend='g-variant-iter-unref'><function>g_variant_iter_unref()</function></link>.
626 <link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
631 <title>Examples</title>
632 <informalexample><programlisting>
633 <![CDATA[GVariantBuilder *builder;
636 builder = g_variant_builder_new (G_VARIANT_TYPE_CLASS_ARRAY, NULL);
637 g_variant_builder_add (builder, "s", "when");
638 g_variant_builder_add (builder, "s", "in");
639 g_variant_builder_add (builder, "s", "the");
640 g_variant_builder_add (builder, "s", "course");
641 value = g_variant_new ("as", builder);
647 g_variant_get (value, "as", &iter);
648 while (g_variant_iter_loop (iter, "s", &str))
650 g_variant_iter_unref (iter);
651 }]]></programlisting></informalexample>
655 <refsect2 id='gvariant-format-strings-maybe-types'>
656 <title>Maybe Types</title>
658 <emphasis role='strong'>
659 Characters: <literal>m</literal>
663 Maybe types are handled in two separate ways depending on the format string that follows the
664 '<literal>m</literal>'. The method that is used currently depends entirely on the character immediately following the
665 '<literal>m</literal>'.
669 The first way is used with format strings starting with '<literal>s</literal>', '<literal>o</literal>',
670 '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>', '<literal>*</literal>',
671 '<literal>?</literal>', '<literal>r</literal>', '<literal>&</literal>', or '<literal>^</literal>'. In all of
672 these cases, for non-maybe types, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes
673 a pointer to a non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> value and
674 <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
675 non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer. When any of these format strings are
676 prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
677 <link linkend='NULL--CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
680 The second way is used with all other format strings. For
681 <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
682 <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
683 <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
684 <code>(<link linkend='gboolean'>gboolean</link> *)</code>. Following this argument, the arguments that are normally
685 collected for the equivalent non-maybe type will be collected.
688 If <link linkend='FALSE--CAPS'><literal>FALSE</literal></link> is given to
689 <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
690 the collected arguments are ignored. Otherwise (if <link linkend='TRUE--CAPS'><literal>TRUE</literal></link> was
691 given), the arguments are used in the normal way to create the Just value.
694 If <link linkend='NULL--CAPS'><literal>NULL</literal></link> is given to
695 <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored. If a
696 non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
697 whether the value was Just. In the case that the value was Just, the
698 <link linkend='gboolean'><type>gboolean</type></link> will be set to
699 <link linkend='TRUE--CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
700 way. In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
701 <link linkend='FALSE--CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
702 but have their values set to binary zero.
706 <title>Examples</title>
707 <informalexample><programlisting>
708 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
709 value1 = g_variant_new ("ms", "Hello world");
710 value2 = g_variant_new ("ms", NULL);
711 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
712 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done"); /* both '-1' are ignored. */
713 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
722 g_variant_get (value1, "ms", &str);
724 g_print ("str: %s\n", str);
726 g_print ("it was null\n");
730 g_variant_get (value2, "m&s", &cstr);
732 g_print ("str: %s\n", cstr);
734 g_print ("it was null\n");
735 /* don't free 'cstr' */
738 /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
739 g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
740 g_print ("string is %s\n", str);
743 /* note: &s used, so g_free() not needed */
744 g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
746 g_print ("it was (%d, %d)\n", x, y);
748 g_print ("it was null\n");
749 g_print ("string is %s\n", cstr);
750 /* don't free 'cstr' */
753 g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
754 if (contents != NULL)
756 g_variant_get (contents, "(ii)", &x, &y);
757 g_print ("it was (%d, %d)\n", x, y);
758 g_variant_unref (contents);
761 g_print ("it was null\n");
762 }]]></programlisting></informalexample>
766 <refsect2 id='gvariant-format-strings-tuples'>
767 <title>Tuples</title>
769 <emphasis role='strong'>
770 Characters: <code>()</code>
775 Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
779 <title>Examples</title>
780 <informalexample><programlisting>
781 <![CDATA[GVariant *value1, *value2;
783 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
784 value2 = g_variant_new ("()");
790 g_variant_get (value1, "(s(ii))", &string, &x, &y);
791 g_print ("%s, %d, %d\n", string, x, y);
794 g_variant_get (value2, "()"); /* do nothing... */
795 }]]></programlisting></informalexample>
799 <refsect2 id='gvariant-format-strings-dictionaries'>
800 <title>Dictionaries</title>
802 <emphasis role='strong'>
803 Characters: <code>{}</code>
808 Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
812 <title>Examples</title>
813 <informalexample><programlisting>
814 <![CDATA[GVariantBuilder *b;
817 b = g_variant_builder_new (G_VARIANT_TYPE_CLASS_ARRAY, G_VARIANT_TYPE ("a{sv}"));
818 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
819 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
820 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
824 <refsect2 id='gvariant-format-strings-gvariant'>
825 <title>GVariant *</title>
827 <emphasis role='strong'>
828 Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
833 Upon encountering a '<literal>@</literal>' in front of a type string,
834 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
835 non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer to a
836 <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
837 create the value. The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
838 type string following the '<literal>@</literal>'. '<literal>*</literal>' is
839 the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
840 '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
841 <link linkend='GVariant'><type>GVariant</type></link> of any basic type). '<literal>r</literal>' is the same as
842 '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
845 Upon encountering a '<literal>@</literal>' in front of a type string,
846 <link linkend='g-variant-get'><function>g_variant_get()</function></link>
847 takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
848 <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
849 <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
850 C types in the usual way). <link linkend='NULL--CAPS'><literal>NULL</literal></link> can be given to ignore the
851 value. '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
852 what is stated above.
855 You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
856 use of '<literal>@</literal>'. Using the other characters where possible is recommended, however, due to the
857 improvements in type safety and code self-documentation.
861 <title>Examples</title>
862 <informalexample><programlisting>
863 <![CDATA[GVariant *value1, *value2;
865 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
867 /* note: consumes floating reference count on 'value1' */
868 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
876 g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
877 string = g_variant_get_string (tmp, &length);
878 g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
879 g_variant_unref (tmp);
881 /* quick way to skip all the values in a tuple */
882 g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
883 g_print ("i only got the string: %s\n", string);
885 }]]></programlisting></informalexample>
889 <refsect2 id='gvariant-format-strings-pointers'>
890 <title>Pointers</title>
892 <emphasis role='strong'>
893 Characters: <code>&</code>
898 The '<code>&</code>' character is used to indicate that serialised data should be directly exchanged via a
902 Currently, the only use for this character is when it is applied to a string (ie: '<literal>&s</literal>',
903 '<literal>&o</literal>' or '<code>&g</code>'). For
904 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect. The string
905 is collected and duplicated normally. For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
906 it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
907 returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
908 always be properly nul-terminated.
912 <title>Examples</title>
913 <informalexample><programlisting>
918 value = g_variant_new ("&s", "hello world");
919 str = g_variant_get ("&s", &str);
920 g_print ("string is: %s\n", str);
921 /* no need to free str */
922 }]]></programlisting></informalexample>
926 <refsect2 id='gvariant-format-strings-convenience'>
927 <title>Convenience Conversions</title>
929 <emphasis role='strong'>
930 Characters: <literal>^</literal>
935 The '<literal>^</literal>' character currently only has one purpose: to convert to and from
936 <link linkend='G-TYPE-STRV'><literal>G_TYPE_STRV</literal></link> type arrays of strings. It is always used with
937 arrays of strings (or other string types). It has two forms.
942 '<literal>^as</literal>' (or <literal>o</literal> or <literal>g</literal>)
947 '<literal>^a&s</literal>' (or <literal>o</literal> or <literal>g</literal>)
952 When used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> both forms are equivalent.
953 A <code>(const <link linkend='gchar'>gchar</link> * const *)</code> is collected. This must be a pointer to the
954 array of <link linkend='NULL--CAPS'><literal>NULL</literal></link>-terminated pointers to strings. This array is
955 converted to a <link linkend='GVariant'><type>GVariant</type></link> instance. Copies are made, so the original
956 array may be freed immediately.
959 When used with <link linkend='g-variant-get'><function>g_variant_get()</function></link> the two forms have
960 different meaning. Both return a freshly allocated
961 <link linkend='NULL--CAPS'><literal>NULL</literal></link>-terminated array of pointers to strings. In the case of
962 '<literal>^as</literal>', the strings are owned by the caller -- it is appropriate to free the array with
963 <link linkend='g-strfreev'><function>g_strfreev()</function></link>. In the case of '<literal>^a&s</literal>',
964 a shallow copy is made; the strings themselves are embedded in the serialised data and owned by the original
965 <link linkend='GVariant'><type>GVariant</type></link> instance -- it is only appropriate to free the outer array
966 with <link linkend='g-free'><function>g_free()</function></link>.