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
This commit is contained in:
Gustavo Sverzut Barbieri 2012-01-12 17:27:53 +00:00
parent 47b08d1a16
commit 9214ac6015
3 changed files with 45 additions and 1 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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();
}