gvariant: Fix confusion between type and format strings in the docs
[platform/upstream/glib.git] / docs / reference / glib / gvariant-varargs.xml
1 <?xml version='1.0' encoding='utf-8'?>
2
3 <refentry id='gvariant-format-strings'>
4  <refmeta>
5   <refentrytitle>GVariant Format Strings</refentrytitle>
6  </refmeta>
7  <refnamediv>
8   <refname>GVariant Format Strings</refname>
9   <refpurpose>varargs conversion of GVariants</refpurpose>
10  </refnamediv>
11
12  <refsect1>
13   <title>Variable Argument Conversions</title>
14
15   <para>
16    This page attempts to document how to perform variable argument
17    conversions with GVariant.
18   </para>
19   <para>
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.
22   </para>
23   <para>
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.
28   </para>
29  </refsect1>
30
31  <refsect1>
32   <title>Syntax</title>
33
34   <para>
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
37    future.
38   </para>
39   <para>
40    Valid format strings have one of the following forms:
41   </para>
42   <itemizedlist>
43    <listitem>
44     <para>any type string</para>
45    </listitem>
46    <listitem>
47     <para>
48      a type string prefixed with a '<literal>@</literal>'
49     </para>
50    </listitem>
51    <listitem>
52     <para>
53      '<literal>&amp;s</literal>' '<literal>&amp;o</literal>', '<literal>&amp;g</literal>', '<literal>^as</literal>',
54      '<literal>^a&amp;s</literal>', '<literal>^ao</literal>', '<literal>^a&amp;o</literal>','<literal>^ay</literal>',
55      '<literal>^&amp;ay</literal>', '<literal>^aay</literal>' or '<literal>^a&amp;ay</literal>'.
56     </para>
57    </listitem>
58    <listitem>
59     <para>
60      any format string, prefixed with an '<literal>m</literal>'
61     </para>
62    </listitem>
63    <listitem>
64     <para>
65      a sequence of zero or more format strings strings, concatenated and enclosed in parentheses
66     </para>
67    </listitem>
68    <listitem>
69     <para>
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)
72     </para>
73    </listitem>
74   </itemizedlist>
75  </refsect1>
76  <refsect1>
77   <title>Symbols</title>
78
79    <para>
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.
82   </para>
83
84   <informaltable>
85    <tgroup cols='2'>
86     <colspec colname='col_0'/>
87     <colspec colname='col_1'/>
88     <tbody>
89
90      <row rowsep='1'>
91       <entry colsep='1' rowsep='1'>
92        <para>
93         <emphasis role='strong'>Symbol</emphasis>
94        </para>
95       </entry>
96       <entry colsep='1' rowsep='1'>
97        <para>
98          <emphasis role='strong'>Meaning</emphasis>
99        </para>
100       </entry>
101      </row>
102
103      <row rowsep='1'>
104       <entry colsep='1' rowsep='1'>
105        <para>
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>
109         </emphasis>
110        </para>
111       </entry>
112       <entry colsep='1' rowsep='1'>
113        <para>
114         Used for building or deconstructing boolean, byte and numeric types.  See
115         <link linkend='gvariant-format-strings-numeric-types'>Numeric Types</link> below.
116        </para>
117       </entry>
118      </row>
119
120      <row rowsep='1'>
121       <entry colsep='1' rowsep='1'>
122        <para>
123         <emphasis role='strong'>
124          <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
125         </emphasis>
126        </para>
127       </entry>
128       <entry colsep='1' rowsep='1'>
129        <para>
130         Used for building or deconstructing string types.  See
131         <link linkend='gvariant-format-strings-strings'>Strings</link> below.
132        </para>
133       </entry>
134      </row>
135
136      <row rowsep='1'>
137       <entry colsep='1' rowsep='1'>
138        <para>
139         <emphasis role='strong'><literal>v</literal></emphasis>
140        </para>
141       </entry>
142       <entry colsep='1' rowsep='1'>
143        <para>
144         Used for building or deconstructing variant types.  See
145         <link linkend='gvariant-format-strings-variants'>Variants</link> below.
146        </para>
147       </entry>
148      </row>
149
150      <row rowsep='1'>
151       <entry colsep='1' rowsep='1'>
152        <para>
153         <emphasis role='strong'>
154          <literal>a</literal>
155         </emphasis>
156        </para>
157       </entry>
158       <entry colsep='1' rowsep='1'>
159        <para>
160         Used for building or deconstructing arrays.  See
161         <link linkend='gvariant-format-strings-arrays'>Arrays</link> below.
162        </para>
163       </entry>
164      </row>
165
166      <row rowsep='1'>
167       <entry colsep='1' rowsep='1'>
168        <para>
169         <emphasis role='strong'>
170          <literal>m</literal>
171         </emphasis>
172        </para>
173       </entry>
174       <entry colsep='1' rowsep='1'>
175        <para>
176          Used for building or deconstructing maybe types.  See
177          <link linkend='gvariant-format-strings-maybe-types'>Maybe Types</link> below.
178        </para>
179       </entry>
180      </row>
181
182      <row rowsep='1'>
183       <entry colsep='1' rowsep='1'>
184        <para>
185         <emphasis role='strong'>
186          <literal>()</literal>
187         </emphasis>
188        </para>
189       </entry>
190       <entry colsep='1' rowsep='1'>
191        <para>
192          Used for building or deconstructing tuples.  See
193          <link linkend='gvariant-format-strings-tuples'>Tuples</link> below.
194        </para>
195       </entry>
196      </row>
197
198      <row rowsep='1'>
199       <entry colsep='1' rowsep='1'>
200        <para>
201         <emphasis role='strong'>
202          <literal>{}</literal>
203         </emphasis>
204        </para>
205       </entry>
206       <entry colsep='1' rowsep='1'>
207        <para>
208         Used for building or deconstructing dictionary entries. See
209         <link linkend='gvariant-format-strings-dictionaries'>Dictionaries</link> below.
210        </para>
211       </entry>
212      </row>
213
214      <row rowsep='1'>
215       <entry colsep='1' rowsep='1'>
216        <para>
217         <emphasis role='strong'>
218          <literal>@</literal>
219         </emphasis>
220        </para>
221       </entry>
222       <entry colsep='1' rowsep='1'>
223        <para>
224         Used as a prefix on a GVariant format string (not type string).  Denotes that a pointer to a
225         <link linkend='GVariant'>GVariant</link> should be used in place of the normal C type or types.  For
226         <link linkend='g-variant-new'><function>g_variant_new()</function></link> this means that you must pass a
227         non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> <code>(<link linkend='GVariant'>GVariant</link>
228         *)</code>; if it is a floating reference, ownership will be taken, as
229         if by using <link linkend="g-variant-ref-sink"><function>g_variant_ref_sink()</function></link>.
230         For <link linkend='g-variant-get'><function>g_variant_get()</function></link> this means that you
231         must pass a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> for the value to be returned
232         by reference or <link linkend='NULL:CAPS'><literal>NULL</literal></link> to ignore the value.  See
233         <link linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
234        </para>
235       </entry>
236      </row>
237
238      <row rowsep='1'>
239       <entry colsep='1' rowsep='1'>
240        <para>
241         <emphasis role='strong'>
242          <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
243         </emphasis>
244        </para>
245       </entry>
246       <entry colsep='1' rowsep='1'>
247        <para>
248         Exactly equivalent to <literal>@*</literal>, <literal>@?</literal> and <literal>@r</literal>.  Provided only for
249         completeness so that all GVariant type strings can be used also as format strings.  See <link
250         linkend='gvariant-format-strings-gvariant'><code>GVariant *</code></link> below.
251        </para>
252       </entry>
253      </row>
254
255      <row rowsep='1'>
256       <entry colsep='1' rowsep='1'>
257        <para>
258         <emphasis role='strong'><literal>&amp;</literal></emphasis>
259        </para>
260       </entry>
261       <entry colsep='1' rowsep='1'>
262        <para>
263         Used as a prefix on a GVariant format string (not type string).  Denotes that a C pointer to serialised data
264         should be used in place of the normal C type.  See
265         <link linkend='gvariant-format-strings-pointers'>Pointers</link> below.
266        </para>
267       </entry>
268      </row>
269
270      <row rowsep='1'>
271       <entry colsep='1' rowsep='1'>
272        <para>
273         <emphasis role='strong'><literal>^</literal></emphasis>
274        </para>
275       </entry>
276       <entry colsep='1' rowsep='1'>
277        <para>
278         Used as a prefix on some specific types of format strings.  See
279         <link linkend='gvariant-format-strings-convenience'>Convenience Conversions</link> below.
280        </para>
281       </entry>
282      </row>
283     </tbody>
284    </tgroup>
285   </informaltable>
286
287
288   <refsect2 id='gvariant-format-strings-numeric-types'>
289    <title>Numeric Types</title>
290    <para>
291     <emphasis role='strong'>
292      Characters: <literal>b</literal>, <literal>y</literal>, <literal>n</literal>, <literal>q</literal>,
293      <literal>i</literal>, <literal>u</literal>, <literal>x</literal>, <literal>t</literal>, <literal>h</literal>,
294      <literal>d</literal>
295     </emphasis>
296    </para>
297
298    <para>
299     Variable argument conversions from numeric types work in the most obvious way possible.  Upon encountering one of
300     these characters, <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes the equivalent C
301     type as an argument.  <link linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to
302     the equivalent C type (or <link linkend='NULL:CAPS'><literal>NULL</literal></link> to ignore the value).
303    </para>
304
305    <para>
306     The equivalent C types are as follows:
307    </para>
308
309    <informaltable>
310     <tgroup cols='2'>
311      <colspec colname='col_0'/><colspec colname='col_1'/>
312      <tbody>
313       <row rowsep='1'>
314        <entry colsep='1' rowsep='1'>
315         <para>
316           <emphasis role='strong'>Character</emphasis>
317         </para>
318        </entry>
319        <entry colsep='1' rowsep='1'>
320         <para>
321           <emphasis role='strong'>Equivalent C type</emphasis>
322         </para>
323        </entry>
324       </row>
325       <row rowsep='1'>
326        <entry colsep='1' rowsep='1'>
327         <para>
328          <emphasis role='strong'>
329           <literal>b</literal>
330          </emphasis>
331         </para>
332        </entry>
333        <entry colsep='1' rowsep='1'>
334         <para>
335          <link linkend='gboolean'><type>gboolean</type></link>
336         </para>
337        </entry>
338       </row>
339       <row rowsep='1'>
340        <entry colsep='1' rowsep='1'>
341         <para>
342          <emphasis role='strong'>
343           <literal>y</literal>
344          </emphasis>
345         </para>
346        </entry>
347        <entry colsep='1' rowsep='1'>
348         <para>
349          <link linkend='guchar'><type>guchar</type></link>
350         </para>
351        </entry>
352       </row>
353       <row rowsep='1'>
354        <entry colsep='1' rowsep='1'>
355         <para>
356          <emphasis role='strong'>
357           <literal>n</literal>
358          </emphasis>
359         </para>
360        </entry>
361        <entry colsep='1' rowsep='1'>
362         <para>
363          <link linkend='gint16'><type>gint16</type></link>
364         </para>
365        </entry>
366       </row>
367       <row rowsep='1'>
368        <entry colsep='1' rowsep='1'>
369         <para>
370          <emphasis role='strong'>
371           <literal>q</literal>
372          </emphasis>
373         </para>
374        </entry>
375        <entry colsep='1' rowsep='1'>
376         <para>
377          <link linkend='guint16'><type>guint16</type></link>
378         </para>
379        </entry>
380       </row>
381       <row rowsep='1'>
382        <entry colsep='1' rowsep='1'>
383         <para>
384          <emphasis role='strong'>
385           <literal>i</literal>
386          </emphasis>
387         </para>
388        </entry>
389        <entry colsep='1' rowsep='1'>
390         <para>
391          <link linkend='gint32'><type>gint32</type></link>
392         </para>
393        </entry>
394       </row>
395       <row rowsep='1'>
396        <entry colsep='1' rowsep='1'>
397         <para>
398          <emphasis role='strong'>
399           <literal>u</literal>
400          </emphasis>
401         </para>
402        </entry>
403        <entry colsep='1' rowsep='1'>
404         <para>
405          <link linkend='guint32'><type>guint32</type></link>
406         </para>
407        </entry>
408       </row>
409       <row rowsep='1'>
410        <entry colsep='1' rowsep='1'>
411         <para>
412          <emphasis role='strong'>
413           <literal>x</literal>
414          </emphasis>
415         </para>
416        </entry>
417        <entry colsep='1' rowsep='1'>
418         <para>
419          <link linkend='gint64'><type>gint64</type></link>
420         </para>
421        </entry>
422       </row>
423       <row rowsep='1'>
424        <entry colsep='1' rowsep='1'>
425         <para>
426          <emphasis role='strong'>
427           <literal>t</literal>
428          </emphasis>
429         </para>
430        </entry>
431        <entry colsep='1' rowsep='1'>
432         <para>
433          <link linkend='guint64'><type>guint64</type></link>
434         </para>
435        </entry>
436       </row>
437       <row rowsep='1'>
438        <entry colsep='1' rowsep='1'>
439         <para>
440          <emphasis role='strong'>
441           <literal>h</literal>
442          </emphasis>
443         </para>
444        </entry>
445        <entry colsep='1' rowsep='1'>
446         <para>
447          <link linkend='gint32'><type>gint32</type></link>
448         </para>
449        </entry>
450       </row>
451       <row rowsep='1'>
452        <entry colsep='1' rowsep='1'>
453         <para>
454          <emphasis role='strong'>
455           <literal>d</literal>
456          </emphasis>
457         </para>
458        </entry>
459        <entry colsep='1' rowsep='1'>
460         <para>
461          <link linkend='gdouble'><type>gdouble</type></link>
462         </para>
463        </entry>
464       </row>
465      </tbody>
466     </tgroup>
467    </informaltable>
468
469    <anchor id='gvariant-varargs'/>
470    <para>
471     Note that in C, small integer types in variable argument lists are promoted up to <link
472     linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
473     read back accordingly.  <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
474     currently suported.  This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
475     with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
476     '<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
477     '<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'.  Specifically, you can use integer
478     literals with these characters.
479    </para>
480
481    <para>
482     When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
483     provide is 64 bit.  This means that you should use a cast or make use of the
484     <link linkend='G-GINT64-CONSTANT:CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
485     <link linkend='G-GUINT64-CONSTANT:CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
486    </para>
487
488    <para>
489     No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
490     it operates with pointers.  The pointers must always point to a memory region of exactly the correct size.
491    </para>
492
493    <refsect3>
494     <title>Examples</title>
495     <informalexample><programlisting>
496 <![CDATA[GVariant *value1, *value2, *value3, *value4;
497
498 value1 = g_variant_new ("y", 200);
499 value2 = g_variant_new ("b", TRUE);
500 value3 = g_variant_new ("d", 37.5):
501 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
502
503 {
504   gdouble floating;
505   gboolean truth;
506   gint64 bignum;
507
508
509   g_variant_get (value1, "y", NULL);      /* ignore the value. */
510   g_variant_get (value2, "b", &truth);
511   g_variant_get (value3, "d", &floating);
512   g_variant_get (value4, "x", &bignum);
513 }]]></programlisting></informalexample>
514    </refsect3>
515   </refsect2>
516
517   <refsect2 id='gvariant-format-strings-strings'>
518    <title>Strings</title>
519    <para>
520     <emphasis role='strong'>
521      Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
522     </emphasis>
523    </para>
524
525    <para>
526     String conversions occur to and from standard nul-terminated C strings.  Upon encountering an
527     '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
528     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
529     <link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
530     <link linkend='NULL:CAPS'><literal>NULL</literal></link> is not a valid string.  If the '<literal>o</literal>' or
531     '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
532     object path or DBus type signature, respectively.
533    </para>
534    <para>
535     Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
536     linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
537     <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
538     sets it to a newly-allocated copy of the string.  It is appropriate to free this copy using
539     <link linkend='g-free'><function>g_free()</function></link>.
540     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
541     string should be ignored (in which case no copy is made).
542    </para>
543
544    <refsect3>
545     <title>Examples</title>
546     <informalexample><programlisting>
547 <![CDATA[GVariant *value1, *value2, *value3;
548
549 value1 = g_variant_new ("s", "hello world!");
550 value2 = g_variant_new ("o", "/must/be/a/valid/path");
551 value3 = g_variant_new ("g", "iias");
552
553 #if 0
554   g_variant_new ("s", NULL);      /* not valid: NULL is not a string. */
555 #endif
556
557 {
558   gchar *result;
559
560   g_variant_get (value1, "s", &result);
561   g_print ("It was '%s'\n", result);
562   g_free (result);
563 }]]></programlisting></informalexample>
564    </refsect3>
565   </refsect2>
566
567   <refsect2 id='gvariant-format-strings-variants'>
568    <title>Variants</title>
569    <para>
570     <emphasis role='strong'>
571      Characters: <literal>v</literal>
572     </emphasis>
573    </para>
574
575    <para>
576     Upon encountering a '<literal>v</literal>',
577     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
578     linkend='GVariant'>GVariant</link> *)</code>.  The value of the
579     <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
580    </para>
581    <para>
582     Upon encountering a '<literal>v</literal>', <link
583     linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
584     <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
585     </code>).  It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
586     containing the contents of the variant value.  It is appropriate to free this reference using
587     <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
588     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
589     ignored (in which case no new reference is created).
590    </para>
591
592    <refsect3>
593     <title>Examples</title>
594     <informalexample><programlisting>
595 <![CDATA[GVariant *x, *y;
596
597 /* the following two lines are equivalent: */
598 x = g_variant_new ("v", y);
599 x = g_variant_new_variant (y);
600
601 /* as are these: */
602 g_variant_get (x, "v", &y);
603 y = g_variant_get_variant (x);]]></programlisting></informalexample>
604    </refsect3>
605   </refsect2>
606
607
608   <refsect2 id='gvariant-format-strings-arrays'>
609    <title>Arrays</title>
610    <para>
611     <emphasis role='strong'>
612      Characters: <literal>a</literal>
613     </emphasis>
614    </para>
615
616    <para>
617     Upon encountering an '<literal>a</literal>' character followed by a type string,
618     <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
619     <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
620     for an array of the type given in the type string.  The builder will have
621     <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
622     result will be used as the value.  As a special exception, if the given type string is a definite type, then
623     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
624    </para>
625
626    <para>
627     Upon encountering an '<literal>a</literal>' character followed by a type string,
628     <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
629     <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
630     <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
631     A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
632     This iterator should be freed when you are done with it, using
633     <link linkend='g-variant-iter-free'><function>g_variant_iter_free()</function></link>.
634     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
635     should be ignored.
636    </para>
637
638    <refsect3>
639     <title>Examples</title>
640     <informalexample><programlisting>
641 <![CDATA[GVariantBuilder *builder;
642 GVariant *value;
643
644 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
645 g_variant_builder_add (builder, "s", "when");
646 g_variant_builder_add (builder, "s", "in");
647 g_variant_builder_add (builder, "s", "the");
648 g_variant_builder_add (builder, "s", "course");
649 value = g_variant_new ("as", builder);
650 g_variant_builder_unref (builder);
651
652 {
653   GVariantIter *iter;
654   gchar *str;
655
656   g_variant_get (value, "as", &iter);
657   while (g_variant_iter_loop (iter, "s", &str))
658     g_print ("%s\n", str);
659   g_variant_iter_free (iter);
660 }
661
662 g_variant_unref (value);]]></programlisting></informalexample>
663    </refsect3>
664   </refsect2>
665
666   <refsect2 id='gvariant-format-strings-maybe-types'>
667    <title>Maybe Types</title>
668    <para>
669     <emphasis role='strong'>
670      Characters: <literal>m</literal>
671     </emphasis>
672    </para>
673    <para>
674     Maybe types are handled in two separate ways depending on the format string that follows the
675     '<literal>m</literal>'.  The method that is used currently depends entirely on the character immediately following the
676     '<literal>m</literal>'.
677    </para>
678
679    <para>
680     The first way is used with format strings starting with '<literal>a</literal>', '<literal>s</literal>',
681     '<literal>o</literal>', '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>',
682     '<literal>*</literal>', '<literal>?</literal>', '<literal>r</literal>', '<literal>&amp;</literal>', or
683     '<literal>^</literal>'.  In all of these cases, for non-maybe types,
684     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a pointer to a
685     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> value and
686     <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
687     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer.  When any of these format strings are
688     prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
689     <link linkend='NULL:CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
690    </para>
691    <para>
692     Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
693     here.  Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
694     the Nothing value -- not an empty array.
695    </para>
696    <para>
697     The second way is used with all other format strings.  For
698     <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
699     <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
700     <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
701     <code>(<link linkend='gboolean'>gboolean</link> *)</code>.  Following this argument, the arguments that are normally
702     collected for the equivalent non-maybe type will be collected.
703    </para>
704    <para>
705     If <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> is given to
706     <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
707     the collected arguments are ignored.  Otherwise (if <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> was
708     given), the arguments are used in the normal way to create the Just value.
709    </para>
710    <para>
711     If <link linkend='NULL:CAPS'><literal>NULL</literal></link> is given to
712     <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored.  If a
713     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
714     whether the value was Just.  In the case that the value was Just, the
715     <link linkend='gboolean'><type>gboolean</type></link> will be set to
716     <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
717     way.  In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
718     <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
719     but have their values set to binary zero.
720    </para>
721
722    <refsect3>
723     <title>Examples</title>
724     <informalexample><programlisting>
725 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
726 value1 = g_variant_new ("ms", "Hello world");
727 value2 = g_variant_new ("ms", NULL);
728 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
729 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done");          /* both '-1' are ignored. */
730 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
731
732 {
733   GVariant *contents;
734   const gchar *cstr;
735   gboolean just;
736   gint32 x, y;
737   gchar *str;
738
739   g_variant_get (value1, "ms", &str);
740   if (str != NULL)
741     g_print ("str: %s\n", str);
742   else
743     g_print ("it was null\n");
744   g_free (str);
745
746
747   g_variant_get (value2, "m&s", &cstr);
748   if (cstr != NULL)
749     g_print ("str: %s\n", cstr);
750   else
751     g_print ("it was null\n");
752   /* don't free 'cstr' */
753
754
755   /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
756   g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
757   g_print ("string is %s\n", str);
758   g_free (str);
759
760   /* note: &s used, so g_free() not needed */
761   g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
762   if (just)
763     g_print ("it was (%d, %d)\n", x, y);
764   else
765     g_print ("it was null\n");
766   g_print ("string is %s\n", cstr);
767   /* don't free 'cstr' */
768
769
770   g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
771   if (contents != NULL)
772     {
773       g_variant_get (contents, "(ii)", &x, &y);
774       g_print ("it was (%d, %d)\n", x, y);
775       g_variant_unref (contents);
776     }
777   else
778     g_print ("it was null\n");
779 }]]></programlisting></informalexample>
780    </refsect3>
781   </refsect2>
782
783   <refsect2 id='gvariant-format-strings-tuples'>
784    <title>Tuples</title>
785    <para>
786     <emphasis role='strong'>
787      Characters: <code>()</code>
788     </emphasis>
789    </para>
790
791    <para>
792     Tuples are handled by handling each item in the tuple, in sequence.  Each item is handled in the usual way.
793    </para>
794
795    <refsect3>
796     <title>Examples</title>
797     <informalexample><programlisting>
798 <![CDATA[GVariant *value1, *value2;
799
800 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
801 value2 = g_variant_new ("()");
802
803 {
804   gchar *string;
805   gint x, y;
806
807   g_variant_get (value1, "(s(ii))", &string, &x, &y);
808   g_print ("%s, %d, %d\n", string, x, y);
809   g_free (string);
810
811   g_variant_get (value2, "()");   /* do nothing... */
812 }]]></programlisting></informalexample>
813    </refsect3>
814   </refsect2>
815
816   <refsect2 id='gvariant-format-strings-dictionaries'>
817    <title>Dictionaries</title>
818    <para>
819     <emphasis role='strong'>
820      Characters: <code>{}</code>
821     </emphasis>
822    </para>
823
824    <para>
825     Dictionary entries are handled by handling first the key, then the value.  Each is handled in the usual way.
826    </para>
827
828    <refsect3>
829     <title>Examples</title>
830     <informalexample><programlisting>
831 <![CDATA[GVariantBuilder *b;
832 GVariant *dict;
833
834 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
835 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
836 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
837 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
838    </refsect3>
839   </refsect2>
840
841   <refsect2 id='gvariant-format-strings-gvariant'>
842    <title>GVariant *</title>
843    <para>
844     <emphasis role='strong'>
845      Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
846     </emphasis>
847
848    </para>
849    <para>
850     Upon encountering a '<literal>@</literal>' in front of a type string,
851     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
852     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer to a
853     <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
854     create the value.  The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
855     type string following the '<literal>@</literal>'.  '<literal>*</literal>' is
856     the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
857     '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
858     <link linkend='GVariant'><type>GVariant</type></link> of any basic type).  '<literal>r</literal>' is the same as
859     '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
860    </para>
861    <para>
862     Upon encountering a '<literal>@</literal>' in front of a type string,
863     <link linkend='g-variant-get'><function>g_variant_get()</function></link>
864     takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
865     <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
866     <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
867     C types in the usual way).  <link linkend='NULL:CAPS'><literal>NULL</literal></link> can be given to ignore the
868     value.  '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
869     what is stated above.
870    </para>
871    <para>
872     You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
873     use of '<literal>@</literal>'.  Using the other characters where possible is recommended, however, due to the
874     improvements in type safety and code self-documentation.
875    </para>
876
877    <refsect3>
878     <title>Examples</title>
879     <informalexample><programlisting>
880 <![CDATA[GVariant *value1, *value2;
881
882 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
883
884 /* note: consumes floating reference count on 'value1' */
885 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
886
887 {
888   const gchar *string;
889   GVariant *tmp;
890   gsize length;
891   gint x, y, z;
892
893   g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
894   string = g_variant_get_string (tmp, &length);
895   g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
896   g_variant_unref (tmp);
897
898   /* quick way to skip all the values in a tuple */
899   g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
900   g_print ("i only got the string: %s\n", string);
901   g_free (string);
902 }]]></programlisting></informalexample>
903    </refsect3>
904   </refsect2>
905
906   <refsect2 id='gvariant-format-strings-pointers'>
907    <title>Pointers</title>
908    <para>
909     <emphasis role='strong'>
910      Characters: <code>&amp;</code>
911     </emphasis>
912    </para>
913
914    <para>
915     The '<code>&amp;</code>' character is used to indicate that serialised data should be directly exchanged via a
916     pointer.
917    </para>
918    <para>
919     Currently, the only use for this character is when it is applied to a string (ie: '<literal>&amp;s</literal>',
920     '<literal>&amp;o</literal>' or '<code>&amp;g</code>').  For
921     <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect.  The string
922     is collected and duplicated normally.  For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
923     it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
924     returned.  This pointer should not be freed.  Validity checks are performed to ensure that the string data will
925     always be properly nul-terminated.
926    </para>
927
928    <refsect3>
929     <title>Examples</title>
930     <informalexample><programlisting>
931 <![CDATA[{
932   const gchar *str;
933   GVariant *value;
934
935   value = g_variant_new ("&s", "hello world");
936   str = g_variant_get (value, "&s", &str);
937   g_print ("string is: %s\n", str);
938   /* no need to free str */
939 }]]></programlisting></informalexample>
940    </refsect3>
941   </refsect2>
942
943   <refsect2 id='gvariant-format-strings-convenience'>
944    <title>Convenience Conversions</title>
945    <para>
946     <emphasis role='strong'>
947      Characters: <literal>^</literal>
948     </emphasis>
949    </para>
950
951    <para>
952     The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
953     of strings or bytestrings.  It has a number of forms.
954    </para>
955
956    <para>
957     In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
958     pointer value is collected from the variable arguments and passed to a function (as given in the table below).
959     The result of that function is used as the value for this position.  When used with
960     <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
961     the function (given in the table) and returned by reference.
962    </para>
963
964    <informaltable>
965     <tgroup cols='2'>
966      <colspec colname='col_0'/>
967      <colspec colname='col_1'/>
968      <colspec colname='col_2'/>
969      <tbody>
970
971       <row rowsep='1'>
972        <entry colsep='1' rowsep='1'>
973         <para>
974          <emphasis role='strong'>Conversion</emphasis>
975         </para>
976        </entry>
977        <entry colsep='1' rowsep='1'>
978         <para>
979           <emphasis role='strong'>
980             Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
981           </emphasis>
982         </para>
983        </entry>
984        <entry colsep='1' rowsep='1'>
985         <para>
986           <emphasis role='strong'>
987             Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
988           </emphasis>
989         </para>
990        </entry>
991       </row>
992
993       <row rowsep='1'>
994        <entry colsep='1' rowsep='1'>
995         <para>
996          <emphasis role='strong'>
997           <literal>^as</literal>
998          </emphasis>
999         </para>
1000        </entry>
1001        <entry colsep='1' rowsep='1' morerows='1'>
1002         <para>
1003          equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
1004         </para>
1005        </entry>
1006        <entry colsep='1' rowsep='1'>
1007         <para>
1008          equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1009         </para>
1010        </entry>
1011       </row>
1012
1013       <row rowsep='1'>
1014        <entry colsep='1' rowsep='1'>
1015         <para>
1016          <emphasis role='strong'>
1017           <literal>^a&amp;s</literal>
1018          </emphasis>
1019         </para>
1020        </entry>
1021        <entry colsep='1' rowsep='1'>
1022         <para>
1023          equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1024         </para>
1025        </entry>
1026       </row>
1027
1028       <row rowsep='1'>
1029        <entry colsep='1' rowsep='1'>
1030         <para>
1031          <emphasis role='strong'>
1032           <literal>^ao</literal>
1033          </emphasis>
1034         </para>
1035        </entry>
1036        <entry colsep='1' rowsep='1' morerows='1'>
1037         <para>
1038          equivalent to <link linkend='g-variant-new-objv'><function>g_variant_new_objv()</function></link>
1039         </para>
1040        </entry>
1041        <entry colsep='1' rowsep='1'>
1042         <para>
1043          equivalent to <link linkend='g-variant-dup-objv'><function>g_variant_dup_objv()</function></link>
1044         </para>
1045        </entry>
1046       </row>
1047
1048       <row rowsep='1'>
1049        <entry colsep='1' rowsep='1'>
1050         <para>
1051          <emphasis role='strong'>
1052           <literal>^a&amp;o</literal>
1053          </emphasis>
1054         </para>
1055        </entry>
1056        <entry colsep='1' rowsep='1'>
1057         <para>
1058          equivalent to <link linkend='g-variant-get-objv'><function>g_variant_get_objv()</function></link>
1059         </para>
1060        </entry>
1061       </row>
1062
1063       <row rowsep='1'>
1064        <entry colsep='1' rowsep='1'>
1065         <para>
1066          <emphasis role='strong'>
1067           <literal>^ay</literal>
1068          </emphasis>
1069         </para>
1070        </entry>
1071        <entry colsep='1' rowsep='1' morerows='1'>
1072         <para>
1073          equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1074         </para>
1075        </entry>
1076        <entry colsep='1' rowsep='1'>
1077         <para>
1078          equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1079         </para>
1080        </entry>
1081       </row>
1082
1083       <row rowsep='1'>
1084        <entry colsep='1' rowsep='1'>
1085         <para>
1086          <emphasis role='strong'>
1087           <literal>^&amp;ay</literal>
1088          </emphasis>
1089         </para>
1090        </entry>
1091        <entry colsep='1' rowsep='1'>
1092         <para>
1093          equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1094         </para>
1095        </entry>
1096       </row>
1097
1098       <row rowsep='1'>
1099        <entry colsep='1' rowsep='1'>
1100         <para>
1101          <emphasis role='strong'>
1102           <literal>^aay</literal>
1103          </emphasis>
1104         </para>
1105        </entry>
1106        <entry colsep='1' rowsep='1' morerows='1'>
1107         <para>
1108          equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1109         </para>
1110        </entry>
1111        <entry colsep='1' rowsep='1'>
1112         <para>
1113          equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1114         </para>
1115        </entry>
1116       </row>
1117
1118       <row rowsep='1'>
1119        <entry colsep='1' rowsep='1'>
1120         <para>
1121          <emphasis role='strong'>
1122           <literal>^a&amp;ay</literal>
1123          </emphasis>
1124         </para>
1125        </entry>
1126        <entry colsep='1' rowsep='1'>
1127         <para>
1128          equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>
1129         </para>
1130        </entry>
1131       </row>
1132
1133      </tbody>
1134     </tgroup>
1135    </informaltable>
1136   </refsect2>
1137  </refsect1>
1138 </refentry>