13#ifndef RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
14#define RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
61 template <
typename Service>
62 void Add(Service&& service,
const std::string& name =
"") {
63 std::string key = CalcMapKey(
typeid(Service), name);
64 if (ListContains(key)) {
65 CII_THROW(
RtctkException,
"service '" + key +
"' already registered");
67 m_services.emplace_back(key,
false, std::forward<Service>(service));
81 template <
typename Service>
82 void Add(std::shared_ptr<Service> service,
const std::string& name =
"") {
83 std::string key = CalcMapKey(
typeid(Service), name);
84 if (ListContains(key)) {
85 CII_THROW(
RtctkException,
"service '" + key +
"' already registered");
87 m_services.emplace_back(key,
true, service);
99 template <
typename Service>
100 Service&
Get(
const std::string& name =
"") {
101 std::string key = CalcMapKey(
typeid(Service), name);
102 auto it = std::find_if(m_services.begin(), m_services.end(), [&](
auto& svc) {
103 return (key == std::get<0>(svc));
106 if (m_services.end() == it) {
107 CII_THROW(
RtctkException,
"service '" + key +
"' not registered");
109 if (std::get<1>(*it)) {
110 return *std::any_cast<std::shared_ptr<Service>>(std::get<2>(*it));
112 return std::any_cast<Service&>(std::get<2>(*it));
124 template <
typename Service>
126 return ListContains(CalcMapKey(
typeid(Service), name));
134 std::vector<std::string>
List();
137 std::string CalcMapKey(
const std::type_info& tid,
const std::string& name);
139 bool ListContains(
const std::string& key);
142 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:237
Container class that holds services of any type.
Definition: serviceContainer.hpp:39
std::vector< std::string > List()
List currently available services.
Definition: serviceContainer.cpp:26
ServiceContainer(ServiceContainer const &other)=delete
void Add(std::shared_ptr< Service > service, const std::string &name="")
Insert new service into the container.
Definition: serviceContainer.hpp:82
ServiceContainer & operator=(ServiceContainer const &other)=delete
bool Contains(const std::string &name="")
Check if the service container holds a certain service.
Definition: serviceContainer.hpp:125
ServiceContainer()=default
ServiceContainer & operator=(ServiceContainer &&other)=delete
~ServiceContainer()
Definition: serviceContainer.cpp:18
void Add(Service &&service, const std::string &name="")
Insert new service into the container.
Definition: serviceContainer.hpp:62
ServiceContainer(ServiceContainer const &&other)=delete
Service & Get(const std::string &name="")
Get a handle to a service.
Definition: serviceContainer.hpp:100
Provides macros and utilities for exception handling.
Definition: commandReplier.cpp:22