-
Notifications
You must be signed in to change notification settings - Fork 0
/
di.h
313 lines (235 loc) · 8.09 KB
/
di.h
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#pragma once
#include <functional>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <stdexcept>
#include <unordered_map>
#include <typeindex>
#include <tuple>
namespace di {
class Bindings;
class Scope;
}// namespace di
namespace di::detail {
class BindingsState;
class ScopeState;
/// Helper class used to keep track of nested service construction in the current scope.
/// If an instance is constructed more than once within the same call stack, there's a cycle.
class CycleChecker
{
public:
class Guard
{
public:
Guard(CycleChecker& parent, std::type_index ctorType) :
parent_{&parent},
ctorType_{ctorType}
{
std::scoped_lock lock(parent.mtx_);
if (auto e = parent.activeCtors_.find(ctorType); e != parent.activeCtors_.end())
throw std::runtime_error("circular dependency");
parent_->activeCtors_.emplace(ctorType, true);
}
~Guard()
{
if (parent_)
{
std::scoped_lock lock(parent_->mtx_);
parent_->activeCtors_.erase(ctorType_);
}
}
private:
CycleChecker* parent_;
std::type_index ctorType_;
};
Guard makeGuard(std::type_index ctorType)
{
return Guard(*this, ctorType);
}
private:
std::mutex mtx_;
// Using map to avoid set include for this single use.
std::unordered_map<std::type_index, bool> activeCtors_;
};
struct ImplData
{
std::function<std::shared_ptr<void>()> factory;
};
class BindingsState
{
public:
template <typename TInterface, typename TImpl, typename ... TArgs>
void setService(TArgs&& ... args)
{
auto interfaceType = std::type_index(typeid(TInterface));
auto implType = std::type_index(typeid(TImpl));
ImplData impl;
impl.factory = [storedArgs = std::make_tuple(std::forward<TArgs>(args) ...)] () -> std::shared_ptr<void> {
return std::apply([](const auto& ... args){
return std::make_shared<TImpl>(args ...);
}, storedArgs);
};
interfaceMap_[interfaceType][implType] = std::move(impl);
}
void registerAtScope(ScopeState& scope) const;
static const BindingsState& fromBindings(const Bindings&);
static BindingsState& fromBindings(Bindings&);
private:
using ImplMap = std::unordered_map<std::type_index, ImplData>;
using InterfaceMap = std::unordered_map<std::type_index, ImplMap>;
// InterfaceType -> ImplType -> ImplData
InterfaceMap interfaceMap_;
};
template <typename Tag, typename U>
struct TaggedType {};
class ScopeState
{
public:
template <typename TInterface, typename Tag>
std::shared_ptr<TInterface> getService()
{
auto interfaceType = std::type_index{typeid(TInterface)};
auto serviceEntry = serviceImpls_.find(interfaceType);
if (serviceEntry == serviceImpls_.end())
throw std::runtime_error("service interface is not bound");
// Create non-cached instance.
if constexpr (std::is_same_v<Tag, tags::Exclusive>)
{
return std::static_pointer_cast<TInterface>(serviceEntry->second.factory());
}
auto instanceType = std::type_index{typeid(TaggedType<Tag, TInterface>)};
// Check for tagged instance (read lock).
{
std::shared_lock lock(mtx_);
auto e = serviceInstances_.find(instanceType);
if (e != serviceInstances_.end())
return std::static_pointer_cast<TInterface>(e->second);
}
auto cycleGuard = cycleChecker_.makeGuard(instanceType);
// Create instance.
std::shared_ptr<void> instance = serviceEntry->second.factory();
// Check again, then set tagged instance (write lock).
{
std::unique_lock lock(mtx_);
auto e = serviceInstances_.find(instanceType);
if (e != serviceInstances_.end())
return std::static_pointer_cast<TInterface>(e->second);
serviceInstances_.emplace(instanceType, instance);
return std::static_pointer_cast<TInterface>(instance);
}
}
void setServiceImpl(std::type_index interfaceType, const ImplData& impl);
static const ScopeState& fromScope(const Scope&);
static ScopeState& fromScope(Scope&);
private:
std::shared_mutex mtx_;
std::unordered_map<std::type_index, std::shared_ptr<void>> serviceInstances_;
std::unordered_map<std::type_index, ImplData> serviceImpls_;
CycleChecker cycleChecker_;
};
class ScopeStack
{
public:
void push(ScopeState& scope);
void pop(ScopeState& scope);
ScopeState& top();
private:
std::vector<ScopeState*> scopes_;
};
class ScopeGuard
{
public:
ScopeGuard(ScopeStack& stack, ScopeState& scope);
~ScopeGuard();
private:
ScopeStack* stack_;
ScopeState* scope_;
};
extern ScopeStack globalScopeStack;
template <typename TInterface, typename Tag>
std::shared_ptr<TInterface> getService()
{
auto& scope = globalScopeStack.top();
return scope.getService<TInterface, Tag>();
}
} // namespace di::detail
namespace di {
/// Bindings define which implementation and arguments to use when instantiating an interface.
class Bindings
{
public:
template <typename TInterface, typename TImpl, typename ... TArgs>
Bindings& service(TArgs&& ... args)
{
state_.setService<TInterface, TImpl>(std::forward<TArgs>(args) ...);
return *this;
}
private:
detail::BindingsState state_;
friend class detail::BindingsState;
};
/// Scope selects the bindings to be used in the current execution scope.
///
/// For the duration of its lifetime, the Scope instance is added to the global stack of active scopes.
/// Scopes must be destroyed in the inverse order of their creation, otherwise a runtime error is thrown.
class Scope
{
public:
template <typename ... TBindings>
explicit Scope(const TBindings& ... bindings) :
guard_{detail::globalScopeStack, state_}
{
using detail::BindingsState;
(BindingsState::fromBindings(bindings).registerAtScope(state_), ...);
}
Scope(const Scope&) = delete;
Scope& operator=(const Scope&) = delete;
Scope(Scope&&) = delete;
Scope& operator=(Scope&&) = delete;
void validate();
private:
detail::ScopeState state_;
detail::ScopeGuard guard_;
friend class detail::ScopeState;
};
namespace tags
{
struct Exclusive {};
struct Shared {};
}
/// A ServiceRef obtains a service instance of the given interface type.
///
/// The bindings of the top-most active scope are used to select an implementation.
/// If no implementation is bound, a runtime error is thrown.
///
/// Depending on the Tag template parameter, the instance may be cached and shared within
/// the active scope.
///
/// If tagged with tags::Unique, a new instance is created exclusively for this ServiceRef.
///
/// Otherwise, the tag type denotes the name under which the instance is shared.
/// A shared instance is created on first reference, then cached and re-used on further ones.
/// Once created, it remains cached until its active scope is destroyed.
/// By default, ServiceRefs are shared under the name tags::Shared; user-defined tags can be used as well.
/// Dependencies between shared instances must not result in cycles, otherwise a runtime error is thrown.
template <typename TInterface, typename Tag = tags::Shared>
class ServiceRef
{
public:
ServiceRef() :
ptr_{detail::getService<TInterface, Tag>()}
{}
ServiceRef(const ServiceRef&) = default;
ServiceRef& operator=(const ServiceRef&) = default;
ServiceRef(ServiceRef&&) = default;
ServiceRef& operator=(ServiceRef&&) = default;
const TInterface& operator*() const { return ptr_.operator*(); }
TInterface& operator*() { return ptr_.operator*(); }
const TInterface* operator->() const { return ptr_.operator->(); }
TInterface* operator->() { return ptr_.operator->(); }
operator std::shared_ptr<TInterface>() const { return ptr_; }
private:
std::shared_ptr<TInterface> ptr_;
};
}// namespace di