summaryrefslogtreecommitdiff
path: root/src/lib/eina
diff options
context:
space:
mode:
authorMarcel Hollerbach <mail@marcel-hollerbach.de>2019-07-01 19:18:11 +0200
committerXavi Artigas <xavierartigas@yahoo.es>2019-07-03 07:41:05 +0200
commit223a433be8ab02267c97069a271cf89c65856c42 (patch)
treee7c7cc3f7bf00da9d793e06794b3e6aca6d897ba /src/lib/eina
parent132520bc7b96ad461a62316f97085e9d03c68dcd (diff)
eina: add accessors for c arrays
This adds a new API which can be used to get access to a carray via the eina_accessor API.
Diffstat (limited to 'src/lib/eina')
-rw-r--r--src/lib/eina/eina_accessor.c61
-rw-r--r--src/lib/eina/eina_accessor.h32
2 files changed, 93 insertions, 0 deletions
diff --git a/src/lib/eina/eina_accessor.c b/src/lib/eina/eina_accessor.c
index 9506b86..1c5889f 100644
--- a/src/lib/eina/eina_accessor.c
+++ b/src/lib/eina/eina_accessor.c
@@ -189,3 +189,64 @@ eina_accessor_unlock(Eina_Accessor *accessor)
189 return accessor->unlock(accessor); 189 return accessor->unlock(accessor);
190 return EINA_TRUE; 190 return EINA_TRUE;
191} 191}
192
193typedef struct _Eina_Accessor_CArray_Length Eina_Accessor_CArray_Length;
194
195struct _Eina_Accessor_CArray_Length
196{
197 Eina_Accessor accessor;
198
199 void** array;
200 void** current;
201
202 void** end;
203 unsigned int step;
204};
205
206static Eina_Bool
207eina_carray_length_accessor_get_at(Eina_Accessor_CArray_Length *accessor, unsigned int idx, void **data)
208{
209 if (accessor->array + idx*accessor->step >= accessor->end)
210 return EINA_FALSE;
211
212 memcpy(data, (void*) accessor->array + idx*accessor->step, accessor->step);
213 accessor->current += accessor->step;
214
215 return EINA_TRUE;
216}
217
218static void**
219eina_carray_length_accessor_get_container(Eina_Accessor_CArray_Length *accessor)
220{
221 return accessor->array;
222}
223
224static void
225eina_carray_length_accessor_free(Eina_Accessor_CArray_Length *accessor)
226{
227 free(accessor);
228}
229
230EAPI Eina_Accessor *
231eina_carray_length_accessor_new(void** array, unsigned int step, unsigned int length)
232{
233 Eina_Accessor_CArray_Length *accessor;
234
235 accessor = calloc(1, sizeof (Eina_Accessor_CArray_Length));
236 if (!accessor) return NULL;
237
238 EINA_MAGIC_SET(&accessor->accessor, EINA_MAGIC_ACCESSOR);
239
240 accessor->array = array;
241 accessor->current = accessor->array;
242 accessor->end = accessor->current + length * step;
243 accessor->step = step;
244
245 accessor->accessor.version = EINA_ACCESSOR_VERSION;
246 accessor->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_carray_length_accessor_get_at);
247 accessor->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER(
248 eina_carray_length_accessor_get_container);
249 accessor->accessor.free = FUNC_ACCESSOR_FREE(eina_carray_length_accessor_free);
250
251 return &accessor->accessor;
252}
diff --git a/src/lib/eina/eina_accessor.h b/src/lib/eina/eina_accessor.h
index 3586d3c..7b47d31 100644
--- a/src/lib/eina/eina_accessor.h
+++ b/src/lib/eina/eina_accessor.h
@@ -349,6 +349,38 @@ EAPI Eina_Bool eina_accessor_unlock(Eina_Accessor *accessor) EINA_ARG_NONNULL(1)
349 (counter)++) 349 (counter)++)
350 350
351/** 351/**
352 * @brief Creates an Eina_Accessor that wraps a plain fixed size C array
353 *
354 * @param[in] array The array
355 * @param[in] step The size of one element in the array
356 * @param[in] length The number of elements in the array
357 * @return The accessor that will give access to the passed array
358 *
359 * You can create it like this:
360 * int array[] = {1, 2, 3, 4, 1337, 42};
361 *
362 * Eina_Accessor* accessor = eina_carray_length_accessor_new(array, sizeof(int), sizeof(array)/sizeof(int));
363 *
364 * @since 1.23
365 */
366EAPI Eina_Accessor* eina_carray_length_accessor_new(void** array, unsigned int step, unsigned int length);
367/**
368 * @def EINA_C_ARRAY_ACCESSOR_NEW
369 * @brief Creates an Eina_Accessor that wraps a plain fixed size C array
370 *
371 * @param[in] array The array
372 * @return The accessor that will give access to the passed array
373 *
374 * You can create it like this:
375 * int array[] = {1, 2, 3, 4, 1337, 42};
376 *
377 * Eina_Accessor* accessor = EINA_C_ARRAY_ACCESSOR_NEW(array)
378 *
379 * @since 1.23
380 */
381#define EINA_C_ARRAY_ACCESSOR_NEW(Array) eina_carray_length_accessor_new((void**) Array, sizeof (Array[0]), EINA_C_ARRAY_LENGTH(Array))
382
383/**
352 * @} 384 * @}
353 */ 385 */
354 386