The main dependency injector class. More...
#include <Injector.hpp>
Inherits enable_shared_from_this< Injector >.
Classes | |
class | ValidationParent |
Encapsulates a validated parent injector for subsequent child validation calls. More... | |
Static Public Member Functions | |
template<typename ... Conf> | |
static std::shared_ptr< Injector > | create (const Conf &... conf) |
Construct an injector with one or more injector configurations. More... | |
static std::shared_ptr< Injector > | create (const std::vector< std::shared_ptr< InjectorConfiguration >> &conf) |
Construct an injector with one or more injector configurations supplied in a vector. More... | |
template<typename InstT > | |
static Impl::BindingKey | createBindingKey (const std::string &name="") |
Used for binding error reporting. More... | |
template<typename ... Conf> | |
static ValidationParent | validate (const Conf &... conf) |
Validate the runtime specified injector configuration. More... | |
static ValidationParent | validate (const std::vector< std::shared_ptr< InjectorConfiguration >> &conf) |
Validate the runtime specified injector configuration. More... | |
template<typename ... Conf> | |
static ValidationParent | validateChild (ValidationParent parent, const Conf &... conf) |
Validate the runtime specified child injector configuration. More... | |
static ValidationParent | validateChild (const ValidationParent &parent, const std::vector< std::shared_ptr< InjectorConfiguration >> &conf) |
Validate the runtime specified child injector configuration. More... | |
Public Member Functions | |
template<typename ... Conf> | |
std::shared_ptr< Injector > | createChild (const Conf &... conf) const |
Construct a child injector with one or more injector configurations. More... | |
std::shared_ptr< Injector > | createChild (const std::vector< std::shared_ptr< InjectorConfiguration >> &conf) const |
Construct a child injector with one or more injector configurations supplied in a vector. More... | |
std::shared_ptr< Injector > | createChild (const std::shared_ptr< Injector > &prototype) const |
Construct a child injector from the previously constructed prototype. More... | |
template<typename InstT > | |
InstT | getInstance (std::string_view name=std::string_view()) const |
Get an instance, the meta-type of which is determined by the specified type. More... | |
template<typename BaseT > | |
BaseT & | getReference (std::string_view name=std::string_view()) const |
Get-instance method for reference providers. More... | |
template<typename BaseT > | |
BaseT & | getReference (BaseT &defaultValue) const |
Get-instance method for reference providers. More... | |
template<typename BaseT > | |
BaseT & | getReference (std::string_view name, BaseT &defaultValue) const |
Get-instance method for reference providers. More... | |
template<typename BaseT > | |
std::shared_ptr< BaseT > | getShared (std::string_view name=std::string_view()) const |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons). More... | |
template<typename BaseT > | |
std::shared_ptr< BaseT > | getShared (const std::shared_ptr< BaseT > &defaultValue) const |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons). More... | |
template<typename BaseT > | |
std::shared_ptr< BaseT > | getShared (std::string_view name, const std::shared_ptr< BaseT > &defaultValue) const |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons). More... | |
template<typename BaseT > | |
std::shared_ptr< BaseT > | getShared (const std::function< std::shared_ptr< BaseT >()> &defaultValueSupplier) const |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons). More... | |
template<typename BaseT > | |
std::shared_ptr< BaseT > | getShared (std::string_view name, const std::function< std::shared_ptr< BaseT >()> &defaultValueSupplier) const |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons). More... | |
template<typename BaseT > | |
std::shared_ptr< BaseT > | getSharedOrNull (std::string_view name=std::string_view()) const |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons). More... | |
template<typename BaseT , typename DeleterT = std::default_delete<BaseT>> | |
std::unique_ptr< BaseT, DeleterT > | getUnique (std::string_view name=std::string_view()) const |
Get-instance method for unique pointers (polymorphic new instances). More... | |
template<typename BaseT , typename DeleterT = std::default_delete<BaseT>> | |
std::unique_ptr< BaseT, DeleterT > | getUnique (std::unique_ptr< BaseT > &&defaultValue) const |
Get-instance method for unique pointers (polymorphic new instances). More... | |
template<typename BaseT , typename DeleterT = std::default_delete<BaseT>> | |
std::unique_ptr< BaseT, DeleterT > | getUnique (std::string_view name, std::unique_ptr< BaseT, DeleterT > &&defaultValue) const |
Get-instance method for unique pointers (polymorphic new instances). More... | |
template<typename BaseT , typename DeleterT = std::default_delete<BaseT>> | |
std::unique_ptr< BaseT, DeleterT > | getUnique (std::function< std::unique_ptr< BaseT, DeleterT >()> &defaultValueSupplier) const |
Get-instance method for unique pointers (polymorphic new instances). More... | |
template<typename BaseT , typename DeleterT = std::default_delete<BaseT>> | |
std::unique_ptr< BaseT, DeleterT > | getUnique (std::string_view name, std::function< std::unique_ptr< BaseT, DeleterT >()> &defaultValueSupplier) const |
Get-instance method for unique pointers (polymorphic new instances). More... | |
template<typename BaseT , typename DeleterT = std::default_delete<BaseT>> | |
std::unique_ptr< BaseT, DeleterT > | getUniqueOrNull (std::string_view name=std::string_view()) const |
Get-instance method for unique pointers (polymorphic new instances). More... | |
template<typename ValueT > | |
ValueT | getValue (std::string_view name=std::string_view()) const |
Get-instance method for values (non-polymorphic new instances). More... | |
template<typename ValueT > | |
ValueT | getValue (const ValueT &defaultValue) const |
Get-instance method for values (non-polymorphic new instances). More... | |
template<typename ValueT > | |
ValueT | getValue (std::string_view name, const ValueT &defaultValue) const |
Get-instance method for values (non-polymorphic new instances). More... | |
template<typename ValueT > | |
ValueT | getValue (const std::function< ValueT()> &defaultValueSupplier) const |
Get-instance method for values (non-polymorphic new instances). More... | |
template<typename ValueT > | |
ValueT | getValue (std::string_view name, const std::function< ValueT()> &defaultValueSupplier) const |
Get-instance method for values (non-polymorphic new instances). More... | |
template<typename InstT > | |
bool | hasBinding (std::string_view name=std::string_view()) const |
Returns true if the injector or its parent has a binding matching the specified type and name. More... | |
template<typename BaseT > | |
void | iterate (const std::function< void(std::shared_ptr< BaseT >)> &func, bool includeThreadLocal=false) const |
For all singleton bindings of the specified type, call the supplied function with the binding's singleton object. More... | |
template<typename BaseT > | |
void | iterate (const std::function< void(std::shared_ptr< const BaseT >)> &func, bool includeThreadLocal=false) const |
For all singleton bindings of the specified type, call the supplied function with the binding's singleton object. More... | |
std::string | printBindings (bool printAncestor=false) const |
Print the bindings of this injector and optionally the injector's ancestors. More... | |
std::string | printBindingsDetailed () const |
Print detailed bindings of this injector and the injector's ancestors. More... | |
void | registerPostConstructionCall (const std::function< void(const Injector &)> &call) const |
Register with the injector a callback that will be called by the injector at the end of construction. More... | |
void | registerPreDestructionCall (const std::function< void()> &call) const |
Register with the injector a callback that will be called in the injector's destructor, before the bindings are deleted. More... | |
template<typename T > | |
void | registerStaticSingleton (std::shared_ptr< T > *ptrPtr, std::string_view name=std::string_view()) const |
Register a static singleton pointer that the injector will set up post-construction and invalidate pre-destruction. More... | |
The main dependency injector class.
Injectors are instantiated by passing one or more injector configuration instances.
An instantiated injector can be used by multiple threads without requiring any synchronisation.
See the injector manual page for detailed usage information and the injector unit tests for examples.
|
inlinestatic |
Construct an injector with one or more injector configurations.
Conf | the first configuration type(s) (must derive from InjectorConfiguration) |
conf | the runtime supplied InjectorConfiguration objects |
|
inlinestatic |
Construct an injector with one or more injector configurations supplied in a vector.
conf | the runtime supplied InjectorConfiguration objects |
|
inlinestatic |
Used for binding error reporting.
The supplied type InstT is ValueT, std::unique_ptr<BaseT>, BaseT &, or std::shared_ptr<BaseT>, and also determines the binding meta-type (Value, Unique, Reference or Shared).
InstT | the full binding type (this also determines the meta-type) |
name | the name of the binding (default = empty) |
|
inline |
Construct a child injector with one or more injector configurations.
Conf | the first configuration type(s) (must derive from InjectorConfiguration) |
conf | the runtime supplied InjectorConfiguration objects |
|
inline |
Construct a child injector with one or more injector configurations supplied in a vector.
conf | the runtime supplied InjectorConfiguration objects |
Construct a child injector from the previously constructed prototype.
Constructing a prototype child injector and subsequently calling this function multiple times with the same prototype avoids running the construction and validation phases each time a child injector is required.
Note that the bindings of the prototype will be shared between all child injectors created from the prototype. This implies that singleton and thread-local singleton instances will be shared across the injectors. If this is not desired behaviour, then a new child injector must be created via the other createChildInjector functions that instantiate their own bindings.
prototype | the prototype child injector to copy |
|
inline |
Get an instance, the meta-type of which is determined by the specified type.
If the type InstT is a std::shared_ptr<BaseT>, the getShared() method is implicitly called. If the type InstT is a BaseT &, the getReference() method is implicitly called. If the type InstT is a std::unique_ptr<BaseT>, the getUnique() method is implicitly called. Otherwise, the getValue() method is implicitly called.
InstT | the complete type of the value, unique, reference, or shared binding |
name | the name of the binding |
NoBindingException | if no suitable binding was found |
|
inline |
Get-instance method for reference providers.
If no suitable binding is found, NoBindingException is thrown.
BaseT | the base type of the reference binding |
name | the name of the binding |
NoBindingException | if no suitable binding was found |
|
inline |
Get-instance method for reference providers.
If no suitable binding is found, the supplied default value is copied into the return value.
BaseT | the base type of the reference binding |
defaultValue | a default reference to return if no suitable binding was found |
|
inline |
Get-instance method for reference providers.
If no suitable binding is found, the supplied default value is referenced instead.
BaseT | the base type of the reference binding |
name | the name of the binding |
defaultValue | a default reference to return if no suitable binding was found |
|
inline |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons).
If no suitable binding is found, NoBindingException is thrown.
A std::shared_ptr may be singleton or thread-local singleton sourced.
BaseT | the base type of the shared binding |
name | the name of the binding |
NoBindingException | if no suitable binding was found |
|
inline |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons).
If no suitable binding is found, the supplied default value is shared into the return value.
A std::shared_ptr may be singleton or thread-local singleton sourced.
BaseT | the base type of the shared binding |
defaultValue | a default pointer to copy if no suitable binding was found |
|
inline |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons).
If no suitable binding is found, the supplied default value is shared into the return value.
A std::shared_ptr may be singleton or thread-local singleton sourced.
BaseT | the base type of the shared binding |
name | the name of the binding |
defaultValue | a default pointer to copy if no suitable binding was found |
|
inline |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons).
If no suitable binding is found, a default value is obtained by the supplied function.
A std::shared_ptr may be singleton or thread-local singleton sourced.
BaseT | the base type of the shared binding |
defaultValueSupplier | a function that supplies a default value if no suitable binding is found |
|
inline |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons).
If no suitable binding is found, a default value is obtained by the supplied function.
A std::shared_ptr may be singleton or thread-local singleton sourced.
BaseT | the base type of the shared binding |
name | the name of the binding |
defaultValueSupplier | a function that supplies a default value if no suitable binding is found |
|
inline |
Get-instance method for shared pointers (polymorphic thread-local or non-thread-local singletons).
Return an empty shared_ptr if no suitable binding is found.
A std::shared_ptr may be singleton or thread-local singleton sourced.
BaseT | the base type of the shared binding |
name | the name of the binding |
|
inline |
Get-instance method for unique pointers (polymorphic new instances).
If no suitable binding is found, NoBindingException is thrown.
A std::unique_ptr may be instance sourced.
BaseT | the base type of the unique binding |
name | the name of the binding |
NoBindingException | if no suitable binding was found |
|
inline |
Get-instance method for unique pointers (polymorphic new instances).
If no suitable binding is found, the supplied default value is moved into the return value.
A std::unique_ptr may be instance sourced.
BaseT | the base type of the unique binding |
defaultValue | a default value to move if no suitable binding was found |
|
inline |
Get-instance method for unique pointers (polymorphic new instances).
If no suitable binding is found, the supplied default value is moved into the return value.
A std::unique_ptr may be instance sourced.
BaseT | the base type of the unique binding |
name | the name of the binding |
defaultValue | a default value to move if no suitable binding was found |
|
inline |
Get-instance method for unique pointers (polymorphic new instances).
If no suitable binding is found, a default value is obtained by the supplied function.
A std::unique_ptr may be instance sourced.
BaseT | the base type of the unique binding |
defaultValueSupplier | a function that supplies a default value if no suitable binding is found |
|
inline |
Get-instance method for unique pointers (polymorphic new instances).
If no suitable binding is found, a default value is obtained by the supplied function.
A std::unique_ptr may be instance sourced.
BaseT | the base type of the unique binding |
name | the name of the binding |
defaultValueSupplier | a function that supplies a default value if no suitable binding is found |
|
inline |
Get-instance method for unique pointers (polymorphic new instances).
Return an empty unique_ptr if there is no suitable binding.
A std::unique_ptr may be instance sourced.
BaseT | the base type of the unique binding |
name | the name of the binding |
|
inline |
Get-instance method for values (non-polymorphic new instances).
If no suitable binding is found, NoBindingException is thrown.
A value may be instance sourced (stack constructed or copied from a prototype).
ValueT | the type of the value binding |
name | the name of the binding |
NoBindingException | if no suitable binding was found |
|
inline |
Get-instance method for values (non-polymorphic new instances).
If no suitable binding is found, the supplied default value is copied into the return value.
A value may be instance sourced (stack constructed or copied from a prototype).
ValueT | the type of the value binding |
defaultValue | a default value to copy if no suitable binding was found |
|
inline |
Get-instance method for values (non-polymorphic new instances).
If no suitable binding is found, the supplied default value is copied into the return value.
A value may be instance sourced (stack constructed or copied from a prototype).
ValueT | the type of the value binding |
name | the name of the binding |
defaultValue | a default value to copy if no suitable binding was found |
|
inline |
Get-instance method for values (non-polymorphic new instances).
If no suitable binding is found, a default value is obtained by the supplied function.
A value may be instance sourced (stack constructed or copied from a prototype).
ValueT | the type of the value binding |
defaultValueSupplier | a function that supplies a default value if no suitable binding is found |
|
inline |
Get-instance method for values (non-polymorphic new instances).
If no suitable binding is found, a default value is obtained by the supplied function.
A value may be instance sourced (stack constructed or copied from a prototype).
ValueT | the type of the value binding |
name | the name of the binding |
defaultValueSupplier | a function that supplies a default value if no suitable binding is found |
|
inline |
Returns true if the injector or its parent has a binding matching the specified type and name.
The supplied type InstT is ValueT, std::unique_ptr<BaseT>, BaseT &, or std::shared_ptr<BaseT>, and also determines the binding meta-type (Value, Unique, Reference or Shared).
InstT | the full binding type (this also determines the meta-type) |
name | the name of the binding (default = empty) |
|
inline |
For all singleton bindings of the specified type, call the supplied function with the binding's singleton object.
BaseT | the base type of the shared binding |
func | the function to call |
includeThreadLocal | if true then thread local bindings will be include (default is false) |
|
inline |
For all singleton bindings of the specified type, call the supplied function with the binding's singleton object.
BaseT | the base type of the shared binding |
func | the function to call |
includeThreadLocal | if true then thread local bindings will be include (default is false) |
|
inline |
Print the bindings of this injector and optionally the injector's ancestors.
The bindings will be printed to the "balau.injector" logging namespace at info level.
printAncestor | print ancestor bindings as well (default = false) |
|
inline |
Print detailed bindings of this injector and the injector's ancestors.
This version of the print functions prints the bindings and their dependencies. This can be useful for examining cyclic dependency issues.
The bindings will be printed to the "balau.injector" logging namespace at info level.
printAncestor | print ancestor bindings as well (default = false) |
|
inline |
Register with the injector a callback that will be called by the injector at the end of construction.
In order to use this method, inject the injector into an eager singleton injectable via a weak pointer and call the method.
Alternatively, the same method is available to call in the ApplicationConfiguration base class. The ApplicationConfiguration method does not require an eager singleton to register the call.
call | the callback |
|
inline |
Register with the injector a callback that will be called in the injector's destructor, before the bindings are deleted.
Although pre-destruction callbacks must be noexcept(true), the pre-destruction function signature does not contain noexcept(true), as this is not yet handled by std::function in C++17. Despite this, functions registered as pre-destruction callbacks must nevertheless be noexcept(true).
In order to use this method, inject the injector into an injectable via a weak pointer and call the method.
Alternatively, the same method is available to call in the ApplicationConfiguration base class.
call | the callback |
|
inline |
Register a static singleton pointer that the injector will set up post-construction and invalidate pre-destruction.
The static pointer will be valid immediately after injection construction up to the start of injector destruction.
This call is a convenience method for calling the registerPostConstructionCall and registerPreDestructionCall methods in order to set up and tear down the static singleton pointer.
In order to use this method, inject the injector into an eager singleton injectable via a weak pointer and call the method.
Alternatively, the same method is available to call in the ApplicationConfiguration base class. The ApplicationConfiguration method does not require an eager singleton to register the calls.
T | the binding type |
ptrPtr | a raw pointer to the statically allocated shared pointer |
name | an optional binding name |
|
inlinestatic |
Validate the runtime specified injector configuration.
Conf | the first configuration type(s) (must derive from InjectorConfiguration) |
conf | the runtime supplied InjectorConfiguration objects |
MissingDependencyException | if a required dependency is missing |
DuplicateBindingException | if duplicate bindings exist |
CyclicDependencyException | if there is a cyclic dependency |
SharedInjectorException | if one of the injectable classes specifies a shared pointer injector dependency |
|
inlinestatic |
Validate the runtime specified injector configuration.
conf | the runtime supplied InjectorConfiguration objects |
MissingDependencyException | if a required dependency is missing |
DuplicateBindingException | if duplicate bindings exist |
CyclicDependencyException | if there is a cyclic dependency |
SharedInjectorException | if one of the injectable classes specifies a shared pointer injector dependency |
|
inlinestatic |
Validate the runtime specified child injector configuration.
Conf | the first configuration type(s) (must derive from InjectorConfiguration) |
parent | the ValidationParent object representing the parent of the child injector |
conf | the runtime supplied InjectorConfiguration objects |
MissingDependencyException | if a required dependency is missing |
DuplicateBindingException | if duplicate bindings exist |
CyclicDependencyException | if there is a cyclic dependency |
SharedInjectorException | if one of the injectable classes specifies a shared pointer injector dependency |
|
inlinestatic |
Validate the runtime specified child injector configuration.
parent | the ValidationParent object representing the parent of the child injector |
conf | the runtime supplied InjectorConfiguration objects |
MissingDependencyException | if a required dependency is missing |
DuplicateBindingException | if duplicate bindings exist |
CyclicDependencyException | if there is a cyclic dependency |
SharedInjectorException | if one of the injectable classes specifies a shared pointer injector dependency |