RTC Toolkit 4.0.1
Loading...
Searching...
No Matches
serviceContainer.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
14#define RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
15
17
18#include <any>
19#include <list>
20#include <memory>
21#include <string>
22#include <utility>
23#include <vector>
24
26
40public:
41 ServiceContainer() = default;
42
43 ServiceContainer(ServiceContainer const& other) = delete;
45
46 ServiceContainer(ServiceContainer const&& other) = delete;
48
50
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");
66 }
67 m_services.emplace_back(key, false, std::forward<Service>(service));
68 }
69
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");
86 }
87 m_services.emplace_back(key, true, service);
88 }
89
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));
104 });
105
106 if (m_services.end() == it) {
107 CII_THROW(RtctkException, "service '" + key + "' not registered");
108 }
109 if (std::get<1>(*it)) {
110 return *std::any_cast<std::shared_ptr<Service>>(std::get<2>(*it));
111 } else {
112 return std::any_cast<Service&>(std::get<2>(*it));
113 }
114 }
115
124 template <typename Service>
125 bool Contains(const std::string& name = "") {
126 return ListContains(CalcMapKey(typeid(Service), name));
127 }
128
134 std::vector<std::string> List();
135
136private:
137 std::string CalcMapKey(const std::type_info& tid, const std::string& name);
138
139 bool ListContains(const std::string& key);
140
141 // tuple elements: service id, flag that is true if svc held by pointer, service object;
142 std::list<std::tuple<std::string, bool, std::any>> m_services;
143};
144
145} // namespace rtctk::componentFramework
146
147#endif
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 & 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