Skip to content

Commit

Permalink
CmptAccessMode remove singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Nov 10, 2020
1 parent b0e52c9 commit 1d67e74
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 67 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(UECS VERSION 0.14.0)
project(UECS VERSION 0.14.1)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
4 changes: 4 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

- 0.14.1: `CmptAccessMode` remove singleton
- 0.14.0: System Lifecycle
2 changes: 1 addition & 1 deletion doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
- [x] system base -> `System`
- [x] singleton
- [ ] doxygen
- [ ] lock ?
- [ ] lock / random access
- [x] system traits

### maybe support in future
Expand Down
7 changes: 0 additions & 7 deletions include/UECS/CmptTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,8 @@ namespace Ubpa::UECS {
LAST_FRAME = 0b000, // LastFrame<Cmpt>
WRITE = 0b001, // Write<Cmpt> / Cmpt*
LATEST = 0b010, // Latest<Cmpt> / const Cmpt*
LAST_FRAME_SINGLETON = 0b100, // LastFrame<Singleton<Cmpt>>
WRITE_SINGLETON = 0b101, // Write<Singleton<Cmpt>> / Singleton<Cmpt>
LATEST_SINGLETON = 0b110, // Latest<Singleton<Cmpt>>
};

constexpr bool AccessMode_IsSingleton(AccessMode mode) noexcept {
return (static_cast<size_t>(mode) & 4) != 0;
}

