Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Sep 22, 2020
1 parent 4bdf72e commit b3cc7e2
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 87 deletions.
10 changes: 5 additions & 5 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include "SystemFunc.h"
#include "detail/Archetype.h"
#include "detail/Job.h"
#include "EntityQuery.h"
#include "SingletonLocator.h"

namespace Ubpa::UECS {
class World;

class SystemFunc;
class IListener;

// Entity Manager of World
Expand All @@ -24,10 +24,12 @@ namespace Ubpa::UECS {
// - when free entries is empty, use new entity entry (version is 0)
class EntityMngr {
public:
EntityMngr(World* world) : world{ world }, sharedChunkPool{ std::make_unique<Pool<Chunk>>() } {}
EntityMngr();
EntityMngr(const EntityMngr& em);
~EntityMngr();

RTDCmptTraits cmptTraits;

// same world
void Swap(EntityMngr& rhs) noexcept;

Expand Down Expand Up @@ -91,8 +93,6 @@ namespace Ubpa::UECS {
void Accept(IListener* listener) const;

private:
World* world;

friend class World;
friend class Archetype;

Expand Down
17 changes: 1 addition & 16 deletions include/UECS/RTDCmptTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,17 @@ namespace Ubpa::UECS {
// - name
class RTDCmptTraits {
public:
static constexpr size_t default_alignment = alignof(std::max_align_t);
static constexpr size_t DefaultAlignment() noexcept { return alignof(std::max_align_t); }

RTDCmptTraits& Clear();

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

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

// optional
RTDCmptTraits& RegisterDefaultConstructor(CmptType, void(*)(void*));

// optional
RTDCmptTraits& RegisterCopyConstructor(CmptType, void(*)(void*, void*));

// optional
RTDCmptTraits& RegisterMoveConstructor(CmptType, void(*)(void*, void*));

// optional
RTDCmptTraits& RegisterMoveAssignment(CmptType, void(*)(void*, void*));

// optional
RTDCmptTraits& RegisterDestructor(CmptType, void(*)(void*));

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

const auto& GetSizeofs() const noexcept { return sizeofs; }
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/Schedule.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "SystemFunc.h"
#include "detail/SystemFunc.h"
#include "detail/SysFuncGraph.h"
#include "detail/Job.h"

Expand Down
1 change: 0 additions & 1 deletion include/UECS/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace Ubpa::UECS {

SystemMngr systemMngr;
EntityMngr entityMngr;
RTDCmptTraits cmptTraits;

// 1. schedule: run registered System's static OnUpdate(Schedule&)
// 2. gen job graph: schedule -> graph
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 @@ -4,7 +4,7 @@
#include "../CmptPtr.h"
#include "../CmptLocator.h"

#include "RTSCmptTraits.h"
#include "ArchetypeCmptTraits.h"
#include "CmptTypeSet.h"
#include "Chunk.h"

Expand Down Expand Up @@ -86,7 +86,7 @@ namespace Ubpa::UECS {

// Components + Entity
const CmptTypeSet& GetCmptTypeSet() const noexcept { return types; }
const RTSCmptTraits& GetRTSCmptTraits() const noexcept { return cmptTraits; }
const ArchetypeCmptTraits& GetArchetypeCmptTraits() const noexcept { return cmptTraits; }

size_t EntityNum() const noexcept { return entityNum; }
size_t EntityNumOfChunk(size_t chunkIdx) const noexcept;
Expand All @@ -109,7 +109,7 @@ namespace Ubpa::UECS {
friend class EntityMngr;

CmptTypeSet types; // Entity + Components
RTSCmptTraits cmptTraits;
ArchetypeCmptTraits cmptTraits;
std::unordered_map<CmptType, size_t> type2offset; // CmptType to offset in chunk (include Entity)

size_t chunkCapacity{ size_t_invalid };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Ubpa::UECS {
// run-time static component traits
class RTSCmptTraits {
class ArchetypeCmptTraits {
public:
size_t Sizeof(CmptType) const;
size_t Alignof(CmptType) const;
Expand Down Expand Up @@ -38,4 +38,4 @@ namespace Ubpa::UECS {
};
}

#include "RTSCmptTraits.inl"
#include "ArchetypeCmptTraits.inl"
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
#include <stdexcept>

namespace Ubpa::UECS {
inline size_t RTSCmptTraits::Sizeof(CmptType type) const {
assert(sizeofs.find(type) != sizeofs.end());
return sizeofs.find(type)->second;
inline size_t ArchetypeCmptTraits::Sizeof(CmptType type) const {
auto target = sizeofs.find(type);
assert(target != sizeofs.end());
return target->second;
}

inline size_t RTSCmptTraits::Alignof(CmptType type) const {
assert(alignments.find(type) != alignments.end());
return alignments.find(type)->second;
inline size_t ArchetypeCmptTraits::Alignof(CmptType type) const {
auto target = alignments.find(type);
assert(target != alignments.end());
return target->second;
}

inline void RTSCmptTraits::CopyConstruct(CmptType type, void* dst, void* src) const {
inline void ArchetypeCmptTraits::CopyConstruct(CmptType type, void* dst, void* src) const {
auto target = copy_constructors.find(type);

if (target != copy_constructors.end())
Expand All @@ -22,7 +24,7 @@ namespace Ubpa::UECS {
memcpy(dst, src, Sizeof(type));
}

inline void RTSCmptTraits::MoveConstruct(CmptType type, void* dst, void* src) const {
inline void ArchetypeCmptTraits::MoveConstruct(CmptType type, void* dst, void* src) const {
auto target = move_constructors.find(type);

if (target != move_constructors.end())
Expand All @@ -31,7 +33,7 @@ namespace Ubpa::UECS {
memcpy(dst, src, Sizeof(type));
}

inline void RTSCmptTraits::MoveAssign(CmptType type, void* dst, void* src) const {
inline void ArchetypeCmptTraits::MoveAssign(CmptType type, void* dst, void* src) const {
auto target = move_assignments.find(type);

if (target != move_assignments.end())
Expand All @@ -40,14 +42,14 @@ namespace Ubpa::UECS {
memcpy(dst, src, Sizeof(type));
}

inline void RTSCmptTraits::Destruct(CmptType type, void* cmpt) const {
inline void ArchetypeCmptTraits::Destruct(CmptType type, void* cmpt) const {
auto target = destructors.find(type);
if (target != destructors.end())
target->second(cmpt);
}

template<typename Cmpt>
void RTSCmptTraits::Register() {
void ArchetypeCmptTraits::Register() {
static_assert(!IsTaggedCmpt_v<Cmpt>, "<Cmpt> should not be tagged");
static_assert(std::is_copy_constructible_v<Cmpt>, "<Cmpt> must be copy-constructible");
static_assert(std::is_move_constructible_v<Cmpt>, "<Cmpt> must be move-constructible");
Expand Down Expand Up @@ -82,7 +84,7 @@ namespace Ubpa::UECS {
}

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

sizeofs.erase(type);
Expand All @@ -98,16 +100,14 @@ namespace Ubpa::UECS {
move_assignments.erase(type);
}

inline void RTSCmptTraits::Register(const RTDCmptTraits& rtdct, CmptType type) {
inline void ArchetypeCmptTraits::Register(const RTDCmptTraits& rtdct, CmptType type) {
auto size_target = rtdct.GetSizeofs().find(type);
if (size_target == rtdct.GetSizeofs().end())
throw std::logic_error("RTSCmptTraits::Register: RTDCmptTraits hasn't registered <CmptType>");
throw std::logic_error("ArchetypeCmptTraits::Register: RTDCmptTraits hasn't registered <CmptType>");
sizeofs[type] = size_target->second;

auto alignment_target = rtdct.GetAlignments().find(type);
if (alignment_target == rtdct.GetAlignments().end())
alignments[type] = RTDCmptTraits::default_alignment;
else
if (alignment_target != rtdct.GetAlignments().end())
alignments[type] = alignment_target->second;

auto destructor_target = rtdct.GetDestructors().find(type);
Expand All @@ -125,7 +125,7 @@ namespace Ubpa::UECS {
move_assignments.emplace(type, move_assignments_target->second);
}

inline void RTSCmptTraits::Deregister(CmptType type) noexcept {
inline void ArchetypeCmptTraits::Deregister(CmptType type) noexcept {
sizeofs.erase(type);
alignments.erase(type);
copy_constructors.erase(type);
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/EntityMngr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace Ubpa::UECS {
size_t srcIdxInArchetype = info.idxInArchetype;
size_t dstIdxInArchetype = dstArchetype->RequestBuffer();

auto srcCmptTraits = srcArchetype->GetRTSCmptTraits();
auto srcCmptTraits = srcArchetype->GetArchetypeCmptTraits();
for (const auto& type : srcCmptTypeSet.data) {
auto srcCmpt = srcArchetype->At(type, srcIdxInArchetype);
auto dstCmpt = dstArchetype->At(type, dstIdxInArchetype);
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/RTDCmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace Ubpa::UECS {
inline size_t RTDCmptTraits::Alignof(CmptType type) const {
auto target = alignments.find(type);

return target != alignments.end() ? target->second : default_alignment;
return target != alignments.end() ? target->second : DefaultAlignment();
}

inline void RTDCmptTraits::DefaultConstruct(CmptType type, void* cmpt) const {
Expand Down
14 changes: 7 additions & 7 deletions include/UECS/SystemFunc.h → include/UECS/detail/SystemFunc.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include "EntityQuery.h"
#include "SingletonLocator.h"
#include "Entity.h"
#include "CmptsView.h"
#include "SingletonsView.h"
#include "ChunkView.h"
#include "../EntityQuery.h"
#include "../SingletonLocator.h"
#include "../Entity.h"
#include "../CmptsView.h"
#include "../SingletonsView.h"
#include "../ChunkView.h"

#include <functional>

Expand Down Expand Up @@ -73,4 +73,4 @@ namespace Ubpa::UECS {
};
}

#include "detail/SystemFunc.inl"
#include "SystemFunc.inl"
46 changes: 25 additions & 21 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
#include <UECS/EntityMngr.h>

#include <UECS/detail/SystemFunc.h>
#include <UECS/World.h>
#include <UECS/IListener.h>

using namespace Ubpa::UECS;
using namespace std;

size_t EntityMngr::RequestEntityFreeEntry() {
if (entityTableFreeEntry.empty()) {
size_t index = entityTable.size();
entityTable.emplace_back();
return index;
}

size_t entry = entityTableFreeEntry.back();
entityTableFreeEntry.pop_back();
return entry;
}
EntityMngr::EntityMngr()
: sharedChunkPool{ std::make_unique<Pool<Chunk>>() }
{}

EntityMngr::EntityMngr(const EntityMngr& em)
: world{ em.world },
:
sharedChunkPool{ std::make_unique<Pool<Chunk>>() }
{
ts2a.reserve(em.ts2a.size());
Expand Down Expand Up @@ -50,12 +43,23 @@ EntityMngr::~EntityMngr() {
sharedChunkPool->FastClear();
}

void EntityMngr::Swap(EntityMngr& rhs) noexcept {
assert(world == rhs.world);
size_t EntityMngr::RequestEntityFreeEntry() {
if (entityTableFreeEntry.empty()) {
size_t index = entityTable.size();
entityTable.emplace_back();
return index;
}

size_t entry = entityTableFreeEntry.back();
entityTableFreeEntry.pop_back();
return entry;
}

void EntityMngr::Swap(EntityMngr& rhs) noexcept {
using std::swap;

swap(ts2a, rhs.ts2a);
swap(cmptTraits, rhs.cmptTraits);
swap(entityTableFreeEntry, rhs.entityTableFreeEntry);
swap(entityTable, rhs.entityTable);
swap(queryCache, rhs.queryCache);
Expand All @@ -79,7 +83,7 @@ Archetype* EntityMngr::GetOrCreateArchetypeOf(const CmptType* types, size_t num)
if (target != ts2a.end())
return target->second.get();

auto archetype = Archetype::New(world->cmptTraits, sharedChunkPool.get(), types, num);
auto archetype = Archetype::New(cmptTraits, sharedChunkPool.get(), types, num);

ts2a.emplace(std::move(typeset), std::unique_ptr<Archetype>{ archetype });
for (auto& [query, archetypes] : queryCache) {
Expand All @@ -96,7 +100,7 @@ Entity EntityMngr::Create(const CmptType* types, size_t num) {
EntityInfo& info = entityTable[entityIndex];
Entity e{ entityIndex, info.version };
info.archetype = archetype;
info.idxInArchetype = archetype->Create(world->cmptTraits, e);
info.idxInArchetype = archetype->Create(cmptTraits, e);
return e;
}

Expand All @@ -117,7 +121,7 @@ Archetype* EntityMngr::AttachWithoutInit(Entity e, const CmptType* types, size_t
Archetype* dstArchetype;
auto target = ts2a.find(dstCmptTypeSet);
if (target == ts2a.end()) {
dstArchetype = Archetype::Add(world->cmptTraits, srcArchetype, types, num);
dstArchetype = Archetype::Add(cmptTraits, srcArchetype, types, num);
assert(dstCmptTypeSet == dstArchetype->GetCmptTypeSet());
for (auto& [query, archetypes] : queryCache) {
if (dstCmptTypeSet.IsMatch(query))
Expand All @@ -134,7 +138,7 @@ Archetype* EntityMngr::AttachWithoutInit(Entity e, const CmptType* types, size_t
// move src to dst
size_t dstIdxInArchetype = dstArchetype->RequestBuffer();

const auto& srcCmptTraits = srcArchetype->GetRTSCmptTraits();
const auto& srcCmptTraits = srcArchetype->GetArchetypeCmptTraits();
for (const auto& type : srcCmptTypeSet.data) {
auto srcCmpt = srcArchetype->At(type, srcIdxInArchetype);
auto dstCmpt = dstArchetype->At(type, dstIdxInArchetype);
Expand All @@ -160,8 +164,8 @@ void EntityMngr::Attach(Entity e, const CmptType* types, size_t num) {
if (origArchetype->GetCmptTypeSet().Contains(type))
continue;

auto target = world->cmptTraits.GetDefaultConstructors().find(type);
if (target == world->cmptTraits.GetDefaultConstructors().end())
auto target = cmptTraits.GetDefaultConstructors().find(type);
if (target == cmptTraits.GetDefaultConstructors().end())
continue;

target->second(info.archetype->At(type, info.idxInArchetype));
Expand Down Expand Up @@ -204,7 +208,7 @@ void EntityMngr::Detach(Entity e, const CmptType* types, size_t num) {
size_t srcIdxInArchetype = info.idxInArchetype;
size_t dstIdxInArchetype = dstArchetype->RequestBuffer();

const auto& srcCmptTraits = srcArchetype->GetRTSCmptTraits();
const auto& srcCmptTraits = srcArchetype->GetArchetypeCmptTraits();
for (const auto& type : dstCmptTypeSet.data) {
auto srcCmpt = srcArchetype->At(type, srcIdxInArchetype);
auto dstCmpt = dstArchetype->At(type, dstIdxInArchetype);
Expand Down
2 changes: 1 addition & 1 deletion src/core/SystemFunc.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <UECS/SystemFunc.h>
#include <UECS/detail/SystemFunc.h>

using namespace Ubpa::UECS;

Expand Down
Loading

0 comments on commit b3cc7e2

Please sign in to comment.