| 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
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 |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
| 27 */ |
27 */ |
| 28 /** |
28 /** |
| 29 * \file collection.h |
29 * @file collection.h |
| 30 * \brief Common definitions for various collection implementations. |
30 * @brief Common definitions for various collection implementations. |
| 31 * \author Mike Becker |
31 * @author Mike Becker |
| 32 * \author Olaf Wintermann |
32 * @author Olaf Wintermann |
| 33 * \copyright 2-Clause BSD License |
33 * @copyright 2-Clause BSD License |
| 34 */ |
34 */ |
| 35 |
35 |
| 36 #ifndef UCX_COLLECTION_H |
36 #ifndef UCX_COLLECTION_H |
| 37 #define UCX_COLLECTION_H |
37 #define UCX_COLLECTION_H |
| 38 |
38 |
| 90 /** |
90 /** |
| 91 * Indicates if this list is supposed to store pointers |
91 * Indicates if this list is supposed to store pointers |
| 92 * instead of copies of the actual objects. |
92 * instead of copies of the actual objects. |
| 93 */ |
93 */ |
| 94 bool store_pointer; |
94 bool store_pointer; |
| |
95 /** |
| |
96 * Indicates if this collection is guaranteed to be sorted. |
| |
97 * Note that the elements can still be sorted, even when the collection is not aware of that. |
| |
98 */ |
| |
99 bool sorted; |
| 95 }; |
100 }; |
| 96 |
101 |
| 97 /** |
102 /** |
| 98 * Use this macro to declare common members for a collection structure. |
103 * Use this macro to declare common members for a collection structure. |
| |
104 * |
| |
105 * @par Example Use |
| |
106 * @code |
| |
107 * struct MyCustomSet { |
| |
108 * CX_COLLECTION_BASE; |
| |
109 * MySetElements *data; |
| |
110 * } |
| |
111 * @endcode |
| 99 */ |
112 */ |
| 100 #define CX_COLLECTION_BASE struct cx_collection_s collection |
113 #define CX_COLLECTION_BASE struct cx_collection_s collection |
| 101 |
114 |
| 102 /** |
115 /** |
| |
116 * Returns the number of elements currently stored. |
| |
117 * |
| |
118 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| |
119 * @return (@c size_t) the number of currently stored elements |
| |
120 */ |
| |
121 #define cxCollectionSize(c) ((c)->collection.size) |
| |
122 |
| |
123 /** |
| |
124 * Returns the size of one element. |
| |
125 * |
| |
126 * If #cxCollectionStoresPointers() returns true, this is the size of a pointer. |
| |
127 * |
| |
128 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| |
129 * @return (@c size_t) the size of one element in bytes |
| |
130 */ |
| |
131 #define cxCollectionElementSize(c) ((c)->collection.elem_size) |
| |
132 |
| |
133 /** |
| |
134 * Indicates whether this collection only stores pointers instead of the actual data. |
| |
135 * |
| |
136 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| |
137 * @retval true if this collection stores only pointers to data |
| |
138 * @retval false if this collection stores the actual element's data |
| |
139 */ |
| |
140 #define cxCollectionStoresPointers(c) ((c)->collection.store_pointer) |
| |
141 |
| |
142 /** |
| |
143 * Indicates whether the collection can guarantee that the stored elements are currently sorted. |
| |
144 * |
| |
145 * This may return false even when the elements are sorted. |
| |
146 * It is totally up to the implementation of the collection whether it keeps track of the order of its elements. |
| |
147 * |
| |
148 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| |
149 * @retval true if the elements are currently sorted wrt. the collection's compare function |
| |
150 * @retval false if the order of elements is unknown |
| |
151 */ |
| |
152 #define cxCollectionSorted(c) ((c)->collection.sorted) |
| |
153 |
| |
154 /** |
| 103 * Sets a simple destructor function for this collection. |
155 * Sets a simple destructor function for this collection. |
| 104 * |
156 * |
| 105 * @param c the collection |
157 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| 106 * @param destr the destructor function |
158 * @param destr the destructor function |
| 107 */ |
159 */ |
| 108 #define cxDefineDestructor(c, destr) \ |
160 #define cxDefineDestructor(c, destr) \ |
| 109 (c)->collection.simple_destructor = (cx_destructor_func) destr |
161 (c)->collection.simple_destructor = (cx_destructor_func) destr |
| 110 |
162 |
| 111 /** |
163 /** |
| 112 * Sets a simple destructor function for this collection. |
164 * Sets a simple destructor function for this collection. |
| 113 * |
165 * |
| 114 * @param c the collection |
166 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| 115 * @param destr the destructor function |
167 * @param destr the destructor function |
| 116 */ |
168 */ |
| 117 #define cxDefineAdvancedDestructor(c, destr, data) \ |
169 #define cxDefineAdvancedDestructor(c, destr, data) \ |
| 118 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ |
170 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ |
| 119 (c)->collection.destructor_data = data |
171 (c)->collection.destructor_data = data |
| 122 * Invokes the simple destructor function for a specific element. |
174 * Invokes the simple destructor function for a specific element. |
| 123 * |
175 * |
| 124 * Usually only used by collection implementations. There should be no need |
176 * Usually only used by collection implementations. There should be no need |
| 125 * to invoke this macro manually. |
177 * to invoke this macro manually. |
| 126 * |
178 * |
| 127 * @param c the collection |
179 * When the collection stores pointers, those pointers are directly passed |
| 128 * @param e the element |
180 * to the destructor. Otherwise, a pointer to the element is passed. |
| |
181 * |
| |
182 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| |
183 * @param e the element (the type is @c void* or @c void** depending on context) |
| 129 */ |
184 */ |
| 130 #define cx_invoke_simple_destructor(c, e) \ |
185 #define cx_invoke_simple_destructor(c, e) \ |
| 131 (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
186 (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
| 132 |
187 |
| 133 /** |
188 /** |
| 134 * Invokes the advanced destructor function for a specific element. |
189 * Invokes the advanced destructor function for a specific element. |
| 135 * |
190 * |
| 136 * Usually only used by collection implementations. There should be no need |
191 * Usually only used by collection implementations. There should be no need |
| 137 * to invoke this macro manually. |
192 * to invoke this macro manually. |
| 138 * |
193 * |
| 139 * @param c the collection |
194 * When the collection stores pointers, those pointers are directly passed |
| 140 * @param e the element |
195 * to the destructor. Otherwise, a pointer to the element is passed. |
| |
196 * |
| |
197 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| |
198 * @param e the element (the type is @c void* or @c void** depending on context) |
| 141 */ |
199 */ |
| 142 #define cx_invoke_advanced_destructor(c, e) \ |
200 #define cx_invoke_advanced_destructor(c, e) \ |
| 143 (c)->collection.advanced_destructor((c)->collection.destructor_data, \ |
201 (c)->collection.advanced_destructor((c)->collection.destructor_data, \ |
| 144 (c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
202 (c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
| 145 |
203 |
| 148 * Invokes all available destructor functions for a specific element. |
206 * Invokes all available destructor functions for a specific element. |
| 149 * |
207 * |
| 150 * Usually only used by collection implementations. There should be no need |
208 * Usually only used by collection implementations. There should be no need |
| 151 * to invoke this macro manually. |
209 * to invoke this macro manually. |
| 152 * |
210 * |
| 153 * @param c the collection |
211 * When the collection stores pointers, those pointers are directly passed |
| 154 * @param e the element |
212 * to the destructor. Otherwise, a pointer to the element is passed. |
| |
213 * |
| |
214 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
| |
215 * @param e the element (the type is @c void* or @c void** depending on context) |
| 155 */ |
216 */ |
| 156 #define cx_invoke_destructor(c, e) \ |
217 #define cx_invoke_destructor(c, e) \ |
| 157 if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \ |
218 if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \ |
| 158 if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e) |
219 if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e) |
| 159 |
220 |