linked-list: Change return value of find_first() and signature of its callback

This avoids the unportable five pointer hack.
This commit is contained in:
Tobias Brunner
2017-05-26 13:56:44 +02:00
parent 8a2e4d4a8b
commit 2e4d110d1e
29 changed files with 526 additions and 398 deletions
+34 -8
View File
@@ -47,6 +47,17 @@ struct element_t {
element_t *next;
};
/*
* Described in header
*/
bool linked_list_match_str(void *item, va_list args)
{
char *a = item, *b;
VA_ARGS_VGET(args, b);
return streq(a, b);
}
/**
* Creates an empty linked list object.
*/
@@ -371,26 +382,41 @@ METHOD(linked_list_t, remove_at, void,
}
}
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)
METHOD(linked_list_t, find_first, bool,
private_linked_list_t *this, linked_list_match_t match, void **item, ...)
{
element_t *current = this->first;
va_list args;
bool matched = FALSE;
if (!match && !item)
{
return FALSE;
}
while (current)
{
if ((match && match(current->value, d1, d2, d3, d4, d5)) ||
(!match && item && current->value == *item))
if (match)
{
va_start(args, item);
matched = match(current->value, args);
va_end(args);
}
else
{
matched = current->value == *item;
}
if (matched)
{
if (item != NULL)
{
*item = current->value;
}
return SUCCESS;
return TRUE;
}
current = current->next;
}
return NOT_FOUND;
return FALSE;
}
METHOD(linked_list_t, invoke_offset, void,
@@ -548,7 +574,7 @@ linked_list_t *linked_list_create()
.reset_enumerator = (void*)_reset_enumerator,
.get_first = _get_first,
.get_last = _get_last,
.find_first = (void*)_find_first,
.find_first = _find_first,
.insert_first = _insert_first,
.insert_last = _insert_last,
.insert_before = (void*)_insert_before,
+21 -15
View File
@@ -28,15 +28,22 @@ typedef struct linked_list_t linked_list_t;
#include <collections/enumerator.h>
/**
* Method to match elements in a linked list (used in find_* functions)
* Function to match elements in a linked list
*
* @param item current list item
* @param ... user supplied data (only pointers, at most 5)
* @return
* - TRUE, if the item matched
* - FALSE, otherwise
* @param args user supplied data
* @return TRUE, if the item matched, FALSE otherwise
*/
typedef bool (*linked_list_match_t)(void *item, ...);
typedef bool (*linked_list_match_t)(void *item, va_list args);
/**
* Helper function to match a string in a linked list of strings
*
* @param item list item (char*)
* @param args user supplied data (char*)
* @return
*/
bool linked_list_match_str(void *item, va_list args);
/**
* Function to be invoked on elements in a linked list
@@ -167,21 +174,20 @@ struct linked_list_t {
*
* The first object passed to the match function is the current list item,
* followed by the user supplied data.
* If the supplied function returns TRUE this function returns SUCCESS, and
* the current object is returned in the third parameter, otherwise,
* If the supplied function returns TRUE so does this function, and the
* current object is returned in the third parameter (if given), otherwise,
* the next item is checked.
*
* If match is NULL, *item and the current object are compared.
*
* @warning Only use pointers as user supplied data.
*
* @param match comparison function to call on each object, or NULL
* @param item the list item, if found
* @param ... user data to supply to match function (limited to 5 arguments)
* @return SUCCESS if found, NOT_FOUND otherwise
* @param item the list item, if found, or NULL
* @param ... user data to supply to match function
* @return TRUE if found, FALSE otherwise (or if neither match,
* nor item is supplied)
*/
status_t (*find_first) (linked_list_t *this, linked_list_match_t match,
void **item, ...);
bool (*find_first)(linked_list_t *this, linked_list_match_t match,
void **item, ...);
/**
* Invoke a method on all of the contained objects.