gwenhywfar  5.11.2beta
list1.h
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Sat Jun 28 2003
3  copyright : (C) 2018 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 
27 #include <gwenhywfar/types.h>
28 #include <assert.h>
29 
30 
31 #ifndef GWEN_DUMMY_EMPTY_ARG
34 # define GWEN_DUMMY_EMPTY_ARG
35 #endif
36 
37 
38 #ifndef GWEN_LIST1_H
39 #define GWEN_LIST1_H
40 
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 
146 
147 
155 typedef struct GWEN_LIST1 GWEN_LIST1;
157 
158 typedef int GWENHYWFAR_CB(*GWEN_LIST1_SORT_FN)(const void *a, const void *b, int ascending);
159 
160 
164 
170 
176 
181 
187 
194 
200 
204 
208 
211 
213 void GWEN_List1_Sort(GWEN_LIST1 *l, int ascending);
214 
215 
216 
220 
224 
229 
235 
241 
242 
250 
255 #define GWEN_LIST_ELEMENT(t) \
256 GWEN_LIST1_ELEMENT *_list1_element;
257 
264 #define GWEN_LIST_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
265  /*typedef GWEN_LIST1 t##_LIST;*/ \
266  typedef struct t##_LIST t##_LIST; \
267  typedef int GWENHYWFAR_CB (*t##_LIST_SORT_FN)(const t *a, const t *b, int ascending); \
268  typedef t* (t##_LIST_FOREACH)(t *element, void *user_data); \
269  \
270  \
271  decl t* pr##_List_First(const t##_LIST *l); \
272  decl t* pr##_List_Last(const t##_LIST *l); \
273  decl t* pr##_List_Next(const t *element); \
274  decl t* pr##_List_Previous(const t *element); \
275  decl uint32_t pr##_List_GetCount(const t##_LIST *l); \
276  decl int pr##_List_HasElement(const t##_LIST *l, const t *element); \
277  decl t##_LIST_SORT_FN pr##_List_SetSortFn(t##_LIST *l, t##_LIST_SORT_FN fn); \
278  decl void pr##_List_Sort(t##_LIST *l, int ascending); \
279  decl t* pr##_List_ForEach(t##_LIST *l, t##_LIST_FOREACH fn, void *user_data);
280 
281 
282 #define GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl) \
283  typedef GWEN_LIST1_ELEMENT t##_LIST_ELEMENT; \
284  \
285  decl void pr##_List_Clear(t##_LIST *l); \
286  decl t##_LIST* pr##_List_new(); \
287  decl void pr##_List_free(t##_LIST *l); \
288  decl int pr##_List_AddList(t##_LIST *dst, t##_LIST *l); \
289  decl int pr##_List_Add(t *element, t##_LIST *list); \
290  decl int pr##_List_Insert(t *element, t##_LIST *list); \
291  decl int pr##_List_Del(t *element);
292 
293 
294 #define GWEN_LIST_FUNCTION_DEFS_CONST(t, pr) \
295  GWEN_LIST_FUNCTION_LIB_DEFS_CONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
296 
297 #define GWEN_LIST_FUNCTION_DEFS_NOCONST(t, pr) \
298  GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
299 
300 
349 #define GWEN_LIST_FUNCTION_LIB_DEFS(t, pr, decl) \
350  GWEN_LIST_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
351  GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl)
352 
353 
358 #define GWEN_LIST_FUNCTION_DEFS(t, pr) \
359  GWEN_LIST_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
360 
361 
367 #define GWEN_LIST_FUNCTIONS(t, pr) \
368  \
369  int pr##_List_Add(t *element, t##_LIST *l) { \
370  return GWEN_List1_Add((GWEN_LIST1*)l, element->_list1_element); \
371  } \
372  \
373  int pr##_List_AddList(t##_LIST *dst, t##_LIST *l) { \
374  return GWEN_List1_AddList((GWEN_LIST1*)dst, (GWEN_LIST1*)l); \
375  } \
376  \
377  int pr##_List_Insert(t *element, t##_LIST *l) { \
378  return GWEN_List1_Insert((GWEN_LIST1*)l, element->_list1_element); \
379  } \
380  \
381  int pr##_List_Del(t *element){ \
382  return GWEN_List1_Del(element->_list1_element); \
383  }\
384  \
385  t* pr##_List_First(const t##_LIST *l) { \
386  if (l) return (t*)GWEN_List1_GetFirst((GWEN_LIST1*)l);\
387  else return 0; \
388  } \
389  \
390  t* pr##_List_Last(const t##_LIST *l) { \
391  if (l) return (t*) GWEN_List1_GetLast((GWEN_LIST1*)l);\
392  else return 0; \
393  } \
394  \
395  void pr##_List_Clear(t##_LIST *l) { \
396  t* el; \
397  while( (el=(t*) GWEN_List1_GetFirst((GWEN_LIST1*)l)) ) {\
398  pr##_List_Del(el);\
399  pr##_free(el);\
400  } /* while */ \
401  } \
402  \
403  int pr##_List_HasElement(const t##_LIST *l, const t *element) { \
404  const t* el; \
405  el=(t*)GWEN_List1_GetFirst((GWEN_LIST1*)l); \
406  while(el) {\
407  if (el==element) \
408  return 1; \
409  el=(const t*)GWEN_List1Element_GetNext(el->_list1_element); \
410  } /* while */ \
411  return 0; \
412  } \
413  \
414  t##_LIST* pr##_List_new(){\
415  return (t##_LIST*)GWEN_List1_new(); \
416  }\
417  \
418  void pr##_List_free(t##_LIST *l) {\
419  if (l) { \
420  pr##_List_Clear(l);\
421  GWEN_List1_free((GWEN_LIST1*)l); \
422  }\
423  } \
424  \
425  t* pr##_List_Next(const t *element) { \
426  return (t*)GWEN_List1Element_GetNext(element->_list1_element);\
427  } \
428  \
429  t* pr##_List_Previous(const t *element) { \
430  return (t*)GWEN_List1Element_GetPrevious(element->_list1_element);\
431  } \
432  \
433  uint32_t pr##_List_GetCount(const t##_LIST *l){\
434  return GWEN_List1_GetCount((GWEN_LIST1*)l);\
435  } \
436  \
437  t##_LIST_SORT_FN pr##_List_SetSortFn(t##_LIST *l, t##_LIST_SORT_FN fn) { \
438  return (t##_LIST_SORT_FN) GWEN_List1_SetSortFn((GWEN_LIST1*)l, (GWEN_LIST1_SORT_FN) fn); \
439  } \
440  \
441  void pr##_List_Sort(t##_LIST *l, int ascending){\
442  GWEN_List1_Sort((GWEN_LIST1*)l, ascending);\
443  }\
444  \
445  t* pr##_List_ForEach(t##_LIST *l, t##_LIST_FOREACH fn, void *user_data){ \
446  t *el; \
447  if (!l) return 0; \
448  \
449  el=pr##_List_First(l); \
450  while(el) { \
451  t *elReturned; \
452  elReturned=fn(el, user_data); \
453  if (elReturned) { \
454  return elReturned; \
455  } \
456  el=pr##_List_Next(el); \
457  } \
458  return 0; \
459  }
460 
466 #define GWEN_LIST_INIT(t, element) \
467  element->_list1_element=GWEN_List1Element_new(element);
468 
469 
475 #define GWEN_LIST_FINI(t, element) \
476  if (element && element->_list1_element) { \
477  GWEN_List1Element_free(element->_list1_element); \
478  element->_list1_element=0; \
479  }
480  /* defgroup */
484 
485 
486 #ifdef __cplusplus
487 }
488 #endif
489 
490 
491 #endif
492 
493 
#define GWENHYWFAR_API
Definition: gwenhywfarapi.h:67
#define GWENHYWFAR_CB
Definition: gwenhywfarapi.h:89
GWENHYWFAR_API void * GWEN_List1_GetLast(const GWEN_LIST1 *l)
GWENHYWFAR_API void * GWEN_List1Element_GetNext(const GWEN_LIST1_ELEMENT *el)
GWENHYWFAR_API void GWEN_List1_free(GWEN_LIST1 *l)
GWENHYWFAR_API GWEN_LIST1_ELEMENT * GWEN_List1Element_new(void *d)
GWENHYWFAR_API int GWEN_List1_Insert(GWEN_LIST1 *l, GWEN_LIST1_ELEMENT *el)
GWENHYWFAR_API int GWEN_List1_AddList(GWEN_LIST1 *dest, GWEN_LIST1 *l)
GWENHYWFAR_API int GWEN_List1_Del(GWEN_LIST1_ELEMENT *el)
GWENHYWFAR_API void * GWEN_List1Element_GetPrevious(const GWEN_LIST1_ELEMENT *el)
int GWENHYWFAR_CB(* GWEN_LIST1_SORT_FN)(const void *a, const void *b, int ascending)
Definition: list1.h:158
GWENHYWFAR_API int GWEN_List1_GetCount(const GWEN_LIST1 *l)
struct GWEN_LIST1 GWEN_LIST1
Definition: list1.h:155
GWENHYWFAR_API int GWEN_List1_Add(GWEN_LIST1 *l, GWEN_LIST1_ELEMENT *el)
GWENHYWFAR_API GWEN_LIST1 * GWEN_List1_new(void)
GWENHYWFAR_API GWEN_LIST1_SORT_FN GWEN_List1_SetSortFn(GWEN_LIST1 *l, GWEN_LIST1_SORT_FN fn)
GWENHYWFAR_API void GWEN_List1_Sort(GWEN_LIST1 *l, int ascending)
GWENHYWFAR_API void * GWEN_List1Element_GetData(const GWEN_LIST1_ELEMENT *el)
GWENHYWFAR_API void GWEN_List1Element_free(GWEN_LIST1_ELEMENT *el)
GWENHYWFAR_API void * GWEN_List1_GetFirst(const GWEN_LIST1 *l)
struct GWEN_LIST1_ELEMENT GWEN_LIST1_ELEMENT
Definition: list1.h:156