Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
service_registry.h
Go to the documentation of this file.
1/*
2 * BSD 3-Clause License
3 * Copyright (c) 2025, DongCheol Shin
4 */
5
12#pragma once
13
14#include <any>
15#include <memory>
16#include <mutex>
17#include <shared_mutex>
18#include <typeindex>
19#include <unordered_map>
20
21namespace kcenon::thread {
22
30private:
31 static inline std::unordered_map<std::type_index, std::any> services_{};
32 static inline std::shared_mutex mutex_{};
33
34public:
44 template <typename Interface>
45 static auto register_service(std::shared_ptr<Interface> service) -> void {
46 std::unique_lock lock(mutex_);
47 services_[std::type_index(typeid(Interface))] = std::move(service);
48 }
49
69 template <typename Interface>
70 static auto get_service() -> std::shared_ptr<Interface> {
71 std::shared_lock lock(mutex_);
72 auto it = services_.find(std::type_index(typeid(Interface)));
73 if (it != services_.end()) {
74 try {
75 return std::any_cast<std::shared_ptr<Interface>>(it->second);
76 } catch (const std::bad_any_cast& e) {
77 // Type mismatch - service was registered with incompatible type
78 return nullptr;
79 }
80 }
81 return nullptr;
82 }
83
90 static auto clear_services() -> void {
91 std::unique_lock lock(mutex_);
92 services_.clear();
93 }
94
102 static auto get_service_count() -> std::size_t {
103 std::shared_lock lock(mutex_);
104 return services_.size();
105 }
106};
107
108} // namespace kcenon::thread
109
Lightweight service registry for dependency lookup.
static auto clear_services() -> void
Remove all registered services.
static auto get_service() -> std::shared_ptr< Interface >
static std::unordered_map< std::type_index, std::any > services_
static std::shared_mutex mutex_
static auto register_service(std::shared_ptr< Interface > service) -> void
Register a service implementation for the given interface type.
static auto get_service_count() -> std::size_t
Get the number of registered services.
Core threading foundation of the thread system library.
Definition thread_impl.h:17