src/list.c

changeset 677
b09aae58bba4
parent 666
b5dd654deb3b
child 680
19379743e5a0
equal deleted inserted replaced
676:d0680a23d850 677:b09aae58bba4
30 30
31 #include <string.h> 31 #include <string.h>
32 32
33 // <editor-fold desc="Store Pointers Functionality"> 33 // <editor-fold desc="Store Pointers Functionality">
34 34
35 static _Thread_local CxListComparator cx_pl_cmpfunc_impl; 35 static _Thread_local cx_compare_func cx_pl_cmpfunc_impl;
36 36
37 static int cx_pl_cmpfunc( 37 static int cx_pl_cmpfunc(
38 void const *l, 38 void const *l,
39 void const *r 39 void const *r
40 ) { 40 ) {
177 cx_pl_reverse, 177 cx_pl_reverse,
178 cx_pl_iterator, 178 cx_pl_iterator,
179 }; 179 };
180 180
181 void cxListStoreObjects(CxList *list) { 181 void cxListStoreObjects(CxList *list) {
182 list->store_pointer = false;
182 if (list->climpl != NULL) { 183 if (list->climpl != NULL) {
183 list->cl = list->climpl; 184 list->cl = list->climpl;
184 list->climpl = NULL; 185 list->climpl = NULL;
185 } 186 }
186 } 187 }
187 188
188 void cxListStorePointers(CxList *list) { 189 void cxListStorePointers(CxList *list) {
189 list->itemsize = sizeof(void *); 190 list->item_size = sizeof(void *);
191 list->store_pointer = true;
190 list->climpl = list->cl; 192 list->climpl = list->cl;
191 list->cl = &cx_pointer_list_class; 193 list->cl = &cx_pointer_list_class;
192 } 194 }
193 195
194 bool cxListIsStoringPointers(CxList const *list) {
195 return list->climpl != NULL;
196 }
197
198 // </editor-fold> 196 // </editor-fold>
199 197
200 void cx_list_invoke_destructor( 198 void cxListDestroy(CxList *list) {
201 CxList const *list, 199 if (list->simple_destructor) {
202 void *elem 200 CxIterator iter = cxListIterator(list);
203 ) { 201 cx_foreach(void*, elem, iter) {
204 switch (list->content_destructor_type) { 202 // already correctly resolved pointer - immediately invoke dtor
205 case CX_DESTRUCTOR_SIMPLE: { 203 list->simple_destructor(elem);
206 cx_list_invoke_simple_destructor(list, elem);
207 break;
208 } 204 }
209 case CX_DESTRUCTOR_ADVANCED: { 205 }
210 cx_list_invoke_advanced_destructor(list, elem); 206 if (list->advanced_destructor) {
211 break; 207 CxIterator iter = cxListIterator(list);
208 cx_foreach(void*, elem, iter) {
209 // already correctly resolved pointer - immediately invoke dtor
210 list->advanced_destructor(list->destructor_data, elem);
212 } 211 }
213 case CX_DESTRUCTOR_NONE:
214 break; // nothing
215 }
216 }
217
218 void cx_list_invoke_simple_destructor(
219 CxList const *list,
220 void *elem
221 ) {
222 if (cxListIsStoringPointers(list)) {
223 elem = *((void **) elem);
224 }
225 list->simple_destructor(elem);
226 }
227
228 void cx_list_invoke_advanced_destructor(
229 CxList const *list,
230 void *elem
231 ) {
232 if (cxListIsStoringPointers(list)) {
233 elem = *((void **) elem);
234 }
235 list->advanced_destructor.func(list->advanced_destructor.data, elem);
236 }
237
238 void cxListDestroy(CxList *list) {
239 switch (list->content_destructor_type) {
240 case CX_DESTRUCTOR_SIMPLE: {
241 CxIterator iter = cxListIterator(list);
242 cx_foreach(void*, elem, iter) {
243 // already correctly resolved pointer - immediately invoke dtor
244 list->simple_destructor(elem);
245 }
246 break;
247 }
248 case CX_DESTRUCTOR_ADVANCED: {
249 CxIterator iter = cxListIterator(list);
250 cx_foreach(void*, elem, iter) {
251 // already correctly resolved pointer - immediately invoke dtor
252 list->advanced_destructor.func(list->advanced_destructor.data, elem);
253 }
254 break;
255 }
256 case CX_DESTRUCTOR_NONE:
257 break; // nothing
258 } 212 }
259 213
260 list->cl->destructor(list); 214 list->cl->destructor(list);
261 cxFree(list->allocator, list); 215 cxFree(list->allocator, list);
262 } 216 }

mercurial