Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 10, 2020
1 parent 7ca7d42 commit 5e8a364
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 70 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.4)
project(UECS VERSION 0.11.5)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand Down
1 change: 0 additions & 1 deletion include/UECS/ChunkView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Ubpa::UECS {
class Archetype;
struct Chunk;

// you shourld get singleton (registered in locator) by world
class ChunkView {
public:
ChunkView(Archetype* archetype, size_t chunkIdx, Chunk* chunk)
Expand Down
1 change: 1 addition & 0 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Ubpa::UECS {

template<typename Cmpt, AccessMode mode>
auto As() const noexcept {
assert(type.GetAccessMode() == mode);
if constexpr (mode == AccessMode::LAST_FRAME)
return LastFrame<Cmpt>{p};
else if constexpr (mode == AccessMode::WRITE)
Expand Down
3 changes: 3 additions & 0 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace Ubpa::UECS {
// use RTDCmptTraits
void Attach(Entity, const CmptType* types, size_t num);

// if not exist cmpt, attach with Args...
// else return it directly
template<typename Cmpt, typename... Args>
Cmpt* Emplace(Entity, Args&&...);

Expand Down Expand Up @@ -85,6 +87,7 @@ namespace Ubpa::UECS {

template<typename... Cmpts>
Archetype* GetOrCreateArchetypeOf();
// types not contain Entity
Archetype* GetOrCreateArchetypeOf(const CmptType* types, size_t num);

template<typename... Cmpts>
Expand Down
24 changes: 20 additions & 4 deletions include/UECS/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ namespace Ubpa::UECS {
// schedule will be clear at the beginning of the next World::Update()
class Schedule {
public:
// Func's argument list:
// World*
// {LastFrame|Latest}<Singleton<Cmpt>>
// SingletonsView
// Entity
// size_t indexInQuery
// <tagged-components>: {LastFrame|Write|Latest}<Cmpt>...
// CmptsView
template<typename Func>
const SystemFunc* RegisterEntityJob(
Func&&,
Expand All @@ -34,26 +42,34 @@ namespace Ubpa::UECS {
SingletonLocator = {}
);

// Func's argument list:
// World*
// {LastFrame|Latest}<Singleton<Cmpt>>
// SingletonsView
// ChunkView (necessary)
template<typename Func>
const SystemFunc* RegisterChunkJob(
Func&&,
std::string name,
ArchetypeFilter = {},
SingletonLocator = {}
);

// Mode::Job

// Func's argument list:
// World*
// {LastFrame|Write|Latest}<Singleton<Cmpt>>
// SingletonsView
template<typename Func>
const SystemFunc* RegisterJob(
Func&&,
std::string name,
SingletonLocator = {}
);

Schedule& LockFilter(std::string_view sys);

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

Schedule& LockFilter(std::string_view sys);

Schedule& InsertAll(std::string_view sys, CmptType);
Schedule& InsertAny(std::string_view sys, CmptType);
Schedule& InsertNone(std::string_view sys, CmptType);
Expand Down
22 changes: 13 additions & 9 deletions include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ namespace Ubpa::UECS {
// [description]
// system function registered by Schedule in <System>::OnUpdate(Schedule&)
// name + query + function<...>
// name must be unique in global
// name('s hashcode) must be unique in global
// query.filter can be change dynamically by other <System> with Schedule
// [system function kind] (distinguish by argument list)
// common : [World*], [{LastFrame|Write|Latest}Singleton<Component>], [SingletonsView]
// 1. per entity function
// * [Entity]
// * [size_t indexInQuery]
// * <tagged-component>: {LastFrame|Write|Latest}<Component>
// * [CmptsView]
// 2. chunk: ChunkView
// 3. job
// common : World*, SingletonsView
// 1. Mode::Entity: per entity function
// * {LastFrame|Latest}<Singleton<Cmpt>>
// * Entity
// * size_t indexInQuery
// * <tagged-components>: {LastFrame|Write|Latest}<Cmpt>...
// * CmptsView
// 2. Mode::Chunk
// * {LastFrame|Latest}<Singleton<Cmpt>>
// * ChunkView (necessary)
// 3. Mode::Job
// * {LastFrame|Write|Latest}<Singleton<Cmpt>>
class SystemFunc {
public:
enum class Mode {
Expand Down
4 changes: 3 additions & 1 deletion include/UECS/detail/Archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ namespace Ubpa::UECS {
~Archetype();

// Entity + Components
// without singleton
std::tuple<std::vector<Entity*>, std::vector<std::vector<CmptPtr>>, std::vector<size_t>>
Locate(const CmptLocator& locator) const;

Expand All @@ -64,6 +63,8 @@ namespace Ubpa::UECS {
// size_t: index in archetype
template<typename... Cmpts>
std::tuple<size_t, std::tuple<Cmpts*...>> Create(Entity);

// use RTDCmptTraits's default constructor
size_t Create(Entity);

// return index in archetype
Expand Down Expand Up @@ -100,6 +101,7 @@ namespace Ubpa::UECS {
void SetLayout();

size_t Offsetof(CmptType type) const { return type2offset.find(type)->second; }
static bool NotContainEntity(const CmptType* types, size_t num);

friend class EntityMngr;

Expand Down
4 changes: 2 additions & 2 deletions include/UECS/detail/Archetype.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Ubpa::UECS {
: types(GenCmptTypeSet<Cmpts...>())
{
static_assert(IsSet_v<TypeList<Entity, Cmpts...>>,
"Archetype::Archetype: <Cmpts> must be different");
"Archetype::Archetype: <Cmpts>... must be different");
cmptTraits.Register<Entity>();
(cmptTraits.Register<Cmpts>(), ...);
SetLayout();
Expand Down Expand Up @@ -41,7 +41,7 @@ namespace Ubpa::UECS {
static_assert((std::is_constructible_v<Cmpts> &&...),
"Archetype::Create: <Cmpts> isn't constructible");
static_assert(IsSet_v<TypeList<Entity, Cmpts...>>,
"Archetype::Create: <Cmpts> must be different");
"Archetype::Create: <Cmpts>... must be different");

size_t idx = RequestBuffer();
size_t idxInChunk = idx % chunkCapacity;
Expand Down
2 changes: 2 additions & 0 deletions include/UECS/detail/CmptTypeSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ namespace Ubpa::UECS {
void Insert(const CmptType* types, size_t num);
void Erase(const CmptType* types, size_t num);
bool Contains(CmptType type) const;
bool Contains(const CmptType* types, size_t num) const;

template<typename CmptTypeContainer>
bool Contains(const CmptTypeContainer& types) const;

bool ContainsAny(const CmptType* types, size_t num) const;
template<typename CmptTypeContainer>
bool ContainsAny(const CmptTypeContainer& types) const;

Expand Down
16 changes: 16 additions & 0 deletions include/UECS/detail/CmptTypeSet.inl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace Ubpa::UECS {
return data.find(type) != data.end();
}

inline bool CmptTypeSet::Contains(const CmptType* types, size_t num) const {
for (size_t i = 0; i < num; i++) {
if (!Contains(types[i]))
return false;
}
return true;
}

template<typename CmptTypeContainer>
bool CmptTypeSet::Contains(const CmptTypeContainer& types) const {
for (const auto& type : types) {
Expand All @@ -24,6 +32,14 @@ namespace Ubpa::UECS {
return true;
}

inline bool CmptTypeSet::ContainsAny(const CmptType* types, size_t num) const {
for (size_t i = 0; i < num; i++) {
if (Contains(types[i]))
return true;
}
return false;
}

template<typename CmptTypeContainer>
bool CmptTypeSet::ContainsAny(const CmptTypeContainer& types) const {
if (types.empty())
Expand Down
35 changes: 15 additions & 20 deletions include/UECS/detail/EntityMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Ubpa::UECS {
template<typename... Cmpts>
Archetype* EntityMngr::GetOrCreateArchetypeOf() {
static_assert(IsSet_v<TypeList<Entity, Cmpts...>>,
"EntityMngr::GetOrCreateArchetypeOf: <Cmpts> must be different");
"EntityMngr::GetOrCreateArchetypeOf: <Cmpts>... must be different");

const auto typeset = Archetype::GenCmptTypeSet<Cmpts...>();
auto target = ts2a.find(typeset);
Expand All @@ -29,7 +29,7 @@ namespace Ubpa::UECS {
template<typename... Cmpts>
std::tuple<Entity, Cmpts*...> EntityMngr::Create() {
static_assert(IsSet_v<TypeList<Entity, Cmpts...>>,
"EntityMngr::Create: <Cmpts> must be different");
"EntityMngr::Create: <Cmpts>... must be different");
Archetype* archetype = GetOrCreateArchetypeOf<Cmpts...>();
size_t entityIndex = RequestEntityFreeEntry();
EntityInfo& info = entityTable[entityIndex];
Expand All @@ -42,7 +42,9 @@ namespace Ubpa::UECS {

template<typename... Cmpts>
void EntityMngr::AttachWithoutInit(Entity e) {
assert(Exist(e));
static_assert(IsSet_v<TypeList<Entity, Cmpts...>>,
"EntityMngr::AttachWithoutInit: <Cmpts>... must be different");
if (!Exist(e)) throw std::invalid_argument("Entity is invalid");

auto& info = entityTable[e.Idx()];
Archetype* srcArchetype = info.archetype;
Expand Down Expand Up @@ -91,16 +93,15 @@ namespace Ubpa::UECS {

template<typename... Cmpts>
std::tuple<Cmpts*...> EntityMngr::Attach(Entity e) {
static_assert((std::is_constructible_v<Cmpts> &&...),
static_assert((std::is_default_constructible_v<Cmpts> &&...),
"EntityMngr::Attach: <Cmpts> isn't default constructible");
if (!Exist(e)) throw std::invalid_argument("Entity is invalid");

using CmptList = TypeList<Cmpts...>;
const auto& cmptTypes = entityTable[e.Idx()].archetype->GetCmptTypeSet();
std::array<bool, sizeof...(Cmpts)> needAttach = { !cmptTypes.Contains(CmptType::Of<Cmpts>)... };
std::array needAttach = { !cmptTypes.Contains(CmptType::Of<Cmpts>)... };
AttachWithoutInit<Cmpts...>(e);
const auto& new_info = entityTable[e.Idx()];
std::tuple<Cmpts*...> cmpts{ new_info.archetype->At<Cmpts>(new_info.idxInArchetype)... };
std::tuple cmpts{ new_info.archetype->At<Cmpts>(new_info.idxInArchetype)... };
((std::get<Find_v<CmptList, Cmpts>>(needAttach) ? new(std::get<Cmpts*>(cmpts))Cmpts : nullptr), ...);

return cmpts;
Expand All @@ -111,35 +112,29 @@ namespace Ubpa::UECS {
static_assert(std::is_constructible_v<Cmpt, Args...>
|| is_list_initializable_v<Cmpt, Args...>,
"EntityMngr::Emplace: <Cmpt> isn't constructible/list_initializable with Args...");
if (!Exist(e)) throw std::invalid_argument("EntityMngr::Emplace: Entity is invalid");

bool needAttach = !entityTable[e.Idx()].archetype->GetCmptTypeSet().Contains(CmptType::Of<Cmpt>);
if (needAttach) {
if (!Have(e, CmptType::Of<Cmpt>)) {
AttachWithoutInit<Cmpt>(e);
const auto& info = entityTable[e.Idx()];
Cmpt* cmpt = info.archetype->At<Cmpt>(info.idxInArchetype);
return new(cmpt)Cmpt{ std::forward<Args>(args)... };
}
else {
const auto& info = entityTable[e.Idx()];
return info.archetype->At<Cmpt>(info.idxInArchetype);
return new(Get<Cmpt>(e))Cmpt{ std::forward<Args>(args)... };
}
else
return Get<Cmpt>(e);
}

inline bool EntityMngr::Have(Entity e, CmptType type) const {
assert(!type.Is<Entity>());
if (!Exist(e)) throw std::invalid_argument("EntityMngr::Have: Entity is invalid");
return entityTable[e.Idx()].archetype->GetCmptTypeSet().Contains(type);
}

template<typename Cmpt>
Cmpt* EntityMngr::Get(Entity e) const {
static_assert(!std::is_same_v<Cmpt, Entity>, "EntityMngr::Get: <Cmpt> != Entity");
if (!Exist(e)) throw std::invalid_argument("EntityMngr::Get: Entity is invalid");
const auto& info = entityTable[e.Idx()];
return info.archetype->At<Cmpt>(info.idxInArchetype);
return reinterpret_cast<Cmpt*>(Get(e, CmptType::Of<Cmpt>).Ptr());
}

inline CmptPtr EntityMngr::Get(Entity e, CmptType type) const {
assert(!type.Is<Entity>());
if (!Exist(e)) throw std::invalid_argument("EntityMngr::Get: Entity is invalid");
const auto& info = entityTable[e.Idx()];
return { type, info.archetype->At(type, info.idxInArchetype) };
Expand Down
Loading

0 comments on commit 5e8a364

Please sign in to comment.