got rid of deprecated create_iterator_locked()
[strongswan.git] / src / libstrongswan / utils / iterator.h
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 /**
20 * @defgroup iterator iterator
21 * @{ @ingroup utils
22 */
23
24 #ifndef ITERATOR_H_
25 #define ITERATOR_H_
26
27 #include <library.h>
28
29 typedef enum hook_result_t hook_result_t;
30
31 /**
32 * Return value of an iterator hook.
33 *
34 * Returning HOOK_AGAIN is useful to "inject" additional elements in an
35 * iteration, HOOK_NEXT is the normal iterator behavior, and HOOK_SKIP may
36 * be used to filter elements out.
37 */
38 enum hook_result_t {
39
40 /**
41 * A value was placed in out, hook is called again with the same "in"
42 */
43 HOOK_AGAIN,
44
45 /**
46 * A value was placed in out, hook is called again with next "in" (if any)
47 */
48 HOOK_NEXT,
49
50 /**
51 * No value in out, call again with next "in" (if any)
52 */
53 HOOK_SKIP,
54 };
55
56 /**
57 * Iterator hook function prototype.
58 *
59 * @param param user supplied parameter
60 * @param in the value the hook receives from the iterator
61 * @param out the value supplied as a result to the iterator
62 * @return a hook_result_t
63 */
64 typedef hook_result_t (iterator_hook_t)(void *param, void *in, void **out);
65
66
67 typedef struct iterator_t iterator_t;
68
69 /**
70 * Iterator interface, allows iteration over collections.
71 *
72 * iterator_t defines an interface for iterating over collections.
73 * It allows searching, deleting, updating and inserting.
74 *
75 * @deprecated Use enumerator instead.
76 */
77 struct iterator_t {
78
79 /**
80 * Return number of list items.
81 *
82 * @return number of list items
83 */
84 int (*get_count) (iterator_t *this);
85
86 /**
87 * Iterate over all items.
88 *
89 * The easy way to iterate over items.
90 *
91 * @param value item
92 * @return TRUE, if there was an element available, FALSE otherwise
93 */
94 bool (*iterate) (iterator_t *this, void** value);
95
96 /**
97 * Hook a function into the iterator.
98 *
99 * Sometimes it is useful to hook in an iterator. The hook function is
100 * called before any successful return of iterate(). It takes the
101 * iterator value, may manipulate it (or the references object), and returns
102 * the value that the iterate() function returns. Depending on the hook
103 * return value, the hook is called again, called with next, or skipped.
104 * A value of NULL deactivates the iterator hook.
105 * If an iterator is hooked, only the iterate() method is valid,
106 * all other methods behave undefined.
107 *
108 * @param hook iterator hook which manipulates the iterated value
109 * @param param user supplied parameter to pass back to the hook
110 */
111 void (*set_iterator_hook) (iterator_t *this, iterator_hook_t *hook,
112 void *param);
113
114 /**
115 * Inserts a new item before the given iterator position.
116 *
117 * The iterator position is not changed after inserting
118 *
119 * @param item value to insert in list
120 */
121 void (*insert_before) (iterator_t *this, void *item);
122
123 /**
124 * Inserts a new item after the given iterator position.
125 *
126 * The iterator position is not changed after inserting.
127 *
128 * @param this calling iterator
129 * @param item value to insert in list
130 */
131 void (*insert_after) (iterator_t *this, void *item);
132
133 /**
134 * Replace the current item at current iterator position.
135 *
136 * The iterator position is not changed after replacing.
137 *
138 * @param this calling iterator
139 * @param old old value will be written here(can be NULL)
140 * @param new new value
141 * @return SUCCESS, FAILED if iterator is on an invalid position
142 */
143 status_t (*replace) (iterator_t *this, void **old, void *new);
144
145 /**
146 * Removes an element from list at the given iterator position.
147 *
148 * The iterator is set the the following position:
149 * - to the item before, if available
150 * - it gets reseted, otherwise
151 *
152 * @return SUCCESS, FAILED if iterator is on an invalid position
153 */
154 status_t (*remove) (iterator_t *this);
155
156 /**
157 * Resets the iterator position.
158 *
159 * After reset, the iterator_t objects doesn't point to an element.
160 * A call to iterator_t.has_next is necessary to do any other operations
161 * with the resetted iterator.
162 */
163 void (*reset) (iterator_t *this);
164
165 /**
166 * Destroys an iterator.
167 */
168 void (*destroy) (iterator_t *this);
169 };
170
171 #endif /*ITERATOR_H_ @} */