plugify 1.2.8
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
plugify::ServiceLocator Class Reference

DI Container / Service Locator with PIMPL. More...

#include <service_locator.hpp>

Classes

class  ServiceBuilder
 

Public Member Functions

 ServiceLocator (const ServiceLocator &)=delete
 
ServiceLocatoroperator= (const ServiceLocator &)=delete
 
 ServiceLocator (ServiceLocator &&) noexcept
 
ServiceLocatoroperator= (ServiceLocator &&) noexcept
 
template<typename Interface , typename Implementation = Interface>
requires (std::is_base_of_v<Interface, Implementation>)
void RegisterInstance (std::shared_ptr< Implementation > instance)
 Register a concrete instance (singleton)
 
template<typename Interface >
void RegisterFactory (std::function< std::shared_ptr< Interface >()> factory, ServiceLifetime lifetime=ServiceLifetime::Transient)
 Register a factory function.
 
template<typename Interface , typename Implementation = Interface>
requires (std::is_base_of_v<Interface, Implementation> && std::is_default_constructible_v<Implementation>)
void RegisterType (ServiceLifetime lifetime=ServiceLifetime::Transient)
 Register a type with automatic construction.
 
template<typename Interface , typename Implementation , typename... Dependencies>
requires (std::is_base_of_v<Interface, Implementation>)
void RegisterWithDependencies (std::function< std::shared_ptr< Implementation >(Dependencies...)> constructor, ServiceLifetime lifetime=ServiceLifetime::Transient)
 Register with dependency injection.
 
template<typename Interface , typename Implementation = Interface>
requires (std::is_base_of_v<Interface, Implementation>)
void RegisterInstanceIfMissing (std::shared_ptr< Implementation > instance)
 Register a concrete instance if missing (singleton)
 
template<typename Interface >
void RegisterFactoryIfMissing (std::function< std::shared_ptr< Interface >()> factory, ServiceLifetime lifetime=ServiceLifetime::Transient)
 Register a factory function.
 
template<typename Interface , typename Implementation = Interface>
requires (std::is_base_of_v<Interface, Implementation> && std::is_default_constructible_v<Implementation>)
void RegisterTypeIfMissing (ServiceLifetime lifetime=ServiceLifetime::Transient)
 Register a type with automatic construction.
 
template<typename Interface , typename Implementation , typename... Dependencies>
requires (std::is_base_of_v<Interface, Implementation>)
void RegisterWithDependenciesIfMissing (std::function< std::shared_ptr< Implementation >(Dependencies...)> constructor, ServiceLifetime lifetime=ServiceLifetime::Transient)
 Register with dependency injection.
 
template<typename Interface >
std::shared_ptr< Interface > Resolve () const
 Resolve a service.
 
template<typename Interface >
std::shared_ptr< Interface > TryResolve () const noexcept
 Try to resolve a service (returns nullptr if not found)
 
template<typename Interface >
bool IsRegistered () const
 Check if a service is registered.
 
void BeginScope ()
 Create a new scope (for scoped services)
 
void EndScope ()
 End current scope.
 
void Clear ()
 Clear all registrations.
 
size_t Count () const
 Get the number of registered services.
 
ServiceBuilder Services ()
 

Detailed Description

DI Container / Service Locator with PIMPL.

Definition at line 20 of file service_locator.hpp.

Member Function Documentation

◆ IsRegistered()

template<typename Interface >
bool plugify::ServiceLocator::IsRegistered ( ) const
inline

Check if a service is registered.

Definition at line 179 of file service_locator.hpp.

◆ RegisterFactory()

template<typename Interface >
void plugify::ServiceLocator::RegisterFactory ( std::function< std::shared_ptr< Interface >()>  factory,
ServiceLifetime  lifetime = ServiceLifetime::Transient 
)
inline

Register a factory function.

Definition at line 51 of file service_locator.hpp.

◆ RegisterFactoryIfMissing()

template<typename Interface >
void plugify::ServiceLocator::RegisterFactoryIfMissing ( std::function< std::shared_ptr< Interface >()>  factory,
ServiceLifetime  lifetime = ServiceLifetime::Transient 
)
inline

Register a factory function.

Definition at line 107 of file service_locator.hpp.

◆ RegisterInstance()

template<typename Interface , typename Implementation = Interface>
requires (std::is_base_of_v<Interface, Implementation>)
void plugify::ServiceLocator::RegisterInstance ( std::shared_ptr< Implementation >  instance)
inline

Register a concrete instance (singleton)

Definition at line 41 of file service_locator.hpp.

◆ RegisterInstanceIfMissing()

template<typename Interface , typename Implementation = Interface>
requires (std::is_base_of_v<Interface, Implementation>)
void plugify::ServiceLocator::RegisterInstanceIfMissing ( std::shared_ptr< Implementation >  instance)
inline

Register a concrete instance if missing (singleton)

Definition at line 94 of file service_locator.hpp.

◆ RegisterType()

template<typename Interface , typename Implementation = Interface>
requires (std::is_base_of_v<Interface, Implementation> && std::is_default_constructible_v<Implementation>)
void plugify::ServiceLocator::RegisterType ( ServiceLifetime  lifetime = ServiceLifetime::Transient)
inline

Register a type with automatic construction.

Definition at line 66 of file service_locator.hpp.

◆ RegisterTypeIfMissing()

template<typename Interface , typename Implementation = Interface>
requires (std::is_base_of_v<Interface, Implementation> && std::is_default_constructible_v<Implementation>)
void plugify::ServiceLocator::RegisterTypeIfMissing ( ServiceLifetime  lifetime = ServiceLifetime::Transient)
inline

Register a type with automatic construction.

Definition at line 125 of file service_locator.hpp.

◆ RegisterWithDependencies()

template<typename Interface , typename Implementation , typename... Dependencies>
requires (std::is_base_of_v<Interface, Implementation>)
void plugify::ServiceLocator::RegisterWithDependencies ( std::function< std::shared_ptr< Implementation >(Dependencies...)>  constructor,
ServiceLifetime  lifetime = ServiceLifetime::Transient 
)
inline

Register with dependency injection.

Definition at line 76 of file service_locator.hpp.

◆ RegisterWithDependenciesIfMissing()

template<typename Interface , typename Implementation , typename... Dependencies>
requires (std::is_base_of_v<Interface, Implementation>)
void plugify::ServiceLocator::RegisterWithDependenciesIfMissing ( std::function< std::shared_ptr< Implementation >(Dependencies...)>  constructor,
ServiceLifetime  lifetime = ServiceLifetime::Transient 
)
inline

Register with dependency injection.

Definition at line 138 of file service_locator.hpp.

◆ Resolve()

template<typename Interface >
std::shared_ptr< Interface > plugify::ServiceLocator::Resolve ( ) const
inline

Resolve a service.

Definition at line 160 of file service_locator.hpp.

◆ TryResolve()

template<typename Interface >
std::shared_ptr< Interface > plugify::ServiceLocator::TryResolve ( ) const
inlinenoexcept

Try to resolve a service (returns nullptr if not found)

Definition at line 170 of file service_locator.hpp.


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