From eebe31aa7ef7a56c662e212910979b34a3fd61a7 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 17 Mar 2018 23:41:56 +0100 Subject: [PATCH] DI: change to heap allocation for singletons up to now we used placement into a static buffer. While this approach is somewhat cool, I can't see much practical benefit anymore, given that we use an elaborate framework which rules out the use of Meyers Singleton. And given that with C++11 we're able just to use std::unique_ptr to do all work. Moreover, the intended configurability will become much simpler by relying on a _closure_ to produce a heap-allocated instance for all cases likewise. The only possible problem I can see is that critical infrastructure might rely on failsafe creation of some singleton. Up to now this scenario remains theoretical however --- research/try.cpp | 28 ++++++-------------------- wiki/thinkPad.ichthyo.mm | 43 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/research/try.cpp b/research/try.cpp index afe11ee2f..20d32aa1d 100644 --- a/research/try.cpp +++ b/research/try.cpp @@ -56,6 +56,7 @@ typedef unsigned int uint; #include #include #include +#include #define SHOW_TYPE(_TY_) \ @@ -75,39 +76,22 @@ using lib::meta::enable_if; class InstanceHolder : boost::noncopyable { - /** storage for the service instance */ - char buff_[sizeof(TAR)]; - bool alive_ = false; + std::unique_ptr instance_; public: - ~InstanceHolder() - { - if (alive_) - try { - alive_ = false; - reinterpret_cast (buff_). ~TAR(); - } - catch(...) - { // no logging since we might be in static shutdown - lumiera_error(); // reset errorflag - } - } - - TAR* buildInstance() { - if (alive_) + if (instance_) throw error::Fatal("Attempt to double-create a singleton service. " "Either the application logic, or the compiler " "or runtime system is seriously broken" ,error::LUMIERA_ERROR_LIFECYCLE); // place new instance into embedded buffer - TAR* newInstance = new(&buff_) TAR (); - alive_ = true; - return newInstance; + instance_.reset (new TAR{}); + return instance_.get(); } }; @@ -232,7 +216,7 @@ main (int, char**) SHOW_EXPR( checksum ); Depend dumm; - Depend::factory = [](){ return nullptr; }; +// Depend::factory = [](){ return nullptr; }; SHOW_EXPR( dumm().probe() ); diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 8f1b18fdf..168ce5fcd 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -26993,7 +26993,48 @@ - + + + + + + + + +

+ statische Storage ist irgendwie cool +

+ + +
+
+ + + + + + + + + + + +

+ das gilt im Besonderen für eine default-Storage als Singleton. +

+

+ Wofern wir dynamisch konfigurieren (wollen), muß dieser Default stets statisch bereitgestellt werden, +

+

+ selbst wenn die dynamische Konfiguration so angelegt ist, daß die Storage nie benötigt wird +

+ + +
+
+ + +