Some minor comment fixes.
[strongswan.git] / src / libstrongswan / utils / linked_list.h
1 /*
2 * Copyright (C) 2007-2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /**
19 * @defgroup linked_list linked_list
20 * @{ @ingroup utils
21 */
22
23 #ifndef LINKED_LIST_H_
24 #define LINKED_LIST_H_
25
26 typedef struct linked_list_t linked_list_t;
27
28 #include <utils/iterator.h>
29 #include <utils/enumerator.h>
30
31 /**
32 * Method to match elements in a linked list (used in find_* functions)
33 *
34 * @param item current list item
35 * @param ... user supplied data (only pointers, at most 5)
36 * @return
37 * - TRUE, if the item matched
38 * - FALSE, otherwise
39 */
40 typedef bool (*linked_list_match_t)(void *item, ...);
41
42 /**
43 * Method to be invoked on elements in a linked list (used in invoke_* functions)
44 *
45 * @param item current list item
46 * @param ... user supplied data (only pointers, at most 5)
47 */
48 typedef void (*linked_list_invoke_t)(void *item, ...);
49
50 /**
51 * Class implementing a double linked list.
52 *
53 * General purpose linked list. This list is not synchronized.
54 */
55 struct linked_list_t {
56
57 /**
58 * Gets the count of items in the list.
59 *
60 * @return number of items in list
61 */
62 int (*get_count) (linked_list_t *this);
63
64 /**
65 * Creates a iterator for the given list.
66 *
67 * @warning Created iterator_t object has to get destroyed by the caller.
68 *
69 * @deprecated Iterator is obsolete and will disappear, it is too
70 * complicated to implement. Use enumerator instead.
71 *
72 * @param forward iterator direction (TRUE: front to end)
73 * @return new iterator_t object
74 */
75 iterator_t *(*create_iterator) (linked_list_t *this, bool forward);
76
77 /**
78 * Create an enumerator over the list.
79 *
80 * The enumerator is a "lightweight" iterator. It only has two methods
81 * and should therefore be much easier to implement.
82 *
83 * @return enumerator over list items
84 */
85 enumerator_t* (*create_enumerator)(linked_list_t *this);
86
87 /**
88 * Inserts a new item at the beginning of the list.
89 *
90 * @param item item value to insert in list
91 */
92 void (*insert_first) (linked_list_t *this, void *item);
93
94 /**
95 * Removes the first item in the list and returns its value.
96 *
97 * @param item returned value of first item, or NULL
98 * @return SUCCESS, or NOT_FOUND if list is empty
99 */
100 status_t (*remove_first) (linked_list_t *this, void **item);
101
102 /**
103 * Remove an item from the list where the enumerator points to.
104 *
105 * @param enumerator enumerator with position
106 */
107 void (*remove_at)(linked_list_t *this, enumerator_t *enumerator);
108
109 /**
110 * Remove items from the list matching the given item.
111 *
112 * If a compare function is given, it is called for each item, with the
113 * first parameter being the current list item and the second parameter
114 * being the supplied item. Return TRUE from the compare function to remove
115 * the item, return FALSE to keep it in the list.
116 *
117 * If compare is NULL, comparison is done by pointers.
118 *
119 * @param item item to remove/pass to comparator
120 * @param compare compare function, or NULL
121 * @return number of removed items
122 */
123 int (*remove)(linked_list_t *this, void *item, bool (*compare)(void*,void*));
124
125 /**
126 * Returns the value of the first list item without removing it.
127 *
128 * @param this calling object
129 * @param item returned value of first item
130 * @return SUCCESS, NOT_FOUND if list is empty
131 */
132 status_t (*get_first) (linked_list_t *this, void **item);
133
134 /**
135 * Inserts a new item at the end of the list.
136 *
137 * @param item value to insert into list
138 */
139 void (*insert_last) (linked_list_t *this, void *item);
140
141 /**
142 * Removes the last item in the list and returns its value.
143 *
144 * @param this calling object
145 * @param item returned value of last item, or NULL
146 * @return SUCCESS, NOT_FOUND if list is empty
147 */
148 status_t (*remove_last) (linked_list_t *this, void **item);
149
150 /**
151 * Returns the value of the last list item without removing it.
152 *
153 * @param this calling object
154 * @param item returned value of last item
155 * @return SUCCESS, NOT_FOUND if list is empty
156 */
157 status_t (*get_last) (linked_list_t *this, void **item);
158
159 /** Find the first matching element in the list.
160 *
161 * The first object passed to the match function is the current list item,
162 * followed by the user supplied data.
163 * If the supplied function returns TRUE this function returns SUCCESS, and
164 * the current object is returned in the third parameter, otherwise,
165 * the next item is checked.
166 *
167 * If match is NULL, *item and the current object are compared.
168 *
169 * @warning Only use pointers as user supplied data.
170 *
171 * @param match comparison function to call on each object, or NULL
172 * @param item the list item, if found
173 * @param ... user data to supply to match function (limited to 5 arguments)
174 * @return SUCCESS if found, NOT_FOUND otherwise
175 */
176 status_t (*find_first) (linked_list_t *this, linked_list_match_t match,
177 void **item, ...);
178
179 /** Find the last matching element in the list.
180 *
181 * The first object passed to the match function is the current list item,
182 * followed by the user supplied data.
183 * If the supplied function returns TRUE this function returns SUCCESS, and
184 * the current object is returned in the third parameter, otherwise,
185 * the next item is checked.
186 *
187 * If match is NULL, *item and the current object are compared.
188 *
189 * @warning Only use pointers as user supplied data.
190 *
191 * @param match comparison function to call on each object, or NULL
192 * @param item the list item, if found
193 * @param ... user data to supply to match function (limited to 5 arguments)
194 * @return SUCCESS if found, NOT_FOUND otherwise
195 */
196 status_t (*find_last) (linked_list_t *this, linked_list_match_t match,
197 void **item, ...);
198
199 /**
200 * Invoke a method on all of the contained objects.
201 *
202 * If a linked list contains objects with function pointers,
203 * invoke() can call a method on each of the objects. The
204 * method is specified by an offset of the function pointer,
205 * which can be evalutated at compile time using the offsetof
206 * macro, e.g.: list->invoke(list, offsetof(object_t, method));
207 *
208 * @warning Only use pointers as user supplied data.
209 *
210 * @param offset offset of the method to invoke on objects
211 * @param ... user data to supply to called function (limited to 5 arguments)
212 */
213 void (*invoke_offset) (linked_list_t *this, size_t offset, ...);
214
215 /**
216 * Invoke a function on all of the contained objects.
217 *
218 * @warning Only use pointers as user supplied data.
219 *
220 * @param function offset of the method to invoke on objects
221 * @param ... user data to supply to called function (limited to 5 arguments)
222 */
223 void (*invoke_function) (linked_list_t *this, linked_list_invoke_t function, ...);
224
225 /**
226 * Clones a list and its objects using the objects' clone method.
227 *
228 * @param offset offset ot the objects clone function
229 * @return cloned list
230 */
231 linked_list_t *(*clone_offset) (linked_list_t *this, size_t offset);
232
233 /**
234 * Clones a list and its objects using a given function.
235 *
236 * @param function function that clones an object
237 * @return cloned list
238 */
239 linked_list_t *(*clone_function) (linked_list_t *this, void*(*)(void*));
240
241 /**
242 * Destroys a linked_list object.
243 */
244 void (*destroy) (linked_list_t *this);
245
246 /**
247 * Destroys a list and its objects using the destructor.
248 *
249 * If a linked list and the contained objects should be destroyed, use
250 * destroy_offset. The supplied offset specifies the destructor to
251 * call on each object. The offset may be calculated using the offsetof
252 * macro, e.g.: list->destroy_offset(list, offsetof(object_t, destroy));
253 *
254 * @param offset offset of the objects destructor
255 */
256 void (*destroy_offset) (linked_list_t *this, size_t offset);
257
258 /**
259 * Destroys a list and its contents using a a cleanup function.
260 *
261 * If a linked list and its contents should get destroyed using a specific
262 * cleanup function, use destroy_function. This is useful when the
263 * list contains malloc()-ed blocks which should get freed,
264 * e.g.: list->destroy_function(list, free);
265 *
266 * @param function function to call on each object
267 */
268 void (*destroy_function) (linked_list_t *this, void (*)(void*));
269 };
270
271 /**
272 * Creates an empty linked list object.
273 *
274 * @return linked_list_t object.
275 */
276 linked_list_t *linked_list_create(void);
277
278 #endif /** LINKED_LIST_H_ @}*/