- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files
- Examples

Data Structures | Macros | Typedefs | Functions
Iterator Functions
Eina » Data Types » Content Access

These functions manage iterators on containers. More...

## Data Structures
struct | _Eina_Iterator
| structure of an iterator More...

## Macros
#define | FUNC_ITERATOR_NEXT(Function) ((Eina_Iterator_Next_Callback)Function)
| Helper macro to cast `Function` to a Eina_Iterator_Next_Callback.
#define | FUNC_ITERATOR_GET_CONTAINER(Function) ((Eina_Iterator_Get_Container_Callback)Function)
| Helper macro to cast `Function` to a Eina_Iterator_Get_Container_Callback.
#define | FUNC_ITERATOR_FREE(Function) ((Eina_Iterator_Free_Callback)Function)
| Helper macro to cast `Function` to a Eina_Iterator_Free_Callback.
#define | FUNC_ITERATOR_LOCK(Function) ((Eina_Iterator_Lock_Callback)Function)
| Helper macro to cast `Function` to a Eina_Iterator_Lock_Callback.
#define | EINA_ITERATOR_FOREACH(itr,data)
| Macro to iterate over all elements easily. More...

## Typedefs
typedef struct _Eina_Iterator | Eina_Iterator
| Abstract type for iterators.
typedef Eina_Bool(* | Eina_Iterator_Next_Callback )(Eina_Iterator *it, void **data)
| Type for a callback that returns the next element in a container.
typedef void *(* | Eina_Iterator_Get_Container_Callback )(Eina_Iterator *it)
| Type for a callback that returns the container.
typedef void(* | Eina_Iterator_Free_Callback )(Eina_Iterator *it)
| Type for a callback that frees the container.
typedef Eina_Bool(* | Eina_Iterator_Lock_Callback )(Eina_Iterator *it)
| Type for a callback that lock the container.

## Functions
void | eina_iterator_free (Eina_Iterator *iterator)
| Free an iterator. More...
void * | eina_iterator_container_get (Eina_Iterator *iterator)
| Return the container of an iterator. More...
Eina_Bool | eina_iterator_next (Eina_Iterator *iterator, void **data)
| Return the value of the current element and go to the next one. More...
void | eina_iterator_foreach (Eina_Iterator *iterator, Eina_Each_Cb callback, const void *fdata)
| Iterate over the container and execute a callback on each element. More...
Eina_Bool | eina_iterator_lock (Eina_Iterator *iterator)
| Lock the container of the iterator. More...
Eina_Bool | eina_iterator_unlock (Eina_Iterator *iterator)
| Unlock the container of the iterator. More...

## Detailed Description

These functions manage iterators on containers.

These functions allow to access elements of a container in a generic way, without knowing which container is used (a bit like iterators in the C++ STL). Iterators only allows sequential access (that is, from an element to the next one). For random access, see Accessor Functions.

Getting an iterator to access elements of a given container is done through the functions of that particular container. There is no function to create a generic iterator as iterators absolutely depend on the container. This means you won't find iterator creation function here, those can be found on the documentation of the container type you're using. Though created with container specific functions iterators are always deleted with the same function: eina_iterator_free().

To get the data and iterate, use eina_iterator_next(). To call a function on all the elements of a container, use eina_iterator_foreach().

Here an example

## Macro Definition Documentation

#define EINA_ITERATOR_FOREACH | ( |  | itr,
|  |  | data
| ) |  |

Value:
while (eina_iterator_next((itr), \
(void **)(void *)&(data)))
eina_iterator_next
Eina_Bool eina_iterator_next(Eina_Iterator *iterator, void **data)
Return the value of the current element and go to the next one.
Definition: eina_iterator.c:116

Macro to iterate over all elements easily.

Parameters

  itr | The iterator to use.
  data | Where to store * data, must be a pointer support getting its address since * eina_iterator_next() requires a pointer to pointer!

This macro is a convenient way to use iterators, very similar to EINA_LIST_FOREACH().

This macro can be used for freeing the data of a list, like in the following example. It has the same goal as the one documented in EINA_LIST_FOREACH(), but using iterators:

1 Eina_List *list;
2 Eina_Iterator *itr;
3 char *data;
4
5 // list is already filled,
6 // its elements are just duplicated strings
7
8 itr = eina_list_iterator_new(list);
9 EINA_ITERATOR_FOREACH(itr, data)
10 free(data);
11 eina_iterator_free(itr);
12 eina_list_free(list);

Note
  this example is not optimal algorithm to release a list since it will walk the list twice, but it serves as an example. For optimized version use EINA_LIST_FREE()

Warning
  The order in which the elements will be traversed depends on the underlying container and shouldn't be relied upon.
  unless explicitly stated in functions returning iterators, do not modify the iterated object while you walk it, in this example using lists, do not remove list nodes or you might crash! This is not a limitiation of iterators themselves, rather in the iterators implementations to keep them as simple and fast as possible.

Examples:
  ecore_con_client_example.c, eet-data-file_descriptor_01.c, eina_file_01.c, eina_list_03.c, and eina_tiler_01.c.

