From d002476c5e6b8f6bc728063fc8e7ce74da562e57 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 20 Nov 2008 16:38:51 +0100 Subject: SingletonPtr ueberarbeitet --- src/Common/Configurable.h | 4 +--- src/Common/Initializable.h | 4 ++++ src/Common/SingletonPtr.h | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 11 deletions(-) (limited to 'src/Common') diff --git a/src/Common/Configurable.h b/src/Common/Configurable.h index 90641a0..47f919d 100644 --- a/src/Common/Configurable.h +++ b/src/Common/Configurable.h @@ -20,15 +20,13 @@ #ifndef MAD_COMMON_CONFIGURABLE_H_ #define MAD_COMMON_CONFIGURABLE_H_ -#include "Initializable.h" - namespace Mad { namespace Common { class ConfigEntry; class ConfigManager; -class Configurable : virtual public Initializable { +class Configurable { public: virtual ~Configurable() {} diff --git a/src/Common/Initializable.h b/src/Common/Initializable.h index 27ab5bf..9f177da 100644 --- a/src/Common/Initializable.h +++ b/src/Common/Initializable.h @@ -25,8 +25,12 @@ namespace Mad { namespace Common { +template class SingletonPtr; + class Initializable { private: + template friend class SingletonPtr; + static std::stack initializedObjects; static bool logInit; diff --git a/src/Common/SingletonPtr.h b/src/Common/SingletonPtr.h index ea4eda1..03d0040 100644 --- a/src/Common/SingletonPtr.h +++ b/src/Common/SingletonPtr.h @@ -21,6 +21,8 @@ #define MAD_COMMON_SINGLETONPTR_H_ #include "Initializable.h" +#include "Configurable.h" +#include "ConfigManager.h" namespace Mad { namespace Common { @@ -32,23 +34,42 @@ class SingletonPtr : public virtual Initializable { protected: virtual void doInit() { - if(ptr) - delete ptr; - ptr = new T(); Initializable *in = dynamic_cast(ptr); - if(in) - in->init(); + if(in) { + in->initialized = true; + in->doInit(); + } + + Configurable *c = dynamic_cast(ptr); + if(c) + ConfigManager::get()->registerConfigurable(c); + + if(in) { + initializing = false; + initialized = true; + } } - public: - SingletonPtr() : ptr(0) {} + virtual void doDeinit() { + Configurable *c = dynamic_cast(ptr); + if(c) + ConfigManager::get()->unregisterConfigurable(c); + + Initializable *in = dynamic_cast(ptr); + if(in) { + in->doDeinit(); + in->initialized = false; + } - virtual ~SingletonPtr() { delete ptr; + ptr = 0; } + public: + SingletonPtr() : ptr(0) {} + T *get() { if(!ptr) init(); -- cgit v1.2.3