|
Common System 0.2.0
Common interfaces and patterns for system integration
|
Concrete implementation of IServiceContainer. More...
#include <service_container.h>


Classes | |
| struct | service_entry |
| Internal service registration entry. More... | |
Public Member Functions | |
| service_container ()=default | |
| Default constructor. | |
| ~service_container () override=default | |
| Destructor. | |
| service_container (const service_container &)=delete | |
| service_container & | operator= (const service_container &)=delete |
| service_container (service_container &&)=delete | |
| service_container & | operator= (service_container &&)=delete |
| 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. | |
| void | freeze () |
| Freeze the container to prevent further registrations. | |
| bool | is_frozen () const |
| Check if the container is frozen. | |
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. | |
Static Public Member Functions | |
| static service_container & | global () |
| Get the global service container instance. | |
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 Member Functions | |
| Result< std::shared_ptr< void > > | resolve_with_detection (std::type_index interface_type, std::unordered_map< std::type_index, std::shared_ptr< void > > *scoped_instances=nullptr) |
| Resolve a service with circular dependency detection. | |
| bool | is_resolving (std::type_index interface_type) const |
| Check if currently resolving this type (circular dependency check). | |
| void | push_resolution (std::type_index interface_type) |
| Push type onto resolution stack. | |
| void | pop_resolution (std::type_index interface_type) |
| Pop type from resolution stack. | |
| std::string | get_resolution_stack_string () const |
| Get current resolution stack as string for error messages. | |
| VoidResult | check_frozen_for_registration (const std::string &type_name, interfaces::registry_action action, const std::string &error_message) const |
| Check if container is frozen and log audit event if so. | |
| VoidResult | check_already_registered (std::type_index interface_type, const std::string &type_name) const |
| Check if a service is already registered and log audit event if so. | |
| Result< std::shared_ptr< void > > | invoke_factory_safe (const std::function< std::shared_ptr< void >(IServiceContainer &)> &factory) |
| Safely invoke a factory function with exception handling. | |
Private Attributes | |
| std::shared_mutex | mutex_ |
| std::unordered_map< std::type_index, service_entry > | services_ |
| std::atomic< bool > | frozen_ {false} |
Static Private Attributes | |
| static thread_local std::unordered_set< std::type_index > | resolution_stack_ |
| static thread_local std::vector< std::type_index > | resolution_order_ |
Friends | |
| class | service_scope |
Concrete implementation of IServiceContainer.
Provides a thread-safe dependency injection container with support for:
Usage Example:
Definition at line 66 of file service_container.h.
|
default |
Default constructor.
|
overridedefault |
Destructor.
|
delete |
|
delete |
|
inlineprivate |
Check if a service is already registered and log audit event if so.
| interface_type | The type to check |
| type_name | The type name for audit logging |
Definition at line 435 of file service_container.h.
References kcenon::common::di::di_error_codes::already_registered, kcenon::common::source_location::current(), kcenon::common::interfaces::RegistryAuditLog::log_event(), kcenon::common::make_error(), kcenon::common::Result< T >::ok(), kcenon::common::interfaces::register_service, and services_.
Referenced by register_factory_internal(), and register_instance_internal().


|
inlineprivate |
Check if container is frozen and log audit event if so.
| type_name | The type name for audit logging |
| action | The registry action being performed |
| error_message | The error message to use if frozen |
Definition at line 415 of file service_container.h.
References kcenon::common::source_location::current(), is_frozen(), kcenon::common::interfaces::RegistryAuditLog::log_event(), kcenon::common::make_error(), kcenon::common::Result< T >::ok(), and kcenon::common::error_codes::REGISTRY_FROZEN.
Referenced by register_factory_internal(), and register_instance_internal().


|
inlineoverridevirtual |
Clear all registrations.
Implements kcenon::common::di::IServiceContainer.
Definition at line 376 of file service_container.h.
References kcenon::common::interfaces::clear_services, kcenon::common::source_location::current(), is_frozen(), kcenon::common::interfaces::RegistryAuditLog::log_event(), kcenon::common::make_error(), mutex_, kcenon::common::Result< T >::ok(), kcenon::common::error_codes::REGISTRY_FROZEN, and services_.
Referenced by kcenon::common::di::unified_bootstrapper::initialize(), and kcenon::common::di::unified_bootstrapper::shutdown().


|
inlineoverridevirtual |
Create a new service scope.
Implements kcenon::common::di::IServiceContainer.
Definition at line 357 of file service_container.h.
|
inline |
Freeze the container to prevent further registrations.
Once frozen, no new services can be registered or unregistered. Existing services can still be resolved. This is a one-way operation and cannot be undone.
Definition at line 399 of file service_container.h.
References kcenon::common::source_location::current(), kcenon::common::interfaces::freeze_service_container, frozen_, and kcenon::common::interfaces::RegistryAuditLog::log_event().
Referenced by kcenon::common::bootstrap::SystemBootstrapper::initialize().


|
inlineprivate |
Get current resolution stack as string for error messages.
Definition at line 730 of file service_container.h.
References resolution_order_.
Referenced by resolve_with_detection().

