diff --git a/repos/base/src/base/include/unmanaged_singleton.h b/repos/base/src/base/include/unmanaged_singleton.h index 754bf234be..578275e2d4 100644 --- a/repos/base/src/base/include/unmanaged_singleton.h +++ b/repos/base/src/base/include/unmanaged_singleton.h @@ -42,6 +42,22 @@ */ inline void * operator new(Genode::size_t, void * p) { return p; } +/** + * Helper class for the use of unmanaged_singleton with the singleton pattern + * + * If a class wants to make its constructor private to force the singleton + * pattern, it can declare this class as friend to be able to still use the + * unmanaged_singleton template. + */ +struct Unmanaged_singleton_constructor +{ + /** + * Call the constructor of 'T' with arguments 'args' at 'dst' + */ + template + static void call(char * const dst, ARGS... args) { new (dst) T(args...); } +}; + /** * Create a singleton object that isn't implicitly constructed or destructed * @@ -66,7 +82,7 @@ static inline T * unmanaged_singleton(ARGS... args) /* execute constructor on first call */ if (!object_constructed) { object_constructed = true; - new (&object_space) T(args...); + Unmanaged_singleton_constructor::call(object_space, args...); } return reinterpret_cast(object_space); }