1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of gsignond
6 * Copyright (C) 2012-2013 Intel Corporation.
8 * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <gsignond/gsignond-dictionary.h>
27 #include <gsignond/gsignond-log.h>
30 * SECTION:gsignond-dictionary
31 * @short_description: a dictionary container holding string keys and variant values
32 * @title: GSignondDictionary
33 * @include: gsignond/gsignond-dictionary.h
35 * A #GSignondDictionary is a dictionary data structure that maps string keys to #GVariant values.
36 * It's used in multiple places in gsignond and its public API to pass key-value
39 * |[ GSignondDictionary* dict = gsignond_dictionary_new();
40 * gsignond_dictionary_set_string(dict, "name", "John Smith");
41 * gsignond_dictionary_set_uint32(dict, "age", 32);
44 * gboolean success = gsignond_dictionary_get_uint32(dict, "age", &age);
45 * const gchar* name = gsignond_dictionary_get_string(dict, "name");
46 * gsignond_dictionary_unref(dict);
53 * #GSignondDictionary is a typedef for #GHashTable, which
54 * means the developers may also use methods associated with that structure.
58 * gsignond_dictionary_new_from_variant:
59 * @variant: instance of #GVariant
61 * Converts the #GVariant to #GSignondDictionary. This is useful for example if
62 * the dictionary needs to be deserialized, or if it's contained in another
63 * #GSignondDictionary and has been retrieved using gsignond_dictionary_get().
65 * Returns: (transfer full): #GSignondDictionary if successful, NULL otherwise.
68 gsignond_dictionary_new_from_variant (GVariant *variant)
70 GSignondDictionary *dict = NULL;
73 GVariant *value = NULL;
75 g_return_val_if_fail (variant != NULL, NULL);
77 dict = gsignond_dictionary_new ();
78 g_variant_iter_init (&iter, variant);
79 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
81 g_hash_table_insert (dict, key, value);
88 * gsignond_dictionary_to_variant:
89 * @dict: instance of #GSignondDictionary
91 * Converts the #GSignondDictionary to a #GVariant. The result can be serialized
92 * or put into another #GSignondDictionary using gsignond_dictionary_set().
94 * Returns: (transfer full): #GVariant object if successful, NULL otherwise.
97 gsignond_dictionary_to_variant (GSignondDictionary *dict)
99 GVariantBuilder builder;
101 GVariant *vdict = NULL;
102 const gchar *key = NULL;
103 GVariant *value = NULL;
105 g_return_val_if_fail (dict != NULL, NULL);
107 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
108 g_hash_table_iter_init (&iter, dict);
109 while (g_hash_table_iter_next (&iter,
113 g_variant_builder_add (&builder, "{sv}",
117 vdict = g_variant_builder_end (&builder);
122 * gsignond_dictionary_new:
124 * Creates a new instance of #GSignondDictionary.
126 * Returns: (transfer full): #GSignondDictionary object if successful,
130 gsignond_dictionary_new (void)
132 return g_hash_table_new_full ((GHashFunc)g_str_hash,
133 (GEqualFunc)g_str_equal,
134 (GDestroyNotify)g_free,
135 (GDestroyNotify)g_variant_unref);
139 * gsignond_dictionary_ref:
140 * @dict: instance of #GSignondDictionary
142 * Increments the reference count of the dictionary structure.
144 * Returns: the pointer to the passed in #GSignondDictionary
147 gsignond_dictionary_ref (GSignondDictionary *dict)
149 g_return_val_if_fail (dict != NULL, NULL);
151 return (GSignondDictionary*)g_hash_table_ref (dict);
155 * gsignond_dictionary_unref:
156 * @dict: instance of #GSignondDictionary
158 * Decrements the reference count of the dictionary structure. If the reference
159 * count reaches zero, the structure is deallocated and shouldn't be used.
163 gsignond_dictionary_unref (GSignondDictionary *dict)
168 g_hash_table_unref (dict);
172 * gsignond_dictionary_get:
173 * @dict: instance of #GSignondDictionary
174 * @key: the key to look up in the dictionary
176 * Retrieves a #GVariant value from the dictionary. This can be used to retrieve
177 * a value of an arbitrary type, and then convert it manually to a specific type
178 * using #GVariant methods. For most commonly used types, also getters that
179 * return the specific type directly are provided (gsignond_dictionary_get_string()
182 * Returns: (transfer none): the value; NULL is returned in case of failure (for
183 * example if the entry corresponding to the supplied key doesn't exist).
186 gsignond_dictionary_get (GSignondDictionary *dict, const gchar *key)
188 g_return_val_if_fail (dict != NULL, NULL);
189 g_return_val_if_fail (key != NULL, NULL);
191 return g_hash_table_lookup (dict, key);
195 * gsignond_dictionary_set:
196 * @dict: instance of #GSignondDictionary
197 * @key: (transfer none): key to be set
198 * @value: (transfer full): value to be set
200 * Adds or replaces key-value pair in the dictionary. This allows to set a value
201 * of an arbitrary type: it first needs to be converted to a #GVariant. For most
202 * commonly used types also type-specific setters are provided.
204 * Returns: TRUE if successful, FALSE otherwise.
207 gsignond_dictionary_set (GSignondDictionary *dict,
208 const gchar *key, GVariant *value)
210 g_return_val_if_fail (dict != NULL, FALSE);
211 g_return_val_if_fail (key != NULL, FALSE);
212 g_return_val_if_fail (value != NULL, FALSE);
214 g_variant_ref_sink(value);
215 g_hash_table_replace (
224 * gsignond_dictionary_get_boolean:
225 * @dict: instance of #GSignondDictionary
226 * @key: (transfer none): key to look up
227 * @value: points to the location where the value should be set
229 * Retrieves a gboolean value.
231 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
234 gsignond_dictionary_get_boolean (GSignondDictionary *dict, const gchar *key,
237 GVariant *variant = gsignond_dictionary_get (dict, key);
243 *value = g_variant_get_boolean (variant);
248 * gsignond_dictionary_set_boolean:
249 * @dict: instance of #GSignondDictionary
250 * @key: (transfer none): key to set
251 * @value: value to set
253 * Sets or replaces a gboolean value in the dictionary.
255 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
258 gsignond_dictionary_set_boolean (GSignondDictionary *dict, const gchar *key,
261 return gsignond_dictionary_set (dict, key, g_variant_new_boolean (value));
265 * gsignond_dictionary_get_int32:
266 * @dict: instance of #GSignondDictionary
267 * @key: (transfer none): key to look up
268 * @value: points to the location where the value should be set
270 * Retrieves a int32 value.
272 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
275 gsignond_dictionary_get_int32 (GSignondDictionary *dict, const gchar *key,
278 GVariant *variant = gsignond_dictionary_get (dict, key);
284 *value = g_variant_get_int32 (variant);
289 * gsignond_dictionary_set_int32:
290 * @dict: instance of #GSignondDictionary
291 * @key: (transfer none): key to set
292 * @value: value to set
294 * Sets or replaces a int32 value in the dictionary.
296 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
299 gsignond_dictionary_set_int32 (GSignondDictionary *dict, const gchar *key,
302 return gsignond_dictionary_set (dict, key, g_variant_new_int32 (value));
306 * gsignond_dictionary_get_uint32:
307 * @dict: instance of #GSignondDictionary
308 * @key: (transfer none): key to look up
309 * @value: points to the location where the value should be set
311 * Retrieves a uint32 value.
313 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
316 gsignond_dictionary_get_uint32 (GSignondDictionary *dict, const gchar *key,
319 GVariant *variant = gsignond_dictionary_get (dict, key);
325 *value = g_variant_get_uint32 (variant);
330 * gsignond_dictionary_set_uint32:
331 * @dict: instance of #GSignondDictionary
332 * @key: (transfer none): key to set
333 * @value: value to set
335 * Sets or replaces a uint32 value in the dictionary.
337 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
340 gsignond_dictionary_set_uint32 (GSignondDictionary *dict, const gchar *key,
343 return gsignond_dictionary_set (dict, key, g_variant_new_uint32 (value));
347 * gsignond_dictionary_get_int64:
348 * @dict: instance of #GSignondDictionary
349 * @key: (transfer none): key to look up
350 * @value: points to the location where the value should be set
352 * Retrieves a int64 value.
354 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
357 gsignond_dictionary_get_int64 (GSignondDictionary *dict, const gchar *key,
360 GVariant *variant = gsignond_dictionary_get (dict, key);
366 *value = g_variant_get_int64 (variant);
371 * gsignond_dictionary_set_int64:
372 * @dict: instance of #GSignondDictionary
373 * @key: (transfer none): key to set
374 * @value: value to set
376 * Sets or replaces a int64 value in the dictionary.
378 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
381 gsignond_dictionary_set_int64 (GSignondDictionary *dict, const gchar *key,
384 return gsignond_dictionary_set (dict, key, g_variant_new_int64 (value));
388 * gsignond_dictionary_get_uint64:
389 * @dict: instance of #GSignondDictionary
390 * @key: (transfer none): key to look up
391 * @value: points to the location where the value should be set
393 * Retrieves a uint64 value.
395 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
398 gsignond_dictionary_get_uint64 (GSignondDictionary *dict, const gchar *key,
401 GVariant *variant = gsignond_dictionary_get (dict, key);
407 *value = g_variant_get_uint64 (variant);
412 * gsignond_dictionary_set_uint64:
413 * @dict: instance of #GSignondDictionary
414 * @key: (transfer none): key to set
415 * @value: value to set
417 * Sets or replaces a uint64 value in the dictionary.
419 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
422 gsignond_dictionary_set_uint64 (GSignondDictionary *dict, const gchar *key,
425 return gsignond_dictionary_set (dict, key, g_variant_new_uint64 (value));
430 * gsignond_dictionary_get_string:
431 * @dict: instance of #GSignondDictionary
432 * @key: (transfer none): key to look up
434 * Retrieves a string value.
436 * Returns: (transfer none): the value if it was retrieved successfully, NULL otherwise.
439 gsignond_dictionary_get_string (GSignondDictionary *dict, const gchar *key)
441 GVariant *variant = gsignond_dictionary_get (dict, key);
446 return g_variant_get_string (variant, NULL);
450 * gsignond_dictionary_set_string:
451 * @dict: instance of #GSignondDictionary
452 * @key: (transfer none): key to set
453 * @value: (transfer none): value to set
455 * Sets or replaces a string value in the dictionary.
457 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
460 gsignond_dictionary_set_string (GSignondDictionary *dict, const gchar *key,
463 return gsignond_dictionary_set (dict, key, g_variant_new_string (value));
467 * gsignond_dictionary_remove:
468 * @dict: instance of #GSignondDictionary
469 * @key: (transfer none): key which needs to be removed from the dictionary
471 * Removes key-value pair in the dictionary as per key.
473 * Returns: TRUE if successful, FALSE otherwise.
476 gsignond_dictionary_remove (GSignondDictionary *dict, const gchar *key)
478 g_return_val_if_fail (dict != NULL, FALSE);
479 g_return_val_if_fail (key != NULL, FALSE);
481 return g_hash_table_remove (
487 * gsignond_dictionary_copy:
488 * @other: instance of #GSignondDictionary
490 * Creates a copy of the dictionary.
492 * Returns: (transfer full): #GSignondDictionary object if the copy was successful,
496 gsignond_dictionary_copy (GSignondDictionary *other)
498 GSignondDictionary *dict = NULL;
501 GVariant *value = NULL;
503 g_return_val_if_fail (other != NULL, NULL);
505 dict = gsignond_dictionary_new ();
507 g_hash_table_iter_init (&iter, other);
508 while (g_hash_table_iter_next (&iter,
512 gsignond_dictionary_set (dict, key, value);