11 #include "threads/CriticalSection.h" 18 #include <unordered_map> 25 template<
class BaseType>
33 return std::const_pointer_cast<T>(std::as_const(*this).template GetComponent<T>());
40 std::unique_lock<CCriticalSection> lock(m_critSection);
41 const auto it = m_components.find(std::type_index(
typeid(T)));
42 if (it != m_components.end())
43 return std::static_pointer_cast<const T>((*it).second);
45 throw std::logic_error(
"ComponentContainer: Attempt to obtain non-existent component");
49 std::size_t
size()
const {
return m_components.size(); }
61 const auto& componentRef = *component;
63 std::unique_lock<CCriticalSection> lock(m_critSection);
64 m_components.insert({std::type_index(
typeid(componentRef)), component});
70 std::unique_lock<CCriticalSection> lock(m_critSection);
71 m_components.erase(typeInfo);
75 mutable CCriticalSection m_critSection;
76 std::unordered_map<std::type_index, std::shared_ptr<BaseType>>
std::size_t size() const
Returns number of registered components.
Definition: ComponentContainer.h:49
std::shared_ptr< T > GetComponent()
Obtain a component.
Definition: ComponentContainer.h:31
std::shared_ptr< const T > GetComponent() const
Obtain a component.
Definition: ComponentContainer.h:38
A generic container for components.
Definition: ServiceBroker.h:55
void DeregisterComponent(const std::type_info &typeInfo)
Deregister a component.
Definition: ComponentContainer.h:68
void RegisterComponent(const std::shared_ptr< BaseType > &component)
Register a new component instance.
Definition: ComponentContainer.h:53