MyGUI  3.4.0
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
16  template <class T>
17  class Singleton
18 #else
19  template <class T>
21 #endif
22  {
23  public:
24  using Base = Singleton<T>;
25 
26  #if defined(__clang__)
27  // This constructor is called before the `T` object is fully constructed, and
28  // pointers are not dereferenced anyway, so UBSan shouldn't check vptrs.
29  __attribute__((no_sanitize("vptr")))
30  #endif
32  {
33  MYGUI_ASSERT(nullptr == msInstance, "Singleton instance " << getClassTypeName() << " already exsist");
34  msInstance = static_cast<T*>(this);
35  }
36 
37  virtual ~Singleton()
38  {
39  if (nullptr == msInstance)
40  MYGUI_LOG(Critical, "Destroying Singleton instance " << getClassTypeName() << " before constructing it.");
41  msInstance = nullptr;
42  }
43 
44  static T& getInstance()
45  {
46  MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance " << getClassTypeName() << " was not created");
47  return (*getInstancePtr());
48  }
49 
50  static T* getInstancePtr()
51  {
52  return msInstance;
53  }
54 
55  static const char* getClassTypeName()
56  {
57  return mClassTypeName;
58  }
59 
60  private:
61  static T* msInstance;
62  static const char* mClassTypeName;
63  };
64 
65 } // namespace MyGUI
66 
67 #endif // MYGUI_SINGLETON_H_
MyGUI::Singleton::getInstance
static T & getInstance()
Definition: MyGUI_Singleton.h:44
MyGUI_Diagnostic.h
MyGUI::Singleton::getClassTypeName
static const char * getClassTypeName()
Definition: MyGUI_Singleton.h:55
MyGUI::Singleton::Singleton
Singleton()
Definition: MyGUI_Singleton.h:31
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:34
MyGUI::Singleton::getInstancePtr
static T * getInstancePtr()
Definition: MyGUI_Singleton.h:50
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:22
MYGUI_EXPORT
#define MYGUI_EXPORT
Definition: MyGUI_Platform.h:89
MyGUI::Singleton
Definition: MyGUI_Singleton.h:22
MyGUI
Definition: MyGUI_ActionController.h:15
MyGUI::Singleton::~Singleton
virtual ~Singleton()
Definition: MyGUI_Singleton.h:37