diff --git a/src/lib/depend.hpp b/src/lib/depend.hpp index ec0566bda..782bdc4bd 100644 --- a/src/lib/depend.hpp +++ b/src/lib/depend.hpp @@ -35,8 +35,8 @@ This code is heavily inspired by #define LIB_DEPEND_H -#include "lib/nobug-init.hpp" #include "lib/sync-classlock.hpp" +#include "lib/dependency-factory.hpp" namespace lib { @@ -52,7 +52,7 @@ namespace lib { * constitute a memory barrier, such as to force any memory writes happening \em within * the singleton ctor to be flushed and visible to other threads when releasing the lock? * To my understanding, the answer is yes. See - * [posix](http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap04.html#tag_04_10) + * [POSIX](http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap04.html#tag_04_10) * @param SI the class of the Singleton instance * @param Create policy defining how to create/destroy the instance * @param Life policy defining how to manage Singleton Lifecycle @@ -61,10 +61,9 @@ namespace lib { class Depend { typedef ClassLock SyncLock; - typedef DependencyFactory Factory; static SI* volatile instance; - static Factory factory; + static DependencyFactory factory; public: @@ -81,7 +80,7 @@ namespace lib { SyncLock guard; if (!instance) - instance = factory.buildInstance(); + instance = static_cast (factory.buildInstance()); } ENSURE (instance); return *instance; @@ -111,22 +110,27 @@ namespace lib { dropReplacement() { SyncLock guard; - factory.discardMock (instance); // EX_FREE - instance = factory.restore (); // EX_SANE + factory.restore (instance); // EX_FREE } - typedef typename Factory::InstanceConstructor Constructor; + typedef DependencyFactory::InstanceConstructor Constructor; - Depend (Constructor ctor = buildSingleton()); + Depend (Constructor ctor = buildSingleton()) + { + factory.installConstructorFunction (ctor); + } - private: + // standard copy operations applicable }; // Storage for SingletonFactory's static fields... template - SI* volatile Depend::instance; + SI* volatile Depend::instance; + + template + DependencyFactory Depend::factory; diff --git a/src/lib/dependency-factory.hpp b/src/lib/dependency-factory.hpp index 705f51bde..de9926ba0 100644 --- a/src/lib/dependency-factory.hpp +++ b/src/lib/dependency-factory.hpp @@ -13,36 +13,91 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -==================================================================== -This code is heavily inspired by - The Loki Library (loki-lib/trunk/include/loki/Singleton.h) - Copyright (c) 2001 by Andrei Alexandrescu - Loki code accompanies the book: - Alexandrescu, Andrei. "Modern C++ Design: Generic Programming - and Design Patterns Applied". - Copyright (c) 2001. Addison-Wesley. ISBN 0201704315 - */ + #ifndef LIB_DEPENDENCY_FACTORY_H #define LIB_DEPENDENCY_FACTORY_H -#include "lib/depend.hpp" -//#include "lib/nobug-init.hpp" +#include "lib/nobug-init.hpp" //#include "lib/sync-classlock.hpp" namespace lib { - /** */ + /** + * Factory to generate and manage service objects classified by type. + */ + class DependencyFactory + { + public: + void* + buildInstance() + { + UNIMPLEMENTED("invoke the ctor function"); + } + + void + deconfigure (void* existingInstance) + { + UNIMPLEMENTED("deregister and destroy a managed product"); + } + + template + void + takeOwnership (TAR* newInstance) + { + UNIMPLEMENTED("enrol existing instance and prepare deleter function"); + } + + template + void + restore (TAR* volatile & activeInstance) + { + UNIMPLEMENTED("disable and destroy temporary shadowing instance and restore the dormant original instance"); + } + + template + void + shaddow (TAR* volatile & activeInstance) + { + UNIMPLEMENTED("set up a temporary replacement, allowing to restore the original later"); + } + + + typedef void* (*InstanceConstructor)(void); + + void + installConstructorFunction (InstanceConstructor ctor) + { + UNIMPLEMENTED("set up constructor function, unless already configured"); + } + + template + static void* + createSingletonInstance() + { + UNIMPLEMENTED("trampoline function to create a singleton"); + } + + + template + friend InstanceConstructor + buildSingleton() + { + return & createSingletonInstance; + } + + }; + } // namespace lib #endif diff --git a/tests/library/dependency-factory-test.cpp b/tests/library/dependency-factory-test.cpp index be6a292eb..76c1d59a1 100644 --- a/tests/library/dependency-factory-test.cpp +++ b/tests/library/dependency-factory-test.cpp @@ -46,7 +46,7 @@ namespace test{ struct Sub : TestTargetObj { - static uint created = 0; + static uint created; uint instanceID_; Sub() @@ -60,6 +60,8 @@ namespace test{ + TestTargetObj::operator string(); } }; + uint Sub::created = 0; + struct SubSub : Sub @@ -181,7 +183,7 @@ namespace test{ static SubSubSub* customFactoryFunction (void) { - SubSubSub* newObject = DependencyFactory::createSingleton(); + SubSubSub* newObject = static_cast (DependencyFactory::createSingletonInstance()); newObject->instanceID_ = MAX_ID + 10; return newObject; } diff --git a/tests/library/test-target-obj.hpp b/tests/library/test-target-obj.hpp index 20327e0d6..7b0b3b402 100644 --- a/tests/library/test-target-obj.hpp +++ b/tests/library/test-target-obj.hpp @@ -74,7 +74,7 @@ namespace test{ inline - TestTargetObj::~TestTargetObj() throw() + TestTargetObj::~TestTargetObj() ///< EX_FREE { delete heapData_; delete[] heapArray_;