From 9214ac60159c1117d88101722024f455d16b604b Mon Sep 17 00:00:00 2001 From: Gustavo Sverzut Barbieri Date: Thu, 12 Jan 2012 17:27:53 +0000 Subject: [PATCH] eina_value: hash now adopts given Eina_Hash. This should allow users to setup the hash manually, in an efficient way, then make it an Eina_Value. SVN revision: 67147 --- legacy/eina/src/include/eina_value.h | 17 +++++++++++++++++ legacy/eina/src/lib/eina_value.c | 4 +++- legacy/eina/src/tests/eina_test_value.c | 25 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/legacy/eina/src/include/eina_value.h b/legacy/eina/src/include/eina_value.h index 447d44c815..05204aad3a 100644 --- a/legacy/eina/src/include/eina_value.h +++ b/legacy/eina/src/include/eina_value.h @@ -249,6 +249,23 @@ EAPI extern const Eina_Value_Type *EINA_VALUE_TYPE_LIST; * @li eina_value_hash_vget() and eina_value_hash_vset() * @li eina_value_hash_pget() and eina_value_hash_pset() * + * eina_value_set() takes an #Eina_Value_Hash where just @c subtype + * and @c buckets_power_size are used. If there is an @c hash, it will + * be adopted and its contents must be properly configurable as @c + * subtype expects. eina_value_pset() takes a pointer to an + * #Eina_Value_Hash. For your convenience, use + * eina_value_hash_setup(). + * + * eina_value_get() and eina_value_pget() takes a pointer to + * #Eina_Value_Hash, it's an exact copy of the current structure in + * use by value, no copies are done. + * + * @note be aware that hash data is always an allocated memory of size + * defined by @c subtype->value_size. If your @c subtype is an + * integer, add as data malloc(sizeof(int)). If your @c subtype + * is an string, add as data malloc(sizeof(char*)) and this data + * value must point to strdup(string)! + * * @since 1.2 */ EAPI extern const Eina_Value_Type *EINA_VALUE_TYPE_HASH; diff --git a/legacy/eina/src/lib/eina_value.c b/legacy/eina/src/lib/eina_value.c index 0ec5eba312..99528b8977 100644 --- a/legacy/eina/src/lib/eina_value.c +++ b/legacy/eina/src/lib/eina_value.c @@ -3306,7 +3306,9 @@ _eina_value_type_hash_pset(const Eina_Value_Type *type __UNUSED__, void *mem, co if (tmem->hash) _eina_value_type_hash_flush_elements(tmem); - if (!_eina_value_type_hash_create(tmem)) + if (desc->hash) + tmem->hash = desc->hash; + else if (!_eina_value_type_hash_create(tmem)) return EINA_FALSE; tmem->subtype = desc->subtype; diff --git a/legacy/eina/src/tests/eina_test_value.c b/legacy/eina/src/tests/eina_test_value.c index d505094e17..5181a2bcf2 100644 --- a/legacy/eina/src/tests/eina_test_value.c +++ b/legacy/eina/src/tests/eina_test_value.c @@ -1265,9 +1265,12 @@ END_TEST START_TEST(eina_value_test_hash) { Eina_Value *value, other; + Eina_Value_Hash desc; char c; char buf[1024]; + char **ptr; char *str; + const char *s; eina_init(); @@ -1342,6 +1345,28 @@ START_TEST(eina_value_test_hash) fail_unless(eina_value_get(&other, &c)); fail_unless(c == 33); + desc.subtype = EINA_VALUE_TYPE_STRING; + desc.buckets_power_size = 0; + desc.hash = eina_hash_string_small_new(NULL); + fail_unless(desc.hash != NULL); + /* watch out hash pointer is to a size of subtype->value_size! */ + ptr = malloc(sizeof(char *)); + *ptr = strdup("there"); + fail_unless(eina_hash_add(desc.hash, "hi", ptr)); + ptr = malloc(sizeof(char *)); + *ptr = strdup("y"); + fail_unless(eina_hash_add(desc.hash, "x", ptr)); + + fail_unless(eina_value_set(value, desc)); + + fail_unless(eina_value_hash_get(value, "hi", &s)); + fail_unless(s != NULL); + fail_unless(strcmp(s, "there") == 0); + + fail_unless(eina_value_hash_get(value, "x", &s)); + fail_unless(s != NULL); + fail_unless(strcmp(s, "y") == 0); + eina_value_free(value); eina_shutdown(); }