From 580b3c8804e999ff76c0243bb3eee82289da12ab Mon Sep 17 00:00:00 2001 From: SirLynix Date: Sat, 20 May 2023 21:35:10 +0200 Subject: [PATCH] Core/ApplicationBase: GetComponent can no longer fail --- include/Nazara/Core/ApplicationBase.hpp | 4 ++-- include/Nazara/Core/ApplicationBase.inl | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/Nazara/Core/ApplicationBase.hpp b/include/Nazara/Core/ApplicationBase.hpp index 90dcf4943..930bd8461 100644 --- a/include/Nazara/Core/ApplicationBase.hpp +++ b/include/Nazara/Core/ApplicationBase.hpp @@ -36,8 +36,8 @@ namespace Nz inline void ClearComponents(); - template T* GetComponent(); - template const T* GetComponent() const; + template T& GetComponent(); + template const T& GetComponent() const; int Run(); diff --git a/include/Nazara/Core/ApplicationBase.inl b/include/Nazara/Core/ApplicationBase.inl index d47b3d75f..6128977c8 100644 --- a/include/Nazara/Core/ApplicationBase.inl +++ b/include/Nazara/Core/ApplicationBase.inl @@ -51,23 +51,23 @@ namespace Nz } template - T* ApplicationBase::GetComponent() + T& ApplicationBase::GetComponent() { std::size_t componentIndex = ApplicationComponentRegistry::GetComponentId(); - if (componentIndex >= m_components.size()) - return nullptr; + if (componentIndex >= m_components.size() || !m_components[componentIndex]) + throw std::runtime_error("component not found"); - return static_cast(m_components[componentIndex].get()); + return static_cast(*m_components[componentIndex]); } template - const T* ApplicationBase::GetComponent() const + const T& ApplicationBase::GetComponent() const { std::size_t componentIndex = ApplicationComponentRegistry::GetComponentId(); - if (componentIndex >= m_components.size()) - return nullptr; + if (componentIndex >= m_components.size() || !m_components[componentIndex]) + throw std::runtime_error("component not found"); - return static_cast(m_components[componentIndex].get()); + return static_cast(*m_components[componentIndex]); } inline void ApplicationBase::Quit()