Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
1. command : EntityMngr -> World, + World*
2. SystemFunc += World*
3. Schedule's Register() return SystemFunc
4. RTDCmptTraits += Clear
  • Loading branch information
Ubpa committed Aug 5, 2020
1 parent 70d61a3 commit fc81750
Show file tree
Hide file tree
Showing 24 changed files with 202 additions and 175 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.4)
project(UECS VERSION 0.10.5)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
29 changes: 24 additions & 5 deletions doc/comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,33 @@ use `CmptTag::LastFrame<Cmpt> (like const <Cmpt>*)`, `CmptTag::Write<Cmpt> == <C
- `[const] Entity`
- `size_t entityInQueryIndex`
- (not-support) `size_t nativeThreadIndex`
- `RTDCmptViewer`
- `ChunkView`
- `[const] CmptsView`
- `[const] ChunkView`

**System kind**

- components (optional: `Entity`, `size_t entityInQueryIndex`, `RTDCmptViewer`) : system for each entity
- empty : job
- chunk: `ChunkView`
- per entity function (default)
- [[const] World*]
- [[const] Entity]
- [size_t indexInQuery]
- [[const] CmptsView]
- \<tagged-component\>: {LastFrame|Write|Latest}\<Component\>
- chunk
- [[const] World*]
- [[const] ChunkView]

```c++
// 1.
// * [[const] World*]
// * [[const] Entity]
// * [size_t indexInQuery]
// * [[const] CmptsView]
// * <tagged-component>: {LastFrame|Write|Latest}<Component>
// 2. chunk: [[const] World*], [const] ChunkView
// 3. job: [[const] World*]
```



### 3.2 System update order

Expand Down
13 changes: 2 additions & 11 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include <UContainer/Pool.h>

#include <mutex>

namespace Ubpa::UECS {
class World;

Expand Down Expand Up @@ -61,8 +59,6 @@ namespace Ubpa::UECS {

size_t EntityNum(const EntityQuery&) const;

void AddCommand(const std::function<void()>& command);

void Accept(IListener* listener) const;

private:
Expand All @@ -82,8 +78,8 @@ namespace Ubpa::UECS {
void AttachWithoutInit(Entity);
void AttachWithoutInit(Entity, const CmptType* types, size_t num);

void GenEntityJob(Job* job, SystemFunc* sys) const;
void GenChunkJob(Job* job, SystemFunc* sys) const;
void GenEntityJob(World*, Job*, SystemFunc*) const;
void GenChunkJob(World*, Job*, SystemFunc*) const;

struct EntityInfo {
Archetype* archetype{ nullptr };
Expand All @@ -98,11 +94,6 @@ namespace Ubpa::UECS {
std::unordered_map<size_t, Archetype*> h2a; // archetype's hashcode to archetype

mutable std::unordered_map<EntityQuery, std::set<Archetype*>> queryCache;

// command
std::vector<std::function<void()>> commandBuffer;
std::mutex commandBufferMutex;
void RunCommands();
};
}

Expand Down
2 changes: 2 additions & 0 deletions include/UECS/RTDCmptTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace Ubpa::UECS {

static RTDCmptTraits& Instance() noexcept;

RTDCmptTraits& Clear();

// neccessary
RTDCmptTraits& RegisterSize(CmptType type, size_t size);

Expand Down
16 changes: 3 additions & 13 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,14 @@ namespace Ubpa::UECS {
class Schedule {
public:
template<typename Func>
Schedule& Register(Func&& func, std::string name, EntityFilter filter = EntityFilter{});
const SystemFunc* Register(Func&& func, std::string name, EntityFilter filter = EntityFilter{});

// run-time dynamic function
template<typename Func>
Schedule& Register(Func&& func, std::string name, EntityLocator locator, EntityFilter filter = EntityFilter{});
const SystemFunc* Register(Func&& func, std::string name, EntityLocator locator, EntityFilter filter = EntityFilter{});

Schedule& LockFilter(std::string_view sys);

// if sys is unregistered, return size_t_invalid
// call LockFilterChange(std::string_view)
size_t EntityNumInQuery(std::string_view sys) const;

EntityMngr* GetEntityMngr() const noexcept { return entityMngr; }
SystemMngr* GetSystemMngr() const noexcept { return systemMngr; }

Schedule& Order(std::string_view x, std::string_view y);

Schedule& InsertAll(std::string_view sys, CmptType);
Expand All @@ -52,9 +45,8 @@ namespace Ubpa::UECS {

private:
template<typename... Args>
void Request(Args&&... args);
const SystemFunc* Request(Args&&... args);

Schedule(EntityMngr* entityMngr, SystemMngr* systemMngr);
void Clear();

struct CmptSysFuncs {
Expand Down Expand Up @@ -86,8 +78,6 @@ namespace Ubpa::UECS {
std::unordered_set<size_t> sysLockFilter;

Pool<SystemFunc> sysFuncPool;
EntityMngr* entityMngr;
SystemMngr* systemMngr;
friend class World;
};
}
Expand Down
18 changes: 11 additions & 7 deletions include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ 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] [CmptsView] <tagged-component>...
// 1. per entity function
// * [[const] World*]
// * [[const] Entity]
// * [size_t indexInQuery]
// * [[const] CmptsView]
// * <tagged-component>: {LastFrame|Write|Latest}<Component>
// 2. chunk: ChunkView
// 3. job: empty argument list
// 2. chunk: [[const] World*], [const] ChunkView
// 3. job: [[const] World*]
class SystemFunc {
public:
enum class Mode {
Expand All @@ -41,9 +45,9 @@ namespace Ubpa::UECS {

size_t HashCode() const noexcept { return hashCode; }

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

Mode GetMode() const noexcept { return mode; }

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

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

std::string name;
Mode mode;
Expand Down
9 changes: 9 additions & 0 deletions include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <UGraphviz/UGraphviz.h>

#include <mutex>

namespace Ubpa::UECS {
class IListener;

Expand Down Expand Up @@ -33,6 +35,8 @@ namespace Ubpa::UECS {

void Accept(IListener* listener) const;

void AddCommand(std::function<void(World*)> command);

private:
mutable JobExecutor executor;
Schedule schedule;
Expand All @@ -41,6 +45,11 @@ namespace Ubpa::UECS {
std::vector<Job*> jobs;
Pool<Job> jobPool;

// command
std::vector<std::function<void(World*)>> commandBuffer;
std::mutex commandBufferMutex;
void RunCommands();

// ==================================================
World(const World& world) = delete;
World(World&& world) = delete;
Expand Down
9 changes: 4 additions & 5 deletions include/UECS/detail/CmptTag.inl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ubpa::UECS {
class Entity;
class EntityLocator;
class World;
}

namespace Ubpa::UECS {
Expand All @@ -14,23 +15,21 @@ namespace Ubpa::UECS {
template<typename Cmpt> struct RemoveTag<LastFrame<Cmpt>> : IType<Cmpt> {};

template<typename T> struct DecayTag : IType<T> {};
// template<typename Cmpt> struct DecayTag<Cmpt*> : IType<Cmpt*> {};
template<typename Cmpt> struct DecayTag<const Cmpt*> : IType<Cmpt*> {};
template<typename Cmpt> struct DecayTag<LastFrame<Cmpt>> : IType<Cmpt*> {};
template<> struct DecayTag<const Entity> : IType<Entity> {};
template<> struct DecayTag<const EntityLocator*> : IType<const EntityLocator*> {};
template<typename T> struct DecayTag<const T> : IType<T> {};

template<typename TaggedCmpt> struct IsLastFrame : std::false_type {};
template<typename Cmpt> struct IsLastFrame<LastFrame<Cmpt>> : std::true_type {};

template<typename TaggedCmpt> struct IsWrite : std::false_type {};
template<typename Cmpt> struct IsWrite<const Cmpt*> : std::false_type {};
template<typename Cmpt> struct IsWrite<Cmpt*> : std::true_type {};
template<> struct IsWrite<Entity*> : std::false_type {};
template<> struct IsWrite<World*> : std::false_type {};

template<typename TaggedCmpt> struct IsLatest : std::false_type {};
template<> struct IsLatest<const EntityLocator*> : std::false_type {};
template<typename Cmpt> struct IsLatest<const Cmpt*> : std::true_type {};
template<> struct IsLatest<const World*> : std::false_type {};

template<typename TaggedCmpt> struct IsTimePoint :
IValue<bool, IsWrite_v<TaggedCmpt> || IsLastFrame_v<TaggedCmpt> || IsLatest_v<TaggedCmpt>> {};
Expand Down
5 changes: 0 additions & 5 deletions include/UECS/detail/EntityMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,4 @@ namespace Ubpa::UECS {
inline bool EntityMngr::Exist(Entity e) const {
return e.Idx() < entityTable.size() && e.Version() == entityTable[e.Idx()].version;
}

inline void EntityMngr::AddCommand(const std::function<void()>& command) {
std::lock_guard<std::mutex> guard(commandBufferMutex);
commandBuffer.push_back(command);
}
}
14 changes: 14 additions & 0 deletions include/UECS/detail/RTDCmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,26 @@ namespace Ubpa::UECS {
}

inline RTDCmptTraits& RTDCmptTraits::Deregister(CmptType type) noexcept {
names.erase(type);
sizeofs.erase(type);
alignments.erase(type);
default_constructors.erase(type);
copy_constructors.erase(type);
move_constructors.erase(type);
move_assignments.erase(type);
destructors.erase(type);
return *this;
}

inline RTDCmptTraits& RTDCmptTraits::Clear() {
names.clear();
sizeofs.clear();
alignments.clear();
default_constructors.clear();
copy_constructors.clear();
move_constructors.clear();
move_assignments.clear();
destructors.clear();
return *this;
}
}
16 changes: 6 additions & 10 deletions include/UECS/detail/Schedule.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@

namespace Ubpa::UECS {
template<typename Func>
Schedule& Schedule::Register(Func&& func, std::string name, EntityFilter filter) {
Request(std::forward<Func>(func), std::move(name), std::move(filter));
return *this;
const SystemFunc* Schedule::Register(Func&& func, std::string name, EntityFilter filter) {
return Request(std::forward<Func>(func), std::move(name), std::move(filter));
}

template<typename Func>
Schedule& Schedule::Register(Func&& func, std::string name, EntityLocator locator, EntityFilter filter) {
Request(std::forward<Func>(func), std::move(name), std::move(locator), std::move(filter));
return *this;
const SystemFunc* Schedule::Register(Func&& func, std::string name, EntityLocator locator, EntityFilter filter) {
return Request(std::forward<Func>(func), std::move(name), std::move(locator), std::move(filter));
}

template<typename... Args>
void Schedule::Request(Args&&... args) {
const SystemFunc* Schedule::Request(Args&&... args) {
SystemFunc* sysFunc = sysFuncPool.Request(std::forward<Args>(args)...);
sysFuncs.emplace(sysFunc->HashCode(), sysFunc);
return sysFunc;
}

inline Schedule::Schedule(EntityMngr* entityMngr, SystemMngr* systemMngr)
: entityMngr{ entityMngr }, systemMngr{ systemMngr }{}

inline Schedule& Schedule::LockFilter(std::string_view sys) {
sysLockFilter.insert(SystemFunc::HashCode(sys));
return *this;
Expand Down
36 changes: 28 additions & 8 deletions include/UECS/detail/SystemFunc.inl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ namespace Ubpa::UECS {
query{ std::move(filter), std::move(locator) }
{
using ArgList = FuncTraits_ArgList<Func>;
using DecayedArgList = Transform_t<ArgList, DecayTag>;

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

static_assert(!Contain_v<ArgList, ChunkView>,
"(Mode::Entity) <Func>'s argument list must not contain ChunkView");
static_assert(!Contain_v<DecayedArgList, ChunkView>,
"(Mode::Entity) <Func>'s argument list must not contain [const] ChunkView");
}

template<typename Func>
Expand All @@ -40,13 +41,31 @@ namespace Ubpa::UECS {
hashCode{ HashCode(this->name) },
query{ std::move(filter), EntityLocator{Filter_t<ArgList, IsTaggedCmpt>{}} }
{
static_assert(!Contain_v<ArgList, CmptsView>,
using DecayedArgList = Transform_t<ArgList, DecayTag>;

static_assert(!Contain_v<DecayedArgList, CmptsView>,
"<Func>'s argument list contains CmptsView, so you should use the constructor of the run-time dynamic version");
if constexpr (IsEmpty_v<ArgList>)

if constexpr (
IsEmpty_v<DecayedArgList>
|| Length_v<DecayedArgList> == 1 && Contain_v<DecayedArgList, World*>
) {
// [[const] World*]
mode = Mode::Job;
else if constexpr (std::is_same_v<ArgList, TypeList<ChunkView>>)
}
else if constexpr (
Contain_v<DecayedArgList, ChunkView>
&& (
Length_v<DecayedArgList> == 1
|| Length_v<DecayedArgList> == 2 && Contain_v<DecayedArgList, World*>
)
) {
// [[const] World*]
// [const] ChunkView
mode = Mode::Chunk;
}
else {
// default
static_assert(!Contain_v<ArgList, ChunkView>,
"(Mode::Entity) <Func>'s argument list must not contain ChunkView");
mode = Mode::Entity;
Expand All @@ -63,8 +82,9 @@ 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, CmptsView rtdcmpts, ChunkView chunkView) {
return [func = std::forward<Func>(func)](World* w, Entity e, size_t entityIndexInQuery, CmptsView rtdcmpts, ChunkView chunkView) {
auto unsorted_arg_tuple = std::make_tuple(
w,
e,
entityIndexInQuery,
rtdcmpts,
Expand Down
Loading

0 comments on commit fc81750

Please sign in to comment.