1 <?xml version='1.0' encoding='utf-8'?>
3 <refentry id='gvariant-format-strings'>
5 <refentrytitle>GVariant Format Strings</refentrytitle>
8 <refname>GVariant Format Strings</refname>
9 <refpurpose>varargs conversion of GVariants</refpurpose>
13 <title>Variable Argument Conversions</title>
16 This page attempts to document how to perform variable argument
17 conversions with GVariant.
20 Conversions occur according to format strings. A format string is a two-way mapping between a single
21 <link linkend='GVariant'>GVariant</link> value and one or more C values.
24 A conversion from C values into a <link linkend='GVariant'>GVariant</link> value is made using the
25 <link linkend='g-variant-new'><function>g_variant_new()</function></link> function. A conversion from a
26 <link linkend='GVariant'>GVariant</link> into C values is made using the
27 <link linkend='g-variant-get'><function>g_variant_get()</function></link> function.
35 This section exhaustively describes all possibilities for GVariant format strings. There are no valid forms of
36 format strings other than those described here. Please note that the format string syntax is likely to expand in the
40 Valid format strings have one of the following forms:
44 <para>any type string</para>
48 a type string prefixed with a '<literal>@</literal>'
53 '<literal>&s</literal>' '<literal>&o</literal>', '<literal>&g</literal>', '<literal>^as</literal>',
54 '<literal>^a&s</literal>', '<literal>^ao</literal>', '<literal>^a&o</literal>','<literal>^ay</literal>',
55 '<literal>^&ay</literal>', '<literal>^aay</literal>' or '<literal>^a&ay</literal>'.
60 any format string, prefixed with an '<literal>m</literal>'
65 a sequence of zero or more format strings, concatenated and enclosed in parentheses
70 an opening brace, followed by two format strings, followed by a closing brace (subject to the constraint that the
71 first format string correspond to a type valid for use as the key type of a dictionary)
77 <title>Symbols</title>
80 The following table describes the rough meaning of symbols that may appear inside a GVariant format string. Each
81 symbol is described in detail in its own section, including usage examples.
86 <colspec colname='col_0'/>
87 <colspec colname='col_1'/>
91 <entry colsep='1' rowsep='1'>
93 <emphasis role='strong'>Symbol</emphasis>
96 <entry colsep='1' rowsep='1'>
98 <emphasis role='strong'>Meaning</emphasis>
104 <entry colsep='1' rowsep='1'>
106 <emphasis role='strong'>
107 <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>, <literal>i</literal>,
108 <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>, <literal>d</literal>
112 <entry colsep='1' rowsep='1'>
114 Used for building or deconstructing boolean, byte and numeric types. See
115 <link linkend='gvariant-format-strings-numeric-types'>Numeric Types</link> below.
121 <entry colsep='1' rowsep='1'>
123 <emphasis role='strong'>
124 <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
128 <entry colsep='1' rowsep='1'>
130 Used for building or deconstructing string types. See
131 <link linkend='gvariant-format-strings-strings'>Strings</link> below.
137 <entry colsep='1' rowsep='1'>
139 <emphasis role='strong'><literal>v</literal></emphasis>
142 <entry colsep='1' rowsep='1'>
144 Used for building or deconstructing variant types. See
145 <link linkend='gvariant-format-strings-variants'>Variants</link> below.
151 <entry colsep='1' rowsep='1'>
153 <emphasis role='strong'>
158 <entry colsep='1' rowsep='1'>
160 Used for building or deconstructing arrays. See
161 <link linkend='gvariant-format-strings-arrays'>Arrays</link> below.
167 <entry colsep='1' rowsep='1'>
169 <emphasis role='strong'>
174 <entry colsep='1' rowsep='1'>
176 Used for building or deconstructing maybe types. See
177 <link linkend='gvariant-format-strings-maybe-types'>Maybe Types</link> below.
183 <entry colsep='1' rowsep='1'>
185 <emphasis role='strong'>
186 <literal>()</literal>
190 <entry colsep='1' rowsep='1'>
192 Used for building or deconstructing tuples. See
193 <link linkend='gvariant-format-strings-tuples'>Tuples</link> below.
199 <entry colsep='1' rowsep='1'>
201 <emphasis role='strong'>
202 <literal>{}</literal>
206 <entry colsep='1' rowsep='1'>
208 Used for building or deconstructing dictionary entries. See
209 <link linkend='gvariant-format-strings-dictionaries'>Dictionaries</link> below.
215 <entry colsep='1' rowsep='1'>
217 <emphasis role='strong'>
222 <entry colsep='1' rowsep='1'>
224 Used as a prefix for a GVariant type string (not a prefix for a format string, so <literal>@as</literal> is
225 a valid format string but <literal>@^as</literal> is not). Denotes that a pointer to a
226 <link linkend='GVariant'>GVariant</link> should be used in place of the normal C type or types. For
227 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this means that you must pass a
228 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> <code>(<link linkend='GVariant'>GVariant</link>
229 *)</code>; if it is a floating reference, ownership will be taken, as
230 if by using <link linkend="g-variant-ref-sink"><function>g_variant_ref_sink()</function></link>.
231 For <link linkend='g-variant-get'><function>g_variant_get()</function></link> this means that you
232 must pass a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> for the value to be returned
233 by reference or <link linkend='NULL:CAPS'><literal>NULL</literal></link> to ignore the value. See
234 <link linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
240 <entry colsep='1' rowsep='1'>
242 <emphasis role='strong'>
243 <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
247 <entry colsep='1' rowsep='1'>
249 Exactly equivalent to <literal>@*</literal>, <literal>@?</literal> and <literal>@r</literal>. Provided only for
250 completeness so that all GVariant type strings can be used also as format strings. See <link
251 linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
257 <entry colsep='1' rowsep='1'>
259 <emphasis role='strong'><literal>&</literal></emphasis>
262 <entry colsep='1' rowsep='1'>
264 Used as a prefix for a GVariant type string (not a prefix for a format string, so <literal>&s</literal> is
265 a valid format string but <literal>&@s</literal> is not).
266 Denotes that a C pointer to serialized data
267 should be used in place of the normal C type. See
268 <link linkend='gvariant-format-strings-pointers'>Pointers</link> below.
274 <entry colsep='1' rowsep='1'>
276 <emphasis role='strong'><literal>^</literal></emphasis>
279 <entry colsep='1' rowsep='1'>
281 Used as a prefix on some specific types of format strings. See
282 <link linkend='gvariant-format-strings-convenience'>Convenience Conversions</link> below.
291 <refsect2 id='gvariant-format-strings-numeric-types'>
292 <title>Numeric Types</title>
294 <emphasis role='strong'>
295 Characters: <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>,
296 <literal>i</literal>, <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>,
302 Variable argument conversions from numeric types work in the most obvious way possible. Upon encountering one of
303 these characters, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes the equivalent C
304 type as an argument. <link linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to
305 the equivalent C type (or <link linkend='NULL:CAPS'><literal>NULL</literal></link> to ignore the value).
309 The equivalent C types are as follows:
314 <colspec colname='col_0'/><colspec colname='col_1'/>
317 <entry colsep='1' rowsep='1'>
319 <emphasis role='strong'>Character</emphasis>
322 <entry colsep='1' rowsep='1'>
324 <emphasis role='strong'>Equivalent C type</emphasis>
329 <entry colsep='1' rowsep='1'>
331 <emphasis role='strong'>
336 <entry colsep='1' rowsep='1'>
338 <link linkend='gboolean'><type>gboolean</type></link>
343 <entry colsep='1' rowsep='1'>
345 <emphasis role='strong'>
350 <entry colsep='1' rowsep='1'>
352 <link linkend='guchar'><type>guchar</type></link>
357 <entry colsep='1' rowsep='1'>
359 <emphasis role='strong'>
364 <entry colsep='1' rowsep='1'>
366 <link linkend='gint16'><type>gint16</type></link>
371 <entry colsep='1' rowsep='1'>
373 <emphasis role='strong'>
378 <entry colsep='1' rowsep='1'>
380 <link linkend='guint16'><type>guint16</type></link>
385 <entry colsep='1' rowsep='1'>
387 <emphasis role='strong'>
392 <entry colsep='1' rowsep='1'>
394 <link linkend='gint32'><type>gint32</type></link>
399 <entry colsep='1' rowsep='1'>
401 <emphasis role='strong'>
406 <entry colsep='1' rowsep='1'>
408 <link linkend='guint32'><type>guint32</type></link>
413 <entry colsep='1' rowsep='1'>
415 <emphasis role='strong'>
420 <entry colsep='1' rowsep='1'>
422 <link linkend='gint64'><type>gint64</type></link>
427 <entry colsep='1' rowsep='1'>
429 <emphasis role='strong'>
434 <entry colsep='1' rowsep='1'>
436 <link linkend='guint64'><type>guint64</type></link>
441 <entry colsep='1' rowsep='1'>
443 <emphasis role='strong'>
448 <entry colsep='1' rowsep='1'>
450 <link linkend='gint32'><type>gint32</type></link>
455 <entry colsep='1' rowsep='1'>
457 <emphasis role='strong'>
462 <entry colsep='1' rowsep='1'>
464 <link linkend='gdouble'><type>gdouble</type></link>
472 <anchor id='gvariant-varargs'/>
474 Note that in C, small integer types in variable argument lists are promoted up to <link
475 linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
476 read back accordingly. <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
477 currently supported. This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
478 with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
479 '<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
480 '<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'. Specifically, you can use integer
481 literals with these characters.
485 When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
486 provide is 64 bit. This means that you should use a cast or make use of the
487 <link linkend='G-GINT64-CONSTANT:CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
488 <link linkend='G-GUINT64-CONSTANT:CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
492 No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
493 it operates with pointers. The pointers must always point to a memory region of exactly the correct size.
497 <title>Examples</title>
498 <informalexample><programlisting>
499 <![CDATA[GVariant *value1, *value2, *value3, *value4;
501 value1 = g_variant_new ("y", 200);
502 value2 = g_variant_new ("b", TRUE);
503 value3 = g_variant_new ("d", 37.5):
504 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
512 g_variant_get (value1, "y", NULL); /* ignore the value. */
513 g_variant_get (value2, "b", &truth);
514 g_variant_get (value3, "d", &floating);
515 g_variant_get (value4, "x", &bignum);
516 }]]></programlisting></informalexample>
520 <refsect2 id='gvariant-format-strings-strings'>
521 <title>Strings</title>
523 <emphasis role='strong'>
524 Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
529 String conversions occur to and from standard nul-terminated C strings. Upon encountering an
530 '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
531 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
532 <link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
533 <link linkend='NULL:CAPS'><literal>NULL</literal></link> is not a valid string; use
534 <link linkend='gvariant-format-strings-maybe-types'>maybe types</link> to encode that. If the '<literal>o</literal>' or
535 '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid D-Bus
536 object path or D-Bus type signature, respectively.
539 Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
540 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
541 <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
542 sets it to a newly-allocated copy of the string. It is appropriate to free this copy using
543 <link linkend='g-free'><function>g_free()</function></link>.
544 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
545 string should be ignored (in which case no copy is made).
549 <title>Examples</title>
550 <informalexample><programlisting>
551 <![CDATA[GVariant *value1, *value2, *value3;
553 value1 = g_variant_new ("s", "hello world!");
554 value2 = g_variant_new ("o", "/must/be/a/valid/path");
555 value3 = g_variant_new ("g", "iias");
558 g_variant_new ("s", NULL); /* not valid: NULL is not a string. */
564 g_variant_get (value1, "s", &result);
565 g_print ("It was '%s'\n", result);
567 }]]></programlisting></informalexample>
571 <refsect2 id='gvariant-format-strings-variants'>
572 <title>Variants</title>
574 <emphasis role='strong'>
575 Characters: <literal>v</literal>
580 Upon encountering a '<literal>v</literal>',
581 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
582 linkend='GVariant'>GVariant</link> *)</code>. The value of the
583 <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
586 Upon encountering a '<literal>v</literal>', <link
587 linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
588 <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
589 </code>). It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
590 containing the contents of the variant value. It is appropriate to free this reference using
591 <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
592 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
593 ignored (in which case no new reference is created).
597 <title>Examples</title>
598 <informalexample><programlisting>
599 <![CDATA[GVariant *x, *y;
601 /* the following two lines are equivalent: */
602 x = g_variant_new ("v", y);
603 x = g_variant_new_variant (y);
606 g_variant_get (x, "v", &y);
607 y = g_variant_get_variant (x);]]></programlisting></informalexample>
612 <refsect2 id='gvariant-format-strings-arrays'>
613 <title>Arrays</title>
615 <emphasis role='strong'>
616 Characters: <literal>a</literal>
621 Upon encountering an '<literal>a</literal>' character followed by a type string,
622 <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
623 <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
624 for an array of the type given in the type string. The builder will have
625 <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
626 result will be used as the value. As a special exception, if the given type string is a definite type, then
627 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
631 Upon encountering an '<literal>a</literal>' character followed by a type string,
632 <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
633 <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
634 <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
635 A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
636 This iterator should be freed when you are done with it, using
637 <link linkend='g-variant-iter-free'><function>g_variant_iter_free()</function></link>.
638 <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
643 <title>Examples</title>
644 <informalexample><programlisting>
645 <![CDATA[GVariantBuilder *builder;
648 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
649 g_variant_builder_add (builder, "s", "when");
650 g_variant_builder_add (builder, "s", "in");
651 g_variant_builder_add (builder, "s", "the");
652 g_variant_builder_add (builder, "s", "course");
653 value = g_variant_new ("as", builder);
654 g_variant_builder_unref (builder);
660 g_variant_get (value, "as", &iter);
661 /* No need to free 'str' unless breaking out of the loop */
662 while (g_variant_iter_loop (iter, "s", &str))
663 g_print ("%s\n", str);
664 g_variant_iter_free (iter);
667 g_variant_unref (value);]]></programlisting></informalexample>
671 <refsect2 id='gvariant-format-strings-maybe-types'>
672 <title>Maybe Types</title>
674 <emphasis role='strong'>
675 Characters: <literal>m</literal>
679 Maybe types are handled in two separate ways depending on the format string that follows the
680 '<literal>m</literal>'. The method that is used currently depends entirely on the character immediately following the
681 '<literal>m</literal>'.
685 The first way is used with format strings starting with '<literal>a</literal>', '<literal>s</literal>',
686 '<literal>o</literal>', '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>',
687 '<literal>*</literal>', '<literal>?</literal>', '<literal>r</literal>', '<literal>&</literal>', or
688 '<literal>^</literal>'. In all of these cases, for non-maybe types,
689 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a pointer to a
690 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> value and
691 <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
692 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer. When any of these format strings are
693 prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
694 <link linkend='NULL:CAPS'><literal>NULL</literal></link> becomes a permissible value, to indicate the Nothing case.
697 Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
698 here. Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
699 the Nothing value -- not an empty array.
702 The second way is used with all other format strings. For
703 <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
704 <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
705 <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
706 <code>(<link linkend='gboolean'>gboolean</link> *)</code>. Following this argument, the arguments that are normally
707 collected for the equivalent non-maybe type will be collected.
710 If <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> is given to
711 <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
712 the collected arguments are ignored. Otherwise (if <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> was
713 given), the arguments are used in the normal way to create the Just value.
716 If <link linkend='NULL:CAPS'><literal>NULL</literal></link> is given to
717 <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored. If a
718 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
719 whether the value was Just. In the case that the value was Just, the
720 <link linkend='gboolean'><type>gboolean</type></link> will be set to
721 <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
722 way. In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
723 <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
724 but have their values set to binary zero.
728 <title>Examples</title>
729 <informalexample><programlisting>
730 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
731 value1 = g_variant_new ("ms", "Hello world");
732 value2 = g_variant_new ("ms", NULL);
733 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
734 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done"); /* both '-1' are ignored. */
735 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
744 g_variant_get (value1, "ms", &str);
746 g_print ("str: %s\n", str);
748 g_print ("it was null\n");
752 g_variant_get (value2, "m&s", &cstr);
754 g_print ("str: %s\n", cstr);
756 g_print ("it was null\n");
757 /* don't free 'cstr' */
760 /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
761 g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
762 g_print ("string is %s\n", str);
765 /* note: &s used, so g_free() not needed */
766 g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
768 g_print ("it was (%d, %d)\n", x, y);
770 g_print ("it was null\n");
771 g_print ("string is %s\n", cstr);
772 /* don't free 'cstr' */
775 g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
776 if (contents != NULL)
778 g_variant_get (contents, "(ii)", &x, &y);
779 g_print ("it was (%d, %d)\n", x, y);
780 g_variant_unref (contents);
783 g_print ("it was null\n");
784 }]]></programlisting></informalexample>
788 <refsect2 id='gvariant-format-strings-tuples'>
789 <title>Tuples</title>
791 <emphasis role='strong'>
792 Characters: <code>()</code>
797 Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
801 <title>Examples</title>
802 <informalexample><programlisting>
803 <![CDATA[GVariant *value1, *value2;
805 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
806 value2 = g_variant_new ("()");
812 g_variant_get (value1, "(s(ii))", &string, &x, &y);
813 g_print ("%s, %d, %d\n", string, x, y);
816 g_variant_get (value2, "()"); /* do nothing... */
817 }]]></programlisting></informalexample>
821 <refsect2 id='gvariant-format-strings-gvariant'>
822 <title>GVariant *</title>
824 <emphasis role='strong'>
825 Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
830 Upon encountering a '<literal>@</literal>' in front of a type string,
831 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
832 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer to a
833 <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
834 create the value. The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
835 type string following the '<literal>@</literal>'. '<literal>*</literal>' is
836 the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
837 '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
838 <link linkend='GVariant'><type>GVariant</type></link> of any basic type). '<literal>r</literal>' is the same as
839 '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
842 Upon encountering a '<literal>@</literal>' in front of a type string,
843 <link linkend='g-variant-get'><function>g_variant_get()</function></link>
844 takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
845 <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
846 <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
847 C types in the usual way). <link linkend='NULL:CAPS'><literal>NULL</literal></link> can be given to ignore the
848 value. '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
849 what is stated above.
852 You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
853 use of '<literal>@</literal>'. Using the other characters where possible is recommended, however, due to the
854 improvements in type safety and code self-documentation.
858 <title>Examples</title>
859 <informalexample><programlisting>
860 <![CDATA[GVariant *value1, *value2;
862 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
864 /* note: consumes floating reference count on 'value1' */
865 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
873 g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
874 string = g_variant_get_string (tmp, &length);
875 g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
876 g_variant_unref (tmp);
878 /* quick way to skip all the values in a tuple */
879 g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
880 g_print ("i only got the string: %s\n", string);
884 /* no need to free 'value1' as 'value2' has consumed its floating reference */
885 g_variant_unref (value2);]]></programlisting></informalexample>
889 <refsect2 id='gvariant-format-strings-dictionaries'>
890 <title>Dictionaries</title>
892 <emphasis role='strong'>
893 Characters: <code>{}</code>
898 Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
902 <title>Examples</title>
903 <informalexample><programlisting>
904 <![CDATA[GVariantBuilder *b;
907 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
908 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
909 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
910 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
914 To extract data from nested dictionaries you can go through a vardict.
918 <title>Examples</title>
919 <informalexample><programlisting>
920 <![CDATA[GVariant *data;
924 /* type (oa{sa{sv}}) */
925 data = g_variant_new_parsed ("(%o, {'brightness': {'value': <%i>, 'max': <%i>}})",
926 "/object/path", value, max);
929 GVariant *p_brightness;
933 g_variant_get (data, "(o@a{?*})", &obj, ¶ms);
934 g_print ("object_path: %s\n", obj);
937 p_brightness = g_variant_lookup_value (params, "brightness", G_VARIANT_TYPE_VARDICT);
938 g_variant_lookup (p_brightness, "max", "i", &p_max);
939 g_print ("max: %d\n", p_max);
940 g_variant_unref (params);
941 }]]></programlisting></informalexample>
946 <refsect2 id='gvariant-format-strings-pointers'>
947 <title>Pointers</title>
949 <emphasis role='strong'>
950 Characters: <code>&</code>
955 The '<code>&</code>' character is used to indicate that serialized data should be directly exchanged via a
959 Currently, the only use for this character is when it is applied to a string (ie: '<literal>&s</literal>',
960 '<literal>&o</literal>' or '<code>&g</code>'). For
961 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect. The string
962 is collected and duplicated normally. For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
963 it means that instead of creating a newly allocated copy of the string, a pointer to the serialized data is
964 returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
965 always be properly nul-terminated.
969 <title>Examples</title>
970 <informalexample><programlisting>
975 value = g_variant_new ("&s", "hello world");
976 g_variant_get (value, "&s", &str);
977 g_print ("string is: %s\n", str);
978 /* no need to free str */
979 }]]></programlisting></informalexample>
983 <refsect2 id='gvariant-format-strings-convenience'>
984 <title>Convenience Conversions</title>
986 <emphasis role='strong'>
987 Characters: <literal>^</literal>
992 The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
993 of strings or bytestrings. It does not support byte arrays. It has a number of forms.
997 In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
998 pointer value is collected from the variable arguments and passed to a function (as given in the table below).
999 The result of that function is used as the value for this position. When used with
1000 <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
1001 the function (given in the table) and returned by reference.
1006 <colspec colname='col_0'/>
1007 <colspec colname='col_1'/>
1008 <colspec colname='col_2'/>
1012 <entry colsep='1' rowsep='1'>
1014 <emphasis role='strong'>Conversion</emphasis>
1017 <entry colsep='1' rowsep='1'>
1019 <emphasis role='strong'>
1020 Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
1024 <entry colsep='1' rowsep='1'>
1026 <emphasis role='strong'>
1027 Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
1034 <entry colsep='1' rowsep='1'>
1036 <emphasis role='strong'>
1037 <literal>^as</literal>
1041 <entry colsep='1' rowsep='1' morerows='1'>
1043 equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
1046 <entry colsep='1' rowsep='1'>
1048 equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1054 <entry colsep='1' rowsep='1'>
1056 <emphasis role='strong'>
1057 <literal>^a&s</literal>
1061 <entry colsep='1' rowsep='1'>
1063 equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1069 <entry colsep='1' rowsep='1'>
1071 <emphasis role='strong'>
1072 <literal>^ao</literal>
1076 <entry colsep='1' rowsep='1' morerows='1'>
1078 equivalent to <link linkend='g-variant-new-objv'><function>g_variant_new_objv()</function></link>
1081 <entry colsep='1' rowsep='1'>
1083 equivalent to <link linkend='g-variant-dup-objv'><function>g_variant_dup_objv()</function></link>
1089 <entry colsep='1' rowsep='1'>
1091 <emphasis role='strong'>
1092 <literal>^a&o</literal>
1096 <entry colsep='1' rowsep='1'>
1098 equivalent to <link linkend='g-variant-get-objv'><function>g_variant_get_objv()</function></link>
1104 <entry colsep='1' rowsep='1'>
1106 <emphasis role='strong'>
1107 <literal>^ay</literal>
1111 <entry colsep='1' rowsep='1' morerows='1'>
1113 equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1116 <entry colsep='1' rowsep='1'>
1118 equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1124 <entry colsep='1' rowsep='1'>
1126 <emphasis role='strong'>
1127 <literal>^&ay</literal>
1131 <entry colsep='1' rowsep='1'>
1133 equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1139 <entry colsep='1' rowsep='1'>
1141 <emphasis role='strong'>
1142 <literal>^aay</literal>
1146 <entry colsep='1' rowsep='1' morerows='1'>
1148 equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1151 <entry colsep='1' rowsep='1'>
1153 equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1159 <entry colsep='1' rowsep='1'>
1161 <emphasis role='strong'>
1162 <literal>^a&ay</literal>
1166 <entry colsep='1' rowsep='1'>
1168 equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>