-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsingleton.h
More file actions
60 lines (48 loc) · 1.07 KB
/
Copy pathsingleton.h
File metadata and controls
60 lines (48 loc) · 1.07 KB
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 __INC_SINGLETON_H__
#define __INC_SINGLETON_H__
#pragma once
#include <cassert>
#if !defined(WIN32) && !defined(__forceinline)
#define __forceinline __attribute__((always_inline))
#endif
template <typename T> class CSingleton
{
static inline T * ms_singleton = nullptr;
public:
CSingleton()
{
assert(!ms_singleton);
ms_singleton = static_cast<T*>(this);
}
virtual ~CSingleton()
{
assert(ms_singleton);
ms_singleton = nullptr;
}
__forceinline static T & Instance()
{
assert(ms_singleton);
return (*ms_singleton);
}
__forceinline static T * InstancePtr()
{
return (ms_singleton);
}
__forceinline static T & instance()
{
assert(ms_singleton);
return (*ms_singleton);
}
__forceinline static T * instance_ptr()
{
return (ms_singleton);
}
// prevent manager 0x0 by deleting copy/assignment operators
CSingleton(const CSingleton&) = delete;
CSingleton& operator=(const CSingleton&) = delete;
CSingleton(CSingleton&&) = delete;
CSingleton& operator=(CSingleton&&) = delete;
};
template <typename T>
using singleton = CSingleton<T>;
#endif