Skip to content

Commit

Permalink
simplify RTDCmptsView to CmptsView
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 3, 2020
1 parent eab0ea8 commit 70d61a3
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 185 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.10.3)
project(UECS VERSION 0.10.4)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ int main() {
- [chunk layout optimization with alignment](src/test/05_alignment/main.cpp)
- [parrallel with `None` filter](src/test/06_none_parallel/main.cpp)
- [system **overload**](src/test/07_overload/main.cpp)
- [runtime dynamic component and system](src/test/11_runtime_cmpt/main.cpp)
- [runtime dynamic component](src/test/11_runtime_cmpt/main.cpp)
- [generate **frame graph** in **Graphviz**](src/test/12_framegraph/main.cpp)
- [performance test](src/test/13_performance/main.cpp)
- [serialize](src/test/14_serialize/main.cpp)
- [chunk job](src/test/15_chunk_job/main.cpp)
- [chunk job](src/test/15_chunk_job/main.cpp)
## Licensing
Expand Down
2 changes: 1 addition & 1 deletion doc/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
- [ ] batch create/instantiate (need benchmark)
- [x] lock `FilterChange`
- [ ] `EntityMngr` `Query`-driven API
- [x] `RTDCmptsView` = `const EntityLocator* locator + void** cmpts`
- [x] `CmptsView` = `const EntityLocator* locator + void** cmpts`
- [ ] Filter Builder

### maybe deprecate
Expand Down
38 changes: 17 additions & 21 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,33 @@ namespace Ubpa::UECS {
CmptPtr(Cmpt* p) : type{ CmptType::Of<Cmpt> }, p{ p }{}

CmptType Type() const noexcept { return type; }

// unchecked
void* Ptr() const noexcept { return p; }

// for static Component
// unchecked
template<typename Cmpt>
Cmpt* As() const noexcept {
assert(type.Is<Cmpt>());
return reinterpret_cast<Cmpt*>(p);
}
private:
CmptType type;
void* p;
};
Cmpt* As() const noexcept { return reinterpret_cast<Cmpt*>(p); }

// CmptType + const void*
class CmptCPtr {
public:
CmptCPtr(CmptType type, const void* p) :type{ type }, p{ p }{}
template<typename Cmpt>
CmptCPtr(const Cmpt* p) : type{ CmptType::Of<Cmpt> }, p{ p }{}
LastFrame<Cmpt> AsLastFrame() const noexcept {
assert(type.GetAccessMode() == AccessMode::LAST_FRAME);
return p;
}

CmptType Type() const noexcept { return type; }
const void* Ptr() const noexcept { return p; }
template<typename Cmpt>
Write<Cmpt> AsWrite() const noexcept {
assert(type.GetAccessMode() == AccessMode::WRITE);
return p;
}

// for static Component
template<typename Cmpt>
const Cmpt* As() const noexcept {
assert(type.Is<Cmpt>());
return reinterpret_cast<Cmpt*>(p);
Latest<Cmpt> AsLatest() const noexcept {
assert(type.GetAccessMode() == AccessMode::LATEST);
return p;
}
private:
CmptType type;
const void* p;
void* p;
};
}
41 changes: 41 additions & 0 deletions include/UECS/CmptsView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "CmptPtr.h"
#include "CmptType.h"
#include "CmptTag.h"

#include <set>

namespace Ubpa::UECS {
class EntityLocator;

// use CmptsView::Iterator to read CmptPtr
class CmptsView {
public:
CmptsView(EntityLocator* locator, void** cmpts)
: locator{ locator }, cmpts{ cmpts }{}

// check AccessMode
CmptPtr GetCmpt(CmptType) const;

template<typename Cmpt>
LastFrame<Cmpt> GetCmptAsLastFrame() const {
return GetCmpt(CmptType::Of<LastFrame<Cmpt>>).AsLastFrame<Cmpt>();
}

template<typename Cmpt>
Write<Cmpt> GetCmptAsWrite() const {
return GetCmpt(CmptType::Of<Write<Cmpt>>).AsWrite<Cmpt>();
}

template<typename Cmpt>
Latest<Cmpt> GetCmptAsLatest() const {
return GetCmpt(CmptType::Of<Latest<Cmpt>>).AsLatest<Cmpt>();
}

void* const* Components() const noexcept { return cmpts; }
private:
EntityLocator* locator;
void* const* cmpts;
};
}
6 changes: 1 addition & 5 deletions include/UECS/EntityLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ namespace Ubpa::UECS {
template<typename... LastFrameCmpts, typename... WriteCmpts, typename... LatestCmpts>
EntityLocator(TypeList<LastFrameCmpts...>, TypeList<WriteCmpts...>, TypeList<LatestCmpts...>);

EntityLocator(
std::set<CmptType> lastFrameCmpts = {},
std::set<CmptType> writeFrameCmpts = {},
std::set<CmptType> latestCmpts = {}
);
EntityLocator(const CmptType* types, size_t num);

size_t HashCode() const noexcept { return hashCode; }

Expand Down
88 changes: 0 additions & 88 deletions include/UECS/RTDCmptsView.h

This file was deleted.

8 changes: 4 additions & 4 deletions include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "EntityQuery.h"
#include "Entity.h"
#include "RTDCmptsView.h"
#include "CmptsView.h"
#include "ChunkView.h"

#include <functional>
Expand All @@ -14,7 +14,7 @@ namespace Ubpa::UECS {
// name must be unique in global
// query.filter can be change dynamically by other <System> with Schedule
// [system function kind] (distinguish by argument list)
// 1. per entity function: [[const] Entity e] [size_t indexInQuery] [RTDCmptsView] <tagged-component>...
// 1. per entity function: [[const] Entity e] [size_t indexInQuery] [CmptsView] <tagged-component>...
// * <tagged-component>: {LastFrame|Write|Latest}<Component>
// 2. chunk: ChunkView
// 3. job: empty argument list
Expand All @@ -41,7 +41,7 @@ namespace Ubpa::UECS {

size_t HashCode() const noexcept { return hashCode; }

void operator()(Entity e, size_t entityIndexInQuery, RTDCmptsView rtdcmpts);
void operator()(Entity e, size_t entityIndexInQuery, CmptsView rtdcmpts);
void operator()(ChunkView chunkView);
void operator()();

Expand All @@ -52,7 +52,7 @@ namespace Ubpa::UECS {
template<typename Func, typename ArgList>
SystemFunc(Func&& func, std::string name, EntityFilter filter, ArgList);

std::function<void(Entity, size_t, RTDCmptsView, ChunkView)> func;
std::function<void(Entity, size_t, CmptsView, ChunkView)> func;

std::string name;
Mode mode;
Expand Down
13 changes: 7 additions & 6 deletions include/UECS/detail/SystemFunc.inl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace Ubpa::UECS {
{
using ArgList = FuncTraits_ArgList<Func>;

static_assert(Contain_v<ArgList, RTDCmptsView>,
"(Mode::Entity) <Func>'s argument list must contain RTDCmptsView");
static_assert(Contain_v<ArgList, CmptsView>,
"(Mode::Entity) <Func>'s argument list must contain CmptsView");

static_assert(!Contain_v<ArgList, ChunkView>,
"(Mode::Entity) <Func>'s argument list must not contain ChunkView");
Expand All @@ -40,8 +40,8 @@ namespace Ubpa::UECS {
hashCode{ HashCode(this->name) },
query{ std::move(filter), EntityLocator{Filter_t<ArgList, IsTaggedCmpt>{}} }
{
static_assert(!Contain_v<ArgList, RTDCmptsView>,
"<Func>'s argument list contains RTDCmptsView, so you should use the constructor of the run-time dynamic version");
static_assert(!Contain_v<ArgList, CmptsView>,
"<Func>'s argument list contains CmptsView, so you should use the constructor of the run-time dynamic version");
if constexpr (IsEmpty_v<ArgList>)
mode = Mode::Job;
else if constexpr (std::is_same_v<ArgList, TypeList<ChunkView>>)
Expand All @@ -63,13 +63,14 @@ namespace Ubpa::UECS::detail::System_ {
using CmptList = TypeList<Cmpts...>; // sorted
template<typename Func>
static auto run(Func&& func) noexcept {
return [func = std::forward<Func>(func)](Entity e, size_t entityIndexInQuery, RTDCmptsView rtdcmpts, ChunkView chunkView) {
return [func = std::forward<Func>(func)](Entity e, size_t entityIndexInQuery, CmptsView rtdcmpts, ChunkView chunkView) {
auto unsorted_arg_tuple = std::make_tuple(
e,
entityIndexInQuery,
rtdcmpts,
chunkView,
reinterpret_cast<Cmpts*>(rtdcmpts.Components()[Find_v<CmptList, Cmpts>])...);
reinterpret_cast<Cmpts*>(rtdcmpts.Components()[Find_v<CmptList, Cmpts>])...
);
func(std::get<DecayedArgs>(unsorted_arg_tuple)...);
};
}
Expand Down
18 changes: 18 additions & 0 deletions src/core/CmptsView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <UECS/CmptsView.h>

#include <UECS/EntityLocator.h>

using namespace Ubpa::UECS;
using namespace std;

CmptPtr CmptsView::GetCmpt(CmptType t) const {
size_t i = 0;
for (auto iter = locator->CmptTypes().begin(); iter != locator->CmptTypes().end(); ++iter, ++i) {
if (*iter == t) {
assert(iter->GetAccessMode() == t.GetAccessMode());
return CmptPtr(*iter, *(cmpts + i));
}
}
assert(false);
return CmptPtr(t, nullptr);
}
26 changes: 19 additions & 7 deletions src/core/EntityLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
using namespace Ubpa::UECS;
using namespace std;

EntityLocator::EntityLocator(set<CmptType> lastFrameCmpts,
set<CmptType> writeFrameCmpts,
set<CmptType> latestCmpts)
: lastFrameCmptTypes{ move(lastFrameCmpts) },
writeCmptTypes{ move(writeFrameCmpts) },
latestCmptTypes{ move(latestCmpts) }
{
EntityLocator::EntityLocator(const CmptType* types, size_t num) {
assert(types != nullptr);
for (size_t i = 0; i < num; i++) {
switch (types[i].GetAccessMode())
{
case Ubpa::UECS::AccessMode::LAST_FRAME:
lastFrameCmptTypes.insert(types[i]);
break;
case Ubpa::UECS::AccessMode::WRITE:
writeCmptTypes.insert(types[i]);
break;
case Ubpa::UECS::AccessMode::LATEST:
latestCmptTypes.insert(types[i]);
break;
default:
assert(false);
break;
}
}
cmptTypes = SetUnion(lastFrameCmptTypes, writeCmptTypes);
cmptTypes = SetUnion(cmptTypes, latestCmptTypes);
hashCode = GenHashCode();
Expand Down
27 changes: 0 additions & 27 deletions src/core/RTDCmptsView.cpp

This file was deleted.

Loading

0 comments on commit 70d61a3

Please sign in to comment.