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

Lightweight service registry for dependency lookup. More...

#include <service_registry.h>

Collaboration diagram for kcenon::thread::service_registry:
Collaboration graph

Static Public Member Functions

template<typename Interface >
static auto register_service (std::shared_ptr< Interface > service) -> void
 Register a service implementation for the given interface type.
 
template<typename Interface >
static auto get_service () -> std::shared_ptr< Interface >
 
static auto clear_services () -> void
 Remove all registered services.
 
static auto get_service_count () -> std::size_t
 Get the number of registered services.
 

Static Private Attributes

static std::unordered_map< std::type_index, std::any > services_ {}
 
static std::shared_mutex mutex_ {}
 

Detailed Description

Lightweight service registry for dependency lookup.

Thread Safety:
Thread-safe for concurrent registration and retrieval. All methods use std::shared_mutex for synchronization.

Definition at line 29 of file service_registry.h.

Member Function Documentation

◆ clear_services()

static auto kcenon::thread::service_registry::clear_services ( ) -> void
inlinestatic

Remove all registered services.

Thread Safety:
Thread-safe. Uses exclusive lock (std::unique_lock).
Warning
Clears all services. Ensure no threads are actively using services.
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/service_registry.h.

Definition at line 90 of file service_registry.h.

90 {
91 std::unique_lock lock(mutex_);
92 services_.clear();
93 }
static std::unordered_map< std::type_index, std::any > services_
static std::shared_mutex mutex_

References mutex_, and services_.

◆ get_service()

template<typename Interface >
static auto kcenon::thread::service_registry::get_service ( ) -> std::shared_ptr<Interface>
inlinestatic
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/service_registry.h.

Definition at line 70 of file service_registry.h.

70 {
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 }

References mutex_, and services_.

Referenced by main().

Here is the caller graph for this function:

◆ get_service_count()

static auto kcenon::thread::service_registry::get_service_count ( ) -> std::size_t
inlinestatic

Get the number of registered services.

Returns
std::size_t Number of currently registered services
Thread Safety:
Thread-safe. Uses shared lock (std::shared_lock).
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/service_registry.h.

Definition at line 102 of file service_registry.h.

102 {
103 std::shared_lock lock(mutex_);
104 return services_.size();
105 }

References mutex_, and services_.

◆ register_service()

template<typename Interface >
static auto kcenon::thread::service_registry::register_service ( std::shared_ptr< Interface > service) -> void
inlinestatic

Register a service implementation for the given interface type.

Template Parameters
InterfaceService interface type
Parameters
serviceShared pointer to service implementation
Thread Safety:
Thread-safe. Uses exclusive lock (std::unique_lock).
Note
If service is already registered, it will be replaced.
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/service_registry.h.

Definition at line 45 of file service_registry.h.

45 {
46 std::unique_lock lock(mutex_);
47 services_[std::type_index(typeid(Interface))] = std::move(service);
48 }

References mutex_, and services_.

Referenced by main().

Here is the caller graph for this function:

Member Data Documentation

◆ mutex_

std::shared_mutex kcenon::thread::service_registry::mutex_ {}
inlinestaticprivate

◆ services_

std::unordered_map<std::type_index, std::any> kcenon::thread::service_registry::services_ {}
inlinestaticprivate

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