base: add Reconstructible::conditional method

The new 'conditional' method simplifies the typical use case for
'Constructible' objects where the constructed/destructed state depends
on a configuration parameter. The method alleviates the need to
re-implement the logic again and again.

The patch also removes the 'Reconstructible' constructor arguments
because they are unused.

Fixes #3006
This commit is contained in:
Norman Feske 2018-10-01 14:04:50 +02:00 committed by Christian Helmuth
parent fe303f0e46
commit 7d641d5f1f
4 changed files with 20 additions and 11 deletions

View File

@ -163,7 +163,6 @@ static constexpr Genode::Boot_modules_header & header() {
Platform::Platform() Platform::Platform()
: bootstrap_region((addr_t)&_prog_img_beg, : bootstrap_region((addr_t)&_prog_img_beg,
((addr_t)&_prog_img_end - (addr_t)&_prog_img_beg)), ((addr_t)&_prog_img_end - (addr_t)&_prog_img_beg)),
core_pd(ram_alloc),
core_elf_addr(header().base), core_elf_addr(header().base),
core_elf(core_elf_addr) core_elf(core_elf_addr)
{ {

View File

@ -127,7 +127,7 @@ class Bootstrap::Platform
Bootstrap::Pic pic { }; Bootstrap::Pic pic { };
Ram_allocator ram_alloc { }; Ram_allocator ram_alloc { };
Memory_region const bootstrap_region; Memory_region const bootstrap_region;
Genode::Constructible<Pd> core_pd; Genode::Constructible<Pd> core_pd { };
addr_t core_elf_addr; addr_t core_elf_addr;
Elf core_elf; Elf core_elf;

View File

@ -136,6 +136,19 @@ class Genode::Reconstructible : Noncopyable
*/ */
bool is_constructed() const { return constructed(); } bool is_constructed() const { return constructed(); }
/**
* Construct or destruct volatile object according to 'condition'
*/
template <typename... ARGS>
void conditional(bool condition, ARGS &&... args)
{
if (condition && !constructed())
construct(args...);
if (!condition && constructed())
destruct();
}
/** /**
* Access contained object * Access contained object
*/ */
@ -161,11 +174,8 @@ class Genode::Reconstructible : Noncopyable
template <typename MT> template <typename MT>
struct Genode::Constructible : Reconstructible<MT> struct Genode::Constructible : Reconstructible<MT>
{ {
template <typename... ARGS> Constructible()
Constructible(ARGS &&...) : Reconstructible<MT>((typename Reconstructible<MT>::Lazy *)nullptr) { }
:
Reconstructible<MT>((typename Reconstructible<MT>::Lazy *)nullptr)
{ }
}; };
#endif /* _INCLUDE__UTIL__RECONSTRUCTIBLE_H_ */ #endif /* _INCLUDE__UTIL__RECONSTRUCTIBLE_H_ */

View File

@ -166,10 +166,10 @@ struct Depot_query::Main
typedef Constructible<Expanding_reporter> Constructible_reporter; typedef Constructible<Expanding_reporter> Constructible_reporter;
Constructible_reporter _directory_reporter { _env, "directory" }; Constructible_reporter _directory_reporter { };
Constructible_reporter _blueprint_reporter { _env, "blueprint" }; Constructible_reporter _blueprint_reporter { };
Constructible_reporter _dependencies_reporter { _env, "dependencies" }; Constructible_reporter _dependencies_reporter { };
Constructible_reporter _user_reporter { _env, "user" }; Constructible_reporter _user_reporter { };
template <typename T, typename... ARGS> template <typename T, typename... ARGS>
static void _construct_if(bool condition, Constructible<T> &obj, ARGS &&... args) static void _construct_if(bool condition, Constructible<T> &obj, ARGS &&... args)