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