RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
serviceContainer.hpp
Go to the documentation of this file.
1
11
12#ifndef RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
13#define RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
14
16
17#include <any>
18#include <list>
19#include <memory>
20#include <string>
21#include <utility>
22#include <vector>
23
25
39public:
40 ServiceContainer() = default;
41
42 ServiceContainer(const ServiceContainer& other) = delete;
44
45 ServiceContainer(const ServiceContainer&& other) = delete;
47
49
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");
65 }
66 m_services.emplace_back(key, false, std::forward<Service>(service));
67 }
68
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");
85 }
86 m_services.emplace_back(key, true, std::move(service));
87 }
88
98 template <typename Service>
99 Service& Get(const std::string& name = "") {
100 std::string key = CalcMapKey(typeid(Service), name);
101 auto it =
102 std::ranges::find_if(m_services, [&](auto& svc) { return (key == std::get<0>(svc)); });
103
104 if (m_services.end() == it) {
105 CII_THROW(RtctkException, "service '" + key + "' not registered");
106 }
107 if (std::get<1>(*it)) {
108 return *std::any_cast<std::shared_ptr<Service>>(std::get<2>(*it));
109 } else {
110 return std::any_cast<Service&>(std::get<2>(*it));
111 }
112 }
113
122 template <typename Service>
123 bool Contains(const std::string& name = "") {
124 return ListContains(CalcMapKey(typeid(Service), name));
125 }
126
132 std::vector<std::string> List();
133
134private:
135 std::string CalcMapKey(const std::type_info& tid, const std::string& name);
136
137 bool ListContains(const std::string& key);
138
139 // tuple elements: service id, flag that is true if svc held by pointer, service object;
140 std::list<std::tuple<std::string, bool, std::any>> m_services;
141};
142
143} // namespace rtctk::componentFramework
144
145#endif
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 & 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