|
Common System 0.2.0
Common interfaces and patterns for system integration
|
Scoped service container implementation. More...
#include <service_container.h>


Public Member Functions | |
| service_scope (service_container &parent) | |
| Construct a scope with a parent container. | |
| ~service_scope () override=default | |
| Destructor - disposes scoped instances. | |
| service_scope (const service_scope &)=delete | |
| service_scope & | operator= (const service_scope &)=delete |
| service_scope (service_scope &&)=delete | |
| service_scope & | operator= (service_scope &&)=delete |
| IServiceContainer & | parent () override |
| Get the parent container. | |
| const IServiceContainer & | parent () const override |
| Get the parent container (const). | |
| std::unique_ptr< IServiceScope > | create_scope () override |
| Create a new service scope. | |
| std::vector< service_descriptor > | registered_services () const override |
| Get list of all registered service descriptors. | |
| VoidResult | clear () override |
| Clear all registrations. | |
Public Member Functions inherited from kcenon::common::di::IServiceScope | |
| ~IServiceScope () override=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. | |
| template<typename TInterface > | |
| bool | is_registered () const |
| Check if a service type is registered. | |
| template<typename TInterface > | |
| VoidResult | unregister () |
| Unregister a service type. | |
| 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 | |
| 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) override |
| Internal factory registration (type-erased). | |
| VoidResult | register_instance_internal (std::type_index interface_type, const std::string &type_name, std::shared_ptr< void > instance) override |
| Internal instance registration (type-erased). | |
| Result< std::shared_ptr< void > > | resolve_internal (std::type_index interface_type) override |
| Internal service resolution (type-erased). | |
| bool | is_registered_internal (std::type_index interface_type) const override |
| Internal registration check (type-erased). | |
| VoidResult | unregister_internal (std::type_index interface_type) override |
| Internal unregistration (type-erased). | |
Protected Member Functions inherited from kcenon::common::di::IServiceContainer | |
| virtual void | register_impl (std::type_index type, std::any factory)=0 |
| virtual void | register_singleton_impl (std::type_index type, std::any instance)=0 |
| virtual std::any | resolve_impl (std::type_index type)=0 |
| virtual bool | has_impl (std::type_index type) const =0 |
Private Attributes | |
| service_container & | parent_ |
| std::shared_mutex | mutex_ |
| std::unordered_map< std::type_index, std::shared_ptr< void > > | scoped_instances_ |
Scoped service container implementation.
A service scope shares singleton registrations with its parent container but maintains its own instances for scoped services. When the scope is destroyed, all scoped instances are disposed.
Thread Safety:
Definition at line 280 of file service_container.h.
|
inlineexplicit |
Construct a scope with a parent container.
| parent | Reference to the parent container |
Definition at line 745 of file service_container.h.
|
overridedefault |
Destructor - disposes scoped instances.
|
delete |
|
delete |
|
inlineoverridevirtual |
Clear all registrations.
Removes all service registrations. Existing resolved instances remain valid but no services can be resolved after this call.
Implements kcenon::common::di::IServiceContainer.
Definition at line 766 of file service_container.h.
References mutex_, kcenon::common::Result< T >::ok(), and scoped_instances_.

|
inlineoverridevirtual |
Create a new service scope.
Returns a scoped container that shares singleton registrations with the parent but maintains its own instances for scoped services.
Implements kcenon::common::di::IServiceContainer.
Definition at line 756 of file service_container.h.
References parent_.
|
inlineoverrideprotectedvirtual |
Internal registration check (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 798 of file service_container.h.
References kcenon::common::di::service_container::is_registered_internal(), and parent_.

|
delete |
|
delete |
|
inlineoverridevirtual |
Get the parent container (const).
Implements kcenon::common::di::IServiceScope.
Definition at line 752 of file service_container.h.
References parent_.
|
inlineoverridevirtual |
Get the parent container.
Implements kcenon::common::di::IServiceScope.
Definition at line 748 of file service_container.h.
References parent_.
|
inlineoverrideprotectedvirtual |
Internal factory registration (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 772 of file service_container.h.
References parent_, and kcenon::common::di::service_container::register_factory_internal().

|
inlineoverrideprotectedvirtual |
Internal instance registration (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 781 of file service_container.h.
References parent_, and kcenon::common::di::service_container::register_instance_internal().

|
inlineoverridevirtual |
Get list of all registered service descriptors.
Implements kcenon::common::di::IServiceContainer.
Definition at line 761 of file service_container.h.
References parent_, and kcenon::common::di::service_container::registered_services().

|
inlineoverrideprotectedvirtual |
Internal service resolution (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 789 of file service_container.h.
References mutex_, parent_, kcenon::common::di::service_container::resolve_with_detection(), and scoped_instances_.

|
inlineoverrideprotectedvirtual |
Internal unregistration (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 802 of file service_container.h.
References parent_, and kcenon::common::di::service_container::unregister_internal().

|
mutableprivate |
Definition at line 340 of file service_container.h.
Referenced by clear(), and resolve_internal().
|
private |
Definition at line 339 of file service_container.h.
Referenced by create_scope(), is_registered_internal(), parent(), parent(), register_factory_internal(), register_instance_internal(), registered_services(), resolve_internal(), and unregister_internal().
|
private |
Definition at line 341 of file service_container.h.
Referenced by clear(), and resolve_internal().