From 7c915969222ed1da2fd3f4aac6676035ebf1fd7c Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Mon, 10 Jun 2024 16:41:10 +0200 Subject: [PATCH] Exception-less overload of 'Id_space::apply' This patch allows the use of the 'Id_space' utility without catching 'Unknown_id' exceptions. Instead, the new 'apply' overload takes a second functor 'missing_fn' as argument, which is called whenever the lookup fails. Issue #5244 --- repos/base/include/base/id_space.h | 41 ++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/repos/base/include/base/id_space.h b/repos/base/include/base/id_space.h index 8af8a2e29f..193ea25823 100644 --- a/repos/base/include/base/id_space.h +++ b/repos/base/include/base/id_space.h @@ -182,6 +182,31 @@ class Genode::Id_space : public Noncopyable _elements.first()->template _for_each(fn); } + /** + * Apply functor 'fn' to object with given ID, or call 'missing_fn' + * + * See 'for_each' for a description of the 'ARG' argument. + * If the ID is not known, 'missing_fn' is called instead of 'fn'. + * Both 'fn' and 'missing_fn' must have the same return type. + */ + template + auto apply(Id id, FN const &fn, auto const &missing_fn) + -> typename Trait::Functor::Return_type + { + T *obj_ptr = nullptr; + { + Mutex::Guard guard(_mutex); + + if (_elements.first()) + if (Element *e = _elements.first()->_lookup(id)) + obj_ptr = &e->_obj; + } + if (obj_ptr) + return fn(static_cast(*obj_ptr)); + else + return missing_fn(); + } + /** * Apply functor 'fn' to object with given ID * @@ -193,20 +218,8 @@ class Genode::Id_space : public Noncopyable auto apply(Id id, FN const &fn) -> typename Trait::Functor::Return_type { - T *obj = nullptr; - { - Mutex::Guard guard(_mutex); - - if (!_elements.first()) - throw Unknown_id(); - - if (Element *e = _elements.first()->_lookup(id)) - obj = &e->_obj; - } - if (obj) - return fn(static_cast(*obj)); - else - throw Unknown_id(); + using Result = typename Trait::Functor::Return_type; + return apply(id, fn, [&] () -> Result { throw Unknown_id(); }); } /**