Skip to content

Commit

Permalink
Add some template API for SystemTraits and SystemMngr
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Nov 17, 2020
1 parent febfaea commit 547c0d3
Show file tree
Hide file tree
Showing 30 changed files with 137 additions and 76 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Ubpa_InitUCMake()

Ubpa_InitProject()

Ubpa_AddDep(UContainer 0.0.8)
Ubpa_AddDep(UContainer 0.0.9)
Ubpa_AddDep(UTemplate 0.4.9)
Ubpa_AddDep(UGraphviz 0.1.6)

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ struct MoverSystem {

int main() {
World w;
auto move = w.systemMngr.systemTraits.Register<MoverSystem>();
w.entityMngr.Create<Position, Velocity>();
w.systemMngr.Activate(move);
w.Update();
w.systemMngr.RegisterAndActivate<MoverSystem>();
w.entityMngr.Create<Position, Velocity>();
w.Update();
}
```
Expand Down
7 changes: 5 additions & 2 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

- 0.14.4: EntityMngr add single `CmptType` API
- 0.14.4
- EntityMngr add single `CmptType` API
- Add some template API for `SystemTraits` and `SystemMngr`
- 0.14.3
- `CmptsView`, `SingletonsView` use `Span`
- API with `const CmptType* types, size_t num` use `Span<CmptType> types` as instead
Expand All @@ -14,4 +16,5 @@
- `CmptAccessType`'s default `AccessMode` change from `LATEST` to `WRITE`
- `World` command buffer layer's type change from `size_t` to `int`
- 0.14.1: `CmptAccessMode` remove singleton
- 0.14.0: System Lifecycle
- 0.14.0: System Lifecycle

2 changes: 1 addition & 1 deletion include/UECS/CmptType.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Ubpa::UECS {
constexpr CmptType GetCmptType() const noexcept { return type; }
constexpr AccessMode GetAccessMode() const noexcept { return mode; }

constexpr operator CmptType()const noexcept { return type; }
constexpr operator CmptType() const noexcept { return type; }

static constexpr CmptAccessType Invalid() noexcept { return CmptAccessType{ static_cast<size_t>(-1), AccessMode{} }; }
constexpr bool Valid() const noexcept { return type.Valid(); }
Expand Down
4 changes: 3 additions & 1 deletion include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace Ubpa::UECS {

std::vector<CmptPtr> Components(Entity) const;

bool Exist(Entity) const;
bool Exist(Entity) const noexcept;

void Destroy(Entity);

Expand All @@ -96,6 +96,8 @@ namespace Ubpa::UECS {

void Accept(IListener* listener) const;

EntityMngr& operator=(EntityMngr&&) noexcept = delete;
EntityMngr& operator=(const EntityMngr&) = delete;
private:
friend class World;
friend class Archetype;
Expand Down
35 changes: 32 additions & 3 deletions include/UECS/SystemMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ namespace Ubpa::UECS {
public:
SystemTraits systemTraits;

SystemMngr(World* w) : w{w}{}
SystemMngr(World* w) : w{w} {}
SystemMngr(const SystemMngr& mngr, World* w);
SystemMngr(SystemMngr&& mngr, World* w) noexcept;
~SystemMngr();

const auto& GetAliveSystemIDs() const noexcept { return aliveSystemIDs; }
const auto& GetActiveSystemsIDs() const noexcept { return activeSystemIDs; }

// not alive -> create
void Create(size_t systemID);

Expand All @@ -33,8 +36,32 @@ namespace Ubpa::UECS {
bool IsAlive(size_t systemID) const;
bool IsActive(size_t systemID) const;

const auto& GetAliveSystemIDs() const noexcept { return aliveSystemIDs; }
const auto& GetActiveSystemsIDs() const noexcept { return activeSystemIDs; }
// [ Template ] Functions
///////////////////////////

template<typename... Systems>
void Create() { (Create(systemTraits.GetID<Systems>()), ...); }

template<typename... Systems>
void Activate() { (Activate(systemTraits.GetID<Systems>()), ...); }

template<typename... Systems>
void Deactivate() { (Deactivate(systemTraits.GetID<Systems>()), ...); }

template<typename... Systems>
void Destroy() { (Destroy(systemTraits.GetID<Systems>()), ...); }

template<typename System>
bool IsAlive() const { return IsAlive(systemTraits.GetID<System>()); }

template<typename System>
bool IsActive() const { return IsActive(systemTraits.GetID<System>()); }

template<typename... Systems>
std::array<size_t, sizeof...(Systems)> RegisterAndCreate();

template<typename... Systems>
std::array<size_t, sizeof...(Systems)> RegisterAndActivate();

SystemMngr(const SystemMngr&) = delete;
SystemMngr(SystemMngr&&) noexcept = delete;
Expand All @@ -51,3 +78,5 @@ namespace Ubpa::UECS {
std::unordered_set<size_t> activeSystemIDs;
};
}

#include "detail/SystemMngr.inl"
41 changes: 30 additions & 11 deletions include/UECS/SystemTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,51 @@ namespace Ubpa::UECS {
SystemTraits() = default;
SystemTraits(const SystemTraits&);
SystemTraits(SystemTraits&&) noexcept = default;
SystemTraits& operator=(SystemTraits&);
SystemTraits& operator=(SystemTraits&&) noexcept = default;

// register system's name and get an ID
// if it is already registered, return it's ID directly
size_t Register(std::string name);
void RegisterOnCreate(size_t ID, std::function<OnCreate>);
void RegisterOnActivate(size_t ID, std::function<OnActivate>);
void RegisterOnUpdate(size_t ID, std::function<OnUpdate>);

// ID must exist
void RegisterOnCreate (size_t ID, std::function<OnCreate>);
void RegisterOnActivate (size_t ID, std::function<OnActivate>);
void RegisterOnUpdate (size_t ID, std::function<OnUpdate>);
void RegisterOnDeactivate(size_t ID, std::function<OnDeactivate>);
void RegisterOnDestroy(size_t ID, std::function<OnDestroy>);
void RegisterOnDestroy (size_t ID, std::function<OnDestroy>);

bool IsRegistered(size_t ID) const noexcept;
size_t GetID(std::string_view name) const;
std::string_view Nameof(size_t ID) const noexcept;
size_t GetID(std::string_view name) const;
bool IsRegistered(size_t ID) const noexcept;
const auto& GetNameIDMap() const noexcept { return name2id; }

// [ Template ] functions
///////////////////////////

template<typename System>
static std::string_view StaticNameof() noexcept;

// for each <System> in <Systems...>
// 1. Register(StaticNameof<System>())
// 2. RegisterOn{Create|Activate|Update|Deactivate|Destroy} if <System> has them
template<typename... Systems>
std::array<size_t, sizeof...(Systems)> Register();

template<typename System>
static std::string_view StaticNameof() noexcept;
size_t GetID() const { return GetID(StaticNameof<System>()); }

template<typename System>
bool IsRegistered() const { return GetID<System>(); }

private:
friend class SystemMngr;

void Create(size_t ID, World*) const;
void Activate(size_t ID, World*) const;
void Update(size_t ID, Schedule&) const;
void Create (size_t ID, World*) const;
void Activate (size_t ID, World*) const;
void Update (size_t ID, Schedule&) const;
void Deactivate(size_t ID, World*) const;
void Destroy(size_t ID, World*) const;
void Destroy (size_t ID, World*) const;

std::vector<std::string> names;
std::unordered_map<std::string_view, size_t> name2id;
Expand Down
9 changes: 2 additions & 7 deletions include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Ubpa::UECS {
class World {
public:
World() : systemMngr{ this }{}
// not copy schedule, so you can't use DumpUpdateJobGraph() and GenUpdateFrameGraph() before Update()
// not copy/move schedule, so you can't use DumpUpdateJobGraph() and GenUpdateFrameGraph() before Update()
World(const World&);
World(World&&) noexcept;
~World();
Expand Down Expand Up @@ -109,6 +109,7 @@ namespace Ubpa::UECS {
bool isParallel = true,
SingletonLocator = {}
) const;

private:
bool inRunningJobGraph{ false };

Expand All @@ -125,12 +126,6 @@ namespace Ubpa::UECS {
void RunCommands();

void Run(SystemFunc*);

// ==================================================
//World(const World& world) = delete;
//World(World&& world) = delete;
//World& operator==(World&& world) = delete;
//World& operator=(const World& world) = delete;
};
}

Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/EntityMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ namespace Ubpa::UECS {
return { type, info.archetype->At(type, info.idxInArchetype) };
}

inline bool EntityMngr::Exist(Entity e) const {
inline bool EntityMngr::Exist(Entity e) const noexcept {
return e.Idx() < entityTable.size() && e.Version() == entityTable[e.Idx()].version;
}

Expand Down
21 changes: 21 additions & 0 deletions include/UECS/detail/SystemMngr.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

namespace Ubpa::UECS {
template<typename... Systems>
std::array<size_t, sizeof...(Systems)> SystemMngr::RegisterAndCreate() {
std::array<size_t, sizeof...(Systems)> systemIDs = systemTraits.Register<Systems...>();
std::apply([this](auto... IDs) {
(Create(IDs), ...);
}, systemIDs);
return systemIDs;
}

template<typename... Systems>
std::array<size_t, sizeof...(Systems)> SystemMngr::RegisterAndActivate() {
std::array<size_t, sizeof...(Systems)> systemIDs = systemTraits.Register<Systems...>();
std::apply([this](auto... IDs) {
(Activate(IDs), ...);
}, systemIDs);
return systemIDs;
}
}
24 changes: 19 additions & 5 deletions src/core/SystemTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ SystemTraits::SystemTraits(const SystemTraits& traits)
}
}

SystemTraits& SystemTraits::operator=(SystemTraits& rhs) {
names = rhs.names;
createMap = rhs.createMap;
activateMap = rhs.activateMap;
updateMap = rhs.updateMap;
deactivateMap = rhs.deactivateMap;
destroyMap = rhs.destroyMap;
for (size_t i = 0; i < names.size(); i++) {
const auto& name = names[i];
name2id.emplace(std::string_view{ name }, i);
}
return *this;
}

size_t SystemTraits::Register(std::string name) {
assert(!name.empty());

Expand All @@ -43,27 +57,27 @@ size_t SystemTraits::GetID(std::string_view name) const {

void SystemTraits::RegisterOnCreate(size_t ID, std::function<OnCreate> func) {
assert(IsRegistered(ID));
createMap.emplace(ID, std::move(func));
createMap[ID] = std::move(func);
}

void SystemTraits::RegisterOnActivate(size_t ID, std::function<OnActivate> func) {
assert(IsRegistered(ID));
activateMap.emplace(ID, std::move(func));
activateMap[ID] = std::move(func);
}

void SystemTraits::RegisterOnUpdate(size_t ID, std::function<OnUpdate> func) {
assert(IsRegistered(ID));
updateMap.emplace(ID, std::move(func));
updateMap[ID] = std::move(func);
}

void SystemTraits::RegisterOnDeactivate(size_t ID, std::function<OnDeactivate> func) {
assert(IsRegistered(ID));
deactivateMap.emplace(ID, std::move(func));
deactivateMap[ID] = std::move(func);
}

void SystemTraits::RegisterOnDestroy(size_t ID, std::function<OnDestroy> func) {
assert(IsRegistered(ID));
destroyMap.emplace(ID, std::move(func));
destroyMap[ID] = std::move(func);
}

std::string_view SystemTraits::Nameof(size_t ID) const noexcept {
Expand Down
3 changes: 1 addition & 2 deletions src/test/00_basic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ struct MoverSystem {

int main() {
World w;
auto [move] = w.systemMngr.systemTraits.Register<MoverSystem>();
w.systemMngr.RegisterAndActivate<MoverSystem>();
w.entityMngr.Create<Position, Velocity>();
w.systemMngr.Activate(move);
w.Update();
}
3 changes: 1 addition & 2 deletions src/test/01_tag/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ struct DataSystem {

int main() {
World w;
auto [dataSystem] = w.systemMngr.systemTraits.Register<DataSystem>();
w.systemMngr.RegisterAndActivate<DataSystem>();

w.entityMngr.Create<Data>();

w.systemMngr.Activate(dataSystem);
w.Update();

cout << w.DumpUpdateJobGraph() << endl;
Expand Down
3 changes: 1 addition & 2 deletions src/test/02_order/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct DataSystem {

int main() {
World w;
auto [dataSystem] = w.systemMngr.systemTraits.Register<DataSystem>();
w.systemMngr.RegisterAndActivate<DataSystem>();

w.entityMngr.cmptTraits.Register<
Data1,
Expand All @@ -32,7 +32,6 @@ int main() {

w.entityMngr.Create<Data1, Data2>();

w.systemMngr.Activate(dataSystem);
w.Update();

cout << w.DumpUpdateJobGraph() << endl;
Expand Down
3 changes: 1 addition & 2 deletions src/test/03_query_entity/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ struct MySystem {

int main() {
World w;
auto [mySystem] = w.systemMngr.systemTraits.Register<MySystem>();
w.systemMngr.RegisterAndActivate<MySystem>();

w.entityMngr.Create<A, B>();

w.systemMngr.Activate(mySystem);
w.Update();
w.Update();
w.Update();
Expand Down
3 changes: 1 addition & 2 deletions src/test/04_filter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct MySystem {

int main() {
World w;
auto [mySystem] = w.systemMngr.systemTraits.Register<MySystem>();
w.systemMngr.RegisterAndActivate<MySystem>();

auto [e0, b0 ] = w.entityMngr.Create< B >(); // x
auto [e1, a1 ] = w.entityMngr.Create<A >(); // x
Expand All @@ -44,7 +44,6 @@ int main() {
w.entityMngr.Emplace<E>(e2, 2.f);
w.entityMngr.Emplace<E>(e3, 3.f);

w.systemMngr.Activate(mySystem);
w.Update();

cout << w.DumpUpdateJobGraph() << endl;
Expand Down
3 changes: 1 addition & 2 deletions src/test/06_none_parallel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ struct MySystem {

int main() {
World w;
auto [mySystem] = w.systemMngr.systemTraits.Register<MySystem>();
w.systemMngr.RegisterAndActivate<MySystem>();
w.entityMngr.Create<>();
w.entityMngr.Create<A>();
w.entityMngr.Create<B>();
w.entityMngr.Create<A, B>();

w.systemMngr.Activate(mySystem);
w.Update();

cout << w.DumpUpdateJobGraph() << endl;
Expand Down
4 changes: 1 addition & 3 deletions src/test/07_overload/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ struct AVP_System {

int main() {
World w;
auto systemIndices = w.systemMngr.systemTraits.Register<VP_System, AVP_System>();
for (auto idx : systemIndices)
w.systemMngr.Activate(idx);
w.systemMngr.RegisterAndActivate<VP_System, AVP_System>();

w.entityMngr.Create<V, P>();
w.entityMngr.Create<A, V, P>();
Expand Down
Loading

0 comments on commit 547c0d3

Please sign in to comment.