Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Nov 14, 2020
1 parent 87ab2f1 commit 2aa59cf
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 99 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Ubpa_InitProject()

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

Ubpa_AddSubDirsRec(include)
Ubpa_AddSubDirsRec(src)
Expand Down
17 changes: 8 additions & 9 deletions include/UECS/ChunkView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ namespace Ubpa::UECS {
class ChunkView {
public:
ChunkView(Archetype* archetype, size_t chunkIdx) noexcept;
ChunkView() noexcept = default;

bool Contains(CmptType) const;
size_t EntityNum() const noexcept { return entityNum; }

// nullptr if not contain
void* GetCmptArray(CmptType) const;
template<typename Cmpt>
Span<Cmpt> GetCmptArray() const {
return { static_cast<Cmpt*>(GetCmptArray(CmptType::Of<Cmpt>)), EntityNum() };
}
Span<const Entity> GetEntityArray() const {
return { static_cast<Entity*>(GetCmptArray(CmptType::Of<Entity>)), EntityNum() };
}
Span<Cmpt> GetCmptArray() const;
Span<const Entity> GetEntityArray() const;

private:
Archetype* archetype;
size_t chunkIdx;
size_t entityNum;
Archetype* archetype{ nullptr };
size_t chunkIdx{ static_cast<size_t>(-1) };
size_t entityNum{ 0 };
};
}

#include "detail/ChunkView.inl"
4 changes: 2 additions & 2 deletions include/UECS/CmptLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

#include "CmptType.h"

#include <set>
#include <UContainer/Span.h>

