9 #ifndef RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
10 #define RTCTK_COMPONENTFRAMEWORK_SERVICECONTAINER_HPP
44 template <
typename Service>
45 void Add(Service&& service,
const std::string& name =
"") {
46 std::string key = CalcMapKey(
typeid(Service), name);
47 if(MapContains(key)) {
48 throw std::runtime_error(
"service '" + key +
"' already registered");
50 m_services.emplace(key, std::make_pair(std::forward<Service>(service),
false));
63 template <
typename Service>
64 void Add(std::shared_ptr<Service> service,
const std::string& name =
"") {
65 std::string key = CalcMapKey(
typeid(Service), name);
66 if(MapContains(key)) {
67 throw std::runtime_error(
"service '" + key +
"' already registered");
69 m_services.emplace(key, std::make_pair(service,
true));
81 template <
typename Service>
82 Service&
Get(
const std::string& name =
"") {
83 std::string key = CalcMapKey(
typeid(Service), name);
84 if(not MapContains(key)) {
85 throw std::runtime_error(
"service '" + key +
"' not registered");
87 if(ServiceIsHeldByPointer(key)) {
88 return *std::any_cast<std::shared_ptr<Service>>(m_services[key].first);
90 return std::any_cast<Service&>(m_services[key].first);
102 template <
typename Service>
104 return MapContains(CalcMapKey(
typeid(Service), name));
112 std::vector<std::string>
List();
116 std::string CalcMapKey(
const std::type_info& tid,
const std::string& name);
118 bool MapContains(
const std::string& key);
119 bool ServiceIsHeldByPointer(
const std::string& key);
122 std::map<std::string, std::pair<std::any, bool>> m_services;