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 5e8a364 commit 7369f4a
Show file tree
Hide file tree
Showing 29 changed files with 105 additions and 94 deletions.
2 changes: 1 addition & 1 deletion include/UECS/ArchetypeFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ namespace Ubpa::UECS {

size_t HashCode() const noexcept;

bool operator==(const ArchetypeFilter& rhs) const noexcept;
bool operator==(const ArchetypeFilter& rhs) const;
};
}
7 changes: 3 additions & 4 deletions include/UECS/ChunkView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Ubpa::UECS {

class ChunkView {
public:
ChunkView(Archetype* archetype, size_t chunkIdx, Chunk* chunk)
: archetype{ archetype }, chunkIdx{ chunkIdx }, chunk{ chunk } {}
ChunkView(Archetype* archetype, size_t chunkIdx)
: archetype{ archetype }, chunkIdx{ chunkIdx } {}

bool Contains(CmptType) const;

Expand All @@ -18,11 +18,10 @@ namespace Ubpa::UECS {
template<typename Cmpt>
Cmpt* GetCmptArray() const { return reinterpret_cast<Cmpt*>(GetCmptArray(CmptType::Of<Cmpt>)); }
const Entity* GetEntityArray() const { return GetCmptArray<Entity>(); }
size_t EntityNum() const;
size_t EntityNum() const noexcept;

private:
Archetype* archetype;
size_t chunkIdx;
Chunk* chunk;
};
}
2 changes: 1 addition & 1 deletion include/UECS/CmptLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Ubpa::UECS {

const std::set<CmptType>& CmptTypes() const noexcept { return cmptTypes; }

bool operator==(const CmptLocator& rhs) const noexcept;
bool operator==(const CmptLocator& rhs) const;
private:
size_t GenHashCode() const noexcept;

Expand Down
5 changes: 3 additions & 2 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Ubpa::UECS {
// CmptType + void*
class CmptPtr {
public:
CmptPtr(CmptType type, void* p) :type{ type }, p{ p }{}
CmptPtr(CmptType type, void* p) noexcept : type{ type }, p{ p }{}
template<typename Cmpt>
CmptPtr(Cmpt* p) : type{ CmptType::Of<Cmpt> }, p{ p }{}
CmptPtr(Cmpt* p) noexcept : type{ CmptType::Of<Cmpt> }, p{ p }{}

// unchecked
void* Ptr() const noexcept { return p; }
Expand All @@ -23,6 +23,7 @@ namespace Ubpa::UECS {
template<typename Cmpt>
Cmpt* As() const noexcept { return reinterpret_cast<Cmpt*>(p); }

// check: type's access mode must be equal to <mode>
template<typename Cmpt, AccessMode mode>
auto As() const noexcept {
assert(type.GetAccessMode() == mode);
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/CmptTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ namespace Ubpa::UECS {
IsLastFrameSingleton_v<T> ? AccessMode::LAST_FRAME_SINGLETON : (
IsWriteSingleton_v<T> ? AccessMode::WRITE_SINGLETON : (
IsLatestSingleton_v<T> ? AccessMode::LATEST_SINGLETON :
AccessMode::WRITE // default, TODO : use static_assert
AccessMode::WRITE // default
)))));
}

Expand Down
4 changes: 2 additions & 2 deletions include/UECS/CmptType.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Ubpa::UECS {
// use a hashcode to distinguish different type
class CmptType {
public:
explicit constexpr CmptType(size_t id, AccessMode mode = AccessMode::WRITE)
explicit constexpr CmptType(size_t id, AccessMode mode = AccessMode::WRITE) noexcept
: hashcode{ id }, mode{ mode } {}
explicit constexpr CmptType(std::string_view type_name, AccessMode mode = AccessMode::WRITE)
explicit constexpr CmptType(std::string_view type_name, AccessMode mode = AccessMode::WRITE) noexcept
: hashcode{ RuntimeTypeID(type_name) }, mode{ mode } {}

template<typename TaggedCmpt> // non-tagged component's access mode is AccessMode::WRITE
Expand Down
4 changes: 2 additions & 2 deletions include/UECS/CmptsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Ubpa::UECS {
class CmptsView {
public:
CmptsView(const CmptPtr* cmpts, size_t num)
CmptsView(const CmptPtr* cmpts, size_t num) noexcept
: cmpts{ cmpts }, num{ num } {}

CmptPtr GetCmpt(CmptType) const;
CmptPtr GetCmpt(CmptType) const noexcept;

const CmptPtr* Components() const noexcept { return cmpts; }
size_t NumberOfComponents() const noexcept { return num; }
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Ubpa::UECS {
private:
friend class EntityMngr;
friend class Archetype;
constexpr Entity(size_t idx, size_t version) : idx(idx), version(version) {}
constexpr Entity(size_t idx, size_t version) noexcept : idx(idx), version(version) {}
size_t idx;
size_t version;
};
Expand Down
5 changes: 2 additions & 3 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ namespace Ubpa::UECS {
// use RTDCmptTraits
void Attach(Entity, const CmptType* types, size_t num);

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

// use RTDCmptTraits
void Detach(Entity, const CmptType* types, size_t num);

bool Have(Entity, CmptType) const;
Expand Down Expand Up @@ -81,7 +80,7 @@ namespace Ubpa::UECS {
friend class World;
EntityMngr() = default;

static bool IsSet(const CmptType* types, size_t num);
static bool IsSet(const CmptType* types, size_t num) noexcept;

const std::set<Archetype*>& QueryArchetypes(const EntityQuery& query) 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 @@ -19,7 +19,7 @@ namespace Ubpa::UECS {

size_t HashCode() const noexcept { return hash_combine(filter.HashCode(), locator.HashCode()); }

bool operator==(const EntityQuery& query) const noexcept {
bool operator==(const EntityQuery& query) const {
return filter == query.filter && locator == query.locator;
}
};
Expand Down
3 changes: 1 addition & 2 deletions include/UECS/SingletonLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ namespace Ubpa::UECS {
class SingletonLocator {
public:
SingletonLocator(const CmptType* types, size_t num);

SingletonLocator();
SingletonLocator() = default;

template<typename Func>
static SingletonLocator Generate();
Expand Down
16 changes: 7 additions & 9 deletions include/UECS/SystemFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@
#include <functional>

namespace Ubpa::UECS {
// [description]
// [- description]
// system function registered by Schedule in <System>::OnUpdate(Schedule&)
// name + query + function<...>
// name + query(archetype filter + component locator) + singleton locator + function<...>
// 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*, SingletonsView
// query.filter can be change dynamically by other <System> with <Schedule>
// [- system function kind] (distinguish by argument list)
// common arguments : World*, SingletonsView, {LastFrame|Latest}<Singleton<Cmpt>>
// 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>>
// * Write<Singleton<Cmpt>> (only job can write singletons)
class SystemFunc {
public:
enum class Mode {
Expand All @@ -53,7 +51,7 @@ namespace Ubpa::UECS {

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

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

size_t HashCode() const noexcept { return hashCode; }

Expand Down
1 change: 1 addition & 0 deletions include/UECS/SystemMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Ubpa::UECS{
SystemMngr(World* world) : world { world } {}

void Register(std::unique_ptr<System> system) {
assert(system.get() != nullptr);
systems.emplace(system->GetName(), std::move(system));
}
bool IsRegister(std::string_view name) const {
Expand Down
6 changes: 3 additions & 3 deletions include/UECS/detail/Archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace Ubpa::UECS {
void* At(CmptType, size_t idx) const;

template<typename Cmpt>
Cmpt* At(size_t idx) const;
Cmpt* At(size_t idx) const{ return reinterpret_cast<Cmpt*>(At(CmptType::Of<Cmpt>, idx)); }

// no Entity
std::vector<CmptPtr> Components(size_t idx) const;
Expand All @@ -66,7 +66,7 @@ namespace Ubpa::UECS {

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

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

Expand Down Expand Up @@ -101,7 +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);
static bool NotContainEntity(const CmptType* types, size_t num) noexcept;

friend class EntityMngr;

Expand Down
6 changes: 1 addition & 5 deletions include/UECS/detail/Archetype.inl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Ubpa::UECS {

template<typename... Cmpts>
Archetype* Archetype::Add(const Archetype* from) {
static_assert(sizeof...(Cmpts) > 0);
assert(((!from->types.Contains(CmptType::Of<Cmpts>)) &&...));

Archetype* rst = new Archetype;
Expand All @@ -30,11 +31,6 @@ namespace Ubpa::UECS {
return rst;
}

template<typename Cmpt>
Cmpt* Archetype::At(size_t idx) const {
return reinterpret_cast<Cmpt*>(At(CmptType::Of<Cmpt>, idx));
}

template<typename... Cmpts>
std::tuple<size_t, std::tuple<Cmpts *...>> Archetype::Create(Entity e) {
assert((types.Contains(CmptType::Of<Cmpts>) &&...) && types.data.size() == 1 + sizeof...(Cmpts));
Expand Down
4 changes: 4 additions & 0 deletions include/UECS/detail/CmptTypeSet.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Ubpa::UECS {
inline void CmptTypeSet::Insert(const CmptType* types, size_t num) {
assert(types || num == 0);
for (size_t i = 0; i < num; i++)
data.insert(types[i]);
}

inline void CmptTypeSet::Erase(const CmptType* types, size_t num) {
assert(types || num == 0);
for (size_t i = 0; i < num; i++)
data.erase(types[i]);
}
Expand All @@ -16,6 +18,7 @@ namespace Ubpa::UECS {
}

inline bool CmptTypeSet::Contains(const CmptType* types, size_t num) const {
assert(types || num == 0);
for (size_t i = 0; i < num; i++) {
if (!Contains(types[i]))
return false;
Expand All @@ -33,6 +36,7 @@ namespace Ubpa::UECS {
}

inline bool CmptTypeSet::ContainsAny(const CmptType* types, size_t num) const {
assert(types || num == 0);
for (size_t i = 0; i < num; i++) {
if (Contains(types[i]))
return true;
Expand Down
42 changes: 21 additions & 21 deletions include/UECS/detail/RTDCmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@ namespace Ubpa::UECS {
}

inline RTDCmptTraits& RTDCmptTraits::RegisterSize(CmptType type, size_t size) {
sizeofs[type] = size;
sizeofs.emplace(type, size);
return *this;
}

inline RTDCmptTraits& RTDCmptTraits::RegisterAlignment(CmptType type, size_t alignment) {
alignments[type] = alignment;
alignments.emplace(type, alignment);
return *this;
}

inline RTDCmptTraits& RTDCmptTraits::RegisterDefaultConstructor(CmptType type, std::function<void(void*)> f) {
default_constructors[type] = std::move(f);
default_constructors.emplace(type, std::move(f));
return *this;
}

inline RTDCmptTraits& RTDCmptTraits::RegisterCopyConstructor(CmptType type, std::function<void(void*, void*)> f) {
copy_constructors[type] = std::move(f);
copy_constructors.emplace(type, std::move(f));
return *this;
}

inline RTDCmptTraits& RTDCmptTraits::RegisterMoveConstructor(CmptType type, std::function<void(void*, void*)> f) {
move_constructors[type] = std::move(f);
move_constructors.emplace(type, move(f));
return *this;
}

inline RTDCmptTraits& RTDCmptTraits::RegisterMoveAssignment(CmptType type, std::function<void(void*, void*)> f) {
move_assignments[type] = std::move(f);
move_assignments.emplace(type, std::move(f));
return *this;
}

inline RTDCmptTraits& RTDCmptTraits::RegisterDestructor(CmptType type, std::function<void(void*)> f) {
destructors[type] = std::move(f);
destructors.emplace(type, std::move(f));
return *this;
}

inline RTDCmptTraits& RTDCmptTraits::RegisterName(CmptType type, std::string name) {
names[type] = std::move(name);
names.emplace(type, std::move(name));
return *this;
}

Expand Down Expand Up @@ -114,34 +114,34 @@ namespace Ubpa::UECS {

constexpr CmptType type = CmptType::Of<Cmpt>;

sizeofs[type] = sizeof(Cmpt);
alignments[type] = alignof(Cmpt);
names[type] = std::string{ nameof::nameof_type<Cmpt>() };
sizeofs.emplace(type, sizeof(Cmpt));
alignments.emplace(type, alignof(Cmpt));
names.emplace(type, std::string{ nameof::nameof_type<Cmpt>() });

if constexpr (!std::is_trivially_default_constructible_v<Cmpt>) {
default_constructors[type] = [](void* cmpt) {
default_constructors.emplace(type, [](void* cmpt) {
new(cmpt)Cmpt;
};
});
}
if constexpr (!std::is_trivially_destructible_v<Cmpt>) {
destructors[type] = [](void* cmpt) {
destructors.emplace(type, [](void* cmpt) {
reinterpret_cast<Cmpt*>(cmpt)->~Cmpt();
};
});
}
if constexpr (!std::is_trivially_move_constructible_v<Cmpt>) {
move_constructors[type] = [](void* dst, void* src) {
move_constructors.emplace(type, [](void* dst, void* src) {
new(dst)Cmpt(std::move(*reinterpret_cast<Cmpt*>(src)));
};
});
}
if constexpr (!std::is_trivially_move_assignable_v<Cmpt>) {
move_assignments[type] = [](void* dst, void* src) {
move_assignments.emplace(type, [](void* dst, void* src) {
*reinterpret_cast<Cmpt*>(dst) = std::move(*reinterpret_cast<Cmpt*>(src));
};
});
}
if constexpr (!std::is_trivially_copy_constructible_v<Cmpt>) {
copy_constructors[type] = [](void* dst, void* src) {
copy_constructors.emplace(type, [](void* dst, void* src) {
new(dst)Cmpt(*reinterpret_cast<Cmpt*>(src));
};
});
}
}

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

template<typename Cmpt>
void Register();

// use RTDCmptTraits
void Register(CmptType type);

template<typename Cmpt>
Expand Down
Loading

0 comments on commit 7369f4a

Please sign in to comment.