-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdi.cpp
73 lines (57 loc) · 1.44 KB
/
di.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
#include "di.h"
namespace di::detail {
const BindingsState& BindingsState::fromBindings(const Bindings& bindings)
{
return bindings.state_;
}
BindingsState& BindingsState::fromBindings(Bindings& bindings)
{
return bindings.state_;
}
const ScopeState& ScopeState::fromScope(const Scope& scope)
{
return scope.state_;
}
ScopeState& ScopeState::fromScope(Scope& scope)
{
return scope.state_;
}
void BindingsState::registerAtScope(ScopeState& scope) const
{
for (const auto& [interfaceType, implMap] : interfaceMap_)
for (const auto& [implType, implData] : implMap)
scope.setServiceImpl(interfaceType, implData);
}
void ScopeState::setServiceImpl(std::type_index interfaceType, const ImplData& impl)
{
serviceImpls_[interfaceType] = impl;
}
void ScopeStack::push(ScopeState& scope)
{
scopes_.push_back(&scope);
}
void ScopeStack::pop(ScopeState& scope)
{
if (scopes_.back() != &scope)
throw std::runtime_error("detected mismatched dependency scope stack");
scopes_.pop_back();
}
ScopeState& ScopeStack::top()
{
if (scopes_.empty())
throw std::runtime_error("no active dependency scope");
return *scopes_.back();
}
ScopeGuard::ScopeGuard(ScopeStack& stack, ScopeState& scope) :
stack_{&stack},
scope_{&scope}
{
stack.push(scope);
}
ScopeGuard::~ScopeGuard()
{
if (stack_ != nullptr && scope_ != nullptr)
stack_->pop(*scope_);
}
ScopeStack globalScopeStack;
}