MyGUI
3.2.1
|
00001 /* 00002 * This source file is part of MyGUI. For the latest info, see http://mygui.info/ 00003 * Distributed under the MIT License 00004 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT) 00005 */ 00006 00007 #ifndef __MYGUI_SINGLETON_H__ 00008 #define __MYGUI_SINGLETON_H__ 00009 00010 #include "MyGUI_Diagnostic.h" 00011 00012 namespace MyGUI 00013 { 00014 00015 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC || MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE 00016 template <class T> 00017 class Singleton 00018 #else 00019 template <class T> 00020 class MYGUI_EXPORT Singleton 00021 #endif 00022 { 00023 public: 00024 typedef Singleton<T> Base; 00025 00026 Singleton() 00027 { 00028 MYGUI_ASSERT(nullptr == msInstance, "Singleton instance " << getClassTypeName() << " already exsist"); 00029 msInstance = static_cast<T*>(this); 00030 } 00031 00032 virtual ~Singleton() 00033 { 00034 MYGUI_ASSERT(nullptr != msInstance, "Destroying Singleton instance " << getClassTypeName() << " before constructing it."); 00035 msInstance = nullptr; 00036 } 00037 00038 static T& getInstance() 00039 { 00040 MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance " << getClassTypeName() << " was not created"); 00041 return (*getInstancePtr()); 00042 } 00043 00044 static T* getInstancePtr() 00045 { 00046 return msInstance; 00047 } 00048 00049 static const char* getClassTypeName() 00050 { 00051 return mClassTypeName; 00052 } 00053 00054 private: 00055 static T* msInstance; 00056 static const char* mClassTypeName; 00057 }; 00058 00059 } // namespace MyGUI 00060 00061 #endif // __MYGUI_SINGLETON_H__