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>^a&s</literal>', '<literal>^ay</literal>', '<literal>^&ay</literal>', '<literal>^aay</literal>'
50 or '<literal>^a&ay</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-variants'>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>; if it is a floating reference, ownership will be taken, as
224 if by using <link linkend="g-variant-ref-sink"><function>g_variant_ref_sink()</function></link>.
225 For <link linkend='g-variant-get'><function>g_variant_get()</function></link> this means that you
226 must pass a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> for the value to be returned
227 by reference or <link linkend='NULL--CAPS'><literal>NULL</literal></link> to ignore the value. See
228 <link linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
234 <entry colsep='1' rowsep='1'>
236 <emphasis role='strong'>
237 <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
241 <entry colsep='1' rowsep='1'>
243 Exactly equivalent to <literal>@*</literal>, <literal>@?</literal> and <literal>@r</literal>. Provided only for
244 completeness so that all GVariant type strings can be used also as format strings. See <link
245 linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
251 <entry colsep='1' rowsep='1'>
253 <emphasis role='strong'><literal>&</literal></emphasis>
256 <entry colsep='1' rowsep='1'>
258 Used as a prefix on a GVariant type string (not format string). Denotes that a C pointer to serialised data
259 should be used in place of the normal C type. See
260 <link linkend='gvariant-format-strings-pointers'>Pointers</link> below.
266 <entry colsep='1' rowsep='1'>
268 <emphasis role='strong'><literal>^</literal></emphasis>
271 <entry colsep='1' rowsep='1'>
273 Used as a prefix on some specific types of format strings. See
274 <link linkend='gvariant-format-strings-convenience'>Convenience Conversions</link> below.
283 <refsect2 id='gvariant-format-strings-numeric-types'>
284 <title>Numeric Types</title>
286 <emphasis role='strong'>
287 Characters: <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>,
288 <literal>i</literal>, <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>,
294 Variable argument conversions from numeric types work in the most obvious way possible. Upon encountering one of
295 these characters, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes the equivalent C
296 type as an argument. <link linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to
297 the equivalent C type (or <link linkend='NULL--CAPS'><literal>NULL</literal></link> to ignore the value).
301 The equivalent C types are as follows:
306 <colspec colname='col_0'/><colspec colname='col_1'/>
309 <entry colsep='1' rowsep='1'>
311 <emphasis role='strong'>Character</emphasis>
314 <entry colsep='1' rowsep='1'>
316 <emphasis role='strong'>Equivalent C type</emphasis>
321 <entry colsep='1' rowsep='1'>
323 <emphasis role='strong'>
328 <entry colsep='1' rowsep='1'>
330 <link linkend='gboolean'><type>gboolean</type></link>
335 <entry colsep='1' rowsep='1'>
337 <emphasis role='strong'>
342 <entry colsep='1' rowsep='1'>
344 <link linkend='guchar'><type>guchar</type></link>
349 <entry colsep='1' rowsep='1'>
351 <emphasis role='strong'>
356 <entry colsep='1' rowsep='1'>
358 <link linkend='gint16'><type>gint16</type></link>
363 <entry colsep='1' rowsep='1'>
365 <emphasis role='strong'>
370 <entry colsep='1' rowsep='1'>
372 <link linkend='guint16'><type>guint16</type></link>
377 <entry colsep='1' rowsep='1'>
379 <emphasis role='strong'>
384 <entry colsep='1' rowsep='1'>
386 <link linkend='gint32'><type>gint32</type></link>
391 <entry colsep='1' rowsep='1'>
393 <emphasis role='strong'>
398 <entry colsep='1' rowsep='1'>
400 <link linkend='guint32'><type>guint32</type></link>
405 <entry colsep='1' rowsep='1'>
407 <emphasis role='strong'>
412 <entry colsep='1' rowsep='1'>
414 <link linkend='gint64'><type>gint64</type></link>
419 <entry colsep='1' rowsep='1'>
421 <emphasis role='strong'>
426 <entry colsep='1' rowsep='1'>
428 <link linkend='guint64'><type>guint64</type></link>
433 <entry colsep='1' rowsep='1'>
435 <emphasis role='strong'>
440 <entry colsep='1' rowsep='1'>
442 <link linkend='gint32'><type>gint32</type></link>
447 <entry colsep='1' rowsep='1'>
449 <emphasis role='strong'>
454 <entry colsep='1' rowsep='1'>
456 <link linkend='gdouble'><type>gdouble</type></link>
465 Note that in C, small integer types in variable argument lists are promoted up to <link
466 linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
467 read back accordingly. <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
468 currently suported. This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
469 with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
470 '<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
471 '<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'. Specifically, you can use integer
472 literals with these characters.
476 When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
477 provide is 64 bit. This means that you should use a cast or make use of the
478 <link linkend='G-GINT64-CONSTANT--CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
479 <link linkend='G-GUINT64-CONSTANT--CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
483 No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
484 it operates with pointers. The pointers must always point to a memory region of exactly the correct size.
488 <title>Examples</title>
489 <informalexample><programlisting>
490 <![CDATA[GVariant *value1, *value2, *value3, *value4;
492 value1 = g_variant_new ("y", 200);
493 value2 = g_variant_new ("b", TRUE);
494 value3 = g_variant_new ("d", 37.5):
495 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
503 g_variant_get (value1, "y", NULL); /* ignore the value. */
504 g_variant_get (value2, "b", &truth);
505 g_variant_get (value3, "d", &floating);
506 g_variant_get (value4, "x", &bignum);
507 }]]></programlisting></informalexample>
511 <refsect2 id='gvariant-format-strings-strings'>
512 <title>Strings</title>
514 <emphasis role='strong'>
515 Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
520 String conversions occur to and from standard nul-terminated C strings. Upon encountering an
521 '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
522 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
523 <link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
524 <link linkend='NULL--CAPS'><literal>NULL</literal></link> is not a valid string. If the '<literal>o</literal>' or
525 '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
526 object path or DBus type signature, respectively.
529 Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
530 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
531 <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
532 sets it to a newly-allocated copy of the string. It is appropriate to free this copy using
533 <link linkend='g-free'><function>g_free()</function></link>.
534 <link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
535 string should be ignored (in which case no copy is made).
539 <title>Examples</title>
540 <informalexample><programlisting>
541 <![CDATA[GVariant *value1, *value2, *value3;
543 value1 = g_variant_new ("s", "hello world!");
544 value2 = g_variant_new ("o", "/must/be/a/valid/path");
545 value3 = g_variant_new ("g", "iias");
548 g_variant_new ("s", NULL); /* not valid: NULL is not a string. */
554 g_variant_get (value1, "s", &result);
555 g_print ("It was '%s'\n", result);
557 }]]></programlisting></informalexample>
561 <refsect2 id='gvariant-format-strings-variants'>
562 <title>Variants</title>
564 <emphasis role='strong'>
565 Characters: <literal>v</literal>
570 Upon encountering a '<literal>v</literal>',
571 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
572 linkend='GVariant'>GVariant</link> *)</code>. The value of the
573 <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
576 Upon encountering a '<literal>v</literal>', <link
577 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
578 <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
579 </code>). It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
580 containing the contents of the variant value. It is appropriate to free this reference using
581 <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
582 <link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
583 ignored (in which case no new reference is created).
587 <title>Examples</title>
588 <informalexample><programlisting>
589 <![CDATA[GVariant *x, *y;
591 /* the following two lines are equivalent: */
592 x = g_variant_new ("v", y);
593 x = g_variant_new_variant (y);
596 g_variant_get (x, "v", &y);
597 y = g_variant_get_variant (x);]]></programlisting></informalexample>
602 <refsect2 id='gvariant-format-strings-arrays'>
603 <title>Arrays</title>
605 <emphasis role='strong'>
606 Characters: <literal>a</literal>
611 Upon encountering an '<literal>a</literal>' character followed by a type string,
612 <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
613 <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
614 for an array of the type given in the type string. The builder will have
615 <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
616 result will be used as the value. As a special exception, if the given type string is a definite type, then
617 <link linkend='NULL--CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
621 Upon encountering an '<literal>a</literal>' character followed by a type string,
622 <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
623 <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
624 <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
625 A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
626 This iterator should be freed when you are done with it, using
627 <link linkend='g-variant-iter-free'><function>g_variant_iter_free()</function></link>.
628 <link linkend='NULL--CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
633 <title>Examples</title>
634 <informalexample><programlisting>
635 <![CDATA[GVariantBuilder *builder;
638 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
639 g_variant_builder_add (builder, "s", "when");
640 g_variant_builder_add (builder, "s", "in");
641 g_variant_builder_add (builder, "s", "the");
642 g_variant_builder_add (builder, "s", "course");
643 value = g_variant_new ("as", builder);
644 g_variant_builder_unref (builder);
650 g_variant_get (value, "as", &iter);
651 while (g_variant_iter_loop (iter, "s", &str))
652 g_print ("%s\n", str);
653 g_variant_iter_free (iter);
656 g_variant_unref (value);]]></programlisting></informalexample>
660 <refsect2 id='gvariant-format-strings-maybe-types'>
661 <title>Maybe Types</title>
663 <emphasis role='strong'>
664 Characters: <literal>m</literal>
668 Maybe types are handled in two separate ways depending on the format string that follows the
669 '<literal>m</literal>'. The method that is used currently depends entirely on the character immediately following the
670 '<literal>m</literal>'.
674 The first way is used with format strings starting with '<literal>s</literal>', '<literal>o</literal>',
675 '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>', '<literal>*</literal>',
676 '<literal>?</literal>', '<literal>r</literal>', '<literal>&</literal>', or '<literal>^</literal>'. In all of
677 these cases, for non-maybe types, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes
678 a pointer to a non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> value and
679 <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
680 non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer. When any of these format strings are
681 prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
682 <link linkend='NULL--CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
685 Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
686 here. Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
687 the Nothing value -- not an empty array.
690 The second way is used with all other format strings. For
691 <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
692 <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
693 <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
694 <code>(<link linkend='gboolean'>gboolean</link> *)</code>. Following this argument, the arguments that are normally
695 collected for the equivalent non-maybe type will be collected.
698 If <link linkend='FALSE--CAPS'><literal>FALSE</literal></link> is given to
699 <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
700 the collected arguments are ignored. Otherwise (if <link linkend='TRUE--CAPS'><literal>TRUE</literal></link> was
701 given), the arguments are used in the normal way to create the Just value.
704 If <link linkend='NULL--CAPS'><literal>NULL</literal></link> is given to
705 <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored. If a
706 non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
707 whether the value was Just. In the case that the value was Just, the
708 <link linkend='gboolean'><type>gboolean</type></link> will be set to
709 <link linkend='TRUE--CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
710 way. In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
711 <link linkend='FALSE--CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
712 but have their values set to binary zero.
716 <title>Examples</title>
717 <informalexample><programlisting>
718 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
719 value1 = g_variant_new ("ms", "Hello world");
720 value2 = g_variant_new ("ms", NULL);
721 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
722 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done"); /* both '-1' are ignored. */
723 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
732 g_variant_get (value1, "ms", &str);
734 g_print ("str: %s\n", str);
736 g_print ("it was null\n");
740 g_variant_get (value2, "m&s", &cstr);
742 g_print ("str: %s\n", cstr);
744 g_print ("it was null\n");
745 /* don't free 'cstr' */
748 /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
749 g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
750 g_print ("string is %s\n", str);
753 /* note: &s used, so g_free() not needed */
754 g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
756 g_print ("it was (%d, %d)\n", x, y);
758 g_print ("it was null\n");
759 g_print ("string is %s\n", cstr);
760 /* don't free 'cstr' */
763 g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
764 if (contents != NULL)
766 g_variant_get (contents, "(ii)", &x, &y);
767 g_print ("it was (%d, %d)\n", x, y);
768 g_variant_unref (contents);
771 g_print ("it was null\n");
772 }]]></programlisting></informalexample>
776 <refsect2 id='gvariant-format-strings-tuples'>
777 <title>Tuples</title>
779 <emphasis role='strong'>
780 Characters: <code>()</code>
785 Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
789 <title>Examples</title>
790 <informalexample><programlisting>
791 <![CDATA[GVariant *value1, *value2;
793 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
794 value2 = g_variant_new ("()");
800 g_variant_get (value1, "(s(ii))", &string, &x, &y);
801 g_print ("%s, %d, %d\n", string, x, y);
804 g_variant_get (value2, "()"); /* do nothing... */
805 }]]></programlisting></informalexample>
809 <refsect2 id='gvariant-format-strings-dictionaries'>
810 <title>Dictionaries</title>
812 <emphasis role='strong'>
813 Characters: <code>{}</code>
818 Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
822 <title>Examples</title>
823 <informalexample><programlisting>
824 <![CDATA[GVariantBuilder *b;
827 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
828 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
829 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
830 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
834 <refsect2 id='gvariant-format-strings-gvariant'>
835 <title>GVariant *</title>
837 <emphasis role='strong'>
838 Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
843 Upon encountering a '<literal>@</literal>' in front of a type string,
844 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
845 non-<link linkend='NULL--CAPS'><literal>NULL</literal></link> pointer to a
846 <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
847 create the value. The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
848 type string following the '<literal>@</literal>'. '<literal>*</literal>' is
849 the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
850 '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
851 <link linkend='GVariant'><type>GVariant</type></link> of any basic type). '<literal>r</literal>' is the same as
852 '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
855 Upon encountering a '<literal>@</literal>' in front of a type string,
856 <link linkend='g-variant-get'><function>g_variant_get()</function></link>
857 takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
858 <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
859 <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
860 C types in the usual way). <link linkend='NULL--CAPS'><literal>NULL</literal></link> can be given to ignore the
861 value. '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
862 what is stated above.
865 You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
866 use of '<literal>@</literal>'. Using the other characters where possible is recommended, however, due to the
867 improvements in type safety and code self-documentation.
871 <title>Examples</title>
872 <informalexample><programlisting>
873 <![CDATA[GVariant *value1, *value2;
875 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
877 /* note: consumes floating reference count on 'value1' */
878 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
886 g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
887 string = g_variant_get_string (tmp, &length);
888 g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
889 g_variant_unref (tmp);
891 /* quick way to skip all the values in a tuple */
892 g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
893 g_print ("i only got the string: %s\n", string);
895 }]]></programlisting></informalexample>
899 <refsect2 id='gvariant-format-strings-pointers'>
900 <title>Pointers</title>
902 <emphasis role='strong'>
903 Characters: <code>&</code>
908 The '<code>&</code>' character is used to indicate that serialised data should be directly exchanged via a
912 Currently, the only use for this character is when it is applied to a string (ie: '<literal>&s</literal>',
913 '<literal>&o</literal>' or '<code>&g</code>'). For
914 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect. The string
915 is collected and duplicated normally. For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
916 it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
917 returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
918 always be properly nul-terminated.
922 <title>Examples</title>
923 <informalexample><programlisting>
928 value = g_variant_new ("&s", "hello world");
929 str = g_variant_get ("&s", &str);
930 g_print ("string is: %s\n", str);
931 /* no need to free str */
932 }]]></programlisting></informalexample>
936 <refsect2 id='gvariant-format-strings-convenience'>
937 <title>Convenience Conversions</title>
939 <emphasis role='strong'>
940 Characters: <literal>^</literal>
945 The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
946 of strings or bytestrings. It has a number of forms.
950 In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
951 pointer value is collected from the variable arguments and passed to a function (as given in the table below).
952 The result of that function is used as the value for this position. When used with
953 <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
954 the function (given in the table) and returned by reference.
959 <colspec colname='col_0'/>
960 <colspec colname='col_1'/>
961 <colspec colname='col_2'/>
965 <entry colsep='1' rowsep='1'>
967 <emphasis role='strong'>Conversion</emphasis>
970 <entry colsep='1' rowsep='1'>
972 <emphasis role='strong'>
973 Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
977 <entry colsep='1' rowsep='1'>
979 <emphasis role='strong'>
980 Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
987 <entry colsep='1' rowsep='1'>
989 <emphasis role='strong'>
990 <literal>^as</literal>
994 <entry colsep='1' rowsep='1' morerows='1'>
996 equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
999 <entry colsep='1' rowsep='1'>
1001 equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1007 <entry colsep='1' rowsep='1'>
1009 <emphasis role='strong'>
1010 <literal>^a&s</literal>
1014 <entry colsep='1' rowsep='1'>
1016 equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1022 <entry colsep='1' rowsep='1'>
1024 <emphasis role='strong'>
1025 <literal>^ay</literal>
1029 <entry colsep='1' rowsep='1' morerows='1'>
1031 equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1034 <entry colsep='1' rowsep='1'>
1036 equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1042 <entry colsep='1' rowsep='1'>
1044 <emphasis role='strong'>
1045 <literal>^&ay</literal>
1049 <entry colsep='1' rowsep='1'>
1051 equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1057 <entry colsep='1' rowsep='1'>
1059 <emphasis role='strong'>
1060 <literal>^aay</literal>
1064 <entry colsep='1' rowsep='1' morerows='1'>
1066 equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1069 <entry colsep='1' rowsep='1'>
1071 equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1077 <entry colsep='1' rowsep='1'>
1079 <emphasis role='strong'>
1080 <literal>^a&ay</literal>
1084 <entry colsep='1' rowsep='1'>
1086 equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>