forked from rasmus-storjohann/Inject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurator.cpp
More file actions
56 lines (45 loc) · 1.38 KB
/
Configurator.cpp
File metadata and controls
56 lines (45 loc) · 1.38 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
#include "Configurator.hpp"
using namespace Injection;
Injector Configurator::CreateInjector()
{
_unsortedTypes = {};
for (auto& type : _types)
{
type.second._sortedState = Detail::Type::State::Unsorted;
_unsortedTypes.insert(type.first);
}
_sortedInitializers = {};
while (!_unsortedTypes.empty())
{
SortDependentInitializersTopographically(*_unsortedTypes.begin());
}
auto injector = Injector();
while (!_sortedInitializers.empty())
{
_sortedInitializers.top()(injector);
_sortedInitializers.pop();
}
return std::move(injector);
}
void Configurator::SortDependentInitializersTopographically(int typeId)
{
auto& type = _types[typeId];
if (type._sortedState == Detail::Type::State::Sorting)
{
throw std::runtime_error(type._debugTypeName + " appears to be part of a cycle");
}
if (type._sortedState == Detail::Type::State::Unsorted)
{
_unsortedTypes.erase(typeId);
type._sortedState = Detail::Type::State::Sorting;
for (auto dependentTypeId : type._dependentTypeIds)
{
SortDependentInitializersTopographically(dependentTypeId);
}
type._sortedState = Detail::Type::State::Sorted;
if (type.IsInitializerFunctionSet())
{
_sortedInitializers.push(type._initializerFunction);
}
}
}