Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
service_container.h
Go to the documentation of this file.
1
7#pragma once
8
9// BSD 3-Clause License
10// Copyright (c) 2025, 🍀☀🌕🌥 🌊
11// See the LICENSE file in the project root for full license information.
12
13#include <memory>
14#include <typeindex>
15#include <unordered_map>
16#include <mutex>
17#include <functional>
18
19namespace kcenon::thread {
20
31public:
35 enum class lifetime {
36 singleton, // Single instance shared across all requests
37 transient // New instance for each request
38 };
39
46 template<typename Interface, typename Implementation>
47 void register_singleton(std::shared_ptr<Implementation> instance) {
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 }
55
62 template<typename Interface>
63 void register_factory(std::function<std::shared_ptr<Interface>()> factory,
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 }
72
79 template<typename Interface, typename Implementation, typename... Args>
81 static_assert(std::is_base_of_v<Interface, Implementation>,
82 "Implementation must derive from Interface");
83
85 []() { return std::make_shared<Implementation>(); },
87 }
88
94 template<typename Interface>
95 std::shared_ptr<Interface> resolve() {
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 }
124
130 template<typename Interface>
131 bool is_registered() const {
132 std::lock_guard<std::mutex> lock(mutex_);
133 return services_.find(std::type_index(typeid(Interface))) != services_.end();
134 }
135
139 void clear() {
140 std::lock_guard<std::mutex> lock(mutex_);
141 services_.clear();
142 }
143
149 static service_container instance;
150 return instance;
151 }
152
153private:
156 mutable std::shared_ptr<void> instance;
157 std::function<std::shared_ptr<void>()> factory;
158 };
159
160 mutable std::mutex mutex_;
161 std::unordered_map<std::type_index, service_entry> services_;
162};
163
167template<typename Interface>
169public:
170 scoped_service(std::shared_ptr<Interface> service) {
171 service_container::global().register_singleton<Interface>(service);
172 }
173
175 // Note: In a real implementation, you might want to support unregistration
176 // For now, services remain registered for the application lifetime
177 }
178};
179
180} // namespace kcenon::thread
RAII helper for scoped service registration.
scoped_service(std::shared_ptr< Interface > service)
Modern service container for dependency injection.
void register_singleton(std::shared_ptr< Implementation > instance)
Register a service with singleton lifetime.
void clear()
Clear all registered services.
static service_container & global()
Get the global service container instance.
bool is_registered() const
Check if a service is registered.
std::shared_ptr< Interface > resolve()
Resolve a service.
void register_factory(std::function< std::shared_ptr< Interface >()> factory, lifetime lt=lifetime::transient)
Register a service with factory function.
void register_transient()
Register a transient service.
std::unordered_map< std::type_index, service_entry > services_
Core threading foundation of the thread system library.
Definition thread_impl.h:17
std::shared_ptr< void > instance
lifetime lifetime_scope
std::function< std::shared_ptr< void >()> factory