template<typename Cmpt>
class Singleton {
public:
Expand Down
6 changes: 3 additions & 3 deletions include/UECS/CmptType.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ namespace Ubpa::UECS {
explicit constexpr CmptAccessType(CmptType type) noexcept : CmptAccessType{ type, AccessMode::LATEST } {}
explicit constexpr CmptAccessType() noexcept : CmptAccessType{ Invalid() } {}

template<typename Cmpt, AccessMode mode = AccessMode::LATEST>
static constexpr CmptAccessType Of = CmptAccessType{ CmptType::Of<RemoveTag_t<Cmpt>>, AccessModeOf_default<Cmpt, mode> };
template<typename Cmpt>
static constexpr CmptAccessType Of = CmptAccessType{ CmptType::Of<RemoveTag_t<Cmpt>>, AccessModeOf<Cmpt> };

// same with CmptType's HashCode
constexpr size_t HashCode() const noexcept { return type.HashCode(); }
Expand All @@ -61,7 +61,7 @@ namespace Ubpa::UECS {

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

static constexpr CmptAccessType Invalid() noexcept { return CmptAccessType{ static_cast<size_t>(-1), AccessMode::LATEST }; }
static constexpr CmptAccessType Invalid() noexcept { return CmptAccessType{ static_cast<size_t>(-1), AccessMode{} }; }
constexpr bool Valid() const noexcept { return type.Valid(); }

// same with CmptType's operator<
Expand Down
16 changes: 5 additions & 11 deletions include/UECS/detail/CmptTag.inl
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,15 @@ namespace Ubpa::UECS {

template<typename T, AccessMode defaultMode>
static constexpr AccessMode AccessModeOf_default =
IsLastFrame_v<T> ? AccessMode::LAST_FRAME : (
IsWrite_v<T> ? AccessMode::WRITE : (
IsLatest_v<T> ? AccessMode::LATEST : (
IsLastFrameSingleton_v<T> ? AccessMode::LAST_FRAME_SINGLETON : (
IsWriteSingleton_v<T> ? AccessMode::WRITE_SINGLETON : (
IsLatestSingleton_v<T> ? AccessMode::LATEST_SINGLETON :
defaultMode
)
)
)
IsLastFrame_v<T> || IsLastFrameSingleton_v<T> ? AccessMode::LAST_FRAME : (
IsWrite_v<T> || IsWriteSingleton_v<T> ? AccessMode::WRITE : (
IsLatest_v<T> || IsLatestSingleton_v<T> ? AccessMode::LATEST :
defaultMode
)
);

template<typename T>
static constexpr AccessMode AccessModeOf = AccessModeOf_default<T, AccessMode::LATEST>;
static constexpr AccessMode AccessModeOf = AccessModeOf_default<T, AccessMode::WRITE>;

template<typename Cmpt> struct RemoveTag : IType<Cmpt> {}; // default

Expand Down
8 changes: 2 additions & 6 deletions src/core/ArchetypeFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ using namespace Ubpa::UECS;

size_t ArchetypeFilter::HashCode() const noexcept {
size_t rst = TypeID<ArchetypeFilter>;
for (const auto& type : all) {
assert(!AccessMode_IsSingleton(type.GetAccessMode()));
for (const auto& type : all)
rst = hash_combine(rst, type.HashCode());
}
for (const auto& type : any) {
assert(!AccessMode_IsSingleton(type.GetAccessMode()));
for (const auto& type : any)
rst = hash_combine(rst, type.HashCode());
}
for (const auto& type : none)
rst = hash_combine(rst, type.HashCode());
return rst;
Expand Down
4 changes: 1 addition & 3 deletions src/core/CmptLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ using namespace Ubpa::UECS;

CmptLocator::CmptLocator(const CmptAccessType* types, size_t num) {
assert(types || num == 0);
for (size_t i = 0; i < num; i++) {
assert(!AccessMode_IsSingleton(types[i].GetAccessMode()));
for (size_t i = 0; i < num; i++)
cmptTypes.insert(types[i]);
}

hashCode = GenHashCode();
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ unordered_map<CmptType, Schedule::CmptSysFuncs> Schedule::GenCmptSysFuncsMap() c
for (const auto& type : sysFunc->singletonLocator.SingletonTypes()) {
switch (type.GetAccessMode())
{
case Ubpa::UECS::AccessMode::LAST_FRAME_SINGLETON:
case Ubpa::UECS::AccessMode::LAST_FRAME:
rst[type].lastFrameSysFuncs.push_back(sysFunc);
break;
case Ubpa::UECS::AccessMode::WRITE_SINGLETON:
case Ubpa::UECS::AccessMode::WRITE:
rst[type].writeSysFuncs.push_back(sysFunc);
break;
case Ubpa::UECS::AccessMode::LATEST_SINGLETON:
case Ubpa::UECS::AccessMode::LATEST:
rst[type].latestSysFuncs.push_back(sysFunc);
break;
default:
Expand Down
3 changes: 1 addition & 2 deletions src/core/SingletonLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ using namespace std;
SingletonLocator::SingletonLocator(const CmptAccessType* types, size_t num) {
assert(types || num == 0);
for (size_t i = 0; i < num; i++) {
assert(AccessMode_IsSingleton(types[i].GetAccessMode()));
singletonTypes.insert(types[i]);
}
}

bool SingletonLocator::HasWriteSingletonType() const noexcept {
for (const auto& type : singletonTypes) {
if (type.GetAccessMode() == AccessMode::WRITE_SINGLETON)
if (type.GetAccessMode() == AccessMode::WRITE)
return true;
}
return false;
Expand Down
64 changes: 34 additions & 30 deletions src/core/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const {
.RegisterGraphEdgeAttr("color", "#C785C8")
.RegisterGraphEdgeAttr("arrowhead", "odot");

unordered_set<CmptAccessType> cmptTypes;
unordered_set<CmptType> cmptTypes;
unordered_set<CmptType> singletonTypes;
unordered_map<CmptType, size_t> cmptType2idx;
unordered_map<size_t, size_t> sysFuncHashcode2idx;

Expand All @@ -159,18 +160,21 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const {
for (auto cmptType : sysFunc->entityQuery.filter.any)
cmptTypes.insert(cmptType);
for (auto cmptType : sysFunc->entityQuery.filter.none)
cmptTypes.insert(CmptAccessType{ cmptType });
cmptTypes.insert(cmptType);
for (auto singleton : sysFunc->singletonLocator.SingletonTypes())
cmptTypes.insert(singleton);
singletonTypes.insert(singleton);
}

for (const auto& cmptType : cmptTypes) {
auto cmptIdx = registry.RegisterNode(queryCmptName(cmptType));
cmptType2idx.emplace(cmptType, cmptIdx);
if (AccessMode_IsSingleton(cmptType.GetAccessMode()))
subgraph_singleton.AddNode(cmptIdx);
else
subgraph_cmpt.AddNode(cmptIdx);
subgraph_cmpt.AddNode(cmptIdx);
}

for (const auto& singletonType : singletonTypes) {
auto cmptIdx = registry.RegisterNode(queryCmptName(singletonType));
cmptType2idx.emplace(singletonType, cmptIdx);
subgraph_singleton.AddNode(cmptIdx);
}

for (const auto& [hash, sysFunc] : schedule.sysFuncs) {
Expand All @@ -184,15 +188,15 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const {
switch (cmptType.GetAccessMode())
{
case AccessMode::LAST_FRAME:
edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx);
edgeIdx = registry.RegisterEdge(cmptType2idx.at(cmptType), sysIdx);
subgraph_lastframe.AddEdge(edgeIdx);
break;
case AccessMode::WRITE:
edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]);
edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx.at(cmptType));
subgraph_write.AddEdge(edgeIdx);
break;
case AccessMode::LATEST:
edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx);
edgeIdx = registry.RegisterEdge(cmptType2idx.at(cmptType), sysIdx);
subgraph_latest.AddEdge(edgeIdx);
break;
default:
Expand All @@ -205,20 +209,20 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const {
if (sysFunc->GetMode() == SystemFunc::Mode::Chunk) {
// filter's <All> and <Any> components are treat as r/w
for (const auto& cmptType : filter.all) {
auto cmptIdx = cmptType2idx[cmptType];
auto cmptIdx = cmptType2idx.at(cmptType);
size_t edgeIdx;
switch (cmptType.GetAccessMode())
{
case AccessMode::LAST_FRAME:
edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx);
edgeIdx = registry.RegisterEdge(cmptType2idx.at(cmptType), sysIdx);
subgraph_lastframe.AddEdge(edgeIdx);
break;
case AccessMode::WRITE:
edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]);
edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx.at(cmptType));
subgraph_write.AddEdge(edgeIdx);
break;
case AccessMode::LATEST:
edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx);
edgeIdx = registry.RegisterEdge(cmptType2idx.at(cmptType), sysIdx);
subgraph_latest.AddEdge(edgeIdx);
break;
default:
Expand All @@ -227,20 +231,20 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const {
}
}
for (const auto& cmptType : filter.any) {
auto cmptIdx = cmptType2idx[cmptType];
auto cmptIdx = cmptType2idx.at(cmptType);
size_t edgeIdx;
switch (cmptType.GetAccessMode())
{
case AccessMode::LAST_FRAME:
edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx);
edgeIdx = registry.RegisterEdge(cmptType2idx.at(cmptType), sysIdx);
subgraph_lastframe.AddEdge(edgeIdx);
break;
case AccessMode::WRITE:
edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]);
edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx.at(cmptType));
subgraph_write.AddEdge(edgeIdx);
break;
case AccessMode::LATEST:
edgeIdx = registry.RegisterEdge(cmptType2idx[cmptType], sysIdx);
edgeIdx = registry.RegisterEdge(cmptType2idx.at(cmptType), sysIdx);
subgraph_latest.AddEdge(edgeIdx);
break;
default:
Expand All @@ -251,42 +255,42 @@ UGraphviz::Graph World::GenUpdateFrameGraph() const {
}
else {
for (const auto& cmptType : filter.all) {
auto cmptIdx = cmptType2idx[cmptType];
auto cmptIdx = cmptType2idx.at(cmptType);
if (registry.IsRegisteredEdge(sysIdx, cmptIdx))
continue;
auto edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]);
auto edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx.at(cmptType));
subgraph_all.AddEdge(edgeIdx);
}
for (const auto& cmptType : filter.any) {
auto cmptIdx = cmptType2idx[cmptType];
auto cmptIdx = cmptType2idx.at(cmptType);
if (registry.IsRegisteredEdge(sysIdx, cmptIdx))
continue;
auto edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]);
auto edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx.at(cmptType));
subgraph_any.AddEdge(edgeIdx);
}
}
for (const auto& cmptType : filter.none) {
auto cmptIdx = cmptType2idx[cmptType];
auto cmptIdx = cmptType2idx.at(cmptType);
if (registry.IsRegisteredEdge(sysIdx, cmptIdx))
continue;
auto edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[cmptType]);
auto edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx.at(cmptType));
subgraph_none.AddEdge(edgeIdx);
}

for (const auto& singleton : sysFunc->singletonLocator.SingletonTypes()) {
size_t edgeIdx;
switch (singleton.GetAccessMode())
{
case AccessMode::LAST_FRAME_SINGLETON:
edgeIdx = registry.RegisterEdge(cmptType2idx[singleton], sysIdx);
case AccessMode::LAST_FRAME:
edgeIdx = registry.RegisterEdge(cmptType2idx.at(singleton), sysIdx);
subgraph_lastframe.AddEdge(edgeIdx);
break;
case AccessMode::WRITE_SINGLETON:
edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx[singleton]);
case AccessMode::WRITE:
edgeIdx = registry.RegisterEdge(sysIdx, cmptType2idx.at(singleton));
subgraph_write.AddEdge(edgeIdx);
break;
case AccessMode::LATEST_SINGLETON:
edgeIdx = registry.RegisterEdge(cmptType2idx[singleton], sysIdx);
case AccessMode::LATEST:
edgeIdx = registry.RegisterEdge(cmptType2idx.at(singleton), sysIdx);
subgraph_latest.AddEdge(edgeIdx);
break;
default:
Expand Down

0 comments on commit 1d67e74

Please sign in to comment.