3 weeks ago
move ui_customwidget_create to separate file (GTK)
174 | 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 | /** | |
440 | 29 | * @file iterator.h |
30 | * @brief Interface for iterator implementations. | |
31 | * @author Mike Becker | |
32 | * @author Olaf Wintermann | |
33 | * @copyright 2-Clause BSD License | |
174 | 34 | */ |
35 | ||
36 | #ifndef UCX_ITERATOR_H | |
37 | #define UCX_ITERATOR_H | |
38 | ||
39 | #include "common.h" | |
40 | ||
440 | 41 | #ifdef __cplusplus |
42 | extern "C" { | |
43 | #endif | |
44 | ||
45 | /** | |
46 | * Common data for all iterators. | |
47 | */ | |
174 | 48 | struct cx_iterator_base_s { |
49 | /** | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
50 | * True if the iterator points to valid data. |
174 | 51 | */ |
324 | 52 | bool (*valid)(const void *); |
174 | 53 | |
54 | /** | |
55 | * Returns a pointer to the current element. | |
56 | * | |
57 | * When valid returns false, the behavior of this function is undefined. | |
58 | */ | |
324 | 59 | void *(*current)(const void *); |
174 | 60 | |
61 | /** | |
62 | * Original implementation in case the function needs to be wrapped. | |
63 | */ | |
324 | 64 | void *(*current_impl)(const void *); |
174 | 65 | |
66 | /** | |
67 | * Advances the iterator. | |
68 | * | |
69 | * When valid returns false, the behavior of this function is undefined. | |
70 | */ | |
71 | void (*next)(void *); | |
72 | /** | |
73 | * Indicates whether this iterator may remove elements. | |
74 | */ | |
75 | bool mutating; | |
76 | /** | |
77 | * Internal flag for removing the current element when advancing. | |
78 | */ | |
79 | bool remove; | |
80 | }; | |
81 | ||
82 | /** | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
83 | * Convenience type definition for the base structure of an iterator. |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
84 | * @see #CX_ITERATOR_BASE |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
85 | */ |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
86 | typedef struct cx_iterator_base_s CxIteratorBase; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
87 | |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
88 | /** |
324 | 89 | * Declares base attributes for an iterator. |
90 | * Must be the first member of an iterator structure. | |
174 | 91 | */ |
324 | 92 | #define CX_ITERATOR_BASE struct cx_iterator_base_s base |
93 | ||
94 | /** | |
95 | * Internal iterator struct - use CxIterator. | |
96 | */ | |
97 | struct cx_iterator_s { | |
440 | 98 | /** |
99 | * Inherited common data for all iterators. | |
100 | */ | |
324 | 101 | CX_ITERATOR_BASE; |
174 | 102 | |
103 | /** | |
324 | 104 | * Handle for the current element. |
174 | 105 | */ |
106 | void *elem_handle; | |
107 | ||
108 | /** | |
109 | * Handle for the source collection, if any. | |
110 | */ | |
324 | 111 | union { |
112 | /** | |
113 | * Access for mutating iterators. | |
114 | */ | |
115 | void *m; | |
116 | /** | |
117 | * Access for normal iterators. | |
118 | */ | |
119 | const void *c; | |
120 | } src_handle; | |
174 | 121 | |
122 | /** | |
123 | * If the iterator is position-aware, contains the index of the element in the underlying collection. | |
124 | * Otherwise, this field is usually uninitialized. | |
125 | */ | |
126 | size_t index; | |
324 | 127 | |
128 | /** | |
129 | * The size of an individual element. | |
130 | */ | |
131 | size_t elem_size; | |
132 | ||
133 | /** | |
134 | * May contain the total number of elements, if known. | |
440 | 135 | * Shall be set to @c SIZE_MAX when the total number is unknown during iteration. |
324 | 136 | */ |
137 | size_t elem_count; | |
174 | 138 | }; |
139 | ||
140 | /** | |
324 | 141 | * Iterator type. |
174 | 142 | * |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
143 | * An iterator points to a certain element in a (possibly unbounded) chain of elements. |
174 | 144 | * Iterators that are based on collections (which have a defined "first" element), are supposed |
145 | * to be "position-aware", which means that they keep track of the current index within the collection. | |
146 | * | |
147 | * @note Objects that are pointed to by an iterator are always mutable through that iterator. However, | |
440 | 148 | * any concurrent mutation of the collection other than by this iterator makes this iterator invalid, |
324 | 149 | * and it must not be used anymore. |
174 | 150 | */ |
151 | typedef struct cx_iterator_s CxIterator; | |
152 | ||
153 | /** | |
154 | * Checks if the iterator points to valid data. | |
155 | * | |
156 | * @param iter the iterator | |
440 | 157 | * @retval true if the iterator points to valid data |
158 | * @retval false if the iterator already moved past the end | |
174 | 159 | */ |
160 | #define cxIteratorValid(iter) (iter).base.valid(&(iter)) | |
161 | ||
162 | /** | |
163 | * Returns a pointer to the current element. | |
164 | * | |
165 | * The behavior is undefined if this iterator is invalid. | |
166 | * | |
167 | * @param iter the iterator | |
168 | * @return a pointer to the current element | |
440 | 169 | * @see cxIteratorValid() |
174 | 170 | */ |
171 | #define cxIteratorCurrent(iter) (iter).base.current(&iter) | |
172 | ||
173 | /** | |
174 | * Advances the iterator to the next element. | |
175 | * | |
176 | * @param iter the iterator | |
177 | */ | |
178 | #define cxIteratorNext(iter) (iter).base.next(&iter) | |
179 | ||
180 | /** | |
324 | 181 | * Flags the current element for removal, if this iterator is mutating. |
174 | 182 | * |
440 | 183 | * Does nothing for non-mutating iterators. |
184 | * | |
174 | 185 | * @param iter the iterator |
186 | */ | |
324 | 187 | #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating |
188 | ||
189 | /** | |
190 | * Obtains a reference to an arbitrary iterator. | |
191 | * | |
192 | * This is useful for APIs that expect some iterator as an argument. | |
193 | * | |
194 | * @param iter the iterator | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
195 | * @return (@c struct @c cx_iterator_base_s*) a pointer to the iterator |
324 | 196 | */ |
197 | #define cxIteratorRef(iter) &((iter).base) | |
174 | 198 | |
199 | /** | |
200 | * Loops over an iterator. | |
440 | 201 | * |
174 | 202 | * @param type the type of the elements |
203 | * @param elem the name of the iteration variable | |
204 | * @param iter the iterator | |
205 | */ | |
206 | #define cx_foreach(type, elem, iter) \ | |
207 | for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter)) | |
208 | ||
324 | 209 | |
210 | /** | |
211 | * Creates an iterator for the specified plain array. | |
212 | * | |
440 | 213 | * The @p array can be @c NULL in which case the iterator will be immediately |
214 | * initialized such that #cxIteratorValid() returns @c false. | |
324 | 215 | * |
440 | 216 | * This iterator yields the addresses of the array elements. |
217 | * If you want to iterator over an array of pointers, you can | |
218 | * use cxIteratorPtr() to create an iterator which directly | |
219 | * yields the stored pointers. | |
324 | 220 | * |
440 | 221 | * @param array a pointer to the array (can be @c NULL) |
324 | 222 | * @param elem_size the size of one array element |
223 | * @param elem_count the number of elements in the array | |
224 | * @return an iterator for the specified array | |
440 | 225 | * @see cxIteratorPtr() |
324 | 226 | */ |
440 | 227 | cx_attr_nodiscard |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
228 | cx_attr_export |
324 | 229 | CxIterator cxIterator( |
230 | const void *array, | |
231 | size_t elem_size, | |
232 | size_t elem_count | |
233 | ); | |
234 | ||
235 | /** | |
236 | * Creates a mutating iterator for the specified plain array. | |
237 | * | |
238 | * While the iterator is in use, the array may only be altered by removing | |
239 | * elements through #cxIteratorFlagRemoval(). Every other change to the array | |
240 | * will bring this iterator to an undefined state. | |
241 | * | |
440 | 242 | * When @p remove_keeps_order is set to @c false, removing an element will only |
324 | 243 | * move the last element to the position of the removed element, instead of |
244 | * moving all subsequent elements by one. Usually, when the order of elements is | |
440 | 245 | * not important, this parameter should be set to @c false. |
324 | 246 | * |
440 | 247 | * The @p array can be @c NULL in which case the iterator will be immediately |
248 | * initialized such that #cxIteratorValid() returns @c false. | |
324 | 249 | * |
250 | * | |
440 | 251 | * @param array a pointer to the array (can be @c NULL) |
324 | 252 | * @param elem_size the size of one array element |
253 | * @param elem_count the number of elements in the array | |
440 | 254 | * @param remove_keeps_order @c true if the order of elements must be preserved |
324 | 255 | * when removing an element |
256 | * @return an iterator for the specified array | |
257 | */ | |
440 | 258 | cx_attr_nodiscard |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
259 | cx_attr_export |
324 | 260 | CxIterator cxMutIterator( |
261 | void *array, | |
262 | size_t elem_size, | |
263 | size_t elem_count, | |
264 | bool remove_keeps_order | |
265 | ); | |
266 | ||
440 | 267 | /** |
268 | * Creates an iterator for the specified plain pointer array. | |
269 | * | |
270 | * This iterator assumes that every element in the array is a pointer | |
271 | * and yields exactly those pointers during iteration (while in contrast | |
272 | * an iterator created with cxIterator() would return the addresses | |
273 | * of those pointers within the array). | |
274 | * | |
275 | * @param array a pointer to the array (can be @c NULL) | |
276 | * @param elem_count the number of elements in the array | |
277 | * @return an iterator for the specified array | |
278 | * @see cxIterator() | |
279 | */ | |
280 | cx_attr_nodiscard | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
281 | cx_attr_export |
440 | 282 | CxIterator cxIteratorPtr( |
283 | const void *array, | |
284 | size_t elem_count | |
285 | ); | |
286 | ||
287 | /** | |
288 | * Creates a mutating iterator for the specified plain pointer array. | |
289 | * | |
290 | * This is the mutating variant of cxIteratorPtr(). See also | |
291 | * cxMutIterator(). | |
292 | * | |
293 | * @param array a pointer to the array (can be @c NULL) | |
294 | * @param elem_count the number of elements in the array | |
295 | * @param remove_keeps_order @c true if the order of elements must be preserved | |
296 | * when removing an element | |
297 | * @return an iterator for the specified array | |
298 | * @see cxMutIterator() | |
299 | * @see cxIteratorPtr() | |
300 | */ | |
301 | cx_attr_nodiscard | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
302 | cx_attr_export |
440 | 303 | CxIterator cxMutIteratorPtr( |
304 | void *array, | |
305 | size_t elem_count, | |
306 | bool remove_keeps_order | |
307 | ); | |
308 | ||
309 | #ifdef __cplusplus | |
310 | } // extern "C" | |
311 | #endif | |
312 | ||
174 | 313 | #endif // UCX_ITERATOR_H |