src/cx/iterator.h

changeset 496
1a07e24801a9
parent 495
2856c74e18ba
child 497
b182a8b8a1af
equal deleted inserted replaced
495:2856c74e18ba 496:1a07e24801a9
98 /** 98 /**
99 * Checks if the iterator points to valid data. 99 * Checks if the iterator points to valid data.
100 * 100 *
101 * This is especially false for past-the-end iterators. 101 * This is especially false for past-the-end iterators.
102 * 102 *
103 * @param iter the iterator 103 * @param iter a pointer to the iterator
104 * @return true iff the iterator points to valid data 104 * @return true iff the iterator points to valid data
105 */ 105 */
106 __attribute__ ((__nonnull__)) 106 __attribute__ ((__nonnull__))
107 static inline bool cxIteratorValid(CxIterator const *iter) { 107 static inline bool cxIteratorValid(CxIterator const *iter) {
108 return iter->valid(iter); 108 return iter->valid(iter);
111 /** 111 /**
112 * Returns a pointer to the current element. 112 * Returns a pointer to the current element.
113 * 113 *
114 * The behavior is undefined if this iterator is invalid. 114 * The behavior is undefined if this iterator is invalid.
115 * 115 *
116 * @param iter the iterator 116 * @param iter a pointer to the iterator
117 * @return a pointer to the current element 117 * @return a pointer to the current element
118 */ 118 */
119 __attribute__ ((__nonnull__)) 119 __attribute__ ((__nonnull__))
120 static inline void *cxIteratorCurrent(CxIterator const *iter) { 120 static inline void *cxIteratorCurrent(CxIterator const *iter) {
121 return iter->current(iter); 121 return iter->current(iter);
122 } 122 }
123 123
124 /** 124 /**
125 * Advances the iterator to the next element. 125 * Advances the iterator to the next element.
126 * 126 *
127 * @param iter the iterator 127 * @param iter a pointer to the iterator
128 */ 128 */
129 __attribute__ ((__nonnull__)) 129 __attribute__ ((__nonnull__))
130 static inline void cxIteratorNext(CxIterator *iter) { 130 static inline void cxIteratorNext(CxIterator *iter) {
131 iter->next(iter); 131 iter->next(iter);
132 } 132 }
133 133
134 /**
135 * Loops over an iterator.
136 * @param type the type of the elements
137 * @param elem the name of the iteration variable
138 * @param iter the iterator
139 */
140 #define cx_foreach(type, elem, iter) \
141 for (type *elem; cxIteratorValid(&iter) && (elem = cxIteratorCurrent(&iter)) != NULL ; cxIteratorNext(&iter)) // NOLINT(bugprone-macro-parentheses)
142
134 #endif /* UCX_ITERATOR_H */ 143 #endif /* UCX_ITERATOR_H */

mercurial