From e99d34f1954cf706c1adbf47f8c342c42063f351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Mon, 9 Apr 2018 12:34:37 +0200 Subject: [PATCH 1/2] Sdk/World: Add const getters for systems --- ChangeLog.md | 1 + SDK/include/NDK/World.hpp | 2 ++ SDK/include/NDK/World.inl | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 2dc442e04..b9182f536 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -120,6 +120,7 @@ Nazara Development Kit: - CollisionComponent[2D|3D] and PhysicsComponent[2D|3D] now configures their internal RigidBody userdata to the entity ID they belong to (useful for callbacks). - Fixed EntityList copy/movement assignment operator which was not properly unregistering contained entities. - ListenerSystem now handles velocity in a generic way (no longer require a VelocityComponent and is compatible with physics) +- World now has const getters for systems # 0.4: diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 8a0fe2648..214f8e936 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -54,7 +54,9 @@ namespace Ndk inline const EntityList& GetEntities() const; inline const ProfilerData& GetProfilerData() const; inline BaseSystem& GetSystem(SystemIndex index); + inline const BaseSystem& GetSystem(SystemIndex index) const; template SystemType& GetSystem(); + template const SystemType& GetSystem() const; inline bool HasSystem(SystemIndex index) const; template bool HasSystem() const; diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index a9d2cc8b8..fb8549ea6 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -177,9 +177,8 @@ namespace Ndk * * \param index Index of the system * - * \remark Produces a NazaraAssert if system is not available in this world + * \remark The world must have the system before calling this function */ - inline BaseSystem& World::GetSystem(SystemIndex index) { NazaraAssert(HasSystem(index), "This system is not part of the world"); @@ -190,13 +189,30 @@ namespace Ndk return *system; } + /*! + * \brief Gets a system in the world by index + * \return A const reference to the system + * + * \param index Index of the system + * + * \remark The world must have the system before calling this function + */ + inline const BaseSystem& World::GetSystem(SystemIndex index) const + { + NazaraAssert(HasSystem(index), "This system is not part of the world"); + + const BaseSystem* system = m_systems[index].get(); + NazaraAssert(system, "Invalid system pointer"); + + return *system; + } + /*! * \brief Gets a system in the world by type * \return A reference to the system * * \remark Produces a NazaraAssert if system is not available in this world */ - template SystemType& World::GetSystem() { @@ -206,6 +222,21 @@ namespace Ndk return static_cast(GetSystem(index)); } + /*! + * \brief Gets a system in the world by type + * \return A const reference to the system + * + * \remark Produces a NazaraAssert if system is not available in this world + */ + template + const SystemType& World::GetSystem() const + { + static_assert(std::is_base_of::value, "SystemType is not a system"); + + SystemIndex index = GetSystemIndex(); + return static_cast(GetSystem(index)); + } + /*! * \brief Checks whether or not a system is present in the world by index * \return true If it is the case From e299e1f03ed446cf1a2484908e4311fedb1960a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Mon, 9 Apr 2018 12:37:58 +0200 Subject: [PATCH 2/2] Sdk/World: Add ForEachSystem method --- ChangeLog.md | 1 + SDK/include/NDK/World.hpp | 3 +++ SDK/include/NDK/World.inl | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index b9182f536..fc9d90771 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -121,6 +121,7 @@ Nazara Development Kit: - Fixed EntityList copy/movement assignment operator which was not properly unregistering contained entities. - ListenerSystem now handles velocity in a generic way (no longer require a VelocityComponent and is compatible with physics) - World now has const getters for systems +- Add World::ForEachSystem method, allowing iteration on every active system on a specific world # 0.4: diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 214f8e936..9b1d4c970 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -50,6 +50,9 @@ namespace Ndk inline void DisableProfiler(); inline void EnableProfiler(bool enable = true); + template void ForEachSystem(const F& iterationFunc); + template void ForEachSystem(const F& iterationFunc) const; + inline const EntityHandle& GetEntity(EntityId id); inline const EntityList& GetEntities() const; inline const ProfilerData& GetProfilerData() const; diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index fb8549ea6..efe8b0480 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -133,6 +133,40 @@ namespace Ndk } } + /*! + * \brief Executes a function on every present system + * + * Calls iterationFunc on every previously added system, in the same order as their indexes + * + * \param iterationFunc Function to be called + */ + template + void World::ForEachSystem(const F& iterationFunc) + { + for (const auto& systemPtr : m_systems) + { + if (systemPtr) + iterationFunc(*systemPtr); + } + } + + /*! + * \brief Executes a function on every present system + * + * Calls iterationFunc on every previously added system, in the same order as their indexes + * + * \param iterationFunc Function to be called + */ + template + void World::ForEachSystem(const F& iterationFunc) const + { + for (const auto& systemPtr : m_systems) + { + if (systemPtr) + iterationFunc(static_cast(*systemPtr)); //< Force const reference + } + } + /*! * \brief Gets an entity * \return A constant reference to a handle of the entity @@ -392,7 +426,7 @@ namespace Ndk m_orderedSystems = std::move(world.m_orderedSystems); m_orderedSystemsUpdated = world.m_orderedSystemsUpdated; m_profilerData = std::move(world.m_profilerData); - m_isProfilerEnabled = world.m_isProfilerEnabled; + m_isProfilerEnabled = m_isProfilerEnabled; m_entities = std::move(world.m_entities); for (EntityBlock& block : m_entities)