From dbce7592a93cde18c49436dc0ebff213e137d2d7 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 1 Mar 2016 14:01:31 +0100 Subject: [PATCH] Sdk: Make World moveable Former-commit-id: 285cb9da4cbafd3da0c0859eb9d366bb2fb978a3 --- SDK/include/NDK/BaseSystem.hpp | 2 +- SDK/include/NDK/BaseSystem.inl | 6 +++--- SDK/include/NDK/Entity.hpp | 5 ++++- SDK/include/NDK/Entity.inl | 7 +++++++ SDK/include/NDK/World.hpp | 8 ++++---- SDK/include/NDK/World.inl | 25 ++++++++++++++++++++++++- SDK/src/NDK/Entity.cpp | 4 ++-- SDK/src/NDK/World.cpp | 4 ++-- 8 files changed, 47 insertions(+), 14 deletions(-) diff --git a/SDK/include/NDK/BaseSystem.hpp b/SDK/include/NDK/BaseSystem.hpp index 0cb66b3b4..9a81c83e0 100644 --- a/SDK/include/NDK/BaseSystem.hpp +++ b/SDK/include/NDK/BaseSystem.hpp @@ -71,7 +71,7 @@ namespace Ndk inline void RemoveEntity(Entity* entity); - inline void SetWorld(World& world); + inline void SetWorld(World* world) noexcept; inline void ValidateEntity(Entity* entity, bool justAdded); diff --git a/SDK/include/NDK/BaseSystem.inl b/SDK/include/NDK/BaseSystem.inl index da1f04d15..0c86ddfdd 100644 --- a/SDK/include/NDK/BaseSystem.inl +++ b/SDK/include/NDK/BaseSystem.inl @@ -172,9 +172,9 @@ namespace Ndk OnEntityValidation(entity, justAdded); } - inline void BaseSystem::SetWorld(World& world) + inline void BaseSystem::SetWorld(World* world) noexcept { - m_world = &world; + m_world = world; } inline bool BaseSystem::Initialize() @@ -186,6 +186,6 @@ namespace Ndk inline void BaseSystem::Uninitialize() { - // Rien à faire + // Nothing to do } } diff --git a/SDK/include/NDK/Entity.hpp b/SDK/include/NDK/Entity.hpp index c7ab78bf5..71f5a4fc7 100644 --- a/SDK/include/NDK/Entity.hpp +++ b/SDK/include/NDK/Entity.hpp @@ -60,13 +60,16 @@ namespace Ndk Entity& operator=(Entity&&) = delete; private: - Entity(World& world, EntityId id); + Entity(World* world, EntityId id); void Create(); void Destroy(); inline void RegisterHandle(EntityHandle* handle); inline void RegisterSystem(SystemIndex index); + + inline void SetWorld(World* world) noexcept; + inline void UnregisterHandle(EntityHandle* handle); inline void UnregisterSystem(SystemIndex index); diff --git a/SDK/include/NDK/Entity.inl b/SDK/include/NDK/Entity.inl index f185a4b0d..caca8b766 100644 --- a/SDK/include/NDK/Entity.inl +++ b/SDK/include/NDK/Entity.inl @@ -113,6 +113,13 @@ namespace Ndk m_systemBits.UnboundedSet(index); } + inline void Entity::SetWorld(World* world) noexcept + { + NazaraAssert(world, "An entity must be attached to a world at any time"); + + m_world = world; + } + inline void Entity::UnregisterHandle(EntityHandle* handle) { ///DOC: Un handle ne doit être libéré qu'une fois, et doit faire partie de la liste, sous peine de crash diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 3b7e16684..ff1de0ef7 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -27,8 +27,8 @@ namespace Ndk inline World(bool addDefaultSystems = true); World(const World&) = delete; - World(World&&) = delete; ///TODO - ~World(); + inline World(World&& world) noexcept; + ~World() noexcept; void AddDefaultSystems(); @@ -38,7 +38,7 @@ namespace Ndk const EntityHandle& CreateEntity(); inline EntityList CreateEntities(unsigned int count); - void Clear(); + void Clear() noexcept; const EntityHandle& GetEntity(EntityId id); inline const EntityList& GetEntities(); @@ -62,7 +62,7 @@ namespace Ndk inline void Update(float elapsedTime); World& operator=(const World&) = delete; - World& operator=(World&&) = delete; ///TODO + inline World& operator=(World&& world) noexcept; private: inline void Invalidate(); diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index a9e22774b..64212357a 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -13,6 +13,11 @@ namespace Ndk AddDefaultSystems(); } + inline World::World(World&& world) noexcept + { + operator=(std::move(world)); + } + inline BaseSystem& World::AddSystem(std::unique_ptr&& system) { NazaraAssert(system, "System must be valid"); @@ -25,7 +30,7 @@ namespace Ndk // Affectation et retour du système m_systems[index] = std::move(system); - m_systems[index]->SetWorld(*this); + m_systems[index]->SetWorld(this); Invalidate(); // On force une mise à jour de toutes les entités @@ -152,4 +157,22 @@ namespace Ndk { m_dirtyEntities.UnboundedSet(id, true); } + + inline World& World::operator=(World&& world) noexcept + { + m_aliveEntities = std::move(world.m_aliveEntities); + m_dirtyEntities = std::move(world.m_dirtyEntities); + m_freeIdList = std::move(world.m_freeIdList); + m_killedEntities = std::move(world.m_killedEntities); + + m_entities = std::move(world.m_entities); + for (EntityBlock& block : m_entities) + block.entity.SetWorld(this); + + m_systems = std::move(world.m_systems); + for (const auto& systemPtr : m_systems) + systemPtr->SetWorld(this); + + return *this; + } } diff --git a/SDK/src/NDK/Entity.cpp b/SDK/src/NDK/Entity.cpp index a2e69d2e1..afd3aa1b6 100644 --- a/SDK/src/NDK/Entity.cpp +++ b/SDK/src/NDK/Entity.cpp @@ -23,9 +23,9 @@ namespace Ndk handle->OnEntityMoved(this); } - Entity::Entity(World& world, EntityId id) : + Entity::Entity(World* world, EntityId id) : m_id(id), - m_world(&world) + m_world(world) { } diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index e8fff42f2..483af6d27 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -46,7 +46,7 @@ namespace Ndk id = m_entities.size(); // Impossible d'utiliser emplace_back à cause de la portée - m_entities.push_back(Entity(*this, id)); + m_entities.push_back(Entity(this, id)); } // On initialise l'entité et on l'ajoute à la liste des entités vivantes @@ -59,7 +59,7 @@ namespace Ndk return m_aliveEntities.back(); } - void World::Clear() + void World::Clear() noexcept { ///DOC: Tous les handles sont correctement invalidés