base: add List_model::apply_first

The new 'apply_first' method enables users of the list model to manually
traverse the list model via the 'Element::next' method instead of
iterating via 'for_each'. This is needed in situations where the
list-model elements are visited via recursion, not via a loop.

Issue #3094
This commit is contained in:
Norman Feske 2018-12-23 16:37:50 +01:00
parent 554a100407
commit cd29ca3c40

View File

@ -22,6 +22,7 @@
/* Genode includes */
#include <util/xml_node.h>
#include <util/list.h>
#include <base/log.h>
namespace Genode { template <typename> class List_model; }
@ -94,6 +95,21 @@ class Genode::List_model
}
}
/**
* Apply functor 'fn' to the first element of the list model
*
* Using this method combined with the 'Element::next' method, the list
* model can be traversed manually. This is handy in situations where
* the list-model elements are visited via recursive function calls
* instead of a 'for_each' loop.
*/
template <typename FN>
void apply_first(FN const &fn) const
{
if (Element const *e = _elements.first())
fn(static_cast<ELEM const &>(*e));
}
/**
* Remove all elements from the data model
*