36 #ifndef UCX_ITERATOR_H |
36 #ifndef UCX_ITERATOR_H |
37 #define UCX_ITERATOR_H |
37 #define UCX_ITERATOR_H |
38 |
38 |
39 #include "common.h" |
39 #include "common.h" |
40 |
40 |
41 #define CX_ITERATOR_BASE \ |
41 struct cx_iterator_base_s { |
42 /** \ |
42 /** |
43 * True iff the iterator points to valid data. \ |
43 * True iff the iterator points to valid data. |
44 */ \ |
44 */ |
45 __attribute__ ((__nonnull__)) \ |
45 __attribute__ ((__nonnull__)) |
46 bool (*valid)(void const *); \ |
46 bool (*valid)(void const *); |
47 /** \ |
47 |
48 * Returns a pointer to the current element. \ |
48 /** |
49 * \ |
49 * Returns a pointer to the current element. |
50 * When valid returns false, the behavior of this function is undefined. \ |
50 * |
51 */ \ |
51 * When valid returns false, the behavior of this function is undefined. |
52 __attribute__ ((__nonnull__)) \ |
52 */ |
53 void *(*current)(void const *); \ |
53 __attribute__ ((__nonnull__)) |
54 /** \ |
54 void *(*current)(void const *); |
55 * Original implementation in case the function needs to be wrapped. \ |
55 |
56 */ \ |
56 /** |
57 __attribute__ ((__nonnull__)) \ |
57 * Original implementation in case the function needs to be wrapped. |
58 void *(*current_impl)(void const *); \ |
58 */ |
59 /** \ |
59 __attribute__ ((__nonnull__)) |
60 * Advances the iterator. \ |
60 void *(*current_impl)(void const *); |
61 * \ |
61 |
62 * When valid returns false, the behavior of this function is undefined. \ |
62 /** |
63 */ \ |
63 * Advances the iterator. |
64 __attribute__ ((__nonnull__)) \ |
64 * |
65 void (*next)(void *); \ |
65 * When valid returns false, the behavior of this function is undefined. |
66 /** \ |
66 */ |
67 * Indicates whether this iterator may remove elements. \ |
67 __attribute__ ((__nonnull__)) |
68 */ \ |
68 void (*next)(void *); |
69 bool mutating; \ |
69 /** |
70 /** \ |
70 * Indicates whether this iterator may remove elements. |
71 * Internal flag for removing the current element when advancing. \ |
71 */ |
72 */ \ |
72 bool mutating; |
|
73 /** |
|
74 * Internal flag for removing the current element when advancing. |
|
75 */ |
73 bool remove; |
76 bool remove; |
|
77 }; |
|
78 |
|
79 /** |
|
80 * Declares base attributes for an iterator. |
|
81 */ |
|
82 #define CX_ITERATOR_BASE struct cx_iterator_base_s base |
74 |
83 |
75 /** |
84 /** |
76 * Internal iterator struct - use CxIterator. |
85 * Internal iterator struct - use CxIterator. |
77 */ |
86 */ |
78 struct cx_iterator_s { |
87 struct cx_iterator_s { |
79 CX_ITERATOR_BASE |
88 CX_ITERATOR_BASE; |
80 |
89 |
81 /** |
90 /** |
82 * Handle for the current element. |
91 * Handle for the current element. |
83 */ |
92 */ |
84 void *elem_handle; |
93 void *elem_handle; |
155 * This is especially false for past-the-end iterators. |
164 * This is especially false for past-the-end iterators. |
156 * |
165 * |
157 * @param iter the iterator |
166 * @param iter the iterator |
158 * @return true iff the iterator points to valid data |
167 * @return true iff the iterator points to valid data |
159 */ |
168 */ |
160 #define cxIteratorValid(iter) (iter).valid(&(iter)) |
169 #define cxIteratorValid(iter) (iter).base.valid(&(iter)) |
161 |
170 |
162 /** |
171 /** |
163 * Returns a pointer to the current element. |
172 * Returns a pointer to the current element. |
164 * |
173 * |
165 * The behavior is undefined if this iterator is invalid. |
174 * The behavior is undefined if this iterator is invalid. |
166 * |
175 * |
167 * @param iter the iterator |
176 * @param iter the iterator |
168 * @return a pointer to the current element |
177 * @return a pointer to the current element |
169 */ |
178 */ |
170 #define cxIteratorCurrent(iter) (iter).current(&iter) |
179 #define cxIteratorCurrent(iter) (iter).base.current(&iter) |
171 |
180 |
172 /** |
181 /** |
173 * Advances the iterator to the next element. |
182 * Advances the iterator to the next element. |
174 * |
183 * |
175 * @param iter the iterator |
184 * @param iter the iterator |
176 */ |
185 */ |
177 #define cxIteratorNext(iter) (iter).next(&iter) |
186 #define cxIteratorNext(iter) (iter).base.next(&iter) |
178 |
187 |
179 /** |
188 /** |
180 * Flags the current element for removal, if this iterator is mutating. |
189 * Flags the current element for removal, if this iterator is mutating. |
181 * |
190 * |
182 * @param iter the iterator |
191 * @param iter the iterator |
183 */ |
192 */ |
184 #define cxIteratorFlagRemoval(iter) (iter).remove |= (iter).mutating |
193 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating |
185 |
194 |
186 /** |
195 /** |
187 * Loops over an iterator. |
196 * Loops over an iterator. |
188 * @param type the type of the elements |
197 * @param type the type of the elements |
189 * @param elem the name of the iteration variable |
198 * @param elem the name of the iteration variable |