summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-03-06 17:24:34 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-03-06 17:24:34 -0300
commitd723990955289a5ca62605262f4bc4cd0aa8d25f (patch)
tree176114705445fc0119aedb801326aff9a6f5f5e4 /src
parent793e4e0b5ad0d2a312afdda7c71257063c8c4a30 (diff)
eina: Add Eina_Iterator implementation for C-array
This iterator is convenient when you already have a C-Array and you need to pass this array to a function receiving an Eina_Iterator. int array[] = {1, 2, 3, 4}; int* array2[] = {&array[0], &array[1], &array[2], &array[3], NULL}; Eina_Iterator* iterator = eina_carray_iterator_new((void**)array);
Diffstat (limited to 'src')
-rw-r--r--src/lib/eina/eina_iterator.c54
-rw-r--r--src/lib/eina/eina_iterator.h15
2 files changed, 69 insertions, 0 deletions
diff --git a/src/lib/eina/eina_iterator.c b/src/lib/eina/eina_iterator.c
index 59dc106..5d62181 100644
--- a/src/lib/eina/eina_iterator.c
+++ b/src/lib/eina/eina_iterator.c
@@ -174,3 +174,57 @@ eina_iterator_unlock(Eina_Iterator *iterator)
174 return iterator->unlock(iterator); 174 return iterator->unlock(iterator);
175 return EINA_TRUE; 175 return EINA_TRUE;
176} 176}
177
178typedef struct _Eina_Iterator_CArray Eina_Iterator_CArray;
179
180struct _Eina_Iterator_CArray
181{
182 Eina_Iterator iterator;
183
184 void** array;
185 void** current;
186};
187
188static Eina_Bool
189eina_carray_iterator_next(Eina_Iterator_CArray *it, void **data)
190{
191 if (!it->current || !*it->current)
192 return EINA_FALSE;
193
194 *data = *it->current++;
195
196 return EINA_TRUE;
197}
198
199static void**
200eina_carray_iterator_get_container(Eina_Iterator_CArray *it)
201{
202 return it->array;
203}
204
205static void
206eina_carray_iterator_free(Eina_Iterator_CArray *it)
207{
208 free(it);
209}
210
211EAPI Eina_Iterator*
212eina_carray_iterator_new(void** array)
213{
214 Eina_Iterator_CArray *it;
215
216 it = calloc(1, sizeof (Eina_Iterator_CArray));
217 if (!it) return NULL;
218
219 EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
220
221 it->array = it->current = array;
222
223 it->iterator.version = EINA_ITERATOR_VERSION;
224 it->iterator.next = FUNC_ITERATOR_NEXT(eina_carray_iterator_next);
225 it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(
226 eina_carray_iterator_get_container);
227 it->iterator.free = FUNC_ITERATOR_FREE(eina_carray_iterator_free);
228
229 return &it->iterator;
230}
diff --git a/src/lib/eina/eina_iterator.h b/src/lib/eina/eina_iterator.h
index 980dbfc..b379d2e 100644
--- a/src/lib/eina/eina_iterator.h
+++ b/src/lib/eina/eina_iterator.h
@@ -285,6 +285,21 @@ EAPI Eina_Bool eina_iterator_lock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1);
285EAPI Eina_Bool eina_iterator_unlock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1); 285EAPI Eina_Bool eina_iterator_unlock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1);
286 286
287/** 287/**
288 * @brief Creates an Eina_Iterator that iterates through a
289 * NUL-terminated C array.
290 *
291 * @param array The NUL-terminated array
292 *
293 * You can create it like this:
294 * int array[] = {1, 2, 3, 4};
295 * int* array2[] = {&array[0], &array[1], &array[2], &array[3], NULL};
296 *
297 * Eina_Iterator* iterator = eina_carray_iterator_new((void**)array);
298 *
299 */
300EAPI Eina_Iterator* eina_carray_iterator_new(void** array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
301
302/**
288 * @def EINA_ITERATOR_FOREACH 303 * @def EINA_ITERATOR_FOREACH
289 * @brief Macro to iterate over all elements easily. 304 * @brief Macro to iterate over all elements easily.
290 * 305 *