FreeTDS API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
dlist.tmpl.h
1 /* Dlist - dynamic list
2  * Copyright (C) 2016 Frediano Ziglio
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
20 #include <freetds/bool.h>
21 #include <freetds/utils.h>
22 
23 #if !defined(DLIST_PREFIX) || !defined(DLIST_ITEM_TYPE) || !defined(DLIST_LIST_TYPE)
24 #error Required defines for dlist missing!
25 #endif
26 
27 #if defined(DLIST_NAME) || defined(DLIST_PASTER) || \
28  defined(DLIST_EVALUATOR) || defined(DLIST_ITEM)
29 #error Some internal dlist macros already defined
30 #endif
31 
32 typedef struct
33 {
34  dlist_ring ring;
36 
37 #define DLIST_PASTER(x,y) x ## _ ## y
38 #define DLIST_EVALUATOR(x,y) DLIST_PASTER(x,y)
39 #define DLIST_NAME(suffix) DLIST_EVALUATOR(DLIST_PREFIX, suffix)
40 #define DLIST_ITEM(ring) \
41  ((DLIST_ITEM_TYPE *) (((char *) (ring)) - TDS_OFFSET(DLIST_ITEM_TYPE, DLIST_NAME(item))))
42 
43 static inline void DLIST_NAME(check)(DLIST_LIST_TYPE *list)
44 {
45 #if ENABLE_EXTRA_CHECKS
46  assert(list != NULL);
47  dlist_ring_check(&list->ring);
48 #endif
49 }
50 
51 static inline void DLIST_NAME(init)(DLIST_LIST_TYPE *list)
52 {
53  list->ring.next = list->ring.prev = &list->ring;
54  DLIST_NAME(check)(list);
55 }
56 
57 static inline DLIST_ITEM_TYPE *DLIST_NAME(first)(DLIST_LIST_TYPE *list)
58 {
59  return list->ring.next == &list->ring ? NULL : DLIST_ITEM(list->ring.next);
60 }
61 
62 static inline DLIST_ITEM_TYPE *DLIST_NAME(last)(DLIST_LIST_TYPE *list)
63 {
64  return list->ring.prev == &list->ring ? NULL : DLIST_ITEM(list->ring.prev);
65 }
66 
67 static inline DLIST_ITEM_TYPE *DLIST_NAME(next)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
68 {
69  return item->DLIST_NAME(item).next == &list->ring ? NULL : DLIST_ITEM(item->DLIST_NAME(item).next);
70 }
71 
72 static inline DLIST_ITEM_TYPE *DLIST_NAME(prev)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
73 {
74  return item->DLIST_NAME(item).prev == &list->ring ? NULL : DLIST_ITEM(item->DLIST_NAME(item).prev);
75 }
76 
77 static inline void DLIST_NAME(prepend)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
78 {
79  DLIST_NAME(check)(list);
80  dlist_insert_after(&list->ring, &item->DLIST_NAME(item));
81  DLIST_NAME(check)(list);
82 }
83 
84 static inline void DLIST_NAME(append)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
85 {
86  DLIST_NAME(check)(list);
87  dlist_insert_after(list->ring.prev, &item->DLIST_NAME(item));
88  DLIST_NAME(check)(list);
89 }
90 
91 static inline void DLIST_NAME(remove)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
92 {
93  dlist_ring *prev = item->DLIST_NAME(item).prev, *next = item->DLIST_NAME(item).next;
94  DLIST_NAME(check)(list);
95  if (prev) {
96  prev->next = next;
97  next->prev = prev;
98  }
99  item->DLIST_NAME(item).prev = NULL;
100  item->DLIST_NAME(item).next = NULL;
101  DLIST_NAME(check)(list);
102 }
103 
104 static inline bool DLIST_NAME(in_list)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
105 {
106  DLIST_NAME(check)(list);
107  return item->DLIST_NAME(item).prev != NULL || item->DLIST_NAME(item).next != NULL;
108 }
109 
110 #undef DLIST_ITEM
111 #undef DLIST_NAME
112 #undef DLIST_ITEM_TYPE
113 #undef DLIST_LIST_TYPE
114 #undef DLIST_PREFIX
115 #undef DLIST_PASTER
116 #undef DLIST_EVALUATOR
117 
Definition: dlist.tmpl.h:32
Definition: dlist.h:23