gdi32: Return the full DIB height in GetImage instead of clipping to the visible...
[wine] / include / wine / list.h
1 /*
2  * Linked lists support
3  *
4  * Copyright (C) 2002 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #ifndef __WINE_SERVER_LIST_H
22 #define __WINE_SERVER_LIST_H
23
24 struct list
25 {
26     struct list *next;
27     struct list *prev;
28 };
29
30 /* Define a list like so:
31  *
32  *   struct gadget
33  *   {
34  *       struct list  entry;   <-- doesn't have to be the first item in the struct
35  *       int          a, b;
36  *   };
37  *
38  *   static struct list global_gadgets = LIST_INIT( global_gadgets );
39  *
40  * or
41  *
42  *   struct some_global_thing
43  *   {
44  *       struct list gadgets;
45  *   };
46  *
47  *   list_init( &some_global_thing->gadgets );
48  *
49  * Manipulate it like this:
50  *
51  *   list_add_head( &global_gadgets, &new_gadget->entry );
52  *   list_remove( &new_gadget->entry );
53  *   list_add_after( &some_random_gadget->entry, &new_gadget->entry );
54  *
55  * And to iterate over it:
56  *
57  *   struct gadget *gadget;
58  *   LIST_FOR_EACH_ENTRY( gadget, &global_gadgets, struct gadget, entry )
59  *   {
60  *       ...
61  *   }
62  *
63  */
64
65 /* add an element after the specified one */
66 static inline void list_add_after( struct list *elem, struct list *to_add )
67 {
68     to_add->next = elem->next;
69     to_add->prev = elem;
70     elem->next->prev = to_add;
71     elem->next = to_add;
72 }
73
74 /* add an element before the specified one */
75 static inline void list_add_before( struct list *elem, struct list *to_add )
76 {
77     to_add->next = elem;
78     to_add->prev = elem->prev;
79     elem->prev->next = to_add;
80     elem->prev = to_add;
81 }
82
83 /* add element at the head of the list */
84 static inline void list_add_head( struct list *list, struct list *elem )
85 {
86     list_add_after( list, elem );
87 }
88
89 /* add element at the tail of the list */
90 static inline void list_add_tail( struct list *list, struct list *elem )
91 {
92     list_add_before( list, elem );
93 }
94
95 /* remove an element from its list */
96 static inline void list_remove( struct list *elem )
97 {
98     elem->next->prev = elem->prev;
99     elem->prev->next = elem->next;
100 }
101
102 /* get the next element */
103 static inline struct list *list_next( const struct list *list, const struct list *elem )
104 {
105     struct list *ret = elem->next;
106     if (elem->next == list) ret = NULL;
107     return ret;
108 }
109
110 /* get the previous element */
111 static inline struct list *list_prev( const struct list *list, const struct list *elem )
112 {
113     struct list *ret = elem->prev;
114     if (elem->prev == list) ret = NULL;
115     return ret;
116 }
117
118 /* get the first element */
119 static inline struct list *list_head( const struct list *list )
120 {
121     return list_next( list, list );
122 }
123
124 /* get the last element */
125 static inline struct list *list_tail( const struct list *list )
126 {
127     return list_prev( list, list );
128 }
129
130 /* check if a list is empty */
131 static inline int list_empty( const struct list *list )
132 {
133     return list->next == list;
134 }
135
136 /* initialize a list */
137 static inline void list_init( struct list *list )
138 {
139     list->next = list->prev = list;
140 }
141
142 /* count the elements of a list */
143 static inline unsigned int list_count( const struct list *list )
144 {
145     unsigned count = 0;
146     const struct list *ptr;
147     for (ptr = list->next; ptr != list; ptr = ptr->next) count++;
148     return count;
149 }
150
151 /* move all elements from src to the tail of dst */
152 static inline void list_move_tail( struct list *dst, struct list *src )
153 {
154     if (list_empty(src)) return;
155
156     dst->prev->next = src->next;
157     src->next->prev = dst->prev;
158     dst->prev = src->prev;
159     src->prev->next = dst;
160     list_init(src);
161 }
162
163 /* move all elements from src to the head of dst */
164 static inline void list_move_head( struct list *dst, struct list *src )
165 {
166     if (list_empty(src)) return;
167
168     dst->next->prev = src->prev;
169     src->prev->next = dst->next;
170     dst->next = src->next;
171     src->next->prev = dst;
172     list_init(src);
173 }
174
175 /* iterate through the list */
176 #define LIST_FOR_EACH(cursor,list) \
177     for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)
178
179 /* iterate through the list, with safety against removal */
180 #define LIST_FOR_EACH_SAFE(cursor, cursor2, list) \
181     for ((cursor) = (list)->next, (cursor2) = (cursor)->next; \
182          (cursor) != (list); \
183          (cursor) = (cursor2), (cursor2) = (cursor)->next)
184
185 /* iterate through the list using a list entry */
186 #define LIST_FOR_EACH_ENTRY(elem, list, type, field) \
187     for ((elem) = LIST_ENTRY((list)->next, type, field); \
188          &(elem)->field != (list); \
189          (elem) = LIST_ENTRY((elem)->field.next, type, field))
190
191 /* iterate through the list using a list entry, with safety against removal */
192 #define LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, list, type, field) \
193     for ((cursor) = LIST_ENTRY((list)->next, type, field), \
194          (cursor2) = LIST_ENTRY((cursor)->field.next, type, field); \
195          &(cursor)->field != (list); \
196          (cursor) = (cursor2), \
197          (cursor2) = LIST_ENTRY((cursor)->field.next, type, field))
198
199 /* iterate through the list in reverse order */
200 #define LIST_FOR_EACH_REV(cursor,list) \
201     for ((cursor) = (list)->prev; (cursor) != (list); (cursor) = (cursor)->prev)
202
203 /* iterate through the list in reverse order, with safety against removal */
204 #define LIST_FOR_EACH_SAFE_REV(cursor, cursor2, list) \
205     for ((cursor) = (list)->prev, (cursor2) = (cursor)->prev; \
206          (cursor) != (list); \
207          (cursor) = (cursor2), (cursor2) = (cursor)->prev)
208
209 /* iterate through the list in reverse order using a list entry */
210 #define LIST_FOR_EACH_ENTRY_REV(elem, list, type, field) \
211     for ((elem) = LIST_ENTRY((list)->prev, type, field); \
212          &(elem)->field != (list); \
213          (elem) = LIST_ENTRY((elem)->field.prev, type, field))
214
215 /* iterate through the list in reverse order using a list entry, with safety against removal */
216 #define LIST_FOR_EACH_ENTRY_SAFE_REV(cursor, cursor2, list, type, field) \
217     for ((cursor) = LIST_ENTRY((list)->prev, type, field), \
218          (cursor2) = LIST_ENTRY((cursor)->field.prev, type, field); \
219          &(cursor)->field != (list); \
220          (cursor) = (cursor2), \
221          (cursor2) = LIST_ENTRY((cursor)->field.prev, type, field))
222
223 /* macros for statically initialized lists */
224 #undef LIST_INIT
225 #define LIST_INIT(list)  { &(list), &(list) }
226
227 /* get pointer to object containing list element */
228 #undef LIST_ENTRY
229 #define LIST_ENTRY(elem, type, field) \
230     ((type *)((char *)(elem) - (size_t)(&((type *)0)->field)))
231
232 #endif  /* __WINE_SERVER_LIST_H */