1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
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.
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.
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/>.
19 #ifndef EINA_ITERATOR_H__
20 #define EINA_ITERATOR_H__
22 #include "eina_config.h"
24 #include "eina_types.h"
25 #include "eina_magic.h"
28 * @page eina_iterator_example_page Eina_Iterator usage
29 * @dontinclude eina_iterator_01.c
31 * As always when using eina we need to include it:
35 * Here we a declare an unimpressive @ref Eina_Each_Cb "function" that prints
38 * @note Returning EINA_TRUE is important so we don't stop iterating over the
41 * And here 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:
46 * And on to our main function were we declare some variables and initialize
47 * eina, nothing too special:
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 :
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
58 * Having our iterator now pointing to interesting data we go ahead and print:
59 * @until print_eina_container
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
64 * @until eina_iterator_free
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
69 * @until eina_iterator_free
70 * @note The only significant diference to the block above is in the
71 * function used to create the iterator.
73 * And now we free the list and shut eina down:
78 * @page eina_iterator_01_c Eina_Iterator usage
79 * @page eina_iterator_01_c Eina_Iterator usage
81 * @include eina_iterator_01.c
82 * @example eina_iterator_01.c
86 * @addtogroup Eina_Iterator_Group Iterator Functions
88 * @brief These functions manage iterators on containers.
90 * These functions allow to access 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 allows sequential access
93 * (that is, from an element to the next one). For random access, see
94 * @ref Eina_Accessor_Group.
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 iterator creation function here, those can be found on
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().
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().
107 * Here an @ref eina_iterator_example_page "example"
111 * @addtogroup Eina_Content_Access_Group Content Access
117 * @defgroup Eina_Iterator_Group Iterator Functions
123 * @typedef Eina_Iterator
124 * Abstract type for iterators.
126 typedef struct _Eina_Iterator Eina_Iterator;
129 * @typedef Eina_Iterator_Next_Callback
130 * Type for a callback that returns the next element in a container.
132 typedef Eina_Bool (*Eina_Iterator_Next_Callback)(Eina_Iterator *it, void **data);
135 * @typedef Eina_Iterator_Get_Container_Callback
136 * Type for a callback that returns the container.
138 typedef void *(*Eina_Iterator_Get_Container_Callback)(Eina_Iterator *it);
141 * @typedef Eina_Iterator_Free_Callback
142 * Type for a callback that frees the container.
144 typedef void (*Eina_Iterator_Free_Callback)(Eina_Iterator *it);
147 * @typedef Eina_Iterator_Lock_Callback
148 * Type for a callback that lock the container.
150 typedef Eina_Bool (*Eina_Iterator_Lock_Callback)(Eina_Iterator *it);
153 * @struct _Eina_Iterator
154 * structure of an iterator
156 * If creating an iterator remember to set the type using @ref EINA_MAGIC_SET.
158 struct _Eina_Iterator
160 #define EINA_ITERATOR_VERSION 1
161 int version; /**< Version of the Iterator API. */
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. */
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. */
170 #define EINA_MAGIC_ITERATOR 0x98761233
175 * @def FUNC_ITERATOR_NEXT(Function)
176 * Helper macro to cast @p Function to a Eina_Iterator_Next_Callback.
178 #define FUNC_ITERATOR_NEXT(Function) ((Eina_Iterator_Next_Callback)Function)
181 * @def FUNC_ITERATOR_GET_CONTAINER(Function)
182 * Helper macro to cast @p Function to a Eina_Iterator_Get_Container_Callback.
184 #define FUNC_ITERATOR_GET_CONTAINER(Function) ((Eina_Iterator_Get_Container_Callback)Function)
187 * @def FUNC_ITERATOR_FREE(Function)
188 * Helper macro to cast @p Function to a Eina_Iterator_Free_Callback.
190 #define FUNC_ITERATOR_FREE(Function) ((Eina_Iterator_Free_Callback)Function)
193 * @def FUNC_ITERATOR_LOCK(Function)
194 * Helper macro to cast @p Function to a Eina_Iterator_Lock_Callback.
196 #define FUNC_ITERATOR_LOCK(Function) ((Eina_Iterator_Lock_Callback)Function)
200 * @brief Free an iterator.
202 * @param iterator The iterator to free.
204 * This function frees @p iterator if it is not @c NULL;
206 EAPI void eina_iterator_free(Eina_Iterator *iterator);
210 * @brief Return the container of an iterator.
212 * @param iterator The iterator.
213 * @return The container which created the iterator.
215 * This function returns the container which created @p iterator. If
216 * @p iterator is @c NULL, this function returns @c NULL.
218 EAPI void *eina_iterator_container_get(Eina_Iterator *iterator) EINA_ARG_NONNULL(1) EINA_PURE;
221 * @brief Return the value of the current element and go to the next one.
223 * @param iterator The iterator.
224 * @param data The data of the element.
225 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
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.
232 EAPI Eina_Bool eina_iterator_next(Eina_Iterator *iterator,
233 void **data) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
237 * @brief Iterate over the container and execute a callback on each element.
239 * @param iterator The iterator.
240 * @param callback The callback called on each iteration.
241 * @param fdata The data passed to the callback.
243 * This function iterates over the elements pointed by @p iterator,
244 * beginning from 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.
250 EAPI void eina_iterator_foreach(Eina_Iterator *iterator,
251 Eina_Each_Cb callback,
252 const void *fdata) EINA_ARG_NONNULL(2);
256 * @brief Lock the container of the iterator.
258 * @param iterator The iterator.
259 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
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.
267 * @warning None of the existing eina data structures are lockable.
269 EAPI Eina_Bool eina_iterator_lock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1);
272 * @brief Unlock the container of the iterator.
274 * @param iterator The iterator.
275 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
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
283 * @warning None of the existing eina data structures are lockable.
285 EAPI Eina_Bool eina_iterator_unlock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1);
288 * @def EINA_ITERATOR_FOREACH
289 * @brief Macro to iterate over all elements easily.
291 * @param itr The iterator to use.
292 * @param data Where to store * data, must be a pointer support getting
293 * its address since * eina_iterator_next() requires a pointer
296 * This macro is a convenient way to use iterators, very similar to
297 * EINA_LIST_FOREACH().
299 * This macro can be used for freeing the data of a list, like in the
300 * following example. It has the same goal as the one documented in
301 * EINA_LIST_FOREACH(), but using iterators:
305 * Eina_Iterator *itr;
308 * // list is already filled,
309 * // its elements are just duplicated strings
311 * itr = eina_list_iterator_new(list);
312 * EINA_ITERATOR_FOREACH(itr, data)
314 * eina_iterator_free(itr);
315 * eina_list_free(list);
318 * @note this example is not optimal algorithm to release a list since
319 * it will walk the list twice, but it serves as an example. For
320 * optimized version use EINA_LIST_FREE()
322 * @warning The order in which the elements will be traversed depends on the
323 * underlying container and @b shouldn't be relied upon.
325 * @warning unless explicitly stated in functions returning iterators,
326 * do not modify the iterated object while you walk it, in this
327 * example using lists, do not remove list nodes or you might
328 * crash! This is not a limitiation of iterators themselves,
329 * rather in the iterators implementations to keep them as simple
330 * and fast as possible.
332 #define EINA_ITERATOR_FOREACH(itr, \
333 data) while (eina_iterator_next((itr), \
334 (void **)(void *)&(data)))