blob: 386ae712505372ee892b2a813d9097c4e236a2fe (
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
60
|
#ifndef ASSERT
#ifdef NDEBUG
#define ASSERT(x) { false ? (void)(x) : (void)0; }
#else
#ifdef _WIN32
#define ASURA_DEBUG_BREAK() __debugbreak()
#else
#define ASURA_DEBUG_BREAK() raise(SIGTRAP)
#endif
#define ASSERT(x) do { const volatile bool asura_assert_b____ = !(x); if(asura_assert_b____) ASURA_DEBUG_BREAK(); } while (false)
#endif
#endif
///
/// ̳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;
|