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_builder:
89 * @dict: instance of #GSignondDictionary
91 * Converts the #GSignondDictionary to a #GVariantBuilder of type
92 * G_VARIANT_TYPE_VARDICT.
94 * Caller should use g_variant_builder_unref() on the return value when it is
97 * Returns: (transfer full): #GVariantBuilder if successful, NULL otherwise.
100 gsignond_dictionary_to_variant_builder (GSignondDictionary *dict)
102 GVariantBuilder *builder;
104 const gchar *key = NULL;
105 GVariant *value = NULL;
107 g_return_val_if_fail (dict != NULL, NULL);
109 builder = g_variant_builder_new (G_VARIANT_TYPE_VARDICT);
111 g_hash_table_iter_init (&iter, dict);
112 while (g_hash_table_iter_next (&iter, (gpointer)&key, (gpointer)&value))
114 g_variant_builder_add (builder, "{sv}", key, value);
121 * gsignond_dictionary_to_variant:
122 * @dict: instance of #GSignondDictionary
124 * Converts the #GSignondDictionary to a #GVariant. The result can be serialized
125 * or put into another #GSignondDictionary using gsignond_dictionary_set().
127 * Returns: (transfer full): #GVariant object if successful, NULL otherwise.
130 gsignond_dictionary_to_variant (GSignondDictionary *dict)
132 GVariantBuilder *builder = NULL;
133 GVariant *vdict = NULL;
135 g_return_val_if_fail (dict != NULL, NULL);
137 builder = gsignond_dictionary_to_variant_builder (dict);
138 if (!builder) return NULL;
140 vdict = g_variant_builder_end (builder);
142 g_variant_builder_unref (builder);
148 * gsignond_dictionary_new:
150 * Creates a new instance of #GSignondDictionary.
152 * Returns: (transfer full): #GSignondDictionary object if successful,
156 gsignond_dictionary_new (void)
158 return g_hash_table_new_full ((GHashFunc)g_str_hash,
159 (GEqualFunc)g_str_equal,
160 (GDestroyNotify)g_free,
161 (GDestroyNotify)g_variant_unref);
165 * gsignond_dictionary_ref:
166 * @dict: instance of #GSignondDictionary
168 * Increments the reference count of the dictionary structure.
170 * Returns: the pointer to the passed in #GSignondDictionary
173 gsignond_dictionary_ref (GSignondDictionary *dict)
175 g_return_val_if_fail (dict != NULL, NULL);
177 return (GSignondDictionary*)g_hash_table_ref (dict);
181 * gsignond_dictionary_unref:
182 * @dict: instance of #GSignondDictionary
184 * Decrements the reference count of the dictionary structure. If the reference
185 * count reaches zero, the structure is deallocated and shouldn't be used.
189 gsignond_dictionary_unref (GSignondDictionary *dict)
194 g_hash_table_unref (dict);
198 * gsignond_dictionary_get:
199 * @dict: instance of #GSignondDictionary
200 * @key: the key to look up in the dictionary
202 * Retrieves a #GVariant value from the dictionary. This can be used to retrieve
203 * a value of an arbitrary type, and then convert it manually to a specific type
204 * using #GVariant methods. For most commonly used types, also getters that
205 * return the specific type directly are provided (gsignond_dictionary_get_string()
208 * Returns: (transfer none): the value; NULL is returned in case of failure (for
209 * example if the entry corresponding to the supplied key doesn't exist).
212 gsignond_dictionary_get (GSignondDictionary *dict, const gchar *key)
214 g_return_val_if_fail (dict != NULL, NULL);
215 g_return_val_if_fail (key != NULL, NULL);
217 return g_hash_table_lookup (dict, key);
221 * gsignond_dictionary_set:
222 * @dict: instance of #GSignondDictionary
223 * @key: (transfer none): key to be set
224 * @value: (transfer full): value to be set
226 * Adds or replaces key-value pair in the dictionary. This allows to set a value
227 * of an arbitrary type: it first needs to be converted to a #GVariant. For most
228 * commonly used types also type-specific setters are provided.
230 * Returns: TRUE if successful, FALSE otherwise.
233 gsignond_dictionary_set (GSignondDictionary *dict,
234 const gchar *key, GVariant *value)
236 g_return_val_if_fail (dict != NULL, FALSE);
237 g_return_val_if_fail (key != NULL, FALSE);
238 g_return_val_if_fail (value != NULL, FALSE);
240 g_variant_ref_sink(value);
241 g_hash_table_replace (
250 * gsignond_dictionary_get_boolean:
251 * @dict: instance of #GSignondDictionary
252 * @key: (transfer none): key to look up
253 * @value: points to the location where the value should be set
255 * Retrieves a gboolean value.
257 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
260 gsignond_dictionary_get_boolean (GSignondDictionary *dict, const gchar *key,
263 GVariant *variant = gsignond_dictionary_get (dict, key);
269 *value = g_variant_get_boolean (variant);
274 * gsignond_dictionary_set_boolean:
275 * @dict: instance of #GSignondDictionary
276 * @key: (transfer none): key to set
277 * @value: value to set
279 * Sets or replaces a gboolean value in the dictionary.
281 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
284 gsignond_dictionary_set_boolean (GSignondDictionary *dict, const gchar *key,
287 return gsignond_dictionary_set (dict, key, g_variant_new_boolean (value));
291 * gsignond_dictionary_get_int32:
292 * @dict: instance of #GSignondDictionary
293 * @key: (transfer none): key to look up
294 * @value: points to the location where the value should be set
296 * Retrieves a int32 value.
298 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
301 gsignond_dictionary_get_int32 (GSignondDictionary *dict, const gchar *key,
304 GVariant *variant = gsignond_dictionary_get (dict, key);
310 *value = g_variant_get_int32 (variant);
315 * gsignond_dictionary_set_int32:
316 * @dict: instance of #GSignondDictionary
317 * @key: (transfer none): key to set
318 * @value: value to set
320 * Sets or replaces a int32 value in the dictionary.
322 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
325 gsignond_dictionary_set_int32 (GSignondDictionary *dict, const gchar *key,
328 return gsignond_dictionary_set (dict, key, g_variant_new_int32 (value));
332 * gsignond_dictionary_get_uint32:
333 * @dict: instance of #GSignondDictionary
334 * @key: (transfer none): key to look up
335 * @value: points to the location where the value should be set
337 * Retrieves a uint32 value.
339 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
342 gsignond_dictionary_get_uint32 (GSignondDictionary *dict, const gchar *key,
345 GVariant *variant = gsignond_dictionary_get (dict, key);
351 *value = g_variant_get_uint32 (variant);
356 * gsignond_dictionary_set_uint32:
357 * @dict: instance of #GSignondDictionary
358 * @key: (transfer none): key to set
359 * @value: value to set
361 * Sets or replaces a uint32 value in the dictionary.
363 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
366 gsignond_dictionary_set_uint32 (GSignondDictionary *dict, const gchar *key,
369 return gsignond_dictionary_set (dict, key, g_variant_new_uint32 (value));
373 * gsignond_dictionary_get_int64:
374 * @dict: instance of #GSignondDictionary
375 * @key: (transfer none): key to look up
376 * @value: points to the location where the value should be set
378 * Retrieves a int64 value.
380 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
383 gsignond_dictionary_get_int64 (GSignondDictionary *dict, const gchar *key,
386 GVariant *variant = gsignond_dictionary_get (dict, key);
392 *value = g_variant_get_int64 (variant);
397 * gsignond_dictionary_set_int64:
398 * @dict: instance of #GSignondDictionary
399 * @key: (transfer none): key to set
400 * @value: value to set
402 * Sets or replaces a int64 value in the dictionary.
404 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
407 gsignond_dictionary_set_int64 (GSignondDictionary *dict, const gchar *key,
410 return gsignond_dictionary_set (dict, key, g_variant_new_int64 (value));
414 * gsignond_dictionary_get_uint64:
415 * @dict: instance of #GSignondDictionary
416 * @key: (transfer none): key to look up
417 * @value: points to the location where the value should be set
419 * Retrieves a uint64 value.
421 * Returns: TRUE if the value was retrieved successfully, FALSE otherwise.
424 gsignond_dictionary_get_uint64 (GSignondDictionary *dict, const gchar *key,
427 GVariant *variant = gsignond_dictionary_get (dict, key);
433 *value = g_variant_get_uint64 (variant);
438 * gsignond_dictionary_set_uint64:
439 * @dict: instance of #GSignondDictionary
440 * @key: (transfer none): key to set
441 * @value: value to set
443 * Sets or replaces a uint64 value in the dictionary.
445 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
448 gsignond_dictionary_set_uint64 (GSignondDictionary *dict, const gchar *key,
451 return gsignond_dictionary_set (dict, key, g_variant_new_uint64 (value));
456 * gsignond_dictionary_get_string:
457 * @dict: instance of #GSignondDictionary
458 * @key: (transfer none): key to look up
460 * Retrieves a string value.
462 * Returns: (transfer none): the value if it was retrieved successfully, NULL otherwise.
465 gsignond_dictionary_get_string (GSignondDictionary *dict, const gchar *key)
467 GVariant *variant = gsignond_dictionary_get (dict, key);
472 return g_variant_get_string (variant, NULL);
476 * gsignond_dictionary_set_string:
477 * @dict: instance of #GSignondDictionary
478 * @key: (transfer none): key to set
479 * @value: (transfer none): value to set
481 * Sets or replaces a string value in the dictionary.
483 * Returns: TRUE if the value was set or replaced successfully, FALSE otherwise.
486 gsignond_dictionary_set_string (GSignondDictionary *dict, const gchar *key,
489 return gsignond_dictionary_set (dict, key, g_variant_new_string (value));
493 * gsignond_dictionary_remove:
494 * @dict: instance of #GSignondDictionary
495 * @key: (transfer none): key which needs to be removed from the dictionary
497 * Removes key-value pair in the dictionary as per key.
499 * Returns: TRUE if successful, FALSE otherwise.
502 gsignond_dictionary_remove (GSignondDictionary *dict, const gchar *key)
504 g_return_val_if_fail (dict != NULL, FALSE);
505 g_return_val_if_fail (key != NULL, FALSE);
507 return g_hash_table_remove (
513 * gsignond_dictionary_copy:
514 * @other: instance of #GSignondDictionary
516 * Creates a copy of the dictionary.
518 * Returns: (transfer full): #GSignondDictionary object if the copy was successful,
522 gsignond_dictionary_copy (GSignondDictionary *other)
524 GSignondDictionary *dict = NULL;
527 GVariant *value = NULL;
529 g_return_val_if_fail (other != NULL, NULL);
531 dict = gsignond_dictionary_new ();
533 g_hash_table_iter_init (&iter, other);
534 while (g_hash_table_iter_next (&iter,
538 gsignond_dictionary_set (dict, key, value);
546 * gsignond_dictionary_contains:
547 * @dict: instance of #GSignondDictionary
548 * @key: (transfer none): key to check
550 * Checks if the @dict contains @key.
552 * Returns: TRUE if found, FALSE otherwise.
555 gsignond_dictionary_contains (GSignondDictionary *dict,
558 g_return_val_if_fail (dict != NULL, FALSE);
559 g_return_val_if_fail (key != NULL, FALSE);
561 return g_hash_table_contains (dict, key);