diff options
Diffstat (limited to 'src/common/singleton.h')
-rw-r--r-- | src/common/singleton.h | 77 |
1 files changed, 47 insertions, 30 deletions
diff --git a/src/common/singleton.h b/src/common/singleton.h index c1b28d9..25e1648 100644 --- a/src/common/singleton.h +++ b/src/common/singleton.h @@ -26,34 +26,51 @@ template<typename T> class CSingleton { - protected: - static T* mInstance; - - public: - static T& GetInstance() { - assert(mInstance != nullptr); - return *mInstance; - } - - static T* GetInstancePointer() { - assert(mInstance != nullptr); - return mInstance; - } - - static bool IsCreated() { - return mInstance != nullptr; - } - - CSingleton() { - assert(mInstance == nullptr); - mInstance = static_cast<T *>(this); - } - - virtual ~CSingleton() { - mInstance = nullptr; - } - - private: - CSingleton& operator=(const CSingleton<T> &); - CSingleton(const CSingleton<T> &); +protected: + static T* m_instance; + +public: + static T& GetInstance() + { + assert(m_instance != nullptr); + return *m_instance; + } + + static T* GetInstancePointer() + { + assert(m_instance != nullptr); + return m_instance; + } + + static bool IsCreated() + { + return m_instance != nullptr; + } + + CSingleton() + { + assert(m_instance == nullptr); + m_instance = static_cast<T *>(this); + } + + virtual ~CSingleton() + { + m_instance = nullptr; + } + + #ifdef TESTS + static void ReplaceInstance(T* newInstance) + { + assert(newInstance != nullptr); + + if (m_instance != nullptr) + delete m_instance; + + m_instance = newInstance; + } + #endif + +private: + CSingleton& operator=(const CSingleton<T> &); + CSingleton(const CSingleton<T> &); }; |