eina_value: array now adopts given Eina_Inarray.

This should allow users to setup the array manually, in an efficient
way, then make it an Eina_Value.



SVN revision: 67145
This commit is contained in:
Gustavo Sverzut Barbieri 2012-01-12 17:07:07 +00:00
parent bf29583354
commit d5de2617e0
3 changed files with 48 additions and 2 deletions

View File

@ -205,6 +205,16 @@ EAPI extern const Eina_Value_Type *EINA_VALUE_TYPE_STRING;
* @li eina_value_array_vget() and eina_value_array_vset()
* @li eina_value_array_pget() and eina_value_array_pset()
*
* eina_value_set() takes an #Eina_Value_Array where just @c subtype
* and @c step are used. If there is an @c array, 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_Array.
* For your convenience, use eina_value_array_setup().
*
* eina_value_get() and eina_value_pget() takes a pointer to
* #Eina_Value_Array, it's an exact copy of the current structure in
* use by value, no copies are done.
*
* @since 1.2
*/
EAPI extern const Eina_Value_Type *EINA_VALUE_TYPE_ARRAY;

View File

@ -2612,22 +2612,37 @@ _eina_value_type_array_pset(const Eina_Value_Type *type __UNUSED__, void *mem, c
{
Eina_Value_Array *tmem = mem;
const Eina_Value_Array *desc = ptr;
Eina_Inarray *desc_array;
if ((!tmem->subtype) && (!desc->subtype))
return EINA_TRUE;
desc_array = desc->array;
if (desc_array)
{
EINA_SAFETY_ON_FALSE_RETURN_VAL
(desc_array->member_size == desc->subtype->value_size, EINA_FALSE);
}
if (tmem->array)
{
_eina_value_type_array_flush_elements(tmem);
eina_inarray_setup(tmem->array, desc->subtype->value_size, desc->step);
if (desc_array)
eina_inarray_free(tmem->array);
else
eina_inarray_setup(tmem->array, desc->subtype->value_size,
desc->step);
}
else
else if (!desc_array)
{
tmem->array = eina_inarray_new(desc->subtype->value_size, desc->step);
if (!tmem->array)
return EINA_FALSE;
}
if (desc_array)
tmem->array = desc_array;
tmem->subtype = desc->subtype;
return EINA_TRUE;

View File

@ -1047,6 +1047,8 @@ END_TEST
START_TEST(eina_value_test_array)
{
Eina_Value *value, other;
Eina_Value_Array desc;
Eina_Inarray *inarray;
char c;
char buf[1024];
char *str;
@ -1128,6 +1130,25 @@ START_TEST(eina_value_test_array)
fail_unless(eina_value_get(&other, &c));
fail_unless(c == 33);
inarray = eina_inarray_new(sizeof(char), 0);
fail_unless(inarray != NULL);
c = 11;
fail_unless(eina_inarray_append(inarray, &c) >= 0);
c = 21;
fail_unless(eina_inarray_append(inarray, &c) >= 0);
c = 31;
fail_unless(eina_inarray_append(inarray, &c) >= 0);
desc.subtype = EINA_VALUE_TYPE_CHAR;
desc.step = 0;
desc.array = inarray; /* will be adopted and freed by value */
fail_unless(eina_value_set(value, desc)); /* manually configure */
fail_unless(eina_value_array_get(value, 0, &c));
fail_unless(c == 11);
fail_unless(eina_value_array_get(value, 1, &c));
fail_unless(c == 21);
fail_unless(eina_value_array_get(value, 2, &c));
fail_unless(c == 31);
eina_value_free(value);
eina_shutdown();
}