Core/ApplicationBase: GetComponent can no longer fail

This commit is contained in:
SirLynix 2023-05-20 21:35:10 +02:00
parent 35c498bf21
commit 580b3c8804
2 changed files with 10 additions and 10 deletions

View File

@ -36,8 +36,8 @@ namespace Nz
inline void ClearComponents(); inline void ClearComponents();
template<typename T> T* GetComponent(); template<typename T> T& GetComponent();
template<typename T> const T* GetComponent() const; template<typename T> const T& GetComponent() const;
int Run(); int Run();

View File

@ -51,23 +51,23 @@ namespace Nz
} }
template<typename T> template<typename T>
T* ApplicationBase::GetComponent() T& ApplicationBase::GetComponent()
{ {
std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId(); std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId();
if (componentIndex >= m_components.size()) if (componentIndex >= m_components.size() || !m_components[componentIndex])
return nullptr; throw std::runtime_error("component not found");
return static_cast<T*>(m_components[componentIndex].get()); return static_cast<T&>(*m_components[componentIndex]);
} }
template<typename T> template<typename T>
const T* ApplicationBase::GetComponent() const const T& ApplicationBase::GetComponent() const
{ {
std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId(); std::size_t componentIndex = ApplicationComponentRegistry<T>::GetComponentId();
if (componentIndex >= m_components.size()) if (componentIndex >= m_components.size() || !m_components[componentIndex])
return nullptr; throw std::runtime_error("component not found");
return static_cast<const T*>(m_components[componentIndex].get()); return static_cast<const T&>(*m_components[componentIndex]);
} }
inline void ApplicationBase::Quit() inline void ApplicationBase::Quit()