ba5f28f6ad6c53e02540f0a8cde2f77a854b27e9
[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 item.
111 *
112 * If a compare function is given, it is called for each item, where
113 * the first parameter is the current list item and the second parameter
114 * is the supplied item parameter.
115 * If compare is NULL, compare is done by pointer.
116 *
117 * @param item item to remove/pass to comparator
118 * @param compare compare function, or NULL
119 * @return number of removed items
120 */
121 int (*remove)(linked_list_t *this, void *item, bool (*compare)(void *,void*));
122
123 /**
124 * Returns the value of the first list item without removing it.
125 *
126 * @param this calling object
127 * @param item returned value of first item
128 * @return SUCCESS, NOT_FOUND if list is empty
129 */
130 status_t (*get_first) (linked_list_t *this, void **item);
131
132 /**
133 * Inserts a new item at the end of the list.
134 *
135 * @param item value to insert into list
136 */
137 void (*insert_last) (linked_list_t *this, void *item);
138
139 /**
140 * Removes the last item in the list and returns its value.
141 *
142 * @param this calling object
143 * @param item returned value of last item, or NULL
144 * @return SUCCESS, NOT_FOUND if list is empty
145 */
146 status_t (*remove_last) (linked_list_t *this, void **item);
147
148 /**
149 * Returns the value of the last list item without removing it.
150 *
151 * @param this calling object
152 * @param item returned value of last item
153 * @return SUCCESS, NOT_FOUND if list is empty
154 */
155 status_t (*get_last) (linked_list_t *this, void **item);
156
157 /** Find the first matching element in the list.
158 *
159 * The first object passed to the match function is the current list item,
160 * followed by the user supplied data.
161 * If the supplied function returns TRUE this function returns SUCCESS, and
162 * the current object is returned in the third parameter, otherwise,
163 * the next item is checked.
164 *
165 * If match is NULL, *item and the current object are compared.
166 *
167 * @warning Only use pointers as user supplied data.
168 *
169 * @param match comparison function to call on each object, or NULL
170 * @param item the list item, if found
171 * @param ... user data to supply to match function (limited to 5 arguments)
172 * @return SUCCESS if found, NOT_FOUND otherwise
173 */
174 status_t (*find_first) (linked_list_t *this, linked_list_match_t match,
175 void **item, ...);
176
177 /** Find the last matching element in the list.
178 *
179 * The first object passed to the match function is the current list item,
180 * followed by the user supplied data.
181 * If the supplied function returns TRUE this function returns SUCCESS, and
182 * the current object is returned in the third parameter, otherwise,
183 * the next item is checked.
184 *
185 * If match is NULL, *item and the current object are compared.
186 *
187 * @warning Only use pointers as user supplied data.
188 *
189 * @param match comparison function to call on each object, or NULL
190 * @param item the list item, if found
191 * @param ... user data to supply to match function (limited to 5 arguments)
192 * @return SUCCESS if found, NOT_FOUND otherwise
193 */
194 status_t (*find_last) (linked_list_t *this, linked_list_match_t match,
195 void **item, ...);
196
197 /**
198 * Invoke a method on all of the contained objects.
199 *
200 * If a linked list contains objects with function pointers,
201 * invoke() can call a method on each of the objects. The
202 * method is specified by an offset of the function pointer,
203 * which can be evalutated at compile time using the offsetof
204 * macro, e.g.: list->invoke(list, offsetof(object_t, method));
205 *
206 * @param offset offset of the method to invoke on objects
207 * @param ... user data to supply to called function (limited to 5 arguments)
208 */
209 void (*invoke_offset) (linked_list_t *this, size_t offset, ...);
210
211 /**
212 * Invoke a function on all of the contained objects.
213 *
214 * @param function offset of the method to invoke on objects
215 * @param ... user data to supply to called function (limited to 5 arguments)
216 */
217 void (*invoke_function) (linked_list_t *this, linked_list_invoke_t function, ...);
218
219 /**
220 * Clones a list and its objects using the objects' clone method.
221 *
222 * @param offset offset ot the objects clone function
223 * @return cloned list
224 */
225 linked_list_t *(*clone_offset) (linked_list_t *this, size_t offset);
226
227 /**
228 * Clones a list and its objects using a given function.
229 *
230 * @param function function that clones an object
231 * @return cloned list
232 */
233 linked_list_t *(*clone_function) (linked_list_t *this, void*(*)(void*));
234
235 /**
236 * Destroys a linked_list object.
237 */
238 void (*destroy) (linked_list_t *this);
239
240 /**
241 * Destroys a list and its objects using the destructor.
242 *
243 * If a linked list and the contained objects should be destroyed, use
244 * destroy_offset. The supplied offset specifies the destructor to
245 * call on each object. The offset may be calculated using the offsetof
246 * macro, e.g.: list->destroy_offset(list, offsetof(object_t, destroy));
247 *
248 * @param offset offset of the objects destructor
249 */
250 void (*destroy_offset) (linked_list_t *this, size_t offset);
251
252 /**
253 * Destroys a list and its contents using a a cleanup function.
254 *
255 * If a linked list and its contents should get destroyed using a specific
256 * cleanup function, use destroy_function. This is useful when the
257 * list contains malloc()-ed blocks which should get freed,
258 * e.g.: list->destroy_function(list, free);
259 *
260 * @param function function to call on each object
261 */
262 void (*destroy_function) (linked_list_t *this, void (*)(void*));
263 };
264
265 /**
266 * Creates an empty linked list object.
267 *
268 * @return linked_list_t object.
269 */
270 linked_list_t *linked_list_create(void);
271
272 #endif /** LINKED_LIST_H_ @}*/