namespace Ubpa::UECS {
// locate components in function's argument list for Archetype
// immutable
class CmptLocator {
public:
CmptLocator(const CmptAccessType* types, size_t num);
CmptLocator(Span<const CmptAccessType> types);

CmptLocator();

Expand Down
6 changes: 0 additions & 6 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ namespace Ubpa::UECS {
return Write<Cmpt>{p};
else if constexpr (mode == AccessMode::LATEST)
return Latest<Cmpt>{p};
else if constexpr (mode == AccessMode::LAST_FRAME_SINGLETON)
return LastFrame<Singleton<Cmpt>>{p};
else if constexpr (mode == AccessMode::WRITE_SINGLETON)
return Write<Singleton<Cmpt>>{p};
else if constexpr (mode == AccessMode::LATEST_SINGLETON)
return Latest<Singleton<Cmpt>>{p};
else
static_assert(false);
}
Expand Down
1 change: 1 addition & 0 deletions include/UECS/CmptsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Ubpa::UECS {
class CmptsView {
public:
CmptsView() noexcept = default;
CmptsView(Span<const CmptAccessPtr> cmpts) noexcept : cmpts{ cmpts } {}

CmptAccessPtr GetCmpt(CmptAccessType) const noexcept;
Expand Down
30 changes: 15 additions & 15 deletions include/UECS/SystemTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ namespace Ubpa::UECS {
// |
// v
// OnDestroy
using OnCreate = std::function<void(World* )>;
using OnActivate = std::function<void(World* )>;
using OnUpdate = std::function<void(Schedule&)>;
using OnDeactivate = std::function<void(World* )>;
using OnDestroy = std::function<void(World* )>;
using OnCreate = void(World*);
using OnActivate = void(World*);
using OnUpdate = void(Schedule&);
using OnDeactivate = void(World*);
using OnDestroy = void(World*);

SystemTraits() = default;
SystemTraits(const SystemTraits&);
SystemTraits(SystemTraits&&) noexcept = default;

size_t Register(std::string name);
void RegisterOnCreate(size_t ID, OnCreate);
void RegisterOnActivate(size_t ID, OnActivate);
void RegisterOnUpdate(size_t ID, OnUpdate);
void RegisterOnDeactivate(size_t ID, OnDeactivate);
void RegisterOnDestroy(size_t ID, OnDestroy);
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>);

bool IsRegistered(size_t ID) const noexcept;
size_t GetID(std::string_view name) const;
Expand All @@ -67,11 +67,11 @@ namespace Ubpa::UECS {
std::vector<std::string> names;
std::unordered_map<std::string_view, size_t> name2id;

std::unordered_map<size_t, OnCreate> createMap;
std::unordered_map<size_t, OnActivate> activateMap;
std::unordered_map<size_t, OnUpdate> updateMap;
std::unordered_map<size_t, OnDeactivate> deactivateMap;
std::unordered_map<size_t, OnDestroy> destroyMap;
std::unordered_map<size_t, std::function<OnCreate >> createMap;
std::unordered_map<size_t, std::function<OnActivate >> activateMap;
std::unordered_map<size_t, std::function<OnUpdate >> updateMap;
std::unordered_map<size_t, std::function<OnDeactivate>> deactivateMap;
std::unordered_map<size_t, std::function<OnDestroy >> destroyMap;
};
}

Expand Down
12 changes: 10 additions & 2 deletions include/UECS/detail/ChunkView.inl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ namespace Ubpa::UECS {
template<typename Cmpt>
Span<Cmpt> ChunkView::GetCmptArray() const {
auto* ptr = GetCmptArray(CmptType::Of<Cmpt>);
if(ptr)
return { static_cast<Cmpt*>(GetCmptArray(CmptType::Of<Cmpt>)), EntityNum() };
if (!ptr)
return {};
return { static_cast<Cmpt*>(ptr), EntityNum() };
}

inline Span<const Entity> ChunkView::GetEntityArray() const {
auto* ptr = GetCmptArray(CmptType::Of<Entity>);
if (!ptr)
return {};
return { static_cast<const Entity*>(ptr), EntityNum() };
}
}
4 changes: 2 additions & 2 deletions include/UECS/detail/CmptsLocator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace Ubpa::UECS::detail {
CmptLocator GenerateCmptLocator(TypeList<Cmpts...>) {
if constexpr (sizeof...(Cmpts) > 0) {
constexpr std::array types{ CmptAccessType::Of<Cmpts>... };
return CmptLocator{ types.data(), types.size() };
return CmptLocator{ types };
}
else
return CmptLocator{};
return {};
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/SingletonLocator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Ubpa::UECS::detail {
return SingletonLocator{ types };
}
else
return SingletonLocator{{}};
return {};
}
}

Expand Down
20 changes: 10 additions & 10 deletions include/UECS/detail/SystemTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@

namespace Ubpa::UECS::detail {
template<typename System>
Concept(HaveOnCreate, static_cast<void(*)(World*)>(&System::OnCreate));
Concept(HaveOnCreate, static_cast<SystemTraits::OnCreate* >(&System::OnCreate ));
template<typename System>
Concept(HaveOnActivate, static_cast<void(*)(World*)>(&System::OnActivate));
Concept(HaveOnActivate, static_cast<SystemTraits::OnActivate* >(&System::OnActivate ));
template<typename System>
Concept(HaveOnUpdate, static_cast<void(*)(Schedule&)>(&System::OnUpdate));
Concept(HaveOnUpdate, static_cast<SystemTraits::OnUpdate* >(&System::OnUpdate ));
template<typename System>
Concept(HaveOnDeactivate, static_cast<void(*)(World*)>(&System::OnDeactivate));
Concept(HaveOnDeactivate, static_cast<SystemTraits::OnDeactivate*>(&System::OnDeactivate));
template<typename System>
Concept(HaveOnDestroy, static_cast<void(*)(World*)>(&System::OnDestroy));
Concept(HaveOnDestroy, static_cast<SystemTraits::OnDestroy* >(&System::OnDestroy ));

template<typename System>
size_t Register(SystemTraits& traits) {
size_t ID = traits.Register(std::string{ SystemTraits::StaticNameof<System>() });
if constexpr (Require<HaveOnCreate, System>)
traits.RegisterOnCreate(ID, std::function{ static_cast<void(*)(World*)>(&System::OnCreate) });
traits.RegisterOnCreate (ID, static_cast<SystemTraits::OnCreate* >(&System::OnCreate ));
if constexpr (Require<HaveOnActivate, System>)
traits.RegisterOnActivate(ID, std::function{ static_cast<void(*)(World*)>(&System::OnActivate) });
traits.RegisterOnActivate (ID, static_cast<SystemTraits::OnActivate* >(&System::OnActivate ));
if constexpr (Require<HaveOnUpdate, System>)
traits.RegisterOnUpdate(ID, static_cast<void(*)(Schedule&)>(&System::OnUpdate));
traits.RegisterOnUpdate (ID, static_cast<SystemTraits::OnUpdate* >(&System::OnUpdate ));
if constexpr (Require<HaveOnDeactivate, System>)
traits.RegisterOnDeactivate(ID, std::function{ static_cast<void(*)(World*)>(&System::OnDeactivate) });
traits.RegisterOnDeactivate(ID, static_cast<SystemTraits::OnDeactivate*>(&System::OnDeactivate));
if constexpr (Require<HaveOnDeactivate, System>)
traits.RegisterOnDestroy(ID, std::function{ static_cast<void(*)(World*)>(&System::OnDestroy) });
traits.RegisterOnDestroy (ID, static_cast<SystemTraits::OnDestroy* >(&System::OnDestroy ));
return ID;
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/core/CmptLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

using namespace Ubpa::UECS;

CmptLocator::CmptLocator(const CmptAccessType* types, size_t num) {
assert(types || num == 0);
for (size_t i = 0; i < num; i++)
cmptTypes.insert(types[i]);
CmptLocator::CmptLocator(Span<const CmptAccessType> types) {
for (const auto& type : types)
cmptTypes.insert(type);

UpdateHashCode();
}
Expand Down
16 changes: 8 additions & 8 deletions src/core/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Ubpa::UECS::detail {
// if (flag)
// return true;
//}
if (y.randomTypes.empty()) { // y.none
if (y.randomTypes.empty() && !y.noneTypes.empty()) { // y.none
bool allFlag = false;
bool anyFlag = true;
bool randomFlag = true;
Expand All @@ -59,8 +59,8 @@ namespace Ubpa::UECS::detail {
// any
auto x_iter = x.anyTypes.begin();
auto y_iter = y.noneTypes.begin();
while (x_iter != x.anyTypes.end() && y_iter != y.noneTypes.end()) {
if (x_iter->GetCmptType() < *y_iter) {
while (x_iter != x.anyTypes.end()) {
if (y_iter == y.noneTypes.end() || x_iter->GetCmptType() < *y_iter) {
anyFlag = false;
break;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace Ubpa::UECS::detail {
return true;
}
}
if (x.randomTypes.empty()) { // x.none
if (x.randomTypes.empty() && !x.noneTypes.empty()) { // x.none
bool allFlag = false;
bool anyFlag = true;
bool randomFlag = true;
Expand All @@ -116,8 +116,8 @@ namespace Ubpa::UECS::detail {
else {
auto x_iter = x.noneTypes.begin();
auto y_iter = y.anyTypes.begin();
while (x_iter != x.noneTypes.end() && y_iter != y.anyTypes.end()) {
if (y_iter->GetCmptType() < *x_iter) {
while (x_iter != x.noneTypes.end()) {
if (y_iter == y.anyTypes.end() || y_iter->GetCmptType() < *x_iter) {
anyFlag = false;
break;
}
Expand Down Expand Up @@ -263,8 +263,8 @@ namespace Ubpa::UECS::detail {
bool haveOrder = false;
for (auto* ifunc : gi.sysFuncs) {
for (auto* jfunc : gj.sysFuncs) {
if (subgraph.HavePath(ifunc, jfunc)
|| subgraph.HavePath(jfunc, ifunc)) {
if (graph.HavePath(ifunc, jfunc)
|| graph.HavePath(jfunc, ifunc)) {
haveOrder = true;
break;
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/SystemFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView, Entity e, s
e,
entityIndexInQuery,
cmptsView,
ChunkView{ nullptr, static_cast<size_t>(-1) }
{}
);
}

Expand All @@ -21,7 +21,7 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView, size_t enti
singletonsView,
Entity::Invalid(),
entityBeginIndexInQuery,
CmptsView{ {} },
{},
chunkView
);
}
Expand All @@ -33,7 +33,7 @@ void SystemFunc::operator()(World* w, SingletonsView singletonsView) const {
singletonsView,
Entity::Invalid(),
static_cast<size_t>(-1),
CmptsView{ {} },
ChunkView{ nullptr, static_cast<size_t>(-1) }
{},
{}
);
}
12 changes: 6 additions & 6 deletions src/core/SystemTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SystemTraits::SystemTraits(const SystemTraits& traits)
}

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

auto target = name2id.find(name);
if (target != name2id.end())
Expand All @@ -41,27 +41,27 @@ size_t SystemTraits::GetID(std::string_view name) const {
return target == name2id.end() ? static_cast<size_t>(-1) : target->second;
}

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

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

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

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

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

0 comments on commit 2aa59cf

Please sign in to comment.