Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::di::ServiceContainer Class Referenceexport

Simple thread-safe service container implementation. More...

Inheritance diagram for kcenon::common::di::ServiceContainer:
Inheritance graph
Collaboration diagram for kcenon::common::di::ServiceContainer:
Collaboration graph

Public Member Functions

 ServiceContainer ()=default
 
- Public Member Functions inherited from kcenon::common::di::IServiceContainer
virtual ~IServiceContainer ()=default
 
template<concepts::ServiceInterface TInterface, concepts::ServiceImplementation< TInterface > TImpl>
VoidResult register_type (service_lifetime lifetime=service_lifetime::singleton)
 Register a service type with its implementation.
 
template<typename TInterface >
VoidResult register_instance (std::shared_ptr< TInterface > instance)
 Register a pre-existing instance as a singleton.
 
template<concepts::ServiceInterface TInterface, concepts::ServiceFactory< TInterface > TFactory>
VoidResult register_factory (TFactory &&factory, service_lifetime lifetime=service_lifetime::singleton)
 Register a factory function for creating service instances.
 
template<concepts::ServiceInterface TInterface, concepts::SimpleServiceFactory< TInterface > TFactory>
VoidResult register_simple_factory (TFactory &&factory, service_lifetime lifetime=service_lifetime::singleton)
 Register a simple factory function without container access.
 
template<typename TInterface >
Result< std::shared_ptr< TInterface > > resolve ()
 Resolve a service by its interface type.
 
template<typename TInterface >
std::shared_ptr< TInterface > resolve_or_null ()
 Try to resolve a service, returning nullptr if not found.
 
virtual std::unique_ptr< IServiceScopecreate_scope ()=0
 Create a new service scope.
 
template<typename TInterface >
bool is_registered () const
 Check if a service type is registered.
 
virtual std::vector< service_descriptorregistered_services () const =0
 Get list of all registered service descriptors.
 
template<typename TInterface >
VoidResult unregister ()
 Unregister a service type.
 
virtual VoidResult clear ()=0
 Clear all registrations.
 
virtual ~IServiceContainer ()=default
 
template<typename Interface , typename Factory >
void register_factory (Factory &&factory)
 Register a service with a factory function.
 
template<typename Interface >
void register_singleton (std::shared_ptr< Interface > instance)
 Register a singleton instance.
 
template<typename Interface >
std::shared_ptr< Interface > resolve ()
 Resolve a service by interface type.
 
template<typename Interface >
bool has () const
 Check if a service is registered.
 

Protected Member Functions

void register_impl (std::type_index type, std::any factory) override
 
void register_singleton_impl (std::type_index type, std::any instance) override
 
std::any resolve_impl (std::type_index type) override
 
bool has_impl (std::type_index type) const override
 
- Protected Member Functions inherited from kcenon::common::di::IServiceContainer
virtual VoidResult register_factory_internal (std::type_index interface_type, const std::string &type_name, std::function< std::shared_ptr< void >(IServiceContainer &)> factory, service_lifetime lifetime)=0
 Internal factory registration (type-erased).
 
virtual VoidResult register_instance_internal (std::type_index interface_type, const std::string &type_name, std::shared_ptr< void > instance)=0
 Internal instance registration (type-erased).
 
virtual Result< std::shared_ptr< void > > resolve_internal (std::type_index interface_type)=0
 Internal service resolution (type-erased).
 
virtual bool is_registered_internal (std::type_index interface_type) const =0
 Internal registration check (type-erased).
 
virtual VoidResult unregister_internal (std::type_index interface_type)=0
 Internal unregistration (type-erased).
 

Private Attributes

std::mutex mutex_
 
std::unordered_map< std::type_index, std::any > factories_
 
std::unordered_map< std::type_index, std::any > singletons_
 

Detailed Description

Simple thread-safe service container implementation.

Definition at line 114 of file di.cppm.

Constructor & Destructor Documentation

◆ ServiceContainer()

kcenon::common::di::ServiceContainer::ServiceContainer ( )
exportdefault

Member Function Documentation

◆ has_impl()

bool kcenon::common::di::ServiceContainer::has_impl ( std::type_index type) const
inlineoverrideexportprotectedvirtual

Implements kcenon::common::di::IServiceContainer.

Definition at line 147 of file di.cppm.

147 {
148 std::lock_guard<std::mutex> lock(mutex_);
149 return singletons_.count(type) > 0 || factories_.count(type) > 0;
150 }
std::unordered_map< std::type_index, std::any > singletons_
Definition di.cppm:155
std::unordered_map< std::type_index, std::any > factories_
Definition di.cppm:154

References factories_, mutex_, and singletons_.

◆ register_impl()

void kcenon::common::di::ServiceContainer::register_impl ( std::type_index type,
std::any factory )
inlineoverrideexportprotectedvirtual

Implements kcenon::common::di::IServiceContainer.

Definition at line 119 of file di.cppm.

119 {
120 std::lock_guard<std::mutex> lock(mutex_);
121 factories_[type] = std::move(factory);
122 }

References factories_, and mutex_.

◆ register_singleton_impl()

void kcenon::common::di::ServiceContainer::register_singleton_impl ( std::type_index type,
std::any instance )
inlineoverrideexportprotectedvirtual

Implements kcenon::common::di::IServiceContainer.

Definition at line 124 of file di.cppm.

124 {
125 std::lock_guard<std::mutex> lock(mutex_);
126 singletons_[type] = std::move(instance);
127 }

References mutex_, and singletons_.

◆ resolve_impl()

std::any kcenon::common::di::ServiceContainer::resolve_impl ( std::type_index type)
inlineoverrideexportprotectedvirtual

Implements kcenon::common::di::IServiceContainer.

Definition at line 129 of file di.cppm.

129 {
130 std::lock_guard<std::mutex> lock(mutex_);
131
132 // Check singletons first
133 auto singleton_it = singletons_.find(type);
134 if (singleton_it != singletons_.end()) {
135 return singleton_it->second;
136 }
137
138 // Check factories
139 auto factory_it = factories_.find(type);
140 if (factory_it != factories_.end()) {
141 return factory_it->second;
142 }
143
144 return std::any{};
145 }

References factories_, mutex_, and singletons_.

Member Data Documentation

◆ factories_

std::unordered_map<std::type_index, std::any> kcenon::common::di::ServiceContainer::factories_
exportprivate

Definition at line 154 of file di.cppm.

Referenced by has_impl(), register_impl(), and resolve_impl().

◆ mutex_

std::mutex kcenon::common::di::ServiceContainer::mutex_
mutableexportprivate

Definition at line 153 of file di.cppm.

Referenced by has_impl(), register_impl(), register_singleton_impl(), and resolve_impl().

◆ singletons_

std::unordered_map<std::type_index, std::any> kcenon::common::di::ServiceContainer::singletons_
exportprivate

Definition at line 155 of file di.cppm.

Referenced by has_impl(), register_singleton_impl(), and resolve_impl().


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