12#ifndef RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
13#define RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
60 template <
typename Service>
61 void Add(Service&& service,
const std::string& name =
"") {
62 std::string key = CalcMapKey(
typeid(Service), name);
63 if (ListContains(key)) {
64 CII_THROW(
RtctkException,
"service '" + key +
"' already registered");
66 m_services.emplace_back(key,
false, std::forward<Service>(service));
80 template <
typename Service>
81 void Add(std::shared_ptr<Service> service,
const std::string& name =
"") {
82 std::string key = CalcMapKey(
typeid(Service), name);
83 if (ListContains(key)) {
84 CII_THROW(
RtctkException,
"service '" + key +
"' already registered");
86 m_services.emplace_back(key,
true, std::move(service));
98 template <
typename Service>
99 Service&
Get(
const std::string& name =
"") {
100 std::string key = CalcMapKey(
typeid(Service), name);
102 std::ranges::find_if(m_services, [&](
auto& svc) {
return (key == std::get<0>(svc)); });
104 if (m_services.end() == it) {
105 CII_THROW(
RtctkException,
"service '" + key +
"' not registered");
107 if (std::get<1>(*it)) {
108 return *std::any_cast<std::shared_ptr<Service>>(std::get<2>(*it));
110 return std::any_cast<Service&>(std::get<2>(*it));
122 template <
typename Service>
124 return ListContains(CalcMapKey(
typeid(Service), name));
132 std::vector<std::string>
List();
135 std::string CalcMapKey(
const std::type_info& tid,
const std::string& name);
137 bool ListContains(
const std::string& key);
140 std::list<std::tuple<std::string, bool, std::any>> m_services;
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:220
std::vector< std::string > List()
List currently available services.
Definition serviceContainer.cpp:25
ServiceContainer(const ServiceContainer &other)=delete
ServiceContainer & operator=(const ServiceContainer &other)=delete
void Add(std::shared_ptr< Service > service, const std::string &name="")
Insert new service into the container.
Definition serviceContainer.hpp:81
bool Contains(const std::string &name="")
Check if the service container holds a certain service.
Definition serviceContainer.hpp:123
ServiceContainer()=default
ServiceContainer & operator=(ServiceContainer &&other)=delete
~ServiceContainer()
Definition serviceContainer.cpp:17
void Add(Service &&service, const std::string &name="")
Insert new service into the container.
Definition serviceContainer.hpp:61
Service & Get(const std::string &name="")
Get a handle to a service.
Definition serviceContainer.hpp:99
ServiceContainer(const ServiceContainer &&other)=delete
Provides macros and utilities for exception handling.
Definition commandReplier.cpp:21