1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
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 #ifndef EINA_ITERATOR_H__
20 #define EINA_ITERATOR_H__
21 
22 #include "eina_config.h"
23 
24 #include "eina_types.h"
25 #include "eina_magic.h"
26 
27 /**
28  * @page eina_iterator_example_page Eina_Iterator usage
29  * @dontinclude eina_iterator_01.c
30  *
31  * As always when using eina we need to include it:
32  * @skip #include
33  * @until Eina.h
34  *
35  * Here we a declare an unimpressive @ref Eina_Each_Cb "function" that prints
36  * some data:
37  * @until }
38  * @note Returning EINA_TRUE is important so we don't stop iterating over the
39  * container.
40  *
41  * And here's a more interesting function, it uses an iterator to print the
42  * contents of a container. What's interesting about it is that it doesn't care
43  * the type of container, it works for anything that can provide an iterator:
44  * @until }
45  *
46  * And on to our main function were we declare some variables and initialize
47  * eina, nothing too special:
48  * @until eina_init
49  *
50  * Next we populate both an array and a list with our strings, for more details
51  * see @ref eina_list_01_example_page and @ref eina_array_01_example_page :
52  * @until }
53  *
54  * And now we create an array and because the first element of the container
55  * doesn't interest us we skip it:
56  * @until iterator_next
57  *
58  * Having our iterator now pointing to interesting data we go ahead and print:
59  * @until print_eina_container
60  *
61  * As always once data with a structure we free it, but just because we can we
62  * do it by asking the iterator for it's container, and then of course free the
63  * iterator itself:
64  * @until eina_iterator_free
65  *
66  * But so far you're not impressed in @ref eina_array_01_example_page an array is
67  * also printed, so now we go to the cool stuff and use an iterator to do same
68  * stuff to a list:
69  * @until eina_iterator_free
70  * @note The only significant difference to the block above is in the
71  * function used to create the iterator.
72  *
73  * And now we free the list and shut eina down:
74  * @until }
75  */
76 
77 /**
78  * @page eina_iterator_01_c Eina_Iterator usage
79  * @page eina_iterator_01_c Eina_Iterator usage
80  *
81  * @include eina_iterator_01.c
82  * @example eina_iterator_01.c
83  */
84 
85 /**
86  * @addtogroup Eina_Iterator_Group Iterator Functions
87  *
88  * @brief These functions manage iterators on containers.
89  *
90  * These functions allow accessing elements of a container in a
91  * generic way, without knowing which container is used (a bit like
92  * iterators in the C++ STL). Iterators only allow sequential access
93  * (that is, from one element to the next one). For random access, see
94  * @ref Eina_Accessor_Group.
95  *
96  * Getting an iterator to access elements of a given container is done through
97  * the functions of that particular container. There is no function to create
98  * a generic iterator as iterators absolutely depend on the container. This
99  * means you won't find an iterator creation function here, those can be found with
100  * the documentation of the container type you're using. Though created with
101  * container specific functions iterators are always deleted with the same
102  * function: eina_iterator_free().
103  *
104  * To get the data and iterate, use eina_iterator_next(). To call a function on
105  * all the elements of a container, use eina_iterator_foreach().
106  *
107  * Here an @ref eina_iterator_example_page "example"
108  */
109 
110 /**
111  * @addtogroup Eina_Content_Access_Group Content Access
112  *
113  * @{
114  */
115 
116 /**
117  * @defgroup Eina_Iterator_Group Iterator Functions
118  *
119  * @{
120  */
121 
122 /**
123  * @typedef Eina_Iterator
124  * Abstract type for iterators.
125  */
126 typedef struct _Eina_Iterator Eina_Iterator;
127 
128 /**
129  * @typedef Eina_Iterator_Next_Callback
130  * Type for a callback that returns the next element in a container.
131  */
132 typedef Eina_Bool           (*Eina_Iterator_Next_Callback)(Eina_Iterator *it, void **data);
133 
134 /**
135  * @typedef Eina_Iterator_Get_Container_Callback
136  * Type for a callback that returns the container.
137  */
138 typedef void               *(*Eina_Iterator_Get_Container_Callback)(Eina_Iterator *it);
139 
140 /**
141  * @typedef Eina_Iterator_Free_Callback
142  * Type for a callback that frees the container.
143  */
144 typedef void                (*Eina_Iterator_Free_Callback)(Eina_Iterator *it);
145 
146 /**
147  * @typedef Eina_Iterator_Lock_Callback
148  * Type for a callback that lock the container.
149  */
150 typedef Eina_Bool           (*Eina_Iterator_Lock_Callback)(Eina_Iterator *it);
151 
152 /**
153  * @struct _Eina_Iterator
154  * structure of an iterator
155  *
156  * If creating an iterator remember to set the type using @ref EINA_MAGIC_SET.
157  */
158 struct _Eina_Iterator
159 {
160 #define EINA_ITERATOR_VERSION 1
161    int                                  version; /**< Version of the Iterator API. */
162 
163    Eina_Iterator_Next_Callback          next          EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT; /**< Callback called when a next element is requested. */
164    Eina_Iterator_Get_Container_Callback get_container EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; /**< Callback called when the container is requested. */
165    Eina_Iterator_Free_Callback          free          EINA_ARG_NONNULL(1); /**< Callback called when the container is freed. */
166 
167    Eina_Iterator_Lock_Callback          lock          EINA_WARN_UNUSED_RESULT; /**< Callback called when the container is locked. */
168    Eina_Iterator_Lock_Callback          unlock        EINA_WARN_UNUSED_RESULT; /**< Callback called when the container is unlocked. */
169 
170 #define EINA_MAGIC_ITERATOR 0x98761233
171    EINA_MAGIC
172 };
173 
174 /**
175  * @def FUNC_ITERATOR_NEXT(Function)
176  * Helper macro to cast @p Function to a Eina_Iterator_Next_Callback.
177  */
178 #define FUNC_ITERATOR_NEXT(Function)          ((Eina_Iterator_Next_Callback)Function)
179 
180 /**
181  * @def FUNC_ITERATOR_GET_CONTAINER(Function)
182  * Helper macro to cast @p Function to a Eina_Iterator_Get_Container_Callback.
183  */
184 #define FUNC_ITERATOR_GET_CONTAINER(Function) ((Eina_Iterator_Get_Container_Callback)Function)
185 
186 /**
187  * @def FUNC_ITERATOR_FREE(Function)
188  * Helper macro to cast @p Function to a Eina_Iterator_Free_Callback.
189  */
190 #define FUNC_ITERATOR_FREE(Function)          ((Eina_Iterator_Free_Callback)Function)
191 
192 /**
193  * @def FUNC_ITERATOR_LOCK(Function)
194  * Helper macro to cast @p Function to a Eina_Iterator_Lock_Callback.
195  */
196 #define FUNC_ITERATOR_LOCK(Function)          ((Eina_Iterator_Lock_Callback)Function)
197 
198 
199 /**
200  * @brief Frees an iterator.
201  *
202  * @param[in,out] iterator The iterator to free.
203  *
204  * This function frees @p iterator if it is not @c NULL;
205  */
206 EAPI void      eina_iterator_free(Eina_Iterator *iterator);
207 
208 
209 /**
210  * @brief Returns the container of an iterator.
211  *
212  * @param[in] iterator The iterator.
213  * @return The container which created the iterator.
214  *
215  * This function returns the container which created @p iterator. If
216  * @p iterator is @c NULL, this function returns @c NULL.
217  */
218 EAPI void     *eina_iterator_container_get(Eina_Iterator *iterator) EINA_ARG_NONNULL(1) EINA_PURE;
219 
220 /**
221  * @brief Returns the value of the current element and go to the next one.
222  *
223  * @param[in,out] iterator The iterator.
224  * @param[out] data The data of the element.
225  * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
226  *
227  * This function returns the value of the current element pointed by
228  * @p iterator in @p data, then goes to the next element. If @p
229  * iterator is @c NULL or if a problem occurred, #EINA_FALSE is
230  * returned, otherwise #EINA_TRUE is returned.
231  */
232 EAPI Eina_Bool eina_iterator_next(Eina_Iterator *iterator,
233                                   void         **data) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
234 
235 
236 /**
237  * @brief Iterates over the container and execute a callback on each element.
238  *
239  * @param[in,out] iterator The iterator.
240  * @param[in] callback The callback called on each iteration.
241  * @param[in] fdata The data passed to the callback.
242  *
243  * This function iterates over the elements pointed by @p iterator,
244  * beginning with the current element. For each element, the callback
245  * @p cb is called with the data @p fdata. If @p iterator is @c NULL,
246  * the function returns immediately. Also, if @p cb returns #EINA_FALSE,
247  * the iteration stops at that point, if @p cb returns #EINA_TRUE
248  * the iteration continues.
249  */
250 EAPI void eina_iterator_foreach(Eina_Iterator *iterator,
251                                 Eina_Each_Cb   callback,
252                                 const void    *fdata) EINA_ARG_NONNULL(2);
253 
254 
255 /**
256  * @brief Locks the container of the iterator.
257  *
258  * @param[in,out] iterator The iterator.
259  * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
260  *
261  * If the container of the @p iterator permits it, it will be locked. When a
262  * container is locked calling eina_iterator_foreach() on it will return
263  * immediately. If @p iterator is @c NULL or if a problem occurred, #EINA_FALSE
264  * is returned, otherwise #EINA_TRUE is returned. If the container isn't
265  * lockable, it will return #EINA_TRUE.
266  *
267  * @warning None of the existing eina data structures are lockable.
268  */
269 EAPI Eina_Bool eina_iterator_lock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1);
270 
271 /**
272  * @brief Unlocks the container of the iterator.
273  *
274  * @param[in,out] iterator The iterator.
275  * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
276  *
277  * If the container of the @p iterator permits it and was previously
278  * locked, it will be unlocked. If @p iterator is @c NULL or if a
279  * problem occurred, #EINA_FALSE is returned, otherwise #EINA_TRUE
280  * is returned. If the container is not lockable, it will
281  * return #EINA_TRUE.
282  *
283  * @warning None of the existing eina data structures are lockable.
284  */
285 EAPI Eina_Bool eina_iterator_unlock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1);
286 
287 /**
288  * @brief Creates an Eina_Iterator that iterates through a
289  * NUL-terminated C array.
290  *
291  * @param[in] array The NUL-terminated array
292  * @return The iterator that will walk over the array.
293  *
294  * You can create it like this:
295  * int array[] = {1, 2, 3, 4};
296  * int* array2[] = {&array[0], &array[1], &array[2], &array[3], NULL};
297  *
298  * Eina_Iterator* iterator = eina_carray_iterator_new((void**)array2);
299  *
300  * @since 1.18
301  */
302 EAPI Eina_Iterator *eina_carray_iterator_new(void** array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
303 
304 /**
305  * @brief Creates an Eina_Iterator that iterates through a
306  * C array of specified size.
307  *
308  * @param[in] array The array
309  * @return The iterator that will walk over the array.
310  *
311  * You can create it like this:
312  * int array[] = {1, 2, 3, 4};
313  *
314  * Eina_Iterator* iterator = eina_carray_length_iterator_new((void**)array, sizeof (array[0]), (EINA_C_ARRAY_LENGTH(array));
315  *
316  * @since 1.22
317  */
318 EAPI Eina_Iterator *eina_carray_length_iterator_new(void** array, unsigned int step, unsigned int length) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
319 
320 /**
321  * @def EINA_C_ARRAY_ITERATOR_NEW
322  * @brief Creates an Eina_Iterator that iterates through a
323  * NUL-terminated C array.
324  *
325  * @param[in] Array The NUL-terminated array
326  * @return The iterator that will walk over the array.
327  *
328  * You can create it like this:
329  * int array[] = {1, 2, 3, 4};
330  *
331  * Eina_Iterator* iterator = EINA_C_ARRAY_ITERATOR_NEW(array);
332  *
333  * @since 1.22
334  */
335 #define EINA_C_ARRAY_ITERATOR_NEW(Array) eina_carray_length_iterator_new((void**) Array, sizeof (Array[0]), EINA_C_ARRAY_LENGTH(Array))
336 
337 /**
338  * @brief Creates a new iterator which which iterates through all elements with are accepted by the filter callback
339  *
340  * @param[in] original the iterator the use as original set
341  * @param[in] filter if the callback returns true the element from the original set is taken into the the new set.
342  * @param[in] free_cb when the iterator is gone this callback will be called with data as argument
343  * @param[in] data the data which is passed to the filter callback
344  *
345  * The iterator is filtered while it is being iterated.
346  * The original iterator you pass in here is is then owned and will be freed once the the new iterator is freed.
347  *
348  * @since 1.19
349  */
350 EAPI Eina_Iterator* eina_iterator_filter_new(Eina_Iterator *original, Eina_Each_Cb filter, Eina_Free_Cb free_cb, void *data) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
351 
352 /**
353  * @brief Creates an Eina_Iterator that iterates through a series
354  * of Eina_Iterator.
355  *
356  * @param[in] it The first Eina_Iterator to iterate over
357  * @return The iterator that will walk all the other iterator
358  *
359  * Eina_Iterator* iterator = eina_multi_iterator_new(it1, it2, it3, NULL);
360  *
361  * @note The returned array will destroy iterator given to it once they are not
362  * necessary anymore. Taking ownership of those iterator.
363  *
364  * @since 1.22
365  */
366 EAPI Eina_Iterator *eina_multi_iterator_internal_new(Eina_Iterator *it, ...) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
367 
368 
369 /**
370  * @brief Calls the process method on each node of iterator, producing new "processed"
371  * nodes and returning a new iterator which contains them.
372  *
373  * @param[in] iterator Iterator containing the nodes to process.
374  * @param[in] process Method to call on each node.
375  * @param[in] free_cb Method called when all nodes have been processed. It receives "data" as a parameter.
376  * @param[in] fdata Additional data passed to the process method.
377  *
378  * Processes every node in the input iterator and returns a new iterator containing
379  * the processed nodes. This is akin to a Map function:
380  * https://en.wikipedia.org/wiki/Map_(higher-order_function)
381  *
382  * @since 1.24
383  */
384 EAPI Eina_Iterator* eina_iterator_processed_new(Eina_Iterator *iterator, Eina_Process_Cb process, Eina_Free_Cb free_cb, void *fdata) EINA_WARN_UNUSED_RESULT;
385 
386 /**
387  * @def eina_multi_iterator_new
388  * @brief Creates an Eina_Iterator that iterates through a series
389  * of Eina_Iterator.
390  *
391  * @param[in] It The first Eina_Iterator to iterate over
392  * @return The iterator that will walk all the other iterator
393  *
394  * Eina_Iterator* iterator = eina_multi_iterator_new(it1, it2, it3);
395  *
396  * @note The returned array will destroy iterator given to it once they are not
397  * necessary anymore. Taking ownership of those iterator.
398  *
399  * @since 1.22
400  */
401 #define eina_multi_iterator_new(It, ...) eina_multi_iterator_internal_new(It, ##__VA_ARGS__, NULL)
402 
403 /**
404  * @def EINA_ITERATOR_FOREACH
405  * @brief Definition for the macro to iterate over all elements easily.
406  *
407  * @param[in,out] itr The iterator to use.
408  * @param[out] data Where to store * data, must be a pointer support getting
409  *        its address since * eina_iterator_next() requires a pointer
410  *        to pointer!
411  *
412  * This macro is a convenient way to use iterators, very similar to
413  * EINA_LIST_FOREACH().
414  *
415  * This macro can be used for freeing the data of a list, like in the
416  * following example. It has the same goal as the one documented in
417  * EINA_LIST_FOREACH(), but using iterators:
418  *
419  * @code
420  * Eina_List     *list;
421  * Eina_Iterator *itr;
422  * char          *data;
423  *
424  * // list is already filled,
425  * // its elements are just duplicated strings
426  *
427  * itr = eina_list_iterator_new(list);
428  * EINA_ITERATOR_FOREACH(itr, data)
429  *   free(data);
430  * eina_iterator_free(itr);
431  * eina_list_free(list);
432  * @endcode
433  *
434  * @note This example is not optimal algorithm to release a list since
435  *    it will walk the list twice, but it serves as an example. For
436  *    optimized version use EINA_LIST_FREE()
437  *
438  * @warning The order in which the elements will be traversed depends on the
439  * underlying container and @b shouldn't be relied upon.
440  *
441  * @warning unless explicitly stated in functions returning iterators,
442  *    do not modify the iterated object while you walk it, in this
443  *    example using lists, do not remove list nodes or you might
444  *    crash!  This is not a limitation of iterators themselves,
445  *    rather in the iterators implementations to keep them as simple
446  *    and fast as possible.
447  */
448 #define EINA_ITERATOR_FOREACH(itr,                                   \
449                               data) while (eina_iterator_next((itr), \
450                                                               (void **)(void *)&(data)))
451 
452 /**
453  * @}
454  */
455 
456 /**
457  * @}
458  */
459 
460 #endif
461