ucx/cx/collection.h

branch
newapi
changeset 324
ce13a778654a
parent 253
087cc9216f28
equal deleted inserted replaced
323:38cb8e3992e8 324:ce13a778654a
36 #ifndef UCX_COLLECTION_H 36 #ifndef UCX_COLLECTION_H
37 #define UCX_COLLECTION_H 37 #define UCX_COLLECTION_H
38 38
39 #include "allocator.h" 39 #include "allocator.h"
40 #include "iterator.h" 40 #include "iterator.h"
41 #include "compare.h"
41 42
42 #ifdef __cplusplus 43 #ifdef __cplusplus
43 extern "C" { 44 extern "C" {
44 #endif 45 #endif
45 46
46 /** 47 /**
47 * Special constant used for creating collections that are storing pointers. 48 * Special constant used for creating collections that are storing pointers.
48 */ 49 */
49 #define CX_STORE_POINTERS 0 50 #define CX_STORE_POINTERS 0
50 51
51 #ifndef CX_COMPARE_FUNC_DEFINED
52 #define CX_COMPARE_FUNC_DEFINED
53 /** 52 /**
54 * A comparator function comparing two collection elements. 53 * Base attributes of a collection.
55 */ 54 */
56 typedef int(*cx_compare_func)( 55 struct cx_collection_s {
57 void const *left, 56 /**
58 void const *right 57 * The allocator to use.
59 ); 58 */
60 #endif // CX_COMPARE_FUNC_DEFINED 59 const CxAllocator *allocator;
60 /**
61 * The comparator function for the elements.
62 */
63 cx_compare_func cmpfunc;
64 /**
65 * The size of each element.
66 */
67 size_t elem_size;
68 /**
69 * The number of currently stored elements.
70 */
71 size_t size;
72 /**
73 * An optional simple destructor for the collection's elements.
74 *
75 * @attention Read the documentation of the particular collection implementation
76 * whether this destructor shall only destroy the contents or also free the memory.
77 */
78 cx_destructor_func simple_destructor;
79 /**
80 * An optional advanced destructor for the collection's elements.
81 *
82 * @attention Read the documentation of the particular collection implementation
83 * whether this destructor shall only destroy the contents or also free the memory.
84 */
85 cx_destructor_func2 advanced_destructor;
86 /**
87 * The pointer to additional data that is passed to the advanced destructor.
88 */
89 void *destructor_data;
90 /**
91 * Indicates if this list is supposed to store pointers
92 * instead of copies of the actual objects.
93 */
94 bool store_pointer;
95 };
61 96
62 /** 97 /**
63 * Use this macro to declare common members for a collection structure. 98 * Use this macro to declare common members for a collection structure.
64 */ 99 */
65 #define CX_COLLECTION_MEMBERS \ 100 #define CX_COLLECTION_BASE struct cx_collection_s collection
66 /** \ 101
67 * The allocator to use. \ 102 /**
68 */ \ 103 * Sets a simple destructor function for this collection.
69 CxAllocator const *allocator; \ 104 *
70 /** \ 105 * @param c the collection
71 * The comparator function for the elements. \ 106 * @param destr the destructor function
72 */ \ 107 */
73 cx_compare_func cmpfunc; \ 108 #define cxDefineDestructor(c, destr) \
74 /** \ 109 (c)->collection.simple_destructor = (cx_destructor_func) destr
75 * The size of each element. \ 110
76 */ \ 111 /**
77 size_t item_size; \ 112 * Sets a simple destructor function for this collection.
78 /** \ 113 *
79 * The number of currently stored elements. \ 114 * @param c the collection
80 */ \ 115 * @param destr the destructor function
81 size_t size; \ 116 */
82 /** \ 117 #define cxDefineAdvancedDestructor(c, destr, data) \
83 * An optional simple destructor for the collection's elements. \ 118 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \
84 * \ 119 (c)->collection.destructor_data = data
85 * @attention Read the documentation of the particular collection implementation \
86 * whether this destructor shall only destroy the contents or also free the memory. \
87 */ \
88 cx_destructor_func simple_destructor; \
89 /** \
90 * An optional advanced destructor for the collection's elements. \
91 * \
92 * @attention Read the documentation of the particular collection implementation \
93 * whether this destructor shall only destroy the contents or also free the memory. \
94 */ \
95 cx_destructor_func2 advanced_destructor; \
96 /** \
97 * The pointer to additional data that is passed to the advanced destructor. \
98 */ \
99 void *destructor_data; \
100 /** \
101 * Indicates if this instance of a collection is supposed to store pointers \
102 * instead of copies of the actual objects. \
103 */ \
104 bool store_pointer;
105 120
106 /** 121 /**
107 * Invokes the simple destructor function for a specific element. 122 * Invokes the simple destructor function for a specific element.
108 * 123 *
109 * Usually only used by collection implementations. There should be no need 124 * Usually only used by collection implementations. There should be no need
111 * 126 *
112 * @param c the collection 127 * @param c the collection
113 * @param e the element 128 * @param e the element
114 */ 129 */
115 #define cx_invoke_simple_destructor(c, e) \ 130 #define cx_invoke_simple_destructor(c, e) \
116 (c)->simple_destructor((c)->store_pointer ? (*((void **) (e))) : (e)) 131 (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e))
117 132
118 /** 133 /**
119 * Invokes the advanced destructor function for a specific element. 134 * Invokes the advanced destructor function for a specific element.
120 * 135 *
121 * Usually only used by collection implementations. There should be no need 136 * Usually only used by collection implementations. There should be no need
123 * 138 *
124 * @param c the collection 139 * @param c the collection
125 * @param e the element 140 * @param e the element
126 */ 141 */
127 #define cx_invoke_advanced_destructor(c, e) \ 142 #define cx_invoke_advanced_destructor(c, e) \
128 (c)->advanced_destructor((c)->destructor_data, \ 143 (c)->collection.advanced_destructor((c)->collection.destructor_data, \
129 (c)->store_pointer ? (*((void **) (e))) : (e)) 144 (c)->collection.store_pointer ? (*((void **) (e))) : (e))
130 145
131 146
132 /** 147 /**
133 * Invokes all available destructor functions for a specific element. 148 * Invokes all available destructor functions for a specific element.
134 * 149 *
137 * 152 *
138 * @param c the collection 153 * @param c the collection
139 * @param e the element 154 * @param e the element
140 */ 155 */
141 #define cx_invoke_destructor(c, e) \ 156 #define cx_invoke_destructor(c, e) \
142 if ((c)->simple_destructor) cx_invoke_simple_destructor(c,e); \ 157 if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \
143 if ((c)->advanced_destructor) cx_invoke_advanced_destructor(c,e) 158 if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e)
144 159
145 #ifdef __cplusplus 160 #ifdef __cplusplus
146 } // extern "C" 161 } // extern "C"
147 #endif 162 #endif
148 163

mercurial