RTC Toolkit  1.0.0
serviceContainer.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
14 
16 
17 #include <any>
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 namespace rtctk::componentFramework {
25 
36 public:
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");
52  }
53  m_services.emplace(key, std::make_pair(std::forward<Service>(service), false));
54  }
55 
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");
71  }
72  m_services.emplace(key, std::make_pair(service, true));
73  }
74 
84  template <typename Service>
85  Service& Get(const std::string& name = "") {
86  std::string key = CalcMapKey(typeid(Service), name);
87  if (not MapContains(key)) {
88  CII_THROW(RtctkException, "service '" + key + "' not registered");
89  }
90  if (ServiceIsHeldByPointer(key)) {
91  return *std::any_cast<std::shared_ptr<Service>>(m_services[key].first);
92  } else {
93  return std::any_cast<Service&>(m_services[key].first);
94  }
95  }
96 
105  template <typename Service>
106  bool Contains(const std::string& name = "") {
107  return MapContains(CalcMapKey(typeid(Service), name));
108  }
109 
115  std::vector<std::string> List();
116 
117 private:
118  std::string CalcMapKey(const std::type_info& tid, const std::string& name);
119 
120  bool MapContains(const std::string& key);
121  bool ServiceIsHeldByPointer(const std::string& key);
122 
123  // string:key, any:service, bool:true if services is held by a pointer
124  std::map<std::string, std::pair<std::any, bool>> m_services;
125 };
126 
127 } // namespace rtctk::componentFramework
128 
129 #endif
exceptions.hpp
Provides macros and utilities for exception handling.
wscript.name
name
Definition: wscript:15
rtctk::componentFramework::ServiceContainer::Get
Service & Get(const std::string &name="")
Get a handle to a service.
Definition: serviceContainer.hpp:85
rtctk::componentFramework::ServiceContainer::Add
void Add(Service &&service, const std::string &name="")
Insert new service into the container.
Definition: serviceContainer.hpp:48
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::ServiceContainer::Add
void Add(std::shared_ptr< Service > service, const std::string &name="")
Insert new service into the container.
Definition: serviceContainer.hpp:67
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
rtctk::componentFramework::ServiceContainer::List
std::vector< std::string > List()
List currently available services.
Definition: serviceContainer.cpp:17
rtctk::componentFramework::ServiceContainer
Container class that holds services of any type.
Definition: serviceContainer.hpp:35
rtctk::componentFramework::ServiceContainer::Contains
bool Contains(const std::string &name="")
Check if the service container holds a certain service.
Definition: serviceContainer.hpp:106