-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingleton.h
53 lines (44 loc) · 1.27 KB
/
Singleton.h
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
#ifndef SINGLETON_H_
#define SINGLETON_H_
#include <iostream>
#include <memory>
template <typename T>
class Singleton
{
public:
~Singleton()
{
if (managedObject)
{
delete managedObject;
managedObject = nullptr;
}
std::cout << "dtor Singleton [" << typeid(T).name() << "]" << std::endl; // this requires rtti, optional, just for debugging purpose
}
Singleton(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton& operator=(Singleton&&) = delete;
static const T& GetInstance()
{
return *(m_GetInstance()->managedObject);
}
static std::unique_ptr<Singleton<T>> AutoDestroy()
{
return std::unique_ptr<Singleton<T>>(m_GetInstance());
}
private:
Singleton(): managedObject(nullptr)
{
std::cout << "ctor Singleton [" << typeid(T).name() << "]" << std::endl; // this requires rtti, optional, just for debugging purpose
managedObject = new T();
}
static Singleton<T>* m_GetInstance()
{
static Singleton<T>* inst = new Singleton<T>();
return inst;
}
std::unique_ptr<Singleton<T>> instancePtr;
T* managedObject;
};
#endif /* SINGLETON_H_ */