From c6f73d365ab42e3d86ebfb9bb66d13d3f95eff37 Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Fri, 10 Jul 2015 12:26:11 +0200 Subject: [PATCH] unmanaged singleton: allow private constructors Introduces a class Unmanaged_singleton_constructor that can be declared as friend to be able to call unmanaged_singleton on classes with a private constructor. Enables the appliance of the singleton pattern. Ref #1625 --- .../src/base/include/unmanaged_singleton.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/repos/base/src/base/include/unmanaged_singleton.h b/repos/base/src/base/include/unmanaged_singleton.h index 754bf234b..578275e2d 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); }