Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Nov 10, 2020
1 parent be99291 commit 57f71aa
Show file tree
Hide file tree
Showing 35 changed files with 149 additions and 141 deletions.
4 changes: 2 additions & 2 deletions 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.13.2)
project(UECS VERSION 0.13.3)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Expand All @@ -9,7 +9,7 @@ Ubpa_InitUCMake()
Ubpa_InitProject()

Ubpa_AddDep(UContainer 0.0.7)
Ubpa_AddDep(UTemplate 0.4.8)
Ubpa_AddDep(UTemplate 0.4.9)
Ubpa_AddDep(UGraphviz 0.1.5)

Ubpa_AddSubDirsRec(include)
Expand Down
2 changes: 1 addition & 1 deletion cmake/InitUCMake.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
macro(Ubpa_InitUCMake)
include(FetchContent)
set(UCMake_VERSION 0.6.2)
set(UCMake_VERSION 0.6.3)
message(STATUS "find package: UCMake ${UCMake_VERSION}")
find_package(UCMake ${UCMake_VERSION} EXACT QUIET)
if(NOT UCMake_FOUND)
Expand Down
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 {

bool HaveWriteCmptType() const noexcept;

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

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

bool Contains(CmptType) const;

// nullptr if not contain
void* GetCmptArray(CmptType) const;
template<typename Cmpt>
Cmpt* GetCmptArray() const { return reinterpret_cast<Cmpt*>(GetCmptArray(CmptType::Of<Cmpt>)); }
const Entity* GetEntityArray() const { return GetCmptArray<Entity>(); }
Cmpt* GetCmptArray() const { return static_cast<Cmpt*>(GetCmptArray(CmptType::Of<Cmpt>)); }
const Entity* GetEntityArray() const noexcept { return GetCmptArray<Entity>(); }
size_t EntityNum() const noexcept;

private:
Expand Down
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 CmptAccessTypeSet& CmptAccessTypes() const noexcept { return cmptTypes; }

bool operator==(const CmptLocator& rhs) const;
bool operator==(const CmptLocator& rhs) const noexcept;

bool HasWriteCmptType() const noexcept;
private:
Expand Down
14 changes: 7 additions & 7 deletions include/UECS/CmptPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Ubpa::UECS {

constexpr CmptType Type() const noexcept { return type; }

static constexpr CmptPtr Invalid() { return { CmptType::Invalid(), nullptr }; };
static constexpr CmptPtr Invalid() noexcept { return { CmptType::Invalid(), nullptr }; };
constexpr bool Valid() const noexcept { return p != nullptr && type.Valid(); }

template<typename Cmpt>
Expand All @@ -44,7 +44,7 @@ namespace Ubpa::UECS {

constexpr CmptAccessType AccessType() const noexcept { return accessType; }

static constexpr CmptAccessPtr Invalid() { return { CmptAccessType::Invalid(), nullptr }; };
static constexpr CmptAccessPtr Invalid() noexcept { return { CmptAccessType::Invalid(), nullptr }; };
constexpr bool Valid() const noexcept { return p != nullptr && accessType.Valid(); }

// check: type's access mode must be equal to <mode>
Expand All @@ -54,15 +54,15 @@ namespace Ubpa::UECS {
if constexpr (mode == AccessMode::LAST_FRAME)
return LastFrame<Cmpt>{p};
else if constexpr (mode == AccessMode::WRITE)
return Write<Cmpt>(p);
return Write<Cmpt>{p};
else if constexpr (mode == AccessMode::LATEST)
return Latest<Cmpt>(p);
return Latest<Cmpt>{p};
else if constexpr (mode == AccessMode::LAST_FRAME_SINGLETON)
return LastFrame<Singleton<Cmpt>>;
return LastFrame<Singleton<Cmpt>>{p};
else if constexpr (mode == AccessMode::WRITE_SINGLETON)
return Write<Singleton<Cmpt>>;
return Write<Singleton<Cmpt>>{p};
else if constexpr (mode == AccessMode::LATEST_SINGLETON)
return Latest<Singleton<Cmpt>>;
return Latest<Singleton<Cmpt>>{p};
else
static_assert(false);
}
Expand Down
12 changes: 6 additions & 6 deletions include/UECS/CmptTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace Ubpa::UECS {
// 6. Latest<Singleton<Cmpt>>

enum class AccessMode : size_t {
LAST_FRAME = 0, // 000, LastFrame<Cmpt>
WRITE = 1, // 001, Write<Cmpt> / Cmpt*
LATEST = 2, // 010, Latest<Cmpt> / const Cmpt*
LAST_FRAME_SINGLETON = 4, // 100, LastFrame<Singleton<Cmpt>>
WRITE_SINGLETON = 5, // 101, Write<Singleton<Cmpt>> / Singleton<Cmpt>
LATEST_SINGLETON = 6, // 110, Latest<Singleton<Cmpt>>
LAST_FRAME = 0b000, // LastFrame<Cmpt>
WRITE = 0b001, // Write<Cmpt> / Cmpt*
LATEST = 0b010, // Latest<Cmpt> / const Cmpt*
LAST_FRAME_SINGLETON = 0b100, // LastFrame<Singleton<Cmpt>>
WRITE_SINGLETON = 0b101, // Write<Singleton<Cmpt>> / Singleton<Cmpt>
LATEST_SINGLETON = 0b110, // Latest<Singleton<Cmpt>>
};

constexpr bool AccessMode_IsSingleton(AccessMode mode) noexcept {
Expand Down
7 changes: 6 additions & 1 deletion include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ namespace Ubpa::UECS {
// use RTDCmptTraits
void Attach(Entity, const CmptType* types, size_t num);

// assert(Have(e, CmptType::Of<Cmpt>))
template<typename Cmpt, typename... Args>
Cmpt* Emplace(Entity, Args&&...);

// use Detach(Entity, const CmptType*, size_t)
template<typename... Cmpts>
void Detach(Entity);
void Detach(Entity, const CmptType* types, size_t num);

// use Have(Entity, CmptType)
template<typename Cmpt>
bool Have(Entity) const;
bool Have(Entity, CmptType) const;

// nullptr if not containts <Cmpt>
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/EntityQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Ubpa::UECS {

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

bool operator==(const EntityQuery& query) const {
bool operator==(const EntityQuery& query) const noexcept {
return filter == query.filter && locator == query.locator;
}
};
Expand Down
2 changes: 0 additions & 2 deletions include/UECS/SingletonLocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include "CmptTag.h"

#include "CmptType.h"

#include <set>
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/SingletonsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Ubpa::UECS {
class SingletonsView {
public:
SingletonsView(const CmptAccessPtr* singletons, size_t num)
SingletonsView(const CmptAccessPtr* singletons, size_t num) noexcept
: singletons{ singletons }, num{ num } {}

CmptAccessPtr GetSingleton(CmptAccessType) const;
Expand Down
4 changes: 2 additions & 2 deletions include/UECS/SystemMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Ubpa::UECS {
template<typename System>
size_t GetIndex() const;

void Clear();
void Clear() noexcept;

size_t Register(std::string name, Func);
// name: nameof::nameof_type<System>
Expand All @@ -43,7 +43,7 @@ namespace Ubpa::UECS {
void Unregister();

void Activate(size_t index);
void Deactivate(size_t index);
void Deactivate(size_t index) noexcept;
private:
std::vector<SystemInfo> systems;
std::vector<size_t> frees;
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/Archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Ubpa::UECS {
// type of Entity + Components is Archetype's type
class Archetype {
public:
Archetype(Pool<Chunk>* chunkPool) : chunkPool{ chunkPool } {}
Archetype(Pool<Chunk>* chunkPool) noexcept : chunkPool{ chunkPool } {}

// argument TypeList<Cmpts...> is for type deduction
// auto add Entity
Expand Down
9 changes: 5 additions & 4 deletions include/UECS/detail/Archetype.inl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Ubpa::UECS {
template<typename... Cmpts>
Archetype::Archetype(Pool<Chunk>* chunkPool, TypeList<Cmpts...>)
: chunkPool{ chunkPool },
types(GenCmptTypeSet<Cmpts...>())
:
types(GenCmptTypeSet<Cmpts...>()),
chunkPool{ chunkPool }
{
static_assert(IsSet_v<TypeList<Entity, Cmpts...>>,
"<Cmpts>... must be different");
Expand Down Expand Up @@ -61,9 +62,9 @@ namespace Ubpa::UECS {
"<Cmpts>... must be different");

constexpr std::array types = { CmptType::Of<Cmpts>... };
return Archetype::GenCmptTypeSet(types.data(), types.size());
return GenCmptTypeSet(types.data(), types.size());
}
else
return Archetype::GenCmptTypeSet(nullptr, 0);
return GenCmptTypeSet(nullptr, 0);
}
}
2 changes: 1 addition & 1 deletion include/UECS/detail/ArchetypeCmptTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Ubpa::UECS {
void Register(const RTDCmptTraits&, CmptType);

template<typename Cmpt>
void Deregister();
void Deregister() noexcept;
void Deregister(CmptType) noexcept;

private:
Expand Down
42 changes: 21 additions & 21 deletions include/UECS/detail/ArchetypeCmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -62,43 +62,26 @@ namespace Ubpa::UECS {

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

template<typename Cmpt>
void ArchetypeCmptTraits::Deregister() {
constexpr CmptType type = CmptType::Of<Cmpt>;

sizeofs.erase(type);
alignments.erase(type);

if constexpr (!std::is_trivially_destructible_v<Cmpt>)
destructors.erase(type);
if constexpr (!std::is_trivially_copy_constructible_v<Cmpt>)
copy_constructors.erase(type);
if constexpr (!std::is_trivially_move_constructible_v<Cmpt>)
move_constructors.erase(type);
if constexpr (!std::is_trivially_move_assignable_v<Cmpt>)
move_assignments.erase(type);
}

inline void ArchetypeCmptTraits::Register(const RTDCmptTraits& rtdct, CmptType type) {
auto size_target = rtdct.GetSizeofs().find(type);
if (size_target == rtdct.GetSizeofs().end())
Expand All @@ -124,6 +107,23 @@ namespace Ubpa::UECS {
move_assignments.emplace(type, move_assignments_target->second);
}

template<typename Cmpt>
void ArchetypeCmptTraits::Deregister() noexcept {
constexpr CmptType type = CmptType::Of<Cmpt>;

sizeofs.erase(type);
alignments.erase(type);

if constexpr (!std::is_trivially_destructible_v<Cmpt>)
destructors.erase(type);
if constexpr (!std::is_trivially_copy_constructible_v<Cmpt>)
copy_constructors.erase(type);
if constexpr (!std::is_trivially_move_constructible_v<Cmpt>)
move_constructors.erase(type);
if constexpr (!std::is_trivially_move_assignable_v<Cmpt>)
move_assignments.erase(type);
}

inline void ArchetypeCmptTraits::Deregister(CmptType type) noexcept {
sizeofs.erase(type);
alignments.erase(type);
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/CmptTag.inl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Ubpa::UECS {

namespace Ubpa::UECS {
template<typename TaggedCmpt>
void* CastToVoidPointer(TaggedCmpt p) {
void* CastToVoidPointer(TaggedCmpt p) noexcept {
return const_cast<void*>(reinterpret_cast<const void*>(p));
}

Expand Down
6 changes: 3 additions & 3 deletions include/UECS/detail/CmptTypeSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace Ubpa::UECS {
struct CmptTypeSet {
std::set<CmptType> data;

size_t HashCode() const;
size_t HashCode() const noexcept;

void Insert(const CmptType* types, size_t num);
void Erase(const CmptType* types, size_t num);
void Erase(const CmptType* types, size_t num) noexcept;
bool Contains(CmptType type) const;

bool ContainsAll(const CmptType* types, size_t num) const;
Expand All @@ -35,7 +35,7 @@ namespace Ubpa::UECS {

bool IsMatch(const EntityQuery& query) const;

bool operator==(const CmptTypeSet& rhs) const { return data == rhs.data; }
bool operator==(const CmptTypeSet& rhs) const noexcept { return data == rhs.data; }
};
}

Expand Down
4 changes: 2 additions & 2 deletions include/UECS/detail/CmptTypeSet.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Ubpa::UECS {
data.insert(types[i]);
}

inline void CmptTypeSet::Erase(const CmptType* types, size_t num) {
inline void CmptTypeSet::Erase(const CmptType* types, size_t num) noexcept {
assert(types || num == 0);
for (size_t i = 0; i < num; i++)
data.erase(types[i]);
Expand Down Expand Up @@ -84,7 +84,7 @@ namespace Ubpa::UECS {
return IsMatch(query.filter) && IsMatch(query.locator);
}

inline size_t CmptTypeSet::HashCode() const {
inline size_t CmptTypeSet::HashCode() const noexcept {
size_t seed = TypeID<CmptTypeSet>;
for (const auto& t : data)
seed = hash_combine(seed, t.HashCode());
Expand Down
11 changes: 11 additions & 0 deletions include/UECS/detail/EntityMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ namespace Ubpa::UECS {
return cmpts;
}

template<typename... Cmpts>
void EntityMngr::Detach(Entity e) {
constexpr std::array types{ CmptType::Of<Cmpts>... };
Detach(e, types.data, types.size());
}

template<typename Cmpt>
bool EntityMngr::Have(Entity e) const {
return Have(e, CmptType::Of<Cmpt>);
}

template<typename Cmpt, typename... Args>
Cmpt* EntityMngr::Emplace(Entity e, Args&&... args) {
static_assert(std::is_constructible_v<Cmpt, Args...>
Expand Down
8 changes: 4 additions & 4 deletions include/UECS/detail/RTDCmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,22 @@ namespace Ubpa::UECS {
}
if constexpr (!std::is_trivially_destructible_v<Cmpt>) {
destructors.emplace(type, [](void* cmpt) {
reinterpret_cast<Cmpt*>(cmpt)->~Cmpt();
static_cast<Cmpt*>(cmpt)->~Cmpt();
});
}
if constexpr (!std::is_trivially_move_constructible_v<Cmpt>) {
move_constructors.emplace(type, [](void* dst, void* src) {
new(dst)Cmpt(std::move(*reinterpret_cast<Cmpt*>(src)));
new(dst)Cmpt(std::move(*static_cast<Cmpt*>(src)));
});
}
if constexpr (!std::is_trivially_move_assignable_v<Cmpt>) {
move_assignments.emplace(type, [](void* dst, void* src) {
*reinterpret_cast<Cmpt*>(dst) = std::move(*reinterpret_cast<Cmpt*>(src));
*static_cast<Cmpt*>(dst) = std::move(*static_cast<Cmpt*>(src));
});
}
if constexpr (!std::is_trivially_copy_constructible_v<Cmpt>) {
copy_constructors.emplace(type, [](void* dst, void* src) {
new(dst)Cmpt(*reinterpret_cast<Cmpt*>(src));
new(dst)Cmpt(*static_cast<Cmpt*>(src));
});
}
}
Expand Down
Loading

0 comments on commit 57f71aa

Please sign in to comment.