From 2aa59cf646a4fbd7e26ebd1232aacb6e707b0605 Mon Sep 17 00:00:00 2001 From: Ubpa Date: Sat, 14 Nov 2020 20:42:29 +0800 Subject: [PATCH] fix bugs --- CMakeLists.txt | 2 +- include/UECS/ChunkView.h | 17 +++++++------- include/UECS/CmptLocator.h | 4 ++-- include/UECS/CmptPtr.h | 6 ----- include/UECS/CmptsView.h | 1 + include/UECS/SystemTraits.h | 30 ++++++++++++------------ include/UECS/detail/ChunkView.inl | 12 ++++++++-- include/UECS/detail/CmptsLocator.inl | 4 ++-- include/UECS/detail/SingletonLocator.inl | 2 +- include/UECS/detail/SystemTraits.inl | 20 ++++++++-------- src/core/CmptLocator.cpp | 7 +++--- src/core/Schedule.cpp | 16 ++++++------- src/core/SystemFunc.cpp | 8 +++---- src/core/SystemTraits.cpp | 12 +++++----- src/core/World.cpp | 27 ++++----------------- src/test/11_runtime_cmpt/main.cpp | 8 ++----- 16 files changed, 77 insertions(+), 99 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4bd991..3cfce0a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ Ubpa_InitProject() Ubpa_AddDep(UContainer 0.0.8) Ubpa_AddDep(UTemplate 0.4.9) -Ubpa_AddDep(UGraphviz 0.1.5) +Ubpa_AddDep(UGraphviz 0.1.6) Ubpa_AddSubDirsRec(include) Ubpa_AddSubDirsRec(src) diff --git a/include/UECS/ChunkView.h b/include/UECS/ChunkView.h index 65ba7f3..32021b5 100644 --- a/include/UECS/ChunkView.h +++ b/include/UECS/ChunkView.h @@ -10,6 +10,7 @@ namespace Ubpa::UECS { class ChunkView { public: ChunkView(Archetype* archetype, size_t chunkIdx) noexcept; + ChunkView() noexcept = default; bool Contains(CmptType) const; size_t EntityNum() const noexcept { return entityNum; } @@ -17,16 +18,14 @@ namespace Ubpa::UECS { // nullptr if not contain void* GetCmptArray(CmptType) const; template - Span GetCmptArray() const { - return { static_cast(GetCmptArray(CmptType::Of)), EntityNum() }; - } - Span GetEntityArray() const { - return { static_cast(GetCmptArray(CmptType::Of)), EntityNum() }; - } + Span GetCmptArray() const; + Span GetEntityArray() const; private: - Archetype* archetype; - size_t chunkIdx; - size_t entityNum; + Archetype* archetype{ nullptr }; + size_t chunkIdx{ static_cast(-1) }; + size_t entityNum{ 0 }; }; } + +#include "detail/ChunkView.inl" diff --git a/include/UECS/CmptLocator.h b/include/UECS/CmptLocator.h index 88f546a..b669f4f 100644 --- a/include/UECS/CmptLocator.h +++ b/include/UECS/CmptLocator.h @@ -4,14 +4,14 @@ #include "CmptType.h" -#include +#include namespace Ubpa::UECS { // locate components in function's argument list for Archetype // immutable class CmptLocator { public: - CmptLocator(const CmptAccessType* types, size_t num); + CmptLocator(Span types); CmptLocator(); diff --git a/include/UECS/CmptPtr.h b/include/UECS/CmptPtr.h index 7c66eb7..3f56133 100644 --- a/include/UECS/CmptPtr.h +++ b/include/UECS/CmptPtr.h @@ -57,12 +57,6 @@ namespace Ubpa::UECS { return Write{p}; else if constexpr (mode == AccessMode::LATEST) return Latest{p}; - else if constexpr (mode == AccessMode::LAST_FRAME_SINGLETON) - return LastFrame>{p}; - else if constexpr (mode == AccessMode::WRITE_SINGLETON) - return Write>{p}; - else if constexpr (mode == AccessMode::LATEST_SINGLETON) - return Latest>{p}; else static_assert(false); } diff --git a/include/UECS/CmptsView.h b/include/UECS/CmptsView.h index 7a3bc00..8527eff 100644 --- a/include/UECS/CmptsView.h +++ b/include/UECS/CmptsView.h @@ -6,6 +6,7 @@ namespace Ubpa::UECS { class CmptsView { public: + CmptsView() noexcept = default; CmptsView(Span cmpts) noexcept : cmpts{ cmpts } {} CmptAccessPtr GetCmpt(CmptAccessType) const noexcept; diff --git a/include/UECS/SystemTraits.h b/include/UECS/SystemTraits.h index 2ee4f90..a7c9746 100644 --- a/include/UECS/SystemTraits.h +++ b/include/UECS/SystemTraits.h @@ -28,22 +28,22 @@ namespace Ubpa::UECS { // | // v // OnDestroy - using OnCreate = std::function; - using OnActivate = std::function; - using OnUpdate = std::function; - using OnDeactivate = std::function; - using OnDestroy = std::function; + using OnCreate = void(World*); + using OnActivate = void(World*); + using OnUpdate = void(Schedule&); + using OnDeactivate = void(World*); + using OnDestroy = void(World*); SystemTraits() = default; SystemTraits(const SystemTraits&); SystemTraits(SystemTraits&&) noexcept = default; size_t Register(std::string name); - void RegisterOnCreate(size_t ID, OnCreate); - void RegisterOnActivate(size_t ID, OnActivate); - void RegisterOnUpdate(size_t ID, OnUpdate); - void RegisterOnDeactivate(size_t ID, OnDeactivate); - void RegisterOnDestroy(size_t ID, OnDestroy); + void RegisterOnCreate(size_t ID, std::function); + void RegisterOnActivate(size_t ID, std::function); + void RegisterOnUpdate(size_t ID, std::function); + void RegisterOnDeactivate(size_t ID, std::function); + void RegisterOnDestroy(size_t ID, std::function); bool IsRegistered(size_t ID) const noexcept; size_t GetID(std::string_view name) const; @@ -67,11 +67,11 @@ namespace Ubpa::UECS { std::vector names; std::unordered_map name2id; - std::unordered_map createMap; - std::unordered_map activateMap; - std::unordered_map updateMap; - std::unordered_map deactivateMap; - std::unordered_map destroyMap; + std::unordered_map> createMap; + std::unordered_map> activateMap; + std::unordered_map> updateMap; + std::unordered_map> deactivateMap; + std::unordered_map> destroyMap; }; } diff --git a/include/UECS/detail/ChunkView.inl b/include/UECS/detail/ChunkView.inl index 1f9cbcb..7093819 100644 --- a/include/UECS/detail/ChunkView.inl +++ b/include/UECS/detail/ChunkView.inl @@ -4,7 +4,15 @@ namespace Ubpa::UECS { template Span ChunkView::GetCmptArray() const { auto* ptr = GetCmptArray(CmptType::Of); - if(ptr) - return { static_cast(GetCmptArray(CmptType::Of)), EntityNum() }; + if (!ptr) + return {}; + return { static_cast(ptr), EntityNum() }; + } + + inline Span ChunkView::GetEntityArray() const { + auto* ptr = GetCmptArray(CmptType::Of); + if (!ptr) + return {}; + return { static_cast(ptr), EntityNum() }; } } diff --git a/include/UECS/detail/CmptsLocator.inl b/include/UECS/detail/CmptsLocator.inl index 0d25c0c..85b31a9 100644 --- a/include/UECS/detail/CmptsLocator.inl +++ b/include/UECS/detail/CmptsLocator.inl @@ -8,10 +8,10 @@ namespace Ubpa::UECS::detail { CmptLocator GenerateCmptLocator(TypeList) { if constexpr (sizeof...(Cmpts) > 0) { constexpr std::array types{ CmptAccessType::Of... }; - return CmptLocator{ types.data(), types.size() }; + return CmptLocator{ types }; } else - return CmptLocator{}; + return {}; } } diff --git a/include/UECS/detail/SingletonLocator.inl b/include/UECS/detail/SingletonLocator.inl index 76d7bdc..5a72a90 100644 --- a/include/UECS/detail/SingletonLocator.inl +++ b/include/UECS/detail/SingletonLocator.inl @@ -11,7 +11,7 @@ namespace Ubpa::UECS::detail { return SingletonLocator{ types }; } else - return SingletonLocator{{}}; + return {}; } } diff --git a/include/UECS/detail/SystemTraits.inl b/include/UECS/detail/SystemTraits.inl index 50dd6ee..0e85bc3 100644 --- a/include/UECS/detail/SystemTraits.inl +++ b/include/UECS/detail/SystemTraits.inl @@ -6,29 +6,29 @@ namespace Ubpa::UECS::detail { template - Concept(HaveOnCreate, static_cast(&System::OnCreate)); + Concept(HaveOnCreate, static_cast(&System::OnCreate )); template - Concept(HaveOnActivate, static_cast(&System::OnActivate)); + Concept(HaveOnActivate, static_cast(&System::OnActivate )); template - Concept(HaveOnUpdate, static_cast(&System::OnUpdate)); + Concept(HaveOnUpdate, static_cast(&System::OnUpdate )); template - Concept(HaveOnDeactivate, static_cast(&System::OnDeactivate)); + Concept(HaveOnDeactivate, static_cast(&System::OnDeactivate)); template - Concept(HaveOnDestroy, static_cast(&System::OnDestroy)); + Concept(HaveOnDestroy, static_cast(&System::OnDestroy )); template size_t Register(SystemTraits& traits) { size_t ID = traits.Register(std::string{ SystemTraits::StaticNameof() }); if constexpr (Require) - traits.RegisterOnCreate(ID, std::function{ static_cast(&System::OnCreate) }); + traits.RegisterOnCreate (ID, static_cast(&System::OnCreate )); if constexpr (Require) - traits.RegisterOnActivate(ID, std::function{ static_cast(&System::OnActivate) }); + traits.RegisterOnActivate (ID, static_cast(&System::OnActivate )); if constexpr (Require) - traits.RegisterOnUpdate(ID, static_cast(&System::OnUpdate)); + traits.RegisterOnUpdate (ID, static_cast(&System::OnUpdate )); if constexpr (Require) - traits.RegisterOnDeactivate(ID, std::function{ static_cast(&System::OnDeactivate) }); + traits.RegisterOnDeactivate(ID, static_cast(&System::OnDeactivate)); if constexpr (Require) - traits.RegisterOnDestroy(ID, std::function{ static_cast(&System::OnDestroy) }); + traits.RegisterOnDestroy (ID, static_cast(&System::OnDestroy )); return ID; } } diff --git a/src/core/CmptLocator.cpp b/src/core/CmptLocator.cpp index 91362e4..0d7b5d1 100644 --- a/src/core/CmptLocator.cpp +++ b/src/core/CmptLocator.cpp @@ -4,10 +4,9 @@ using namespace Ubpa::UECS; -CmptLocator::CmptLocator(const CmptAccessType* types, size_t num) { - assert(types || num == 0); - for (size_t i = 0; i < num; i++) - cmptTypes.insert(types[i]); +CmptLocator::CmptLocator(Span types) { + for (const auto& type : types) + cmptTypes.insert(type); UpdateHashCode(); } diff --git a/src/core/Schedule.cpp b/src/core/Schedule.cpp index af1ab83..315d04a 100644 --- a/src/core/Schedule.cpp +++ b/src/core/Schedule.cpp @@ -33,7 +33,7 @@ namespace Ubpa::UECS::detail { // if (flag) // return true; //} - if (y.randomTypes.empty()) { // y.none + if (y.randomTypes.empty() && !y.noneTypes.empty()) { // y.none bool allFlag = false; bool anyFlag = true; bool randomFlag = true; @@ -59,8 +59,8 @@ namespace Ubpa::UECS::detail { // any auto x_iter = x.anyTypes.begin(); auto y_iter = y.noneTypes.begin(); - while (x_iter != x.anyTypes.end() && y_iter != y.noneTypes.end()) { - if (x_iter->GetCmptType() < *y_iter) { + while (x_iter != x.anyTypes.end()) { + if (y_iter == y.noneTypes.end() || x_iter->GetCmptType() < *y_iter) { anyFlag = false; break; } @@ -91,7 +91,7 @@ namespace Ubpa::UECS::detail { return true; } } - if (x.randomTypes.empty()) { // x.none + if (x.randomTypes.empty() && !x.noneTypes.empty()) { // x.none bool allFlag = false; bool anyFlag = true; bool randomFlag = true; @@ -116,8 +116,8 @@ namespace Ubpa::UECS::detail { else { auto x_iter = x.noneTypes.begin(); auto y_iter = y.anyTypes.begin(); - while (x_iter != x.noneTypes.end() && y_iter != y.anyTypes.end()) { - if (y_iter->GetCmptType() < *x_iter) { + while (x_iter != x.noneTypes.end()) { + if (y_iter == y.anyTypes.end() || y_iter->GetCmptType() < *x_iter) { anyFlag = false; break; } @@ -263,8 +263,8 @@ namespace Ubpa::UECS::detail { bool haveOrder = false; for (auto* ifunc : gi.sysFuncs) { for (auto* jfunc : gj.sysFuncs) { - if (subgraph.HavePath(ifunc, jfunc) - || subgraph.HavePath(jfunc, ifunc)) { + if (graph.HavePath(ifunc, jfunc) + || graph.HavePath(jfunc, ifunc)) { haveOrder = true; break; } diff --git a/src/core/SystemFunc.cpp b/src/core/SystemFunc.cpp index 11aaedb..c5c0bd8 100644 --- a/src/core/SystemFunc.cpp +++ b/src/core/SystemFunc.cpp @@ -10,7 +10,7 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView, Entity e, s e, entityIndexInQuery, cmptsView, - ChunkView{ nullptr, static_cast(-1) } + {} ); } @@ -21,7 +21,7 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView, size_t enti singletonsView, Entity::Invalid(), entityBeginIndexInQuery, - CmptsView{ {} }, + {}, chunkView ); } @@ -33,7 +33,7 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView) const { singletonsView, Entity::Invalid(), static_cast(-1), - CmptsView{ {} }, - ChunkView{ nullptr, static_cast(-1) } + {}, + {} ); } diff --git a/src/core/SystemTraits.cpp b/src/core/SystemTraits.cpp index 8ce76a9..97404e8 100644 --- a/src/core/SystemTraits.cpp +++ b/src/core/SystemTraits.cpp @@ -19,7 +19,7 @@ SystemTraits::SystemTraits(const SystemTraits& traits) } size_t SystemTraits::Register(std::string name) { - assert(names.empty()); + assert(!name.empty()); auto target = name2id.find(name); if (target != name2id.end()) @@ -41,27 +41,27 @@ size_t SystemTraits::GetID(std::string_view name) const { return target == name2id.end() ? static_cast(-1) : target->second; } -void SystemTraits::RegisterOnCreate(size_t ID, OnCreate func) { +void SystemTraits::RegisterOnCreate(size_t ID, std::function func) { assert(IsRegistered(ID)); createMap.emplace(ID, std::move(func)); } -void SystemTraits::RegisterOnActivate(size_t ID, OnActivate func) { +void SystemTraits::RegisterOnActivate(size_t ID, std::function func) { assert(IsRegistered(ID)); activateMap.emplace(ID, std::move(func)); } -void SystemTraits::RegisterOnUpdate(size_t ID, OnUpdate func) { +void SystemTraits::RegisterOnUpdate(size_t ID, std::function func) { assert(IsRegistered(ID)); updateMap.emplace(ID, std::move(func)); } -void SystemTraits::RegisterOnDeactivate(size_t ID, OnDeactivate func) { +void SystemTraits::RegisterOnDeactivate(size_t ID, std::function func) { assert(IsRegistered(ID)); deactivateMap.emplace(ID, std::move(func)); } -void SystemTraits::RegisterOnDestroy(size_t ID, OnDestroy func) { +void SystemTraits::RegisterOnDestroy(size_t ID, std::function func) { assert(IsRegistered(ID)); destroyMap.emplace(ID, std::move(func)); } diff --git a/src/core/World.cpp b/src/core/World.cpp index c258bf7..e1cb2ea 100644 --- a/src/core/World.cpp +++ b/src/core/World.cpp @@ -108,13 +108,8 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { auto& subgraph_basic_all = subgraph_basic.GenSubgraph("Basic All Edges"); auto& subgraph_basic_any = subgraph_basic.GenSubgraph("Basic Any Edges"); - auto& subgraph_lastframe_all = subgraph_lastframe.GenSubgraph("LastFrame All Edges"); auto& subgraph_lastframe_any = subgraph_lastframe.GenSubgraph("LastFrame Any Edges"); - - auto& subgraph_write_all = subgraph_write.GenSubgraph("Write All Edges"); auto& subgraph_write_any = subgraph_write.GenSubgraph("Write Any Edges"); - - auto& subgraph_latest_all = subgraph_latest.GenSubgraph("Latest All Edges"); auto& subgraph_latest_any = subgraph_latest.GenSubgraph("Latest Any Edges"); auto& subgraph_none = subgraph_basic.GenSubgraph("None Edges"); @@ -141,30 +136,16 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { subgraph_latest.RegisterGraphEdgeAttr("color", "#6BD089"); subgraph_order.RegisterGraphEdgeAttr("color", "#00A2E8"); - subgraph_basic_all - .RegisterGraphEdgeAttr("style", "dashed") - .RegisterGraphEdgeAttr("arrowhead", "box"); - subgraph_lastframe_all - .RegisterGraphEdgeAttr("style", "dashed") - .RegisterGraphEdgeAttr("arrowhead", "box"); - subgraph_write_all - .RegisterGraphEdgeAttr("style", "dashed") - .RegisterGraphEdgeAttr("arrowhead", "box"); - subgraph_latest_all - .RegisterGraphEdgeAttr("style", "dashed") - .RegisterGraphEdgeAttr("arrowhead", "box"); + subgraph_basic_all.RegisterGraphEdgeAttr("style", "dashed"); subgraph_basic_any .RegisterGraphEdgeAttr("style", "dashed") .RegisterGraphEdgeAttr("arrowhead", "diamond"); subgraph_lastframe_any - .RegisterGraphEdgeAttr("style", "dashed") .RegisterGraphEdgeAttr("arrowhead", "diamond"); subgraph_write_any - .RegisterGraphEdgeAttr("style", "dashed") .RegisterGraphEdgeAttr("arrowhead", "diamond"); subgraph_latest_any - .RegisterGraphEdgeAttr("style", "dashed") .RegisterGraphEdgeAttr("arrowhead", "diamond"); subgraph_none @@ -258,15 +239,15 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { { case AccessMode::LAST_FRAME: edgeIdx = registry.RegisterEdge(cmptType2idx.at(cmptType), sysIdx); - (isChunk ? subgraph_lastframe_all : subgraph_basic_all).AddEdge(edgeIdx); + (isChunk ? subgraph_lastframe : subgraph_basic).AddEdge(edgeIdx); break; case AccessMode::WRITE: edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx.at(cmptType)); - (isChunk ? subgraph_write_all : subgraph_basic_all).AddEdge(edgeIdx); + (isChunk ? subgraph_write : subgraph_basic).AddEdge(edgeIdx); break; case AccessMode::LATEST: edgeIdx = registry.RegisterEdge(cmptType2idx.at(cmptType), sysIdx); - (isChunk ? subgraph_latest_all : subgraph_basic_all).AddEdge(edgeIdx); + (isChunk ? subgraph_latest : subgraph_basic).AddEdge(edgeIdx); break; default: assert(false); diff --git a/src/test/11_runtime_cmpt/main.cpp b/src/test/11_runtime_cmpt/main.cpp index 9029193..24bc1c8 100644 --- a/src/test/11_runtime_cmpt/main.cpp +++ b/src/test/11_runtime_cmpt/main.cpp @@ -14,12 +14,8 @@ struct RTDSystem { CmptAccessType{ "LuaCmpt", AccessMode::LATEST } }; - CmptLocator locator_write( - cmpts_write.data(), cmpts_write.size() - ); - CmptLocator locator_read( - cmpts_read.data(), cmpts_read.size() - ); + CmptLocator locator_write(cmpts_write); + CmptLocator locator_read(cmpts_read); schedule.RegisterEntityJob( [](CmptsView cmpts) {