File: MyGUI_Singleton.h

package info (click to toggle)
mygui 3.4.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 38,800 kB
  • sloc: cpp: 122,825; ansic: 30,231; xml: 15,792; cs: 12,601; tcl: 776; python: 397; makefile: 38
file content (159 lines) | stat: -rw-r--r-- 4,001 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
 * Distributed under the MIT License
 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
 */

#ifndef MYGUI_SINGLETON_H_
#define MYGUI_SINGLETON_H_

#include "MyGUI_Diagnostic.h"

namespace MyGUI
{

#if MYGUI_COMPILER == MYGUI_COMPILER_MSVC
	template <class T>
	class Singleton
#else
	template <class T>
	class MYGUI_EXPORT MYGUI_OBSOLETE("Singleton class is deprecated. Do not use singletons.") Singleton
#endif
	{
	public:

		#if defined(__clang__)
			// This constructor is called before the `T` object is fully constructed, and
			// pointers are not dereferenced anyway, so UBSan shouldn't check vptrs.
			__attribute__((no_sanitize("vptr")))
		#endif
		Singleton()
		{
			MYGUI_ASSERT(nullptr == msInstance, "Singleton instance " << getClassTypeName() << " already exsist");
			msInstance = static_cast<T*>(this);
		}

		virtual ~Singleton()
		{
			if (nullptr == msInstance)
				MYGUI_LOG(Critical, "Destroying Singleton instance " << getClassTypeName() << " before constructing it.");
			msInstance = nullptr;
		}

		static T& getInstance()
		{
			MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance " << getClassTypeName() << " was not created");
			return (*getInstancePtr());
		}

		static T* getInstancePtr()
		{
			return msInstance;
		}

		static const char* getClassTypeName()
		{
			return mClassTypeName;
		}

	private:
		static T* msInstance;
		static const char* mClassTypeName;
	};

/*
    // Template Singleton class was replaces with a set of macroses, because there were too many issues with it,
    // all appearing in different compilers:
    //   - incorrect exporting template class;
    //   - static variables in headers issues;
    //   - explicit specialization/implicit instantiation issues;
    //   - many other related compile errors.
    // It is possible to move all template definitions into cpp code, but result looked even worse.

    // Usage:

	// in header
	class MyClass
	{
		MYGUI_SINGLETON_DECLARATION(MyClass);

		MyClass()
		// ...
	};

	// in cpp
	MYGUI_SINGLETON_DEFINITION(MyClass);

 	MyClass() : mSingletonHolder(this)
 	{
 		// ...
 	}
*/

	// proxy class to avoid calling initialiseSingleton/shutdownSingleton in constructor/destructor
	template<class T>
	class SingletonHolder
	{
	public:
		SingletonHolder(T* instance) :
			mInstance(instance)
		{
			mInstance->initialiseSingleton();
		}
		~SingletonHolder()
		{
			mInstance->shutdownSingleton();
		}
	private:
		T* mInstance;
	};

#define MYGUI_SINGLETON_DECLARATION(ClassName) \
	private: \
	friend MyGUI::SingletonHolder<ClassName>; \
	MyGUI::SingletonHolder<ClassName> mSingletonHolder; \
	void initialiseSingleton(); \
	void shutdownSingleton(); \
	 \
	public: \
	static ClassName& getInstance(); \
	static ClassName* getInstancePtr(); \
	static const char* getClassTypeName()

#define MYGUI_SINGLETON_DEFINITION(ClassName) \
	static ClassName* ClassName##Instance = nullptr; \
	static const char* ClassName##ClassTypeName = #ClassName; \
	 \
	void ClassName::initialiseSingleton() \
	{ \
		MYGUI_ASSERT(nullptr == ClassName##Instance, "Singleton instance " << getClassTypeName() << " already exsist"); \
		ClassName##Instance = this; \
	} \
	 \
	void ClassName::shutdownSingleton() \
	{ \
		if (nullptr == ClassName##Instance) \
			MYGUI_LOG(Critical, "Destroying Singleton instance " << getClassTypeName() << " before constructing it."); \
		ClassName##Instance = nullptr; \
	} \
	 \
	ClassName& ClassName::getInstance() \
	{ \
		MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance " << getClassTypeName() << " was not created"); \
		return (*getInstancePtr()); \
	} \
	 \
	ClassName* ClassName::getInstancePtr() \
	{ \
		return ClassName##Instance; \
	} \
	 \
	const char* ClassName::getClassTypeName() \
	{ \
		return ClassName##ClassTypeName; \
	} \
	static_assert(true, "require semicolon")

} // namespace MyGUI

#endif // MYGUI_SINGLETON_H_