-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadsetting.cpp
More file actions
50 lines (41 loc) · 1.12 KB
/
threadsetting.cpp
File metadata and controls
50 lines (41 loc) · 1.12 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
// Documentation link:
// https://sourcemaking.com/design_patterns/singleton/cpp/1
#include "threadsettinginterface.h"
#include <memory>
// This class is designed to instanciate of singleton object
class ThreadSetting : public ThreadSettingInterface
{
unsigned m_nbThread;
static const unsigned m_nbThreadMax = 4;
static ThreadSetting* s_instance;
// As the ctor is private, is class cannot be instanciate else than through
// its static method 'instance()'
ThreadSetting(unsigned v = 0)
{
SetNbThread(v);
}
public:
unsigned GetNbThread() const
{
return m_nbThread;
}
void SetNbThread(unsigned v)
{
if (v == 0)
v = m_nbThreadMax;
m_nbThread = (v <= m_nbThreadMax) ? v : m_nbThreadMax;
}
static ThreadSetting* instance()
{
if (!s_instance)
s_instance = new ThreadSetting;
return s_instance;
}
};
ThreadSettingInterface* ThreadSettingInterface::instance()
{
return ThreadSetting::instance();
}
// Allocating and initializing ThreadSetting's static data member.
// The pointer is being allocated - not the object inself.
ThreadSetting *ThreadSetting::s_instance = 0;