-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.hpp
62 lines (51 loc) · 1.15 KB
/
singleton.hpp
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
#ifndef SINGLETON_HPP
#define SINGLETON_HPP
#include "pthread_mutex.hpp"
#include <memory>
template<typename DataType, int instanceId = 0>
class Singleton
{
public:
static DataType* instance()
{
if (NULL == dataHolderM.get())
{
PthreadGuard lock(dbLockMutexM);
if (NULL == dataHolderM.get())
{
dataHolderM.reset(new DataType);
}
}
return dataHolderM.get();
}
private:
Singleton(){};
~Singleton(){};
static std::unique_ptr<DataType> dataHolderM;
static PthreadMutex dbLockMutexM;
};
template<typename DataType, int instanceId>
std::unique_ptr<DataType> Singleton<DataType, instanceId>::dataHolderM;
template<typename DataType, int instanceId>
PthreadMutex Singleton<DataType, instanceId>::dbLockMutexM;
template<typename DataType, int instanceId = 0>
class LeakSingleton
{
public:
static DataType* instance()
{
return dataM;
}
static void init()
{
dataM = new DataType;
}
private:
LeakSingleton(){};
~LeakSingleton(){};
static DataType* dataM;
static PthreadMutex* dbLockMutexM;
};
template<typename DataType, int instanceId>
DataType* LeakSingleton<DataType, instanceId>::dataM = NULL;
#endif /* SINGLETON_HPP */