Skip to content

Commit

Permalink
improve resource management scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Mar 23, 2021
1 parent a601785 commit 653d6ab
Show file tree
Hide file tree
Showing 16 changed files with 521 additions and 411 deletions.
36 changes: 15 additions & 21 deletions include/UECS/CmptTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ namespace Ubpa::UECS {
CmptTraits& RegisterMoveAssignment(TypeID, std::function<void(void*,void*)>);
CmptTraits& RegisterDestructor(TypeID, std::function<void(void*)>);

const auto& GetSizeofs() const noexcept { return sizeofs; }
const auto& GetAlignments() const noexcept { return alignments; };
const auto& GetDefaultConstructors() const noexcept { return default_constructors; }
const auto& GetCopyConstructors() const noexcept { return copy_constructors; }
const auto& GetMoveConstructors() const noexcept { return move_constructors; }
const auto& GetMoveAssignments() const noexcept { return move_assignments; }
const auto& GetDestructors() const noexcept { return destructors; }
const auto& GetNames() const noexcept { return names; }
const std::pmr::unordered_set<TypeID>& GetTrivials() const noexcept;
const std::pmr::unordered_map<TypeID, std::string_view>& GetNames() const noexcept;
const std::pmr::unordered_map<TypeID, std::size_t>& GetSizeofs() const noexcept;
const std::pmr::unordered_map<TypeID, std::size_t>& GetAlignments() const noexcept;
const std::pmr::unordered_map<TypeID, std::function<void(void*, std::pmr::memory_resource*)>>& GetDefaultConstructors() const noexcept;
const std::pmr::unordered_map<TypeID, std::function<void(void*, const void*, std::pmr::memory_resource*)>>& GetCopyConstructors() const noexcept;
const std::pmr::unordered_map<TypeID, std::function<void(void*, void*, std::pmr::memory_resource*)>>& GetMoveConstructors() const noexcept;
const std::pmr::unordered_map<TypeID, std::function<void(void*, void*)>>& GetMoveAssignments() const noexcept;
const std::pmr::unordered_map<TypeID, std::function<void(void*)>>& GetDestructors() const noexcept;

bool IsTrivial(TypeID) const;
std::size_t Sizeof(TypeID) const;
Expand All @@ -64,9 +65,10 @@ namespace Ubpa::UECS {

private:
friend class EntityMngr;
CmptTraits();
CmptTraits(const CmptTraits& other);
CmptTraits(CmptTraits&& other) noexcept = default;
CmptTraits(std::pmr::unsynchronized_pool_resource* rsrc);
CmptTraits(const CmptTraits& other, std::pmr::unsynchronized_pool_resource* rsrc);
CmptTraits(CmptTraits&& other) noexcept;
~CmptTraits();

// register all for Cmpt
// static_assert
Expand All @@ -81,16 +83,8 @@ namespace Ubpa::UECS {
template<typename Cmpt>
void UnsafeRegisterOne();

std::unique_ptr<std::pmr::unsynchronized_pool_resource> rsrc;
std::unordered_set<TypeID> trivials;
std::unordered_map<TypeID, std::string_view> names;
std::unordered_map<TypeID, std::size_t> sizeofs;
std::unordered_map<TypeID, std::size_t> alignments;
std::unordered_map<TypeID, std::function<void(void*, std::pmr::memory_resource*)>> default_constructors; // dst <- src
std::unordered_map<TypeID, std::function<void(void*, const void*, std::pmr::memory_resource*)>> copy_constructors; // dst <- src
std::unordered_map<TypeID, std::function<void(void*, void*, std::pmr::memory_resource*)>> move_constructors; // dst <- src
std::unordered_map<TypeID, std::function<void(void*, void*)>> move_assignments; // dst <- src
std::unordered_map<TypeID, std::function<void(void*)>> destructors;
struct Impl;
std::unique_ptr<Impl> impl;
};
}

Expand Down
19 changes: 6 additions & 13 deletions include/UECS/EntityMngr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,15 @@ namespace Ubpa::UECS {

void Clear();



private:
EntityMngr(std::pmr::synchronized_pool_resource* sync_rsrc, synchronized_monotonic_buffer_resource* sync_frame_rsrc);
EntityMngr(const EntityMngr&, std::pmr::synchronized_pool_resource* sync_rsrc, synchronized_monotonic_buffer_resource* sync_frame_rsrc);
EntityMngr(EntityMngr&&, std::pmr::synchronized_pool_resource* sync_rsrc, synchronized_monotonic_buffer_resource* sync_frame_rsrc) noexcept;
friend class World;

EntityMngr(World* world);
EntityMngr(const EntityMngr&, World* world);
EntityMngr(EntityMngr&&, World* world) noexcept;
~EntityMngr();

friend class World;
friend class Archetype;
World* world;

static bool IsSet(std::span<const TypeID> types) noexcept;

Expand All @@ -113,10 +112,6 @@ namespace Ubpa::UECS {
// if job is nullptr, direct run
bool AutoGen(World*, Job*, SystemFunc*, int layer) const;

std::uint64_t version{ 0 };
std::pmr::synchronized_pool_resource* sync_rsrc;
synchronized_monotonic_buffer_resource* sync_frame_rsrc;

struct EntityInfo {
Archetype* archetype{ nullptr };
std::size_t chunkIdx{ static_cast<std::size_t>(-1) };
Expand All @@ -128,12 +123,10 @@ namespace Ubpa::UECS {
std::size_t RequestEntityFreeEntry();
void RecycleEntityEntry(Entity);

std::unique_ptr<std::pmr::unsynchronized_pool_resource> rsrc;
struct TypeIDSetHash {
std::size_t operator()(const small_flat_set<TypeID>& types) const noexcept;
};
std::unordered_map<small_flat_set<TypeID>, std::unique_ptr<Archetype>, TypeIDSetHash> ts2a; // archetype's TypeIDSet to archetype
void NewFrame() noexcept;
void UpdateVersion(std::uint64_t world_version) noexcept;
};
}
14 changes: 7 additions & 7 deletions include/UECS/SystemMngr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace Ubpa::UECS {
public:
SystemTraits systemTraits;

const auto& GetAliveSystemIDs() const noexcept { return aliveSystemIDs; }
const auto& GetActiveSystemIDs() const noexcept { return activeSystemIDs; }
const std::pmr::unordered_set<Ubpa::NameID>& GetAliveSystemIDs() const noexcept;
const std::pmr::unordered_set<Ubpa::NameID>& GetActiveSystemIDs() const noexcept;

// not alive -> create
void Create(NameID);
Expand Down Expand Up @@ -56,17 +56,17 @@ namespace Ubpa::UECS {
private:
friend class World;

SystemMngr(World* w) : w{ w } {}
SystemMngr(World* w);
SystemMngr(const SystemMngr& mngr, World* w);
SystemMngr(SystemMngr&& mngr, World* w) noexcept;
~SystemMngr();

World* w;
void Update(NameID, Schedule&) const;
void Clear();

std::unordered_set<NameID> aliveSystemIDs;
std::unordered_set<NameID> activeSystemIDs;

World* w;
struct Impl;
std::unique_ptr<Impl> impl;
};
}

Expand Down
24 changes: 9 additions & 15 deletions include/UECS/SystemTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ namespace Ubpa::UECS {
using OnDeactivate = void(World*);
using OnDestroy = void(World*);

SystemTraits() = default;
SystemTraits(const SystemTraits&);
SystemTraits(SystemTraits&&) noexcept = default;
SystemTraits& operator=(const SystemTraits&);
SystemTraits& operator=(SystemTraits&&) noexcept = default;

// register system's name and get an ID
// if it is already registered, return it's ID directly
Name Register(std::string_view name);
Expand All @@ -59,7 +53,7 @@ namespace Ubpa::UECS {

std::string_view Nameof(NameID ID) const noexcept;
bool IsRegistered(NameID ID) const noexcept;
const auto& GetNames() const noexcept { return names; }
const std::pmr::unordered_map<NameID, std::string_view>& GetNames() const noexcept;

// [ Template ] functions
///////////////////////////
Expand All @@ -79,20 +73,20 @@ namespace Ubpa::UECS {
private:
friend class SystemMngr;

SystemTraits(std::pmr::unsynchronized_pool_resource*);
SystemTraits(const SystemTraits&, std::pmr::unsynchronized_pool_resource*);
SystemTraits(SystemTraits&&) noexcept;
~SystemTraits();
SystemTraits& operator=(SystemTraits&&) noexcept = delete;

void Create (NameID, World*) const;
void Activate (NameID, World*) const;
void Update (NameID, Schedule&) const;
void Deactivate(NameID, World*) const;
void Destroy (NameID, World*) const;

std::pmr::synchronized_pool_resource rsrc;
std::unordered_map<NameID, std::string_view> names;

std::unordered_map<NameID, std::function<OnCreate >> createMap;
std::unordered_map<NameID, std::function<OnActivate >> activateMap;
std::unordered_map<NameID, std::function<OnUpdate >> updateMap;
std::unordered_map<NameID, std::function<OnDeactivate>> deactivateMap;
std::unordered_map<NameID, std::function<OnDestroy >> destroyMap;
struct Impl;
std::unique_ptr<Impl> impl;
};
}

Expand Down
2 changes: 2 additions & 0 deletions include/UECS/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ namespace Ubpa::UECS {

public:
World();
World(std::pmr::memory_resource* upstream);
World(const World&);
World(const World&, std::pmr::memory_resource* upstream);
World(World&&) noexcept;
~World();

Expand Down
55 changes: 23 additions & 32 deletions include/UECS/details/CmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -40,59 +40,59 @@ namespace Ubpa::UECS {
constexpr Type type = Type_of<Cmpt>;

if constexpr (std::is_trivial_v<Cmpt>)
trivials.insert(TypeID_of<Cmpt>);
RegisterTrivial(type.GetID());

sizeofs.emplace(type.GetID(), sizeof(Cmpt));
alignments.emplace(type.GetID(), alignof(Cmpt));
names.emplace(type.GetID(), type.GetName());
RegisterSize(type.GetID(), sizeof(Cmpt));
RegisterAlignment(type.GetID(), alignof(Cmpt));
RegisterName(type);

if constexpr (details::DefaultCtorWithAlloc<Cmpt>) {
default_constructors.emplace(type.GetID(), [](void* cmpt, std::pmr::memory_resource* world_rsrc) {
RegisterDefaultConstructor(type.GetID(), [](void* cmpt, std::pmr::memory_resource* world_rsrc) {
using Alloc = typename Cmpt::allocator_type;
Alloc alloc(world_rsrc);
std::allocator_traits<Alloc>::template construct(alloc, reinterpret_cast<Cmpt*>(cmpt));
});
}
else if constexpr (std::is_default_constructible_v<Cmpt>) {
default_constructors.emplace(type.GetID(), [](void* cmpt, std::pmr::memory_resource*) {
RegisterDefaultConstructor(type.GetID(), [](void* cmpt, std::pmr::memory_resource*) {
new(cmpt)Cmpt();
});
}

if constexpr (std::is_destructible_v<Cmpt> && !std::is_trivially_destructible_v<Cmpt>) {
destructors.emplace(type.GetID(), [](void* cmpt) {
RegisterDestructor(type.GetID(), [](void* cmpt) {
static_cast<Cmpt*>(cmpt)->~Cmpt();
});
}

if constexpr (details::MoveCtorWithAlloc<Cmpt>) {
move_constructors.emplace(type.GetID(), [](void* dst, void* src, std::pmr::memory_resource* world_rsrc) {
RegisterMoveConstructor(type.GetID(), [](void* dst, void* src, std::pmr::memory_resource* world_rsrc) {
using Alloc = typename Cmpt::allocator_type;
Alloc alloc(world_rsrc);
std::allocator_traits<Alloc>::template construct(alloc, reinterpret_cast<Cmpt*>(dst), std::move(*static_cast<const Cmpt*>(src)));
});
}
else if constexpr (std::is_move_constructible_v<Cmpt> && !std::is_trivially_move_constructible_v<Cmpt>) {
move_constructors.emplace(type.GetID(), [](void* dst, void* src, std::pmr::memory_resource*) {
RegisterMoveConstructor(type.GetID(), [](void* dst, void* src, std::pmr::memory_resource*) {
new(dst)Cmpt(std::move(*static_cast<Cmpt*>(src)));
});
}

if constexpr (std::is_move_assignable_v<Cmpt> && !std::is_trivially_move_assignable_v<Cmpt>) {
move_assignments.emplace(type.GetID(), [](void* dst, void* src) {
RegisterMoveAssignment(type.GetID(), [](void* dst, void* src) {
*static_cast<Cmpt*>(dst) = std::move(*static_cast<Cmpt*>(src));
});
}

if constexpr (details::CopyCtorWithAlloc<Cmpt>) {
copy_constructors.emplace(type.GetID(), [](void* dst, const void* src, std::pmr::memory_resource* world_rsrc) {
RegisterCopyConstructor(type.GetID(), [](void* dst, const void* src, std::pmr::memory_resource* world_rsrc) {
using Alloc = typename Cmpt::allocator_type;
Alloc alloc(world_rsrc);
std::allocator_traits<Alloc>::template construct(alloc, reinterpret_cast<Cmpt*>(dst), *static_cast<const Cmpt*>(src));
});
}
else if constexpr (std::is_copy_constructible_v<Cmpt> && !std::is_trivially_copy_constructible_v<Cmpt>) {
copy_constructors.emplace(type.GetID(), [](void* dst, const void* src, std::pmr::memory_resource*) {
RegisterCopyConstructor(type.GetID(), [](void* dst, const void* src, std::pmr::memory_resource*) {
new(dst)Cmpt(*static_cast<const Cmpt*>(src));
});
}
Expand All @@ -101,9 +101,16 @@ namespace Ubpa::UECS {
template<typename Cmpt>
void CmptTraits::RegisterOne() {
static_assert(!IsTaggedCmpt_v<Cmpt>, "<Cmpt> should not be tagged");
static_assert(std::is_default_constructible_v<Cmpt> || details::DefaultCtorWithAlloc<Cmpt>, "<Cmpt> must be default-constructible or with alloc");
static_assert(std::is_copy_constructible_v<Cmpt> || details::CopyCtorWithAlloc<Cmpt>, "<Cmpt> must be copy-constructible or with alloc");
static_assert(std::is_move_constructible_v<Cmpt> || details::MoveCtorWithAlloc<Cmpt>, "<Cmpt> must be move-constructible or with alloc");
if constexpr (details::ContainPmrAlloc<Cmpt>) {
static_assert(details::DefaultCtorWithAlloc<Cmpt>, "alloc-awared <Cmpt> must be default-constructible with alloc");
static_assert(details::CopyCtorWithAlloc<Cmpt>, "alloc-awared <Cmpt> must be copy-constructible with alloc");
static_assert(details::MoveCtorWithAlloc<Cmpt>, "alloc-awared <Cmpt> must be move-constructible with alloc");
}
else {
static_assert(std::is_default_constructible_v<Cmpt>, "<Cmpt> must be default-constructible");
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");
}
static_assert(std::is_move_assignable_v<Cmpt>, "<Cmpt> must be move-assignable");
static_assert(std::is_destructible_v<Cmpt>, "<Cmpt> must be destructible");

Expand All @@ -113,22 +120,6 @@ namespace Ubpa::UECS {
template<typename Cmpt>
void CmptTraits::Deregister() {
constexpr TypeID type = TypeID_of<Cmpt>;

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

if constexpr (std::is_trivial_v<Cmpt>)
trivials.erase(TypeID_of<Cmpt>);

default_constructors.erase(type);
if constexpr (!std::is_trivially_destructible_v<Cmpt>)
destructors.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);
if constexpr (!std::is_trivially_copy_constructible_v<Cmpt>)
copy_constructors.erase(type);
Deregister(type);
}
}
Loading

0 comments on commit 653d6ab

Please sign in to comment.