Tue, 03 Feb 2026 19:09:53 +0100
use bool instead of WSBool in strreplace
| 490 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2023 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 | /** | |
| 579 | 29 | * @file collection.h |
| 30 | * @brief Common definitions for various collection implementations. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 490 | 34 | */ |
| 35 | ||
| 36 | #ifndef UCX_COLLECTION_H | |
| 37 | #define UCX_COLLECTION_H | |
| 38 | ||
| 39 | #include "allocator.h" | |
| 40 | #include "iterator.h" | |
| 579 | 41 | #include "compare.h" |
| 490 | 42 | |
| 43 | /** | |
| 44 | * Special constant used for creating collections that are storing pointers. | |
| 45 | */ | |
| 46 | #define CX_STORE_POINTERS 0 | |
| 47 | ||
| 48 | /** | |
| 579 | 49 | * Base attributes of a collection. |
| 490 | 50 | */ |
| 579 | 51 | struct cx_collection_s { |
| 52 | /** | |
| 53 | * The allocator to use. | |
| 54 | */ | |
| 55 | const CxAllocator *allocator; | |
| 56 | /** | |
| 57 | * The size of each element. | |
| 58 | */ | |
| 59 | size_t elem_size; | |
| 60 | /** | |
| 61 | * The number of currently stored elements. | |
| 62 | */ | |
| 63 | size_t size; | |
| 64 | /** | |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
65 | * A two-argument comparator function for the elements. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
66 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
67 | cx_compare_func simple_cmp; |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
68 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
69 | * A three-argument comparator function for the elements. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
70 | * If specified, this function has precedence over the @c simple_cmp function. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
71 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
72 | cx_compare_func2 advanced_cmp; |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
73 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
74 | * A pointer to custom data for the @c advanced_cmp function |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
75 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
76 | void *cmp_data; |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
77 | /** |
| 579 | 78 | * An optional simple destructor for the collection's elements. |
| 79 | * | |
| 80 | * @attention Read the documentation of the particular collection implementation | |
| 81 | * whether this destructor shall only destroy the contents or also free the memory. | |
| 82 | */ | |
| 83 | cx_destructor_func simple_destructor; | |
| 84 | /** | |
| 85 | * An optional advanced destructor for the collection's elements. | |
| 86 | * | |
| 87 | * @attention Read the documentation of the particular collection implementation | |
| 88 | * whether this destructor shall only destroy the contents or also free the memory. | |
| 89 | */ | |
| 90 | cx_destructor_func2 advanced_destructor; | |
| 91 | /** | |
| 92 | * The pointer to additional data that is passed to the advanced destructor. | |
| 93 | */ | |
| 94 | void *destructor_data; | |
| 95 | /** | |
| 96 | * Indicates if this list is supposed to store pointers | |
| 97 | * instead of copies of the actual objects. | |
| 98 | */ | |
| 99 | bool store_pointer; | |
| 100 | /** | |
| 101 | * Indicates if this collection is guaranteed to be sorted. | |
| 102 | * Note that the elements can still be sorted, even when the collection is not aware of that. | |
| 103 | */ | |
| 104 | bool sorted; | |
| 105 | }; | |
| 490 | 106 | |
| 107 | /** | |
| 108 | * Use this macro to declare common members for a collection structure. | |
| 579 | 109 | * |
| 110 | * @par Example Use | |
| 111 | * @code | |
| 112 | * struct MyCustomSet { | |
| 113 | * CX_COLLECTION_BASE; | |
| 114 | * MySetElements *data; | |
| 115 | * } | |
| 116 | * @endcode | |
| 490 | 117 | */ |
| 579 | 118 | #define CX_COLLECTION_BASE struct cx_collection_s collection |
| 119 | ||
| 120 | /** | |
| 121 | * Returns the number of elements currently stored. | |
| 122 | * | |
| 123 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
| 124 | * @return (@c size_t) the number of currently stored elements | |
| 125 | */ | |
| 126 | #define cxCollectionSize(c) ((c)->collection.size) | |
| 127 | ||
| 128 | /** | |
| 129 | * Returns the size of one element. | |
| 130 | * | |
| 131 | * If #cxCollectionStoresPointers() returns true, this is the size of a pointer. | |
| 132 | * | |
| 133 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
| 134 | * @return (@c size_t) the size of one element in bytes | |
| 135 | */ | |
| 136 | #define cxCollectionElementSize(c) ((c)->collection.elem_size) | |
| 137 | ||
| 138 | /** | |
| 139 | * Indicates whether this collection only stores pointers instead of the actual data. | |
| 140 | * | |
| 141 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
| 142 | * @retval true if this collection stores only pointers to data | |
| 143 | * @retval false if this collection stores the actual element's data | |
| 144 | */ | |
| 145 | #define cxCollectionStoresPointers(c) ((c)->collection.store_pointer) | |
| 146 | ||
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
147 | |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
148 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
149 | * Convenience macro for adding indirection to an element if the collection is storing pointers. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
150 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
151 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
152 | * @param elem the pointer that shall be taken the address from, if the collection is storing pointers |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
153 | * @return if the collection is storing pointers, takes the address of @p elem, otherwise returns @p elem |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
154 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
155 | #define cx_ref(c, elem) (cxCollectionStoresPointers(c) ? ((void*)&(elem)) : (elem)) |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
156 | |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
157 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
158 | * Convenience macro for dereferencing an element if the collection is storing pointers. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
159 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
160 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
161 | * @param elem a pointer to the collection element |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
162 | * @return if the collection is storing pointers, dereferences @p elem, otherwise returns @p elem |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
163 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
164 | #define cx_deref(c, elem) (cxCollectionStoresPointers(c) ? *((void**)(elem)) : (elem)) |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
165 | |
| 579 | 166 | /** |
| 167 | * Indicates whether the collection can guarantee that the stored elements are currently sorted. | |
| 168 | * | |
| 621 | 169 | * This may return @c false even when the elements are sorted. |
| 170 | * It is totally up to the implementation of the collection when to check if the elements are sorted. | |
| 171 | * It is usually a good practice to establish this property as an invariant that does not need | |
| 172 | * to be re-checked on certain operations. | |
| 579 | 173 | * |
| 174 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
| 175 | * @retval true if the elements are currently sorted wrt. the collection's compare function | |
| 176 | * @retval false if the order of elements is unknown | |
| 177 | */ | |
| 621 | 178 | #define cxCollectionSorted(c) ((c)->collection.sorted || (c)->collection.size == 0) |
| 179 | ||
| 180 | /** | |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
181 | * Sets a simple compare function for a collection. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
182 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
183 | * Erases a possible advanced compare function. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
184 | * If you want to set both, because you want to access the simple function |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
185 | * in your advanced function, you must set the simple function first. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
186 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
187 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
188 | * @param func (@c cx_compare_func) the compare function |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
189 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
190 | #define cxSetCompareFunc(c, func) \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
191 | (c)->collection.simple_cmp = (cx_compare_func)(func); \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
192 | (c)->collection.advanced_cmp = NULL |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
193 | |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
194 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
195 | * Sets an advanced compare function that supports custom data for a collection. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
196 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
197 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
198 | * @param func (@c cx_compare_func2) the compare function |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
199 | * @param data (@c void*) the pointer to custom data that is passed to the compare function |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
200 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
201 | #define cxSetAdvancedCompareFunc(c, func, data) \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
202 | (c)->collection.advanced_cmp = (cx_compare_func2) func; \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
203 | (c)->collection.cmp_data = data |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
204 | |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
205 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
206 | * Invokes the simple comparator function for two elements. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
207 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
208 | * Usually only used by collection implementations. There should be no need |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
209 | * to invoke this macro manually. |
| 621 | 210 | * |
| 211 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
212 | * @param left (@c void*) pointer to data |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
213 | * @param right (@c void*) pointer to data |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
214 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
215 | #define cx_invoke_simple_compare_func(c, left, right) \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
216 | (c)->collection.simple_cmp(left, right) |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
217 | |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
218 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
219 | * Invokes the advanced comparator function for two elements. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
220 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
221 | * Usually only used by collection implementations. There should be no need |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
222 | * to invoke this macro manually. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
223 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
224 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
225 | * @param left (@c void*) pointer to data |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
226 | * @param right (@c void*) pointer to data |
| 621 | 227 | */ |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
228 | #define cx_invoke_advanced_compare_func(c, left, right) \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
229 | (c)->collection.advanced_cmp(left, right, (c)->collection.cmp_data) |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
230 | |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
231 | |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
232 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
233 | * Invokes the configured comparator function for two elements. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
234 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
235 | * Usually only used by collection implementations. There should be no need |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
236 | * to invoke this macro manually. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
237 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
238 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
239 | * @param left (@c void*) pointer to data |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
240 | * @param right (@c void*) pointer to data |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
241 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
242 | #define cx_invoke_compare_func(c, left, right) \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
243 | (((c)->collection.advanced_cmp) ? \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
244 | cx_invoke_advanced_compare_func(c,left,right) : \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
245 | cx_invoke_simple_compare_func(c,left,right)) |
| 579 | 246 | |
| 247 | /** | |
| 248 | * Sets a simple destructor function for this collection. | |
| 249 | * | |
| 250 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
251 | * @param destr (@c cx_destructor_func) the destructor function |
| 579 | 252 | */ |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
253 | #define cxSetDestructor(c, destr) \ |
| 579 | 254 | (c)->collection.simple_destructor = (cx_destructor_func) destr |
| 255 | ||
| 256 | /** | |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
257 | * Sets an advanced destructor function for this collection. |
| 579 | 258 | * |
| 259 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
260 | * @param destr (@c cx_destructor_func2) the destructor function |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
261 | * @param data (@c void*) the additional data the advanced destructor is invoked with |
| 579 | 262 | */ |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
263 | #define cxSetAdvancedDestructor(c, destr, data) \ |
| 579 | 264 | (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ |
| 265 | (c)->collection.destructor_data = data | |
| 490 | 266 | |
| 267 | /** | |
| 268 | * Invokes the simple destructor function for a specific element. | |
| 269 | * | |
| 270 | * Usually only used by collection implementations. There should be no need | |
| 271 | * to invoke this macro manually. | |
| 272 | * | |
| 579 | 273 | * When the collection stores pointers, those pointers are directly passed |
| 274 | * to the destructor. Otherwise, a pointer to the element is passed. | |
| 275 | * | |
| 276 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
| 277 | * @param e the element (the type is @c void* or @c void** depending on context) | |
| 490 | 278 | */ |
| 279 | #define cx_invoke_simple_destructor(c, e) \ | |
| 579 | 280 | (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
| 490 | 281 | |
| 282 | /** | |
| 283 | * Invokes the advanced destructor function for a specific element. | |
| 284 | * | |
| 285 | * Usually only used by collection implementations. There should be no need | |
| 286 | * to invoke this macro manually. | |
| 287 | * | |
| 579 | 288 | * When the collection stores pointers, those pointers are directly passed |
| 289 | * to the destructor. Otherwise, a pointer to the element is passed. | |
| 290 | * | |
| 291 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
| 292 | * @param e the element (the type is @c void* or @c void** depending on context) | |
| 490 | 293 | */ |
| 294 | #define cx_invoke_advanced_destructor(c, e) \ | |
| 579 | 295 | (c)->collection.advanced_destructor((c)->collection.destructor_data, \ |
| 296 | (c)->collection.store_pointer ? (*((void **) (e))) : (e)) | |
| 490 | 297 | |
| 298 | ||
| 504 | 299 | /** |
| 300 | * Invokes all available destructor functions for a specific element. | |
| 301 | * | |
| 302 | * Usually only used by collection implementations. There should be no need | |
| 303 | * to invoke this macro manually. | |
| 304 | * | |
| 579 | 305 | * When the collection stores pointers, those pointers are directly passed |
| 306 | * to the destructor. Otherwise, a pointer to the element is passed. | |
| 307 | * | |
| 308 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE | |
| 309 | * @param e the element (the type is @c void* or @c void** depending on context) | |
| 504 | 310 | */ |
| 490 | 311 | #define cx_invoke_destructor(c, e) \ |
| 579 | 312 | if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \ |
| 313 | if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e) | |
| 490 | 314 | |
|
660
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
315 | /** |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
316 | * Invokes all available destructor functions for a specific element. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
317 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
318 | * Usually only used by collection implementations. There should be no need |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
319 | * to invoke this macro manually. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
320 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
321 | * In contrast to cx_invoke_destructor(), this macro does not automatically |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
322 | * dereference pointers to the elements when cxCollectionStoresPointers() |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
323 | * returns true. |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
324 | * |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
325 | * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
326 | * @param e pointer to the element |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
327 | */ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
328 | #define cx_invoke_destructor_raw(c, e) \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
329 | if ((c)->collection.simple_destructor) (c)->collection.simple_destructor(e); \ |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
330 | if ((c)->collection.advanced_destructor) (c)->collection.advanced_destructor((c)->collection.destructor_data, e) |
|
f00d03835dd9
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
621
diff
changeset
|
331 | |
| 490 | 332 | |
| 333 | #endif // UCX_COLLECTION_H |