Skip to content

Commit

Permalink
* SystemMngr
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed May 24, 2020
1 parent 65aaf0a commit 3ec65d8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [ ] batch create/instantiate (need benchmark)
- [x] lock `FilterChange`
- [ ] `EntityMngr` `Query`-driven API
- [ ] `RTDCmpts` = `const EntityLocator* locator + void** cmpts`

### maybe deprecate

Expand Down
23 changes: 17 additions & 6 deletions include/UECS/SystemMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@

#include "Schedule.h"

#include <UContainer/xSTL/xMap.h>

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<void(Schedule&)> 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<typename... Systems>
void Register();
template<typename System>
bool IsRegistered() const;
template<typename System>
template<typename... Systems>
void Deregister() noexcept;


private:
template<typename System>
void RegisterOne();
template<typename System>
void DeregisterOne();

struct SystemLifecycle {
void(*OnUpdate)(Schedule&);
};

std::unordered_map<size_t, SystemLifecycle> lifecycleMap;
xMap<std::string, std::function<void(Schedule&)>> onUpdateMap;

friend class World;
};
Expand Down
11 changes: 8 additions & 3 deletions include/UECS/detail/SystemMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Ubpa {
template<typename System>
void SystemMngr::RegisterOne() {
lifecycleMap.emplace(TypeID<System>, SystemLifecycle{ &System::OnUpdate });
Register(std::string{ nameof::nameof_type<System>() }, &System::OnUpdate);
}

template<typename... Systems>
Expand All @@ -13,11 +13,16 @@ namespace Ubpa {

template<typename System>
bool SystemMngr::IsRegistered() const {
return lifecycleMap.find(TypeID<System>) != lifecycleMap.end();
return IsRegistered(nameof::nameof_type<System>());
}

template<typename System>
void DeregisterOne() {
Deregister(std::string{ nameof::nameof_type<System>() });
}

template<typename... Systems>
void SystemMngr::Deregister() noexcept {
lifecycleMap.erase(TypeID<System>);
(DeregisterOne<Systems>(), ...);
}
}
4 changes: 2 additions & 2 deletions src/core/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SystemFunc*, JobHandle> table;
Expand Down

0 comments on commit 3ec65d8

Please sign in to comment.