docs/Writerside/topics/iterator.h.md

changeset 1681
56e76fbac167
parent 1613
75a8c63db7c7
equal deleted inserted replaced
1680:1aa21afb8763 1681:56e76fbac167
90 90
91 For this purpose, most collections allow using `cxIteratorFlagRemoval()`, which instructs the iterator to remove 91 For this purpose, most collections allow using `cxIteratorFlagRemoval()`, which instructs the iterator to remove
92 the current element from the collection on the next call to `cxIteratorNext()`. 92 the current element from the collection on the next call to `cxIteratorNext()`.
93 If you are implementing your own iterator, it is up to you to implement this behavior. 93 If you are implementing your own iterator, it is up to you to implement this behavior.
94 94
95 ## Passing Iterators to Functions
96
97 To eliminate the need of memory management for iterators, the structures are usually passed by value.
98 However, sometimes it is necessary to pass an iterator to another function.
99
100 To make that possible in a generalized way, such functions should accept a `CxIteratorBase*` pointer
101 which can be obtained with the `cxIteratorRef()` macro on the calling site.
102
103 In the following example, elements from a list are inserted into a tree:
104
105 ```C
106 CxList *list = // ...
107 CxTree *tree = // ...
108
109 CxIterator iter = cxListIterator(list);
110 cxTreeInsertIter(tree, cxIteratorRef(iter), cxListSize(list));
111 ```
112
113 > This is the reason why `CX_ITERATOR_BASE` must be the first member of any iterator structure.
114 > Otherwise, the address taken by `cxIteratorRef()` would not equal the address of the iterator.
115 {style="note"}
116
117 ## Custom Iterators 95 ## Custom Iterators
118 96
119 The base structure is defined as follows: 97 The base structure is defined as follows:
120 ```C 98 ```C
121 struct cx_iterator_base_s { 99 struct cx_iterator_base_s {

mercurial