diff --git a/doc/todo.md b/doc/todo.md index a72a15b..6ffc853 100644 --- a/doc/todo.md +++ b/doc/todo.md @@ -31,6 +31,10 @@ - [x] random access - [x] system traits - [ ] change filter +- [ ] resource + - [ ] world + - [ ] frame +- [ ] view + iterator ### maybe support in future diff --git a/include/UECS/Schedule.h b/include/UECS/Schedule.h index e39800a..36f975e 100644 --- a/include/UECS/Schedule.h +++ b/include/UECS/Schedule.h @@ -25,10 +25,6 @@ namespace Ubpa::UECS { // schedule will be clear at the beginning of the next World::Update() class Schedule { public: - Schedule() : - rsrc{std::make_unique()}, - sysFuncAllocator{ rsrc.get() } {} - // Func's argument list: // [const] World* // {LastFrame|Latest}> @@ -115,8 +111,8 @@ namespace Ubpa::UECS { std::map>> commandBuffer; - std::unique_ptr rsrc; - std::pmr::polymorphic_allocator sysFuncAllocator; + std::pmr::unsynchronized_pool_resource sysfuncRsrc; + std::pmr::polymorphic_allocator GetSysFuncAllocator(); friend class World; }; } diff --git a/include/UECS/SystemTraits.h b/include/UECS/SystemTraits.h index 1ec1187..d802ddf 100644 --- a/include/UECS/SystemTraits.h +++ b/include/UECS/SystemTraits.h @@ -48,6 +48,7 @@ namespace Ubpa::UECS { // register system's name and get an ID // if it is already registered, return it's ID directly Name Register(std::string_view name); + void Unregister(NameID); // ID must exist void RegisterOnCreate (NameID, std::function ); diff --git a/include/UECS/details/Schedule.inl b/include/UECS/details/Schedule.inl index 6bff053..35e4346 100644 --- a/include/UECS/details/Schedule.inl +++ b/include/UECS/details/Schedule.inl @@ -58,7 +58,7 @@ namespace Ubpa::UECS { template const SystemFunc* Schedule::Request(Args&&... args) { - SystemFunc* sysFunc = sysFuncAllocator.allocate(1); + SystemFunc* sysFunc = GetSysFuncAllocator().allocate(1); new(sysFunc)SystemFunc(std::forward(args)...); sysFuncs.emplace(sysFunc->GetValue(), sysFunc); return sysFunc; diff --git a/src/core/Schedule.cpp b/src/core/Schedule.cpp index 8ce4936..127497f 100644 --- a/src/core/Schedule.cpp +++ b/src/core/Schedule.cpp @@ -6,6 +6,10 @@ using namespace Ubpa; using namespace Ubpa::UECS; using namespace std; +std::pmr::polymorphic_allocator Schedule::GetSysFuncAllocator() { + return &sysfuncRsrc; +} + Schedule& Schedule::Order(string_view x, string_view y) { sysFuncOrder.emplace(SystemFunc::GetValue(x), SystemFunc::GetValue(y)); return *this; @@ -28,9 +32,10 @@ Schedule& Schedule::EraseNone(string_view sys, TypeID type) { } void Schedule::Clear() { + auto alloc = GetSysFuncAllocator(); for (const auto& [hash, sysFunc] : sysFuncs) { sysFunc->~SystemFunc(); - sysFuncAllocator.deallocate(sysFunc, 1); + alloc.deallocate(sysFunc, 1); } sysFuncs.clear(); sysFuncOrder.clear(); diff --git a/src/core/SystemTraits.cpp b/src/core/SystemTraits.cpp index db4d359..bbc4656 100644 --- a/src/core/SystemTraits.cpp +++ b/src/core/SystemTraits.cpp @@ -54,6 +54,19 @@ Name SystemTraits::Register(std::string_view name) { return { newname, id }; } +void SystemTraits::Unregister(NameID ID) { + auto target = names.find(ID); + if (target == names.end()) + return; + + names.erase(target); + createMap.erase(ID); + activateMap.erase(ID); + updateMap.erase(ID); + deactivateMap.erase(ID); + destroyMap.erase(ID); +} + bool SystemTraits::IsRegistered(NameID ID) const noexcept { return names.contains(ID); }