-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.cpp
More file actions
62 lines (52 loc) · 1.91 KB
/
6.cpp
File metadata and controls
62 lines (52 loc) · 1.91 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
61
62
#include <loki/Singleton.h>
#include <iostream>
#include <string>
using namespace Loki;
using namespace std;
class LogImpl{
friend class CreateUsingNew<LogImpl>;
public:
void Create();
void Write(const char* msg) { cout << "Log call: " << msg << endl; }
private:
LogImpl() = default;
LogImpl(const LogImpl&) = delete;
LogImpl& operator=(const LogImpl&) = delete;
~LogImpl() { cout << "Destructor of LogImpl\n"; }
};
typedef SingletonHolder<LogImpl, CreateUsingNew, SingletonWithLongevity> Log;
class KeyboardImpl{
friend class CreateUsingNew<KeyboardImpl>;
public:
void Create();
void Print() { cout << "I'm printing from KeyboardImpl\n"; Log::Instance().Write("KeyboardImpl::Print()"); }
private:
KeyboardImpl() = default;
KeyboardImpl(const KeyboardImpl&) = delete;
KeyboardImpl& operator=(const KeyboardImpl&) = delete;
~KeyboardImpl() { cout << "Destructor of KeyboardImpl\n"; Log::Instance().Write("KeyboardImpl::~KeyboardImpl()"); }
};
class DisplayImpl{
friend class CreateUsingNew<DisplayImpl>;
public:
void Create();
void Show() { cout << "I show from DisplayImpl\n"; Log::Instance().Write("DisplayImpl::Show()"); }
private:
DisplayImpl() = default;
DisplayImpl(const DisplayImpl&) = delete;
DisplayImpl& operator=(const DisplayImpl&) = delete;
~DisplayImpl() { cout << "Destructor of DisplayImpl\n"; Log::Instance().Write("DisplayImpl::~DisplayImpl()"); }
};
inline unsigned int GetLongevity(KeyboardImpl*) { return 1; }
inline unsigned int GetLongevity(DisplayImpl*) { return 1; }
inline unsigned int GetLongevity(LogImpl*) { return 2; }
typedef SingletonHolder<KeyboardImpl, CreateUsingNew, SingletonWithLongevity> Keyboard;
typedef SingletonHolder<DisplayImpl, CreateUsingNew, SingletonWithLongevity> Display;
int main()
{
Keyboard::Instance().Print();
Display::Instance().Show();
Keyboard::Instance().Print();
return 0;
}
// clang++ -std=c++11 -I. loki-0.1.7/src/Singleton.cpp 6.cpp