Skip to content

Commit bb36a22

Browse files
committed
Streamline injector definitions
Signed-off-by: Julian Fleischer <[email protected]>
1 parent 0576fd3 commit bb36a22

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/dependency_injector.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ class UnregisteredDependenciesError : public InjectionError {
149149
mutable std::string m_error_message;
150150

151151
public:
152-
std::vector<std::pair<std::string, std::type_index>> m_missingDependencies;
152+
std::vector<std::pair<std::string, std::type_index>> m_missing_dependencies;
153153
explicit UnregisteredDependenciesError(
154154
std::vector<std::pair<std::string, std::type_index>>
155155
&&missingDependencies)
156-
: m_missingDependencies(std::move(missingDependencies)){};
156+
: m_missing_dependencies(std::move(missingDependencies)){};
157157
const char *what() const noexcept override {
158158
if (m_error_message.empty()) {
159159
std::ostringstream s;
160-
for (const auto &missingDependency : m_missingDependencies) {
160+
for (const auto &missingDependency : m_missing_dependencies) {
161161
tfm::format(s, "%s requires %s, but that is not a known component\n",
162162
missingDependency.first, missingDependency.second.name());
163163
}
@@ -236,6 +236,9 @@ class Injector {
236236
// they need access to the injector (hence it's passed in).
237237
using Method = void (*)(InjectorType *);
238238

239+
// a function pointer to a function that takes an Injector is its first
240+
// argument and creates a pointer to an unmanaged component from it. The
241+
// injector will take ownership of that pointer.
239242
template <typename T>
240243
using Factory = T *(*)(InjectorType *);
241244

@@ -252,7 +255,7 @@ class Injector {
252255
};
253256

254257
std::map<std::type_index, Component> m_components;
255-
std::vector<std::type_index> m_destructionOrder;
258+
std::vector<std::type_index> m_destruction_order;
256259

257260
static std::vector<void *> GatherDependencies(I *injector,
258261
const Component &component) {
@@ -298,8 +301,7 @@ class Injector {
298301
template <typename T>
299302
struct Registrator {
300303
template <typename... Deps>
301-
static Dependency<T> Register(I *injector, const std::string &name,
302-
Method init) {
304+
static Dependency<T> Register(I *const injector, const std::string &name, Method init) {
303305
std::type_index typeIndex(typeid(T));
304306
Component component;
305307
component.m_name = name;
@@ -314,7 +316,7 @@ class Injector {
314316

315317
template <typename ComponentType>
316318
struct Initializer {
317-
static Component &GetComponent(I *injector) {
319+
static Component &GetComponent(I *const injector) {
318320
return injector->m_components[typeid(ComponentType)];
319321
}
320322
template <typename... Args>
@@ -330,13 +332,18 @@ class Injector {
330332
}
331333
};
332334
struct Unmanaged {
333-
template <typename T>
334-
static ComponentType *Init(I *injector, Factory<T> factory) {
335+
static ComponentType *Init(I *const injector, ComponentType *const pointer) {
335336
auto &component = GetComponent(injector);
336-
T *const ptr = factory(injector);
337-
component.m_instance = ptr;
337+
component.m_instance = pointer;
338338
component.m_deleter = nullptr;
339-
return ptr;
339+
return pointer;
340+
}
341+
static ComponentType *Init(I *const injector, Factory<ComponentType> factory) {
342+
auto &component = GetComponent(injector);
343+
ComponentType *const pointer = factory(injector);
344+
component.m_instance = pointer;
345+
component.m_deleter = nullptr;
346+
return pointer;
340347
}
341348
};
342349
};
@@ -404,7 +411,7 @@ class Injector {
404411
}
405412
}
406413
std::reverse(initializationOrder.begin(), initializationOrder.end());
407-
m_destructionOrder = std::move(initializationOrder);
414+
m_destruction_order = std::move(initializationOrder);
408415
m_stopped.clear();
409416
}
410417

@@ -414,7 +421,7 @@ class Injector {
414421
return;
415422
}
416423
std::vector<ComponentError> errors;
417-
for (const std::type_index &component_type : m_destructionOrder) {
424+
for (const std::type_index &component_type : m_destruction_order) {
418425
if (m_components[component_type].m_stopper) {
419426
try {
420427
m_components[component_type].m_stopper(static_cast<I *>(this));
@@ -439,7 +446,7 @@ class Injector {
439446
} catch (StoppingComponentsError &) {
440447
// see https://stackoverflow.com/a/130123/471478
441448
}
442-
for (const std::type_index &componentType : m_destructionOrder) {
449+
for (const std::type_index &componentType : m_destruction_order) {
443450
if (m_components[componentType].m_deleter) {
444451
m_components[componentType].m_deleter(static_cast<I *>(this));
445452
}

src/injector.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@
3535

3636
class UnitEInjector : public Injector<UnitEInjector> {
3737

38-
static ::ArgsManager *GetGlobalArgs(UnitEInjector *) { return &gArgs; }
39-
UNMANAGED_COMPONENT(ArgsManager, ::ArgsManager, GetGlobalArgs)
38+
UNMANAGED_COMPONENT(ArgsManager, ::ArgsManager, &gArgs)
4039

41-
static blockchain::Behavior *GetGlobalBehavior(UnitEInjector *) { return &blockchain::Behavior::GetGlobal(); }
42-
UNMANAGED_COMPONENT(BlockchainBehavior, blockchain::Behavior, GetGlobalBehavior)
40+
UNMANAGED_COMPONENT(BlockchainBehavior, blockchain::Behavior, &blockchain::Behavior::GetGlobal())
4341

44-
static UnitEInjectorConfiguration *GetInjectorConfiguration(UnitEInjector *injector) { return &injector->m_config; }
45-
UNMANAGED_COMPONENT(InjectorConfiguration, UnitEInjectorConfiguration, GetInjectorConfiguration)
42+
UNMANAGED_COMPONENT(InjectorConfiguration, UnitEInjectorConfiguration, [](UnitEInjector *i) { return &i->m_config; })
4643

4744
COMPONENT(Settings, ::Settings, Settings::New,
4845
::ArgsManager,

0 commit comments

Comments
 (0)