Changeset 4577
- Timestamp:
- 11/05/08 09:37:09 (2 months ago)
- Files:
-
- trunk/src/libstrongswan/utils/iterator.h (modified) (2 diffs)
- trunk/src/libstrongswan/utils/linked_list.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/libstrongswan/utils/iterator.h
r3589 r4577 26 26 27 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 an35 * iteration, HOOK_NEXT is the normal iterator behavior, and HOOK_SKIP may36 * 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 parameter60 * @param in the value the hook receives from the iterator61 * @param out the value supplied as a result to the iterator62 * @return a hook_result_t63 */64 typedef hook_result_t (iterator_hook_t)(void *param, void *in, void **out);65 28 66 29 … … 93 56 */ 94 57 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 is100 * called before any successful return of iterate(). It takes the101 * iterator value, may manipulate it (or the references object), and returns102 * the value that the iterate() function returns. Depending on the hook103 * 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 value109 * @param param user supplied parameter to pass back to the hook110 */111 void (*set_iterator_hook) (iterator_t *this, iterator_hook_t *hook,112 void *param);113 58 114 59 /** trunk/src/libstrongswan/utils/linked_list.c
r4576 r4577 120 120 */ 121 121 bool forward; 122 123 /**124 * iteration hook125 */126 iterator_hook_t *hook;127 128 /**129 * user parameter for iterator hook130 */131 void *hook_param;132 122 }; 133 123 … … 204 194 205 195 /** 206 * default iterator hook which does nothing207 */208 static hook_result_t iterator_hook(void *param, void *in, void **out)209 {210 *out = in;211 return HOOK_NEXT;212 }213 214 /**215 * Implementation of iterator_t.set_iterator_hook.216 */217 static void set_iterator_hook(private_iterator_t *this, iterator_hook_t *hook,218 void* param)219 {220 if (hook == NULL)221 {222 this->hook = iterator_hook;223 this->hook_param = NULL;224 }225 else226 {227 this->hook = hook;228 this->hook_param = param;229 }230 }231 232 /**233 196 * Implementation of iterator_t.iterate. 234 197 */ 235 198 static bool iterate(private_iterator_t *this, void** value) 236 199 { 237 while (TRUE) 238 { 239 if (this->forward) 240 { 241 this->current = this->current ? this->current->next : this->list->first; 242 } 243 else 244 { 245 this->current = this->current ? this->current->previous : this->list->last; 246 } 247 248 if (this->current == NULL) 249 { 250 return FALSE; 251 } 252 253 switch (this->hook(this->hook_param, this->current->value, value)) 254 { 255 case HOOK_AGAIN: 256 /* rewind */ 257 if (this->forward) 258 { 259 this->current = this->current->previous; 260 } 261 else 262 { 263 this->current = this->current->next; 264 } 265 break; 266 case HOOK_NEXT: 267 /* normal iteration */ 268 break; 269 case HOOK_SKIP: 270 /* advance */ 271 continue; 272 } 273 break; 200 if (this->forward) 201 { 202 this->current = this->current ? this->current->next : this->list->first; 203 } 204 else 205 { 206 this->current = this->current ? this->current->previous : this->list->last; 207 } 208 if (this->current == NULL) 209 { 210 return FALSE; 274 211 } 275 212 return TRUE; … … 787 724 this->public.get_count = (int (*) (iterator_t*)) get_list_count; 788 725 this->public.iterate = (bool (*) (iterator_t*, void **value)) iterate; 789 this->public.set_iterator_hook = (void(*)(iterator_t*, iterator_hook_t*, void*))set_iterator_hook;790 726 this->public.insert_before = (void (*) (iterator_t*, void *item)) insert_before; 791 727 this->public.insert_after = (void (*) (iterator_t*, void *item)) insert_after; … … 798 734 this->current = NULL; 799 735 this->list = linked_list; 800 this->hook = iterator_hook;801 736 802 737 return &this->public;
