Sat, 16 Apr 2022 17:28:36 +0200
fix required cast if compiled with C++
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * \file iterator.h
30 * \brief Interface for iterator implementations.
31 * \author Mike Becker
32 * \author Olaf Wintermann
33 * \version 3.0
34 * \copyright 2-Clause BSD License
35 */
37 #ifndef UCX_ITERATOR_H
38 #define UCX_ITERATOR_H
40 #include "common.h"
42 /**
43 * Internal iterator struct - use CxIterator.
44 */
45 struct cx_iterator_s {
46 /**
47 * True iff the iterator points to valid data.
48 */
49 bool (*valid)(struct cx_iterator_s const *) __attribute__ ((__nonnull__));
51 /**
52 * Returns a pointer to the current element.
53 */
54 void *(*current)(struct cx_iterator_s const *) __attribute__ ((__nonnull__));
56 /**
57 * Advances the iterator.
58 */
59 void (*next)(struct cx_iterator_s *) __attribute__ ((__nonnull__));
61 /**
62 * Handle for the current element, if required.
63 */
64 void *elem_handle;
66 /**
67 * Handle for the source collection, if any.
68 */
69 void *src_handle;
71 /**
72 * If the iterator is position-aware, contains the index of the element in the underlying collection.
73 * Otherwise, this field is usually uninitialized.
74 */
75 size_t index;
77 /**
78 * Users may set this to true, if the current element shall be removed from the underlying collection
79 * when the iterator advances.
80 * Has no effect on iterators that are not based on a collection.
81 */
82 bool remove;
83 };
85 /**
86 * Iterator value type.
87 * An iterator points to a certain element in an (possibly unbounded) chain of elements.
88 * Iterators that are based on collections (which have a defined "first" element), are supposed
89 * to be "position-aware", which means that they keep track of the current index within the collection.
90 *
91 * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the
92 * iterator is based on a collection and the underlying collection is mutated (elements added or removed),
93 * the iterator becomes invalid (regardless of what cxIteratorValid() returns) and MUST be re-obtained
94 * from the collection.
95 */
96 typedef struct cx_iterator_s CxIterator;
98 /**
99 * Checks if the iterator points to valid data.
100 *
101 * This is especially false for past-the-end iterators.
102 *
103 * @param iter a pointer to the iterator
104 * @return true iff the iterator points to valid data
105 */
106 __attribute__ ((__nonnull__))
107 static inline bool cxIteratorValid(CxIterator const *iter) {
108 return iter->valid(iter);
109 }
111 /**
112 * Returns a pointer to the current element.
113 *
114 * The behavior is undefined if this iterator is invalid.
115 *
116 * @param iter a pointer to the iterator
117 * @return a pointer to the current element
118 */
119 __attribute__ ((__nonnull__))
120 static inline void *cxIteratorCurrent(CxIterator const *iter) {
121 return iter->current(iter);
122 }
124 /**
125 * Advances the iterator to the next element.
126 *
127 * @param iter a pointer to the iterator
128 */
129 __attribute__ ((__nonnull__))
130 static inline void cxIteratorNext(CxIterator *iter) {
131 iter->next(iter);
132 }
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 = (type)cxIteratorCurrent(&iter)) != NULL ; cxIteratorNext(&iter)) // NOLINT(bugprone-macro-parentheses)
143 #endif /* UCX_ITERATOR_H */