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 serialised 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 DBus
536 object path or DBus 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 while (g_variant_iter_loop (iter, "s", &str))
662 g_print ("%s\n", str);
663 g_variant_iter_free (iter);
666 g_variant_unref (value);]]></programlisting></informalexample>
670 <refsect2 id='gvariant-format-strings-maybe-types'>
671 <title>Maybe Types</title>
673 <emphasis role='strong'>
674 Characters: <literal>m</literal>
678 Maybe types are handled in two separate ways depending on the format string that follows the
679 '<literal>m</literal>'. The method that is used currently depends entirely on the character immediately following the
680 '<literal>m</literal>'.
684 The first way is used with format strings starting with '<literal>a</literal>', '<literal>s</literal>',
685 '<literal>o</literal>', '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>',
686 '<literal>*</literal>', '<literal>?</literal>', '<literal>r</literal>', '<literal>&</literal>', or
687 '<literal>^</literal>'. In all of these cases, for non-maybe types,
688 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a pointer to a
689 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> value and
690 <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
691 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer. When any of these format strings are
692 prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
693 <link linkend='NULL:CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
696 Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
697 here. Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
698 the Nothing value -- not an empty array.
701 The second way is used with all other format strings. For
702 <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
703 <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
704 <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
705 <code>(<link linkend='gboolean'>gboolean</link> *)</code>. Following this argument, the arguments that are normally
706 collected for the equivalent non-maybe type will be collected.
709 If <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> is given to
710 <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
711 the collected arguments are ignored. Otherwise (if <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> was
712 given), the arguments are used in the normal way to create the Just value.
715 If <link linkend='NULL:CAPS'><literal>NULL</literal></link> is given to
716 <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored. If a
717 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
718 whether the value was Just. In the case that the value was Just, the
719 <link linkend='gboolean'><type>gboolean</type></link> will be set to
720 <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
721 way. In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
722 <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
723 but have their values set to binary zero.
727 <title>Examples</title>
728 <informalexample><programlisting>
729 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
730 value1 = g_variant_new ("ms", "Hello world");
731 value2 = g_variant_new ("ms", NULL);
732 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
733 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done"); /* both '-1' are ignored. */
734 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
743 g_variant_get (value1, "ms", &str);
745 g_print ("str: %s\n", str);
747 g_print ("it was null\n");
751 g_variant_get (value2, "m&s", &cstr);
753 g_print ("str: %s\n", cstr);
755 g_print ("it was null\n");
756 /* don't free 'cstr' */
759 /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
760 g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
761 g_print ("string is %s\n", str);
764 /* note: &s used, so g_free() not needed */
765 g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
767 g_print ("it was (%d, %d)\n", x, y);
769 g_print ("it was null\n");
770 g_print ("string is %s\n", cstr);
771 /* don't free 'cstr' */
774 g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
775 if (contents != NULL)
777 g_variant_get (contents, "(ii)", &x, &y);
778 g_print ("it was (%d, %d)\n", x, y);
779 g_variant_unref (contents);
782 g_print ("it was null\n");
783 }]]></programlisting></informalexample>
787 <refsect2 id='gvariant-format-strings-tuples'>
788 <title>Tuples</title>
790 <emphasis role='strong'>
791 Characters: <code>()</code>
796 Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
800 <title>Examples</title>
801 <informalexample><programlisting>
802 <![CDATA[GVariant *value1, *value2;
804 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
805 value2 = g_variant_new ("()");
811 g_variant_get (value1, "(s(ii))", &string, &x, &y);
812 g_print ("%s, %d, %d\n", string, x, y);
815 g_variant_get (value2, "()"); /* do nothing... */
816 }]]></programlisting></informalexample>
820 <refsect2 id='gvariant-format-strings-gvariant'>
821 <title>GVariant *</title>
823 <emphasis role='strong'>
824 Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
829 Upon encountering a '<literal>@</literal>' in front of a type string,
830 <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
831 non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer to a
832 <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
833 create the value. The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
834 type string following the '<literal>@</literal>'. '<literal>*</literal>' is
835 the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
836 '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
837 <link linkend='GVariant'><type>GVariant</type></link> of any basic type). '<literal>r</literal>' is the same as
838 '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
841 Upon encountering a '<literal>@</literal>' in front of a type string,
842 <link linkend='g-variant-get'><function>g_variant_get()</function></link>
843 takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
844 <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
845 <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
846 C types in the usual way). <link linkend='NULL:CAPS'><literal>NULL</literal></link> can be given to ignore the
847 value. '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
848 what is stated above.
851 You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
852 use of '<literal>@</literal>'. Using the other characters where possible is recommended, however, due to the
853 improvements in type safety and code self-documentation.
857 <title>Examples</title>
858 <informalexample><programlisting>
859 <![CDATA[GVariant *value1, *value2;
861 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
863 /* note: consumes floating reference count on 'value1' */
864 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
872 g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
873 string = g_variant_get_string (tmp, &length);
874 g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
875 g_variant_unref (tmp);
877 /* quick way to skip all the values in a tuple */
878 g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
879 g_print ("i only got the string: %s\n", string);
881 }]]></programlisting></informalexample>
885 <refsect2 id='gvariant-format-strings-dictionaries'>
886 <title>Dictionaries</title>
888 <emphasis role='strong'>
889 Characters: <code>{}</code>
894 Dictionary entries are handled by handling first the key, then the value. Each is handled in the usual way.
898 <title>Examples</title>
899 <informalexample><programlisting>
900 <![CDATA[GVariantBuilder *b;
903 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
904 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
905 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
906 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
910 To extract data from nested dictionaries you can go through a vardict.
914 <title>Examples</title>
915 <informalexample><programlisting>
916 <![CDATA[GVariant *data;
920 /* type (oa{sa{sv}) */
921 data = g_variant_new_parsed ("(%o, {'brightness': {'value': <%i>, 'max': <%i>}})",
922 "/object/path", value, max);
925 GVariant *p_brightness;
929 g_variant_get (data, "(o@a{?*})", &obj, ¶ms);
930 g_print ("object_path: %s\n", obj);
932 p_brightness = g_variant_lookup_value (params, "brightness", G_VARIANT_TYPE_VARDICT);
933 g_variant_lookup (p_brightness, "max", "i", &p_max);
934 g_print ("max: %d\n", p_max);
935 }]]></programlisting></informalexample>
940 <refsect2 id='gvariant-format-strings-pointers'>
941 <title>Pointers</title>
943 <emphasis role='strong'>
944 Characters: <code>&</code>
949 The '<code>&</code>' character is used to indicate that serialised data should be directly exchanged via a
953 Currently, the only use for this character is when it is applied to a string (ie: '<literal>&s</literal>',
954 '<literal>&o</literal>' or '<code>&g</code>'). For
955 <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect. The string
956 is collected and duplicated normally. For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
957 it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
958 returned. This pointer should not be freed. Validity checks are performed to ensure that the string data will
959 always be properly nul-terminated.
963 <title>Examples</title>
964 <informalexample><programlisting>
969 value = g_variant_new ("&s", "hello world");
970 g_variant_get (value, "&s", &str);
971 g_print ("string is: %s\n", str);
972 /* no need to free str */
973 }]]></programlisting></informalexample>
977 <refsect2 id='gvariant-format-strings-convenience'>
978 <title>Convenience Conversions</title>
980 <emphasis role='strong'>
981 Characters: <literal>^</literal>
986 The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
987 of strings or bytestrings. It does not support byte arrays. It has a number of forms.
991 In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
992 pointer value is collected from the variable arguments and passed to a function (as given in the table below).
993 The result of that function is used as the value for this position. When used with
994 <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
995 the function (given in the table) and returned by reference.
1000 <colspec colname='col_0'/>
1001 <colspec colname='col_1'/>
1002 <colspec colname='col_2'/>
1006 <entry colsep='1' rowsep='1'>
1008 <emphasis role='strong'>Conversion</emphasis>
1011 <entry colsep='1' rowsep='1'>
1013 <emphasis role='strong'>
1014 Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
1018 <entry colsep='1' rowsep='1'>
1020 <emphasis role='strong'>
1021 Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
1028 <entry colsep='1' rowsep='1'>
1030 <emphasis role='strong'>
1031 <literal>^as</literal>
1035 <entry colsep='1' rowsep='1' morerows='1'>
1037 equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
1040 <entry colsep='1' rowsep='1'>
1042 equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1048 <entry colsep='1' rowsep='1'>
1050 <emphasis role='strong'>
1051 <literal>^a&s</literal>
1055 <entry colsep='1' rowsep='1'>
1057 equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1063 <entry colsep='1' rowsep='1'>
1065 <emphasis role='strong'>
1066 <literal>^ao</literal>
1070 <entry colsep='1' rowsep='1' morerows='1'>
1072 equivalent to <link linkend='g-variant-new-objv'><function>g_variant_new_objv()</function></link>
1075 <entry colsep='1' rowsep='1'>
1077 equivalent to <link linkend='g-variant-dup-objv'><function>g_variant_dup_objv()</function></link>
1083 <entry colsep='1' rowsep='1'>
1085 <emphasis role='strong'>
1086 <literal>^a&o</literal>
1090 <entry colsep='1' rowsep='1'>
1092 equivalent to <link linkend='g-variant-get-objv'><function>g_variant_get_objv()</function></link>
1098 <entry colsep='1' rowsep='1'>
1100 <emphasis role='strong'>
1101 <literal>^ay</literal>
1105 <entry colsep='1' rowsep='1' morerows='1'>
1107 equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1110 <entry colsep='1' rowsep='1'>
1112 equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1118 <entry colsep='1' rowsep='1'>
1120 <emphasis role='strong'>
1121 <literal>^&ay</literal>
1125 <entry colsep='1' rowsep='1'>
1127 equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1133 <entry colsep='1' rowsep='1'>
1135 <emphasis role='strong'>
1136 <literal>^aay</literal>
1140 <entry colsep='1' rowsep='1' morerows='1'>
1142 equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1145 <entry colsep='1' rowsep='1'>
1147 equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1153 <entry colsep='1' rowsep='1'>
1155 <emphasis role='strong'>
1156 <literal>^a&ay</literal>
1160 <entry colsep='1' rowsep='1'>
1162 equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>