From: bdilly Date: Thu, 1 Mar 2012 17:15:54 +0000 (+0000) Subject: Eina: Add eina_value_array_value_get(). X-Git-Tag: 1.0_branch~169 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=36376523b8d49c0d42a13707a2ec93eb23e89fb2;p=profile%2Fivi%2Feina.git Eina: Add eina_value_array_value_get(). Since k-s is on the road this days, we'd better write some code ourselves. This is array counterpart of eina_value_struct_value_get(), and retrieves an item from an Eina_Value_Array as an Eina_Value. Code writing the setter is welcome. Patch by: "Raphael Kubo da Costa" git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@68581 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- diff --git a/src/include/eina_inline_value.x b/src/include/eina_inline_value.x index 2d5f336..33c83f5 100644 --- a/src/include/eina_inline_value.x +++ b/src/include/eina_inline_value.x @@ -823,6 +823,29 @@ eina_value_array_pappend(Eina_Value *value, const void *ptr) return EINA_FALSE; } +static inline Eina_Bool +eina_value_array_value_get(const Eina_Value *src, unsigned int position, Eina_Value *dst) +{ + Eina_Value_Array desc; + + EINA_VALUE_TYPE_ARRAY_CHECK_RETURN_VAL(src, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(dst, EINA_FALSE); + + if (!eina_value_pget(src, &desc)) + return EINA_FALSE; + if (position >= eina_inarray_count(desc.array)) + return EINA_FALSE; + if (!eina_value_setup(dst, desc.subtype)) + return EINA_FALSE; + if (!eina_value_pset(dst, eina_inarray_nth(desc.array, position))) + { + eina_value_flush(dst); + return EINA_FALSE; + } + + return EINA_TRUE; +} + #undef EINA_VALUE_TYPE_ARRAY_CHECK_RETURN_VAL #define EINA_VALUE_TYPE_LIST_CHECK_RETURN_VAL(value, retval) \ diff --git a/src/include/eina_value.h b/src/include/eina_value.h index b043f91..341781f 100644 --- a/src/include/eina_value.h +++ b/src/include/eina_value.h @@ -1720,6 +1720,22 @@ static inline Eina_Bool eina_value_array_pappend(Eina_Value *value, const void *ptr) EINA_ARG_NONNULL(1); /** + * @brief Retrieves a value from the array as an Eina_Value copy. + * @param value source value object + * @param position index of the member + * @param dst where to return the array member + * @return #EINA_TRUE on success, #EINA_FALSE otherwise. + * + * The argument @a dst is considered uninitialized and it's setup to + * the type of the member. + * + * @since 1.2 + */ +static inline Eina_Bool eina_value_array_value_get(const Eina_Value *src, + unsigned int position, + Eina_Value *dst) EINA_ARG_NONNULL(1, 3); + +/** * @} */ diff --git a/src/tests/eina_test_value.c b/src/tests/eina_test_value.c index 08442af..94a5ab7 100644 --- a/src/tests/eina_test_value.c +++ b/src/tests/eina_test_value.c @@ -1750,7 +1750,7 @@ START_TEST(eina_value_test_array_of_struct) EINA_VALUE_STRUCT_OPERATIONS_BINSEARCH, myst_members, 4, sizeof(struct myst) }; - Eina_Value *value; + Eina_Value *value, array_item; char *str; int i; @@ -1792,6 +1792,17 @@ START_TEST(eina_value_test_array_of_struct) "]") == 0); free(str); + eina_value_array_value_get(value, 2, &array_item); + eina_value_struct_get(&array_item, "a", &i); + ck_assert_int_eq(i, 2); + eina_value_struct_get(&array_item, "b", &i); + ck_assert_int_eq(i, 20); + eina_value_struct_get(&array_item, "c", &i); + ck_assert_int_eq(i, 200); + eina_value_struct_get(&array_item, "s", &str); + ck_assert_str_eq(str, "item02"); + eina_value_flush(&array_item); + eina_value_free(value); eina_shutdown(); }