solinject
1.0.0
C++17 Dependency Injection header-only library
|
Go to the documentation of this file.
42 namespace impl {
class IService; }
101 ScopedLock lock(*a.m_Mutex, *b.m_Mutex);
103 swap(a.m_RegisteredServices, b.m_RegisteredServices);
104 swap(a.m_ScopedServiceBuilders, b.m_ScopedServiceBuilders);
105 swap(a.m_Mutex, b.m_Mutex);
115 using namespace impl;
117 auto lock = LockMutex();
119 RegisteredServices diServices = m_RegisteredServices;
120 diServices.Merge(m_ScopedServiceBuilders.BuildDIServices());
141 auto lock = LockMutex();
142 m_RegisteredServices.template RegisterSingletonService<T>(factory);
155 if (instance ==
nullptr)
158 auto lock = LockMutex();
159 m_RegisteredServices.template RegisterSingletonService<T>(instance);
170 auto lock = LockMutex();
171 m_RegisteredServices.template RegisterTransientService<T>(factory);
182 auto lock = LockMutex();
183 m_RegisteredServices.template RegisterSharedService<T>(factory);
194 auto lock = LockMutex();
195 m_ScopedServiceBuilders.template RegisterScopedService<T>(factory);
231 auto lock = LockMutex();
232 return m_RegisteredServices.template GetRequiredService<T>(*
this);
244 auto lock = LockMutex();
245 return m_RegisteredServices.template GetService<T>(*
this);
257 auto lock = LockMutex();
258 return m_RegisteredServices.template GetServices<T>(*
this);
262 #ifndef SOLINJECT_NOTHREADSAFE
265 using ScopedLock = std::scoped_lock<Mutex, Mutex>;
283 m_RegisteredServices(
std::move(services)),
291 impl::RegisteredServices m_RegisteredServices;
294 impl::ScopedServiceBuilders m_ScopedServiceBuilders;
303 bool m_IsScope =
false;
309 Lock LockMutex()
const
311 return Lock(*m_Mutex);
Container(Container &&other) noexcept
Move constructor.
Definition: Container.hpp:78
#define solinject_assert(expression)
assert() macro, which is disabled in tests project.
Definition: Defines.hpp:34
Dependency Injection container.
Definition: Container.hpp:48
friend void swap(Container &a, Container &b) noexcept
Swaps two Container instances.
Definition: Container.hpp:94
void RegisterScopedService(Factory< T > factory)
Registers a scoped service.
Definition: ScopedServiceBuilders.hpp:75
void RegisterScopedServiceBuilder(std::type_index type, ScopedServiceBuilderPtr serviceBuilder)
Registers a scoped service builder.
Definition: Container.hpp:217
Registered DI services collection.
Definition: RegisteredServices.hpp:45
Container & operator=(Container &&other) noexcept
Move-assignment operator.
Definition: Container.hpp:87
#define solinject_req_assert(expression)
Required assert, which is disabled only when the assert() macro from assert.h is disabled.
Definition: Defines.hpp:41
void RegisterService(std::type_index type, DIServicePtr diService)
Registers a service.
Definition: Container.hpp:205
void RegisterScopedService(Factory< T > factory)
Registers a service with scoped lifetime.
Definition: Container.hpp:192
void RegisterSingletonService(ServicePtr< T > instance)
Registers a service with singleton lifetime.
Definition: Container.hpp:151
typename impl::IServiceTyped< T >::Factory Factory
Factory function that accepts a reference to a DI container and returns a pointer to an instance of a...
Definition: Container.hpp:56
Container()
Default constructor.
Definition: Container.hpp:72
typename impl::IServiceTyped< T >::ServicePtr ServicePtr
Pointer to an instance of a service.
Definition: Container.hpp:63
std::vector< ServicePtr< T > > GetServices() const
Resolves services.
Definition: Container.hpp:255
void RegisterTransientService(Factory< T > factory)
Registers a service with transient lifetime.
Definition: Container.hpp:168
Container CreateScope() const
Creates a scoped container from the current container.
Definition: Container.hpp:113
ServicePtr< T > GetRequiredService() const
Resolves a required service.
Definition: Container.hpp:229
Empty class.
Definition: Utils.hpp:31
ServicePtr< T > GetService() const
Resolves an optional service.
Definition: Container.hpp:242
typename std::shared_ptr< T > ServicePtr
Pointer to an instance of a service.
Definition: IServiceTyped.hpp:36
Container & operator=(const Container &other)=delete
Copy-assignment operator (deleted)
std::shared_ptr< IService > DIServicePtr
Pointer to a DI service instance.
Definition: RegisteredServices.hpp:63
std::shared_ptr< IScopedServiceBuilder > ScopedServiceBuilderPtr
Pointer to a scoped service builder.
Definition: ScopedServiceBuilders.hpp:61
typename std::function< ServicePtr(const Container &)> Factory
Factory function that accepts a reference to a DI container and returns a pointer to an instance of a...
Definition: IServiceTyped.hpp:42
void RegisterSharedService(Factory< T > factory)
Registers a service with shared lifetime.
Definition: Container.hpp:180
void RegisterService(std::type_index type, DIServicePtr diService)
Registers a service.
Definition: RegisteredServices.hpp:181
bool IsScope()
Tells if the container is a scope container.
Definition: Container.hpp:131
void RegisterSingletonService(Factory< T > factory)
Registers a service with singleton lifetime.
Definition: Container.hpp:139