From 57f71aa502e0d47ccc1754fff5d015d9fc556ed0 Mon Sep 17 00:00:00 2001 From: Ubpa Date: Tue, 10 Nov 2020 13:13:35 +0800 Subject: [PATCH] clean up --- CMakeLists.txt | 4 +- cmake/InitUCMake.cmake | 2 +- include/UECS/ArchetypeFilter.h | 2 +- include/UECS/ChunkView.h | 6 +-- include/UECS/CmptLocator.h | 2 +- include/UECS/CmptPtr.h | 14 +++---- include/UECS/CmptTag.h | 12 +++--- include/UECS/EntityMngr.h | 7 +++- include/UECS/EntityQuery.h | 2 +- include/UECS/SingletonLocator.h | 2 - include/UECS/SingletonsView.h | 2 +- include/UECS/SystemMngr.h | 4 +- include/UECS/detail/Archetype.h | 2 +- include/UECS/detail/Archetype.inl | 9 ++-- include/UECS/detail/ArchetypeCmptTraits.h | 2 +- include/UECS/detail/ArchetypeCmptTraits.inl | 42 +++++++++---------- include/UECS/detail/CmptTag.inl | 2 +- include/UECS/detail/CmptTypeSet.h | 6 +-- include/UECS/detail/CmptTypeSet.inl | 4 +- include/UECS/detail/EntityMngr.inl | 11 +++++ include/UECS/detail/RTDCmptTraits.inl | 8 ++-- include/UECS/detail/SystemFunc.h | 11 +++-- include/UECS/detail/SystemFunc.inl | 20 ++++----- include/UECS/detail/SystemMngr.inl | 2 +- include/UECS/detail/Util.h | 8 ++-- include/UECS/detail/Util.inl | 4 +- src/core/Archetype.cpp | 18 ++++---- src/core/ArchetypeFilter.cpp | 2 +- src/core/CmptLocator.cpp | 2 +- src/core/EntityMngr.cpp | 46 ++++++++++----------- src/core/Schedule.cpp | 4 +- src/core/SingletonLocator.cpp | 2 - src/core/SysFuncGraph.cpp | 14 +++---- src/core/SystemFunc.cpp | 6 +-- src/core/SystemMngr.cpp | 6 +-- 35 files changed, 149 insertions(+), 141 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7dcc94d..4162a9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -project(UECS VERSION 0.13.2) +project(UECS VERSION 0.13.3) message(STATUS "[Project] ${PROJECT_NAME}") include(cmake/InitUCMake.cmake) @@ -9,7 +9,7 @@ Ubpa_InitUCMake() Ubpa_InitProject() Ubpa_AddDep(UContainer 0.0.7) -Ubpa_AddDep(UTemplate 0.4.8) +Ubpa_AddDep(UTemplate 0.4.9) Ubpa_AddDep(UGraphviz 0.1.5) Ubpa_AddSubDirsRec(include) diff --git a/cmake/InitUCMake.cmake b/cmake/InitUCMake.cmake index fab41e3..7cdb7da 100644 --- a/cmake/InitUCMake.cmake +++ b/cmake/InitUCMake.cmake @@ -1,6 +1,6 @@ macro(Ubpa_InitUCMake) include(FetchContent) - set(UCMake_VERSION 0.6.2) + set(UCMake_VERSION 0.6.3) message(STATUS "find package: UCMake ${UCMake_VERSION}") find_package(UCMake ${UCMake_VERSION} EXACT QUIET) if(NOT UCMake_FOUND) diff --git a/include/UECS/ArchetypeFilter.h b/include/UECS/ArchetypeFilter.h index 2a6181d..dea9d53 100644 --- a/include/UECS/ArchetypeFilter.h +++ b/include/UECS/ArchetypeFilter.h @@ -15,6 +15,6 @@ namespace Ubpa::UECS { bool HaveWriteCmptType() const noexcept; - bool operator==(const ArchetypeFilter& rhs) const; + bool operator==(const ArchetypeFilter& rhs) const noexcept; }; } diff --git a/include/UECS/ChunkView.h b/include/UECS/ChunkView.h index c29e3d5..f82a68e 100644 --- a/include/UECS/ChunkView.h +++ b/include/UECS/ChunkView.h @@ -7,7 +7,7 @@ namespace Ubpa::UECS { class ChunkView { public: - ChunkView(Archetype* archetype, size_t chunkIdx) + ChunkView(Archetype* archetype, size_t chunkIdx) noexcept : archetype{ archetype }, chunkIdx{ chunkIdx } {} bool Contains(CmptType) const; @@ -15,8 +15,8 @@ namespace Ubpa::UECS { // nullptr if not contain void* GetCmptArray(CmptType) const; template - Cmpt* GetCmptArray() const { return reinterpret_cast(GetCmptArray(CmptType::Of)); } - const Entity* GetEntityArray() const { return GetCmptArray(); } + Cmpt* GetCmptArray() const { return static_cast(GetCmptArray(CmptType::Of)); } + const Entity* GetEntityArray() const noexcept { return GetCmptArray(); } size_t EntityNum() const noexcept; private: diff --git a/include/UECS/CmptLocator.h b/include/UECS/CmptLocator.h index 1a53457..2be7301 100644 --- a/include/UECS/CmptLocator.h +++ b/include/UECS/CmptLocator.h @@ -25,7 +25,7 @@ namespace Ubpa::UECS { const CmptAccessTypeSet& CmptAccessTypes() const noexcept { return cmptTypes; } - bool operator==(const CmptLocator& rhs) const; + bool operator==(const CmptLocator& rhs) const noexcept; bool HasWriteCmptType() const noexcept; private: diff --git a/include/UECS/CmptPtr.h b/include/UECS/CmptPtr.h index 13c7068..7c66eb7 100644 --- a/include/UECS/CmptPtr.h +++ b/include/UECS/CmptPtr.h @@ -17,7 +17,7 @@ namespace Ubpa::UECS { constexpr CmptType Type() const noexcept { return type; } - static constexpr CmptPtr Invalid() { return { CmptType::Invalid(), nullptr }; }; + static constexpr CmptPtr Invalid() noexcept { return { CmptType::Invalid(), nullptr }; }; constexpr bool Valid() const noexcept { return p != nullptr && type.Valid(); } template @@ -44,7 +44,7 @@ namespace Ubpa::UECS { constexpr CmptAccessType AccessType() const noexcept { return accessType; } - static constexpr CmptAccessPtr Invalid() { return { CmptAccessType::Invalid(), nullptr }; }; + static constexpr CmptAccessPtr Invalid() noexcept { return { CmptAccessType::Invalid(), nullptr }; }; constexpr bool Valid() const noexcept { return p != nullptr && accessType.Valid(); } // check: type's access mode must be equal to @@ -54,15 +54,15 @@ namespace Ubpa::UECS { if constexpr (mode == AccessMode::LAST_FRAME) return LastFrame{p}; else if constexpr (mode == AccessMode::WRITE) - return Write(p); + return Write{p}; else if constexpr (mode == AccessMode::LATEST) - return Latest(p); + return Latest{p}; else if constexpr (mode == AccessMode::LAST_FRAME_SINGLETON) - return LastFrame>; + return LastFrame>{p}; else if constexpr (mode == AccessMode::WRITE_SINGLETON) - return Write>; + return Write>{p}; else if constexpr (mode == AccessMode::LATEST_SINGLETON) - return Latest>; + return Latest>{p}; else static_assert(false); } diff --git a/include/UECS/CmptTag.h b/include/UECS/CmptTag.h index 47e83db..6490e70 100644 --- a/include/UECS/CmptTag.h +++ b/include/UECS/CmptTag.h @@ -12,12 +12,12 @@ namespace Ubpa::UECS { // 6. Latest> enum class AccessMode : size_t { - LAST_FRAME = 0, // 000, LastFrame - WRITE = 1, // 001, Write / Cmpt* - LATEST = 2, // 010, Latest / const Cmpt* - LAST_FRAME_SINGLETON = 4, // 100, LastFrame> - WRITE_SINGLETON = 5, // 101, Write> / Singleton - LATEST_SINGLETON = 6, // 110, Latest> + LAST_FRAME = 0b000, // LastFrame + WRITE = 0b001, // Write / Cmpt* + LATEST = 0b010, // Latest / const Cmpt* + LAST_FRAME_SINGLETON = 0b100, // LastFrame> + WRITE_SINGLETON = 0b101, // Write> / Singleton + LATEST_SINGLETON = 0b110, // Latest> }; constexpr bool AccessMode_IsSingleton(AccessMode mode) noexcept { diff --git a/include/UECS/EntityMngr.h b/include/UECS/EntityMngr.h index 2eee2ef..bfe1d44 100644 --- a/include/UECS/EntityMngr.h +++ b/include/UECS/EntityMngr.h @@ -46,12 +46,17 @@ namespace Ubpa::UECS { // use RTDCmptTraits void Attach(Entity, const CmptType* types, size_t num); - // assert(Have(e, CmptType::Of)) template Cmpt* Emplace(Entity, Args&&...); + // use Detach(Entity, const CmptType*, size_t) + template + void Detach(Entity); void Detach(Entity, const CmptType* types, size_t num); + // use Have(Entity, CmptType) + template + bool Have(Entity) const; bool Have(Entity, CmptType) const; // nullptr if not containts diff --git a/include/UECS/EntityQuery.h b/include/UECS/EntityQuery.h index 5659d4e..81bf25d 100644 --- a/include/UECS/EntityQuery.h +++ b/include/UECS/EntityQuery.h @@ -16,7 +16,7 @@ namespace Ubpa::UECS { size_t HashCode() const noexcept { return hash_combine(filter.HashCode(), locator.HashCode()); } - bool operator==(const EntityQuery& query) const { + bool operator==(const EntityQuery& query) const noexcept { return filter == query.filter && locator == query.locator; } }; diff --git a/include/UECS/SingletonLocator.h b/include/UECS/SingletonLocator.h index 5c665b4..565d86a 100644 --- a/include/UECS/SingletonLocator.h +++ b/include/UECS/SingletonLocator.h @@ -1,7 +1,5 @@ #pragma once -#include "CmptTag.h" - #include "CmptType.h" #include diff --git a/include/UECS/SingletonsView.h b/include/UECS/SingletonsView.h index 470b106..621c563 100644 --- a/include/UECS/SingletonsView.h +++ b/include/UECS/SingletonsView.h @@ -5,7 +5,7 @@ namespace Ubpa::UECS { class SingletonsView { public: - SingletonsView(const CmptAccessPtr* singletons, size_t num) + SingletonsView(const CmptAccessPtr* singletons, size_t num) noexcept : singletons{ singletons }, num{ num } {} CmptAccessPtr GetSingleton(CmptAccessType) const; diff --git a/include/UECS/SystemMngr.h b/include/UECS/SystemMngr.h index 9a92e05..f8e194e 100644 --- a/include/UECS/SystemMngr.h +++ b/include/UECS/SystemMngr.h @@ -29,7 +29,7 @@ namespace Ubpa::UECS { template size_t GetIndex() const; - void Clear(); + void Clear() noexcept; size_t Register(std::string name, Func); // name: nameof::nameof_type @@ -43,7 +43,7 @@ namespace Ubpa::UECS { void Unregister(); void Activate(size_t index); - void Deactivate(size_t index); + void Deactivate(size_t index) noexcept; private: std::vector systems; std::vector frees; diff --git a/include/UECS/detail/Archetype.h b/include/UECS/detail/Archetype.h index 2e289dc..d97574f 100644 --- a/include/UECS/detail/Archetype.h +++ b/include/UECS/detail/Archetype.h @@ -22,7 +22,7 @@ namespace Ubpa::UECS { // type of Entity + Components is Archetype's type class Archetype { public: - Archetype(Pool* chunkPool) : chunkPool{ chunkPool } {} + Archetype(Pool* chunkPool) noexcept : chunkPool{ chunkPool } {} // argument TypeList is for type deduction // auto add Entity diff --git a/include/UECS/detail/Archetype.inl b/include/UECS/detail/Archetype.inl index 59df937..e8157d7 100644 --- a/include/UECS/detail/Archetype.inl +++ b/include/UECS/detail/Archetype.inl @@ -5,8 +5,9 @@ namespace Ubpa::UECS { template Archetype::Archetype(Pool* chunkPool, TypeList) - : chunkPool{ chunkPool }, - types(GenCmptTypeSet()) + : + types(GenCmptTypeSet()), + chunkPool{ chunkPool } { static_assert(IsSet_v>, "... must be different"); @@ -61,9 +62,9 @@ namespace Ubpa::UECS { "... must be different"); constexpr std::array types = { CmptType::Of... }; - return Archetype::GenCmptTypeSet(types.data(), types.size()); + return GenCmptTypeSet(types.data(), types.size()); } else - return Archetype::GenCmptTypeSet(nullptr, 0); + return GenCmptTypeSet(nullptr, 0); } } diff --git a/include/UECS/detail/ArchetypeCmptTraits.h b/include/UECS/detail/ArchetypeCmptTraits.h index be1fb4b..9efcd6c 100644 --- a/include/UECS/detail/ArchetypeCmptTraits.h +++ b/include/UECS/detail/ArchetypeCmptTraits.h @@ -23,7 +23,7 @@ namespace Ubpa::UECS { void Register(const RTDCmptTraits&, CmptType); template - void Deregister(); + void Deregister() noexcept; void Deregister(CmptType) noexcept; private: diff --git a/include/UECS/detail/ArchetypeCmptTraits.inl b/include/UECS/detail/ArchetypeCmptTraits.inl index fd3295f..acffad9 100644 --- a/include/UECS/detail/ArchetypeCmptTraits.inl +++ b/include/UECS/detail/ArchetypeCmptTraits.inl @@ -62,43 +62,26 @@ namespace Ubpa::UECS { if constexpr (!std::is_trivially_destructible_v) { destructors.emplace(type, [](void* cmpt) { - reinterpret_cast(cmpt)->~Cmpt(); + static_cast(cmpt)->~Cmpt(); }); } if constexpr (!std::is_trivially_move_constructible_v) { move_constructors.emplace(type, [](void* dst, void* src) { - new(dst)Cmpt(std::move(*reinterpret_cast(src))); + new(dst)Cmpt(std::move(*static_cast(src))); }); } if constexpr (!std::is_trivially_copy_assignable_v) { move_assignments.emplace(type, [](void* dst, void* src) { - *reinterpret_cast(dst) = std::move(*reinterpret_cast(src)); + *static_cast(dst) = std::move(*static_cast(src)); }); } if constexpr (!std::is_trivially_copy_constructible_v) { copy_constructors.emplace(type, [](void* dst, void* src) { - new(dst)Cmpt(*reinterpret_cast(src)); + new(dst)Cmpt(*static_cast(src)); }); } } - template - void ArchetypeCmptTraits::Deregister() { - constexpr CmptType type = CmptType::Of; - - sizeofs.erase(type); - alignments.erase(type); - - if constexpr (!std::is_trivially_destructible_v) - destructors.erase(type); - if constexpr (!std::is_trivially_copy_constructible_v) - copy_constructors.erase(type); - if constexpr (!std::is_trivially_move_constructible_v) - move_constructors.erase(type); - if constexpr (!std::is_trivially_move_assignable_v) - move_assignments.erase(type); - } - inline void ArchetypeCmptTraits::Register(const RTDCmptTraits& rtdct, CmptType type) { auto size_target = rtdct.GetSizeofs().find(type); if (size_target == rtdct.GetSizeofs().end()) @@ -124,6 +107,23 @@ namespace Ubpa::UECS { move_assignments.emplace(type, move_assignments_target->second); } + template + void ArchetypeCmptTraits::Deregister() noexcept { + constexpr CmptType type = CmptType::Of; + + sizeofs.erase(type); + alignments.erase(type); + + if constexpr (!std::is_trivially_destructible_v) + destructors.erase(type); + if constexpr (!std::is_trivially_copy_constructible_v) + copy_constructors.erase(type); + if constexpr (!std::is_trivially_move_constructible_v) + move_constructors.erase(type); + if constexpr (!std::is_trivially_move_assignable_v) + move_assignments.erase(type); + } + inline void ArchetypeCmptTraits::Deregister(CmptType type) noexcept { sizeofs.erase(type); alignments.erase(type); diff --git a/include/UECS/detail/CmptTag.inl b/include/UECS/detail/CmptTag.inl index dd144bb..b6e7122 100644 --- a/include/UECS/detail/CmptTag.inl +++ b/include/UECS/detail/CmptTag.inl @@ -10,7 +10,7 @@ namespace Ubpa::UECS { namespace Ubpa::UECS { template - void* CastToVoidPointer(TaggedCmpt p) { + void* CastToVoidPointer(TaggedCmpt p) noexcept { return const_cast(reinterpret_cast(p)); } diff --git a/include/UECS/detail/CmptTypeSet.h b/include/UECS/detail/CmptTypeSet.h index 20825a7..ed7ae76 100644 --- a/include/UECS/detail/CmptTypeSet.h +++ b/include/UECS/detail/CmptTypeSet.h @@ -12,10 +12,10 @@ namespace Ubpa::UECS { struct CmptTypeSet { std::set data; - size_t HashCode() const; + size_t HashCode() const noexcept; void Insert(const CmptType* types, size_t num); - void Erase(const CmptType* types, size_t num); + void Erase(const CmptType* types, size_t num) noexcept; bool Contains(CmptType type) const; bool ContainsAll(const CmptType* types, size_t num) const; @@ -35,7 +35,7 @@ namespace Ubpa::UECS { bool IsMatch(const EntityQuery& query) const; - bool operator==(const CmptTypeSet& rhs) const { return data == rhs.data; } + bool operator==(const CmptTypeSet& rhs) const noexcept { return data == rhs.data; } }; } diff --git a/include/UECS/detail/CmptTypeSet.inl b/include/UECS/detail/CmptTypeSet.inl index 80c518c..5e8f91b 100644 --- a/include/UECS/detail/CmptTypeSet.inl +++ b/include/UECS/detail/CmptTypeSet.inl @@ -7,7 +7,7 @@ namespace Ubpa::UECS { data.insert(types[i]); } - inline void CmptTypeSet::Erase(const CmptType* types, size_t num) { + inline void CmptTypeSet::Erase(const CmptType* types, size_t num) noexcept { assert(types || num == 0); for (size_t i = 0; i < num; i++) data.erase(types[i]); @@ -84,7 +84,7 @@ namespace Ubpa::UECS { return IsMatch(query.filter) && IsMatch(query.locator); } - inline size_t CmptTypeSet::HashCode() const { + inline size_t CmptTypeSet::HashCode() const noexcept { size_t seed = TypeID; for (const auto& t : data) seed = hash_combine(seed, t.HashCode()); diff --git a/include/UECS/detail/EntityMngr.inl b/include/UECS/detail/EntityMngr.inl index c65b542..f433da1 100644 --- a/include/UECS/detail/EntityMngr.inl +++ b/include/UECS/detail/EntityMngr.inl @@ -99,6 +99,17 @@ namespace Ubpa::UECS { return cmpts; } + template + void EntityMngr::Detach(Entity e) { + constexpr std::array types{ CmptType::Of... }; + Detach(e, types.data, types.size()); + } + + template + bool EntityMngr::Have(Entity e) const { + return Have(e, CmptType::Of); + } + template Cmpt* EntityMngr::Emplace(Entity e, Args&&... args) { static_assert(std::is_constructible_v diff --git a/include/UECS/detail/RTDCmptTraits.inl b/include/UECS/detail/RTDCmptTraits.inl index cbf200d..f3ad57c 100644 --- a/include/UECS/detail/RTDCmptTraits.inl +++ b/include/UECS/detail/RTDCmptTraits.inl @@ -130,22 +130,22 @@ namespace Ubpa::UECS { } if constexpr (!std::is_trivially_destructible_v) { destructors.emplace(type, [](void* cmpt) { - reinterpret_cast(cmpt)->~Cmpt(); + static_cast(cmpt)->~Cmpt(); }); } if constexpr (!std::is_trivially_move_constructible_v) { move_constructors.emplace(type, [](void* dst, void* src) { - new(dst)Cmpt(std::move(*reinterpret_cast(src))); + new(dst)Cmpt(std::move(*static_cast(src))); }); } if constexpr (!std::is_trivially_move_assignable_v) { move_assignments.emplace(type, [](void* dst, void* src) { - *reinterpret_cast(dst) = std::move(*reinterpret_cast(src)); + *static_cast(dst) = std::move(*static_cast(src)); }); } if constexpr (!std::is_trivially_copy_constructible_v) { copy_constructors.emplace(type, [](void* dst, void* src) { - new(dst)Cmpt(*reinterpret_cast(src)); + new(dst)Cmpt(*static_cast(src)); }); } } diff --git a/include/UECS/detail/SystemFunc.h b/include/UECS/detail/SystemFunc.h index 9e87ec9..d3e7372 100644 --- a/include/UECS/detail/SystemFunc.h +++ b/include/UECS/detail/SystemFunc.h @@ -56,21 +56,20 @@ namespace Ubpa::UECS { size_t HashCode() const noexcept { return hashCode; } - void operator()(World*, SingletonsView, Entity, size_t entityIndexInQuery, CmptsView); - void operator()(World*, SingletonsView, size_t entityBeginIndexInQuery, ChunkView); - void operator()(World*, SingletonsView); + void operator()(World*, SingletonsView, Entity, size_t entityIndexInQuery, CmptsView) const; + void operator()(World*, SingletonsView, size_t entityBeginIndexInQuery, ChunkView) const; + void operator()(World*, SingletonsView) const; Mode GetMode() const noexcept { return mode; } bool IsParallel() const noexcept { return isParallel; } bool operator==(const SystemFunc& sysFunc) const noexcept { return name == sysFunc.name; } private: - std::function func; - - std::string name; Mode mode; + std::string name; size_t hashCode; // after name bool isParallel; + std::function func; }; } diff --git a/include/UECS/detail/SystemFunc.inl b/include/UECS/detail/SystemFunc.inl index f12eb58..d6c9079 100644 --- a/include/UECS/detail/SystemFunc.inl +++ b/include/UECS/detail/SystemFunc.inl @@ -1,7 +1,5 @@ #pragma once -#include <_deps/nameof.hpp> - #include namespace Ubpa::UECS::detail { @@ -20,13 +18,13 @@ namespace Ubpa::UECS { SingletonLocator singletonLocator, bool isParallel ) : - mode{ Mode::Entity }, - func{ detail::Pack(std::forward(func)) }, entityQuery{ std::move(archetypeFilter), std::move(cmptLocator.Combine()) }, singletonLocator{ std::move(singletonLocator.Combine()) }, + mode{ Mode::Entity }, name{ std::move(name) }, hashCode{ HashCode(this->name) }, - isParallel{ isParallel } + isParallel{ isParallel }, + func{ detail::Pack(std::forward(func)) } { using ArgList = FuncTraits_ArgList>; @@ -48,13 +46,13 @@ namespace Ubpa::UECS { SingletonLocator singletonLocator, bool isParallel ) : - mode{ Mode::Chunk }, - func{ detail::Pack(std::forward(func)) }, entityQuery{ std::move(archetypeFilter) }, singletonLocator{ std::move(singletonLocator.Combine()) }, + mode{ Mode::Chunk }, name{ std::move(name) }, hashCode{ HashCode(this->name) }, - isParallel{ isParallel } + isParallel{ isParallel }, + func{ detail::Pack(std::forward(func)) } { using ArgList = FuncTraits_ArgList>; @@ -80,12 +78,12 @@ namespace Ubpa::UECS { template SystemFunc::SystemFunc(Func&& func, std::string name, SingletonLocator singletonLocator) : - mode{ Mode::Job }, - func{ detail::Pack(std::forward(func)) }, singletonLocator{ std::move(singletonLocator.Combine()) }, + mode{ Mode::Job }, name{ std::move(name) }, hashCode{ HashCode(this->name) }, - isParallel{ false } + isParallel{ false }, + func{ detail::Pack(std::forward(func)) } { using ArgList = FuncTraits_ArgList>; diff --git a/include/UECS/detail/SystemMngr.inl b/include/UECS/detail/SystemMngr.inl index 2736e5a..7f4731a 100644 --- a/include/UECS/detail/SystemMngr.inl +++ b/include/UECS/detail/SystemMngr.inl @@ -1,6 +1,6 @@ #pragma once -#include <_deps/nameof.hpp> +#include namespace Ubpa::UECS { template diff --git a/include/UECS/detail/Util.h b/include/UECS/detail/Util.h index 29f7779..719d12b 100644 --- a/include/UECS/detail/Util.h +++ b/include/UECS/detail/Util.h @@ -4,14 +4,14 @@ #include namespace Ubpa::UECS { - inline constexpr size_t hash_combine(size_t x, size_t y) noexcept; + constexpr size_t hash_combine(size_t x, size_t y) noexcept; template - inline constexpr size_t hash_combine(const std::array& arr) noexcept; + constexpr size_t hash_combine(const std::array& arr) noexcept; template - inline size_t hash_combine(const Container& container) noexcept; + size_t hash_combine(const Container& container) noexcept; // fnv1a - inline constexpr size_t hash_string(std::string_view str) noexcept; + constexpr size_t hash_string(std::string_view str) noexcept; } #include "Util.inl" diff --git a/include/UECS/detail/Util.inl b/include/UECS/detail/Util.inl index 5150b04..77fa783 100644 --- a/include/UECS/detail/Util.inl +++ b/include/UECS/detail/Util.inl @@ -8,7 +8,7 @@ namespace Ubpa::UECS { } template - inline constexpr size_t hash_combine(const std::array& arr) noexcept { + constexpr size_t hash_combine(const std::array& arr) noexcept { static_assert(N >= 2); size_t rst = arr[0]; for (size_t i = 1; i < N; i++) @@ -31,7 +31,7 @@ namespace Ubpa::UECS { } // fnv1a - inline constexpr size_t hash_string(std::string_view str) noexcept { + constexpr size_t hash_string(std::string_view str) noexcept { return RuntimeTypeID(str); } } diff --git a/src/core/Archetype.cpp b/src/core/Archetype.cpp index 712b67a..0efa259 100644 --- a/src/core/Archetype.cpp +++ b/src/core/Archetype.cpp @@ -37,13 +37,13 @@ Archetype::Archetype(Pool* pool, const Archetype& src) chunks.resize(src.chunks.size(), nullptr); for (size_t i = 0; i < src.chunks.size(); i++) { - auto srcChunk = src.chunks[i]; - auto dstChunk = chunks[i] = chunkPool->Request(); + auto* srcChunk = src.chunks[i]; + auto* dstChunk = chunks[i] = chunkPool->Request(); size_t num = src.EntityNumOfChunk(i); for (auto type : types.data) { auto offset = Offsetof(type); - auto srcBegin = srcChunk->Data() + offset; - auto dstBegin = dstChunk->Data() + offset; + auto* srcBegin = srcChunk->Data() + offset; + auto* dstBegin = dstChunk->Data() + offset; auto size = cmptTraits.Sizeof(type); auto target = cmptTraits.copy_constructors.find(type); if (target != cmptTraits.copy_constructors.end()) { @@ -85,7 +85,7 @@ void Archetype::SetLayout() { Archetype* Archetype::New(RTDCmptTraits& rtdCmptTraits, Pool* chunkPool, const CmptType* types, size_t num) { assert(NotContainEntity(types, num)); - auto rst = new Archetype{ chunkPool }; + auto* rst = new Archetype{ chunkPool }; rst->types.Insert(types, num); rst->types.data.insert(CmptType::Of); rst->cmptTraits.Register(); @@ -99,7 +99,7 @@ Archetype* Archetype::Add(RTDCmptTraits& rtdCmptTraits, const Archetype* from, c assert(NotContainEntity(types, num)); assert(!from->types.ContainsAll(types, num)); - Archetype* rst = new Archetype{ from->chunkPool }; + auto* rst = new Archetype{ from->chunkPool }; rst->types = from->types; rst->cmptTraits = from->cmptTraits; @@ -116,7 +116,7 @@ Archetype* Archetype::Remove(const Archetype* from, const CmptType* types, size_ assert(NotContainEntity(types, num)); assert(from->types.ContainsAny(types, num)); - Archetype* rst = new Archetype{ from->chunkPool }; + auto* rst = new Archetype{ from->chunkPool }; rst->types = from->types; rst->cmptTraits = from->cmptTraits; @@ -157,7 +157,7 @@ size_t Archetype::Create(RTDCmptTraits& rtdCmptTraits, Entity e) { size_t Archetype::RequestBuffer() { if (entityNum == chunks.size() * chunkCapacity) { - auto chunk = chunkPool->Request(); + auto* chunk = chunkPool->Request(); chunks.push_back(chunk); } return entityNum++; @@ -237,7 +237,7 @@ void* Archetype::Locate(size_t chunkIdx, CmptType t) const { if (!types.Contains(t)) return nullptr; - auto buffer = chunks[chunkIdx]->Data(); + auto* buffer = chunks[chunkIdx]->Data(); return buffer + Offsetof(t); } diff --git a/src/core/ArchetypeFilter.cpp b/src/core/ArchetypeFilter.cpp index 944fa62..4c23bf9 100644 --- a/src/core/ArchetypeFilter.cpp +++ b/src/core/ArchetypeFilter.cpp @@ -31,7 +31,7 @@ bool ArchetypeFilter::HaveWriteCmptType() const noexcept { return false; } -bool ArchetypeFilter::operator==(const ArchetypeFilter& rhs) const { +bool ArchetypeFilter::operator==(const ArchetypeFilter& rhs) const noexcept { return all == rhs.all && any == rhs.any && none == rhs.none; diff --git a/src/core/CmptLocator.cpp b/src/core/CmptLocator.cpp index 9a28109..96d4145 100644 --- a/src/core/CmptLocator.cpp +++ b/src/core/CmptLocator.cpp @@ -32,6 +32,6 @@ bool CmptLocator::HasWriteCmptType() const noexcept { return false; } -bool CmptLocator::operator==(const CmptLocator& rhs) const { +bool CmptLocator::operator==(const CmptLocator& rhs) const noexcept { return cmptTypes == rhs.cmptTypes; } diff --git a/src/core/EntityMngr.cpp b/src/core/EntityMngr.cpp index 5ddd0b5..c219517 100644 --- a/src/core/EntityMngr.cpp +++ b/src/core/EntityMngr.cpp @@ -12,9 +12,9 @@ EntityMngr::EntityMngr() {} EntityMngr::EntityMngr(const EntityMngr& em) - : - sharedChunkPool{ std::make_unique>() }, - cmptTraits{em.cmptTraits} + : + cmptTraits{ em.cmptTraits }, + sharedChunkPool{ std::make_unique>() } { ts2a.reserve(em.ts2a.size()); for (const auto& [ts, a] : em.ts2a) { @@ -34,7 +34,7 @@ EntityMngr::EntityMngr(const EntityMngr& em) queryCache.reserve(em.queryCache.size()); for (const auto& [query, srcArchetypes] : em.queryCache) { auto& dstArchetypes = queryCache[query]; - for (auto archetype : srcArchetypes) + for (auto* archetype : srcArchetypes) dstArchetypes.insert(ts2a.at(archetype->types).get()); } } @@ -73,7 +73,7 @@ Archetype* EntityMngr::GetOrCreateArchetypeOf(const CmptType* types, size_t num) if (target != ts2a.end()) return target->second.get(); - auto archetype = Archetype::New(cmptTraits, sharedChunkPool.get(), types, num); + auto* archetype = Archetype::New(cmptTraits, sharedChunkPool.get(), types, num); ts2a.emplace(std::move(typeset), std::unique_ptr{ archetype }); for (auto& [query, archetypes] : queryCache) { @@ -87,7 +87,7 @@ Archetype* EntityMngr::GetOrCreateArchetypeOf(const CmptType* types, size_t num) Entity EntityMngr::Create(const CmptType* types, size_t num) { Archetype* archetype = GetOrCreateArchetypeOf(types, num); size_t entityIndex = RequestEntityFreeEntry(); - EntityInfo& info = entityTable[entityIndex]; + auto& info = entityTable[entityIndex]; Entity e{ entityIndex, info.version }; info.archetype = archetype; info.idxInArchetype = archetype->Create(cmptTraits, e); @@ -130,8 +130,8 @@ Archetype* EntityMngr::AttachWithoutInit(Entity e, const CmptType* types, size_t const auto& srcCmptTraits = srcArchetype->GetArchetypeCmptTraits(); for (const auto& type : srcCmptTypeSet.data) { - auto srcCmpt = srcArchetype->At(type, srcIdxInArchetype); - auto dstCmpt = dstArchetype->At(type, dstIdxInArchetype); + auto* srcCmpt = srcArchetype->At(type, srcIdxInArchetype); + auto* dstCmpt = dstArchetype->At(type, dstIdxInArchetype); srcCmptTraits.MoveConstruct(type, dstCmpt, srcCmpt); } @@ -147,7 +147,7 @@ Archetype* EntityMngr::AttachWithoutInit(Entity e, const CmptType* types, size_t } void EntityMngr::Attach(Entity e, const CmptType* types, size_t num) { - auto origArchetype = AttachWithoutInit(e, types, num); + auto* origArchetype = AttachWithoutInit(e, types, num); const auto& info = entityTable[e.Idx()]; for (size_t i = 0; i < num; i++) { const auto& type = types[i]; @@ -200,13 +200,13 @@ void EntityMngr::Detach(Entity e, const CmptType* types, size_t num) { const auto& srcCmptTraits = srcArchetype->GetArchetypeCmptTraits(); for (const auto& type : dstCmptTypeSet.data) { - auto srcCmpt = srcArchetype->At(type, srcIdxInArchetype); - auto dstCmpt = dstArchetype->At(type, dstIdxInArchetype); + auto* srcCmpt = srcArchetype->At(type, srcIdxInArchetype); + auto* dstCmpt = dstArchetype->At(type, dstIdxInArchetype); srcCmptTraits.MoveConstruct(type, dstCmpt, srcCmpt); } // erase - auto srcMovedEntityIndex = srcArchetype->Erase(srcIdxInArchetype); // call destructor + size_t srcMovedEntityIndex = srcArchetype->Erase(srcIdxInArchetype); // call destructor if (srcMovedEntityIndex != static_cast(-1)) entityTable[srcMovedEntityIndex].idxInArchetype = srcIdxInArchetype; @@ -275,7 +275,7 @@ void EntityMngr::Destroy(Entity e) { if (!Exist(e)) throw std::invalid_argument("Entity is invalid"); auto info = entityTable[e.Idx()]; - auto archetype = info.archetype; + auto* archetype = info.archetype; auto idxInArchetype = info.idxInArchetype; auto movedEntityIndex = archetype->Erase(idxInArchetype); @@ -504,7 +504,7 @@ bool EntityMngr::IsSingleton(CmptType t) const { const auto& archetypes = QueryArchetypes(query); if (archetypes.size() != 1) return false; - auto archetype = *archetypes.begin(); + auto* archetype = *archetypes.begin(); if (archetype->EntityNum() != 1) return false; @@ -516,7 +516,7 @@ Entity EntityMngr::GetSingletonEntity(CmptType t) const { ArchetypeFilter filter{ {CmptAccessType{t}}, {}, {} }; EntityQuery query(move(filter)); const auto& archetypes = QueryArchetypes(query); - auto archetype = *archetypes.begin(); + auto* archetype = *archetypes.begin(); return *archetype->At(0); } @@ -526,13 +526,13 @@ CmptPtr EntityMngr::GetSingleton(CmptType t) const { const auto& archetypes = QueryArchetypes(query); size_t num = 0; - for (auto archetype : archetypes) + for (auto* archetype : archetypes) num += archetype->EntityNum(); if (num != 1) return { CmptType::Invalid(), nullptr }; - for (auto archetype : archetypes) { + for (auto* archetype : archetypes) { if (archetype->EntityNum() != 0) return { t, archetype->At(t, 0) }; } @@ -552,7 +552,7 @@ std::vector EntityMngr::GetCmptArray(const ArchetypeFilter& filter, Cmp num += archetype->EntityNum(); rst.reserve(num); - for (auto archetype : archetypes) { + for (auto* archetype : archetypes) { /*for (size_t i = 0; i < archetype->EntityNum(); i++) rst[idx++] = *archetype->At(type, i);*/ @@ -561,8 +561,8 @@ std::vector EntityMngr::GetCmptArray(const ArchetypeFilter& filter, Cmp size_t size = archetype->cmptTraits.Sizeof(type); size_t offset = archetype->Offsetof(type); for (size_t c = 0; c < archetype->chunks.size(); c++) { - auto buffer = archetype->chunks[c]->Data(); - auto beg = buffer + offset; + auto* buffer = archetype->chunks[c]->Data(); + auto* beg = buffer + offset; size_t chunkSize = archetype->EntityNumOfChunk(c); for (size_t i = 0; i < chunkSize; i++) rst.emplace_back(type, beg + i * size); @@ -582,7 +582,7 @@ std::vector EntityMngr::GetEntityArray(const ArchetypeFilter& filter) co num += archetype->EntityNum(); rst.reserve(num); - for (auto archetype : archetypes) { + for (auto* archetype : archetypes) { /*for (size_t i = 0; i < archetype->EntityNum(); i++) rst[idx++] = *archetype->At(i);*/ @@ -590,8 +590,8 @@ std::vector EntityMngr::GetEntityArray(const ArchetypeFilter& filter) co size_t offset = archetype->Offsetof(CmptType::Of); for (size_t c = 0; c < archetype->chunks.size(); c++) { - auto buffer = archetype->chunks[c]->Data(); - auto beg = buffer + offset; + auto* buffer = archetype->chunks[c]->Data(); + auto* beg = buffer + offset; size_t chunkSize = archetype->EntityNumOfChunk(c); for (size_t i = 0; i < chunkSize; i++) rst.push_back(*reinterpret_cast(beg + i * sizeof(Entity))); diff --git a/src/core/Schedule.cpp b/src/core/Schedule.cpp index 6a5dd08..d65d8a4 100644 --- a/src/core/Schedule.cpp +++ b/src/core/Schedule.cpp @@ -345,8 +345,8 @@ SysFuncGraph Schedule::GenSysFuncGraph() const { for (size_t i = 0; i < sortedGroup.size() - 1; i++) { const auto& gx = sortedGroup[i]; const auto& gy = sortedGroup[i + 1]; - for (auto fx : gx.sysFuncs) { - for (auto fy : gy.sysFuncs) + for (auto* fx : gx.sysFuncs) { + for (auto* fy : gy.sysFuncs) graph.AddEdge(fx, fy); } } diff --git a/src/core/SingletonLocator.cpp b/src/core/SingletonLocator.cpp index cfc2738..a7ac51a 100644 --- a/src/core/SingletonLocator.cpp +++ b/src/core/SingletonLocator.cpp @@ -1,7 +1,5 @@ #include -#include - using namespace Ubpa::UECS; using namespace std; diff --git a/src/core/SysFuncGraph.cpp b/src/core/SysFuncGraph.cpp index b43c90a..7295752 100644 --- a/src/core/SysFuncGraph.cpp +++ b/src/core/SysFuncGraph.cpp @@ -37,10 +37,10 @@ bool SysFuncGraph::HavePath(SystemFunc* x, SystemFunc* y) const { queue q; q.push(x); while (!q.empty()) { - auto v = q.front(); + auto* v = q.front(); visited.insert(v); q.pop(); - for (auto adjV : adjList.at(v)) { + for (auto* adjV : adjList.at(v)) { if (visited.find(adjV) != visited.end()) continue; if (y == adjV) @@ -64,11 +64,11 @@ void SysFuncGraph::AddEdge(SystemFunc* x, SystemFunc* y) { SysFuncGraph SysFuncGraph::SubGraph(const std::vector& vertices) const { assert(HaveVertices(vertices)); SysFuncGraph subgraph; - for (auto v : vertices) + for (auto* v : vertices) subgraph.AddVertex(v); - for (auto x : vertices) { - for (auto y : vertices) { + for (auto* x : vertices) { + for (auto* y : vertices) { if (y == x) continue; if (HavePath(x, y)) @@ -99,11 +99,11 @@ tuple> SysFuncGraph::Toposort() const { } while (!zero_in_degree_vertices.empty()) { - auto v = zero_in_degree_vertices.top(); + auto* v = zero_in_degree_vertices.top(); zero_in_degree_vertices.pop(); sorted_vertices.push_back(v); in_degree_map.erase(v); - for (auto child : adjList.at(v)) { + for (auto* child : adjList.at(v)) { auto target = in_degree_map.find(child); if (target == in_degree_map.end()) continue; diff --git a/src/core/SystemFunc.cpp b/src/core/SystemFunc.cpp index 31a3498..d298e67 100644 --- a/src/core/SystemFunc.cpp +++ b/src/core/SystemFunc.cpp @@ -2,7 +2,7 @@ using namespace Ubpa::UECS; -void SystemFunc::operator()(World* w, SingletonsView singletonsView, Entity e, size_t entityIndexInQuery, CmptsView cmptsView) { +void SystemFunc::operator()(World* w, SingletonsView singletonsView, Entity e, size_t entityIndexInQuery, CmptsView cmptsView) const { assert(mode == Mode::Entity); return func( w, @@ -14,7 +14,7 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView, Entity e, s ); } -void SystemFunc::operator()(World* w, SingletonsView singletonsView, size_t entityBeginIndexInQuery, ChunkView chunkView) { +void SystemFunc::operator()(World* w, SingletonsView singletonsView, size_t entityBeginIndexInQuery, ChunkView chunkView) const { assert(mode == Mode::Chunk); return func( w, @@ -26,7 +26,7 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView, size_t enti ); } -void SystemFunc::operator()(World* w, SingletonsView singletonsView) { +void SystemFunc::operator()(World* w, SingletonsView singletonsView) const { assert(mode == Mode::Job); return func( w, diff --git a/src/core/SystemMngr.cpp b/src/core/SystemMngr.cpp index b4dff9b..23a53c2 100644 --- a/src/core/SystemMngr.cpp +++ b/src/core/SystemMngr.cpp @@ -1,7 +1,5 @@ #include -#include - #include using namespace Ubpa::UECS; @@ -22,7 +20,7 @@ SystemMngr::SystemMngr(const SystemMngr& mngr) } } -void SystemMngr::Clear() { +void SystemMngr::Clear() noexcept { frees.clear(); activeSystemIndices.clear(); name2idx.clear(); @@ -80,7 +78,7 @@ void SystemMngr::Activate(size_t index) { activeSystemIndices.insert(index); } -void SystemMngr::Deactivate(size_t index) { +void SystemMngr::Deactivate(size_t index) noexcept { assert(index < systems.size()); activeSystemIndices.erase(index); }