MyGUI 3.2.0

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_Singleton.h
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #ifndef __MYGUI_SINGLETON_H__
23 #define __MYGUI_SINGLETON_H__
24
25 #include "MyGUI_Diagnostic.h"
26
27 namespace MyGUI
28 {
29
30 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC || MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
31 template <class T>
32 class Singleton
33 #else
34 template <class T>
35 class MYGUI_EXPORT Singleton
36 #endif
37 {
38 public:
39 typedef Singleton<T> Base;
40
41 Singleton()
42 {
43 MYGUI_ASSERT(nullptr == msInstance, "Singleton instance " << getClassTypeName() << " already exsist");
44 msInstance = static_cast<T*>(this);
45 }
46
47 virtual ~Singleton()
48 {
49 MYGUI_ASSERT(nullptr != msInstance, "Destroying Singleton instance " << getClassTypeName() << " before constructing it.");
50 msInstance = nullptr;
51 }
52
53 static T& getInstance()
54 {
55 MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance " << getClassTypeName() << " was not created");
56 return (*getInstancePtr());
57 }
58
59 static T* getInstancePtr()
60 {
61 return msInstance;
62 }
63
64 static const char* getClassTypeName()
65 {
66 return mClassTypeName;
67 }
68
69 private:
70 static T* msInstance;
71
72 protected:
73 static const char* mClassTypeName;
74 };
75
76 } // namespace MyGUI
77
78 #endif // __MYGUI_SINGLETON_H__
MyGUI_Diagnostic.h
MyGUI::Singleton::~Singleton
virtual ~Singleton()
Definition: MyGUI_Singleton.h:47
MyGUI::Singleton::getInstance
static T & getInstance()
Definition: MyGUI_Singleton.h:53
MyGUI::Singleton::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:64
MyGUI::Singleton::Singleton
Singleton()
Definition: MyGUI_Singleton.h:41
MYGUI_EXPORT
#define MYGUI_EXPORT
Definition: MyGUI_Platform.h:120
MyGUI::Singleton::getInstancePtr
static T * getInstancePtr()
Definition: MyGUI_Singleton.h:59
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:58
MyGUI::Singleton
Definition: MyGUI_Singleton.h:35
MyGUI::Singleton::mClassTypeName
static const char * mClassTypeName
Definition: MyGUI_Singleton.h:73
MyGUI::Singleton::Base
Singleton< T > Base
Definition: MyGUI_Singleton.h:39