Sun, 07 Dec 2025 19:20:48 +0100
implement radiobutton (Client)
| 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 *); |
| 870 | 53 | /** |
| 54 | * Original implementation in case the function needs to be wrapped. | |
| 55 | */ | |
| 56 | bool (*valid_impl)(const void *); | |
| 174 | 57 | /** |
| 58 | * Returns a pointer to the current element. | |
| 59 | * | |
| 60 | * When valid returns false, the behavior of this function is undefined. | |
| 61 | */ | |
| 324 | 62 | void *(*current)(const void *); |
| 174 | 63 | |
| 64 | /** | |
| 65 | * Original implementation in case the function needs to be wrapped. | |
| 66 | */ | |
| 324 | 67 | void *(*current_impl)(const void *); |
| 174 | 68 | /** |
| 69 | * Advances the iterator. | |
| 70 | * | |
| 71 | * When valid returns false, the behavior of this function is undefined. | |
| 72 | */ | |
| 73 | void (*next)(void *); | |
| 74 | /** | |
| 845 | 75 | * Original implementation in case the function needs to be wrapped. |
| 76 | */ | |
| 77 | void (*next_impl)(void *); | |
| 78 | /** | |
| 174 | 79 | * Indicates whether this iterator may remove elements. |
| 80 | */ | |
| 870 | 81 | bool allow_remove; |
| 174 | 82 | /** |
| 83 | * Internal flag for removing the current element when advancing. | |
| 84 | */ | |
| 85 | bool remove; | |
| 86 | }; | |
| 87 | ||
| 88 | /** | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
89 | * 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
|
90 | * @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
|
91 | */ |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
92 | 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
|
93 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
94 | /** |
| 324 | 95 | * Declares base attributes for an iterator. |
| 96 | * Must be the first member of an iterator structure. | |
| 174 | 97 | */ |
| 324 | 98 | #define CX_ITERATOR_BASE struct cx_iterator_base_s base |
| 99 | ||
| 100 | /** | |
| 101 | * Internal iterator struct - use CxIterator. | |
| 102 | */ | |
| 103 | struct cx_iterator_s { | |
| 440 | 104 | /** |
| 105 | * Inherited common data for all iterators. | |
| 106 | */ | |
| 324 | 107 | CX_ITERATOR_BASE; |
| 174 | 108 | |
| 109 | /** | |
| 324 | 110 | * Handle for the current element. |
| 174 | 111 | */ |
| 112 | void *elem_handle; | |
| 113 | ||
| 114 | /** | |
| 115 | * Handle for the source collection, if any. | |
| 116 | */ | |
| 870 | 117 | void *src_handle; |
| 174 | 118 | |
| 119 | /** | |
| 120 | * If the iterator is position-aware, contains the index of the element in the underlying collection. | |
| 121 | * Otherwise, this field is usually uninitialized. | |
| 122 | */ | |
| 123 | size_t index; | |
| 324 | 124 | |
| 125 | /** | |
| 126 | * The size of an individual element. | |
| 127 | */ | |
| 128 | size_t elem_size; | |
| 129 | ||
| 130 | /** | |
| 131 | * May contain the total number of elements, if known. | |
| 440 | 132 | * Shall be set to @c SIZE_MAX when the total number is unknown during iteration. |
| 324 | 133 | */ |
| 134 | size_t elem_count; | |
| 174 | 135 | }; |
| 136 | ||
| 137 | /** | |
| 324 | 138 | * Iterator type. |
| 174 | 139 | * |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
140 | * An iterator points to a certain element in a (possibly unbounded) chain of elements. |
| 845 | 141 | * Iterators that are based on collections (which have a defined "first" element) are supposed |
| 174 | 142 | * to be "position-aware", which means that they keep track of the current index within the collection. |
| 143 | * | |
| 144 | * @note Objects that are pointed to by an iterator are always mutable through that iterator. However, | |
| 870 | 145 | * any concurrent mutation of the collection other than by this iterator makes this iterator obsolete, |
| 324 | 146 | * and it must not be used anymore. |
| 174 | 147 | */ |
| 148 | typedef struct cx_iterator_s CxIterator; | |
| 149 | ||
| 150 | /** | |
| 151 | * Checks if the iterator points to valid data. | |
| 152 | * | |
| 153 | * @param iter the iterator | |
| 440 | 154 | * @retval true if the iterator points to valid data |
| 155 | * @retval false if the iterator already moved past the end | |
| 174 | 156 | */ |
| 157 | #define cxIteratorValid(iter) (iter).base.valid(&(iter)) | |
| 158 | ||
| 159 | /** | |
| 160 | * Returns a pointer to the current element. | |
| 161 | * | |
| 162 | * The behavior is undefined if this iterator is invalid. | |
| 163 | * | |
| 164 | * @param iter the iterator | |
| 165 | * @return a pointer to the current element | |
| 440 | 166 | * @see cxIteratorValid() |
| 174 | 167 | */ |
| 168 | #define cxIteratorCurrent(iter) (iter).base.current(&iter) | |
| 169 | ||
| 170 | /** | |
| 171 | * Advances the iterator to the next element. | |
| 172 | * | |
| 173 | * @param iter the iterator | |
| 174 | */ | |
| 175 | #define cxIteratorNext(iter) (iter).base.next(&iter) | |
| 176 | ||
| 177 | /** | |
| 870 | 178 | * Flags the current element for removal if the iterator allows it. |
| 440 | 179 | * |
| 174 | 180 | * @param iter the iterator |
| 870 | 181 | * @return @c true if removal is allowed, @c false otherwise |
| 174 | 182 | */ |
| 870 | 183 | #define cxIteratorFlagRemoval(iter) ((iter).base.remove = (iter).base.allow_remove) |
| 324 | 184 | |
| 185 | /** | |
| 186 | * Obtains a reference to an arbitrary iterator. | |
| 187 | * | |
| 188 | * This is useful for APIs that expect some iterator as an argument. | |
| 189 | * | |
| 190 | * @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
|
191 | * @return (@c struct @c cx_iterator_base_s*) a pointer to the iterator |
| 324 | 192 | */ |
| 193 | #define cxIteratorRef(iter) &((iter).base) | |
| 174 | 194 | |
| 195 | /** | |
| 196 | * Loops over an iterator. | |
| 440 | 197 | * |
| 174 | 198 | * @param type the type of the elements |
| 199 | * @param elem the name of the iteration variable | |
| 200 | * @param iter the iterator | |
| 201 | */ | |
| 202 | #define cx_foreach(type, elem, iter) \ | |
| 203 | for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter)) | |
| 204 | ||
| 324 | 205 | |
| 206 | /** | |
| 207 | * Creates an iterator for the specified plain array. | |
| 208 | * | |
| 845 | 209 | * The @p array can be @c NULL, in which case the iterator will be immediately |
| 440 | 210 | * initialized such that #cxIteratorValid() returns @c false. |
| 324 | 211 | * |
| 440 | 212 | * This iterator yields the addresses of the array elements. |
| 213 | * If you want to iterator over an array of pointers, you can | |
| 214 | * use cxIteratorPtr() to create an iterator which directly | |
| 215 | * yields the stored pointers. | |
| 324 | 216 | * |
| 870 | 217 | * While the iterator is in use, the array may only be altered by removing |
| 218 | * elements through #cxIteratorFlagRemoval(). Every other change to the array | |
| 219 | * will bring this iterator to an undefined state. | |
| 220 | * | |
| 221 | * When @p remove_keeps_order is set to @c false, removing an element will only | |
| 222 | * move the last element to the position of the removed element, instead of | |
| 223 | * moving all subsequent elements by one. Usually, when the order of elements is | |
| 224 | * not important, this parameter should be set to @c false. | |
| 225 | * | |
| 440 | 226 | * @param array a pointer to the array (can be @c NULL) |
| 324 | 227 | * @param elem_size the size of one array element |
| 228 | * @param elem_count the number of elements in the array | |
| 870 | 229 | * @param remove_keeps_order @c true if the order of elements must be preserved |
| 230 | * when removing an element | |
| 324 | 231 | * @return an iterator for the specified array |
| 440 | 232 | * @see cxIteratorPtr() |
| 324 | 233 | */ |
| 440 | 234 | cx_attr_nodiscard |
| 870 | 235 | CX_EXPORT CxIterator cxIterator(const void *array, |
| 236 | size_t elem_size, size_t elem_count, bool remove_keeps_order); | |
| 324 | 237 | |
| 238 | /** | |
| 870 | 239 | * Creates an iterator for the specified plain pointer array. |
| 240 | * | |
| 241 | * This iterator assumes that every element in the array is a pointer | |
| 242 | * and yields exactly those pointers during iteration (on the other | |
| 243 | * hand, an iterator created with cxIterator() would return the | |
| 244 | * addresses of those pointers within the array). | |
| 324 | 245 | * |
| 246 | * While the iterator is in use, the array may only be altered by removing | |
| 247 | * elements through #cxIteratorFlagRemoval(). Every other change to the array | |
| 248 | * will bring this iterator to an undefined state. | |
| 249 | * | |
| 440 | 250 | * When @p remove_keeps_order is set to @c false, removing an element will only |
| 324 | 251 | * move the last element to the position of the removed element, instead of |
| 252 | * moving all subsequent elements by one. Usually, when the order of elements is | |
| 440 | 253 | * not important, this parameter should be set to @c false. |
| 324 | 254 | * |
| 440 | 255 | * @param array a pointer to the array (can be @c NULL) |
| 256 | * @param elem_count the number of elements in the array | |
| 257 | * @param remove_keeps_order @c true if the order of elements must be preserved | |
| 258 | * when removing an element | |
| 259 | * @return an iterator for the specified array | |
| 870 | 260 | * @see cxIterator() |
| 440 | 261 | */ |
| 262 | cx_attr_nodiscard | |
| 870 | 263 | CX_EXPORT CxIterator cxIteratorPtr(const void *array, size_t elem_count, |
| 264 | bool remove_keeps_order); | |
| 440 | 265 | |
| 266 | #ifdef __cplusplus | |
| 267 | } // extern "C" | |
| 268 | #endif | |
| 269 | ||
| 174 | 270 | #endif // UCX_ITERATOR_H |