diff --git a/doc/todo.md b/doc/todo.md index 064e167..49e468a 100644 --- a/doc/todo.md +++ b/doc/todo.md @@ -31,6 +31,7 @@ - [ ] batch create/instantiate (need benchmark) - [x] lock `FilterChange` - [ ] `EntityMngr` `Query`-driven API +- [ ] `RTDCmpts` = `const EntityLocator* locator + void** cmpts` ### maybe deprecate diff --git a/include/UECS/SystemMngr.h b/include/UECS/SystemMngr.h index 2e04781..935d59b 100644 --- a/include/UECS/SystemMngr.h +++ b/include/UECS/SystemMngr.h @@ -2,28 +2,39 @@ #include "Schedule.h" +#include + namespace Ubpa{ // System Manager // System is a struct with specific function // signature: static void OnUpdate(Schedule&) class SystemMngr { public: + void Register(std::string name, std::function onUpdate) { + onUpdateMap.emplace(std::move(name), std::move(onUpdate)); + } + bool IsRegister(std::string_view name) const { + return onUpdateMap.find(name) != onUpdateMap.end(); + } + void Deregister(const std::string& name) { + onUpdateMap.erase(name); + } + template void Register(); template bool IsRegistered() const; - template + template void Deregister() noexcept; + private: template void RegisterOne(); + template + void DeregisterOne(); - struct SystemLifecycle { - void(*OnUpdate)(Schedule&); - }; - - std::unordered_map lifecycleMap; + xMap> onUpdateMap; friend class World; }; diff --git a/include/UECS/detail/SystemMngr.inl b/include/UECS/detail/SystemMngr.inl index 595a587..908357f 100644 --- a/include/UECS/detail/SystemMngr.inl +++ b/include/UECS/detail/SystemMngr.inl @@ -3,7 +3,7 @@ namespace Ubpa { template void SystemMngr::RegisterOne() { - lifecycleMap.emplace(TypeID, SystemLifecycle{ &System::OnUpdate }); + Register(std::string{ nameof::nameof_type() }, &System::OnUpdate); } template @@ -13,11 +13,16 @@ namespace Ubpa { template bool SystemMngr::IsRegistered() const { - return lifecycleMap.find(TypeID) != lifecycleMap.end(); + return IsRegistered(nameof::nameof_type()); } template + void DeregisterOne() { + Deregister(std::string{ nameof::nameof_type() }); + } + + template void SystemMngr::Deregister() noexcept { - lifecycleMap.erase(TypeID); + (DeregisterOne(), ...); } } diff --git a/src/core/World.cpp b/src/core/World.cpp index efd60a8..55b1148 100644 --- a/src/core/World.cpp +++ b/src/core/World.cpp @@ -10,8 +10,8 @@ void World::Update() { jobs.clear(); jobGraph.clear(); - for (const auto& [id, lifecycle] : systemMngr.lifecycleMap) - lifecycle.OnUpdate(schedule); + for (const auto& [id, onUpdate] : systemMngr.onUpdateMap) + onUpdate(schedule); auto graph = schedule.GenSysFuncGraph(); unordered_map table;