diff options
-rw-r--r-- | src/Common/Configurable.h | 4 | ||||
-rw-r--r-- | src/Common/Initializable.h | 4 | ||||
-rw-r--r-- | src/Common/SingletonPtr.h | 37 | ||||
-rw-r--r-- | src/Core/ConnectionManager.h | 3 |
4 files changed, 36 insertions, 12 deletions
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 <typename T> class SingletonPtr; + class Initializable { private: + template <typename T> friend class SingletonPtr; + static std::stack<Initializable*> 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<Initializable*>(ptr); - if(in) - in->init(); + if(in) { + in->initialized = true; + in->doInit(); + } + + Configurable *c = dynamic_cast<Configurable*>(ptr); + if(c) + ConfigManager::get()->registerConfigurable(c); + + if(in) { + initializing = false; + initialized = true; + } } - public: - SingletonPtr() : ptr(0) {} + virtual void doDeinit() { + Configurable *c = dynamic_cast<Configurable*>(ptr); + if(c) + ConfigManager::get()->unregisterConfigurable(c); + + Initializable *in = dynamic_cast<Initializable*>(ptr); + if(in) { + in->doDeinit(); + in->initialized = false; + } - virtual ~SingletonPtr() { delete ptr; + ptr = 0; } + public: + SingletonPtr() : ptr(0) {} + T *get() { if(!ptr) init(); diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index 86c342c..fdc8a46 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -27,6 +27,7 @@ #include <Common/Configurable.h> #include <Common/Exception.h> #include <Common/HostInfo.h> +#include <Common/Initializable.h> #include <Common/RequestManager.h> #include <Common/SingletonPtr.h> @@ -43,7 +44,7 @@ class Packet; namespace Core { -class ConnectionManager : public Common::Configurable { +class ConnectionManager : public Common::Configurable, public Common::Initializable { private: static Common::SingletonPtr<ConnectionManager> connectionManager; |