solinject  1.0.0
C++17 Dependency Injection header-only library
sol::di::Container Class Reference

Dependency Injection container. More...

#include <solinject.hpp>

Public Types

template<class T >
using Factory = typename impl::IServiceTyped< T >::Factory
 Factory function that accepts a reference to a DI container and returns a pointer to an instance of a service. More...
 
template<class T >
using ServicePtr = typename impl::IServiceTyped< T >::ServicePtr
 Pointer to an instance of a service. More...
 
using DIServicePtr = impl::RegisteredServices::DIServicePtr
 Pointer to a DI service instance. More...
 
using ScopedServiceBuilderPtr = impl::ScopedServiceBuilders::ScopedServiceBuilderPtr
 Pointer to a scoped service builder. More...
 

Public Member Functions

 Container ()
 Default constructor.
 
 Container (const Container &other)=delete
 Copy constructor (deleted)
 
 Container (Container &&other) noexcept
 Move constructor.
 
Containeroperator= (const Container &other)=delete
 Copy-assignment operator (deleted)
 
Containeroperator= (Container &&other) noexcept
 Move-assignment operator.
 
Container CreateScope () const
 Creates a scoped container from the current container. More...
 
bool IsScope ()
 Tells if the container is a scope container. More...
 
template<class T >
void RegisterSingletonService (Factory< T > factory)
 Registers a service with singleton lifetime. More...
 
template<class T >
void RegisterSingletonService (ServicePtr< T > instance)
 Registers a service with singleton lifetime. More...
 
template<class T >
void RegisterTransientService (Factory< T > factory)
 Registers a service with transient lifetime. More...
 
template<class T >
void RegisterSharedService (Factory< T > factory)
 Registers a service with shared lifetime. More...
 
template<class T >
void RegisterScopedService (Factory< T > factory)
 Registers a service with scoped lifetime. More...
 
void RegisterService (std::type_index type, DIServicePtr diService)
 Registers a service. More...
 
void RegisterScopedServiceBuilder (std::type_index type, ScopedServiceBuilderPtr serviceBuilder)
 Registers a scoped service builder. More...
 
template<class T >
ServicePtr< T > GetRequiredService () const
 Resolves a required service. More...
 
template<class T >
ServicePtr< T > GetService () const
 Resolves an optional service. More...
 
template<class T >
std::vector< ServicePtr< T > > GetServices () const
 Resolves services. More...
 

Friends

void swap (Container &a, Container &b) noexcept
 Swaps two Container instances.
 

Detailed Description

Dependency Injection container.

Member Typedef Documentation

◆ DIServicePtr

◆ Factory

template<class T >
using sol::di::Container::Factory = typename impl::IServiceTyped<T>::Factory

Factory function that accepts a reference to a DI container and returns a pointer to an instance of a service.

Template Parameters
Tthe service type

◆ ScopedServiceBuilderPtr

◆ ServicePtr

template<class T >
using sol::di::Container::ServicePtr = typename impl::IServiceTyped<T>::ServicePtr

Pointer to an instance of a service.

Template Parameters
Tservice type

Member Function Documentation

◆ CreateScope()

Container sol::di::Container::CreateScope ( ) const
inline

Creates a scoped container from the current container.

Returns
Scoped Container instance

◆ GetRequiredService()

template<class T >
ServicePtr<T> sol::di::Container::GetRequiredService ( ) const
inline

Resolves a required service.

Template Parameters
Tservice type
Returns
Pointer to an instance of the service
Exceptions
sol::di::exc::ServiceNotRegisteredException

◆ GetService()

template<class T >
ServicePtr<T> sol::di::Container::GetService ( ) const
inline

Resolves an optional service.

Template Parameters
Tservice type
Returns
Pointer to an instance of the service or nullptr if the service is not registered

◆ GetServices()

template<class T >
std::vector<ServicePtr<T> > sol::di::Container::GetServices ( ) const
inline

Resolves services.

Template Parameters
Tthe service type
Returns
std::vector of pointers to service instances or empty vector if the service is not registered

◆ IsScope()

bool sol::di::Container::IsScope ( )
inline

Tells if the container is a scope container.

Returns
true if the container is a scope container, false otherwise

◆ RegisterScopedService()

template<class T >
void sol::di::Container::RegisterScopedService ( Factory< T >  factory)
inline

Registers a service with scoped lifetime.

Template Parameters
Tservice type
Parameters
factoryfactory function

◆ RegisterScopedServiceBuilder()

void sol::di::Container::RegisterScopedServiceBuilder ( std::type_index  type,
ScopedServiceBuilderPtr  serviceBuilder 
)
inline

Registers a scoped service builder.

Parameters
typeservice type
serviceBuilderservice builder
Warning
This method is intended for use by the ContainerBuilder class only

◆ RegisterService()

void sol::di::Container::RegisterService ( std::type_index  type,
DIServicePtr  diService 
)
inline

Registers a service.

Parameters
typeservice type
diServicepointer to a DI service instance
Warning
This method is intended for use by the ContainerBuilder class only

◆ RegisterSharedService()

template<class T >
void sol::di::Container::RegisterSharedService ( Factory< T >  factory)
inline

Registers a service with shared lifetime.

Template Parameters
Tservice type
Parameters
factoryfactory function

◆ RegisterSingletonService() [1/2]

template<class T >
void sol::di::Container::RegisterSingletonService ( Factory< T >  factory)
inline

Registers a service with singleton lifetime.

Template Parameters
Tservice type
Parameters
factoryfactory function

◆ RegisterSingletonService() [2/2]

template<class T >
void sol::di::Container::RegisterSingletonService ( ServicePtr< T >  instance)
inline

Registers a service with singleton lifetime.

Template Parameters
Tservice type
Parameters
instancepointer to an instance of the service

◆ RegisterTransientService()

template<class T >
void sol::di::Container::RegisterTransientService ( Factory< T >  factory)
inline

Registers a service with transient lifetime.

Template Parameters
Tservice type
Parameters
factoryfactory function

The documentation for this class was generated from the following file: