re86.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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>&lt;GVariantDict&gt;: </title>
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.2">
  7. <link rel="home" href="index.html" title="">
  8. <link rel="up" href="ch01.html" title="GLib">
  9. <link rel="prev" href="re85.html" title="&lt;%GLibVariantClass&gt;">
  10. <link rel="next" href="re87.html" title="&lt;%GLibVariantParseError&gt;">
  11. <meta name="generator" content="GTK-Doc V1.33.1 (XML mode)">
  12. <link rel="stylesheet" href="style.css" type="text/css">
  13. </head>
  14. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  15. <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
  16. <td width="100%" align="left" class="shortcuts"></td>
  17. <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
  18. <td><img src="up-insensitive.png" width="16" height="16" border="0"></td>
  19. <td><a accesskey="p" href="re85.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
  20. <td><a accesskey="n" href="re87.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
  21. </tr></table>
  22. <div class="refentry">
  23. <a name="id-1.1.87"></a><div class="titlepage"></div>
  24. <div class="refnamediv">
  25. <h2>&lt;GVariantDict&gt;</h2>
  26. <p>&lt;GVariantDict&gt;</p>
  27. </div>
  28. <div class="refsect1">
  29. <a name="id-1.1.87.2"></a><h2>Description</h2>
  30. <p><span class="type">GVariantDict</span> is a mutable interface to <span class="type">GVariant</span> dictionaries.
  31. </p>
  32. <p>It can be used for doing a sequence of dictionary lookups in an
  33. efficient way on an existing <span class="type">GVariant</span> dictionary or it can be used
  34. to construct new dictionaries with a hashtable-like interface. It
  35. can also be used for taking existing dictionaries and modifying them
  36. in order to create new ones.
  37. </p>
  38. <p><span class="type">GVariantDict</span> can only be used with <code class="constant">G_VARIANT_TYPE_VARDICT</code>
  39. dictionaries.
  40. </p>
  41. <p>It is possible to use <span class="type">GVariantDict</span> allocated on the stack or on the
  42. heap. When using a stack-allocated <span class="type">GVariantDict</span>, you begin with a
  43. call to <code class="function">g_variant_dict_init()</code> and free the resources with a call to
  44. <code class="function">g_variant_dict_clear()</code>.
  45. </p>
  46. <p>Heap-allocated <span class="type">GVariantDict</span> follows normal refcounting rules: you
  47. allocate it with <code class="function">g_variant_dict_new()</code> and use <code class="function">g_variant_dict_ref()</code>
  48. and <code class="function">g_variant_dict_unref()</code>.
  49. </p>
  50. <p><code class="function">g_variant_dict_end()</code> is used to convert the <span class="type">GVariantDict</span> back into a
  51. dictionary-type <span class="type">GVariant</span>. When used with stack-allocated instances,
  52. this also implicitly frees all associated memory, but for
  53. heap-allocated instances, you must still call <code class="function">g_variant_dict_unref()</code>
  54. afterwards.
  55. </p>
  56. <p>You will typically want to use a heap-allocated <span class="type">GVariantDict</span> when
  57. you expose it as part of an API. For most other uses, the
  58. stack-allocated form will be more convenient.
  59. </p>
  60. <p>Consider the following two examples that do the same thing in each
  61. style: take an existing dictionary and look up the "count" uint32
  62. key, adding 1 to it if it is found, or returning an error if the
  63. key is not found. Each returns the new dictionary as a floating
  64. <span class="type">GVariant</span>.
  65. </p>
  66. <div class="refsect2">
  67. <a name="id-1.1.87.2.10"></a><h3>Using a stack-allocated GVariantDict</h3>
  68. <div class="informalexample"><pre class="programlisting">
  69. GVariant *
  70. add_to_count (GVariant *orig,
  71. GError **error)
  72. {
  73. GVariantDict dict;
  74. guint32 count;
  75. g_variant_dict_init (&amp;dict, orig);
  76. if (!g_variant_dict_lookup (&amp;dict, "count", "u", &amp;count))
  77. {
  78. g_set_error (...);
  79. g_variant_dict_clear (&amp;dict);
  80. return NULL;
  81. }
  82. g_variant_dict_insert (&amp;dict, "count", "u", count + 1);
  83. return g_variant_dict_end (&amp;dict);
  84. }
  85. </pre></div>
  86. </div>
  87. <div class="refsect2">
  88. <a name="id-1.1.87.2.11"></a><h3>Using heap-allocated GVariantDict</h3>
  89. <div class="informalexample"><pre class="programlisting">
  90. GVariant *
  91. add_to_count (GVariant *orig,
  92. GError **error)
  93. {
  94. GVariantDict *dict;
  95. GVariant *result;
  96. guint32 count;
  97. dict = g_variant_dict_new (orig);
  98. if (g_variant_dict_lookup (dict, "count", "u", &amp;count))
  99. {
  100. g_variant_dict_insert (dict, "count", "u", count + 1);
  101. result = g_variant_dict_end (dict);
  102. }
  103. else
  104. {
  105. g_set_error (...);
  106. result = NULL;
  107. }
  108. g_variant_dict_unref (dict);
  109. return result;
  110. }
  111. </pre></div>
  112. </div>
  113. </div>
  114. <div class="refsect1">
  115. <a name="id-1.1.87.3"></a><h2>Functions</h2>
  116. <div class="refsect2">
  117. <a name="id-1.1.87.3.2"></a><h3>clear</h3>
  118. <div class="informalexample"><pre class="programlisting">(define-values () (variant-dict:clear self))
  119. </pre></div>
  120. <p>Releases all memory associated with a <span class="type">GVariantDict</span> without freeing
  121. the <span class="type">GVariantDict</span> structure itself.
  122. </p>
  123. <p>It typically only makes sense to do this on a stack-allocated
  124. <span class="type">GVariantDict</span> if you want to abort building the value part-way
  125. through. This function need not be called if you call
  126. <code class="function">g_variant_dict_end()</code> and it also doesn't need to be called on dicts
  127. allocated with g_variant_dict_new (see <code class="function">g_variant_dict_unref()</code> for
  128. that).
  129. </p>
  130. <p>It is valid to call this function on either an initialised
  131. <span class="type">GVariantDict</span> or one that was previously cleared by an earlier call
  132. to <code class="function">g_variant_dict_clear()</code> but it is not valid to call this function
  133. on uninitialised memory.</p>
  134. <div class="refsect3">
  135. <a name="id-1.1.87.3.2.6"></a><h4>Parameters</h4>
  136. <div class="informaltable"><table><tr>
  137. <td class="parameter_name"><p>dict</p></td>
  138. <td class="parameter_description">
  139. <p>a <span class="type">GVariantDict</span></p>
  140. <p>Passed as <code class="code">self</code></p>
  141. </td>
  142. </tr></table></div>
  143. </div>
  144. </div>
  145. <div class="refsect2">
  146. <a name="id-1.1.87.3.3"></a><h3>contains?</h3>
  147. <div class="informalexample"><pre class="programlisting">(define-values (%return) (variant-dict:contains? self key))
  148. </pre></div>
  149. <p>Checks if <em class="parameter"><code>key</code></em> exists in <em class="parameter"><code>dict</code></em>.</p>
  150. <div class="refsect3">
  151. <a name="id-1.1.87.3.3.4"></a><h4>Parameters</h4>
  152. <div class="informaltable"><table>
  153. <tr>
  154. <td class="parameter_name"><p>dict</p></td>
  155. <td class="parameter_description">
  156. <p>a <span class="type">GVariantDict</span></p>
  157. <p>Passed as <code class="code">self</code></p>
  158. </td>
  159. </tr>
  160. <tr>
  161. <td class="parameter_name"><p>key</p></td>
  162. <td class="parameter_description">
  163. <p>the key to look up in the dictionary</p>
  164. <p>Passed as <code class="code">key</code></p>
  165. </td>
  166. </tr>
  167. </table></div>
  168. </div>
  169. </div>
  170. <div class="refsect2">
  171. <a name="id-1.1.87.3.4"></a><h3>end</h3>
  172. <div class="informalexample"><pre class="programlisting">(define-values (%return) (variant-dict:end self))
  173. </pre></div>
  174. <p>Returns the current value of <em class="parameter"><code>dict</code></em> as a <span class="type">GVariant</span> of type
  175. <code class="constant">G_VARIANT_TYPE_VARDICT</code>, clearing it in the process.
  176. </p>
  177. <p>It is not permissible to use <em class="parameter"><code>dict</code></em> in any way after this call except
  178. for reference counting operations (in the case of a heap-allocated
  179. <span class="type">GVariantDict</span>) or by reinitialising it with <code class="function">g_variant_dict_init()</code> (in
  180. the case of stack-allocated).</p>
  181. <div class="refsect3">
  182. <a name="id-1.1.87.3.4.5"></a><h4>Parameters</h4>
  183. <div class="informaltable"><table><tr>
  184. <td class="parameter_name"><p>dict</p></td>
  185. <td class="parameter_description">
  186. <p>a <span class="type">GVariantDict</span></p>
  187. <p>Passed as <code class="code">self</code></p>
  188. </td>
  189. </tr></table></div>
  190. </div>
  191. </div>
  192. <div class="refsect2">
  193. <a name="id-1.1.87.3.5"></a><h3>insert-value</h3>
  194. <div class="informalexample"><pre class="programlisting">(define-values () (variant-dict:insert-value self key value))
  195. </pre></div>
  196. <p>Inserts (or replaces) a key in a <span class="type">GVariantDict</span>.
  197. </p>
  198. <p><em class="parameter"><code>value</code></em> is consumed if it is floating.</p>
  199. <div class="refsect3">
  200. <a name="id-1.1.87.3.5.5"></a><h4>Parameters</h4>
  201. <div class="informaltable"><table>
  202. <tr>
  203. <td class="parameter_name"><p>dict</p></td>
  204. <td class="parameter_description">
  205. <p>a <span class="type">GVariantDict</span></p>
  206. <p>Passed as <code class="code">self</code></p>
  207. </td>
  208. </tr>
  209. <tr>
  210. <td class="parameter_name"><p>key</p></td>
  211. <td class="parameter_description">
  212. <p>the key to insert a value for</p>
  213. <p>Passed as <code class="code">key</code></p>
  214. </td>
  215. </tr>
  216. <tr>
  217. <td class="parameter_name"><p>value</p></td>
  218. <td class="parameter_description">
  219. <p>the value to insert</p>
  220. <p>Passed as <code class="code">value</code></p>
  221. </td>
  222. </tr>
  223. </table></div>
  224. </div>
  225. </div>
  226. <div class="refsect2">
  227. <a name="id-1.1.87.3.6"></a><h3>lookup-value</h3>
  228. <div class="informalexample"><pre class="programlisting">(define-values (%return) (variant-dict:lookup-value self key expected-type))
  229. </pre></div>
  230. <p>Looks up a value in a <span class="type">GVariantDict</span>.
  231. </p>
  232. <p>If <em class="parameter"><code>key</code></em> is not found in <em class="parameter"><code>dictionary</code></em>, <code class="constant">NULL</code> is returned.
  233. </p>
  234. <p>The <em class="parameter"><code>expected_type</code></em> string specifies what type of value is expected.
  235. If the value associated with <em class="parameter"><code>key</code></em> has a different type then <code class="constant">NULL</code> is
  236. returned.
  237. </p>
  238. <p>If the key is found and the value has the correct type, it is
  239. returned. If <em class="parameter"><code>expected_type</code></em> was specified then any non-<code class="constant">NULL</code> return
  240. value will have this type.</p>
  241. <div class="refsect3">
  242. <a name="id-1.1.87.3.6.7"></a><h4>Parameters</h4>
  243. <div class="informaltable"><table>
  244. <tr>
  245. <td class="parameter_name"><p>dict</p></td>
  246. <td class="parameter_description">
  247. <p>a <span class="type">GVariantDict</span></p>
  248. <p>Passed as <code class="code">self</code></p>
  249. </td>
  250. </tr>
  251. <tr>
  252. <td class="parameter_name"><p>key</p></td>
  253. <td class="parameter_description">
  254. <p>the key to look up in the dictionary</p>
  255. <p>Passed as <code class="code">key</code></p>
  256. </td>
  257. </tr>
  258. <tr>
  259. <td class="parameter_name"><p>expected_type</p></td>
  260. <td class="parameter_description">
  261. <p>a <span class="type">GVariantType</span>, or <code class="constant">NULL</code></p>
  262. <p>Passed as <code class="code">expected-type</code></p>
  263. </td>
  264. </tr>
  265. </table></div>
  266. </div>
  267. </div>
  268. <div class="refsect2">
  269. <a name="id-1.1.87.3.7"></a><h3>ref</h3>
  270. <div class="informalexample"><pre class="programlisting">(define-values (%return) (variant-dict:ref self))
  271. </pre></div>
  272. <p>Increases the reference count on <em class="parameter"><code>dict</code></em>.
  273. </p>
  274. <p>Don't call this on stack-allocated <span class="type">GVariantDict</span> instances or bad
  275. things will happen.</p>
  276. <div class="refsect3">
  277. <a name="id-1.1.87.3.7.5"></a><h4>Parameters</h4>
  278. <div class="informaltable"><table><tr>
  279. <td class="parameter_name"><p>dict</p></td>
  280. <td class="parameter_description">
  281. <p>a heap-allocated <span class="type">GVariantDict</span></p>
  282. <p>Passed as <code class="code">self</code></p>
  283. </td>
  284. </tr></table></div>
  285. </div>
  286. </div>
  287. <div class="refsect2">
  288. <a name="id-1.1.87.3.8"></a><h3>remove?</h3>
  289. <div class="informalexample"><pre class="programlisting">(define-values (%return) (variant-dict:remove? self key))
  290. </pre></div>
  291. <p>Removes a key and its associated value from a <span class="type">GVariantDict</span>.</p>
  292. <div class="refsect3">
  293. <a name="id-1.1.87.3.8.4"></a><h4>Parameters</h4>
  294. <div class="informaltable"><table>
  295. <tr>
  296. <td class="parameter_name"><p>dict</p></td>
  297. <td class="parameter_description">
  298. <p>a <span class="type">GVariantDict</span></p>
  299. <p>Passed as <code class="code">self</code></p>
  300. </td>
  301. </tr>
  302. <tr>
  303. <td class="parameter_name"><p>key</p></td>
  304. <td class="parameter_description">
  305. <p>the key to remove</p>
  306. <p>Passed as <code class="code">key</code></p>
  307. </td>
  308. </tr>
  309. </table></div>
  310. </div>
  311. </div>
  312. <div class="refsect2">
  313. <a name="id-1.1.87.3.9"></a><h3>unref</h3>
  314. <div class="informalexample"><pre class="programlisting">(define-values () (variant-dict:unref self))
  315. </pre></div>
  316. <p>Decreases the reference count on <em class="parameter"><code>dict</code></em>.
  317. </p>
  318. <p>In the event that there are no more references, releases all memory
  319. associated with the <span class="type">GVariantDict</span>.
  320. </p>
  321. <p>Don't call this on stack-allocated <span class="type">GVariantDict</span> instances or bad
  322. things will happen.</p>
  323. <div class="refsect3">
  324. <a name="id-1.1.87.3.9.6"></a><h4>Parameters</h4>
  325. <div class="informaltable"><table><tr>
  326. <td class="parameter_name"><p>dict</p></td>
  327. <td class="parameter_description">
  328. <p>a heap-allocated <span class="type">GVariantDict</span></p>
  329. <p>Passed as <code class="code">self</code></p>
  330. </td>
  331. </tr></table></div>
  332. </div>
  333. </div>
  334. <div class="refsect2">
  335. <a name="id-1.1.87.3.10"></a><h3>variant-dict:new</h3>
  336. <div class="informalexample"><pre class="programlisting">(define-values (%return) (variant-dict:new from-asv))
  337. </pre></div>
  338. <p>Undocumented</p>
  339. <div class="refsect3">
  340. <a name="id-1.1.87.3.10.4"></a><h4>Parameters</h4>
  341. <div class="informaltable"><table><tr>
  342. <td class="parameter_name"><p>from_asv</p></td>
  343. <td class="parameter_description">
  344. <p></p>
  345. <p>Passed as <code class="code">from-asv</code></p>
  346. </td>
  347. </tr></table></div>
  348. </div>
  349. </div>
  350. </div>
  351. </div>
  352. <div class="footer">
  353. <hr>Generated by GTK-Doc V1.33.1</div>
  354. </body>
  355. </html>