MyGUI  3.2.0
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>
36 #endif
37  {
38  public:
39  typedef Singleton<T> Base;
40 
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 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC || MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
71  static T* msInstance;
72 
73  static const char* mClassTypeName;
74 #else
75  static MYGUI_EXPORT T* msInstance;
76 
77  static MYGUI_EXPORT const char* mClassTypeName;
78 #endif
79  };
80 
81 } // namespace MyGUI
82 
83 #endif // __MYGUI_SINGLETON_H__
virtual ~Singleton()
static T & getInstance()
static const char * getClassTypeName()
#define MYGUI_EXPORT
static T * getInstancePtr()
#define MYGUI_ASSERT(exp, dest)
Singleton< T > Base