summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Blumenkrantz <zmike@osg.samsung.com>2014-09-23 14:37:48 -0400
committerMike Blumenkrantz <zmike@osg.samsung.com>2014-09-23 14:37:48 -0400
commit77ba1c2761db5964dd57631e0a35db2b764180d9 (patch)
tree4dd82d7ae5ca753d00367a740a20c52eeecaf2b6
parent5fbe3f1f70ff4ce16b2a6b13dcea921b500836eb (diff)
+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
-rw-r--r--src/Makefile_Eina.am3
-rw-r--r--src/lib/eina/eina_inline_value_util.x235
-rw-r--r--src/lib/eina/eina_value_util.c80
-rw-r--r--src/lib/eina/eina_value_util.h44
4 files changed, 362 insertions, 0 deletions
diff --git a/src/Makefile_Eina.am b/src/Makefile_Eina.am
index fe12abb8f0..1db4df2564 100644
--- a/src/Makefile_Eina.am
+++ b/src/Makefile_Eina.am
@@ -77,6 +77,8 @@ lib/eina/eina_mmap.h \
77lib/eina/eina_xattr.h \ 77lib/eina/eina_xattr.h \
78lib/eina/eina_value.h \ 78lib/eina/eina_value.h \
79lib/eina/eina_inline_value.x \ 79lib/eina/eina_inline_value.x \
80lib/eina/eina_value_util.h \
81lib/eina/eina_inline_value_util.x \
80lib/eina/eina_inline_lock_barrier.x \ 82lib/eina/eina_inline_lock_barrier.x \
81lib/eina/eina_tmpstr.h \ 83lib/eina/eina_tmpstr.h \
82lib/eina/eina_alloca.h \ 84lib/eina/eina_alloca.h \
@@ -142,6 +144,7 @@ lib/eina/eina_unicode.c \
142lib/eina/eina_ustrbuf.c \ 144lib/eina/eina_ustrbuf.c \
143lib/eina/eina_ustringshare.c \ 145lib/eina/eina_ustringshare.c \
144lib/eina/eina_value.c \ 146lib/eina/eina_value.c \
147lib/eina/eina_value_util.c \
145lib/eina/eina_xattr.c \ 148lib/eina/eina_xattr.c \
146lib/eina/eina_share_common.h \ 149lib/eina/eina_share_common.h \
147lib/eina/eina_private.h \ 150lib/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 0000000000..e1ddf50ed7
--- /dev/null
+++ b/src/lib/eina/eina_inline_value_util.x
@@ -0,0 +1,235 @@
1/*
2 * Copyright (C) 2013, 2014 Mike Blumenkrantz
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20#ifndef EINA_INLINE_VALUE_UTIL_X_
21#define EINA_INLINE_VALUE_UTIL_X_
22
23#include <stdlib.h>
24#include <string.h>
25#include <inttypes.h>
26#include <math.h>
27#include <time.h>
28
29
30/**
31 * @brief Get size of #Eina_Value_Type based on C type
32 * @param type The type to get the size of
33 * @return The size of the type
34 * @since 1.12
35 */
36static inline size_t
37eina_value_util_type_size(const Eina_Value_Type *type)
38{
39 if (type == EINA_VALUE_TYPE_INT) return sizeof(int32_t);
40 if (type == EINA_VALUE_TYPE_UCHAR) return sizeof(unsigned char);
41 if ((type == EINA_VALUE_TYPE_STRING) || (type == EINA_VALUE_TYPE_STRINGSHARE)) return sizeof(char*);
42 if (type == EINA_VALUE_TYPE_TIMESTAMP) return sizeof(time_t);
43 if (type == EINA_VALUE_TYPE_ARRAY) return sizeof(Eina_Value_Array);
44 if (type == EINA_VALUE_TYPE_DOUBLE) return sizeof(double);
45 if (type == EINA_VALUE_TYPE_STRUCT) return sizeof(Eina_Value_Struct);
46 return 0;
47}
48
49/**
50 * @brief Get padding of #Eina_Value_Type based on C type and base padding
51 * @param type The type to get the offset of
52 * @param base The existing base size
53 * @return The offset of the type
54 * @since 1.12
55 */
56static inline unsigned int
57eina_value_util_type_offset(const Eina_Value_Type *type, unsigned int base)
58{
59 unsigned size, padding;
60 size = eina_value_util_type_size(type);
61 if (!(base % size))
62 return base;
63 padding = abs(base - size);
64 return base + padding;
65}
66
67/**
68 * @brief Create a new #Eina_Value containing the passed parameter
69 * @param i The value to use
70 * @return The #Eina_Value
71 * @since 1.12
72 */
73static inline Eina_Value *
74eina_value_util_int_new(int i)
75{
76 Eina_Value *v;
77
78 v = eina_value_new(EINA_VALUE_TYPE_INT);
79 if (v) eina_value_set(v, i);
80 return v;
81}
82
83/**
84 * @brief Create a new #Eina_Value containing the passed parameter
85 * @param d The value to use
86 * @return The #Eina_Value
87 * @since 1.12
88 */
89static inline Eina_Value *
90eina_value_util_double_new(double d)
91{
92 Eina_Value *v;
93
94 v = eina_value_new(EINA_VALUE_TYPE_DOUBLE);
95 if (v) eina_value_set(v, d);
96 return v;
97}
98
99/**
100 * @brief Create a new #Eina_Value containing the passed parameter
101 * @param b The value to use
102 * @return The #Eina_Value
103 * @since 1.12
104 */
105static inline Eina_Value *
106eina_value_util_bool_new(Eina_Bool b)
107{
108 Eina_Value *v;
109
110 v = eina_value_new(EINA_VALUE_TYPE_UCHAR);
111 if (v) eina_value_set(v, b);
112 return v;
113}
114
115/**
116 * @brief Create a new #Eina_Value containing the passed parameter
117 * @param str The value to use
118 * @return The #Eina_Value
119 * @since 1.12
120 */
121static inline Eina_Value *
122eina_value_util_string_new(const char *str)
123{
124 Eina_Value *v;
125
126 v = eina_value_new(EINA_VALUE_TYPE_STRING);
127 if (v) eina_value_set(v, str);
128 return v;
129}
130
131/**
132 * @brief Create a new #Eina_Value containing the passed parameter
133 * @param str The value to use
134 * @return The #Eina_Value
135 * @since 1.12
136 */
137static inline Eina_Value *
138eina_value_util_stringshare_new(const char *str)
139{
140 Eina_Value *v;
141
142 v = eina_value_new(EINA_VALUE_TYPE_STRINGSHARE);
143 if (v) eina_value_set(v, str);
144 return v;
145}
146
147/**
148 * @brief Create a new #Eina_Value containing the passed parameter
149 * @param t The value to use
150 * @return The #Eina_Value
151 * @since 1.12
152 */
153static inline Eina_Value *
154eina_value_util_time_new(time_t t)
155{
156 Eina_Value *v;
157
158 v = eina_value_new(EINA_VALUE_TYPE_TIMESTAMP);
159 if (v) eina_value_set(v, t);
160 return v;
161}
162
163/**
164 * @brief Create a new #Eina_Value containing the passed parameter
165 * @param timestr The value to use
166 * @return The #Eina_Value
167 * @since 1.12
168 */
169static inline Eina_Value *
170eina_value_util_timestamp_new(const char *timestr)
171{
172 Eina_Value *v;
173 struct tm tm;
174 time_t t;
175
176 if (!strptime(timestr, "%Y%m%dT%H:%M:%S", &tm)) return NULL;
177 t = mktime(&tm);
178 v = eina_value_new(EINA_VALUE_TYPE_TIMESTAMP);
179 if (v) eina_value_set(v, t);
180 return v;
181}
182
183/**
184 * @brief Create a new #Eina_Value containing the passed parameter
185 * @param val The value to use
186 * @return The #Eina_Value
187 * @since 1.12
188 */
189static inline Eina_Value *
190eina_value_util_dup(const Eina_Value *val)
191{
192 Eina_Value *v;
193
194 v = eina_value_new(eina_value_type_get(val));
195 EINA_SAFETY_ON_NULL_RETURN_VAL(v, NULL);
196 eina_value_copy(val, v);
197 return v;
198}
199
200/**
201 * @brief Copy the stringshare in the passed #Eina_Value
202 * @param val The value to copy
203 * @param str The pointer to copy the stringshare to
204 * @return @c EINA_TRUE on success
205 * @since 1.12
206 */
207static inline Eina_Bool
208eina_value_util_stringshare_copy(const Eina_Value *val, Eina_Stringshare **str)
209{
210 EINA_SAFETY_ON_NULL_RETURN_VAL(val, EINA_FALSE);
211 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
212 if (!eina_value_get(val, str)) return EINA_FALSE;
213 eina_stringshare_ref(*str);
214 return EINA_TRUE;
215}
216
217/**
218 * @brief Copy the string in the passed #Eina_Value
219 * @param val The value to copy
220 * @param str The pointer to copy the string to
221 * @return @c EINA_TRUE on success
222 * @since 1.12
223 */
224static inline Eina_Bool
225eina_value_util_string_copy(const Eina_Value *val, char **str)
226{
227 char *s;
228 EINA_SAFETY_ON_NULL_RETURN_VAL(val, EINA_FALSE);
229 EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
230 if (!eina_value_get(val, &s)) return EINA_FALSE;
231 *str = s ? strdup(s) : NULL;
232 return EINA_TRUE;
233}
234
235#endif
diff --git a/src/lib/eina/eina_value_util.c b/src/lib/eina/eina_value_util.c
new file mode 100644
index 0000000000..261eba2dc2
--- /dev/null
+++ b/src/lib/eina/eina_value_util.c
@@ -0,0 +1,80 @@
1/*
2 * Copyright (C) 2013, 2014 Mike Blumenkrantz
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include "eina_safety_checks.h"
24#include "eina_value.h"
25#include "eina_stringshare.h"
26
27
28typedef struct _Eina_Value_Util_Struct_Desc
29{
30 Eina_Value_Struct_Desc base;
31 int refcount;
32} Eina_Value_Util_Struct_Desc;
33
34static void *
35_ops_malloc(const Eina_Value_Struct_Operations *ops EINA_UNUSED, const Eina_Value_Struct_Desc *desc)
36{
37 Eina_Value_Util_Struct_Desc *edesc = (Eina_Value_Util_Struct_Desc*)desc;
38 edesc->refcount++;
39 //DBG("%p refcount=%d", edesc, edesc->refcount);
40 return malloc(desc->size);
41}
42
43static void
44_ops_free(const Eina_Value_Struct_Operations *ops EINA_UNUSED, const Eina_Value_Struct_Desc *desc, void *memory)
45{
46 Eina_Value_Util_Struct_Desc *edesc = (Eina_Value_Util_Struct_Desc*) desc;
47 edesc->refcount--;
48 free(memory);
49 //DBG("%p refcount=%d", edesc, edesc->refcount);
50 if (edesc->refcount <= 0)
51 {
52 unsigned i;
53 for (i = 0; i < edesc->base.member_count; i++)
54 eina_stringshare_del((char *)edesc->base.members[i].name);
55 free((Eina_Value_Struct_Member *)edesc->base.members);
56 free(edesc);
57 }
58}
59
60static Eina_Value_Struct_Operations operations =
61{
62 EINA_VALUE_STRUCT_OPERATIONS_VERSION,
63 _ops_malloc,
64 _ops_free,
65 NULL,
66 NULL,
67 NULL
68};
69
70EAPI Eina_Value_Struct_Desc *
71eina_value_util_struct_desc_new(void)
72{
73 Eina_Value_Util_Struct_Desc *st_desc;
74
75 st_desc = calloc(1, sizeof(Eina_Value_Util_Struct_Desc));
76 EINA_SAFETY_ON_NULL_RETURN_VAL(st_desc, NULL);
77 st_desc->base.version = EINA_VALUE_STRUCT_DESC_VERSION;
78 st_desc->base.ops = &operations;
79 return (Eina_Value_Struct_Desc*)st_desc;
80}
diff --git a/src/lib/eina/eina_value_util.h b/src/lib/eina/eina_value_util.h
new file mode 100644
index 0000000000..b4022a5a03
--- /dev/null
+++ b/src/lib/eina/eina_value_util.h
@@ -0,0 +1,44 @@
1/*
2 * Copyright (C) 2013, 2014 Mike Blumenkrantz
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20#ifndef EINA_INLINE_VALUE_UTIL_H_
21#define EINA_INLINE_VALUE_UTIL_H_
22
23#include "eina_value.h"
24
25/**
26 * @addtogroup Eina_Value
27 *
28 * @{
29 */
30
31/**
32 * @brief Create a basic #Eina_Value struct desc with refcounting
33 * @return The #Eina_Value_Struct_Desc on success, @c NULL on failure
34 * @since 1.12
35 */
36EAPI Eina_Value_Struct_Desc *eina_value_util_struct_desc_new(void);
37
38#include "eina_inline_value_util.x"
39
40/**
41 * @}
42 */
43
44#endif