|
inlinestatic |
Get the global service container instance.
Returns a reference to the application-wide singleton container. Thread-safe for concurrent access.
Definition at line 352 of file service_container.h.
Referenced by kcenon::common::bootstrap::SystemBootstrapper::initialize(), kcenon::common::di::unified_bootstrapper::initialize(), main(), kcenon::common::di::unified_bootstrapper::register_module(), kcenon::common::di::unified_bootstrapper::register_optional_services(), kcenon::common::di::unified_bootstrapper::services(), and kcenon::common::di::unified_bootstrapper::shutdown().

|
inlineprivate |
Safely invoke a factory function with exception handling.
| factory | The factory function to invoke |
Definition at line 454 of file service_container.h.
References kcenon::common::di::di_error_codes::factory_error, kcenon::common::make_error(), and kcenon::common::ok().
Referenced by resolve_with_detection().


|
inline |
Check if the container is frozen.
Definition at line 407 of file service_container.h.
References frozen_.
Referenced by check_frozen_for_registration(), clear(), and unregister_internal().

|
inlineoverrideprotectedvirtual |
Internal registration check (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 670 of file service_container.h.
References mutex_, and services_.
Referenced by kcenon::common::di::service_scope::is_registered_internal().

|
inlineprivate |
Check if currently resolving this type (circular dependency check).
Definition at line 714 of file service_container.h.
References resolution_stack_.
Referenced by resolve_with_detection().

|
delete |
|
delete |
|
inlineprivate |
Pop type from resolution stack.
Definition at line 723 of file service_container.h.
References resolution_order_, and resolution_stack_.
Referenced by resolve_with_detection().

|
inlineprivate |
Push type onto resolution stack.
Definition at line 718 of file service_container.h.
References resolution_order_, and resolution_stack_.
Referenced by resolve_with_detection().

|
inlineoverrideprotectedvirtual |
Internal factory registration (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 468 of file service_container.h.
References check_already_registered(), check_frozen_for_registration(), kcenon::common::source_location::current(), kcenon::common::interfaces::RegistryAuditLog::log_event(), mutex_, kcenon::common::Result< T >::ok(), kcenon::common::interfaces::register_service, and services_.
Referenced by kcenon::common::di::service_scope::register_factory_internal().


|
inlineoverrideprotectedvirtual |
Internal instance registration (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 503 of file service_container.h.
References check_already_registered(), check_frozen_for_registration(), kcenon::common::source_location::current(), kcenon::common::interfaces::RegistryAuditLog::log_event(), mutex_, kcenon::common::Result< T >::ok(), kcenon::common::interfaces::register_service, services_, kcenon::common::di::singleton, and kcenon::common::di::service_container::service_entry::singleton_instance.
Referenced by kcenon::common::di::service_scope::register_instance_internal().


|
inlineoverridevirtual |
Get list of all registered service descriptors.
Implements kcenon::common::di::IServiceContainer.
Definition at line 361 of file service_container.h.
References kcenon::common::di::service_descriptor::is_instantiated, mutex_, and services_.
Referenced by kcenon::common::di::service_scope::registered_services().

|
inlineoverrideprotectedvirtual |
Internal service resolution (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 544 of file service_container.h.
References resolve_with_detection().

|
inlineprivate |
Resolve a service with circular dependency detection.
| interface_type | The type to resolve |
| scoped_instances | Optional map for scoped instances (from scope) |
Definition at line 549 of file service_container.h.
References kcenon::common::di::di_error_codes::circular_dependency, get_resolution_stack_string(), kcenon::common::di::di_error_codes::invalid_lifetime, invoke_factory_safe(), is_resolving(), kcenon::common::make_error(), mutex_, kcenon::common::ok(), pop_resolution(), push_resolution(), kcenon::common::di::scoped, kcenon::common::di::di_error_codes::scoped_from_root, kcenon::common::di::di_error_codes::service_not_registered, services_, kcenon::common::di::singleton, and kcenon::common::di::transient.
Referenced by resolve_internal(), and kcenon::common::di::service_scope::resolve_internal().


|
inlineoverrideprotectedvirtual |
Internal unregistration (type-erased).
Implements kcenon::common::di::IServiceContainer.
Definition at line 675 of file service_container.h.
References kcenon::common::source_location::current(), is_frozen(), kcenon::common::interfaces::RegistryAuditLog::log_event(), kcenon::common::make_error(), mutex_, kcenon::common::Result< T >::ok(), kcenon::common::error_codes::REGISTRY_FROZEN, kcenon::common::di::di_error_codes::service_not_registered, services_, and kcenon::common::interfaces::unregister_service.
Referenced by kcenon::common::di::service_scope::unregister_internal().


|
friend |
Definition at line 161 of file service_container.h.
|
private |
Definition at line 256 of file service_container.h.
Referenced by freeze(), and is_frozen().
|
mutableprivate |
Definition at line 252 of file service_container.h.
Referenced by clear(), is_registered_internal(), register_factory_internal(), register_instance_internal(), registered_services(), resolve_with_detection(), and unregister_internal().
|
inlinestaticprivate |
Definition at line 260 of file service_container.h.
Referenced by get_resolution_stack_string(), pop_resolution(), and push_resolution().
|
inlinestaticprivate |
Definition at line 259 of file service_container.h.
Referenced by is_resolving(), pop_resolution(), and push_resolution().
|
private |
Definition at line 253 of file service_container.h.
Referenced by check_already_registered(), clear(), is_registered_internal(), register_factory_internal(), register_instance_internal(), registered_services(), resolve_with_detection(), and unregister_internal().