6 #include <shared_mutex>
8 #include <unordered_map>
12 #include <spdlog/spdlog.h>
100 template<
typename specific_service>
102 const std::unique_lock<std::shared_mutex> lock{_m_mutex};
104 const std::type_index type_index = std::type_index(
typeid(specific_service));
106 spdlog::get(
"illixr")->debug(
"[phonebook] Register {}", type_index.name());
108 assert(_m_registry.count(type_index) == 0);
109 _m_registry.try_emplace(type_index, impl);
121 template<
typename specific_service>
123 const std::shared_lock<std::shared_mutex> lock{_m_mutex};
125 const std::type_index type_index = std::type_index(
typeid(specific_service));
129 if (_m_registry.count(type_index) != 1) {
130 throw std::runtime_error{
"Attempted to lookup an unregistered implementation " + std::string{type_index.name()}};
134 std::shared_ptr<service> this_service = _m_registry.at(type_index);
135 assert(this_service);
137 std::shared_ptr<specific_service> this_specific_service = std::dynamic_pointer_cast<specific_service>(this_service);
138 assert(this_specific_service);
140 return this_specific_service;
144 std::unordered_map<std::type_index, const std::shared_ptr<service>> _m_registry;
145 mutable std::shared_mutex _m_mutex;
A 'service' that can be registered in the phonebook.
Definition: phonebook.hpp:86
A service locator for ILLIXR.
Definition: phonebook.hpp:68
void register_impl(std::shared_ptr< specific_service > impl)
Registers an implementation of baseclass for future calls to lookup.
Definition: phonebook.hpp:101
std::shared_ptr< specific_service > lookup_impl() const
Look up an implementation of specific_service, which should be registered first.
Definition: phonebook.hpp:122
RAC_ERRNO_MSG.
Definition: data_format.hpp:15