Eina: Add eina_value_array_value_get().
authorbdilly <bdilly@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 1 Mar 2012 17:15:54 +0000 (17:15 +0000)
committerbdilly <bdilly@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 1 Mar 2012 17:15:54 +0000 (17:15 +0000)
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" <kubo@profusion.mobi>

git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@68581 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/include/eina_inline_value.x
src/include/eina_value.h
src/tests/eina_test_value.c

index 2d5f336..33c83f5 100644 (file)
@@ -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)   \
index b043f91..341781f 100644 (file)
@@ -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);
+
+/**
  * @}
  */
 
index 08442af..94a5ab7 100644 (file)
@@ -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();
 }