The container for implementations; used to request interfaces.
More...
#include <di.h>
|
| template<typename INTERFACE, typename IMPLEMENTATION, typename... PROPS> |
| void | RegisterType (PROPS &&... properties) |
| | Registers the type IMPLEMENTATION as an implementation of INTERFACE.
|
| template<typename INTERFACE> |
| bool | HasAnyImplementationsOf () const |
| | Checks if there are any implementation registered for INTERFACE.
|
| template<typename INTERFACE> |
| std::shared_ptr< INTERFACE > | Resolve () |
| | Returns a pointer to an implementation of INTERFACE.
|
| template<typename INTERFACE> |
| std::shared_ptr< INTERFACE > | ResolveByName (std::string_view name) |
| | Returns a pointer to an implementation of INTERFACE with the name name If an implementation with that name is not found, nullptr will be returned.
|
| template<typename INTERFACE> |
| std::vector< std::shared_ptr< INTERFACE > > | ResolveAll () |
| | Will return a vector of all available implementations of INTERFACE; useful for interfaces like image_decoder.
|
| template<typename TYPE> |
| std::shared_ptr< TYPE > | Create () |
| | Creates a new (shared pointer to an) object of TYPE, resolving any interface pointer parameters its constructor requests using this container.
|
| template<typename TYPE> |
| std::unique_ptr< TYPE > | CreateRaw () |
| | Same as Create, but returns a unique_ptr to the new object.
|
| void | DestroyAll () |
| | Unregisters all the registered implementations and removes any pointers to their instances.
|
|
| Lifetime | DefaultLifetime |
| | The default lifetime of implementations in this container, unless specified otherwise by the interface/implementation types, or when registering them.
|
The container for implementations; used to request interfaces.
Definition at line 54 of file di.h.
◆ Create()
template<typename TYPE>
| std::shared_ptr< TYPE > ghassanpl::di::Container::Create |
( |
| ) |
|
Creates a new (shared pointer to an) object of TYPE, resolving any interface pointer parameters its constructor requests using this container.
Any new implementations created to fulfill the constructor's requests will of course be resolved by the container recursively, so their constructors can also request other interface pointers.
struct ILogger
{
ILogger(shared_ptr<ITimer> timer);
};
struct BigClass
{
BigClass(shared_ptr<ILogger> logger, shared_ptr<ITimer> timer);
};
container.Create<BigClass>() -> make_shared<BigClass>(container.Resolve<ILogger>(), container.Resolve<ITimer>());
Definition at line 414 of file di_impl.h.
◆ CreateRaw()
template<typename TYPE>
| std::unique_ptr< TYPE > ghassanpl::di::Container::CreateRaw |
( |
| ) |
|
Same as Create, but returns a unique_ptr to the new object.
Definition at line 420 of file di_impl.h.
◆ DestroyAll()
| void ghassanpl::di::Container::DestroyAll |
( |
| ) |
|
|
inline |
Unregisters all the registered implementations and removes any pointers to their instances.
Definition at line 128 of file di.h.
◆ HasAnyImplementationsOf()
template<typename INTERFACE>
| bool ghassanpl::di::Container::HasAnyImplementationsOf |
( |
| ) |
const |
Checks if there are any implementation registered for INTERFACE.
Definition at line 362 of file di_impl.h.
◆ RegisterType()
template<typename INTERFACE, typename IMPLEMENTATION, typename ... PROPS>
| void ghassanpl::di::Container::RegisterType |
( |
PROPS &&... | properties | ) |
|
Registers the type IMPLEMENTATION as an implementation of INTERFACE.
The arguments specify the lifetime, behavior and properties of the implementation:
| Argument Type | Means | Usage |
| string_view | name | will register name as the name of this implementation |
| Lifetime | lifetime | will specify the lifetime of the implementation |
| DefaultImplementationStruct | DefaultImplementation | will specify this implementation as the default one for the INTERFACE (specifically, the first one on the list of implementations) |
| function<shared_ptr<INTERFACE>(Container&)> | factory | will be used as the factory function to create the implementation objects |
| shared_ptr<INTERFACE> | instance | will be used as the instance singleton for this interface |
| INTERFACE* | instance | same as above |
| function<void(Container&, shared_ptr<INTERFACE>)> | on_create | function that will be called when a new instance of the implementation is created |
Also, if INTERFACE/IMPLEMENTATION::DefaultLifetime is declared (see has_default_lifetime), that lifetime will be used by default for that interface/implementation.
You can register multiple implementations of the same INTERFACE type, but a specific IMPLEMENTATION type only once.
Definition at line 370 of file di_impl.h.
◆ Resolve()
template<typename INTERFACE>
| std::shared_ptr< INTERFACE > ghassanpl::di::Container::Resolve |
( |
| ) |
|
Returns a pointer to an implementation of INTERFACE.
The first implementation on the list of registered implementations will be used, if there are multiple. If there are no implementations of this interface, nullptr will be returned.
Definition at line 390 of file di_impl.h.
◆ ResolveAll()
template<typename INTERFACE>
| std::vector< std::shared_ptr< INTERFACE > > ghassanpl::di::Container::ResolveAll |
( |
| ) |
|
Will return a vector of all available implementations of INTERFACE; useful for interfaces like image_decoder.
Definition at line 408 of file di_impl.h.
◆ ResolveByName()
template<typename INTERFACE>
| std::shared_ptr< INTERFACE > ghassanpl::di::Container::ResolveByName |
( |
std::string_view | name | ) |
|
Returns a pointer to an implementation of INTERFACE with the name name If an implementation with that name is not found, nullptr will be returned.
Definition at line 396 of file di_impl.h.
◆ DefaultLifetime
| Lifetime ghassanpl::di::Container::DefaultLifetime |
The default lifetime of implementations in this container, unless specified otherwise by the interface/implementation types, or when registering them.
Definition at line 57 of file di.h.