Referenced by ecore_file_dir_is_empty(), ecore_file_ls(), ecore_file_recursive_rm(), edje_available_modules_get(), edje_color_class_del(), edje_color_class_set(), edje_edit_clean_save_as(), edje_edit_data_list_get(), edje_edit_font_path_get(), edje_edit_fonts_list_get(), edje_edit_group_add(), edje_edit_group_aliased_get(), edje_edit_group_aliases_get(), edje_edit_group_copy(), edje_edit_group_data_list_get(), edje_edit_image_del(), edje_edit_image_replace(), edje_edit_image_usage_list_get(), edje_mmap_collection_list(), edje_mmap_group_exists(), edje_text_class_del(), edje_text_class_set(), eina_file_flush(), eina_tiler_subtract(), eina_tiler_union(), and eldbus_shutdown().

## Function Documentation

void eina_iterator_free | ( | Eina_Iterator * | iterator | ) |

Free an iterator.

Parameters

  iterator | The iterator to free.

This function frees `iterator` if it is not `NULL`;

References _Eina_Iterator::free.

Referenced by ecore_file_dir_is_empty(), ecore_file_ls(), ecore_file_recursive_rm(), ecore_wl_window_surface_find(), edje_available_modules_get(), edje_color_class_del(), edje_color_class_set(), edje_edit_clean_save_as(), edje_edit_data_list_get(), edje_edit_font_path_get(), edje_edit_fonts_list_get(), edje_edit_group_add(), edje_edit_group_aliased_get(), edje_edit_group_aliases_get(), edje_edit_group_copy(), edje_edit_group_data_list_get(), edje_edit_image_del(), edje_edit_image_replace(), edje_edit_image_usage_list_get(), edje_mmap_collection_list(), edje_mmap_group_exists(), edje_text_class_del(), edje_text_class_set(), eina_cow_gc(), eina_file_flush(), eina_hash_foreach(), eina_tiler_equal(), eina_tiler_intersection(), eina_tiler_subtract(), eina_tiler_union(), eldbus_shutdown(), and efl::eina::_common_iterator_base< T const >::~_common_iterator_base().

void* eina_iterator_container_get | ( | Eina_Iterator * | iterator | ) |

Return the container of an iterator.

Parameters

  iterator | The iterator.

Returns
  The container which created the iterator.

This function returns the container which created `iterator`. If `iterator` is `NULL`, this function returns `NULL`.

References _Eina_Iterator::get_container.

Eina_Bool eina_iterator_next | ( | Eina_Iterator * | iterator,
|  | void ** | data
| ) |  |

Return the value of the current element and go to the next one.

Parameters

  iterator | The iterator.
  data | The data of the element.

Returns
  EINA_TRUE on success, EINA_FALSE otherwise.

This function returns the value of the current element pointed by `iterator` in `data`, then goes to the next element. If `iterator` is `NULL` or if a problem occurred, EINA_FALSE is returned, otherwise EINA_TRUE is returned.

References EINA_FALSE, and _Eina_Iterator::next.

Referenced by ecore_wl_window_surface_find(), eina_cow_gc(), eina_tiler_equal(), eina_tiler_intersection(), and efl::eina::iterator< T >::operator++().

void eina_iterator_foreach | ( | Eina_Iterator * | iterator,
|  | Eina_Each_Cb | callback,
|  | const void * | fdata
| ) |  |

Iterate over the container and execute a callback on each element.

Parameters

  iterator | The iterator.
  callback | The callback called on each iteration.
  fdata | The data passed to the callback.

This function iterates over the elements pointed by `iterator`, beginning from the current element. For Each element, the callback `cb` is called with the data `fdata`. If `iterator` is `NULL`, the function returns immediately. Also, if `cb` returns EINA_FALSE, the iteration stops at that point, if `cb` returns EINA_TRUE the iteration continues.

References eina_iterator_lock(), eina_iterator_unlock(), EINA_TRUE, _Eina_Iterator::get_container, and _Eina_Iterator::next.

Referenced by eina_hash_foreach().

Eina_Bool eina_iterator_lock | ( | Eina_Iterator * | iterator | ) |

Lock the container of the iterator.

Parameters

  iterator | The iterator.

Returns
  EINA_TRUE on success, EINA_FALSE otherwise.

If the container of the `iterator` permits it, it will be locked. When a container is locked calling eina_iterator_foreach() on it will return immediately. If `iterator` is `NULL` or if a problem occurred, EINA_FALSE is returned, otherwise EINA_TRUE is returned. If the container isn't lockable, it will return EINA_TRUE.

Warning
  None of the existing eina data structures are lockable.

References EINA_FALSE, EINA_TRUE, and _Eina_Iterator::lock.

Referenced by eina_iterator_foreach().

Eina_Bool eina_iterator_unlock | ( | Eina_Iterator * | iterator | ) |

Unlock the container of the iterator.

Parameters

  iterator | The iterator.

Returns
  EINA_TRUE on success, EINA_FALSE otherwise.

If the container of the `iterator` permits it and was previously locked, it will be unlocked. If `iterator` is `NULL` or if a problem occurred, EINA_FALSE is returned, otherwise EINA_TRUE is returned. If the container is not lockable, it will return EINA_TRUE.

Warning
  None of the existing eina data structures are lockable.

References EINA_FALSE, EINA_TRUE, and _Eina_Iterator::unlock.

Referenced by eina_iterator_foreach().