summaryrefslogtreecommitdiff
path: root/source/modules/asura-utils/singleton.hpp
blob: 9bb73363c554cda9f919baea1535f5b5929d9ce4 (plain)
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
#ifndef _ASURA_SINGLETON_H_
#define _ASURA_SINGLETON_H_

#include "UtilsConfig.h"

namespace AsuraEngine
{

	///
	/// ̳Singletonڵһʵʱʵ֮ٴʵᱨ
	/// 
	template<class T>
	class Singleton
	{
	public:

		static T* Get()
		{
			// ֮ǰûдһ
			if (!instance) instance = new T;
			// ʵ
			return instance;
		}

		static void Destroy()
		{
			delete instance;
			instance = nullptr;
		}

	protected:

		Singleton()
		{
			// instanceζִһʵǴġ
			ASSERT(!instance);
			// 򣬽ʵΪʵ
			instance = static_cast<T*>(this);
		};

		virtual ~Singleton() {};

		static T* instance;

	private:

		Singleton(const Singleton& singleton);

		Singleton& operator = (const Singleton& singleton);

	};

	// ʵʼΪ
	template<class T>
	T* Singleton<T>::instance = nullptr;

}

#endif // _ASURA_SINGLETON_H_