EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / include / eina_iterator.h
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 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 diference 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 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.
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 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().
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 Free an iterator.
201  *
202  * @param 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 Return the container of an iterator.
211  *
212  * @param 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 Return the value of the current element and go to the next one.
222  *
223  * @param iterator The iterator.
224  * @param 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 Iterate over the container and execute a callback on each element.
238  *
239  * @param iterator The iterator.
240  * @param callback The callback called on each iteration.
241  * @param fdata The data passed to the callback.
242  *
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.
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 Lock the container of the iterator.
257  *
258  * @param 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 Unlock the container of the iterator.
273  *
274  * @param 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  * @def EINA_ITERATOR_FOREACH
289  * @brief Macro to iterate over all elements easily.
290  *
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
294  *        to pointer!
295  *
296  * This macro is a convenient way to use iterators, very similar to
297  * EINA_LIST_FOREACH().
298  *
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:
302  *
303  * @code
304  * Eina_List     *list;
305  * Eina_Iterator *itr;
306  * char          *data;
307  *
308  * // list is already filled,
309  * // its elements are just duplicated strings
310  *
311  * itr = eina_list_iterator_new(list);
312  * EINA_ITERATOR_FOREACH(itr, data)
313  *   free(data);
314  * eina_iterator_free(itr);
315  * eina_list_free(list);
316  * @endcode
317  *
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()
321  *
322  * @warning The order in which the elements will be traversed depends on the
323  * underlying container and @b shouldn't be relied upon.
324  *
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.
331  */
332 #define EINA_ITERATOR_FOREACH(itr,                                   \
333                               data) while (eina_iterator_next((itr), \
334                                                               (void **)(void *)&(data)))
335
336 /**
337  * @}
338  */
339
340 /**
341  * @}
342  */
343
344 #endif