Skip to content

Commit

Permalink
distinguish three types of system function in API
Browse files Browse the repository at this point in the history
Entity Job, Chunk Job, Job
  • Loading branch information
Ubpa committed Aug 9, 2020
1 parent f42e4e3 commit d532d8b
Show file tree
Hide file tree
Showing 34 changed files with 188 additions and 120 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.11.1)
project(UECS VERSION 0.11.2)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/ArchetypeFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Ubpa::UECS {
template<typename CmptTypeContainer> void EraseAny(const CmptTypeContainer&);
template<typename CmptTypeContainer> void EraseNone(const CmptTypeContainer&);

bool operator==(const ArchetypeFilter& filter) const noexcept;
bool operator==(const ArchetypeFilter& rhs) const noexcept;

private:
size_t GenAllHashCode() const noexcept;
Expand Down
6 changes: 4 additions & 2 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ namespace Ubpa::UECS {

size_t EntityNum(const EntityQuery&) const;

std::tuple<bool, std::vector<CmptPtr>> LocateSingletons(const SingletonLocator&) const;

bool IsSingleton(CmptType) const;
Entity GetSingletonEntity(CmptType) const;
CmptPtr GetSingleton(CmptType) const;
// nullptr if not singleton
CmptPtr GetIfSingleton(CmptType) const;
template<typename Cmpt>
Cmpt* GetSingleton() const { return GetSingleton(CmptType::Of<Cmpt>).As<Cmpt>(); }
template<typename Cmpt>
Cmpt* GetIfSingleton() const { return GetIfSingleton(CmptType::Of<Cmpt>).As<Cmpt>(); }

void Accept(IListener* listener) const;

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

std::tuple<bool, std::vector<CmptPtr>> LocateSingletons(const SingletonLocator& locator) const;

void GenEntityJob(World*, Job*, SystemFunc*) const;
void GenChunkJob(World*, Job*, SystemFunc*) const;
void GenJob(World*, Job*, SystemFunc*) const;
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/EntityQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Ubpa::UECS {
template<typename... AllCmpts, typename... AnyCmpts, typename... NoneCmpts, typename... Cmpts>
EntityQuery(TypeList<AllCmpts...>, TypeList<AnyCmpts...>, TypeList<NoneCmpts...>, TypeList<Cmpts...>);

EntityQuery(ArchetypeFilter filter, CmptLocator locator = {})
EntityQuery(ArchetypeFilter filter = {}, CmptLocator locator = {})
:filter{ std::move(filter) }, locator{ std::move(locator) } {}

// without locator's singletons
Expand Down
24 changes: 12 additions & 12 deletions include/UECS/IListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ namespace Ubpa::UECS {

class IListener {
public:
virtual void EnterWorld(const World* w) = 0;
virtual void ExistWorld(const World* w) = 0;
virtual void EnterWorld(const World*) = 0;
virtual void ExistWorld(const World*) = 0;

virtual void EnterSystemMngr(const SystemMngr* sm) = 0;
virtual void ExistSystemMngr(const SystemMngr* sm) = 0;
virtual void EnterSystemMngr(const SystemMngr*) = 0;
virtual void ExistSystemMngr(const SystemMngr*) = 0;

virtual void EnterSystem(const System* s) = 0;
virtual void ExistSystem(const System* s) = 0;
virtual void EnterSystem(const System*) = 0;
virtual void ExistSystem(const System*) = 0;

virtual void EnterEntityMngr(const EntityMngr* em) = 0;
virtual void ExistEntityMngr(const EntityMngr* em) = 0;
virtual void EnterEntityMngr(const EntityMngr*) = 0;
virtual void ExistEntityMngr(const EntityMngr*) = 0;

virtual void EnterEntity(const Entity* e) = 0;
virtual void ExistEntity(const Entity* e) = 0;
virtual void EnterEntity(const Entity*) = 0;
virtual void ExistEntity(const Entity*) = 0;

virtual void EnterCmptPtr(const CmptPtr* cmpt) = 0;
virtual void ExistCmptPtr(const CmptPtr* cmpt) = 0;
virtual void EnterCmptPtr(const CmptPtr*) = 0;
virtual void ExistCmptPtr(const CmptPtr*) = 0;
};
}
32 changes: 16 additions & 16 deletions include/UECS/RTDCmptTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,38 @@ namespace Ubpa::UECS {
RTDCmptTraits& Clear();

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

// optional
RTDCmptTraits& RegisterAlignment(CmptType type, size_t alignment);
RTDCmptTraits& RegisterAlignment(CmptType, size_t alignment);

// optional
RTDCmptTraits& RegisterDefaultConstructor(CmptType type, std::function<void(void*)> f);
RTDCmptTraits& RegisterDefaultConstructor(CmptType, std::function<void(void*)>);

// optional
RTDCmptTraits& RegisterCopyConstructor(CmptType type, std::function<void(void*, void*)> f);
RTDCmptTraits& RegisterCopyConstructor(CmptType, std::function<void(void*, void*)>);

// optional
RTDCmptTraits& RegisterMoveConstructor(CmptType type, std::function<void(void*, void*)> f);
RTDCmptTraits& RegisterMoveConstructor(CmptType, std::function<void(void*, void*)>);

// optional
RTDCmptTraits& RegisterMoveAssignment(CmptType type, std::function<void(void*, void*)> f);
RTDCmptTraits& RegisterMoveAssignment(CmptType, std::function<void(void*, void*)>);

// optional
RTDCmptTraits& RegisterDestructor(CmptType type, std::function<void(void*)> f);
RTDCmptTraits& RegisterDestructor(CmptType, std::function<void(void*)>);

// optional
RTDCmptTraits& RegisterName(CmptType type, std::string name);
RTDCmptTraits& RegisterName(CmptType, std::string name);

size_t Sizeof(CmptType type) const;
size_t Alignof(CmptType type) const;
void CopyConstruct(CmptType type, void* dst, void* src) const;
void MoveConstruct(CmptType type, void* dst, void* src) const;
void MoveAssign(CmptType type, void* dst, void* src) const;
void Destruct(CmptType type, void* cmpt) const;
std::string_view Nameof(CmptType type) const;
size_t Sizeof(CmptType) const;
size_t Alignof(CmptType) const;
void CopyConstruct(CmptType, void* dst, void* src) const;
void MoveConstruct(CmptType, void* dst, void* src) const;
void MoveAssign(CmptType, void* dst, void* src) const;
void Destruct(CmptType, void* cmpt) const;
std::string_view Nameof(CmptType) const;

RTDCmptTraits& Deregister(CmptType type) noexcept;
RTDCmptTraits& Deregister(CmptType) noexcept;

template<typename... Cmpts>
void Register();
Expand Down
30 changes: 19 additions & 11 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,29 @@ namespace Ubpa::UECS {
class Schedule {
public:
template<typename Func>
const SystemFunc* Register(
Func&& func,
const SystemFunc* RegisterEntityJob(
Func&&,
std::string name,
ArchetypeFilter filter = ArchetypeFilter{},
SingletonLocator singletonLocator = SingletonLocator{}
ArchetypeFilter = {},
CmptLocator = {},
SingletonLocator = {}
);

// run-time dynamic function
template<typename Func>
const SystemFunc* Register(
Func&& func,
const SystemFunc* RegisterChunkJob(
Func&&,
std::string name,
CmptLocator locator,
ArchetypeFilter filter = ArchetypeFilter{},
SingletonLocator singletonLocator = SingletonLocator{});
ArchetypeFilter = {},
SingletonLocator = {}
);

// Mode::Job
template<typename Func>
const SystemFunc* RegisterJob(
Func&&,
std::string name,
SingletonLocator = {}
);

Schedule& LockFilter(std::string_view sys);

Expand All @@ -55,7 +63,7 @@ namespace Ubpa::UECS {

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

void Clear();

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

const std::set<CmptType>& SingletonTypes() const noexcept { return singletonTypes; }

bool HasWriteSingletonType() const noexcept;

private:
std::set<CmptType> singletonTypes;
};
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Ubpa::UECS {
World* GetWorld() const noexcept { return world; }
const std::string& GetName() const noexcept { return name; }

virtual void OnUpdate(Schedule& schedule) = 0;
virtual void OnUpdate(Schedule&) = 0;
private:
World* world;
std::string name;
Expand Down
20 changes: 13 additions & 7 deletions include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,33 @@ namespace Ubpa::UECS {
EntityQuery entityQuery;
SingletonLocator singletonLocator;

// Mode::Entity
template<typename Func>
SystemFunc(Func&& func, std::string name, ArchetypeFilter archetypeFilter, SingletonLocator singletonLocator);
SystemFunc(Func&&, std::string name, ArchetypeFilter, CmptLocator, SingletonLocator);

// Mode::Chunk
template<typename Func>
SystemFunc(Func&& func, std::string name, CmptLocator cmptLocator, ArchetypeFilter archetypeFilter, SingletonLocator singletonLocator);
SystemFunc(Func&&, std::string name, ArchetypeFilter, SingletonLocator);

// Mode::Job
template<typename Func>
SystemFunc(Func&&, std::string name, SingletonLocator);

const std::string& Name() const noexcept { return name; }

static constexpr size_t HashCode(std::string_view name) { return hash_string(name); }

size_t HashCode() const noexcept { return hashCode; }

void operator()(World*, SingletonsView singletonsView, Entity e, size_t entityIndexInQuery, CmptsView cmptsView);
void operator()(World*, SingletonsView singletonsView, ChunkView chunkView);
void operator()(World*, SingletonsView singletonsView);
void operator()(World*, SingletonsView, Entity, size_t entityIndexInQuery, CmptsView);
void operator()(World*, SingletonsView, ChunkView);
void operator()(World*, SingletonsView);

Mode GetMode() const noexcept { return mode; }

bool operator==(const SystemFunc& func) const noexcept { return name == func.name; }
bool operator==(const SystemFunc& sysFunc) const noexcept { return name == sysFunc.name; }
private:
std::function<void(World*, SingletonsView singletonsView, Entity, size_t, CmptsView, ChunkView)> func;
std::function<void(World*, SingletonsView, Entity, size_t, CmptsView, ChunkView)> func;

std::string name;
Mode mode;
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/SystemMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Ubpa::UECS{
template<typename... DerivedSystems>
void Deregister() noexcept;

void Accept(IListener* listener) const;
void Accept(IListener*) const;

private:
template<typename DerivedSystem>
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Ubpa::UECS {
// use RTDCmptTraits' registered component name
UGraphviz::Graph GenUpdateFrameGraph() const;

void Accept(IListener* listener) const;
void Accept(IListener*) const;

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

Expand Down
8 changes: 4 additions & 4 deletions include/UECS/detail/Archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace Ubpa::UECS {

Chunk* GetChunk(size_t chunkIdx) const { return chunks[chunkIdx]; }

void* At(CmptType type, size_t idx) const;
void* At(CmptType, size_t idx) const;

template<typename Cmpt>
Cmpt* At(size_t idx) const;
Expand All @@ -63,11 +63,11 @@ namespace Ubpa::UECS {
// init cmpts, set Entity
// size_t: index in archetype
template<typename... Cmpts>
std::tuple<size_t, std::tuple<Cmpts*...>> Create(Entity e);
size_t Create(Entity e);
std::tuple<size_t, std::tuple<Cmpts*...>> Create(Entity);
size_t Create(Entity);

// return index in archetype
size_t Instantiate(Entity e, size_t srcIdx);
size_t Instantiate(Entity, size_t srcIdx);

// erase idx-th entity
// if idx != num-1, back entity will put at idx, return moved Entity's index
Expand Down
22 changes: 18 additions & 4 deletions include/UECS/detail/Schedule.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,51 @@

namespace Ubpa::UECS {
template<typename Func>
const SystemFunc* Schedule::Register(
const SystemFunc* Schedule::RegisterEntityJob(
Func&& func,
std::string name,
ArchetypeFilter filter,
CmptLocator cmptLocator,
SingletonLocator singletonLocator)
{
return Request(
std::forward<Func>(func),
std::move(name),
std::move(filter),
std::move(cmptLocator),
std::move(singletonLocator)
);
}


template<typename Func>
const SystemFunc* Schedule::Register(
const SystemFunc* Schedule::RegisterChunkJob(
Func&& func,
std::string name,
CmptLocator locator,
ArchetypeFilter filter,
SingletonLocator singletonLocator
) {
return Request(
std::forward<Func>(func),
std::move(name),
std::move(locator),
std::move(filter),
std::move(singletonLocator)
);
}

template<typename Func>
const SystemFunc* Schedule::RegisterJob(
Func&& func,
std::string name,
SingletonLocator singletonLocator
) {
return Request(
std::forward<Func>(func),
std::move(name),
std::move(singletonLocator)
);
}

template<typename... Args>
const SystemFunc* Schedule::Request(Args&&... args) {
SystemFunc* sysFunc = sysFuncPool.Request(std::forward<Args>(args)...);
Expand Down
Empty file.
Loading

0 comments on commit d532d8b

Please sign in to comment.