Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::service_container Class Reference

Modern service container for dependency injection. More...

#include <service_container.h>

Collaboration diagram for kcenon::thread::service_container:
Collaboration graph

Classes

struct  service_entry
 

Public Types

enum class  lifetime { singleton , transient }
 Service lifetime scope. More...
 

Public Member Functions

template<typename Interface , typename Implementation >
void register_singleton (std::shared_ptr< Implementation > instance)
 Register a service with singleton lifetime.
 
template<typename Interface >
void register_factory (std::function< std::shared_ptr< Interface >()> factory, lifetime lt=lifetime::transient)
 Register a service with factory function.
 
template<typename Interface , typename Implementation , typename... Args>
void register_transient ()
 Register a transient service.
 
template<typename Interface >
std::shared_ptr< Interface > resolve ()
 Resolve a service.
 
template<typename Interface >
bool is_registered () const
 Check if a service is registered.
 
void clear ()
 Clear all registered services.
 

Static Public Member Functions

static service_containerglobal ()
 Get the global service container instance.
 

Private Attributes

std::mutex mutex_
 
std::unordered_map< std::type_index, service_entryservices_
 

Detailed Description

Modern service container for dependency injection.

This container supports:

  • Registration of interfaces with concrete implementations
  • Factory functions for lazy instantiation
  • Singleton and transient lifetime management
  • Thread-safe operations

Definition at line 30 of file service_container.h.

Member Enumeration Documentation

◆ lifetime

Service lifetime scope.

Enumerator
singleton 
transient 

Definition at line 35 of file service_container.h.

35 {
36 singleton, // Single instance shared across all requests
37 transient // New instance for each request
38 };

Member Function Documentation

◆ clear()

void kcenon::thread::service_container::clear ( )
inline

Clear all registered services.

Definition at line 139 of file service_container.h.

139 {
140 std::lock_guard<std::mutex> lock(mutex_);
141 services_.clear();
142 }
std::unordered_map< std::type_index, service_entry > services_

References mutex_, and services_.

Referenced by complete_integration_example(), demonstrate_minimal_usage(), dynamic_service_example(), main(), thread_pool_with_logger_example(), and thread_pool_with_monitoring_example().

Here is the caller graph for this function:

◆ global()

static service_container & kcenon::thread::service_container::global ( )
inlinestatic

Get the global service container instance.

Returns
Reference to the global container

Definition at line 148 of file service_container.h.

148 {
149 static service_container instance;
150 return instance;
151 }

Referenced by complete_integration_example(), demonstrate_composition(), demonstrate_minimal_usage(), dynamic_service_example(), kcenon::thread::thread_context_builder::from_global_registry(), main(), kcenon::thread::scoped_service< Interface >::scoped_service(), thread_pool_with_logger_example(), and thread_pool_with_monitoring_example().

Here is the caller graph for this function:

◆ is_registered()

template<typename Interface >
bool kcenon::thread::service_container::is_registered ( ) const
inline

Check if a service is registered.

Template Parameters
InterfaceThe interface type to check
Returns
true if the service is registered

Definition at line 131 of file service_container.h.

131 {
132 std::lock_guard<std::mutex> lock(mutex_);
133 return services_.find(std::type_index(typeid(Interface))) != services_.end();
134 }

References mutex_, and services_.

◆ register_factory()

template<typename Interface >
void kcenon::thread::service_container::register_factory ( std::function< std::shared_ptr< Interface >()> factory,
lifetime lt = lifetime::transient )
inline

Register a service with factory function.

Template Parameters
InterfaceThe interface type
Parameters
factoryFactory function that creates the service
ltService lifetime (default: transient)

Definition at line 63 of file service_container.h.

64 {
65 std::lock_guard<std::mutex> lock(mutex_);
66 services_[std::type_index(typeid(Interface))] =
67 service_entry{lt, nullptr,
68 [factory]() -> std::shared_ptr<void> {
69 return factory();
70 }};
71 }

References mutex_, and services_.

Referenced by register_transient().

Here is the caller graph for this function:

◆ register_singleton()

template<typename Interface , typename Implementation >
void kcenon::thread::service_container::register_singleton ( std::shared_ptr< Implementation > instance)
inline

Register a service with singleton lifetime.

Template Parameters
InterfaceThe interface type
ImplementationThe concrete implementation type
Parameters
instanceShared pointer to the implementation

Definition at line 47 of file service_container.h.

47 {
48 static_assert(std::is_base_of_v<Interface, Implementation>,
49 "Implementation must derive from Interface");
50
51 std::lock_guard<std::mutex> lock(mutex_);
52 services_[std::type_index(typeid(Interface))] =
53 service_entry{lifetime::singleton, instance, nullptr};
54 }

References mutex_, services_, and singleton.

Referenced by complete_integration_example(), dynamic_service_example(), kcenon::thread::scoped_service< Interface >::scoped_service(), thread_pool_with_logger_example(), and thread_pool_with_monitoring_example().

Here is the caller graph for this function:

◆ register_transient()

template<typename Interface , typename Implementation , typename... Args>
void kcenon::thread::service_container::register_transient ( )
inline

Register a transient service.

Template Parameters
InterfaceThe interface type
ImplementationThe concrete implementation type
ArgsConstructor argument types

Definition at line 80 of file service_container.h.

80 {
81 static_assert(std::is_base_of_v<Interface, Implementation>,
82 "Implementation must derive from Interface");
83
85 []() { return std::make_shared<Implementation>(); },
87 }
void register_factory(std::function< std::shared_ptr< Interface >()> factory, lifetime lt=lifetime::transient)
Register a service with factory function.

References register_factory(), and transient.

Here is the call graph for this function:

◆ resolve()

template<typename Interface >
std::shared_ptr< Interface > kcenon::thread::service_container::resolve ( )
inline

Resolve a service.

Template Parameters
InterfaceThe interface type to resolve
Returns
Shared pointer to the service, or nullptr if not found

Definition at line 95 of file service_container.h.

95 {
96 std::lock_guard<std::mutex> lock(mutex_);
97
98 auto it = services_.find(std::type_index(typeid(Interface)));
99 if (it == services_.end()) {
100 return nullptr;
101 }
102
103 auto& entry = it->second;
104
105 // For singletons with existing instance
106 if (entry.lifetime_scope == lifetime::singleton && entry.instance) {
107 return std::static_pointer_cast<Interface>(entry.instance);
108 }
109
110 // Create new instance using factory
111 if (entry.factory) {
112 auto instance = entry.factory();
113
114 // Store singleton instances
115 if (entry.lifetime_scope == lifetime::singleton) {
116 entry.instance = instance;
117 }
118
119 return std::static_pointer_cast<Interface>(instance);
120 }
121
122 return nullptr;
123 }

References mutex_, services_, and singleton.

Referenced by kcenon::thread::thread_context_builder::from_global_registry().

Here is the caller graph for this function:

Member Data Documentation

◆ mutex_

std::mutex kcenon::thread::service_container::mutex_
mutableprivate

◆ services_

std::unordered_map<std::type_index, service_entry> kcenon::thread::service_container::services_
private

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