-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsingletonmanager.hpp
55 lines (44 loc) · 1.08 KB
/
singletonmanager.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
#ifndef SINGLETONMANAGER_HPP
#define SINGLETONMANAGER_HPP
#include "singleton.hpp"
class Singleton1
{
public:
Singleton1() { std::cout << "Singleton1" << std::endl; }
~Singleton1() { std::cout << "~Singleton1" << std::endl; }
};
class Singleton2
{
public:
Singleton2() { std::cout << "Singleton2" << std::endl; }
~Singleton2() { std::cout << "~Singleton2" << std::endl; }
};
class SingletonManager
{
SingletonManager()
: m_s1_ptr(new Singleton1)
, m_s2_ptr(new Singleton2)
{}
~SingletonManager() {}
DISABLE_COPY_MOVE(SingletonManager)
std::mutex m_mutex;
std::unique_ptr<Singleton1> m_s1_ptr;
std::unique_ptr<Singleton2> m_s2_ptr;
public:
static auto instance() -> SingletonManager &
{
static SingletonManager s; // C++11 thread safe
return s;
}
Singleton1 &singleton1()
{
std::unique_lock locker(m_mutex);
return *m_s1_ptr;
}
auto singleton2() -> Singleton2 &
{
std::unique_lock locker(m_mutex);
return *m_s2_ptr;
}
};
#endif // SINGLETONMANAGER_HPP