discomfitor pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=77ba1c2761db5964dd57631e0a35db2b764180d9

commit 77ba1c2761db5964dd57631e0a35db2b764180d9
Author: Mike Blumenkrantz <zm...@osg.samsung.com>
Date:   Tue Sep 23 14:37:48 2014 -0400

    +eina_value_util api
    
    merged from maelstrom/azy. commonly used functionality for eina_value with 
_util_ namespace to make them easier to find in the value header nightmare
    
    @feature
---
 src/Makefile_Eina.am                  |   3 +
 src/lib/eina/eina_inline_value_util.x | 235 ++++++++++++++++++++++++++++++++++
 src/lib/eina/eina_value_util.c        |  80 ++++++++++++
 src/lib/eina/eina_value_util.h        |  44 +++++++
 4 files changed, 362 insertions(+)

diff --git a/src/Makefile_Eina.am b/src/Makefile_Eina.am
index fe12abb..1db4df2 100644
--- a/src/Makefile_Eina.am
+++ b/src/Makefile_Eina.am
@@ -77,6 +77,8 @@ lib/eina/eina_mmap.h \
 lib/eina/eina_xattr.h \
 lib/eina/eina_value.h \
 lib/eina/eina_inline_value.x \
+lib/eina/eina_value_util.h \
+lib/eina/eina_inline_value_util.x \
 lib/eina/eina_inline_lock_barrier.x \
 lib/eina/eina_tmpstr.h \
 lib/eina/eina_alloca.h \
@@ -142,6 +144,7 @@ lib/eina/eina_unicode.c \
 lib/eina/eina_ustrbuf.c \
 lib/eina/eina_ustringshare.c \
 lib/eina/eina_value.c \
+lib/eina/eina_value_util.c \
 lib/eina/eina_xattr.c \
 lib/eina/eina_share_common.h \
 lib/eina/eina_private.h \
