2#ifndef _CONSOLIX_SERVICE_LOCATOR_HPP_INCLUDED
3#define _CONSOLIX_SERVICE_LOCATOR_HPP_INCLUDED
9#if CONSOLIX_USE_LOGIT == 1
10#include <log-it/LogIt.hpp>
13#include <unordered_map>
18#include <shared_mutex>
44 std::unique_lock<std::shared_mutex> lock(
m_mutex);
45 const auto type = std::type_index(
typeid(T));
47# if CONSOLIX_USE_LOGIT == 1
48 LOGIT_PRINT_ERROR(
"Service already registered: ", std::string(
typeid(T).name()));
50 throw std::runtime_error(
"Service already registered: " + std::string(
typeid(T).name()));
63 std::unique_lock<std::shared_mutex> lock(
m_mutex);
64 const auto type = std::type_index(
typeid(T));
66# if CONSOLIX_USE_LOGIT == 1
67 LOGIT_PRINT_ERROR(
"Service already registered: ", std::string(
typeid(T).name()));
69 throw std::runtime_error(
"Service already registered: " + std::string(
typeid(T).name()));
80 std::shared_lock<std::shared_mutex> lock(
m_mutex);
81 const auto type = std::type_index(
typeid(T));
84# if CONSOLIX_USE_LOGIT == 1
85 LOGIT_PRINT_ERROR(
"Service not registered: ", std::string(
typeid(T).name()));
87 throw std::runtime_error(
"Service not registered: " + std::string(
typeid(T).name()));
89 return *std::static_pointer_cast<T>(it->second);
97 std::shared_lock<std::shared_mutex> lock(
m_mutex);
98 const auto type_id = std::type_index(
typeid(T));
104 std::unique_lock<std::shared_mutex> lock(
m_mutex);
static ServiceLocator & get_instance()
Retrieves the singleton instance of the ServiceLocator.
void clear_all()
Clears all registered resources.
ServiceLocator(ServiceLocator &&)=delete
void register_service(std::function< std::shared_ptr< T >()> creator)
Registers a resource or service.
std::unordered_map< std::type_index, std::shared_ptr< void > > m_services
Registered services.
bool has_service()
Checks if a resource is registered.
ServiceLocator & operator=(const ServiceLocator &)=delete
T & get_service()
Retrieves a resource from the locator.
~ServiceLocator()=default
std::shared_mutex m_mutex
Mutex for thread-safe access.
void register_service()
Registers a resource with default construction.
ServiceLocator(const ServiceLocator &)=delete
ServiceLocator & operator=(ServiceLocator &&)=delete
< Utility modules and helpers.