-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdi.cpp
221 lines (192 loc) · 7.49 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
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
/*
* Copyright (C) 2011
*/
#ifndef DI_HEADER_ONLY
#include "di.h"
#endif
#include <map>
namespace di
{
namespace internal
{
DI_INLINE void* BeanBase::convertTo(const InstanceBase& typeToConvertTo) const /* throw (DependencyInjectionException) */
{
const void* obj = getConcrete();
for(std::vector<InstanceConverterBase*>::const_iterator it = isAlsoTheseInstances.begin(); it != isAlsoTheseInstances.end(); it++)
{
InstanceConverterBase* typeConverter = (*it);
if (typeConverter->isInstanceToConvertTo(typeToConvertTo))
{
void* ret = ((InstanceConverterBase*)typeConverter)->doConvert((void*)obj);
if (ret == NULL)
throw DependencyInjectionException("Failed to convert a \"%s\" to a \"%s\" using a dynamic_cast for ", typeConverter->toString().c_str(), type.getInstanceInfo().name());
return ret;
}
}
return NULL;
}
DI_INLINE bool BeanBase::canConvertTo(const internal::InstanceBase& typeToConvertTo) const
{
for(std::vector<InstanceConverterBase*>::const_iterator it = isAlsoTheseInstances.begin(); it != isAlsoTheseInstances.end(); it++)
{
InstanceConverterBase* typeConverter = (*it);
if (typeConverter->isInstanceToConvertTo(typeToConvertTo))
return true;
}
return false;
}
}
DI_INLINE internal::BeanBase* Context::find(const internal::InstanceBase& typeInfo, const char* id, bool exact)
{
for(std::vector<internal::BeanBase*>::iterator it = instances.begin(); it != instances.end(); it++)
{
internal::BeanBase* instance = (*it);
if (((exact && instance->type.getInstanceInfo() == typeInfo.getInstanceInfo()) ||
(!exact && instance->canConvertTo(typeInfo))) &&
(id == NULL || instance->id == id))
return instance;
}
return NULL;
}
DI_INLINE void Context::findAll(std::vector<internal::BeanBase*>& ret, const internal::InstanceBase& typeInfo, const char* id, bool exact)
{
for(std::vector<internal::BeanBase*>::iterator it = instances.begin(); it != instances.end(); it++)
{
internal::BeanBase* instance = (*it);
if (((exact && instance->type.getInstanceInfo() == typeInfo.getInstanceInfo()) ||
(!exact && instance->canConvertTo(typeInfo))) &&
(id == NULL || instance->id == id))
ret.push_back(instance);
}
}
DI_INLINE void Context::resetBeans()
{
internal::BeanBase* instance;
for(std::vector<internal::BeanBase*>::iterator it = instances.begin(); it != instances.end(); it++)
{
// since this results in the instance destructor being called ... in case some moron
// throws from the destructor, we don't want to stop deleting.
try
{
instance = (*it);
instance->reset();
}
catch (DependencyInjectionException& die) { throw die; }
catch (...)
{
// this prints a message to the log as long as there is a logger set in the exception
DependencyInjectionException ex("Exception detected in the destructor of the instance for \"%s.\"",instance->toString().c_str());
}
}
curPhase = stopped;
}
DI_INLINE void Context::stop() /* throw (DependencyInjectionException) */
{
if (! isStopped())
{
internal::BeanBase* instance;
// pre destroy step
for(std::vector<internal::BeanBase*>::iterator it = instances.begin(); it != instances.end(); it++)
{
instance = (*it);
try
{
instance->doPreDestroy();
}
catch (DependencyInjectionException& die) { throw die; }
catch (...)
{
// hum .... what to do? c++ sucks here in that I cannot get a handle to the
// original exception. ... I can either log and rethrow or I can throw another
// known exception. I wish I could wrap and throw.
resetBeans();
throw DependencyInjectionException("Unknown exception intercepted while executing PreDestroy phase on \"%s.\"", instance->toString().c_str());
}
}
}
// clear out the instances.
resetBeans();
}
DI_INLINE void Context::clear()
{
try { stop(); } catch (DependencyInjectionException& ex) {}
for(std::vector<internal::BeanBase*>::iterator it = instances.begin(); it != instances.end(); it++)
delete (*it);
instances.clear();
curPhase = initial;
}
DI_INLINE void Context::start() /* throw (DependencyInjectionException) */
{
if (isStarted())
throw DependencyInjectionException("Called start for a second time on a di::Context.");
// First instantiate
// This loop is stupid. Instead the order of instantiation ought to be determined.
// but for now this works.
internal::BeanBase* instance;
try
{
std::vector<internal::BeanBase*> workingList;
workingList = instances;
while (workingList.size() > 0)
{
unsigned int preCount = workingList.size();
internal::BeanBase* firstNotInstantiated = NULL;
std::vector<internal::BeanBase*> tmpvector;
for(std::vector<internal::BeanBase*>::iterator it = workingList.begin(); it != workingList.end(); it++)
{
instance = (*it);
if (instance->factory->dependenciesSatisfied(this))
instance->instantiateBean(this);
else if (firstNotInstantiated == NULL)
{
firstNotInstantiated = instance;
tmpvector.push_back(instance);
}
}
workingList = tmpvector;
if (preCount == workingList.size() && workingList.size() > 0)
throw DependencyInjectionException("Cannot resolve constructor dependencies for \"%s\"", firstNotInstantiated->toString().c_str());
}
}
catch (DependencyInjectionException& die) { throw die; }
catch (...)
{
// hum .... what to do? c++ sucks here in that I cannot get a handle to the
// original exception. ... I can either log and rethrow or I can throw another
// known exception. I wish I could wrap and throw.
resetBeans();
throw DependencyInjectionException("Unknown exception intercepted while instantiating \"%s.\"", instance->toString().c_str());
}
// we need a map of Instance types to the Beans that provide those types
for(std::vector<internal::BeanBase*>::iterator it = instances.begin(); it != instances.end(); it++)
{
instance = (*it);
// find out what it requires.
std::vector<internal::RequirementBase*>& requirements = instance->getRequirements();
for (std::vector<internal::RequirementBase*>::iterator rit = requirements.begin(); rit != requirements.end(); rit++)
{
internal::RequirementBase* requirement = (*rit);
requirement->satisfy(instance,this);
}
}
// post construct step
for(std::vector<internal::BeanBase*>::iterator it = instances.begin(); it != instances.end(); it++)
{
instance = (*it);
try
{
instance->doPostConstruct();
}
catch (DependencyInjectionException& die) { throw die; }
catch (...)
{
// hum .... what to do? c++ sucks here in that I cannot get a handle to the
// original exception. ... I can either log and rethrow or I can throw another
// known exception. I wish I could wrap and throw.
resetBeans();
throw DependencyInjectionException("Unknown exception intercepted while executing postConstruct phase on \"%s.\"", instance->toString().c_str());
}
}
curPhase = started;
}
}