Tizen 2.1 base
[platform/upstream/glib2.0.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 type string (not format 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 type string (not format 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    <para>
470     Note that in C, small integer types in variable argument lists are promoted up to <link
471     linkend='gint'><type>int</type></link> or <link linkend='guint'><type>unsigned int</type></link> as appropriate, and
472     read back accordingly.  <link linkend='gint'><type>int</type></link> is 32 bits on every platform on which GLib is
473     currently suported.  This means that you can use C expressions of type <link linkend='gint'><type>int</type></link>
474     with <link linkend='g-variant-new'><function>g_variant_new()</function></link> and format characters
475     '<literal>b</literal>', '<literal>y</literal>', '<literal>n</literal>', '<literal>q</literal>',
476     '<literal>i</literal>', '<literal>u</literal>' and '<literal>h</literal>'.  Specifically, you can use integer
477     literals with these characters.
478    </para>
479
480    <para>
481     When using the '<literal>x</literal>' and '<literal>t</literal>' characters, you must ensure that the value that you
482     provide is 64 bit.  This means that you should use a cast or make use of the
483     <link linkend='G-GINT64-CONSTANT:CAPS'><literal>G_GINT64_CONSTANT</literal></link> or
484     <link linkend='G-GUINT64-CONSTANT:CAPS'><literal>G_GUINT64_CONSTANT</literal></link> macros.
485    </para>
486
487    <para>
488     No type promotion occurs when using <link linkend='g-variant-get'><function>g_variant_get()</function></link> since
489     it operates with pointers.  The pointers must always point to a memory region of exactly the correct size.
490    </para>
491
492    <refsect3>
493     <title>Examples</title>
494     <informalexample><programlisting>
495 <![CDATA[GVariant *value1, *value2, *value3, *value4;
496
497 value1 = g_variant_new ("y", 200);
498 value2 = g_variant_new ("b", TRUE);
499 value3 = g_variant_new ("d", 37.5):
500 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
501
502 {
503   gdouble floating;
504   gboolean truth;
505   gint64 bignum;
506
507
508   g_variant_get (value1, "y", NULL);      /* ignore the value. */
509   g_variant_get (value2, "b", &truth);
510   g_variant_get (value3, "d", &floating);
511   g_variant_get (value4, "x", &bignum);
512 }]]></programlisting></informalexample>
513    </refsect3>
514   </refsect2>
515
516   <refsect2 id='gvariant-format-strings-strings'>
517    <title>Strings</title>
518    <para>
519     <emphasis role='strong'>
520      Characters: <literal>s</literal>, <literal>o</literal>, <literal>g</literal>
521     </emphasis>
522    </para>
523
524    <para>
525     String conversions occur to and from standard nul-terminated C strings.  Upon encountering an
526     '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>' in a format string,
527     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(const
528     <link linkend='gchar'>gchar</link> *)</code> and makes a copy of it.
529     <link linkend='NULL:CAPS'><literal>NULL</literal></link> is not a valid string.  If the '<literal>o</literal>' or
530     '<literal>g</literal>' characters are used, care must be taken to ensure that the passed string is a valid DBus
531     object path or DBus type signature, respectively.
532    </para>
533    <para>
534     Upon encounting '<literal>s</literal>', '<literal>o</literal>' or '<literal>g</literal>', <link
535     linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
536     <code>(<link linkend='gchar'>gchar</link> *)</code> (ie: <code>(<link linkend='gchar'>gchar</link> **)</code>) and
537     sets it to a newly-allocated copy of the string.  It is appropriate to free this copy using
538     <link linkend='g-free'><function>g_free()</function></link>.
539     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value of the
540     string should be ignored (in which case no copy is made).
541    </para>
542
543    <refsect3>
544     <title>Examples</title>
545     <informalexample><programlisting>
546 <![CDATA[GVariant *value1, *value2, *value3;
547
548 value1 = g_variant_new ("s", "hello world!");
549 value2 = g_variant_new ("o", "/must/be/a/valid/path");
550 value3 = g_variant_new ("g", "iias");
551
552 #if 0
553   g_variant_new ("s", NULL);      /* not valid: NULL is not a string. */
554 #endif
555
556 {
557   gchar *result;
558
559   g_variant_get (value1, "s", &result);
560   g_print ("It was '%s'\n", result);
561   g_free (result);
562 }]]></programlisting></informalexample>
563    </refsect3>
564   </refsect2>
565
566   <refsect2 id='gvariant-format-strings-variants'>
567    <title>Variants</title>
568    <para>
569     <emphasis role='strong'>
570      Characters: <literal>v</literal>
571     </emphasis>
572    </para>
573
574    <para>
575     Upon encountering a '<literal>v</literal>',
576     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a <code>(<link
577     linkend='GVariant'>GVariant</link> *)</code>.  The value of the
578     <link linkend='GVariant'><type>GVariant</type></link> is used as the contents of the variant value.
579    </para>
580    <para>
581     Upon encountering a '<literal>v</literal>', <link
582     linkend='g-variant-get'><function>g_variant_get()</function></link> takes a pointer to a
583     <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: <code>(<link linkend='GVariant'>GVariant</link> **)
584     </code>).  It is set to a new reference to a <link linkend='GVariant'><type>GVariant</type></link> instance
585     containing the contents of the variant value.  It is appropriate to free this reference using
586     <link linkend='g-variant-unref'><function>g_variant_unref()</function></link>.
587     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be passed to indicate that the value should be
588     ignored (in which case no new reference is created).
589    </para>
590
591    <refsect3>
592     <title>Examples</title>
593     <informalexample><programlisting>
594 <![CDATA[GVariant *x, *y;
595
596 /* the following two lines are equivalent: */
597 x = g_variant_new ("v", y);
598 x = g_variant_new_variant (y);
599
600 /* as are these: */
601 g_variant_get (x, "v", &y);
602 y = g_variant_get_variant (x);]]></programlisting></informalexample>
603    </refsect3>
604   </refsect2>
605
606
607   <refsect2 id='gvariant-format-strings-arrays'>
608    <title>Arrays</title>
609    <para>
610     <emphasis role='strong'>
611      Characters: <literal>a</literal>
612     </emphasis>
613    </para>
614
615    <para>
616     Upon encountering an '<literal>a</literal>' character followed by a type string,
617     <link linkend='g-variant-new'><function>g_variant_new()</function></link> will take a
618     <code>(<link linkend='GVariantBuilder'>GVariantBuilder</link> *)</code> that has been created as an array builder
619     for an array of the type given in the type string.  The builder will have
620     <link linkend='g-variant-builder-end'><function>g_variant_builder_end()</function></link> called on it and the
621     result will be used as the value.  As a special exception, if the given type string is a definite type, then
622     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may be given to mean an empty array of that type.
623    </para>
624
625    <para>
626     Upon encountering an '<literal>a</literal>' character followed by a type string,
627     <link linkend='g-variant-get'><function>g_variant_get()</function></link> will take a pointer to a
628     <code>(<link linkend='GVariantIter'>GVariantIter</link> *)</code> (ie:
629     <code>(<link linkend='GVariantIter'>GVariantIter</link> **)</code>).
630     A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
631     This iterator should be freed when you are done with it, using
632     <link linkend='g-variant-iter-free'><function>g_variant_iter_free()</function></link>.
633     <link linkend='NULL:CAPS'><literal>NULL</literal></link> may also be given to indicate that the value of the array
634     should be ignored.
635    </para>
636
637    <refsect3>
638     <title>Examples</title>
639     <informalexample><programlisting>
640 <![CDATA[GVariantBuilder *builder;
641 GVariant *value;
642
643 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
644 g_variant_builder_add (builder, "s", "when");
645 g_variant_builder_add (builder, "s", "in");
646 g_variant_builder_add (builder, "s", "the");
647 g_variant_builder_add (builder, "s", "course");
648 value = g_variant_new ("as", builder);
649 g_variant_builder_unref (builder);
650
651 {
652   GVariantIter *iter;
653   gchar *str;
654
655   g_variant_get (value, "as", &iter);
656   while (g_variant_iter_loop (iter, "s", &str))
657     g_print ("%s\n", str);
658   g_variant_iter_free (iter);
659 }
660
661 g_variant_unref (value);]]></programlisting></informalexample>
662    </refsect3>
663   </refsect2>
664
665   <refsect2 id='gvariant-format-strings-maybe-types'>
666    <title>Maybe Types</title>
667    <para>
668     <emphasis role='strong'>
669      Characters: <literal>m</literal>
670     </emphasis>
671    </para>
672    <para>
673     Maybe types are handled in two separate ways depending on the format string that follows the
674     '<literal>m</literal>'.  The method that is used currently depends entirely on the character immediately following the
675     '<literal>m</literal>'.
676    </para>
677
678    <para>
679     The first way is used with format strings starting with '<literal>a</literal>', '<literal>s</literal>',
680     '<literal>o</literal>', '<literal>g</literal>', '<literal>v</literal>', '<literal>@</literal>',
681     '<literal>*</literal>', '<literal>?</literal>', '<literal>r</literal>', '<literal>&amp;</literal>', or
682     '<literal>^</literal>'.  In all of these cases, for non-maybe types,
683     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a pointer to a
684     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> value and
685     <link linkend='g-variant-get'><function>g_variant_get()</function></link> returns (by reference) a
686     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer.  When any of these format strings are
687     prefixed with an '<literal>m</literal>', the type of arguments that are collected does not change in any way, but
688     <link linkend='NULL:CAPS'><literal>NULL</literal></link> becomes a permissable value, to indicate the Nothing case.
689    </para>
690    <para>
691     Note that the "special exception" introduced in the array section for constructing empty arrays is ignored
692     here.  Using a <literal>NULL</literal> pointer with the format string '<literal>mas</literal>' constructs
693     the Nothing value -- not an empty array.
694    </para>
695    <para>
696     The second way is used with all other format strings.  For
697     <link linkend='g-variant-new'><function>g_variant_new()</function></link> an additional
698     <link linkend='gboolean'><type>gboolean</type></link> argument is collected and for
699     <link linkend='g-variant-get'><function>g_variant_get()</function></link> an additional
700     <code>(<link linkend='gboolean'>gboolean</link> *)</code>.  Following this argument, the arguments that are normally
701     collected for the equivalent non-maybe type will be collected.
702    </para>
703    <para>
704     If <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> is given to
705     <link linkend='g-variant-new'><function>g_variant_new()</function></link> then the Nothing value is constructed and
706     the collected arguments are ignored.  Otherwise (if <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> was
707     given), the arguments are used in the normal way to create the Just value.
708    </para>
709    <para>
710     If <link linkend='NULL:CAPS'><literal>NULL</literal></link> is given to
711     <link linkend='g-variant-get'><function>g_variant_get()</function></link> then the value is ignored.  If a
712     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer is given then it is used to return by reference
713     whether the value was Just.  In the case that the value was Just, the
714     <link linkend='gboolean'><type>gboolean</type></link> will be set to
715     <link linkend='TRUE:CAPS'><literal>TRUE</literal></link> and the value will be stored in the arguments in the usual
716     way.  In the case that the value was Nothing, the <link linkend='gboolean'><type>gboolean</type></link> will be set to
717     <link linkend='FALSE:CAPS'><literal>FALSE</literal></link> and the arguments will be collected in the normal way
718     but have their values set to binary zero.
719    </para>
720
721    <refsect3>
722     <title>Examples</title>
723     <informalexample><programlisting>
724 <![CDATA[GVariant *value1, *value2, *value3, *value4, *value5, *value6;
725 value1 = g_variant_new ("ms", "Hello world");
726 value2 = g_variant_new ("ms", NULL);
727 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
728 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done");          /* both '-1' are ignored. */
729 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
730
731 {
732   GVariant *contents;
733   const gchar *cstr;
734   gboolean just;
735   gint32 x, y;
736   gchar *str;
737
738   g_variant_get (value1, "ms", &str);
739   if (str != NULL)
740     g_print ("str: %s\n", str);
741   else
742     g_print ("it was null\n");
743   g_free (str);
744
745
746   g_variant_get (value2, "m&s", &cstr);
747   if (cstr != NULL)
748     g_print ("str: %s\n", cstr);
749   else
750     g_print ("it was null\n");
751   /* don't free 'cstr' */
752
753
754   /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
755   g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &str);
756   g_print ("string is %s\n", str);
757   g_free (str);
758
759   /* note: &s used, so g_free() not needed */
760   g_variant_get (value4, "(m(ii)&s)", &just, &x, &y, &cstr);
761   if (just)
762     g_print ("it was (%d, %d)\n", x, y);
763   else
764     g_print ("it was null\n");
765   g_print ("string is %s\n", cstr);
766   /* don't free 'cstr' */
767
768
769   g_variant_get (value5, "(m*s)", &contents, NULL); /* ignore the string. */
770   if (contents != NULL)
771     {
772       g_variant_get (contents, "(ii)", &x, &y);
773       g_print ("it was (%d, %d)\n", x, y);
774       g_variant_unref (contents);
775     }
776   else
777     g_print ("it was null\n");
778 }]]></programlisting></informalexample>
779    </refsect3>
780   </refsect2>
781
782   <refsect2 id='gvariant-format-strings-tuples'>
783    <title>Tuples</title>
784    <para>
785     <emphasis role='strong'>
786      Characters: <code>()</code>
787     </emphasis>
788    </para>
789
790    <para>
791     Tuples are handled by handling each item in the tuple, in sequence.  Each item is handled in the usual way.
792    </para>
793
794    <refsect3>
795     <title>Examples</title>
796     <informalexample><programlisting>
797 <![CDATA[GVariant *value1, *value2;
798
799 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
800 value2 = g_variant_new ("()");
801
802 {
803   gchar *string;
804   gint x, y;
805
806   g_variant_get (value1, "(s(ii))", &string, &x, &y);
807   g_print ("%s, %d, %d\n", string, x, y);
808   g_free (string);
809
810   g_variant_get (value2, "()");   /* do nothing... */
811 }]]></programlisting></informalexample>
812    </refsect3>
813   </refsect2>
814
815   <refsect2 id='gvariant-format-strings-dictionaries'>
816    <title>Dictionaries</title>
817    <para>
818     <emphasis role='strong'>
819      Characters: <code>{}</code>
820     </emphasis>
821    </para>
822
823    <para>
824     Dictionary entries are handled by handling first the key, then the value.  Each is handled in the usual way.
825    </para>
826
827    <refsect3>
828     <title>Examples</title>
829     <informalexample><programlisting>
830 <![CDATA[GVariantBuilder *b;
831 GVariant *dict;
832
833 b = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
834 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
835 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
836 dict = g_variant_builder_end (b);]]></programlisting></informalexample>
837    </refsect3>
838   </refsect2>
839
840   <refsect2 id='gvariant-format-strings-gvariant'>
841    <title>GVariant *</title>
842    <para>
843     <emphasis role='strong'>
844      Characters: <literal>@</literal>, <literal>*</literal>, <literal>?</literal>, <literal>r</literal>
845     </emphasis>
846
847    </para>
848    <para>
849     Upon encountering a '<literal>@</literal>' in front of a type string,
850     <link linkend='g-variant-new'><function>g_variant_new()</function></link> takes a
851     non-<link linkend='NULL:CAPS'><literal>NULL</literal></link> pointer to a
852     <link linkend='GVariant'><type>GVariant</type></link> and uses its value directly instead of collecting arguments to
853     create the value.  The provided <link linkend='GVariant'><type>GVariant</type></link> must have a type that matches the
854     type string following the '<literal>@</literal>'.  '<literal>*</literal>' is
855     the same as '<literal>@*</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any type).
856     '<literal>?</literal>' is the same as '<literal>@?</literal>' (ie: take a
857     <link linkend='GVariant'><type>GVariant</type></link> of any basic type).  '<literal>r</literal>' is the same as
858     '<literal>@r</literal>' (ie: take a <link linkend='GVariant'><type>GVariant</type></link> of any tuple type).
859    </para>
860    <para>
861     Upon encountering a '<literal>@</literal>' in front of a type string,
862     <link linkend='g-variant-get'><function>g_variant_get()</function></link>
863     takes a pointer to a <code>(<link linkend='GVariant'>GVariant</link> *)</code> (ie: a
864     <code>(<link linkend='GVariant'>GVariant</link> **)</code>) and sets it to a new reference to a
865     <link linkend='GVariant'><type>GVariant</type></link> containing the value (instead of deconstructing the value into
866     C types in the usual way).  <link linkend='NULL:CAPS'><literal>NULL</literal></link> can be given to ignore the
867     value.  '<literal>*</literal>', '<literal>?</literal>' and '<literal>r</literal>' are handled in a way analogous to
868     what is stated above.
869    </para>
870    <para>
871     You can always use '<literal>*</literal>' as an alternative to '<literal>?</literal>', '<literal>r</literal>' or any
872     use of '<literal>@</literal>'.  Using the other characters where possible is recommended, however, due to the
873     improvements in type safety and code self-documentation.
874    </para>
875
876    <refsect3>
877     <title>Examples</title>
878     <informalexample><programlisting>
879 <![CDATA[GVariant *value1, *value2;
880
881 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
882
883 /* note: consumes floating reference count on 'value1' */
884 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
885
886 {
887   const gchar *string;
888   GVariant *tmp;
889   gsize length;
890   gint x, y, z;
891
892   g_variant_get (value2, "((iii)*)", &x, &y, &z, &tmp);
893   string = g_variant_get_string (tmp, &length);
894   g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
895   g_variant_unref (tmp);
896
897   /* quick way to skip all the values in a tuple */
898   g_variant_get (value2, "(rs)", NULL, &string); /* or "(@(iii)s)" */
899   g_print ("i only got the string: %s\n", string);
900   g_free (string);
901 }]]></programlisting></informalexample>
902    </refsect3>
903   </refsect2>
904
905   <refsect2 id='gvariant-format-strings-pointers'>
906    <title>Pointers</title>
907    <para>
908     <emphasis role='strong'>
909      Characters: <code>&amp;</code>
910     </emphasis>
911    </para>
912
913    <para>
914     The '<code>&amp;</code>' character is used to indicate that serialised data should be directly exchanged via a
915     pointer.
916    </para>
917    <para>
918     Currently, the only use for this character is when it is applied to a string (ie: '<literal>&amp;s</literal>',
919     '<literal>&amp;o</literal>' or '<code>&amp;g</code>').  For
920     <link linkend='g-variant-new'><function>g_variant_new()</function></link> this has absolutely no effect.  The string
921     is collected and duplicated normally.  For <link linkend='g-variant-get'><function>g_variant_get()</function></link>
922     it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
923     returned.  This pointer should not be freed.  Validity checks are performed to ensure that the string data will
924     always be properly nul-terminated.
925    </para>
926
927    <refsect3>
928     <title>Examples</title>
929     <informalexample><programlisting>
930 <![CDATA[{
931   const gchar *str;
932   GVariant *value;
933
934   value = g_variant_new ("&s", "hello world");
935   str = g_variant_get ("&s", &str);
936   g_print ("string is: %s\n", str);
937   /* no need to free str */
938 }]]></programlisting></informalexample>
939    </refsect3>
940   </refsect2>
941
942   <refsect2 id='gvariant-format-strings-convenience'>
943    <title>Convenience Conversions</title>
944    <para>
945     <emphasis role='strong'>
946      Characters: <literal>^</literal>
947     </emphasis>
948    </para>
949
950    <para>
951     The '<literal>^</literal>' character currently supports conversion to and from bytestrings or to and from arrays
952     of strings or bytestrings.  It has a number of forms.
953    </para>
954
955    <para>
956     In all forms, when used with <link linkend='g-variant-new'><function>g_variant_new()</function></link> one
957     pointer value is collected from the variable arguments and passed to a function (as given in the table below).
958     The result of that function is used as the value for this position.  When used with
959     <link linkend='g-variant-get'><function>g_variant_get()</function></link> one pointer value is produced by using
960     the function (given in the table) and returned by reference.
961    </para>
962
963    <informaltable>
964     <tgroup cols='2'>
965      <colspec colname='col_0'/>
966      <colspec colname='col_1'/>
967      <colspec colname='col_2'/>
968      <tbody>
969
970       <row rowsep='1'>
971        <entry colsep='1' rowsep='1'>
972         <para>
973          <emphasis role='strong'>Conversion</emphasis>
974         </para>
975        </entry>
976        <entry colsep='1' rowsep='1'>
977         <para>
978           <emphasis role='strong'>
979             Used with <link linkend='g-variant-new'><function>g_variant_new()</function></link>
980           </emphasis>
981         </para>
982        </entry>
983        <entry colsep='1' rowsep='1'>
984         <para>
985           <emphasis role='strong'>
986             Used with <link linkend='g-variant-get'><function>g_variant_get()</function></link>
987           </emphasis>
988         </para>
989        </entry>
990       </row>
991
992       <row rowsep='1'>
993        <entry colsep='1' rowsep='1'>
994         <para>
995          <emphasis role='strong'>
996           <literal>^as</literal>
997          </emphasis>
998         </para>
999        </entry>
1000        <entry colsep='1' rowsep='1' morerows='1'>
1001         <para>
1002          equivalent to <link linkend='g-variant-new-strv'><function>g_variant_new_strv()</function></link>
1003         </para>
1004        </entry>
1005        <entry colsep='1' rowsep='1'>
1006         <para>
1007          equivalent to <link linkend='g-variant-dup-strv'><function>g_variant_dup_strv()</function></link>
1008         </para>
1009        </entry>
1010       </row>
1011
1012       <row rowsep='1'>
1013        <entry colsep='1' rowsep='1'>
1014         <para>
1015          <emphasis role='strong'>
1016           <literal>^a&amp;s</literal>
1017          </emphasis>
1018         </para>
1019        </entry>
1020        <entry colsep='1' rowsep='1'>
1021         <para>
1022          equivalent to <link linkend='g-variant-get-strv'><function>g_variant_get_strv()</function></link>
1023         </para>
1024        </entry>
1025       </row>
1026
1027       <row rowsep='1'>
1028        <entry colsep='1' rowsep='1'>
1029         <para>
1030          <emphasis role='strong'>
1031           <literal>^ao</literal>
1032          </emphasis>
1033         </para>
1034        </entry>
1035        <entry colsep='1' rowsep='1' morerows='1'>
1036         <para>
1037          equivalent to <link linkend='g-variant-new-objv'><function>g_variant_new_objv()</function></link>
1038         </para>
1039        </entry>
1040        <entry colsep='1' rowsep='1'>
1041         <para>
1042          equivalent to <link linkend='g-variant-dup-objv'><function>g_variant_dup_objv()</function></link>
1043         </para>
1044        </entry>
1045       </row>
1046
1047       <row rowsep='1'>
1048        <entry colsep='1' rowsep='1'>
1049         <para>
1050          <emphasis role='strong'>
1051           <literal>^a&amp;o</literal>
1052          </emphasis>
1053         </para>
1054        </entry>
1055        <entry colsep='1' rowsep='1'>
1056         <para>
1057          equivalent to <link linkend='g-variant-get-objv'><function>g_variant_get_objv()</function></link>
1058         </para>
1059        </entry>
1060       </row>
1061
1062       <row rowsep='1'>
1063        <entry colsep='1' rowsep='1'>
1064         <para>
1065          <emphasis role='strong'>
1066           <literal>^ay</literal>
1067          </emphasis>
1068         </para>
1069        </entry>
1070        <entry colsep='1' rowsep='1' morerows='1'>
1071         <para>
1072          equivalent to <link linkend='g-variant-new-bytestring'><function>g_variant_new_bytestring()</function></link>
1073         </para>
1074        </entry>
1075        <entry colsep='1' rowsep='1'>
1076         <para>
1077          equivalent to <link linkend='g-variant-dup-bytestring'><function>g_variant_dup_bytestring()</function></link>
1078         </para>
1079        </entry>
1080       </row>
1081
1082       <row rowsep='1'>
1083        <entry colsep='1' rowsep='1'>
1084         <para>
1085          <emphasis role='strong'>
1086           <literal>^&amp;ay</literal>
1087          </emphasis>
1088         </para>
1089        </entry>
1090        <entry colsep='1' rowsep='1'>
1091         <para>
1092          equivalent to <link linkend='g-variant-get-bytestring'><function>g_variant_get_bytestring()</function></link>
1093         </para>
1094        </entry>
1095       </row>
1096
1097       <row rowsep='1'>
1098        <entry colsep='1' rowsep='1'>
1099         <para>
1100          <emphasis role='strong'>
1101           <literal>^aay</literal>
1102          </emphasis>
1103         </para>
1104        </entry>
1105        <entry colsep='1' rowsep='1' morerows='1'>
1106         <para>
1107          equivalent to <link linkend='g-variant-new-bytestring-array'><function>g_variant_new_bytestring_array()</function></link>
1108         </para>
1109        </entry>
1110        <entry colsep='1' rowsep='1'>
1111         <para>
1112          equivalent to <link linkend='g-variant-dup-bytestring-array'><function>g_variant_dup_bytestring_array()</function></link>
1113         </para>
1114        </entry>
1115       </row>
1116
1117       <row rowsep='1'>
1118        <entry colsep='1' rowsep='1'>
1119         <para>
1120          <emphasis role='strong'>
1121           <literal>^a&amp;ay</literal>
1122          </emphasis>
1123         </para>
1124        </entry>
1125        <entry colsep='1' rowsep='1'>
1126         <para>
1127          equivalent to <link linkend='g-variant-get-bytestring-array'><function>g_variant_get_bytestring_array()</function></link>
1128         </para>
1129        </entry>
1130       </row>
1131
1132      </tbody>
1133     </tgroup>
1134    </informaltable>
1135   </refsect2>
1136  </refsect1>
1137 </refentry>