Tizen 2.0 Release
[framework/graphics/cairo.git] / src / cairo-list-inline.h
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Chris Wilson
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it either under the terms of the GNU Lesser General Public
7  * License version 2.1 as published by the Free Software Foundation
8  * (the "LGPL") or, at your option, under the terms of the Mozilla
9  * Public License Version 1.1 (the "MPL"). If you do not alter this
10  * notice, a recipient may use your version of this file under either
11  * the MPL or the LGPL.
12  *
13  * You should have received a copy of the LGPL along with this library
14  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16  * You should have received a copy of the MPL along with this library
17  * in the file COPYING-MPL-1.1
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26  * the specific language governing rights and limitations.
27  *
28  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Chris Wilson.
31  *
32  * Contributor(s):
33  *      Chris Wilson <chris@chris-wilson.co.uk>
34  *
35  */
36
37 #ifndef CAIRO_LIST_INLINE_H
38 #define CAIRO_LIST_INLINE_H
39
40 #include "cairo-list-private.h"
41
42 #define cairo_list_entry(ptr, type, member) \
43         cairo_container_of(ptr, type, member)
44
45 #define cairo_list_first_entry(ptr, type, member) \
46         cairo_list_entry((ptr)->next, type, member)
47
48 #define cairo_list_last_entry(ptr, type, member) \
49         cairo_list_entry((ptr)->prev, type, member)
50
51 #define cairo_list_foreach(pos, head)                   \
52         for (pos = (head)->next; pos != (head); pos = pos->next)
53
54 #define cairo_list_foreach_entry(pos, type, head, member)               \
55         for (pos = cairo_list_entry((head)->next, type, member);\
56              &pos->member != (head);                                    \
57              pos = cairo_list_entry(pos->member.next, type, member))
58
59 #define cairo_list_foreach_entry_safe(pos, n, type, head, member)       \
60         for (pos = cairo_list_entry ((head)->next, type, member),\
61              n = cairo_list_entry (pos->member.next, type, member);\
62              &pos->member != (head);                                    \
63              pos = n, n = cairo_list_entry (n->member.next, type, member))
64
65 #define cairo_list_foreach_entry_reverse(pos, type, head, member)       \
66         for (pos = cairo_list_entry((head)->prev, type, member);\
67              &pos->member != (head);                                    \
68              pos = cairo_list_entry(pos->member.prev, type, member))
69
70 #define cairo_list_foreach_entry_reverse_safe(pos, n, type, head, member)       \
71         for (pos = cairo_list_entry((head)->prev, type, member),\
72              n = cairo_list_entry (pos->member.prev, type, member);\
73              &pos->member != (head);                                    \
74              pos = n, n = cairo_list_entry (n->member.prev, type, member))
75
76 #ifdef CAIRO_LIST_DEBUG
77 static inline void
78 _cairo_list_validate (const cairo_list_t *link)
79 {
80     assert (link->next->prev == link);
81     assert (link->prev->next == link);
82 }
83 static inline void
84 cairo_list_validate (const cairo_list_t *head)
85 {
86     cairo_list_t *link;
87
88     cairo_list_foreach (link, head)
89         _cairo_list_validate (link);
90 }
91 static inline cairo_bool_t
92 cairo_list_is_empty (const cairo_list_t *head);
93 static inline void
94 cairo_list_validate_is_empty (const cairo_list_t *head)
95 {
96     assert (head->next == NULL || (cairo_list_is_empty (head) && head->next == head->prev));
97 }
98 #else
99 #define _cairo_list_validate(link)
100 #define cairo_list_validate(head)
101 #define cairo_list_validate_is_empty(head)
102 #endif
103
104 static inline void
105 cairo_list_init (cairo_list_t *entry)
106 {
107     entry->next = entry;
108     entry->prev = entry;
109 }
110
111 static inline void
112 __cairo_list_add (cairo_list_t *entry,
113                   cairo_list_t *prev,
114                   cairo_list_t *next)
115 {
116     next->prev = entry;
117     entry->next = next;
118     entry->prev = prev;
119     prev->next = entry;
120 }
121
122 static inline void
123 cairo_list_add (cairo_list_t *entry, cairo_list_t *head)
124 {
125     cairo_list_validate (head);
126     cairo_list_validate_is_empty (entry);
127     __cairo_list_add (entry, head, head->next);
128     cairo_list_validate (head);
129 }
130
131 static inline void
132 cairo_list_add_tail (cairo_list_t *entry, cairo_list_t *head)
133 {
134     cairo_list_validate (head);
135     cairo_list_validate_is_empty (entry);
136     __cairo_list_add (entry, head->prev, head);
137     cairo_list_validate (head);
138 }
139
140 static inline void
141 __cairo_list_del (cairo_list_t *prev, cairo_list_t *next)
142 {
143     next->prev = prev;
144     prev->next = next;
145 }
146
147 static inline void
148 cairo_list_del (cairo_list_t *entry)
149 {
150     __cairo_list_del (entry->prev, entry->next);
151     cairo_list_init (entry);
152 }
153
154 static inline void
155 cairo_list_move (cairo_list_t *entry, cairo_list_t *head)
156 {
157     cairo_list_validate (head);
158     __cairo_list_del (entry->prev, entry->next);
159     __cairo_list_add (entry, head, head->next);
160     cairo_list_validate (head);
161 }
162
163 static inline void
164 cairo_list_move_tail (cairo_list_t *entry, cairo_list_t *head)
165 {
166     cairo_list_validate (head);
167     __cairo_list_del (entry->prev, entry->next);
168     __cairo_list_add (entry, head->prev, head);
169     cairo_list_validate (head);
170 }
171
172 static inline void
173 cairo_list_swap (cairo_list_t *entry, cairo_list_t *other)
174 {
175     __cairo_list_add (entry, other->prev, other->next);
176     cairo_list_init (other);
177 }
178
179 static inline cairo_bool_t
180 cairo_list_is_first (const cairo_list_t *entry,
181                      const cairo_list_t *head)
182 {
183     cairo_list_validate (head);
184     return entry->prev == head;
185 }
186
187 static inline cairo_bool_t
188 cairo_list_is_last (const cairo_list_t *entry,
189                     const cairo_list_t *head)
190 {
191     cairo_list_validate (head);
192     return entry->next == head;
193 }
194
195 static inline cairo_bool_t
196 cairo_list_is_empty (const cairo_list_t *head)
197 {
198     cairo_list_validate (head);
199     return head->next == head;
200 }
201
202 static inline cairo_bool_t
203 cairo_list_is_singular (const cairo_list_t *head)
204 {
205     cairo_list_validate (head);
206     return head->next == head || head->next == head->prev;
207 }
208
209 #endif /* CAIRO_LIST_INLINE_H */