MyGUI: MyGUI_Singleton.h Source File

MyGUI 3.2.2

MyGUI_Singleton.h
Go to the documentation of this file.
1 /*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7 #ifndef MYGUI_SINGLETON_H_
8 #define MYGUI_SINGLETON_H_
9
10 #include "MyGUI_Diagnostic.h"
11
12 namespace MyGUI
13 {
14
15 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC || MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
16 template <class T>
17 class Singleton
18 #else
19 template <class T>
20 class MYGUI_EXPORT Singleton
21 #endif
22 {
23 public:
24 typedef Singleton<T> Base;
25
26 Singleton()
27 {
28 MYGUI_ASSERT(nullptr == msInstance, "Singleton instance " << getClassTypeName() << " already exsist");
29 msInstance = static_cast<T*>(this);
30 }
31
32 virtual ~Singleton()
33 {
34 MYGUI_ASSERT(nullptr != msInstance, "Destroying Singleton instance " << getClassTypeName() << " before constructing it.");
35 msInstance = nullptr;
36 }
37
38 static T& getInstance()
39 {
40 MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance " << getClassTypeName() << " was not created");
41 return (*getInstancePtr());
42 }
43
44 static T* getInstancePtr()
45 {
46 return msInstance;
47 }
48
49 static const char* getClassTypeName()
50 {
51 return mClassTypeName;
52 }
53
54 private:
55 static T* msInstance;
56 static const char* mClassTypeName;
57 };
58
59 } // namespace MyGUI
60
61 #endif // MYGUI_SINGLETON_H_
MyGUI::Singleton::getInstance
static T & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI_Diagnostic.h
MyGUI::Singleton::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:49
MyGUI::Singleton::Singleton
Singleton()
Definition: MyGUI_Singleton.h:26
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:42
MyGUI::Singleton::getInstancePtr
static T * getInstancePtr()
Definition: MyGUI_Singleton.h:44
MYGUI_EXPORT
#define MYGUI_EXPORT
Definition: MyGUI_Platform.h:103
MyGUI::Singleton
Definition: MyGUI_Singleton.h:20
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::Singleton::Base
Singleton< T > Base
Definition: MyGUI_Singleton.h:24
MyGUI::Singleton::~Singleton
virtual ~Singleton()
Definition: MyGUI_Singleton.h:32