48 * Special constant used for creating collections that are storing pointers. |
48 * Special constant used for creating collections that are storing pointers. |
49 */ |
49 */ |
50 #define CX_STORE_POINTERS 0 |
50 #define CX_STORE_POINTERS 0 |
51 |
51 |
52 /** |
52 /** |
53 * A comparator function comparing two collection elements. |
53 * Base attributes of a collection. |
54 */ |
54 */ |
55 typedef int(*cx_compare_func)( |
55 struct cx_collection_s { |
56 void const *left, |
56 /** |
57 void const *right |
57 * The allocator to use. |
58 ); |
58 */ |
|
59 CxAllocator const *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 }; |
59 |
96 |
60 /** |
97 /** |
61 * Use this macro to declare common members for a collection structure. |
98 * Use this macro to declare common members for a collection structure. |
62 */ |
99 */ |
63 #define CX_COLLECTION_MEMBERS \ |
100 #define CX_COLLECTION_BASE struct cx_collection_s collection |
64 /** \ |
101 |
65 * The allocator to use. \ |
102 /** |
66 */ \ |
103 * Sets a simple destructor function for this collection. |
67 CxAllocator const *allocator; \ |
104 * |
68 /** \ |
105 * @param c the collection |
69 * The comparator function for the elements. \ |
106 * @param destr the destructor function |
70 */ \ |
107 */ |
71 cx_compare_func cmpfunc; \ |
108 #define cxDefineDestructor(c, destr) \ |
72 /** \ |
109 (c)->collection.simple_destructor = (cx_destructor_func) destr |
73 * The size of each element. \ |
110 |
74 */ \ |
111 /** |
75 size_t item_size; \ |
112 * Sets a simple destructor function for this collection. |
76 /** \ |
113 * |
77 * The number of currently stored elements. \ |
114 * @param c the collection |
78 */ \ |
115 * @param destr the destructor function |
79 size_t size; \ |
116 */ |
80 /** \ |
117 #define cxDefineAdvancedDestructor(c, destr, data) \ |
81 * An optional simple destructor for the collection's elements. \ |
118 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ |
82 * \ |
119 (c)->collection.destructor_data = data |
83 * @attention Read the documentation of the particular collection implementation \ |
|
84 * whether this destructor shall only destroy the contents or also free the memory. \ |
|
85 */ \ |
|
86 cx_destructor_func simple_destructor; \ |
|
87 /** \ |
|
88 * An optional advanced destructor for the collection's elements. \ |
|
89 * \ |
|
90 * @attention Read the documentation of the particular collection implementation \ |
|
91 * whether this destructor shall only destroy the contents or also free the memory. \ |
|
92 */ \ |
|
93 cx_destructor_func2 advanced_destructor; \ |
|
94 /** \ |
|
95 * The pointer to additional data that is passed to the advanced destructor. \ |
|
96 */ \ |
|
97 void *destructor_data; \ |
|
98 /** \ |
|
99 * Indicates if this instance of a collection is supposed to store pointers \ |
|
100 * instead of copies of the actual objects. \ |
|
101 */ \ |
|
102 bool store_pointer; |
|
103 |
120 |
104 /** |
121 /** |
105 * Invokes the simple destructor function for a specific element. |
122 * Invokes the simple destructor function for a specific element. |
106 * |
123 * |
107 * Usually only used by collection implementations. There should be no need |
124 * Usually only used by collection implementations. There should be no need |
121 * |
138 * |
122 * @param c the collection |
139 * @param c the collection |
123 * @param e the element |
140 * @param e the element |
124 */ |
141 */ |
125 #define cx_invoke_advanced_destructor(c, e) \ |
142 #define cx_invoke_advanced_destructor(c, e) \ |
126 (c)->advanced_destructor((c)->destructor_data, \ |
143 (c)->collection.advanced_destructor((c)->collection.destructor_data, \ |
127 (c)->store_pointer ? (*((void **) (e))) : (e)) |
144 (c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
128 |
145 |
129 |
146 |
130 /** |
147 /** |
131 * Invokes all available destructor functions for a specific element. |
148 * Invokes all available destructor functions for a specific element. |
132 * |
149 * |