From cd29ca3c40b746c65856dce933fbbf573911c214 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Sun, 23 Dec 2018 16:37:50 +0100 Subject: [PATCH] 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 --- repos/base/include/util/list_model.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/repos/base/include/util/list_model.h b/repos/base/include/util/list_model.h index 3a4b043f30..5f950ac6d1 100644 --- a/repos/base/include/util/list_model.h +++ b/repos/base/include/util/list_model.h @@ -22,6 +22,7 @@ /* Genode includes */ #include #include +#include namespace Genode { template 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 + void apply_first(FN const &fn) const + { + if (Element const *e = _elements.first()) + fn(static_cast(*e)); + } + /** * Remove all elements from the data model *