-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.cpp
174 lines (132 loc) · 5.56 KB
/
Example.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <dependency_injection.h>
#include <fstream>
#include <iostream>
std::ofstream logFile("destructor_log.txt");
struct CoolInterface {
virtual void doCoolStuff() = 0;
};
struct RadInterface {
virtual void doRadStuff() = 0;
};
class CoolClass : public CoolInterface {
public:
void doCoolStuff() override { std::cout << "CoolClass::doCoolStuff()" << std::endl; }
};
class RadClass : public RadInterface {
int radNumber = 0;
public:
RadClass(int radNumber) : radNumber(radNumber) {
std::cout << "RadClass::RadClass(" << radNumber << ")" << std::endl;
}
~RadClass() { std::cout << "DELETE RadClass::~RadClass()" << std::endl; }
void doRadStuff() override {
std::cout << "RadClass::doRadStuff() with number " << radNumber << std::endl;
}
};
struct SomeService {
virtual ~SomeService() = default;
virtual void Increment() = 0;
virtual int GetCounter() = 0;
};
class SomeServiceImpl : public SomeService {
int counter = 0;
public:
SomeServiceImpl() { std::cout << "SomeServiceImpl::SomeServiceImpl()" << std::endl; }
SomeServiceImpl(int startCount) : counter(startCount) {
std::cout << "SomeServiceImpl::SomeServiceImpl(" << startCount << ")" << std::endl;
}
~SomeServiceImpl() {
//
std::cout << "DELETE SomeServiceImpl::~SomeServiceImpl()" << std::endl;
if (logFile.is_open()) {
logFile << "DELETE SomeServiceImpl::~SomeServiceImpl() with counter " << counter
<< std::endl;
}
}
void Increment() override { counter++; }
int GetCounter() override { return counter; }
};
class SomeConcreteClass {
int counter = 0;
public:
SomeConcreteClass() { std::cout << "SomeConcreteClass::SomeConcreteClass()" << std::endl; }
~SomeConcreteClass() {
std::cout << "DELETE SomeConcreteClass::~SomeConcreteClass()" << std::endl;
}
void Increment() { counter++; }
int GetCounter() { return counter; }
};
// SomeServiceImpl someServiceInstance(123);
void doThings() {
// auto x = 69;
DependencyInjection::Container container;
// container.RegisterSingleton<SomeService, SomeServiceImpl>();
// auto& someServiceReference = container.Get<SomeService>();
// someServiceReference->Increment();
// someServiceReference->Increment();
// std::cout << "someServiceReference->GetCounter() = " << someServiceReference->GetCounter()
// << std::endl;
// // But we can also reset it so it makes a new instance
// container.ResetSingleton<SomeService>();
// std::cout << "someServiceReference->GetCounter() = " << someServiceReference->GetCounter()
// << std::endl; // should be 0 now, new instance of SomeServiceImpl
// // Finally, I want to support passing constructor arguments in both of these scenarios:
// // 1 - on RegisterSingleton
// // 2 - on ResetSingleton
// // container.RegisterSingleton<SomeService, SomeServiceImpl>(42);
// // // or on ResetSingleton
// // container.ResetSingleton<SomeService>(42);
// // previous examples should still work, of course
// container.RegisterType<CoolClass>();
// // container.Register<CoolInterface, CoolClass>();
// container.RegisterInterface<RadInterface, RadClass, int>();
// auto newCoolThing = container.Make<CoolClass>();
// // auto newCoolThing = container.Make<CoolInterface>();
// newCoolThing->doCoolStuff();
// auto newRadThing = container.Make<RadInterface>(42);
// newRadThing->doRadStuff();
// // Do things using the singleton
// DI::RegisterSingleton<CoolInterface, CoolClass>();
// auto& globalCoolThing = DI::Get<CoolInterface>();
// globalCoolThing->doCoolStuff();
// Test passing an instance...
SomeServiceImpl someServiceInstance(123);
DI::RegisterSingleton<SomeServiceImpl>(someServiceInstance);
auto* someServiceReference = DI::Get<SomeServiceImpl>();
std::cout << "someServiceReference->GetCounter() = " << someServiceReference->GetCounter()
<< std::endl;
// auto instance = std::unique_ptr<SomeService>(new SomeServiceImpl{123});
// DI::RegisterSingleton<SomeService>(std::move(instance));
// auto* someServiceReference = DI::Get<SomeService>();
// std::cout << "someServiceReference->GetCounter() = " << someServiceReference->GetCounter()
// << std::endl;
// DI::RegisterSingleton<SomeConcreteClass>(std::make_unique<SomeConcreteClass>());
// auto& someServiceReference = DI::Get<SomeConcreteClass>();
// someServiceReference->Increment();
// DI::RegisterSingletonType<RadClass>(42);
// auto& radThing = DI::Get<RadClass>();
// radThing->doRadStuff();
// DI::RegisterSingletonInterface<RadInterface, RadClass>(42);
// auto* radThing = DI::Get<RadInterface>();
// radThing->doRadStuff();
// DI::RegisterSingletonInterface<CoolInterface, CoolClass>();
// auto* coolThing = DI::Get<CoolInterface>();
// coolThing->doCoolStuff();
}
int main() {
try {
doThings();
// The some service I registered should be deleted here...
// Let's try to get it anyway
// auto& someServiceReference = DI::Get<SomeServiceImpl>();
// // This should throw an exception
// someServiceReference->Increment();
// std::cout << "someServiceReference->GetCounter() = " <<
// someServiceReference->GetCounter()
// << std::endl;
// DI::ResetSingleton<SomeService>();
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}