/*
- * Copyright (C) 2007-2008 Tobias Brunner
+ * Copyright (C) 2007-2011 Tobias Brunner
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil
*/
element_t *element_create(void *value)
{
- element_t *this = malloc_thing(element_t);
-
- this->previous = NULL;
- this->next = NULL;
- this->value = value;
-
- return (this);
+ element_t *this;
+ INIT(this,
+ .value = value,
+ );
+ return this;
}
element_t *current;
};
-/**
- * Implementation of private_enumerator_t.enumerator.enumerate.
- */
-static bool enumerate(private_enumerator_t *this, void **item)
+METHOD(enumerator_t, enumerate, bool,
+ private_enumerator_t *this, void **item)
{
if (!this->current)
{
return TRUE;
}
-/**
- * Implementation of linked_list_t.create_enumerator.
- */
-static enumerator_t* create_enumerator(private_linked_list_t *this)
+METHOD(linked_list_t, create_enumerator, enumerator_t*,
+ private_linked_list_t *this)
{
- private_enumerator_t *enumerator = malloc_thing(private_enumerator_t);
+ private_enumerator_t *enumerator;
- enumerator->enumerator.enumerate = (void*)enumerate;
- enumerator->enumerator.destroy = (void*)free;
- enumerator->list = this;
- enumerator->current = NULL;
+ INIT(enumerator,
+ .enumerator = {
+ .enumerate = (void*)_enumerate,
+ .destroy = (void*)free,
+ },
+ .list = this,
+ );
return &enumerator->enumerator;
}
-/**
- * Implementation of iterator_t.get_count.
- */
-static int get_list_count(private_iterator_t *this)
+METHOD(iterator_t, iterator_get_count, int,
+ private_iterator_t *this)
{
return this->list->count;
}
-/**
- * Implementation of iterator_t.iterate.
- */
-static bool iterate(private_iterator_t *this, void** value)
+METHOD(iterator_t, iterate, bool,
+ private_iterator_t *this, void** value)
{
if (this->forward)
{
return TRUE;
}
-/**
- * Implementation of iterator_t.reset.
- */
-static void iterator_reset(private_iterator_t *this)
+METHOD(iterator_t, iterator_reset, void,
+ private_iterator_t *this)
{
this->current = NULL;
}
-/**
- * Implementation of iterator_t.remove.
- */
-static status_t iterator_remove(private_iterator_t *this)
+METHOD(iterator_t, iterator_remove, status_t,
+ private_iterator_t *this)
{
element_t *new_current;
return SUCCESS;
}
-/**
- * Implementation of iterator_t.insert_before.
- */
-static void insert_before(private_iterator_t * iterator, void *item)
+METHOD(iterator_t, iterator_insert_before, void,
+ private_iterator_t * iterator, void *item)
{
if (iterator->current == NULL)
{
iterator->list->count++;
}
-/**
- * Implementation of iterator_t.replace.
- */
-static status_t replace(private_iterator_t *this, void **old_item, void *new_item)
+METHOD(iterator_t, iterator_replace, status_t,
+ private_iterator_t *this, void **old_item, void *new_item)
{
if (this->current == NULL)
{
return SUCCESS;
}
-/**
- * Implementation of iterator_t.insert_after.
- */
-static void insert_after(private_iterator_t *iterator, void *item)
+METHOD(iterator_t, iterator_insert_after, void,
+ private_iterator_t *iterator, void *item)
{
if (iterator->current == NULL)
{
iterator->list->count++;
}
-/**
- * Implementation of iterator_t.destroy.
- */
-static void iterator_destroy(private_iterator_t *this)
+METHOD(iterator_t, iterator_destroy, void,
+ private_iterator_t *this)
{
free(this);
}
-/**
- * Implementation of linked_list_t.get_count.
- */
-static int get_count(private_linked_list_t *this)
+METHOD(linked_list_t, get_count, int,
+ private_linked_list_t *this)
{
return this->count;
}
-/**
- * Implementation of linked_list_t.insert_first.
- */
-static void insert_first(private_linked_list_t *this, void *item)
+METHOD(linked_list_t, insert_first, void,
+ private_linked_list_t *this, void *item)
{
element_t *element;
/**
* unlink an element form the list, returns following element
*/
-static element_t* remove_element(private_linked_list_t *this, element_t *element)
+static element_t* remove_element(private_linked_list_t *this,
+ element_t *element)
{
element_t *next, *previous;
return next;
}
-/**
- * Implementation of linked_list_t.get_first.
- */
-static status_t get_first(private_linked_list_t *this, void **item)
+METHOD(linked_list_t, get_first, status_t,
+ private_linked_list_t *this, void **item)
{
if (this->count == 0)
{
return SUCCESS;
}
-/**
- * Implementation of linked_list_t.remove_first.
- */
-static status_t remove_first(private_linked_list_t *this, void **item)
+METHOD(linked_list_t, remove_first, status_t,
+ private_linked_list_t *this, void **item)
{
if (get_first(this, item) == SUCCESS)
{
return NOT_FOUND;
}
-/**
- * Implementation of linked_list_t.insert_last.
- */
-static void insert_last(private_linked_list_t *this, void *item)
+METHOD(linked_list_t, insert_last, void,
+ private_linked_list_t *this, void *item)
{
element_t *element = element_create(item);
this->count++;
}
-/**
- * Implementation of linked_list_t.get_last.
- */
-static status_t get_last(private_linked_list_t *this, void **item)
+METHOD(linked_list_t, get_last, status_t,
+ private_linked_list_t *this, void **item)
{
if (this->count == 0)
{
return SUCCESS;
}
-/**
- * Implementation of linked_list_t.remove_last.
- */
-static status_t remove_last(private_linked_list_t *this, void **item)
+METHOD(linked_list_t, remove_last, status_t,
+ private_linked_list_t *this, void **item)
{
if (get_last(this, item) == SUCCESS)
{
return NOT_FOUND;
}
-/**
- * Implementation of linked_list_t.remove.
- */
-static int remove_(private_linked_list_t *this, void *item,
- bool (*compare)(void *,void*))
+METHOD(linked_list_t, remove_, int,
+ private_linked_list_t *this, void *item, bool (*compare)(void*,void*))
{
element_t *current = this->first;
int removed = 0;
return removed;
}
-/**
- * Implementation of linked_list_t.remove_at.
- */
-static void remove_at(private_linked_list_t *this, private_enumerator_t *enumerator)
+METHOD(linked_list_t, remove_at, void,
+ private_linked_list_t *this, private_enumerator_t *enumerator)
{
element_t *current;
}
}
-/**
- * Implementation of linked_list_t.find_first.
- */
-static status_t find_first(private_linked_list_t *this, linked_list_match_t match,
- void **item, void *d1, void *d2, void *d3, void *d4, void *d5)
+METHOD(linked_list_t, find_first, status_t,
+ private_linked_list_t *this, linked_list_match_t match,
+ void **item, void *d1, void *d2, void *d3, void *d4, void *d5)
{
element_t *current = this->first;
return NOT_FOUND;
}
-/**
- * Implementation of linked_list_t.find_last.
- */
-static status_t find_last(private_linked_list_t *this, linked_list_match_t match,
- void **item, void *d1, void *d2, void *d3, void *d4, void *d5)
+METHOD(linked_list_t, find_last, status_t,
+ private_linked_list_t *this, linked_list_match_t match,
+ void **item, void *d1, void *d2, void *d3, void *d4, void *d5)
{
element_t *current = this->last;
return NOT_FOUND;
}
-/**
- * Implementation of linked_list_t.invoke_offset.
- */
-static void invoke_offset(private_linked_list_t *this, size_t offset,
- void *d1, void *d2, void *d3, void *d4, void *d5)
+METHOD(linked_list_t, invoke_offset, void,
+ private_linked_list_t *this, size_t offset,
+ void *d1, void *d2, void *d3, void *d4, void *d5)
{
element_t *current = this->first;
}
}
-/**
- * Implementation of linked_list_t.invoke_function.
- */
-static void invoke_function(private_linked_list_t *this, linked_list_invoke_t fn,
- void *d1, void *d2, void *d3, void *d4, void *d5)
+METHOD(linked_list_t, invoke_function, void,
+ private_linked_list_t *this, linked_list_invoke_t fn,
+ void *d1, void *d2, void *d3, void *d4, void *d5)
{
element_t *current = this->first;
}
}
-/**
- * Implementation of linked_list_t.clone_offset
- */
-static linked_list_t *clone_offset(private_linked_list_t *this, size_t offset)
+METHOD(linked_list_t, clone_offset, linked_list_t*,
+ private_linked_list_t *this, size_t offset)
{
linked_list_t *clone = linked_list_create();
element_t *current = this->first;
return clone;
}
-/**
- * Implementation of linked_list_t.clone_function
- */
-static linked_list_t *clone_function(private_linked_list_t *this, void* (*fn)(void*))
+METHOD(linked_list_t, clone_function, linked_list_t*,
+ private_linked_list_t *this, void* (*fn)(void*))
{
linked_list_t *clone = linked_list_create();
element_t *current = this->first;
return clone;
}
-/**
- * Implementation of linked_list_t.destroy.
- */
-static void destroy(private_linked_list_t *this)
+METHOD(linked_list_t, destroy, void,
+ private_linked_list_t *this)
{
void *value;
/* Remove all list items before destroying list */
free(this);
}
-/**
- * Implementation of linked_list_t.destroy_offset.
- */
-static void destroy_offset(private_linked_list_t *this, size_t offset)
+METHOD(linked_list_t, destroy_offset, void,
+ private_linked_list_t *this, size_t offset)
{
element_t *current = this->first, *next;
free(this);
}
-/**
- * Implementation of linked_list_t.destroy_function.
- */
-static void destroy_function(private_linked_list_t *this, void (*fn)(void*))
+METHOD(linked_list_t, destroy_function, void,
+ private_linked_list_t *this, void (*fn)(void*))
{
element_t *current = this->first, *next;
free(this);
}
-/**
- * Implementation of linked_list_t.create_iterator.
- */
-static iterator_t *create_iterator(private_linked_list_t *linked_list, bool forward)
+METHOD(linked_list_t, create_iterator, iterator_t*,
+ private_linked_list_t *linked_list, bool forward)
{
- private_iterator_t *this = malloc_thing(private_iterator_t);
-
- this->public.get_count = (int (*) (iterator_t*)) get_list_count;
- this->public.iterate = (bool (*) (iterator_t*, void **value)) iterate;
- this->public.insert_before = (void (*) (iterator_t*, void *item)) insert_before;
- this->public.insert_after = (void (*) (iterator_t*, void *item)) insert_after;
- this->public.replace = (status_t (*) (iterator_t*, void **, void *)) replace;
- this->public.remove = (status_t (*) (iterator_t*)) iterator_remove;
- this->public.reset = (void (*) (iterator_t*)) iterator_reset;
- this->public.destroy = (void (*) (iterator_t*)) iterator_destroy;
+ private_iterator_t *this;
- this->forward = forward;
- this->current = NULL;
- this->list = linked_list;
+ INIT(this,
+ .public = {
+ .get_count = _iterator_get_count,
+ .iterate = _iterate,
+ .insert_before = _iterator_insert_before,
+ .insert_after = _iterator_insert_after,
+ .replace = _iterator_replace,
+ .remove = _iterator_remove,
+ .reset = _iterator_reset,
+ .destroy = _iterator_destroy,
+ },
+ .forward = forward,
+ .list = linked_list,
+ );
return &this->public;
}
*/
linked_list_t *linked_list_create()
{
- private_linked_list_t *this = malloc_thing(private_linked_list_t);
-
- this->public.get_count = (int (*) (linked_list_t *)) get_count;
- this->public.create_iterator = (iterator_t * (*) (linked_list_t *,bool))create_iterator;
- this->public.create_enumerator = (enumerator_t*(*)(linked_list_t*))create_enumerator;
- this->public.get_first = (status_t (*) (linked_list_t *, void **item))get_first;
- this->public.get_last = (status_t (*) (linked_list_t *, void **item))get_last;
- this->public.find_first = (status_t (*) (linked_list_t *, linked_list_match_t,void**,...))find_first;
- this->public.find_last = (status_t (*) (linked_list_t *, linked_list_match_t,void**,...))find_last;
- this->public.insert_first = (void (*) (linked_list_t *, void *item))insert_first;
- this->public.insert_last = (void (*) (linked_list_t *, void *item))insert_last;
- this->public.remove_first = (status_t (*) (linked_list_t *, void **item))remove_first;
- this->public.remove_last = (status_t (*) (linked_list_t *, void **item))remove_last;
- this->public.remove = (int(*)(linked_list_t*, void *item, bool (*compare)(void *,void*)))remove_;
- this->public.remove_at = (void(*)(linked_list_t*, enumerator_t *enumerator))remove_at;
- this->public.invoke_offset = (void (*)(linked_list_t*,size_t,...))invoke_offset;
- this->public.invoke_function = (void (*)(linked_list_t*,linked_list_invoke_t,...))invoke_function;
- this->public.clone_offset = (linked_list_t * (*)(linked_list_t*,size_t))clone_offset;
- this->public.clone_function = (linked_list_t * (*)(linked_list_t*,void*(*)(void*)))clone_function;
- this->public.destroy = (void (*) (linked_list_t *))destroy;
- this->public.destroy_offset = (void (*) (linked_list_t *,size_t))destroy_offset;
- this->public.destroy_function = (void (*)(linked_list_t*,void(*)(void*)))destroy_function;
-
- this->count = 0;
- this->first = NULL;
- this->last = NULL;
+ private_linked_list_t *this;
+
+ INIT(this,
+ .public = {
+ .get_count = _get_count,
+ .create_iterator = _create_iterator,
+ .create_enumerator = _create_enumerator,
+ .get_first = _get_first,
+ .get_last = _get_last,
+ .find_first = (void*)_find_first,
+ .find_last = (void*)_find_last,
+ .insert_first = _insert_first,
+ .insert_last = _insert_last,
+ .remove_first = _remove_first,
+ .remove_last = _remove_last,
+ .remove = _remove_,
+ .remove_at = (void*)_remove_at,
+ .invoke_offset = (void*)_invoke_offset,
+ .invoke_function = (void*)_invoke_function,
+ .clone_offset = _clone_offset,
+ .clone_function = _clone_function,
+ .destroy = _destroy,
+ .destroy_offset = _destroy_offset,
+ .destroy_function = _destroy_function,
+ },
+ );
return &this->public;
}