|
Common System 0.2.0
Common interfaces and patterns for system integration
|
Simple thread-safe service container implementation. More...


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< IServiceScope > | create_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_descriptor > | registered_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_ |
|
exportdefault |
|
inlineoverrideexportprotectedvirtual |
Implements kcenon::common::di::IServiceContainer.
Definition at line 147 of file di.cppm.
References factories_, mutex_, and singletons_.
|
inlineoverrideexportprotectedvirtual |
Implements kcenon::common::di::IServiceContainer.
Definition at line 119 of file di.cppm.
References factories_, and mutex_.
|
inlineoverrideexportprotectedvirtual |
Implements kcenon::common::di::IServiceContainer.
Definition at line 124 of file di.cppm.
References mutex_, and singletons_.
|
inlineoverrideexportprotectedvirtual |
Implements kcenon::common::di::IServiceContainer.
Definition at line 129 of file di.cppm.
References factories_, mutex_, and singletons_.
|
exportprivate |
Definition at line 154 of file di.cppm.
Referenced by has_impl(), register_impl(), and resolve_impl().
|
mutableexportprivate |
Definition at line 153 of file di.cppm.
Referenced by has_impl(), register_impl(), register_singleton_impl(), and resolve_impl().
|
exportprivate |
Definition at line 155 of file di.cppm.
Referenced by has_impl(), register_singleton_impl(), and resolve_impl().