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