From ba65258a3014bd301904b453e63c419b95344fa3 Mon Sep 17 00:00:00 2001 From: Ubpa <641614112@qq.com> Date: Fri, 14 Aug 2020 23:37:10 +0800 Subject: [PATCH] RTDCmptTraits -> EntityMngr --- include/UECS/EntityMngr.h | 2 ++ include/UECS/RTDCmptTraits.h | 3 +-- include/UECS/detail/RTDCmptTraits.inl | 5 ----- include/UECS/detail/RTSCmptTraits.h | 3 ++- include/UECS/detail/RTSCmptTraits.inl | 5 +---- src/core/Archetype.cpp | 6 +++--- src/core/EntityMngr.cpp | 5 ++--- src/core/World.cpp | 4 ++-- src/test/02_order/main.cpp | 8 ++++---- src/test/11_runtime_cmpt/main.cpp | 7 ++++--- src/test/12_framegraph/main.cpp | 2 +- src/test/14_serialize/main.cpp | 6 ++++-- src/test/15_chunk_job/main.cpp | 4 ++-- src/test/16_singleton/main.cpp | 5 ++--- 14 files changed, 30 insertions(+), 35 deletions(-) diff --git a/include/UECS/EntityMngr.h b/include/UECS/EntityMngr.h index bc80fa0..3156d75 100644 --- a/include/UECS/EntityMngr.h +++ b/include/UECS/EntityMngr.h @@ -26,6 +26,8 @@ namespace Ubpa::UECS { // - API with Entity require Entity exist (throw std::invalid_argument) class EntityMngr { public: + RTDCmptTraits cmptTraits; + template std::tuple Create(); diff --git a/include/UECS/RTDCmptTraits.h b/include/UECS/RTDCmptTraits.h index 9d857a6..74256d8 100644 --- a/include/UECS/RTDCmptTraits.h +++ b/include/UECS/RTDCmptTraits.h @@ -20,8 +20,6 @@ namespace Ubpa::UECS { public: static constexpr size_t default_alignment = alignof(std::max_align_t); - static RTDCmptTraits& Instance() noexcept; - RTDCmptTraits& Clear(); // neccessary @@ -69,6 +67,7 @@ namespace Ubpa::UECS { friend class RTSCmptTraits; friend class Archetype; friend class EntityMngr; + friend class World; RTDCmptTraits() = default; diff --git a/include/UECS/detail/RTDCmptTraits.inl b/include/UECS/detail/RTDCmptTraits.inl index 806e862..f993bcc 100644 --- a/include/UECS/detail/RTDCmptTraits.inl +++ b/include/UECS/detail/RTDCmptTraits.inl @@ -3,11 +3,6 @@ #include "../RTDCmptTraits.h" namespace Ubpa::UECS { - inline RTDCmptTraits& RTDCmptTraits::Instance() noexcept { - static RTDCmptTraits instance; - return instance; - } - inline RTDCmptTraits& RTDCmptTraits::RegisterSize(CmptType type, size_t size) { sizeofs.emplace(type, size); return *this; diff --git a/include/UECS/detail/RTSCmptTraits.h b/include/UECS/detail/RTSCmptTraits.h index b5b8261..ff27e54 100644 --- a/include/UECS/detail/RTSCmptTraits.h +++ b/include/UECS/detail/RTSCmptTraits.h @@ -1,6 +1,7 @@ #pragma once #include "../CmptType.h" +#include "../RTDCmptTraits.h" #include #include @@ -20,7 +21,7 @@ namespace Ubpa::UECS { void Register(); // use RTDCmptTraits - void Register(CmptType type); + void Register(const RTDCmptTraits& rtdCmptTraits, CmptType type); template void Deregister(); diff --git a/include/UECS/detail/RTSCmptTraits.inl b/include/UECS/detail/RTSCmptTraits.inl index c11ad34..ab3cec8 100644 --- a/include/UECS/detail/RTSCmptTraits.inl +++ b/include/UECS/detail/RTSCmptTraits.inl @@ -1,7 +1,5 @@ #pragma once -#include "../RTDCmptTraits.h" - #include namespace Ubpa::UECS { @@ -99,8 +97,7 @@ namespace Ubpa::UECS { move_assignments.erase(type); } - inline void RTSCmptTraits::Register(CmptType type) { - const auto& rtdct = RTDCmptTraits().Instance(); + inline void RTSCmptTraits::Register(const RTDCmptTraits& rtdct, CmptType type) { auto size_target = rtdct.sizeofs.find(type); if (size_target == rtdct.sizeofs.end()) throw std::logic_error("RTSCmptTraits::Register: RTDCmptTraits hasn't registered "); diff --git a/src/core/Archetype.cpp b/src/core/Archetype.cpp index f63fa0b..d9c2960 100644 --- a/src/core/Archetype.cpp +++ b/src/core/Archetype.cpp @@ -58,7 +58,7 @@ Archetype* Archetype::New(EntityMngr* entityMngr, const CmptType* types, size_t rst->types.data.insert(CmptType::Of); rst->cmptTraits.Register(); for (size_t i = 0; i < num; i++) - rst->cmptTraits.Register(types[i]); + rst->cmptTraits.Register(entityMngr->cmptTraits, types[i]); rst->SetLayout(); return rst; } @@ -73,7 +73,7 @@ Archetype* Archetype::Add(const Archetype* from, const CmptType* types, size_t n rst->cmptTraits = from->cmptTraits; rst->types.Insert(types, num); for (size_t i = 0; i < num; i++) - rst->cmptTraits.Register(types[i]); + rst->cmptTraits.Register(rst->entityMngr->cmptTraits, types[i]); rst->SetLayout(); @@ -102,7 +102,7 @@ size_t Archetype::Create(Entity e) { size_t idxInChunk = idx % chunkCapacity; byte* buffer = chunks[idx / chunkCapacity]->Data(); - const auto& rtdct = RTDCmptTraits::Instance(); + const auto& rtdct = entityMngr->cmptTraits; for (const auto& type : types.data) { if (type.Is()) { constexpr size_t size = sizeof(Entity); diff --git a/src/core/EntityMngr.cpp b/src/core/EntityMngr.cpp index ce29dbd..7d699d0 100644 --- a/src/core/EntityMngr.cpp +++ b/src/core/EntityMngr.cpp @@ -110,14 +110,13 @@ 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); const auto& info = entityTable[e.Idx()]; - const auto& rtdct = RTDCmptTraits::Instance(); for (size_t i = 0; i < num; i++) { const auto& type = types[i]; if (origArchetype->GetCmptTypeSet().Contains(type)) continue; - auto target = rtdct.default_constructors.find(type); - if (target == rtdct.default_constructors.end()) + auto target = cmptTraits.default_constructors.find(type); + if (target == cmptTraits.default_constructors.end()) continue; target->second(info.archetype->At(type, info.idxInArchetype)); diff --git a/src/core/World.cpp b/src/core/World.cpp index 3c4bb89..52b01d9 100644 --- a/src/core/World.cpp +++ b/src/core/World.cpp @@ -119,8 +119,8 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { unordered_map cmptType2idx; unordered_map sysFuncHashcode2idx; - auto queryCmptName = [](CmptType type) -> string { - auto cmptName = RTDCmptTraits::Instance().Nameof(type); + auto queryCmptName = [this](CmptType type) -> string { + auto cmptName = entityMngr.cmptTraits.Nameof(type); return cmptName.empty() ? std::to_string(type.HashCode()) : string{ cmptName }; }; diff --git a/src/test/02_order/main.cpp b/src/test/02_order/main.cpp index ac489d0..b8946c3 100644 --- a/src/test/02_order/main.cpp +++ b/src/test/02_order/main.cpp @@ -25,14 +25,14 @@ class DataSystem : public System { }; int main() { - RTDCmptTraits::Instance().Register< + World w; + w.systemMngr.Register(); + + w.entityMngr.cmptTraits.Register< Data1, Data2 >(); - World w; - w.systemMngr.Register(); - w.entityMngr.Create(); w.Update(); diff --git a/src/test/11_runtime_cmpt/main.cpp b/src/test/11_runtime_cmpt/main.cpp index a61299e..62de438 100644 --- a/src/test/11_runtime_cmpt/main.cpp +++ b/src/test/11_runtime_cmpt/main.cpp @@ -44,12 +44,13 @@ int main() { // LuaCmpt { // number value; // } - RTDCmptTraits::Instance().RegisterSize(type, 8); - RTDCmptTraits::Instance().RegisterDefaultConstructor(type, [](void*) { cout << "construct" << endl;}); - RTDCmptTraits::Instance().RegisterDestructor(type, [](void*) { cout << "destruct" << endl; }); World w; w.systemMngr.Register(); + w.entityMngr.cmptTraits + .RegisterSize(type, 8) + .RegisterDefaultConstructor(type, [](void*) { cout << "construct" << endl; }) + .RegisterDestructor(type, [](void*) { cout << "destruct" << endl; }); auto [e] = w.entityMngr.Create(); w.entityMngr.Attach(e, &type, 1); diff --git a/src/test/12_framegraph/main.cpp b/src/test/12_framegraph/main.cpp index 28eb63d..3d1f5a4 100644 --- a/src/test/12_framegraph/main.cpp +++ b/src/test/12_framegraph/main.cpp @@ -36,7 +36,7 @@ int main() { World w; w.systemMngr.Register(); - RTDCmptTraits::Instance() + w.entityMngr.cmptTraits .RegisterName(CmptType::Of, "A") .RegisterName(CmptType::Of, "B") .RegisterName(CmptType::Of, "C") diff --git a/src/test/14_serialize/main.cpp b/src/test/14_serialize/main.cpp index 326bb2a..24d3541 100644 --- a/src/test/14_serialize/main.cpp +++ b/src/test/14_serialize/main.cpp @@ -14,6 +14,7 @@ class Dumper : public IListener { bool firstSystem{ true }; bool firstEntity{ true }; bool firstCmpt{ true }; + const World* w; void PrintIndent() { for (size_t i = 0; i < indent; i++) @@ -26,6 +27,7 @@ class Dumper : public IListener { indent++; PrintIndent(); cout << "\"type\" : \"World\"," << endl; + this->w = w; } virtual void ExistWorld(const World* w) override { @@ -128,7 +130,7 @@ class Dumper : public IListener { cout << "{" << endl; indent++; PrintIndent(); - cout << "\"type\" : \"" << RTDCmptTraits::Instance().Nameof(cmpt->Type()) << "\""; + cout << "\"type\" : \"" << w->entityMngr.cmptTraits.Nameof(cmpt->Type()) << "\""; if (cmpt->Type().Is()) { auto v = cmpt->As(); cout << "," << endl; @@ -165,7 +167,7 @@ class MoverSystem : public System { int main() { World w; - RTDCmptTraits::Instance().Register(); + w.entityMngr.cmptTraits.Register(); w.systemMngr.Register(); w.entityMngr.Create(); w.entityMngr.Create(); diff --git a/src/test/15_chunk_job/main.cpp b/src/test/15_chunk_job/main.cpp index 3918d9c..326d18b 100644 --- a/src/test/15_chunk_job/main.cpp +++ b/src/test/15_chunk_job/main.cpp @@ -52,11 +52,11 @@ class SAB_System : public System { }; int main() { - RTDCmptTraits::Instance().Register(); - World w; w.systemMngr.Register(); + w.entityMngr.cmptTraits.Register(); + w.entityMngr.Create(); w.entityMngr.Create(); w.entityMngr.Create(); diff --git a/src/test/16_singleton/main.cpp b/src/test/16_singleton/main.cpp index 17455ed..95e3420 100644 --- a/src/test/16_singleton/main.cpp +++ b/src/test/16_singleton/main.cpp @@ -29,13 +29,12 @@ class MoverSystem : public System { }; int main() { - RTDCmptTraits::Instance().Register - (); - World w; w.systemMngr.Register(); w.entityMngr.Create(); w.entityMngr.Create(); + w.entityMngr.cmptTraits.Register + (); w.Update(); std::cout << w.DumpUpdateJobGraph() << std::endl;