diff --git a/src/lib/eina/eina_inline_value_util.x 
b/src/lib/eina/eina_inline_value_util.x
new file mode 100644
index 0000000..e1ddf50
--- /dev/null
+++ b/src/lib/eina/eina_inline_value_util.x
@@ -0,0 +1,235 @@
+/*
+ * Copyright (C) 2013, 2014 Mike Blumenkrantz
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef EINA_INLINE_VALUE_UTIL_X_
+#define EINA_INLINE_VALUE_UTIL_X_
+
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include <math.h>
+#include <time.h>
+
+
+/**
+ * @brief Get size of #Eina_Value_Type based on C type
+ * @param type The type to get the size of
+ * @return The size of the type
+ * @since 1.12
+ */
+static inline size_t
+eina_value_util_type_size(const Eina_Value_Type *type)
+{
+   if (type == EINA_VALUE_TYPE_INT) return sizeof(int32_t);
+   if (type == EINA_VALUE_TYPE_UCHAR) return sizeof(unsigned char);
+   if ((type == EINA_VALUE_TYPE_STRING) || (type == 
EINA_VALUE_TYPE_STRINGSHARE)) return sizeof(char*);
+   if (type == EINA_VALUE_TYPE_TIMESTAMP) return sizeof(time_t);
+   if (type == EINA_VALUE_TYPE_ARRAY) return sizeof(Eina_Value_Array);
+   if (type == EINA_VALUE_TYPE_DOUBLE) return sizeof(double);
+   if (type == EINA_VALUE_TYPE_STRUCT) return sizeof(Eina_Value_Struct);
+   return 0;
+}
+
+/**
+ * @brief Get padding of #Eina_Value_Type based on C type and base padding
+ * @param type The type to get the offset of
+ * @param base The existing base size
+ * @return The offset of the type
+ * @since 1.12
+ */
+static inline unsigned int
+eina_value_util_type_offset(const Eina_Value_Type *type, unsigned int base)
+{
+   unsigned size, padding;
+   size = eina_value_util_type_size(type);
+   if (!(base % size))
+     return base;
+   padding = abs(base - size);
+   return base + padding;
+}
+
+/**
+ * @brief Create a new #Eina_Value containing the passed parameter
+ * @param i The value to use
+ * @return The #Eina_Value
+ * @since 1.12
+ */
+static inline Eina_Value *
+eina_value_util_int_new(int i)
+{
+   Eina_Value *v;
+
+   v = eina_value_new(EINA_VALUE_TYPE_INT);
+   if (v) eina_value_set(v, i);
+   return v;
+}
+
+/**
+ * @brief Create a new #Eina_Value containing the passed parameter
+ * @param d The value to use
+ * @return The #Eina_Value
+ * @since 1.12
+ */
+static inline Eina_Value *
+eina_value_util_double_new(double d)
+{
+   Eina_Value *v;
+
+   v = eina_value_new(EINA_VALUE_TYPE_DOUBLE);
+   if (v) eina_value_set(v, d);
+   return v;
+}
+
+/**
+ * @brief Create a new #Eina_Value containing the passed parameter
+ * @param b The value to use
+ * @return The #Eina_Value
+ * @since 1.12
+ */
+static inline Eina_Value *
+eina_value_util_bool_new(Eina_Bool b)
+{
+   Eina_Value *v;
+
+   v = eina_value_new(EINA_VALUE_TYPE_UCHAR);
+   if (v) eina_value_set(v, b);
+   return v;
+}
+
+/**
+ * @brief Create a new #Eina_Value containing the passed parameter
+ * @param str The value to use
+ * @return The #Eina_Value
+ * @since 1.12
+ */
+static inline Eina_Value *
+eina_value_util_string_new(const char *str)
+{
+   Eina_Value *v;
+
+   v = eina_value_new(EINA_VALUE_TYPE_STRING);
+   if (v) eina_value_set(v, str);
+   return v;
+}
+
+/**
+ * @brief Create a new #Eina_Value containing the passed parameter
+ * @param str The value to use
+ * @return The #Eina_Value
+ * @since 1.12
+ */
+static inline Eina_Value *
+eina_value_util_stringshare_new(const char *str)
+{
+   Eina_Value *v;
+
+   v = eina_value_new(EINA_VALUE_TYPE_STRINGSHARE);
+   if (v) eina_value_set(v, str);
+   return v;
+}
+
+/**
+ * @brief Create a new #Eina_Value containing the passed parameter
+ * @param t The value to use
+ * @return The #Eina_Value
+ * @since 1.12
+ */
+static inline Eina_Value *
+eina_value_util_time_new(time_t t)
+{
+   Eina_Value *v;
+
+   v = eina_value_new(EINA_VALUE_TYPE_TIMESTAMP);
+   if (v) eina_value_set(v, t);
+   return v;
+}
+
+/**
+ * @brief Create a new #Eina_Value containing the passed parameter
+ * @param timestr The value to use
+ * @return The #Eina_Value
+ * @since 1.12
+ */
+static inline Eina_Value *
+eina_value_util_timestamp_new(const char *timestr)
+{
+   Eina_Value *v;
+   struct tm tm;
+   time_t t;
+
+   if (!strptime(timestr, "%Y%m%dT%H:%M:%S", &tm)) return NULL;
+   t = mktime(&tm);
+   v = eina_value_new(EINA_VALUE_TYPE_TIMESTAMP);
+   if (v) eina_value_set(v, t);
+   return v;
+}
+
+/**
+ * @brief Create a new #Eina_Value containing the passed parameter
+ * @param val The value to use
+ * @return The #Eina_Value
+ * @since 1.12
+ */
+static inline Eina_Value *
+eina_value_util_dup(const Eina_Value *val)
+{
+   Eina_Value *v;
+
+   v = eina_value_new(eina_value_type_get(val));
+   EINA_SAFETY_ON_NULL_RETURN_VAL(v, NULL);
+   eina_value_copy(val, v);
+   return v;
+}
+
+/**
+ * @brief Copy the stringshare in the passed #Eina_Value
+ * @param val The value to copy
+ * @param str The pointer to copy the stringshare to
+ * @return @c EINA_TRUE on success
+ * @since 1.12
+ */
+static inline Eina_Bool
+eina_value_util_stringshare_copy(const Eina_Value *val, Eina_Stringshare **str)
+{
+   EINA_SAFETY_ON_NULL_RETURN_VAL(val, EINA_FALSE);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
+   if (!eina_value_get(val, str)) return EINA_FALSE;
+   eina_stringshare_ref(*str);
+   return EINA_TRUE;
+}
+
+/**
+ * @brief Copy the string in the passed #Eina_Value
+ * @param val The value to copy
+ * @param str The pointer to copy the string to
+ * @return @c EINA_TRUE on success
+ * @since 1.12
+ */
+static inline Eina_Bool
+eina_value_util_string_copy(const Eina_Value *val, char **str)
+{
+   char *s;
+   EINA_SAFETY_ON_NULL_RETURN_VAL(val, EINA_FALSE);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
+   if (!eina_value_get(val, &s)) return EINA_FALSE;
+   *str = s ? strdup(s) : NULL;
+   return EINA_TRUE;
+}
+
+#endif
diff --git a/src/lib/eina/eina_value_util.c b/src/lib/eina/eina_value_util.c
new file mode 100644
index 0000000..261eba2
--- /dev/null
+++ b/src/lib/eina/eina_value_util.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2013, 2014 Mike Blumenkrantz
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "eina_safety_checks.h"
+#include "eina_value.h"
+#include "eina_stringshare.h"
+
+
+typedef struct _Eina_Value_Util_Struct_Desc
+{
+   Eina_Value_Struct_Desc base;
+   int refcount;
+} Eina_Value_Util_Struct_Desc;
+
+static void *
+_ops_malloc(const Eina_Value_Struct_Operations *ops EINA_UNUSED, const 
Eina_Value_Struct_Desc *desc)
+{
+   Eina_Value_Util_Struct_Desc *edesc = (Eina_Value_Util_Struct_Desc*)desc;
+   edesc->refcount++;
+   //DBG("%p refcount=%d", edesc, edesc->refcount);
+   return malloc(desc->size);
+}
+
+static void
+_ops_free(const Eina_Value_Struct_Operations *ops EINA_UNUSED, const 
Eina_Value_Struct_Desc *desc, void *memory)
+{
+   Eina_Value_Util_Struct_Desc *edesc = (Eina_Value_Util_Struct_Desc*) desc;
+   edesc->refcount--;
+   free(memory);
+   //DBG("%p refcount=%d", edesc, edesc->refcount);
+   if (edesc->refcount <= 0)
+     {
+        unsigned i;
+        for (i = 0; i < edesc->base.member_count; i++)
+          eina_stringshare_del((char *)edesc->base.members[i].name);
+        free((Eina_Value_Struct_Member *)edesc->base.members);
+        free(edesc);
+     }
+}
+
+static Eina_Value_Struct_Operations operations =
+{
+   EINA_VALUE_STRUCT_OPERATIONS_VERSION,
+   _ops_malloc,
+   _ops_free,
+   NULL,
+   NULL,
+   NULL
+};
+
+EAPI Eina_Value_Struct_Desc *
+eina_value_util_struct_desc_new(void)
+{
+   Eina_Value_Util_Struct_Desc *st_desc;
+
+   st_desc = calloc(1, sizeof(Eina_Value_Util_Struct_Desc));
+   EINA_SAFETY_ON_NULL_RETURN_VAL(st_desc, NULL);
+   st_desc->base.version = EINA_VALUE_STRUCT_DESC_VERSION;
+   st_desc->base.ops = &operations;
+   return (Eina_Value_Struct_Desc*)st_desc;
+}
diff --git a/src/lib/eina/eina_value_util.h b/src/lib/eina/eina_value_util.h
new file mode 100644
index 0000000..b4022a5
--- /dev/null
+++ b/src/lib/eina/eina_value_util.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2013, 2014 Mike Blumenkrantz
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef EINA_INLINE_VALUE_UTIL_H_
+#define EINA_INLINE_VALUE_UTIL_H_
+
+#include "eina_value.h"
+
+/**
+ * @addtogroup Eina_Value
+ *
+ * @{
+ */
+
+/**
+ * @brief Create a basic #Eina_Value struct desc with refcounting
+ * @return The #Eina_Value_Struct_Desc on success, @c NULL on failure
+ * @since 1.12
+ */
+EAPI Eina_Value_Struct_Desc *eina_value_util_struct_desc_new(void);
+
+#include "eina_inline_value_util.x"
+
+/**
+ * @}
+ */
+
+#endif

-- 


Reply via email to