summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcel Hollerbach <mail@marcel-hollerbach.de>2019-12-18 17:15:09 +0100
committerMarcel Hollerbach <mail@marcel-hollerbach.de>2019-12-18 21:45:42 +0100
commit9ccc66b90004a29c0790d58106857dc5e3377976 (patch)
treeadea60e8c455f3d4842565a7db59593b75cc3fc1
parent52aa2f629f9e63c22d9fa641daf30f9ff8f957ee (diff)
eina_accessor: make carray accessor work properly
a accessor is not really helpfull if it operates like the carray accessor before this commit. What it was doing is to fill the content of the "current line" into the data pointer. In a carray the "current line" is the content of its carray-member. However, accessors like for inarray array list or inlist do work completly differently. They are returning the pointer to the "current line" not the value of the current line. The only case where this worked is efl_ui_format.c which was only tested with this accessor, and this accessor also only worked with this accessor. Reviewed-by: Cedric BAIL <cedric.bail@free.fr> Differential Revision: https://phab.enlightenment.org/D10908
-rw-r--r--src/lib/eina/eina_accessor.c38
-rw-r--r--src/lib/eina/eina_accessor.h36
-rw-r--r--src/tests/eina/eina_test_accessor.c32
3 files changed, 101 insertions, 5 deletions
diff --git a/src/lib/eina/eina_accessor.c b/src/lib/eina/eina_accessor.c
index 1c5889f918..67494987a9 100644
--- a/src/lib/eina/eina_accessor.c
+++ b/src/lib/eina/eina_accessor.c
@@ -197,7 +197,6 @@ struct _Eina_Accessor_CArray_Length
197 Eina_Accessor accessor; 197 Eina_Accessor accessor;
198 198
199 void** array; 199 void** array;
200 void** current;
201 200
202 void** end; 201 void** end;
203 unsigned int step; 202 unsigned int step;
@@ -210,7 +209,16 @@ eina_carray_length_accessor_get_at(Eina_Accessor_CArray_Length *accessor, unsign
210 return EINA_FALSE; 209 return EINA_FALSE;
211 210
212 memcpy(data, (void*) accessor->array + idx*accessor->step, accessor->step); 211 memcpy(data, (void*) accessor->array + idx*accessor->step, accessor->step);
213 accessor->current += accessor->step; 212
213 return EINA_TRUE;
214}
215static Eina_Bool
216eina_carray_length_accessor_ptr_get_at(Eina_Accessor_CArray_Length *accessor, unsigned int idx, void **data)
217{
218 if (accessor->array + idx*accessor->step >= accessor->end)
219 return EINA_FALSE;
220
221 *data = (((void*)accessor->array) + idx*accessor->step);
214 222
215 return EINA_TRUE; 223 return EINA_TRUE;
216} 224}
@@ -238,8 +246,7 @@ eina_carray_length_accessor_new(void** array, unsigned int step, unsigned int le
238 EINA_MAGIC_SET(&accessor->accessor, EINA_MAGIC_ACCESSOR); 246 EINA_MAGIC_SET(&accessor->accessor, EINA_MAGIC_ACCESSOR);
239 247
240 accessor->array = array; 248 accessor->array = array;
241 accessor->current = accessor->array; 249 accessor->end = accessor->array + length * step;
242 accessor->end = accessor->current + length * step;
243 accessor->step = step; 250 accessor->step = step;
244 251
245 accessor->accessor.version = EINA_ACCESSOR_VERSION; 252 accessor->accessor.version = EINA_ACCESSOR_VERSION;
@@ -250,3 +257,26 @@ eina_carray_length_accessor_new(void** array, unsigned int step, unsigned int le
250 257
251 return &accessor->accessor; 258 return &accessor->accessor;
252} 259}
260
261EAPI Eina_Accessor *
262eina_carray_length_ptr_accessor_new(void** array, unsigned int step, unsigned int length)
263{
264 Eina_Accessor_CArray_Length *accessor;
265
266 accessor = calloc(1, sizeof (Eina_Accessor_CArray_Length));
267 if (!accessor) return NULL;
268
269 EINA_MAGIC_SET(&accessor->accessor, EINA_MAGIC_ACCESSOR);
270
271 accessor->array = array;
272 accessor->end = accessor->array + length * step;
273 accessor->step = step;
274
275 accessor->accessor.version = EINA_ACCESSOR_VERSION;
276 accessor->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_carray_length_accessor_ptr_get_at);
277 accessor->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER(
278 eina_carray_length_accessor_get_container);
279 accessor->accessor.free = FUNC_ACCESSOR_FREE(eina_carray_length_accessor_free);
280
281 return &accessor->accessor;
282}
diff --git a/src/lib/eina/eina_accessor.h b/src/lib/eina/eina_accessor.h
index 7b47d31a01..ccfccceba3 100644
--- a/src/lib/eina/eina_accessor.h
+++ b/src/lib/eina/eina_accessor.h
@@ -364,6 +364,26 @@ EAPI Eina_Bool eina_accessor_unlock(Eina_Accessor *accessor) EINA_ARG_NONNULL(1)
364 * @since 1.23 364 * @since 1.23
365 */ 365 */
366EAPI Eina_Accessor* eina_carray_length_accessor_new(void** array, unsigned int step, unsigned int length); 366EAPI Eina_Accessor* eina_carray_length_accessor_new(void** array, unsigned int step, unsigned int length);
367
368
369/**
370 * @brief Creates an Eina_Accessor that wraps a plain fixed size C array
371 *
372 * @param[in] array The array
373 * @param[in] step The size of one element in the array
374 * @param[in] length The number of elements in the array
375 * @return The accessor that will give access to the passed array
376 *
377 * You can create it like this:
378 * int array[] = {1, 2, 3, 4, 1337, 42};
379 *
380 * Eina_Accessor* accessor = eina_carray_length_accessor_new(array, sizeof(int), sizeof(array)/sizeof(int));
381 *
382 * Note: The difference to eina_carray_length_accessor_new is that this will fill the pointer to the value into the data pointer. *
383
384 * @since 1.24
385 */
386EAPI Eina_Accessor* eina_carray_length_ptr_accessor_new(void** array, unsigned int step, unsigned int length);
367/** 387/**
368 * @def EINA_C_ARRAY_ACCESSOR_NEW 388 * @def EINA_C_ARRAY_ACCESSOR_NEW
369 * @brief Creates an Eina_Accessor that wraps a plain fixed size C array 389 * @brief Creates an Eina_Accessor that wraps a plain fixed size C array
@@ -379,6 +399,22 @@ EAPI Eina_Accessor* eina_carray_length_accessor_new(void** array, unsigned int s
379 * @since 1.23 399 * @since 1.23
380 */ 400 */
381#define EINA_C_ARRAY_ACCESSOR_NEW(Array) eina_carray_length_accessor_new((void**) Array, sizeof (Array[0]), EINA_C_ARRAY_LENGTH(Array)) 401#define EINA_C_ARRAY_ACCESSOR_NEW(Array) eina_carray_length_accessor_new((void**) Array, sizeof (Array[0]), EINA_C_ARRAY_LENGTH(Array))
402/**
403 * @def EINA_C_ARRAY_ACCESSOR_NEW
404 * @brief Creates an Eina_Accessor that wraps a plain fixed size C array
405 *
406 * @param[in] array The array
407 * @return The accessor that will give access to the passed array
408 *
409 * You can create it like this:
410 * int array[] = {1, 2, 3, 4, 1337, 42};
411 *
412 * Eina_Accessor* accessor = EINA_C_ARRAY_ACCESSOR_NEW(array)
413 *
414 * @since 1.23
415 */
416#define EINA_C_ARRAY_ACCESSOR_PTR_NEW(Array) eina_carray_length_ptr_accessor_new((void**) Array, sizeof (Array[0]), EINA_C_ARRAY_LENGTH(Array))
417
382 418
383/** 419/**
384 * @} 420 * @}
diff --git a/src/tests/eina/eina_test_accessor.c b/src/tests/eina/eina_test_accessor.c
index 734d833205..62e77e06ee 100644
--- a/src/tests/eina/eina_test_accessor.c
+++ b/src/tests/eina/eina_test_accessor.c
@@ -230,7 +230,7 @@ EFL_START_TEST(eina_accessor_list_simple)
230} 230}
231EFL_END_TEST 231EFL_END_TEST
232 232
233EFL_START_TEST(eina_accessor_carray_simple) 233EFL_START_TEST(eina_accessor_carray_simple_ptr)
234{ 234{
235 Eina_Accessor *it; 235 Eina_Accessor *it;
236 int data[] = { 6, 9, 42, 1, 7, 1337, 81, 1664 }; 236 int data[] = { 6, 9, 42, 1, 7, 1337, 81, 1664 };
@@ -257,11 +257,41 @@ EFL_START_TEST(eina_accessor_carray_simple)
257 eina_accessor_free(it); 257 eina_accessor_free(it);
258} 258}
259EFL_END_TEST 259EFL_END_TEST
260
261EFL_START_TEST(eina_accessor_carray_simple)
262{
263 Eina_Accessor *it;
264 int data[] = { 6, 9, 42, 1, 7, 1337, 81, 1664 };
265 int *j, c;
266
267 it = EINA_C_ARRAY_ACCESSOR_PTR_NEW(data);
268
269 EINA_ACCESSOR_FOREACH(it, c, j)
270 {
271 ck_assert_int_eq(data[c], *j);
272 }
273
274 fail_if(eina_accessor_data_get(it, 5, (void **)&j) != EINA_TRUE);
275 fail_if(*j != 1337);
276 fail_if(eina_accessor_data_get(it, 3, (void **)&j) != EINA_TRUE);
277 fail_if(*j != 1);
278 fail_if(eina_accessor_data_get(it, 3, (void **)&j) != EINA_TRUE);
279 fail_if(*j != 1);
280 fail_if(eina_accessor_data_get(it, 1, (void **)&j) != EINA_TRUE);
281 fail_if(*j != 9);
282 fail_if(eina_accessor_data_get(it, 5, (void **)&j) != EINA_TRUE);
283 fail_if(*j != 1337);
284
285 eina_accessor_free(it);
286}
287EFL_END_TEST
288
260void 289void
261eina_test_accessor(TCase *tc) 290eina_test_accessor(TCase *tc)
262{ 291{
263 tcase_add_test(tc, eina_accessor_array_simple); 292 tcase_add_test(tc, eina_accessor_array_simple);
264 tcase_add_test(tc, eina_accessor_inlist_simple); 293 tcase_add_test(tc, eina_accessor_inlist_simple);
265 tcase_add_test(tc, eina_accessor_list_simple); 294 tcase_add_test(tc, eina_accessor_list_simple);
295 tcase_add_test(tc, eina_accessor_carray_simple_ptr);
266 tcase_add_test(tc, eina_accessor_carray_simple); 296 tcase_add_test(tc, eina_accessor_carray_simple);
267} 297}