Migrated linked_list_t to INIT/METHOD macros.

This commit is contained in:
Tobias Brunner
2011-07-06 09:43:45 +02:00
parent 3ee8fed445
commit 178760012c
+118 -173
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2007-2008 Tobias Brunner * Copyright (C) 2007-2011 Tobias Brunner
* Copyright (C) 2005-2006 Martin Willi * Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter * Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
@@ -51,13 +51,11 @@ struct element_t {
*/ */
element_t *element_create(void *value) element_t *element_create(void *value)
{ {
element_t *this = malloc_thing(element_t); element_t *this;
INIT(this,
this->previous = NULL; .value = value,
this->next = NULL; );
this->value = value; return this;
return (this);
} }
@@ -142,10 +140,8 @@ struct private_enumerator_t {
element_t *current; element_t *current;
}; };
/** METHOD(enumerator_t, enumerate, bool,
* Implementation of private_enumerator_t.enumerator.enumerate. private_enumerator_t *this, void **item)
*/
static bool enumerate(private_enumerator_t *this, void **item)
{ {
if (!this->current) if (!this->current)
{ {
@@ -167,33 +163,30 @@ static bool enumerate(private_enumerator_t *this, void **item)
return TRUE; return TRUE;
} }
/** METHOD(linked_list_t, create_enumerator, enumerator_t*,
* Implementation of linked_list_t.create_enumerator. private_linked_list_t *this)
*/
static enumerator_t* create_enumerator(private_linked_list_t *this)
{ {
private_enumerator_t *enumerator = malloc_thing(private_enumerator_t); private_enumerator_t *enumerator;
enumerator->enumerator.enumerate = (void*)enumerate; INIT(enumerator,
enumerator->enumerator.destroy = (void*)free; .enumerator = {
enumerator->list = this; .enumerate = (void*)_enumerate,
enumerator->current = NULL; .destroy = (void*)free,
},
.list = this,
);
return &enumerator->enumerator; return &enumerator->enumerator;
} }
/** METHOD(iterator_t, iterator_get_count, int,
* Implementation of iterator_t.get_count. private_iterator_t *this)
*/
static int get_list_count(private_iterator_t *this)
{ {
return this->list->count; return this->list->count;
} }
/** METHOD(iterator_t, iterate, bool,
* Implementation of iterator_t.iterate. private_iterator_t *this, void** value)
*/
static bool iterate(private_iterator_t *this, void** value)
{ {
if (this->forward) if (this->forward)
{ {
@@ -211,18 +204,14 @@ static bool iterate(private_iterator_t *this, void** value)
return TRUE; return TRUE;
} }
/** METHOD(iterator_t, iterator_reset, void,
* Implementation of iterator_t.reset. private_iterator_t *this)
*/
static void iterator_reset(private_iterator_t *this)
{ {
this->current = NULL; this->current = NULL;
} }
/** METHOD(iterator_t, iterator_remove, status_t,
* Implementation of iterator_t.remove. private_iterator_t *this)
*/
static status_t iterator_remove(private_iterator_t *this)
{ {
element_t *new_current; element_t *new_current;
@@ -281,10 +270,8 @@ static status_t iterator_remove(private_iterator_t *this)
return SUCCESS; return SUCCESS;
} }
/** METHOD(iterator_t, iterator_insert_before, void,
* Implementation of iterator_t.insert_before. private_iterator_t * iterator, void *item)
*/
static void insert_before(private_iterator_t * iterator, void *item)
{ {
if (iterator->current == NULL) if (iterator->current == NULL)
{ {
@@ -309,10 +296,8 @@ static void insert_before(private_iterator_t * iterator, void *item)
iterator->list->count++; iterator->list->count++;
} }
/** METHOD(iterator_t, iterator_replace, status_t,
* Implementation of iterator_t.replace. private_iterator_t *this, void **old_item, void *new_item)
*/
static status_t replace(private_iterator_t *this, void **old_item, void *new_item)
{ {
if (this->current == NULL) if (this->current == NULL)
{ {
@@ -327,10 +312,8 @@ static status_t replace(private_iterator_t *this, void **old_item, void *new_ite
return SUCCESS; return SUCCESS;
} }
/** METHOD(iterator_t, iterator_insert_after, void,
* Implementation of iterator_t.insert_after. private_iterator_t *iterator, void *item)
*/
static void insert_after(private_iterator_t *iterator, void *item)
{ {
if (iterator->current == NULL) if (iterator->current == NULL)
{ {
@@ -355,26 +338,20 @@ static void insert_after(private_iterator_t *iterator, void *item)
iterator->list->count++; iterator->list->count++;
} }
/** METHOD(iterator_t, iterator_destroy, void,
* Implementation of iterator_t.destroy. private_iterator_t *this)
*/
static void iterator_destroy(private_iterator_t *this)
{ {
free(this); free(this);
} }
/** METHOD(linked_list_t, get_count, int,
* Implementation of linked_list_t.get_count. private_linked_list_t *this)
*/
static int get_count(private_linked_list_t *this)
{ {
return this->count; return this->count;
} }
/** METHOD(linked_list_t, insert_first, void,
* Implementation of linked_list_t.insert_first. private_linked_list_t *this, void *item)
*/
static void insert_first(private_linked_list_t *this, void *item)
{ {
element_t *element; element_t *element;
@@ -401,7 +378,8 @@ static void insert_first(private_linked_list_t *this, void *item)
/** /**
* unlink an element form the list, returns following 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; element_t *next, *previous;
@@ -432,10 +410,8 @@ static element_t* remove_element(private_linked_list_t *this, element_t *element
return next; return next;
} }
/** METHOD(linked_list_t, get_first, status_t,
* Implementation of linked_list_t.get_first. private_linked_list_t *this, void **item)
*/
static status_t get_first(private_linked_list_t *this, void **item)
{ {
if (this->count == 0) if (this->count == 0)
{ {
@@ -445,10 +421,8 @@ static status_t get_first(private_linked_list_t *this, void **item)
return SUCCESS; return SUCCESS;
} }
/** METHOD(linked_list_t, remove_first, status_t,
* Implementation of linked_list_t.remove_first. private_linked_list_t *this, void **item)
*/
static status_t remove_first(private_linked_list_t *this, void **item)
{ {
if (get_first(this, item) == SUCCESS) if (get_first(this, item) == SUCCESS)
{ {
@@ -458,10 +432,8 @@ static status_t remove_first(private_linked_list_t *this, void **item)
return NOT_FOUND; return NOT_FOUND;
} }
/** METHOD(linked_list_t, insert_last, void,
* Implementation of linked_list_t.insert_last. private_linked_list_t *this, void *item)
*/
static void insert_last(private_linked_list_t *this, void *item)
{ {
element_t *element = element_create(item); element_t *element = element_create(item);
@@ -484,10 +456,8 @@ static void insert_last(private_linked_list_t *this, void *item)
this->count++; this->count++;
} }
/** METHOD(linked_list_t, get_last, status_t,
* Implementation of linked_list_t.get_last. private_linked_list_t *this, void **item)
*/
static status_t get_last(private_linked_list_t *this, void **item)
{ {
if (this->count == 0) if (this->count == 0)
{ {
@@ -497,10 +467,8 @@ static status_t get_last(private_linked_list_t *this, void **item)
return SUCCESS; return SUCCESS;
} }
/** METHOD(linked_list_t, remove_last, status_t,
* Implementation of linked_list_t.remove_last. private_linked_list_t *this, void **item)
*/
static status_t remove_last(private_linked_list_t *this, void **item)
{ {
if (get_last(this, item) == SUCCESS) if (get_last(this, item) == SUCCESS)
{ {
@@ -510,11 +478,8 @@ static status_t remove_last(private_linked_list_t *this, void **item)
return NOT_FOUND; return NOT_FOUND;
} }
/** METHOD(linked_list_t, remove_, int,
* Implementation of linked_list_t.remove. private_linked_list_t *this, void *item, bool (*compare)(void*,void*))
*/
static int remove_(private_linked_list_t *this, void *item,
bool (*compare)(void *,void*))
{ {
element_t *current = this->first; element_t *current = this->first;
int removed = 0; int removed = 0;
@@ -535,10 +500,8 @@ static int remove_(private_linked_list_t *this, void *item,
return removed; return removed;
} }
/** METHOD(linked_list_t, remove_at, void,
* Implementation of linked_list_t.remove_at. private_linked_list_t *this, private_enumerator_t *enumerator)
*/
static void remove_at(private_linked_list_t *this, private_enumerator_t *enumerator)
{ {
element_t *current; element_t *current;
@@ -550,11 +513,9 @@ static void remove_at(private_linked_list_t *this, private_enumerator_t *enumera
} }
} }
/** METHOD(linked_list_t, find_first, status_t,
* Implementation of linked_list_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)
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)
{ {
element_t *current = this->first; element_t *current = this->first;
@@ -574,11 +535,9 @@ static status_t find_first(private_linked_list_t *this, linked_list_match_t matc
return NOT_FOUND; return NOT_FOUND;
} }
/** METHOD(linked_list_t, find_last, status_t,
* Implementation of linked_list_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)
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)
{ {
element_t *current = this->last; element_t *current = this->last;
@@ -598,11 +557,9 @@ static status_t find_last(private_linked_list_t *this, linked_list_match_t match
return NOT_FOUND; return NOT_FOUND;
} }
/** METHOD(linked_list_t, invoke_offset, void,
* Implementation of linked_list_t.invoke_offset. private_linked_list_t *this, size_t offset,
*/ void *d1, void *d2, void *d3, void *d4, void *d5)
static void invoke_offset(private_linked_list_t *this, size_t offset,
void *d1, void *d2, void *d3, void *d4, void *d5)
{ {
element_t *current = this->first; element_t *current = this->first;
@@ -614,11 +571,9 @@ static void invoke_offset(private_linked_list_t *this, size_t offset,
} }
} }
/** METHOD(linked_list_t, invoke_function, void,
* Implementation of linked_list_t.invoke_function. private_linked_list_t *this, linked_list_invoke_t fn,
*/ void *d1, void *d2, void *d3, void *d4, void *d5)
static void invoke_function(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; element_t *current = this->first;
@@ -629,10 +584,8 @@ static void invoke_function(private_linked_list_t *this, linked_list_invoke_t fn
} }
} }
/** METHOD(linked_list_t, clone_offset, linked_list_t*,
* Implementation of linked_list_t.clone_offset private_linked_list_t *this, size_t offset)
*/
static linked_list_t *clone_offset(private_linked_list_t *this, size_t offset)
{ {
linked_list_t *clone = linked_list_create(); linked_list_t *clone = linked_list_create();
element_t *current = this->first; element_t *current = this->first;
@@ -647,10 +600,8 @@ static linked_list_t *clone_offset(private_linked_list_t *this, size_t offset)
return clone; return clone;
} }
/** METHOD(linked_list_t, clone_function, linked_list_t*,
* Implementation of linked_list_t.clone_function private_linked_list_t *this, void* (*fn)(void*))
*/
static linked_list_t *clone_function(private_linked_list_t *this, void* (*fn)(void*))
{ {
linked_list_t *clone = linked_list_create(); linked_list_t *clone = linked_list_create();
element_t *current = this->first; element_t *current = this->first;
@@ -664,10 +615,8 @@ static linked_list_t *clone_function(private_linked_list_t *this, void* (*fn)(vo
return clone; return clone;
} }
/** METHOD(linked_list_t, destroy, void,
* Implementation of linked_list_t.destroy. private_linked_list_t *this)
*/
static void destroy(private_linked_list_t *this)
{ {
void *value; void *value;
/* Remove all list items before destroying list */ /* Remove all list items before destroying list */
@@ -679,10 +628,8 @@ static void destroy(private_linked_list_t *this)
free(this); free(this);
} }
/** METHOD(linked_list_t, destroy_offset, void,
* Implementation of linked_list_t.destroy_offset. private_linked_list_t *this, size_t offset)
*/
static void destroy_offset(private_linked_list_t *this, size_t offset)
{ {
element_t *current = this->first, *next; element_t *current = this->first, *next;
@@ -697,10 +644,8 @@ static void destroy_offset(private_linked_list_t *this, size_t offset)
free(this); free(this);
} }
/** METHOD(linked_list_t, destroy_function, void,
* Implementation of linked_list_t.destroy_function. private_linked_list_t *this, void (*fn)(void*))
*/
static void destroy_function(private_linked_list_t *this, void (*fn)(void*))
{ {
element_t *current = this->first, *next; element_t *current = this->first, *next;
@@ -714,25 +659,25 @@ static void destroy_function(private_linked_list_t *this, void (*fn)(void*))
free(this); free(this);
} }
/** METHOD(linked_list_t, create_iterator, iterator_t*,
* Implementation of linked_list_t.create_iterator. private_linked_list_t *linked_list, bool forward)
*/
static iterator_t *create_iterator(private_linked_list_t *linked_list, bool forward)
{ {
private_iterator_t *this = malloc_thing(private_iterator_t); private_iterator_t *this;
this->public.get_count = (int (*) (iterator_t*)) get_list_count; INIT(this,
this->public.iterate = (bool (*) (iterator_t*, void **value)) iterate; .public = {
this->public.insert_before = (void (*) (iterator_t*, void *item)) insert_before; .get_count = _iterator_get_count,
this->public.insert_after = (void (*) (iterator_t*, void *item)) insert_after; .iterate = _iterate,
this->public.replace = (status_t (*) (iterator_t*, void **, void *)) replace; .insert_before = _iterator_insert_before,
this->public.remove = (status_t (*) (iterator_t*)) iterator_remove; .insert_after = _iterator_insert_after,
this->public.reset = (void (*) (iterator_t*)) iterator_reset; .replace = _iterator_replace,
this->public.destroy = (void (*) (iterator_t*)) iterator_destroy; .remove = _iterator_remove,
.reset = _iterator_reset,
this->forward = forward; .destroy = _iterator_destroy,
this->current = NULL; },
this->list = linked_list; .forward = forward,
.list = linked_list,
);
return &this->public; return &this->public;
} }
@@ -742,32 +687,32 @@ static iterator_t *create_iterator(private_linked_list_t *linked_list, bool forw
*/ */
linked_list_t *linked_list_create() linked_list_t *linked_list_create()
{ {
private_linked_list_t *this = malloc_thing(private_linked_list_t); private_linked_list_t *this;
this->public.get_count = (int (*) (linked_list_t *)) get_count; INIT(this,
this->public.create_iterator = (iterator_t * (*) (linked_list_t *,bool))create_iterator; .public = {
this->public.create_enumerator = (enumerator_t*(*)(linked_list_t*))create_enumerator; .get_count = _get_count,
this->public.get_first = (status_t (*) (linked_list_t *, void **item))get_first; .create_iterator = _create_iterator,
this->public.get_last = (status_t (*) (linked_list_t *, void **item))get_last; .create_enumerator = _create_enumerator,
this->public.find_first = (status_t (*) (linked_list_t *, linked_list_match_t,void**,...))find_first; .get_first = _get_first,
this->public.find_last = (status_t (*) (linked_list_t *, linked_list_match_t,void**,...))find_last; .get_last = _get_last,
this->public.insert_first = (void (*) (linked_list_t *, void *item))insert_first; .find_first = (void*)_find_first,
this->public.insert_last = (void (*) (linked_list_t *, void *item))insert_last; .find_last = (void*)_find_last,
this->public.remove_first = (status_t (*) (linked_list_t *, void **item))remove_first; .insert_first = _insert_first,
this->public.remove_last = (status_t (*) (linked_list_t *, void **item))remove_last; .insert_last = _insert_last,
this->public.remove = (int(*)(linked_list_t*, void *item, bool (*compare)(void *,void*)))remove_; .remove_first = _remove_first,
this->public.remove_at = (void(*)(linked_list_t*, enumerator_t *enumerator))remove_at; .remove_last = _remove_last,
this->public.invoke_offset = (void (*)(linked_list_t*,size_t,...))invoke_offset; .remove = _remove_,
this->public.invoke_function = (void (*)(linked_list_t*,linked_list_invoke_t,...))invoke_function; .remove_at = (void*)_remove_at,
this->public.clone_offset = (linked_list_t * (*)(linked_list_t*,size_t))clone_offset; .invoke_offset = (void*)_invoke_offset,
this->public.clone_function = (linked_list_t * (*)(linked_list_t*,void*(*)(void*)))clone_function; .invoke_function = (void*)_invoke_function,
this->public.destroy = (void (*) (linked_list_t *))destroy; .clone_offset = _clone_offset,
this->public.destroy_offset = (void (*) (linked_list_t *,size_t))destroy_offset; .clone_function = _clone_function,
this->public.destroy_function = (void (*)(linked_list_t*,void(*)(void*)))destroy_function; .destroy = _destroy,
.destroy_offset = _destroy_offset,
this->count = 0; .destroy_function = _destroy_function,
this->first = NULL; },
this->last = NULL; );
return &this->public; return &this->public;
} }