Initial commit
[platform/upstream/glib2.0.git] / docs / reference / glib / html / gvariant-format-strings.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>GVariant Format Strings</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
7 <link rel="home" href="index.html" title="GLib Reference Manual">
8 <link rel="up" href="glib-data-types.html" title="GLib Data Types">
9 <link rel="prev" href="glib-GVariant.html" title="GVariant">
10 <link rel="next" href="tools.html" title="GLib Tools">
11 <meta name="generator" content="GTK-Doc V1.13 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 <link rel="chapter" href="glib.html" title="GLib Overview">
14 <link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals">
15 <link rel="chapter" href="glib-core.html" title="GLib Core Application Support">
16 <link rel="chapter" href="glib-utilities.html" title="GLib Utilities">
17 <link rel="chapter" href="glib-data-types.html" title="GLib Data Types">
18 <link rel="chapter" href="tools.html" title="GLib Tools">
19 <link rel="index" href="api-index-full.html" title="Index">
20 <link rel="index" href="api-index-deprecated.html" title="Index of deprecated symbols">
21 <link rel="index" href="api-index-2-2.html" title="Index of new symbols in 2.2">
22 <link rel="index" href="api-index-2-4.html" title="Index of new symbols in 2.4">
23 <link rel="index" href="api-index-2-6.html" title="Index of new symbols in 2.6">
24 <link rel="index" href="api-index-2-8.html" title="Index of new symbols in 2.8">
25 <link rel="index" href="api-index-2-10.html" title="Index of new symbols in 2.10">
26 <link rel="index" href="api-index-2-12.html" title="Index of new symbols in 2.12">
27 <link rel="index" href="api-index-2-14.html" title="Index of new symbols in 2.14">
28 <link rel="index" href="api-index-2-16.html" title="Index of new symbols in 2.16">
29 <link rel="index" href="api-index-2-18.html" title="Index of new symbols in 2.18">
30 <link rel="index" href="api-index-2-20.html" title="Index of new symbols in 2.20">
31 <link rel="index" href="api-index-2-22.html" title="Index of new symbols in 2.22">
32 <link rel="index" href="api-index-2-24.html" title="Index of new symbols in 2.24">
33 </head>
34 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
35 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
36 <td><a accesskey="p" href="glib-GVariant.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
37 <td><a accesskey="u" href="glib-data-types.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
38 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
39 <th width="100%" align="center">GLib Reference Manual</th>
40 <td><a accesskey="n" href="tools.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
41 </tr></table>
42 <div class="refentry" title="GVariant Format Strings">
43 <a name="gvariant-format-strings"></a><div class="titlepage"></div>
44 <div class="refsect1" title="Variable Argument Conversions">
45 <a name="id458992"></a><h2>Variable Argument Conversions</h2>
46 <p>
47    This page attempts to document how to perform variable argument conversions with GVariant.
48   </p>
49 <p>
50    Conversions occur according to format strings.  A format string is a two-way mapping between a single
51    <a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> value and one or more C values.
52   </p>
53 <p>
54    A conversion from C values into a <a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> value is made using the
55    <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> function.  A conversion from a
56    <a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> into C values is made using the
57    <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> function.
58   </p>
59 </div>
60 <div class="refsect1" title="Syntax">
61 <a name="id483738"></a><h2>Syntax</h2>
62 <p>
63    This section exhaustively describes all possibilities for GVariant format strings.  There are no valid forms of
64    format strings other than those described here.  Please note that the format string syntax is likely to expand in the
65    future.
66   </p>
67 <p>
68    Valid format strings have one of the following forms:
69   </p>
70 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
71 <li class="listitem"><p>any type string</p></li>
72 <li class="listitem"><p>
73      a type string prefixed with a '<code class="literal">@</code>'
74     </p></li>
75 <li class="listitem"><p>
76      '<code class="literal">&amp;s</code>' '<code class="literal">&amp;o</code>', '<code class="literal">&amp;g</code>', '<code class="literal">^as</code>',
77      '<code class="literal">^ao</code>', '<code class="literal">^ag</code>', '<code class="literal">^a&amp;s</code>', '<code class="literal">^a&amp;o</code>' or
78      '<code class="literal">^a&amp;g</code>'
79     </p></li>
80 <li class="listitem"><p>
81      any format string, prefixed with an '<code class="literal">m</code>'
82     </p></li>
83 <li class="listitem"><p>
84      a sequence of zero or more format strings strings, concatenated and enclosed in parentheses
85     </p></li>
86 <li class="listitem"><p>
87      an opening brace, followed by two format strings, followed by a closing brace (subject to the constraint that the
88      first format string correspond to a type valid for use as the key type of a dictionary)
89     </p></li>
90 </ul></div>
91 </div>
92 <div class="refsect1" title="Symbols">
93 <a name="id511368"></a><h2>Symbols</h2>
94 <p>
95     The following table describes the rough meaning of symbols that may appear inside a GVariant format string.  Each
96     symbol is described in detail in its own section, including usage examples.
97   </p>
98 <div class="informaltable"><table border="1">
99 <colgroup>
100 <col>
101 <col>
102 </colgroup>
103 <tbody>
104 <tr>
105 <td>
106        <p>
107         <span class="strong"><strong>Symbol</strong></span>
108        </p>
109       </td>
110 <td>
111        <p>
112          <span class="strong"><strong>Meaning</strong></span>
113        </p>
114       </td>
115 </tr>
116 <tr>
117 <td>
118        <p>
119         <span class="strong"><strong>
120          <code class="literal">b</code>, <code class="literal">y</code>, <code class="literal">n</code>, <code class="literal">q</code>, <code class="literal">i</code>,
121          <code class="literal">u</code>, <code class="literal">x</code>, <code class="literal">t</code>, <code class="literal">h</code>, <code class="literal">d</code>
122         </strong></span>
123        </p>
124       </td>
125 <td>
126        <p>
127         Used for building or deconstructing boolean, byte and numeric types.  See
128         <a href="gvariant-format-strings.html#gvariant-format-strings-numeric-types">Numeric Types</a> below.
129        </p>
130       </td>
131 </tr>
132 <tr>
133 <td>
134        <p>
135         <span class="strong"><strong>
136          <code class="literal">s</code>, <code class="literal">o</code>, <code class="literal">g</code>
137         </strong></span>
138        </p>
139       </td>
140 <td>
141        <p>
142         Used for building or deconstructing string types.  See
143         <a href="gvariant-format-strings.html#gvariant-format-strings-strings">Strings</a> below.
144        </p>
145       </td>
146 </tr>
147 <tr>
148 <td>
149        <p>
150         <span class="strong"><strong><code class="literal">v</code></strong></span>
151        </p>
152       </td>
153 <td>
154        <p>
155         Used for building or deconstructing variant types.  See
156         Variants below.
157        </p>
158       </td>
159 </tr>
160 <tr>
161 <td>
162        <p>
163         <span class="strong"><strong>
164          <code class="literal">a</code>
165         </strong></span>
166        </p>
167       </td>
168 <td>
169        <p>
170         Used for building or deconstructing arrays.  See
171         <a href="gvariant-format-strings.html#gvariant-format-strings-arrays">Arrays</a> below.
172        </p>
173       </td>
174 </tr>
175 <tr>
176 <td>
177        <p>
178         <span class="strong"><strong>
179          <code class="literal">m</code>
180         </strong></span>
181        </p>
182       </td>
183 <td>
184        <p>
185          Used for building or deconstructing maybe types.  See
186          <a href="gvariant-format-strings.html#gvariant-format-strings-maybe-types">Maybe Types</a> below.
187        </p>
188       </td>
189 </tr>
190 <tr>
191 <td>
192        <p>
193         <span class="strong"><strong>
194          <code class="literal">()</code>
195         </strong></span>
196        </p>
197       </td>
198 <td>
199        <p>
200          Used for building or deconstructing tuples.  See
201          <a href="gvariant-format-strings.html#gvariant-format-strings-tuples">Tuples</a> below.
202        </p>
203       </td>
204 </tr>
205 <tr>
206 <td>
207        <p>
208         <span class="strong"><strong>
209          <code class="literal">{}</code>
210         </strong></span>
211        </p>
212       </td>
213 <td>
214        <p>
215         Used for building or deconstructing dictionary entries. See
216         <a href="gvariant-format-strings.html#gvariant-format-strings-dictionaries">Dictionaries</a> below.
217        </p>
218       </td>
219 </tr>
220 <tr>
221 <td>
222        <p>
223         <span class="strong"><strong>
224          <code class="literal">@</code>
225         </strong></span>
226        </p>
227       </td>
228 <td>
229        <p>
230         Used as a prefix on a GVariant type string (not format string).  Denotes that a pointer to a
231         <a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> should be used in place of the normal C type or types.  For
232         <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> this means that you must pass a
233         non-<code class="literal">NULL</code> <code class="code">(<a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a>
234         *)</code>.  For <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> this means that you
235         must pass a pointer to a <code class="code">(<a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> *)</code> for the value to be returned
236         by reference or <code class="literal">NULL</code> to ignore the value.  See
237         <a href="gvariant-format-strings.html#gvariant-format-strings-gvariant"><code class="code">GVariant *</code></a> below.
238        </p>
239       </td>
240 </tr>
241 <tr>
242 <td>
243        <p>
244         <span class="strong"><strong>
245          <code class="literal">*</code>, <code class="literal">?</code>, <code class="literal">r</code>
246         </strong></span>
247        </p>
248       </td>
249 <td>
250        <p>
251         Exactly equivalent to <code class="literal">@*</code>, <code class="literal">@?</code> and <code class="literal">@r</code>.  Provided only for
252         completeness so that all GVariant type strings can be used also as format strings.  See <a href="gvariant-format-strings.html#gvariant-format-strings-gvariant"><code class="code">GVariant *</code></a> below.
253        </p>
254       </td>
255 </tr>
256 <tr>
257 <td>
258        <p>
259         <span class="strong"><strong><code class="literal">&amp;</code></strong></span>
260        </p>
261       </td>
262 <td>
263        <p>
264         Used as a prefix on a GVariant type string (not format string).  Denotes that a C pointer to serialised data
265         should be used in place of the normal C type.  See
266         <a href="gvariant-format-strings.html#gvariant-format-strings-pointers">Pointers</a> below.
267        </p>
268       </td>
269 </tr>
270 <tr>
271 <td>
272        <p>
273         <span class="strong"><strong><code class="literal">^</code></strong></span>
274        </p>
275       </td>
276 <td>
277        <p>
278         Used as a prefix on some specific types of format strings.  See
279         <a href="gvariant-format-strings.html#gvariant-format-strings-convenience">Convenience Conversions</a> below.
280        </p>
281       </td>
282 </tr>
283 </tbody>
284 </table></div>
285 <div class="refsect2" title="Numeric Types">
286 <a name="gvariant-format-strings-numeric-types"></a><h3>Numeric Types</h3>
287 <p>
288     <span class="strong"><strong>
289      Characters: <code class="literal">b</code>, <code class="literal">y</code>, <code class="literal">n</code>, <code class="literal">q</code>,
290      <code class="literal">i</code>, <code class="literal">u</code>, <code class="literal">x</code>, <code class="literal">t</code>, <code class="literal">h</code>,
291      <code class="literal">d</code>
292     </strong></span>
293    </p>
294 <p>
295     Variable argument conversions from numeric types work in the most obvious way possible.  Upon encountering one of
296     these characters, <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> takes the equivalent C
297     type as an argument.  <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> takes a pointer to
298     the equivalent C type (or <code class="literal">NULL</code> to ignore the value).
299    </p>
300 <p>
301     The equivalent C types are as follows:
302    </p>
303 <div class="informaltable"><table border="1">
304 <colgroup>
305 <col>
306 <col>
307 </colgroup>
308 <tbody>
309 <tr>
310 <td>
311         <p>
312           <span class="strong"><strong>Character</strong></span>
313         </p>
314        </td>
315 <td>
316         <p>
317           <span class="strong"><strong>Equivalent C type</strong></span>
318         </p>
319        </td>
320 </tr>
321 <tr>
322 <td>
323         <p>
324          <span class="strong"><strong>
325           <code class="literal">b</code>
326          </strong></span>
327         </p>
328        </td>
329 <td>
330         <p>
331          <a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="type">gboolean</span></a>
332         </p>
333        </td>
334 </tr>
335 <tr>
336 <td>
337         <p>
338          <span class="strong"><strong>
339           <code class="literal">y</code>
340          </strong></span>
341         </p>
342        </td>
343 <td>
344         <p>
345          <a class="link" href="glib-Basic-Types.html#guchar" title="guchar"><span class="type">guchar</span></a>
346         </p>
347        </td>
348 </tr>
349 <tr>
350 <td>
351         <p>
352          <span class="strong"><strong>
353           <code class="literal">n</code>
354          </strong></span>
355         </p>
356        </td>
357 <td>
358         <p>
359          <a class="link" href="glib-Basic-Types.html#gint16" title="gint16"><span class="type">gint16</span></a>
360         </p>
361        </td>
362 </tr>
363 <tr>
364 <td>
365         <p>
366          <span class="strong"><strong>
367           <code class="literal">q</code>
368          </strong></span>
369         </p>
370        </td>
371 <td>
372         <p>
373          <a class="link" href="glib-Basic-Types.html#guint16" title="guint16"><span class="type">guint16</span></a>
374         </p>
375        </td>
376 </tr>
377 <tr>
378 <td>
379         <p>
380          <span class="strong"><strong>
381           <code class="literal">i</code>
382          </strong></span>
383         </p>
384        </td>
385 <td>
386         <p>
387          <a class="link" href="glib-Basic-Types.html#gint32" title="gint32"><span class="type">gint32</span></a>
388         </p>
389        </td>
390 </tr>
391 <tr>
392 <td>
393         <p>
394          <span class="strong"><strong>
395           <code class="literal">u</code>
396          </strong></span>
397         </p>
398        </td>
399 <td>
400         <p>
401          <a class="link" href="glib-Basic-Types.html#guint32" title="guint32"><span class="type">guint32</span></a>
402         </p>
403        </td>
404 </tr>
405 <tr>
406 <td>
407         <p>
408          <span class="strong"><strong>
409           <code class="literal">x</code>
410          </strong></span>
411         </p>
412        </td>
413 <td>
414         <p>
415          <a class="link" href="glib-Basic-Types.html#gint64" title="gint64"><span class="type">gint64</span></a>
416         </p>
417        </td>
418 </tr>
419 <tr>
420 <td>
421         <p>
422          <span class="strong"><strong>
423           <code class="literal">t</code>
424          </strong></span>
425         </p>
426        </td>
427 <td>
428         <p>
429          <a class="link" href="glib-Basic-Types.html#guint64" title="guint64"><span class="type">guint64</span></a>
430         </p>
431        </td>
432 </tr>
433 <tr>
434 <td>
435         <p>
436          <span class="strong"><strong>
437           <code class="literal">h</code>
438          </strong></span>
439         </p>
440        </td>
441 <td>
442         <p>
443          <a class="link" href="glib-Basic-Types.html#gint32" title="gint32"><span class="type">gint32</span></a>
444         </p>
445        </td>
446 </tr>
447 <tr>
448 <td>
449         <p>
450          <span class="strong"><strong>
451           <code class="literal">d</code>
452          </strong></span>
453         </p>
454        </td>
455 <td>
456         <p>
457          <a class="link" href="glib-Basic-Types.html#gdouble" title="gdouble"><span class="type">gdouble</span></a>
458         </p>
459        </td>
460 </tr>
461 </tbody>
462 </table></div>
463 <p>
464     Note that in C, small integer types in variable argument lists are promoted up to <a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">int</span></a> or <a class="link" href="glib-Basic-Types.html#guint" title="guint"><span class="type">unsigned int</span></a> as appropriate, and
465     read back accordingly.  <a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">int</span></a> is 32 bits on every platform on which GLib is
466     currently suported.  This means that you can use C expressions of type <a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">int</span></a>
467     with <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> and format characters
468     '<code class="literal">b</code>', '<code class="literal">y</code>', '<code class="literal">n</code>', '<code class="literal">q</code>',
469     '<code class="literal">i</code>', '<code class="literal">u</code>' and '<code class="literal">h</code>'.  Specifically, you can use integer
470     literals with these characters.
471    </p>
472 <p>
473     When using the '<code class="literal">x</code>' and '<code class="literal">t</code>' characters, you must ensure that the value that you
474     provide is 64 bit.  This means that you should use a cast or make use of the
475     <code class="literal">G_GINT64_CONSTANT</code> or
476     <code class="literal">G_GUINT64_CONSTANT</code> macros.
477    </p>
478 <p>
479     No type promotion occurs when using <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> since
480     it operates with pointers.  The pointers must always point to a memory region of exactly the correct size.
481    </p>
482 <div class="refsect3" title="Examples">
483 <a name="id618391"></a><h4>Examples</h4>
484 <div class="informalexample"><pre class="programlisting">
485 GVariant *value1, *value2, *value3, *value4;
486
487 value1 = g_variant_new ("y", 200);
488 value2 = g_variant_new ("b", TRUE);
489 value3 = g_variant_new ("d", 37.5):
490 value4 = g_variant_new ("x", G_GINT64_CONSTANT (998877665544332211));
491
492 {
493   gdouble floating;
494   gboolean truth;
495   gint64 bignum;
496
497
498   g_variant_get (value1, "y", NULL);      /* ignore the value. */
499   g_variant_get (value2, "b", &amp;truth);
500   g_variant_get (value3, "d", &amp;floating);
501   g_variant_get (value4, "x", &amp;bignum);
502 }</pre></div>
503 </div>
504 </div>
505 <hr>
506 <div class="refsect2" title="Strings">
507 <a name="gvariant-format-strings-strings"></a><h3>Strings</h3>
508 <p>
509     <span class="strong"><strong>
510      Characters: <code class="literal">s</code>, <code class="literal">o</code>, <code class="literal">g</code>
511     </strong></span>
512    </p>
513 <p>
514     String conversions occur to and from standard nul-terminated C strings.  Upon encountering an
515     '<code class="literal">s</code>', '<code class="literal">o</code>' or '<code class="literal">g</code>' in a format string,
516     <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> takes a <code class="code">(const
517     <a class="link" href="glib-Basic-Types.html#gchar" title="gchar">gchar</a> *)</code> and makes a copy of it.
518     <code class="literal">NULL</code> is not a valid string.  If the '<code class="literal">o</code>' or
519     '<code class="literal">g</code>' characters are used, care must be taken to ensure that the passed string is a valid DBus
520     object path or DBus type signature, respectively.
521    </p>
522 <p>
523     Upon encounting '<code class="literal">s</code>', '<code class="literal">o</code>' or '<code class="literal">g</code>', <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> takes a pointer to a
524     <code class="code">(<a class="link" href="glib-Basic-Types.html#gchar" title="gchar">gchar</a> *)</code> (ie: <code class="code">(<a class="link" href="glib-Basic-Types.html#gchar" title="gchar">gchar</a> **)</code>) and
525     sets it to a newly-allocated copy of the string.  It is appropriate to free this copy using
526     <a class="link" href="glib-Memory-Allocation.html#g-free" title="g_free ()"><code class="function">g_free()</code></a>.
527     <code class="literal">NULL</code> may also be passed to indicate that the value of the
528     string should be ignored (in which case no copy is made).
529    </p>
530 <div class="refsect3" title="Examples">
531 <a name="id498258"></a><h4>Examples</h4>
532 <div class="informalexample"><pre class="programlisting">
533 GVariant *value1, *value2, *value3;
534
535 value1 = g_variant_new ("s", "hello world!");
536 value2 = g_variant_new ("o", "/must/be/a/valid/path");
537 value3 = g_variant_new ("g", "iias");
538
539 #if 0
540   g_variant_new ("s", NULL);      /* not valid: NULL is not a string. */
541 #endif
542
543 {
544   gchar *result;
545
546   g_variant_get (value1, "s", &amp;result);
547   g_print ("It was '%s'\n", result);
548   g_free (result);
549 }</pre></div>
550 </div>
551 </div>
552 <hr>
553 <div class="refsect2" title="Variants">
554 <a name="gvariant-format-strings-variants"></a><h3>Variants</h3>
555 <p>
556     <span class="strong"><strong>
557      Characters: <code class="literal">v</code>
558     </strong></span>
559    </p>
560 <p>
561     Upon encountering a '<code class="literal">v</code>',
562     <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> takes a <code class="code">(<a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> *)</code>.  The value of the
563     <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> is used as the contents of the variant value.
564    </p>
565 <p>
566     Upon encountering a '<code class="literal">v</code>', <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> takes a pointer to a
567     <code class="code">(<a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> *)</code> (ie: <code class="code">(<a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> **)
568     </code>).  It is set to a new reference to a <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> instance
569     containing the contents of the variant value.  It is appropriate to free this reference using
570     <a class="link" href="glib-GVariant.html#g-variant-unref" title="g_variant_unref ()"><code class="function">g_variant_unref()</code></a>.
571     <code class="literal">NULL</code> may also be passed to indicate that the value should be
572     ignored (in which case no new reference is created).
573    </p>
574 <div class="refsect3" title="Examples">
575 <a name="id482476"></a><h4>Examples</h4>
576 <div class="informalexample"><pre class="programlisting">
577 GVariant *x, *y;
578
579 /* the following two lines are equivalent: */
580 x = g_variant_new ("v", y);
581 x = g_variant_new_variant (y);
582
583 /* as are these: */
584 g_variant_get (x, "v", &amp;y);
585 y = g_variant_get_variant (x);</pre></div>
586 </div>
587 </div>
588 <hr>
589 <div class="refsect2" title="Arrays">
590 <a name="gvariant-format-strings-arrays"></a><h3>Arrays</h3>
591 <p>
592     <span class="strong"><strong>
593      Characters: <code class="literal">a</code>
594     </strong></span>
595    </p>
596 <p>
597     Upon encountering an '<code class="literal">a</code>' character followed by a type string,
598     <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> will take a
599     <code class="code">(<a class="link" href="glib-GVariant.html#GVariantBuilder" title="GVariantBuilder">GVariantBuilder</a> *)</code> that has been created as an array builder
600     for an array of the type given in the type string.  The builder will have
601     <a class="link" href="glib-GVariant.html#g-variant-builder-end" title="g_variant_builder_end ()"><code class="function">g_variant_builder_end()</code></a> called on it and the
602     result will be used as the value.  As a special exception, if the given type string is a definite type, then
603     <code class="literal">NULL</code> may be given to mean an empty array of that type.
604    </p>
605 <p>
606     Upon encountering an '<code class="literal">a</code>' character followed by a type string,
607     <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> will take a pointer to a
608     <code class="code">(<a class="link" href="glib-GVariant.html#GVariantIter" title="GVariantIter">GVariantIter</a> *)</code> (ie:
609     <code class="code">(<a class="link" href="glib-GVariant.html#GVariantIter" title="GVariantIter">GVariantIter</a> **)</code>).
610     A new heap-allocated iterator is created and returned, initialised for iterating over the elements of the array.
611     This iterator should be freed when you are done with it, using
612     <code class="function">g_variant_iter_unref()</code>.
613     <code class="literal">NULL</code> may also be given to indicate that the value of the array
614     should be ignored.
615    </p>
616 <div class="refsect3" title="Examples">
617 <a name="id486923"></a><h4>Examples</h4>
618 <div class="informalexample"><pre class="programlisting">
619 GVariantBuilder *builder;
620 GVariant *value;
621
622 builder = g_variant_builder_new (G_VARIANT_TYPE_CLASS_ARRAY, NULL);
623 g_variant_builder_add (builder, "s", "when");
624 g_variant_builder_add (builder, "s", "in");
625 g_variant_builder_add (builder, "s", "the");
626 g_variant_builder_add (builder, "s", "course");
627 value = g_variant_new ("as", builder);
628
629 {
630   GVariantIter *iter;
631   gchar *str;
632
633   g_variant_get (value, "as", &amp;iter);
634   while (g_variant_iter_loop (iter, "s", &amp;str))
635     g_print ("%s\n");
636   g_variant_iter_unref (iter);
637 }</pre></div>
638 </div>
639 </div>
640 <hr>
641 <div class="refsect2" title="Maybe Types">
642 <a name="gvariant-format-strings-maybe-types"></a><h3>Maybe Types</h3>
643 <p>
644     <span class="strong"><strong>
645      Characters: <code class="literal">m</code>
646     </strong></span>
647    </p>
648 <p>
649     Maybe types are handled in two separate ways depending on the format string that follows the
650     '<code class="literal">m</code>'.  The method that is used currently depends entirely on the character immediately following the
651     '<code class="literal">m</code>'.
652    </p>
653 <p>
654     The first way is used with format strings starting with '<code class="literal">s</code>', '<code class="literal">o</code>',
655     '<code class="literal">g</code>', '<code class="literal">v</code>', '<code class="literal">@</code>', '<code class="literal">*</code>',
656     '<code class="literal">?</code>', '<code class="literal">r</code>', '<code class="literal">&amp;</code>', or '<code class="literal">^</code>'.  In all of
657     these cases, for non-maybe types, <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> takes
658     a pointer to a non-<code class="literal">NULL</code> value and
659     <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> returns (by reference) a
660     non-<code class="literal">NULL</code> pointer.  When any of these format strings are
661     prefixed with an '<code class="literal">m</code>', the type of arguments that are collected does not change in any way, but
662     <code class="literal">NULL</code> becomes a permissable value, to indicate the Nothing case.
663    </p>
664 <p>
665     The second way is used with all other format strings.  For
666     <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> an additional
667     <a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="type">gboolean</span></a> argument is collected and for
668     <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> an additional
669     <code class="code">(<a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean">gboolean</a> *)</code>.  Following this argument, the arguments that are normally
670     collected for the equivalent non-maybe type will be collected.
671    </p>
672 <p>
673     If <code class="literal">FALSE</code> is given to
674     <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> then the Nothing value is constructed and
675     the collected arguments are ignored.  Otherwise (if <code class="literal">TRUE</code> was
676     given), the arguments are used in the normal way to create the Just value.
677    </p>
678 <p>
679     If <code class="literal">NULL</code> is given to
680     <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> then the value is ignored.  If a
681     non-<code class="literal">NULL</code> pointer is given then it is used to return by reference
682     whether the value was Just.  In the case that the value was Just, the
683     <a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="type">gboolean</span></a> will be set to
684     <code class="literal">TRUE</code> and the value will be stored in the arguments in the usual
685     way.  In the case that the value was Nothing, the <a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="type">gboolean</span></a> will be set to
686     <code class="literal">FALSE</code> and the arguments will be collected in the normal way
687     but have their values set to binary zero.
688    </p>
689 <div class="refsect3" title="Examples">
690 <a name="id482813"></a><h4>Examples</h4>
691 <div class="informalexample"><pre class="programlisting">
692 GVariant *value1, *value2, *value3, *value4, *value5, *value6;
693 value1 = g_variant_new ("ms", "Hello world");
694 value2 = g_variant_new ("ms", NULL);
695 value3 = g_variant_new ("(m(ii)s)", TRUE, 123, 456, "Done");
696 value4 = g_variant_new ("(m(ii)s)", FALSE, -1, -1, "Done");          /* both '-1' are ignored. */
697 value5 = g_variant_new ("(m@(ii)s)", NULL, "Done");
698
699 {
700   GVariant *contents;
701   const gchar *cstr;
702   gboolean just;
703   gint32 x, y;
704   gchar *str;
705
706   g_variant_get (value1, "ms", &amp;str);
707   if (str != NULL)
708     g_print ("str: %s\n", str);
709   else
710     g_print ("it was null\n");
711   g_free (str);
712
713
714   g_variant_get (value2, "m&amp;s", &amp;cstr);
715   if (cstr != NULL)
716     g_print ("str: %s\n", cstr);
717   else
718     g_print ("it was null\n");
719   /* don't free 'cstr' */
720
721
722   /* NULL passed for the gboolean *, but two 'gint32 *' still collected */
723   g_variant_get (value3, "(m(ii)s)", NULL, NULL, NULL, &amp;str);
724   g_print ("string is %s\n", str);
725   g_free (str);
726
727   /* note: &amp;s used, so g_free() not needed */
728   g_variant_get (value4, "(m(ii)&amp;s)", &amp;just, &amp;x, &amp;y, &amp;cstr);
729   if (just)
730     g_print ("it was (%d, %d)\n", x, y);
731   else
732     g_print ("it was null\n");
733   g_print ("string is %s\n", cstr);
734   /* don't free 'cstr' */
735
736
737   g_variant_get (value5, "(m*s)", &amp;contents, NULL); /* ignore the string. */
738   if (contents != NULL)
739     {
740       g_variant_get (contents, "(ii)", &amp;x, &amp;y);
741       g_print ("it was (%d, %d)\n", x, y);
742       g_variant_unref (contents);
743     }
744   else
745     g_print ("it was null\n");
746 }</pre></div>
747 </div>
748 </div>
749 <hr>
750 <div class="refsect2" title="Tuples">
751 <a name="gvariant-format-strings-tuples"></a><h3>Tuples</h3>
752 <p>
753     <span class="strong"><strong>
754      Characters: <code class="code">()</code>
755     </strong></span>
756    </p>
757 <p>
758     Tuples are handled by handling each item in the tuple, in sequence.  Each item is handled in the usual way.
759    </p>
760 <div class="refsect3" title="Examples">
761 <a name="id504933"></a><h4>Examples</h4>
762 <div class="informalexample"><pre class="programlisting">
763 GVariant *value1, *value2;
764
765 value1 = g_variant_new ("(s(ii))", "Hello", 55, 77);
766 value2 = g_variant_new ("()");
767
768 {
769   gchar *string;
770   gint x, y;
771
772   g_variant_get (value1, "(s(ii))", &amp;string, &amp;x, &amp;y);
773   g_print ("%s, %d, %d\n", string, x, y);
774   g_free (string);
775
776   g_variant_get (value2, "()");   /* do nothing... */
777 }</pre></div>
778 </div>
779 </div>
780 <hr>
781 <div class="refsect2" title="Dictionaries">
782 <a name="gvariant-format-strings-dictionaries"></a><h3>Dictionaries</h3>
783 <p>
784     <span class="strong"><strong>
785      Characters: <code class="code">{}</code>
786     </strong></span>
787    </p>
788 <p>
789     Dictionary entries are handled by handling first the key, then the value.  Each is handled in the usual way.
790    </p>
791 <div class="refsect3" title="Examples">
792 <a name="id594360"></a><h4>Examples</h4>
793 <div class="informalexample"><pre class="programlisting">
794 GVariantBuilder *b;
795 GVariant *dict;
796
797 b = g_variant_builder_new (G_VARIANT_TYPE_CLASS_ARRAY, G_VARIANT_TYPE ("a{sv}"));
798 g_variant_builder_add (b, "{sv}", "name", g_variant_new_string ("foo"));
799 g_variant_builder_add (b, "{sv}", "timeout", g_variant_new_int32 (10));
800 dict = g_variant_builder_end (b);</pre></div>
801 </div>
802 </div>
803 <hr>
804 <div class="refsect2" title="GVariant *">
805 <a name="gvariant-format-strings-gvariant"></a><h3>GVariant *</h3>
806 <p>
807     <span class="strong"><strong>
808      Characters: <code class="literal">@</code>, <code class="literal">*</code>, <code class="literal">?</code>, <code class="literal">r</code>
809     </strong></span>
810
811    </p>
812 <p>
813     Upon encountering a '<code class="literal">@</code>' in front of a type string,
814     <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> takes a
815     non-<code class="literal">NULL</code> pointer to a
816     <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> and uses its value directly instead of collecting arguments to
817     create the value.  The provided <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> must have a type that matches the
818     type string following the '<code class="literal">@</code>'.  '<code class="literal">*</code>' is
819     the same as '<code class="literal">@*</code>' (ie: take a <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> of any type).
820     '<code class="literal">?</code>' is the same as '<code class="literal">@?</code>' (ie: take a
821     <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> of any basic type).  '<code class="literal">r</code>' is the same as
822     '<code class="literal">@r</code>' (ie: take a <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> of any tuple type).
823    </p>
824 <p>
825     Upon encountering a '<code class="literal">@</code>' in front of a type string,
826     <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a>
827     takes a pointer to a <code class="code">(<a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> *)</code> (ie: a
828     <code class="code">(<a class="link" href="glib-GVariant.html#GVariant" title="GVariant">GVariant</a> **)</code>) and sets it to a new reference to a
829     <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> containing the value (instead of deconstructing the value into
830     C types in the usual way).  <code class="literal">NULL</code> can be given to ignore the
831     value.  '<code class="literal">*</code>', '<code class="literal">?</code>' and '<code class="literal">r</code>' are handled in a way analogous to
832     what is stated above.
833    </p>
834 <p>
835     You can always use '<code class="literal">*</code>' as an alternative to '<code class="literal">?</code>', '<code class="literal">r</code>' or any
836     use of '<code class="literal">@</code>'.  Using the other characters where possible is recommended, however, due to the
837     improvements in type safety and code self-documentation.
838    </p>
839 <div class="refsect3" title="Examples">
840 <a name="id463922"></a><h4>Examples</h4>
841 <div class="informalexample"><pre class="programlisting">
842 GVariant *value1, *value2;
843
844 value1 = g_variant_new ("(i@ii)", 44, g_variant_new_int32 (55), 66);
845
846 /* note: consumes floating reference count on 'value1' */
847 value2 = g_variant_new ("(@(iii)*)", value1, g_variant_new_string ("foo"));
848
849 {
850   const gchar *string;
851   GVariant *tmp;
852   gsize length;
853   gint x, y, z;
854
855   g_variant_get (value2, "((iii)*)", &amp;x, &amp;y, &amp;z, &amp;tmp);
856   string = g_variant_get_string (tmp, &amp;length);
857   g_print ("it is %d %d %d %s (length=%d)\n", x, y, z, string, (int) length);
858   g_variant_unref (tmp);
859
860   /* quick way to skip all the values in a tuple */
861   g_variant_get (value2, "(rs)", NULL, &amp;string); /* or "(@(iii)s)" */
862   g_print ("i only got the string: %s\n", string);
863   g_free (string);
864 }</pre></div>
865 </div>
866 </div>
867 <hr>
868 <div class="refsect2" title="Pointers">
869 <a name="gvariant-format-strings-pointers"></a><h3>Pointers</h3>
870 <p>
871     <span class="strong"><strong>
872      Characters: <code class="code">&amp;</code>
873     </strong></span>
874    </p>
875 <p>
876     The '<code class="code">&amp;</code>' character is used to indicate that serialised data should be directly exchanged via a
877     pointer.
878    </p>
879 <p>
880     Currently, the only use for this character is when it is applied to a string (ie: '<code class="literal">&amp;s</code>',
881     '<code class="literal">&amp;o</code>' or '<code class="code">&amp;g</code>').  For
882     <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> this has absolutely no effect.  The string
883     is collected and duplicated normally.  For <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a>
884     it means that instead of creating a newly allocated copy of the string, a pointer to the serialised data is
885     returned.  This pointer should not be freed.  Validity checks are performed to ensure that the string data will
886     always be properly nul-terminated.
887    </p>
888 <div class="refsect3" title="Examples">
889 <a name="id472125"></a><h4>Examples</h4>
890 <div class="informalexample"><pre class="programlisting">
891 {
892   const gchar *str;
893   GVariant *value;
894
895   value = g_variant_new ("&amp;s", "hello world");
896   str = g_variant_get ("&amp;s", &amp;str);
897   g_print ("string is: %s\n", str);
898   /* no need to free str */
899 }</pre></div>
900 </div>
901 </div>
902 <hr>
903 <div class="refsect2" title="Convenience Conversions">
904 <a name="gvariant-format-strings-convenience"></a><h3>Convenience Conversions</h3>
905 <p>
906     <span class="strong"><strong>
907      Characters: <code class="literal">^</code>
908     </strong></span>
909    </p>
910 <p>
911     The '<code class="literal">^</code>' character currently only has one purpose: to convert to and from
912     <code class="literal">G_TYPE_STRV</code> type arrays of strings.  It is always used with
913     arrays of strings (or other string types).  It has two forms.
914    </p>
915 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
916 <li class="listitem"><p>
917       '<code class="literal">^as</code>' (or <code class="literal">o</code> or <code class="literal">g</code>)
918      </p></li>
919 <li class="listitem"><p>
920       '<code class="literal">^a&amp;s</code>' (or <code class="literal">o</code> or <code class="literal">g</code>)
921      </p></li>
922 </ul></div>
923 <p>
924     When used with <a class="link" href="glib-GVariant.html#g-variant-new" title="g_variant_new ()"><code class="function">g_variant_new()</code></a> both forms are equivalent.
925     A <code class="code">(const <a class="link" href="glib-Basic-Types.html#gchar" title="gchar">gchar</a> * const *)</code> is collected.  This must be a pointer to the
926     array of <code class="literal">NULL</code>-terminated pointers to strings.  This array is
927     converted to a <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> instance.  Copies are made, so the original
928     array may be freed immediately.
929    </p>
930 <p>
931     When used with <a class="link" href="glib-GVariant.html#g-variant-get" title="g_variant_get ()"><code class="function">g_variant_get()</code></a> the two forms have
932     different meaning.  Both return a freshly allocated
933     <code class="literal">NULL</code>-terminated array of pointers to strings.  In the case of
934     '<code class="literal">^as</code>', the strings are owned by the caller -- it is appropriate to free the array with
935     <a class="link" href="glib-String-Utility-Functions.html#g-strfreev" title="g_strfreev ()"><code class="function">g_strfreev()</code></a>.  In the case of '<code class="literal">^a&amp;s</code>',
936     a shallow copy is made; the strings themselves are embedded in the serialised data and owned by the original
937     <a class="link" href="glib-GVariant.html#GVariant" title="GVariant"><span class="type">GVariant</span></a> instance -- it is only appropriate to free the outer array
938     with <a class="link" href="glib-Memory-Allocation.html#g-free" title="g_free ()"><code class="function">g_free()</code></a>.
939    </p>
940 </div>
941 </div>
942 </div>
943 <div class="footer">
944 <hr>
945           Generated by GTK-Doc V1.13</div>
946 </body>
947 </html>