gvariant: Clarify 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, 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 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.
235        </para>
236       </entry>
237      </row>
238
239      <row rowsep='1'>
240       <entry colsep='1' rowsep='1'>
241        <para>
242         <emphasis role='strong'>
243          <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
244         </emphasis>
245        </para>
246       </entry>
247       <entry colsep='1' rowsep='1'>
248        <para>
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.
252        </para>
253       </entry>
254      </row>
255
256      <row rowsep='1'>
257       <entry colsep='1' rowsep='1'>
258        <para>
259         <emphasis role='strong'><literal>&amp;</literal></emphasis>
260        </para>
261       </entry>
262       <entry colsep='1' rowsep='1'>
263        <para>
264         Used as a prefix for a GVariant type string (not a prefix for a format string, so <literal>&amp;s</literal> is
265         a valid format string but <literal>&amp;@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.
269        </para>
270       </entry>
271      </row>
272
273      <row rowsep='1'>
274       <entry colsep='1' rowsep='1'>
275        <para>
276         <emphasis role='strong'><literal>^</literal></emphasis>
277        </para>
278       </entry>
279       <entry colsep='1' rowsep='1'>
280        <para>
281         Used as a prefix on some specific types of format strings.  See
282         <link linkend='gvariant-format-strings-convenience'>Convenience Conversions</link> below.
283        </para>
284       </entry>
285      </row>
286     </tbody>
287    </tgroup>
288   </informaltable>
289
290
291   <refsect2 id='gvariant-format-strings-numeric-types'>
292    <title>Numeric Types</title>
293    <para>
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>,
297      <literal>d</literal>
298     </emphasis>
299    </para>
300
301    <para>
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).
306    </para>
307
308    <para>
309     The equivalent C types are as follows:
310    </para>
311
312    <informaltable>
313     <tgroup cols='2'>
314      <colspec colname='col_0'/><colspec colname='col_1'/>
315      <tbody>
316       <row rowsep='1'>
317        <entry colsep='1' rowsep='1'>
318         <para>
319           <emphasis role='strong'>Character</emphasis>
320         </para>
321        </entry>
322        <entry colsep='1' rowsep='1'>
323         <para>
324           <emphasis role='strong'>Equivalent C type</emphasis>
325         </para>
326        </entry>
327       </row>
328       <row rowsep='1'>
329        <entry colsep='1' rowsep='1'>
330         <para>
331          <emphasis role='strong'>
332           <literal>b</literal>
333          </emphasis>
334         </para>
335        </entry>
336        <entry colsep='1' rowsep='1'>
337         <para>
338          <link linkend='gboolean'><type>gboolean</type></link>
339         </para>
340        </entry>
341       </row>
342       <row rowsep='1'>
343        <entry colsep='1' rowsep='1'>
344         <para>
345          <emphasis role='strong'>
346           <literal>y</literal>
347          </emphasis>
348         </para>
349        </entry>
350        <entry colsep='1' rowsep='1'>
351         <para>
352          <link linkend='guchar'><type>guchar</type></link>
353         </para>
354        </entry>
355       </row>
356       <row rowsep='1'>
357        <entry colsep='1' rowsep='1'>
358         <para>
359          <emphasis role='strong'>
360           <literal>n</literal>
361          </emphasis>
362         </para>
363        </entry>
364        <entry colsep='1' rowsep='1'>
365         <para>
366          <link linkend='gint16'><type>gint16</type></link>
367         </para>
368        </entry>
369       </row>
370       <row rowsep='1'>
371        <entry colsep='1' rowsep='1'>
372         <para>
373          <emphasis role='strong'>
374           <literal>q</literal>
375          </emphasis>
376         </para>
377        </entry>
378        <entry colsep='1' rowsep='1'>
379         <para>
380          <link linkend='guint16'><type>guint16</type></link>
381         </para>
382        </entry>
383       </row>
384       <row rowsep='1'>
385        <entry colsep='1' rowsep='1'>
386         <para>
387          <emphasis role='strong'>
388           <literal>i</literal>
389          </emphasis>
390         </para>
391        </entry>
392        <entry colsep='1' rowsep='1'>
393         <para>
394          <link linkend='gint32'><type>gint32</type></link>
395         </para>
396        </entry>
397       </row>
398       <row rowsep='1'>
399        <entry colsep='1' rowsep='1'>
400         <para>
401          <emphasis role='strong'>
402           <literal>u</literal>
403          </emphasis>
404         </para>
405        </entry>
406        <entry colsep='1' rowsep='1'>
407         <para>
408          <link linkend='guint32'><type>guint32</type></link>
409         </para>
410        </entry>
411       </row>
412       <row rowsep='1'>
413        <entry colsep='1' rowsep='1'>
414         <para>
415          <emphasis role='strong'>
416           <literal>x</literal>
417          </emphasis>
418         </para>
419        </entry>
420        <entry colsep='1' rowsep='1'>
421         <para>
422          <link linkend='gint64'><type>gint64</type></link>
423         </para>
424        </entry>
425       </row>
426       <row rowsep='1'>
427        <entry colsep='1' rowsep='1'>
428         <para>
429          <emphasis role='strong'>
430           <literal>t</literal>
431          </emphasis>
432         </para>
433        </entry>
434        <entry colsep='1' rowsep='1'>
435         <para>
436          <link linkend='guint64'><type>guint64</type></link>
437         </para>
438        </entry>
439       </row>
440       <row rowsep='1'>
441        <entry colsep='1' rowsep='1'>
442         <para>
443          <emphasis role='strong'>
444           <literal>h</literal>
445          </emphasis>
446         </para>
447        </entry>
448        <entry colsep='1' rowsep='1'>
449         <para>
450          <link linkend='gint32'><type>gint32</type></link>
451         </para>
452        </entry>
453       </row>
454       <row rowsep='1'>
455        <entry colsep='1' rowsep='1'>
456         <para>
457          <emphasis role='strong'>
458           <literal>d</literal>
459          </emphasis>
460         </para>
461        </entry>
462        <entry colsep='1' rowsep='1'>
463         <para>
464          <link linkend='gdouble'><type>gdouble</type></link>
465         </para>
466        </entry>
467       </row>
468      </tbody>
469     </tgroup>
470    </informaltable>
471
472    <anchor id='gvariant-varargs'/>
473    <para>
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.
482    </para>
483
484    <para>
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.
489    </para>
490
491    <para>
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.
494    </para>
495
496    <refsect3>
497     <title>Examples</title>
498     <informalexample><programlisting>
499 <![CDATA[GVariant *value1, *value2, *value3, *value4;
500
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));
505
506 {
507   gdouble floating;
508   gboolean truth;
509   gint64 bignum;
510
511
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>
517    </refsect3>
518   </refsect2>
519
520   <refsect2 id='gvariant-format-strings-strings'>
521    <title>Strings</title>
522    <para>
523     <emphasis role='strong'>
524      Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
525     </emphasis>
526    </para>
527
528    <para>
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.  If the '<literal>o</literal>' or
534     '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
535     object path or DBus type signature, respectively.
536    </para>
537    <para>
538     Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
539     linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
540     <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
541     sets it to a newly-allocated copy of the string.  It is appropriate to free this copy using
542     <link linkend='g-free'><function>g_free()</function></link>.
543     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
544     string should be ignored (in which case no copy is made).
545    </para>
546
547    <refsect3>
548     <title>Examples</title>
549     <informalexample><programlisting>
550 <![CDATA[GVariant *value1, *value2, *value3;
551
552 value1 = g_variant_new ("s", "hello world!");
553 value2 = g_variant_new ("o", "/must/be/a/valid/path");
554 value3 = g_variant_new ("g", "iias");
555
556 #if 0
557   g_variant_new ("s", NULL);      /* not valid: NULL is not a string. */
558 #endif
559
560 {
561   gchar *result;
562
563   g_variant_get (value1, "s", &result);
564   g_print ("It was '%s'\n", result);
565   g_free (result);
566 }]]></programlisting></informalexample>
567    </refsect3>
568   </refsect2>
569
570   <refsect2 id='gvariant-format-strings-variants'>
571    <title>Variants</title>
572    <para>
573     <emphasis role='strong'>
574      Characters: <literal>v</literal>
575     </emphasis>
576    </para>
577
578    <para>
579     Upon encountering a '<literal>v</literal>',
580     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
581     linkend='GVariant'>GVariant</link> *)</code>.  The value of the
582     <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
583    </para>
584    <para>
585     Upon encountering a '<literal>v</literal>', <link
586     linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
587     <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
588     </code>).  It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
589     containing the contents of the variant value.  It is appropriate to free this reference using
590     <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
591     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
592     ignored (in which case no new reference is created).
593    </para>
594
595    <refsect3>
596     <title>Examples</title>
597     <informalexample><programlisting>
598 <![CDATA[GVariant *x, *y;
599
600 /* the following two lines are equivalent: */
601 x = g_variant_new ("v", y);
602 x = g_variant_new_variant (y);
603
604 /* as are these: */
605 g_variant_get (x, "v", &y);
606 y = g_variant_get_variant (x);]]></programlisting></informalexample>
607    </refsect3>
608   </refsect2>
609
610
611   <refsect2 id='gvariant-format-strings-arrays'>
612    <title>Arrays</title>
613    <para>
614     <emphasis role='strong'>
615      Characters: <literal>a</literal>
616     </emphasis>
617    </para>
618
619    <para>
620     Upon encountering an '<literal>a</literal>' character followed by a type string,
621     <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
622     <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
623     for an array of the type given in the type string.  The builder will have
624     <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
625     result will be used as the value.  As a special exception, if the given type string is a definite type, then
626     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
627    </para>
628
629    <para>
630     Upon encountering an '<literal>a</literal>' character followed by a type string,
631     <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
632     <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
633     <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
634     A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
635     This iterator should be freed when you are done with it, using
636     <link linkend='g-variant-iter-free'><function>g_variant_iter_free()</function></link>.
637     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
638     should be ignored.
639    </para>
640
641    <refsect3>
642     <title>Examples</title>
643     <informalexample><programlisting>
644 <![CDATA[GVariantBuilder *builder;
645 GVariant *value;
646
647 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
648 g_variant_builder_add (builder, "s", "when");
649 g_variant_builder_add (builder, "s", "in");
650 g_variant_builder_add (builder, "s", "the");
651 g_variant_builder_add (builder, "s", "course");
652 value = g_variant_new ("as", builder);
653 g_variant_builder_unref (builder);
654
655 {
656   GVariantIter *iter;
657   gchar *str;
658
659   g_variant_get (value, "as", &iter);
660   while (g_variant_iter_loop (iter, "s", &str))
661     g_print ("%s\n", str);
662   g_variant_iter_free (iter);
663 }
664
665 g_variant_unref (value);]]></programlisting></informalexample>
666    </refsect3>
667   </refsect2>
668
669   <refsect2 id='gvariant-format-strings-maybe-types'>
670    <title>Maybe Types</title>
671    <para>
672     <emphasis role='strong'>
673      Characters: <literal>m</literal>
674     </emphasis>
675    </para>
676    <para>
677     Maybe types are handled in two separate ways depending on the format string that follows the
678     '<literal>m</literal>'.  The method that is used currently depends entirely on the character immediately following the
679     '<literal>m</literal>'.
680    </para>
681
682    <para>
683     The first way is used with format strings starting with '<literal>a</literal>', '<literal>s</literal>',
684     '<literal>o</literal>', '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>',
685     '<literal>*</literal>', '<literal>?</literal>', '<literal>r</literal>', '<literal>&amp;</literal>', or
686     '<literal>^</literal>'.  In all of these cases, for non-maybe types,
687     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a pointer to a
688     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> value and
689     <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
690     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer.  When any of these format strings are
691     prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
692     <link linkend='NULL:CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
693    </para>
694    <para>
695     Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
696     here.  Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
697     the Nothing value -- not an empty array.
698    </para>
699    <para>
700     The second way is used with all other format strings.  For
701     <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
702     <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
703     <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
704     <code>(<link linkend='gboolean'>gboolean</link> *)</code>.  Following this argument, the arguments that are normally
705     collected for the equivalent non-maybe type will be collected.
706    </para>
707    <para>
708     If <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> is given to
709     <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
710     the collected arguments are ignored.  Otherwise (if <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> was
711     given), the arguments are used in the normal way to create the Just value.
712    </para>
713    <para>
714     If <link linkend='NULL:CAPS'><literal>NULL</literal></link> is given to
715     <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored.  If a
716     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
717     whether the value was Just.  In the case that the value was Just, the
718     <link linkend='gboolean'><type>gboolean</type></link> will be set to
719     <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
720     way.  In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
721     <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
722     but have their values set to binary zero.
723    </para>
724
725    <refsect3>
726     <title>Examples</title>
727     <informalexample><programlisting>
728 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
729 value1 = g_variant_new ("ms", "Hello world");
730 value2 = g_variant_new ("ms", NULL);
731 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
732 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done");          /* both '-1' are ignored. */
733 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
734
735 {
736   GVariant *contents;
737   const gchar *cstr;
738   gboolean just;
739   gint32 x, y;
740   gchar *str;
741
742   g_variant_get (value1, "ms", &str);
743   if (str != NULL)
744     g_print ("str: %s\n", str);
745   else
746     g_print ("it was null\n");
747   g_free (str);
748
749
750   g_variant_get (value2, "m&s", &cstr);
751   if (cstr != NULL)
752     g_print ("str: %s\n", cstr);
753   else
754     g_print ("it was null\n");
755   /* don't free 'cstr' */
756
757
758   /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
759   g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
760   g_print ("string is %s\n", str);
761   g_free (str);
762
763   /* note: &s used, so g_free() not needed */
764   g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
765   if (just)
766     g_print ("it was (%d, %d)\n", x, y);
767   else
768     g_print ("it was null\n");
769   g_print ("string is %s\n", cstr);
770   /* don't free 'cstr' */
771
772
773   g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
774   if (contents != NULL)
775     {
776       g_variant_get (contents, "(ii)", &x, &y);
777       g_print ("it was (%d, %d)\n", x, y);
778       g_variant_unref (contents);
779     }
780   else
781     g_print ("it was null\n");
782 }]]></programlisting></informalexample>
783    </refsect3>
784   </refsect2>
785
786   <refsect2 id='gvariant-format-strings-tuples'>
787    <title>Tuples</title>
788    <para>
789     <emphasis role='strong'>
790      Characters: <code>()</code>
791     </emphasis>
792    </para>
793
794    <para>
795     Tuples are handled by handling each item in the tuple, in sequence.  Each item is handled in the usual way.
796    </para>
797
798    <refsect3>
799     <title>Examples</title>
800     <informalexample><programlisting>
801 <![CDATA[GVariant *value1, *value2;
802
803 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
804 value2 = g_variant_new ("()");
805
806 {
807   gchar *string;
808   gint x, y;
809
810   g_variant_get (value1, "(s(ii))", &string, &x, &y);
811   g_print ("%s, %d, %d\n", string, x, y);
812   g_free (string);
813
814   g_variant_get (value2, "()");   /* do nothing... */
815 }]]></programlisting></informalexample>
816    </refsect3>
817   </refsect2>
818
819   <refsect2 id='gvariant-format-strings-dictionaries'>
820    <title>Dictionaries</title>
821    <para>
822     <emphasis role='strong'>
823      Characters: <code>{}</code>
824     </emphasis>
825    </para>
826
827    <para>
828     Dictionary entries are handled by handling first the key, then the value.  Each is handled in the usual way.
829    </para>
830
831    <refsect3>
832     <title>Examples</title>
833     <informalexample><programlisting>
834 <![CDATA[GVariantBuilder *b;
835 GVariant *dict;
836
837 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
838 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
839 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
840 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
841    </refsect3>
842   </refsect2>
843
844   <refsect2 id='gvariant-format-strings-gvariant'>
845    <title>GVariant *</title>
846    <para>
847     <emphasis role='strong'>
848      Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
849     </emphasis>
850
851    </para>
852    <para>
853     Upon encountering a '<literal>@</literal>' in front of a type string,
854     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
855     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer to a
856     <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
857     create the value.  The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
858     type string following the '<literal>@</literal>'.  '<literal>*</literal>' is
859     the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
860     '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
861     <link linkend='GVariant'><type>GVariant</type></link> of any basic type).  '<literal>r</literal>' is the same as
862     '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
863    </para>
864    <para>
865     Upon encountering a '<literal>@</literal>' in front of a type string,
866     <link linkend='g-variant-get'><function>g_variant_get()</function></link>
867     takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
868     <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
869     <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
870     C types in the usual way).  <link linkend='NULL:CAPS'><literal>NULL</literal></link> can be given to ignore the
871     value.  '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
872     what is stated above.
873    </para>
874    <para>
875     You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
876     use of '<literal>@</literal>'.  Using the other characters where possible is recommended, however, due to the
877     improvements in type safety and code self-documentation.
878    </para>
879
880    <refsect3>
881     <title>Examples</title>
882     <informalexample><programlisting>
883 <![CDATA[GVariant *value1, *value2;
884
885 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
886
887 /* note: consumes floating reference count on 'value1' */
888 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
889
890 {
891   const gchar *string;
892   GVariant *tmp;
893   gsize length;
894   gint x, y, z;
895
896   g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
897   string = g_variant_get_string (tmp, &length);
898   g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
899   g_variant_unref (tmp);
900
901   /* quick way to skip all the values in a tuple */
902   g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
903   g_print ("i only got the string: %s\n", string);
904   g_free (string);
905 }]]></programlisting></informalexample>
906    </refsect3>
907   </refsect2>
908
909   <refsect2 id='gvariant-format-strings-pointers'>
910    <title>Pointers</title>
911    <para>
912     <emphasis role='strong'>
913      Characters: <code>&amp;</code>
914     </emphasis>
915    </para>
916
917    <para>
918     The '<code>&amp;</code>' character is used to indicate that serialised data should be directly exchanged via a
919     pointer.
920    </para>
921    <para>
922     Currently, the only use for this character is when it is applied to a string (ie: '<literal>&amp;s</literal>',
923     '<literal>&amp;o</literal>' or '<code>&amp;g</code>').  For
924     <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect.  The string
925     is collected and duplicated normally.  For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
926     it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
927     returned.  This pointer should not be freed.  Validity checks are performed to ensure that the string data will
928     always be properly nul-terminated.
929    </para>
930
931    <refsect3>
932     <title>Examples</title>
933     <informalexample><programlisting>
934 <![CDATA[{
935   const gchar *str;
936   GVariant *value;
937
938   value = g_variant_new ("&s", "hello world");
939   str = g_variant_get (value, "&s", &str);
940   g_print ("string is: %s\n", str);
941   /* no need to free str */
942 }]]></programlisting></informalexample>
943    </refsect3>
944   </refsect2>
945
946   <refsect2 id='gvariant-format-strings-convenience'>
947    <title>Convenience Conversions</title>
948    <para>
949     <emphasis role='strong'>
950      Characters: <literal>^</literal>
951     </emphasis>
952    </para>
953
954    <para>
955     The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
956     of strings or bytestrings.  It has a number of forms.
957    </para>
958
959    <para>
960     In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
961     pointer value is collected from the variable arguments and passed to a function (as given in the table below).
962     The result of that function is used as the value for this position.  When used with
963     <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
964     the function (given in the table) and returned by reference.
965    </para>
966
967    <informaltable>
968     <tgroup cols='2'>
969      <colspec colname='col_0'/>
970      <colspec colname='col_1'/>
971      <colspec colname='col_2'/>
972      <tbody>
973
974       <row rowsep='1'>
975        <entry colsep='1' rowsep='1'>
976         <para>
977          <emphasis role='strong'>Conversion</emphasis>
978         </para>
979        </entry>
980        <entry colsep='1' rowsep='1'>
981         <para>
982           <emphasis role='strong'>
983             Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
984           </emphasis>
985         </para>
986        </entry>
987        <entry colsep='1' rowsep='1'>
988         <para>
989           <emphasis role='strong'>
990             Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
991           </emphasis>
992         </para>
993        </entry>
994       </row>
995
996       <row rowsep='1'>
997        <entry colsep='1' rowsep='1'>
998         <para>
999          <emphasis role='strong'>
1000           <literal>^as</literal>
1001          </emphasis>
1002         </para>
1003        </entry>
1004        <entry colsep='1' rowsep='1' morerows='1'>
1005         <para>
1006          equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
1007         </para>
1008        </entry>
1009        <entry colsep='1' rowsep='1'>
1010         <para>
1011          equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1012         </para>
1013        </entry>
1014       </row>
1015
1016       <row rowsep='1'>
1017        <entry colsep='1' rowsep='1'>
1018         <para>
1019          <emphasis role='strong'>
1020           <literal>^a&amp;s</literal>
1021          </emphasis>
1022         </para>
1023        </entry>
1024        <entry colsep='1' rowsep='1'>
1025         <para>
1026          equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1027         </para>
1028        </entry>
1029       </row>
1030
1031       <row rowsep='1'>
1032        <entry colsep='1' rowsep='1'>
1033         <para>
1034          <emphasis role='strong'>
1035           <literal>^ao</literal>
1036          </emphasis>
1037         </para>
1038        </entry>
1039        <entry colsep='1' rowsep='1' morerows='1'>
1040         <para>
1041          equivalent to <link linkend='g-variant-new-objv'><function>g_variant_new_objv()</function></link>
1042         </para>
1043        </entry>
1044        <entry colsep='1' rowsep='1'>
1045         <para>
1046          equivalent to <link linkend='g-variant-dup-objv'><function>g_variant_dup_objv()</function></link>
1047         </para>
1048        </entry>
1049       </row>
1050
1051       <row rowsep='1'>
1052        <entry colsep='1' rowsep='1'>
1053         <para>
1054          <emphasis role='strong'>
1055           <literal>^a&amp;o</literal>
1056          </emphasis>
1057         </para>
1058        </entry>
1059        <entry colsep='1' rowsep='1'>
1060         <para>
1061          equivalent to <link linkend='g-variant-get-objv'><function>g_variant_get_objv()</function></link>
1062         </para>
1063        </entry>
1064       </row>
1065
1066       <row rowsep='1'>
1067        <entry colsep='1' rowsep='1'>
1068         <para>
1069          <emphasis role='strong'>
1070           <literal>^ay</literal>
1071          </emphasis>
1072         </para>
1073        </entry>
1074        <entry colsep='1' rowsep='1' morerows='1'>
1075         <para>
1076          equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1077         </para>
1078        </entry>
1079        <entry colsep='1' rowsep='1'>
1080         <para>
1081          equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1082         </para>
1083        </entry>
1084       </row>
1085
1086       <row rowsep='1'>
1087        <entry colsep='1' rowsep='1'>
1088         <para>
1089          <emphasis role='strong'>
1090           <literal>^&amp;ay</literal>
1091          </emphasis>
1092         </para>
1093        </entry>
1094        <entry colsep='1' rowsep='1'>
1095         <para>
1096          equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1097         </para>
1098        </entry>
1099       </row>
1100
1101       <row rowsep='1'>
1102        <entry colsep='1' rowsep='1'>
1103         <para>
1104          <emphasis role='strong'>
1105           <literal>^aay</literal>
1106          </emphasis>
1107         </para>
1108        </entry>
1109        <entry colsep='1' rowsep='1' morerows='1'>
1110         <para>
1111          equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1112         </para>
1113        </entry>
1114        <entry colsep='1' rowsep='1'>
1115         <para>
1116          equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1117         </para>
1118        </entry>
1119       </row>
1120
1121       <row rowsep='1'>
1122        <entry colsep='1' rowsep='1'>
1123         <para>
1124          <emphasis role='strong'>
1125           <literal>^a&amp;ay</literal>
1126          </emphasis>
1127         </para>
1128        </entry>
1129        <entry colsep='1' rowsep='1'>
1130         <para>
1131          equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>
1132         </para>
1133        </entry>
1134       </row>
1135
1136      </tbody>
1137     </tgroup>
1138    </informaltable>
1139   </refsect2>
1140  </refsect1>
1141 </refentry>