17#include <shared_mutex>
19#include <unordered_map>
31 static inline std::unordered_map<std::type_index, std::any>
services_{};
32 static inline std::shared_mutex
mutex_{};
44 template <
typename Interface>
46 std::unique_lock lock(
mutex_);
47 services_[std::type_index(
typeid(Interface))] = std::move(service);
69 template <
typename Interface>
71 std::shared_lock lock(
mutex_);
72 auto it =
services_.find(std::type_index(
typeid(Interface)));
75 return std::any_cast<std::shared_ptr<Interface>>(it->second);
76 }
catch (
const std::bad_any_cast& e) {
91 std::unique_lock lock(
mutex_);
103 std::shared_lock lock(
mutex_);
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.