1.1 --- a/src/cx/collection.h Thu May 23 19:29:14 2024 +0200 1.2 +++ b/src/cx/collection.h Thu May 23 20:29:28 2024 +0200 1.3 @@ -38,6 +38,7 @@ 1.4 1.5 #include "allocator.h" 1.6 #include "iterator.h" 1.7 +#include "compare.h" 1.8 1.9 #ifdef __cplusplus 1.10 extern "C" { 1.11 @@ -48,60 +49,55 @@ 1.12 */ 1.13 #define CX_STORE_POINTERS 0 1.14 1.15 -#ifndef CX_COMPARE_FUNC_DEFINED 1.16 -#define CX_COMPARE_FUNC_DEFINED 1.17 /** 1.18 - * A comparator function comparing two collection elements. 1.19 + * Base attributes of a collection. 1.20 */ 1.21 -typedef int(*cx_compare_func)( 1.22 - void const *left, 1.23 - void const *right 1.24 -); 1.25 -#endif // CX_COMPARE_FUNC_DEFINED 1.26 +struct cx_collection_s { 1.27 + /** 1.28 + * The allocator to use. 1.29 + */ 1.30 + CxAllocator const *allocator; 1.31 + /** 1.32 + * The comparator function for the elements. 1.33 + */ 1.34 + cx_compare_func cmpfunc; 1.35 + /** 1.36 + * The size of each element. 1.37 + */ 1.38 + size_t item_size; 1.39 + /** 1.40 + * The number of currently stored elements. 1.41 + */ 1.42 + size_t size; 1.43 + /** 1.44 + * An optional simple destructor for the collection's elements. 1.45 + * 1.46 + * @attention Read the documentation of the particular collection implementation 1.47 + * whether this destructor shall only destroy the contents or also free the memory. 1.48 + */ 1.49 + cx_destructor_func simple_destructor; 1.50 + /** 1.51 + * An optional advanced destructor for the collection's elements. 1.52 + * 1.53 + * @attention Read the documentation of the particular collection implementation 1.54 + * whether this destructor shall only destroy the contents or also free the memory. 1.55 + */ 1.56 + cx_destructor_func2 advanced_destructor; 1.57 + /** 1.58 + * The pointer to additional data that is passed to the advanced destructor. 1.59 + */ 1.60 + void *destructor_data; 1.61 + /** 1.62 + * Indicates if this list is supposed to store pointers 1.63 + * instead of copies of the actual objects. 1.64 + */ 1.65 + bool store_pointer; 1.66 +}; 1.67 1.68 /** 1.69 * Use this macro to declare common members for a collection structure. 1.70 */ 1.71 -#define CX_COLLECTION_MEMBERS \ 1.72 - /** \ 1.73 - * The allocator to use. \ 1.74 - */ \ 1.75 - CxAllocator const *allocator; \ 1.76 - /** \ 1.77 - * The comparator function for the elements. \ 1.78 - */ \ 1.79 - cx_compare_func cmpfunc; \ 1.80 - /** \ 1.81 - * The size of each element. \ 1.82 - */ \ 1.83 - size_t item_size; \ 1.84 - /** \ 1.85 - * The number of currently stored elements. \ 1.86 - */ \ 1.87 - size_t size; \ 1.88 - /** \ 1.89 - * An optional simple destructor for the collection's elements. \ 1.90 - * \ 1.91 - * @attention Read the documentation of the particular collection implementation \ 1.92 - * whether this destructor shall only destroy the contents or also free the memory. \ 1.93 - */ \ 1.94 - cx_destructor_func simple_destructor; \ 1.95 - /** \ 1.96 - * An optional advanced destructor for the collection's elements. \ 1.97 - * \ 1.98 - * @attention Read the documentation of the particular collection implementation \ 1.99 - * whether this destructor shall only destroy the contents or also free the memory. \ 1.100 - */ \ 1.101 - cx_destructor_func2 advanced_destructor; \ 1.102 - /** \ 1.103 - * The pointer to additional data that is passed to the advanced destructor. \ 1.104 - */ \ 1.105 - void *destructor_data; \ 1.106 - /** \ 1.107 - * Indicates if this instance of a collection is supposed to store pointers \ 1.108 - * instead of copies of the actual objects. \ 1.109 - */ \ 1.110 - bool store_pointer; 1.111 +#define CX_COLLECTION_BASE struct cx_collection_s base 1.112 1.113 /** 1.114 * Invokes the simple destructor function for a specific element. 1.115 @@ -113,7 +109,7 @@ 1.116 * @param e the element 1.117 */ 1.118 #define cx_invoke_simple_destructor(c, e) \ 1.119 - (c)->simple_destructor((c)->store_pointer ? (*((void **) (e))) : (e)) 1.120 + (c)->base.simple_destructor((c)->base.store_pointer ? (*((void **) (e))) : (e)) 1.121 1.122 /** 1.123 * Invokes the advanced destructor function for a specific element. 1.124 @@ -125,8 +121,8 @@ 1.125 * @param e the element 1.126 */ 1.127 #define cx_invoke_advanced_destructor(c, e) \ 1.128 - (c)->advanced_destructor((c)->destructor_data, \ 1.129 - (c)->store_pointer ? (*((void **) (e))) : (e)) 1.130 + (c)->base.advanced_destructor((c)->base.destructor_data, \ 1.131 + (c)->base.store_pointer ? (*((void **) (e))) : (e)) 1.132 1.133 1.134 /** 1.135 @@ -139,8 +135,8 @@ 1.136 * @param e the element 1.137 */ 1.138 #define cx_invoke_destructor(c, e) \ 1.139 - if ((c)->simple_destructor) cx_invoke_simple_destructor(c,e); \ 1.140 - if ((c)->advanced_destructor) cx_invoke_advanced_destructor(c,e) 1.141 + if ((c)->base.simple_destructor) cx_invoke_simple_destructor(c,e); \ 1.142 + if ((c)->base.advanced_destructor) cx_invoke_advanced_destructor(c,e) 1.143 1.144 #ifdef __cplusplus 1.145 } // extern "C"