From ff1ca9a3201f38e379bd29897c712f5479f161d9 Mon Sep 17 00:00:00 2001 From: Ubpa <641614112@qq.com> Date: Sun, 9 Aug 2020 00:43:59 +0800 Subject: [PATCH] * SingletonLocator, CmptLocator --- include/UECS/CmptLocator.h | 8 +++ include/UECS/Schedule.h | 14 +++- include/UECS/SingletonLocator.h | 8 +++ include/UECS/SystemFunc.h | 5 +- include/UECS/detail/CmptsLocator.inl | 35 +++++++++ include/UECS/detail/Schedule.inl | 30 ++++++-- include/UECS/detail/SingletonLocator.inl | 35 +++++++++ include/UECS/detail/SystemFunc.inl | 90 ++++++++++-------------- src/test/08_job/main.cpp | 2 +- src/test/09_idx_in_query/main.cpp | 2 +- 10 files changed, 166 insertions(+), 63 deletions(-) create mode 100644 include/UECS/detail/CmptsLocator.inl diff --git a/include/UECS/CmptLocator.h b/include/UECS/CmptLocator.h index d973cb6..133cc1e 100644 --- a/include/UECS/CmptLocator.h +++ b/include/UECS/CmptLocator.h @@ -15,6 +15,12 @@ namespace Ubpa::UECS { CmptLocator(); + template + static CmptLocator Generate(); + + template + CmptLocator& Combine(); + size_t HashCode() const noexcept { return hashCode; } const std::set& LastFrameCmptTypes() const noexcept { return lastFrameCmptTypes; } @@ -34,3 +40,5 @@ namespace Ubpa::UECS { size_t hashCode; }; } + +#include "detail/CmptsLocator.inl" diff --git a/include/UECS/Schedule.h b/include/UECS/Schedule.h index 700cbef..34a0e8a 100644 --- a/include/UECS/Schedule.h +++ b/include/UECS/Schedule.h @@ -26,11 +26,21 @@ namespace Ubpa::UECS { class Schedule { public: template - const SystemFunc* Register(Func&& func, std::string name, ArchetypeFilter filter = ArchetypeFilter{}); + const SystemFunc* Register( + Func&& func, + std::string name, + ArchetypeFilter filter = ArchetypeFilter{}, + SingletonLocator singletonLocator = SingletonLocator{} + ); // run-time dynamic function template - const SystemFunc* Register(Func&& func, std::string name, CmptLocator locator, ArchetypeFilter filter = ArchetypeFilter{}); + const SystemFunc* Register( + Func&& func, + std::string name, + CmptLocator locator, + ArchetypeFilter filter = ArchetypeFilter{}, + SingletonLocator singletonLocator = SingletonLocator{}); Schedule& LockFilter(std::string_view sys); diff --git a/include/UECS/SingletonLocator.h b/include/UECS/SingletonLocator.h index 4df7159..88ce9f2 100644 --- a/include/UECS/SingletonLocator.h +++ b/include/UECS/SingletonLocator.h @@ -13,6 +13,12 @@ namespace Ubpa::UECS { SingletonLocator(); + template + static SingletonLocator Generate(); + + template + SingletonLocator& Combine(); + const std::set& LastFrameSingletonTypes() const noexcept { return lastFrameSingletonTypes; } const std::set& WriteSingletonTypes() const noexcept { return writeSingletonTypes; } const std::set& LatestSingletonTypes() const noexcept { return latestSingletonTypes; } @@ -25,3 +31,5 @@ namespace Ubpa::UECS { std::set singletonTypes; }; } + +#include "detail/SingletonLocator.inl" diff --git a/include/UECS/SystemFunc.h b/include/UECS/SystemFunc.h index bf2e0ee..6c24d8b 100644 --- a/include/UECS/SystemFunc.h +++ b/include/UECS/SystemFunc.h @@ -36,7 +36,10 @@ namespace Ubpa::UECS { SingletonLocator singletonLocator; template - SystemFunc(Func&& func, std::string name, ArchetypeFilter archetypeFilter); + SystemFunc(Func&& func, std::string name, ArchetypeFilter archetypeFilter, SingletonLocator singletonLocator); + + template + SystemFunc(Func&& func, std::string name, CmptLocator cmptLocator, ArchetypeFilter archetypeFilter, SingletonLocator singletonLocator); const std::string& Name() const noexcept { return name; } diff --git a/include/UECS/detail/CmptsLocator.inl b/include/UECS/detail/CmptsLocator.inl new file mode 100644 index 0000000..123611a --- /dev/null +++ b/include/UECS/detail/CmptsLocator.inl @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +namespace Ubpa::UECS::detail { + template + CmptLocator GenerateCmptLocator(TypeList) { + if constexpr (sizeof...(Cmpts) > 0) { + constexpr std::array types{ CmptType::Of... }; + return CmptLocator{ types.data(), types.size() }; + } + else + return CmptLocator{}; + } +} + +namespace Ubpa::UECS { + template + CmptLocator CmptLocator::Generate() { + using ArgList = FuncTraits_ArgList>; + using CmptList = Filter_t; + return detail::GenerateCmptLocator(CmptList{}); + } + + template + CmptLocator& CmptLocator::Combine() { + CmptLocator funcLocator = Generate(); + lastFrameCmptTypes = SetUnion(lastFrameCmptTypes, funcLocator.lastFrameCmptTypes); + writeCmptTypes = SetUnion(writeCmptTypes, funcLocator.writeCmptTypes); + latestCmptTypes = SetUnion(latestCmptTypes, funcLocator.latestCmptTypes); + cmptTypes = SetUnion(cmptTypes, funcLocator.cmptTypes); + return *this; + } +} diff --git a/include/UECS/detail/Schedule.inl b/include/UECS/detail/Schedule.inl index 724f9fc..0e80cf9 100644 --- a/include/UECS/detail/Schedule.inl +++ b/include/UECS/detail/Schedule.inl @@ -2,13 +2,35 @@ namespace Ubpa::UECS { template - const SystemFunc* Schedule::Register(Func&& func, std::string name, ArchetypeFilter filter) { - return Request(std::forward(func), std::move(name), std::move(filter)); + const SystemFunc* Schedule::Register( + Func&& func, + std::string name, + ArchetypeFilter filter, + SingletonLocator singletonLocator) + { + return Request( + std::forward(func), + std::move(name), + std::move(filter), + std::move(singletonLocator) + ); } template - const SystemFunc* Schedule::Register(Func&& func, std::string name, CmptLocator locator, ArchetypeFilter filter) { - return Request(std::forward(func), std::move(name), std::move(locator), std::move(filter)); + const SystemFunc* Schedule::Register( + Func&& func, + std::string name, + CmptLocator locator, + ArchetypeFilter filter, + SingletonLocator singletonLocator + ) { + return Request( + std::forward(func), + std::move(name), + std::move(locator), + std::move(filter), + std::move(singletonLocator) + ); } template diff --git a/include/UECS/detail/SingletonLocator.inl b/include/UECS/detail/SingletonLocator.inl index e69de29..a6ad208 100644 --- a/include/UECS/detail/SingletonLocator.inl +++ b/include/UECS/detail/SingletonLocator.inl @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +namespace Ubpa::UECS::detail { + template + SingletonLocator GenerateSingletonLocator(TypeList) { + if constexpr (sizeof...(Singletons) > 0) { + constexpr std::array types{ CmptType::Of... }; + return SingletonLocator{ types.data(), types.size() }; + } + else + return SingletonLocator{}; + } +} + +namespace Ubpa::UECS { + template + SingletonLocator SingletonLocator::Generate() { + using ArgList = FuncTraits_ArgList>; + using SingletonList = Filter_t; + return detail::GenerateSingletonLocator(SingletonList{}); + } + + template + SingletonLocator& SingletonLocator::Combine() { + SingletonLocator funcLocator = Generate(); + lastFrameSingletonTypes = SetUnion(lastFrameSingletonTypes, funcLocator.lastFrameSingletonTypes); + writeSingletonTypes = SetUnion(writeSingletonTypes, funcLocator.writeSingletonTypes); + latestSingletonTypes = SetUnion(latestSingletonTypes, funcLocator.latestSingletonTypes); + singletonTypes = SetUnion(singletonTypes, funcLocator.singletonTypes); + return *this; + } +} diff --git a/include/UECS/detail/SystemFunc.inl b/include/UECS/detail/SystemFunc.inl index f10feec..86bb070 100644 --- a/include/UECS/detail/SystemFunc.inl +++ b/include/UECS/detail/SystemFunc.inl @@ -7,24 +7,24 @@ namespace Ubpa::UECS::detail { template auto Pack(Func&& func) noexcept; - template - constexpr CmptLocator GenCmptLocator() noexcept; - template - constexpr SingletonLocator GenSingletonLocator() noexcept; } namespace Ubpa::UECS { template - SystemFunc::SystemFunc(Func&& func, std::string name, ArchetypeFilter archetypeFilter) - : + SystemFunc::SystemFunc( + Func&& func, + std::string name, + ArchetypeFilter archetypeFilter, + SingletonLocator singletonLocator + ) : func{ detail::Pack(std::forward(func)) }, - entityQuery{ std::move(archetypeFilter), detail::GenCmptLocator() }, - singletonLocator{ detail::GenSingletonLocator() }, + entityQuery{ std::move(archetypeFilter), CmptLocator::Generate() }, + singletonLocator{ std::move(singletonLocator.Combine()) }, name{ std::move(name) }, hashCode{ HashCode(this->name) } { using ArgList = FuncTraits_ArgList>; - static_assert(!Contain_v && !Contain_v); + static_assert(!Contain_v); if constexpr (Length_v> > 0) { static_assert(!Contain_v); @@ -38,6 +38,26 @@ namespace Ubpa::UECS { mode = Mode::Job; } } + + template + SystemFunc::SystemFunc( + Func&& func, + std::string name, + CmptLocator cmptLocator, + ArchetypeFilter archetypeFilter, + SingletonLocator singletonLocator + ) : + mode{ Mode::Entity }, + func{ detail::Pack(std::forward(func)) }, + entityQuery{ std::move(archetypeFilter), std::move(cmptLocator.Combine()) }, + singletonLocator{ std::move(singletonLocator.Combine()) }, + name{ std::move(name) }, + hashCode{ HashCode(this->name) } + { + using ArgList = FuncTraits_ArgList>; + static_assert(!Contain_v); + assert(!entityQuery.locator.CmptTypes().empty()); + } } namespace Ubpa::UECS::detail { @@ -46,26 +66,26 @@ namespace Ubpa::UECS::detail { template struct Packer, TypeList, TypeList> { - using SortedSingletonList = TypeList; // sorted - using SortedNonSingletonList = TypeList; // sorted + using SingletonList = TypeList; // sorted + using NonSingletonList = TypeList; // sorted template static auto run(Func&& func) noexcept { return [func = std::forward(func)]( World* w, - SingletonsView singletonsView, + SingletonsView singletons, Entity e, size_t entityIndexInQuery, - CmptsView cmptsView, + CmptsView cmpts, ChunkView chunkView) { auto args = std::tuple{ w, - reinterpret_cast(singletonsView.Singletons()[Find_v].Ptr())..., + reinterpret_cast(singletons.Singletons()[Find_v].Ptr())..., e, entityIndexInQuery, - cmptsView, + cmpts, chunkView, - reinterpret_cast(cmptsView.Components()[Find_v].Ptr())... + reinterpret_cast(cmpts.Components()[Find_v].Ptr())... }; func(std::get(args)...); }; @@ -92,42 +112,4 @@ namespace Ubpa::UECS::detail { return Packer::run(std::forward(func)); } - - // ==================== - - template - constexpr CmptLocator GenCmptLocator(TypeList) noexcept { - if constexpr (sizeof...(Cmpts) > 0) { - constexpr std::array types{ CmptType::Of... }; - return CmptLocator{ types.data(), types.size() }; - } - else - return CmptLocator{}; - } - - template - constexpr CmptLocator GenCmptLocator() noexcept { - using ArgList = FuncTraits_ArgList>; - using CmptList = Filter_t; - return GenCmptLocator(CmptList{}); - } - - // ==================== - - template - constexpr SingletonLocator GenSingletonLocator(TypeList) noexcept { - if constexpr (sizeof...(Singletons) > 0) { - constexpr std::array types{ CmptType::Of... }; - return SingletonLocator{ types.data(), types.size() }; - } - else - return SingletonLocator{}; - } - - template - constexpr SingletonLocator GenSingletonLocator() noexcept { - using ArgList = FuncTraits_ArgList>; - using SingletonList = Filter_t; - return GenSingletonLocator(SingletonList{}); - } } diff --git a/src/test/08_job/main.cpp b/src/test/08_job/main.cpp index c840e5f..72a1dcf 100644 --- a/src/test/08_job/main.cpp +++ b/src/test/08_job/main.cpp @@ -28,7 +28,7 @@ class MySystem : public System { ); schedule.Order("system function", "job"); - size_t num = GetWorld()->entityMngr.EntityNum(f->query); + size_t num = GetWorld()->entityMngr.EntityNum(f->entityQuery); buffer->resize(num); } }; diff --git a/src/test/09_idx_in_query/main.cpp b/src/test/09_idx_in_query/main.cpp index a059f6c..2517d26 100644 --- a/src/test/09_idx_in_query/main.cpp +++ b/src/test/09_idx_in_query/main.cpp @@ -26,7 +26,7 @@ class MySystem : public System { }, "print flag" ); schedule.Order("set flag", "print flag"); - size_t num = GetWorld()->entityMngr.EntityNum(f->query); + size_t num = GetWorld()->entityMngr.EntityNum(f->entityQuery); flags->insert(flags->begin(), num, false); } };