From 23ea1989ef29823bdef5a7f355f90e5d322dd2bb Mon Sep 17 00:00:00 2001 From: SirLynix Date: Fri, 23 Feb 2024 22:40:13 +0100 Subject: [PATCH] Core/ApplicationBase: Replace component indices with hashes Fixes usage of components across DLL --- include/Nazara/Core.hpp | 1 - include/Nazara/Core/ApplicationBase.hpp | 4 +- include/Nazara/Core/ApplicationBase.inl | 49 +++++++++++-------- .../Core/ApplicationComponentRegistry.hpp | 24 --------- .../Core/ApplicationComponentRegistry.inl | 24 --------- src/Nazara/Core/ApplicationBase.cpp | 4 +- 6 files changed, 33 insertions(+), 73 deletions(-) delete mode 100644 include/Nazara/Core/ApplicationComponentRegistry.hpp delete mode 100644 include/Nazara/Core/ApplicationComponentRegistry.inl diff --git a/include/Nazara/Core.hpp b/include/Nazara/Core.hpp index b58893686..13bc9ef22 100644 --- a/include/Nazara/Core.hpp +++ b/include/Nazara/Core.hpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include diff --git a/include/Nazara/Core/ApplicationBase.hpp b/include/Nazara/Core/ApplicationBase.hpp index b8596ba10..31719e668 100644 --- a/include/Nazara/Core/ApplicationBase.hpp +++ b/include/Nazara/Core/ApplicationBase.hpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include namespace Nz { @@ -72,7 +72,7 @@ namespace Nz }; std::atomic_bool m_running; - std::vector> m_components; + std::unordered_map> m_components; std::vector m_updaters; CommandLineParameters m_commandLineParams; HighPrecisionClock m_clock; diff --git a/include/Nazara/Core/ApplicationBase.inl b/include/Nazara/Core/ApplicationBase.inl index 41651b99f..b868e245b 100644 --- a/include/Nazara/Core/ApplicationBase.inl +++ b/include/Nazara/Core/ApplicationBase.inl @@ -2,7 +2,6 @@ // This file is part of the "Nazara Engine - Core module" // For conditions of distribution and use, see copyright notice in Export.hpp -#include #include namespace Nz @@ -57,31 +56,37 @@ namespace Nz template T& ApplicationBase::GetComponent() { - std::size_t componentIndex = ApplicationComponentRegistry::GetComponentId(); - if (componentIndex >= m_components.size() || !m_components[componentIndex]) + constexpr UInt64 typeHash = FNV1a64(TypeName()); + + auto it = m_components.find(typeHash); + if (it != m_components.end()) throw std::runtime_error("component not found"); - return static_cast(*m_components[componentIndex]); + return static_cast(*it->second); } template const T& ApplicationBase::GetComponent() const { - std::size_t componentIndex = ApplicationComponentRegistry::GetComponentId(); - if (componentIndex >= m_components.size() || !m_components[componentIndex]) + constexpr UInt64 typeHash = FNV1a64(TypeName()); + + auto it = m_components.find(typeHash); + if (it != m_components.end()) throw std::runtime_error("component not found"); - return static_cast(*m_components[componentIndex]); + return static_cast(*it->second); } template bool ApplicationBase::HasComponent() const { - std::size_t componentIndex = ApplicationComponentRegistry::GetComponentId(); - if (componentIndex >= m_components.size()) + constexpr UInt64 typeHash = FNV1a64(TypeName()); + + auto it = m_components.find(typeHash); + if (it != m_components.end()) return false; - return m_components[componentIndex] != nullptr; + return it->second != nullptr; } inline void ApplicationBase::Quit() @@ -92,21 +97,25 @@ namespace Nz template T* ApplicationBase::TryGetComponent() { - std::size_t componentIndex = ApplicationComponentRegistry::GetComponentId(); - if (componentIndex >= m_components.size()) + constexpr UInt64 typeHash = FNV1a64(TypeName()); + + auto it = m_components.find(typeHash); + if (it != m_components.end()) return nullptr; - return static_cast(m_components[componentIndex].get()); + return static_cast(it->second.get()); } template const T* ApplicationBase::TryGetComponent() const { - std::size_t componentIndex = ApplicationComponentRegistry::GetComponentId(); - if (componentIndex >= m_components.size()) + constexpr UInt64 typeHash = FNV1a64(TypeName()); + + auto it = m_components.find(typeHash); + if (it != m_components.end()) return nullptr; - return static_cast(m_components[componentIndex].get()); + return static_cast(it->second.get()); } inline ApplicationBase* ApplicationBase::Instance() @@ -117,17 +126,15 @@ namespace Nz template T& ApplicationBase::AddComponent(Args&&... args) { - std::size_t componentIndex = ApplicationComponentRegistry::GetComponentId(); + constexpr UInt64 typeHash = FNV1a64(TypeName()); std::unique_ptr component = std::make_unique(*this, std::forward(args)...); T& componentRef = *component; - if (componentIndex >= m_components.size()) - m_components.resize(componentIndex + 1); - else if (m_components[componentIndex] != nullptr) + if (m_components.contains(typeHash)) throw std::runtime_error("component was added multiple times"); - m_components[componentIndex] = std::move(component); + m_components[typeHash] = std::move(component); return componentRef; } diff --git a/include/Nazara/Core/ApplicationComponentRegistry.hpp b/include/Nazara/Core/ApplicationComponentRegistry.hpp deleted file mode 100644 index 7403f130e..000000000 --- a/include/Nazara/Core/ApplicationComponentRegistry.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com) -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Export.hpp - -#pragma once - -#ifndef NAZARA_CORE_APPLICATIONCOMPONENTREGISTRY_HPP -#define NAZARA_CORE_APPLICATIONCOMPONENTREGISTRY_HPP - -#include -#include - -namespace Nz -{ - template - struct ApplicationComponentRegistry - { - static std::size_t GetComponentId(); - }; -} - -#include - -#endif // NAZARA_CORE_APPLICATIONCOMPONENTREGISTRY_HPP diff --git a/include/Nazara/Core/ApplicationComponentRegistry.inl b/include/Nazara/Core/ApplicationComponentRegistry.inl deleted file mode 100644 index 9ae78279d..000000000 --- a/include/Nazara/Core/ApplicationComponentRegistry.inl +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com) -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Export.hpp - - -namespace Nz -{ - namespace Detail - { - inline std::size_t ComponentCounter() - { - static std::size_t counter = 0; - return counter++; - } - } - - template - std::size_t ApplicationComponentRegistry::GetComponentId() - { - static std::size_t typeId = Detail::ComponentCounter(); - return typeId; - } -} - diff --git a/src/Nazara/Core/ApplicationBase.cpp b/src/Nazara/Core/ApplicationBase.cpp index d93b57178..338b2ce57 100644 --- a/src/Nazara/Core/ApplicationBase.cpp +++ b/src/Nazara/Core/ApplicationBase.cpp @@ -90,8 +90,10 @@ namespace Nz updaterEntry.nextUpdate = std::max(updaterEntry.nextUpdate, m_currentTime); } - for (auto& componentPtr : m_components) + for (auto&& [typeHash, componentPtr] : m_components) { + NazaraUnused(typeHash); + if (componentPtr) componentPtr->Update(elapsedTime); }