diff --git a/include/UECS/EntityMngr.h b/include/UECS/EntityMngr.h index 78a0f97..cd479ea 100644 --- a/include/UECS/EntityMngr.h +++ b/include/UECS/EntityMngr.h @@ -1,13 +1,13 @@ #pragma once -#include "SystemFunc.h" #include "detail/Archetype.h" #include "detail/Job.h" #include "EntityQuery.h" +#include "SingletonLocator.h" namespace Ubpa::UECS { class World; - + class SystemFunc; class IListener; // Entity Manager of World @@ -24,10 +24,12 @@ namespace Ubpa::UECS { // - when free entries is empty, use new entity entry (version is 0) class EntityMngr { public: - EntityMngr(World* world) : world{ world }, sharedChunkPool{ std::make_unique>() } {} + EntityMngr(); EntityMngr(const EntityMngr& em); ~EntityMngr(); + RTDCmptTraits cmptTraits; + // same world void Swap(EntityMngr& rhs) noexcept; @@ -91,8 +93,6 @@ namespace Ubpa::UECS { void Accept(IListener* listener) const; private: - World* world; - friend class World; friend class Archetype; diff --git a/include/UECS/RTDCmptTraits.h b/include/UECS/RTDCmptTraits.h index a1c2730..297dbed 100644 --- a/include/UECS/RTDCmptTraits.h +++ b/include/UECS/RTDCmptTraits.h @@ -18,32 +18,17 @@ namespace Ubpa::UECS { // - name class RTDCmptTraits { public: - static constexpr size_t default_alignment = alignof(std::max_align_t); + static constexpr size_t DefaultAlignment() noexcept { return alignof(std::max_align_t); } RTDCmptTraits& Clear(); - // neccessary RTDCmptTraits& RegisterSize(CmptType, size_t size); - - // optional RTDCmptTraits& RegisterAlignment(CmptType, size_t alignment); - - // optional RTDCmptTraits& RegisterDefaultConstructor(CmptType, void(*)(void*)); - - // optional RTDCmptTraits& RegisterCopyConstructor(CmptType, void(*)(void*, void*)); - - // optional RTDCmptTraits& RegisterMoveConstructor(CmptType, void(*)(void*, void*)); - - // optional RTDCmptTraits& RegisterMoveAssignment(CmptType, void(*)(void*, void*)); - - // optional RTDCmptTraits& RegisterDestructor(CmptType, void(*)(void*)); - - // optional RTDCmptTraits& RegisterName(CmptType, std::string name); const auto& GetSizeofs() const noexcept { return sizeofs; } diff --git a/include/UECS/Schedule.h b/include/UECS/Schedule.h index 5af2fab..0a7c6e4 100644 --- a/include/UECS/Schedule.h +++ b/include/UECS/Schedule.h @@ -1,6 +1,6 @@ #pragma once -#include "SystemFunc.h" +#include "detail/SystemFunc.h" #include "detail/SysFuncGraph.h" #include "detail/Job.h" diff --git a/include/UECS/World.h b/include/UECS/World.h index 7ce0233..312785f 100644 --- a/include/UECS/World.h +++ b/include/UECS/World.h @@ -18,7 +18,6 @@ namespace Ubpa::UECS { SystemMngr systemMngr; EntityMngr entityMngr; - RTDCmptTraits cmptTraits; // 1. schedule: run registered System's static OnUpdate(Schedule&) // 2. gen job graph: schedule -> graph diff --git a/include/UECS/detail/Archetype.h b/include/UECS/detail/Archetype.h index 14bf596..fb1eebd 100644 --- a/include/UECS/detail/Archetype.h +++ b/include/UECS/detail/Archetype.h @@ -4,7 +4,7 @@ #include "../CmptPtr.h" #include "../CmptLocator.h" -#include "RTSCmptTraits.h" +#include "ArchetypeCmptTraits.h" #include "CmptTypeSet.h" #include "Chunk.h" @@ -86,7 +86,7 @@ namespace Ubpa::UECS { // Components + Entity const CmptTypeSet& GetCmptTypeSet() const noexcept { return types; } - const RTSCmptTraits& GetRTSCmptTraits() const noexcept { return cmptTraits; } + const ArchetypeCmptTraits& GetArchetypeCmptTraits() const noexcept { return cmptTraits; } size_t EntityNum() const noexcept { return entityNum; } size_t EntityNumOfChunk(size_t chunkIdx) const noexcept; @@ -109,7 +109,7 @@ namespace Ubpa::UECS { friend class EntityMngr; CmptTypeSet types; // Entity + Components - RTSCmptTraits cmptTraits; + ArchetypeCmptTraits cmptTraits; std::unordered_map type2offset; // CmptType to offset in chunk (include Entity) size_t chunkCapacity{ size_t_invalid }; diff --git a/include/UECS/detail/RTSCmptTraits.h b/include/UECS/detail/ArchetypeCmptTraits.h similarity index 94% rename from include/UECS/detail/RTSCmptTraits.h rename to include/UECS/detail/ArchetypeCmptTraits.h index b9d63e3..c0cb449 100644 --- a/include/UECS/detail/RTSCmptTraits.h +++ b/include/UECS/detail/ArchetypeCmptTraits.h @@ -8,7 +8,7 @@ namespace Ubpa::UECS { // run-time static component traits - class RTSCmptTraits { + class ArchetypeCmptTraits { public: size_t Sizeof(CmptType) const; size_t Alignof(CmptType) const; @@ -38,4 +38,4 @@ namespace Ubpa::UECS { }; } -#include "RTSCmptTraits.inl" +#include "ArchetypeCmptTraits.inl" diff --git a/include/UECS/detail/RTSCmptTraits.inl b/include/UECS/detail/ArchetypeCmptTraits.inl similarity index 77% rename from include/UECS/detail/RTSCmptTraits.inl rename to include/UECS/detail/ArchetypeCmptTraits.inl index dde503b..30629d2 100644 --- a/include/UECS/detail/RTSCmptTraits.inl +++ b/include/UECS/detail/ArchetypeCmptTraits.inl @@ -3,17 +3,19 @@ #include namespace Ubpa::UECS { - inline size_t RTSCmptTraits::Sizeof(CmptType type) const { - assert(sizeofs.find(type) != sizeofs.end()); - return sizeofs.find(type)->second; + inline size_t ArchetypeCmptTraits::Sizeof(CmptType type) const { + auto target = sizeofs.find(type); + assert(target != sizeofs.end()); + return target->second; } - inline size_t RTSCmptTraits::Alignof(CmptType type) const { - assert(alignments.find(type) != alignments.end()); - return alignments.find(type)->second; + inline size_t ArchetypeCmptTraits::Alignof(CmptType type) const { + auto target = alignments.find(type); + assert(target != alignments.end()); + return target->second; } - inline void RTSCmptTraits::CopyConstruct(CmptType type, void* dst, void* src) const { + inline void ArchetypeCmptTraits::CopyConstruct(CmptType type, void* dst, void* src) const { auto target = copy_constructors.find(type); if (target != copy_constructors.end()) @@ -22,7 +24,7 @@ namespace Ubpa::UECS { memcpy(dst, src, Sizeof(type)); } - inline void RTSCmptTraits::MoveConstruct(CmptType type, void* dst, void* src) const { + inline void ArchetypeCmptTraits::MoveConstruct(CmptType type, void* dst, void* src) const { auto target = move_constructors.find(type); if (target != move_constructors.end()) @@ -31,7 +33,7 @@ namespace Ubpa::UECS { memcpy(dst, src, Sizeof(type)); } - inline void RTSCmptTraits::MoveAssign(CmptType type, void* dst, void* src) const { + inline void ArchetypeCmptTraits::MoveAssign(CmptType type, void* dst, void* src) const { auto target = move_assignments.find(type); if (target != move_assignments.end()) @@ -40,14 +42,14 @@ namespace Ubpa::UECS { memcpy(dst, src, Sizeof(type)); } - inline void RTSCmptTraits::Destruct(CmptType type, void* cmpt) const { + inline void ArchetypeCmptTraits::Destruct(CmptType type, void* cmpt) const { auto target = destructors.find(type); if (target != destructors.end()) target->second(cmpt); } template - void RTSCmptTraits::Register() { + void ArchetypeCmptTraits::Register() { static_assert(!IsTaggedCmpt_v, " should not be tagged"); static_assert(std::is_copy_constructible_v, " must be copy-constructible"); static_assert(std::is_move_constructible_v, " must be move-constructible"); @@ -82,7 +84,7 @@ namespace Ubpa::UECS { } template - void RTSCmptTraits::Deregister() { + void ArchetypeCmptTraits::Deregister() { constexpr CmptType type = CmptType::Of; sizeofs.erase(type); @@ -98,16 +100,14 @@ namespace Ubpa::UECS { move_assignments.erase(type); } - inline void RTSCmptTraits::Register(const RTDCmptTraits& rtdct, CmptType type) { + inline void ArchetypeCmptTraits::Register(const RTDCmptTraits& rtdct, CmptType type) { auto size_target = rtdct.GetSizeofs().find(type); if (size_target == rtdct.GetSizeofs().end()) - throw std::logic_error("RTSCmptTraits::Register: RTDCmptTraits hasn't registered "); + throw std::logic_error("ArchetypeCmptTraits::Register: RTDCmptTraits hasn't registered "); sizeofs[type] = size_target->second; auto alignment_target = rtdct.GetAlignments().find(type); - if (alignment_target == rtdct.GetAlignments().end()) - alignments[type] = RTDCmptTraits::default_alignment; - else + if (alignment_target != rtdct.GetAlignments().end()) alignments[type] = alignment_target->second; auto destructor_target = rtdct.GetDestructors().find(type); @@ -125,7 +125,7 @@ namespace Ubpa::UECS { move_assignments.emplace(type, move_assignments_target->second); } - inline void RTSCmptTraits::Deregister(CmptType type) noexcept { + inline void ArchetypeCmptTraits::Deregister(CmptType type) noexcept { sizeofs.erase(type); alignments.erase(type); copy_constructors.erase(type); diff --git a/include/UECS/detail/EntityMngr.inl b/include/UECS/detail/EntityMngr.inl index e8a08ee..d261f9f 100644 --- a/include/UECS/detail/EntityMngr.inl +++ b/include/UECS/detail/EntityMngr.inl @@ -68,7 +68,7 @@ namespace Ubpa::UECS { size_t srcIdxInArchetype = info.idxInArchetype; size_t dstIdxInArchetype = dstArchetype->RequestBuffer(); - auto srcCmptTraits = srcArchetype->GetRTSCmptTraits(); + auto srcCmptTraits = srcArchetype->GetArchetypeCmptTraits(); for (const auto& type : srcCmptTypeSet.data) { auto srcCmpt = srcArchetype->At(type, srcIdxInArchetype); auto dstCmpt = dstArchetype->At(type, dstIdxInArchetype); diff --git a/include/UECS/detail/RTDCmptTraits.inl b/include/UECS/detail/RTDCmptTraits.inl index 0e72f66..64be9be 100644 --- a/include/UECS/detail/RTDCmptTraits.inl +++ b/include/UECS/detail/RTDCmptTraits.inl @@ -51,7 +51,7 @@ namespace Ubpa::UECS { inline size_t RTDCmptTraits::Alignof(CmptType type) const { auto target = alignments.find(type); - return target != alignments.end() ? target->second : default_alignment; + return target != alignments.end() ? target->second : DefaultAlignment(); } inline void RTDCmptTraits::DefaultConstruct(CmptType type, void* cmpt) const { diff --git a/include/UECS/SystemFunc.h b/include/UECS/detail/SystemFunc.h similarity index 91% rename from include/UECS/SystemFunc.h rename to include/UECS/detail/SystemFunc.h index 122ce95..0ee9522 100644 --- a/include/UECS/SystemFunc.h +++ b/include/UECS/detail/SystemFunc.h @@ -1,11 +1,11 @@ #pragma once -#include "EntityQuery.h" -#include "SingletonLocator.h" -#include "Entity.h" -#include "CmptsView.h" -#include "SingletonsView.h" -#include "ChunkView.h" +#include "../EntityQuery.h" +#include "../SingletonLocator.h" +#include "../Entity.h" +#include "../CmptsView.h" +#include "../SingletonsView.h" +#include "../ChunkView.h" #include @@ -73,4 +73,4 @@ namespace Ubpa::UECS { }; } -#include "detail/SystemFunc.inl" +#include "SystemFunc.inl" diff --git a/src/core/EntityMngr.cpp b/src/core/EntityMngr.cpp index 82badd0..0292f98 100644 --- a/src/core/EntityMngr.cpp +++ b/src/core/EntityMngr.cpp @@ -1,25 +1,18 @@ #include +#include #include #include using namespace Ubpa::UECS; using namespace std; -size_t EntityMngr::RequestEntityFreeEntry() { - if (entityTableFreeEntry.empty()) { - size_t index = entityTable.size(); - entityTable.emplace_back(); - return index; - } - - size_t entry = entityTableFreeEntry.back(); - entityTableFreeEntry.pop_back(); - return entry; -} +EntityMngr::EntityMngr() + : sharedChunkPool{ std::make_unique>() } +{} EntityMngr::EntityMngr(const EntityMngr& em) - : world{ em.world }, + : sharedChunkPool{ std::make_unique>() } { ts2a.reserve(em.ts2a.size()); @@ -50,12 +43,23 @@ EntityMngr::~EntityMngr() { sharedChunkPool->FastClear(); } -void EntityMngr::Swap(EntityMngr& rhs) noexcept { - assert(world == rhs.world); +size_t EntityMngr::RequestEntityFreeEntry() { + if (entityTableFreeEntry.empty()) { + size_t index = entityTable.size(); + entityTable.emplace_back(); + return index; + } + size_t entry = entityTableFreeEntry.back(); + entityTableFreeEntry.pop_back(); + return entry; +} + +void EntityMngr::Swap(EntityMngr& rhs) noexcept { using std::swap; swap(ts2a, rhs.ts2a); + swap(cmptTraits, rhs.cmptTraits); swap(entityTableFreeEntry, rhs.entityTableFreeEntry); swap(entityTable, rhs.entityTable); swap(queryCache, rhs.queryCache); @@ -79,7 +83,7 @@ Archetype* EntityMngr::GetOrCreateArchetypeOf(const CmptType* types, size_t num) if (target != ts2a.end()) return target->second.get(); - auto archetype = Archetype::New(world->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) { @@ -96,7 +100,7 @@ Entity EntityMngr::Create(const CmptType* types, size_t num) { EntityInfo& info = entityTable[entityIndex]; Entity e{ entityIndex, info.version }; info.archetype = archetype; - info.idxInArchetype = archetype->Create(world->cmptTraits, e); + info.idxInArchetype = archetype->Create(cmptTraits, e); return e; } @@ -117,7 +121,7 @@ Archetype* EntityMngr::AttachWithoutInit(Entity e, const CmptType* types, size_t Archetype* dstArchetype; auto target = ts2a.find(dstCmptTypeSet); if (target == ts2a.end()) { - dstArchetype = Archetype::Add(world->cmptTraits, srcArchetype, types, num); + dstArchetype = Archetype::Add(cmptTraits, srcArchetype, types, num); assert(dstCmptTypeSet == dstArchetype->GetCmptTypeSet()); for (auto& [query, archetypes] : queryCache) { if (dstCmptTypeSet.IsMatch(query)) @@ -134,7 +138,7 @@ Archetype* EntityMngr::AttachWithoutInit(Entity e, const CmptType* types, size_t // move src to dst size_t dstIdxInArchetype = dstArchetype->RequestBuffer(); - const auto& srcCmptTraits = srcArchetype->GetRTSCmptTraits(); + const auto& srcCmptTraits = srcArchetype->GetArchetypeCmptTraits(); for (const auto& type : srcCmptTypeSet.data) { auto srcCmpt = srcArchetype->At(type, srcIdxInArchetype); auto dstCmpt = dstArchetype->At(type, dstIdxInArchetype); @@ -160,8 +164,8 @@ void EntityMngr::Attach(Entity e, const CmptType* types, size_t num) { if (origArchetype->GetCmptTypeSet().Contains(type)) continue; - auto target = world->cmptTraits.GetDefaultConstructors().find(type); - if (target == world->cmptTraits.GetDefaultConstructors().end()) + auto target = cmptTraits.GetDefaultConstructors().find(type); + if (target == cmptTraits.GetDefaultConstructors().end()) continue; target->second(info.archetype->At(type, info.idxInArchetype)); @@ -204,7 +208,7 @@ void EntityMngr::Detach(Entity e, const CmptType* types, size_t num) { size_t srcIdxInArchetype = info.idxInArchetype; size_t dstIdxInArchetype = dstArchetype->RequestBuffer(); - const auto& srcCmptTraits = srcArchetype->GetRTSCmptTraits(); + const auto& srcCmptTraits = srcArchetype->GetArchetypeCmptTraits(); for (const auto& type : dstCmptTypeSet.data) { auto srcCmpt = srcArchetype->At(type, srcIdxInArchetype); auto dstCmpt = dstArchetype->At(type, dstIdxInArchetype); diff --git a/src/core/SystemFunc.cpp b/src/core/SystemFunc.cpp index cc01d90..3fb278c 100644 --- a/src/core/SystemFunc.cpp +++ b/src/core/SystemFunc.cpp @@ -1,4 +1,4 @@ -#include +#include using namespace Ubpa::UECS; diff --git a/src/core/World.cpp b/src/core/World.cpp index 7e6c0ef..422ea5a 100644 --- a/src/core/World.cpp +++ b/src/core/World.cpp @@ -6,7 +6,7 @@ using namespace Ubpa::UECS; using namespace Ubpa; using namespace std; -World::World() : systemMngr{ this }, entityMngr{ this } {} +World::World() : systemMngr{ this } {} void World::Update() { inRunningJobGraph = true; @@ -121,7 +121,7 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const { unordered_map sysFuncHashcode2idx; auto queryCmptName = [this](CmptType type) -> string { - auto cmptName = cmptTraits.Nameof(type); + 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 e8ff2c6..b8946c3 100644 --- a/src/test/02_order/main.cpp +++ b/src/test/02_order/main.cpp @@ -28,7 +28,7 @@ int main() { World w; w.systemMngr.Register(); - w.cmptTraits.Register< + w.entityMngr.cmptTraits.Register< Data1, Data2 >(); diff --git a/src/test/11_runtime_cmpt/main.cpp b/src/test/11_runtime_cmpt/main.cpp index efe4195..51e4b95 100644 --- a/src/test/11_runtime_cmpt/main.cpp +++ b/src/test/11_runtime_cmpt/main.cpp @@ -47,7 +47,7 @@ int main() { World w; w.systemMngr.Register(); - w.cmptTraits + w.entityMngr.cmptTraits .RegisterSize(type, 8) .RegisterDefaultConstructor(type, [](void*) { cout << "construct" << endl; }) .RegisterDestructor(type, [](void*) { cout << "destruct" << endl; }); diff --git a/src/test/12_framegraph/main.cpp b/src/test/12_framegraph/main.cpp index 85da3c1..8952c3b 100644 --- a/src/test/12_framegraph/main.cpp +++ b/src/test/12_framegraph/main.cpp @@ -37,7 +37,7 @@ int main() { World w; w.systemMngr.Register(); - w.cmptTraits + 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 aae5e70..24d3541 100644 --- a/src/test/14_serialize/main.cpp +++ b/src/test/14_serialize/main.cpp @@ -130,7 +130,7 @@ class Dumper : public IListener { cout << "{" << endl; indent++; PrintIndent(); - cout << "\"type\" : \"" << w->cmptTraits.Nameof(cmpt->Type()) << "\""; + cout << "\"type\" : \"" << w->entityMngr.cmptTraits.Nameof(cmpt->Type()) << "\""; if (cmpt->Type().Is()) { auto v = cmpt->As(); cout << "," << endl; @@ -167,7 +167,7 @@ class MoverSystem : public System { int main() { World w; - w.cmptTraits.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 dc3485e..af971fc 100644 --- a/src/test/15_chunk_job/main.cpp +++ b/src/test/15_chunk_job/main.cpp @@ -55,7 +55,7 @@ int main() { World w; w.systemMngr.Register(); - w.cmptTraits.Register(); + w.entityMngr.cmptTraits.Register(); w.entityMngr.Create(); w.entityMngr.Create(); diff --git a/src/test/16_singleton/main.cpp b/src/test/16_singleton/main.cpp index 6fc530d..95e3420 100644 --- a/src/test/16_singleton/main.cpp +++ b/src/test/16_singleton/main.cpp @@ -33,7 +33,7 @@ int main() { w.systemMngr.Register(); w.entityMngr.Create(); w.entityMngr.Create(); - w.cmptTraits.Register + w.entityMngr.cmptTraits.Register (); w.Update(); diff --git a/src/test/17_serial/main.cpp b/src/test/17_serial/main.cpp index f70767a..e3d99b8 100644 --- a/src/test/17_serial/main.cpp +++ b/src/test/17_serial/main.cpp @@ -34,7 +34,7 @@ int main() { w.entityMngr.Create(); w.entityMngr.Create(); w.entityMngr.Create(); - w.cmptTraits.Register + w.entityMngr.cmptTraits.Register (); for (size_t i = 0; i < 5; i++) {