2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
4 /* EINA - EFL data type library
5 * Copyright (C) 2002-2008 Cedric Bail
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library;
19 * if not, see <http://www.gnu.org/licenses/>.
28 #include "eina_config.h"
29 #include "eina_private.h"
31 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
32 #include "eina_safety_checks.h"
33 #include "eina_iterator.h"
35 /*============================================================================*
37 *============================================================================*/
43 #define EINA_MAGIC_CHECK_ITERATOR(d) \
45 if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_ITERATOR)) \
46 EINA_MAGIC_FAIL(d, EINA_MAGIC_ITERATOR); \
54 /*============================================================================*
56 *============================================================================*/
58 /*============================================================================*
60 *============================================================================*/
63 * @addtogroup Eina_Iterator_Group Iterator Functions
65 * @brief These functions manage iterators on containers.
67 * These functions allow to access elements of a container in a
68 * generic way, without knowing which container is used (a bit like
69 * iterators in the C++ STL). Iterators only allows sequential access
70 * (that is, from an element to the next one). For random access, see
71 * @ref Eina_Accessor_Group.
73 * An iterator is created from container data types, so no creation
74 * function is available here. An iterator is deleted with
75 * eina_iterator_free(). To get the data and iterate, use
76 * eina_iterator_next(). To call a function on all the elements of a
77 * container, use eina_iterator_foreach().
84 * @brief Initialize the iterator module.
86 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
88 * This function sets up the iterator module of Eina. It is called by
94 eina_iterator_init(void)
96 return eina_magic_string_set(EINA_MAGIC_ITERATOR, "Eina Iterator");
101 * @brief Shut down the iterator module.
103 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
105 * This function shuts down the iterator module set up by
106 * eina_iterator_init(). It is called by eina_shutdown().
108 * @see eina_shutdown()
111 eina_iterator_shutdown(void)
117 * @brief Free an iterator.
119 * @param iterator The iterator to free.
121 * This function frees @p iterator if it is not @c NULL;
124 eina_iterator_free(Eina_Iterator *iterator)
126 EINA_MAGIC_CHECK_ITERATOR(iterator);
127 EINA_SAFETY_ON_NULL_RETURN(iterator);
128 EINA_SAFETY_ON_NULL_RETURN(iterator->free);
129 iterator->free(iterator);
133 * @brief Return the container of an iterator.
135 * @param iterator The iterator.
136 * @return The container which created the iterator.
138 * This function returns the container which created @p iterator. If
139 * @p iterator is @c NULL, this function returns @c NULL.
142 eina_iterator_container_get(Eina_Iterator *iterator)
144 EINA_MAGIC_CHECK_ITERATOR(iterator);
145 EINA_SAFETY_ON_NULL_RETURN_VAL(iterator, NULL);
146 EINA_SAFETY_ON_NULL_RETURN_VAL(iterator->get_container, NULL);
147 return iterator->get_container(iterator);
151 * @brief Return the value of the current element and go to the next one.
153 * @param iterator The iterator.
154 * @param data The data of the element.
155 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
157 * This function returns the value of the current element pointed by
158 * @p iterator in @p data, then goes to the next element. If @p
159 * iterator is @c NULL or if a problem occured, #EINA_FALSE is
160 * returned, otherwise #EINA_TRUE is returned.
163 eina_iterator_next(Eina_Iterator *iterator, void **data)
165 if (!iterator) return EINA_FALSE;
166 EINA_MAGIC_CHECK_ITERATOR(iterator);
167 EINA_SAFETY_ON_NULL_RETURN_VAL(iterator, EINA_FALSE);
168 EINA_SAFETY_ON_NULL_RETURN_VAL(iterator->next, EINA_FALSE);
169 EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
170 return iterator->next(iterator, data);
174 * @brief Iterate over the container and execute a callback on each element.
176 * @param iterator The iterator.
177 * @param cb The callback called on each iteration.
178 * @param fdata The data passed to the callback.
180 * This function iterates over the elements pointed by @p iterator,
181 * beginning from the current element. For Each element, the callback
182 * @p cb is called with the data @p fdata.If @p iterator is @c NULL,
183 * the function returns immediatly.
186 eina_iterator_foreach(Eina_Iterator *iterator,
190 const void *container;
193 EINA_MAGIC_CHECK_ITERATOR(iterator);
194 EINA_SAFETY_ON_NULL_RETURN(iterator);
195 EINA_SAFETY_ON_NULL_RETURN(iterator->get_container);
196 EINA_SAFETY_ON_NULL_RETURN(iterator->next);
197 EINA_SAFETY_ON_NULL_RETURN(cb);
199 container = iterator->get_container(iterator);
200 while (iterator->next(iterator, &data) == EINA_TRUE) {
201 if (cb(container, data, (void*) fdata) != EINA_TRUE) return ;