Go to the documentation of this file.
12 #ifndef RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
47 template <
typename Service>
48 void Add(Service&& service,
const std::string&
name =
"") {
49 std::string key = CalcMapKey(
typeid(Service),
name);
50 if (MapContains(key)) {
51 CII_THROW(
RtctkException,
"service '" + key +
"' already registered");
53 m_services.emplace(key, std::make_pair(std::forward<Service>(service),
false));
66 template <
typename Service>
67 void Add(std::shared_ptr<Service> service,
const std::string&
name =
"") {
68 std::string key = CalcMapKey(
typeid(Service),
name);
69 if (MapContains(key)) {
70 CII_THROW(
RtctkException,
"service '" + key +
"' already registered");
72 m_services.emplace(key, std::make_pair(service,
true));
84 template <
typename Service>
85 Service&
Get(
const std::string&
name =
"") {
86 std::string key = CalcMapKey(
typeid(Service),
name);
87 if (not MapContains(key)) {
90 if (ServiceIsHeldByPointer(key)) {
91 return *std::any_cast<std::shared_ptr<Service>>(m_services[key].first);
93 return std::any_cast<Service&>(m_services[key].first);
105 template <
typename Service>
107 return MapContains(CalcMapKey(
typeid(Service),
name));
115 std::vector<std::string>
List();
118 std::string CalcMapKey(
const std::type_info& tid,
const std::string&
name);
120 bool MapContains(
const std::string& key);
121 bool ServiceIsHeldByPointer(
const std::string& key);
124 std::map<std::string, std::pair<std::any, bool>> m_services;
Provides macros and utilities for exception handling.
name
Definition: wscript:15
Service & Get(const std::string &name="")
Get a handle to a service.
Definition: serviceContainer.hpp:85
void Add(Service &&service, const std::string &name="")
Insert new service into the container.
Definition: serviceContainer.hpp:48
Definition: commandReplier.cpp:20
void Add(std::shared_ptr< Service > service, const std::string &name="")
Insert new service into the container.
Definition: serviceContainer.hpp:67
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
std::vector< std::string > List()
List currently available services.
Definition: serviceContainer.cpp:17
Container class that holds services of any type.
Definition: serviceContainer.hpp:35
bool Contains(const std::string &name="")
Check if the service container holds a certain service.
Definition: serviceContainer.hpp:106