gwenhywfar  5.11.1beta
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 int GWENHYWFAR_CB (*t##_LIST_SORT_FN)(const t *a, const t *b, int ascending); \
267  typedef t* (t##_LIST_FOREACH)(t *element, void *user_data); \
268  \
269  \
270  decl t* pr##_List_First(const t##_LIST *l); \
271  decl t* pr##_List_Last(const t##_LIST *l); \
272  decl t* pr##_List_Next(const t *element); \
273  decl t* pr##_List_Previous(const t *element); \
274  decl uint32_t pr##_List_GetCount(const t##_LIST *l); \
275  decl int pr##_List_HasElement(const t##_LIST *l, const t *element); \
276  decl t##_LIST_SORT_FN pr##_List_SetSortFn(t##_LIST *l, t##_LIST_SORT_FN fn); \
277  decl void pr##_List_Sort(t##_LIST *l, int ascending); \
278  decl t* pr##_List_ForEach(t##_LIST *l, t##_LIST_FOREACH fn, void *user_data);
279 
280 
281 #define GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl) \
282  typedef GWEN_LIST1_ELEMENT t##_LIST_ELEMENT; \
283  \
284  decl void pr##_List_Clear(t##_LIST *l); \
285  decl t##_LIST* pr##_List_new(); \
286  decl void pr##_List_free(t##_LIST *l); \
287  decl int pr##_List_AddList(t##_LIST *dst, t##_LIST *l); \
288  decl int pr##_List_Add(t *element, t##_LIST *list); \
289  decl int pr##_List_Insert(t *element, t##_LIST *list); \
290  decl int pr##_List_Del(t *element);
291 
292 
293 #define GWEN_LIST_FUNCTION_DEFS_CONST(t, pr) \
294  GWEN_LIST_FUNCTION_LIB_DEFS_CONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
295 
296 #define GWEN_LIST_FUNCTION_DEFS_NOCONST(t, pr) \
297  GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
298 
299 
348 #define GWEN_LIST_FUNCTION_LIB_DEFS(t, pr, decl) \
349  GWEN_LIST_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
350  GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl)
351 
352 
357 #define GWEN_LIST_FUNCTION_DEFS(t, pr) \
358  GWEN_LIST_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
359 
360 
366 #define GWEN_LIST_FUNCTIONS(t, pr) \
367  \
368  int pr##_List_Add(t *element, t##_LIST *l) { \
369  return GWEN_List1_Add(l, element->_list1_element); \
370  } \
371  \
372  int pr##_List_AddList(t##_LIST *dst, t##_LIST *l) { \
373  return GWEN_List1_AddList(dst, l); \
374  } \
375  \
376  int pr##_List_Insert(t *element, t##_LIST *l) { \
377  return GWEN_List1_Insert(l, element->_list1_element); \
378  } \
379  \
380  int pr##_List_Del(t *element){ \
381  return GWEN_List1_Del(element->_list1_element); \
382  }\
383  \
384  t* pr##_List_First(const t##_LIST *l) { \
385  if (l) return (t*)GWEN_List1_GetFirst(l);\
386  else return 0; \
387  } \
388  \
389  t* pr##_List_Last(const t##_LIST *l) { \
390  if (l) return (t*) GWEN_List1_GetLast(l);\
391  else return 0; \
392  } \
393  \
394  void pr##_List_Clear(t##_LIST *l) { \
395  t* el; \
396  while( (el=(t*) GWEN_List1_GetFirst(l)) ) {\
397  pr##_List_Del(el);\
398  pr##_free(el);\
399  } /* while */ \
400  } \
401  \
402  int pr##_List_HasElement(const t##_LIST *l, const t *element) { \
403  const t* el; \
404  el=(t*)GWEN_List1_GetFirst(l); \
405  while(el) {\
406  if (el==element) \
407  return 1; \
408  el=(const t*)GWEN_List1Element_GetNext(el->_list1_element); \
409  } /* while */ \
410  return 0; \
411  } \
412  \
413  t##_LIST* pr##_List_new(){\
414  return (t##_LIST*)GWEN_List1_new(); \
415  }\
416  \
417  void pr##_List_free(t##_LIST *l) {\
418  if (l) { \
419  pr##_List_Clear(l);\
420  GWEN_List1_free(l); \
421  }\
422  } \
423  \
424  t* pr##_List_Next(const t *element) { \
425  return (t*)GWEN_List1Element_GetNext(element->_list1_element);\
426  } \
427  \
428  t* pr##_List_Previous(const t *element) { \
429  return (t*)GWEN_List1Element_GetPrevious(element->_list1_element);\
430  } \
431  \
432  uint32_t pr##_List_GetCount(const t##_LIST *l){\
433  return GWEN_List1_GetCount(l);\
434  } \
435  \
436  t##_LIST_SORT_FN pr##_List_SetSortFn(t##_LIST *l, t##_LIST_SORT_FN fn) { \
437  return (t##_LIST_SORT_FN) GWEN_List1_SetSortFn(l, (GWEN_LIST1_SORT_FN) fn); \
438  } \
439  \
440  void pr##_List_Sort(t##_LIST *l, int ascending){\
441  GWEN_List1_Sort(l, ascending);\
442  }\
443  \
444  t* pr##_List_ForEach(t##_LIST *l, t##_LIST_FOREACH fn, void *user_data){ \
445  t *el; \
446  if (!l) return 0; \
447  \
448  el=pr##_List_First(l); \
449  while(el) { \
450  t *elReturned; \
451  elReturned=fn(el, user_data); \
452  if (elReturned) { \
453  return elReturned; \
454  } \
455  el=pr##_List_Next(el); \
456  } \
457  return 0; \
458  }
459 
465 #define GWEN_LIST_INIT(t, element) \
466  element->_list1_element=GWEN_List1Element_new(element);
467 
468 
474 #define GWEN_LIST_FINI(t, element) \
475  if (element && element->_list1_element) { \
476  GWEN_List1Element_free(element->_list1_element); \
477  element->_list1_element=0; \
478  }
479  /* defgroup */
483 
484 
485 #ifdef __cplusplus
486 }
487 #endif
488 
489 
490 #endif
491 
492 
#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