enumerator: Add venumerate() method to enumerator_t that takes a va_list

This will allow us to implement e.g. enumerator_cleaner without having to
use that unportable 5 pointer forwarding or having to define a callback for
each instance.

A generic implementation for enumerate() is provided so only venumerate()
has to be implemented, which may be simplified by using the VA_ARGS_VGET()
macro.
This commit is contained in:
Tobias Brunner
2017-05-26 13:56:44 +02:00
parent 5297c65398
commit 16bffa8b55
2 changed files with 51 additions and 8 deletions
+21 -3
View File
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2008-2013 Tobias Brunner
* Copyright (C) 2008-2017 Tobias Brunner
* Copyright (C) 2007 Martin Willi
* Hochschule fuer Technik Rapperswil
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -31,6 +31,25 @@
#include <utils/debug.h>
/*
* Described in header.
*/
bool enumerator_enumerate_default(enumerator_t *enumerator, ...)
{
va_list args;
bool result;
if (!enumerator->venumerate)
{
DBG1(DBG_LIB, "!!! ENUMERATE DEFAULT: venumerate() missing !!!");
return FALSE;
}
va_start(args, enumerator);
result = enumerator->venumerate(enumerator, args);
va_end(args);
return result;
}
/**
* Implementation of enumerator_create_empty().enumerate
*/
@@ -646,4 +665,3 @@ enumerator_t *enumerator_create_single(void *item, void (*cleanup)(void *item))
return &this->public;
}
+30 -5
View File
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2013 Tobias Brunner
* Copyright (C) 2013-2017 Tobias Brunner
* Copyright (C) 2007 Martin Willi
* Hochschule fuer Technik Rapperswil
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -34,8 +34,11 @@ struct enumerator_t {
/**
* Enumerate collection.
*
* The enumerate function takes a variable argument list containing
* pointers where the enumerated values get written.
* The enumerate() method takes a variable number of pointer arguments
* where the enumerated values get written to.
*
* @note Just assigning the generic enumerator_enumerate_default() function
* that calls the enumerator's venumerate() method is usually enough.
*
* @param ... variable list of enumerated items, implementation dependent
* @return TRUE if pointers returned
@@ -43,11 +46,33 @@ struct enumerator_t {
bool (*enumerate)(enumerator_t *this, ...);
/**
* Destroy a enumerator instance.
* Enumerate collection.
*
* The venumerate() method takes a variable argument list containing
* pointers where the enumerated values get written to.
*
* To simplify the implementation the VA_ARGS_VGET() macro may be used.
*
* @param args variable list of enumerated items, implementation dependent
* @return TRUE if pointers returned
*/
bool (*venumerate)(enumerator_t *this, va_list args);
/**
* Destroy an enumerator_t instance.
*/
void (*destroy)(enumerator_t *this);
};
/**
* Generic implementation of enumerator_t::enumerate() that simply calls
* the enumerator's venumerate() method.
*
* @param enumerator the enumerator
* @param ... arguments passed to enumerate()
*/
bool enumerator_enumerate_default(enumerator_t *enumerator, ...);
/**
* Create an enumerator which enumerates over nothing
*