Skip to content

Commit

Permalink
clear up RTDCmptTraits
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed May 17, 2020
1 parent fbd9a4a commit 8813bdb
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 40 deletions.
56 changes: 18 additions & 38 deletions include/UECS/RTDCmptTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,33 @@ namespace Ubpa {
public:
static constexpr size_t default_alignment = alignof(std::max_align_t);

static RTDCmptTraits& Instance() noexcept {
static RTDCmptTraits instance;
return instance;
}
inline static RTDCmptTraits& Instance() noexcept;

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

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

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

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

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

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

RTDCmptTraits& Deregister(CmptType type) {
sizeofs.erase(type);
alignments.erase(type);
default_constructors.erase(type);
copy_constructors.erase(type);
move_constructors.erase(type);
destructors.erase(type);
return *this;
}
inline RTDCmptTraits& RegisterDestructor(CmptType type, std::function<void(void*)> f);

inline RTDCmptTraits& Deregister(CmptType type) noexcept;

template<typename Cmpt>
void Register();

template<typename Cmpt>
void Deregister();

private:
friend class RTSCmptTraits;
Expand All @@ -76,6 +53,7 @@ namespace Ubpa {

RTDCmptTraits() = default;


std::unordered_map<CmptType, size_t> sizeofs;
std::unordered_map<CmptType, size_t> alignments;
std::unordered_map<CmptType, std::function<void(void*)>> default_constructors; // dst <- src
Expand All @@ -84,3 +62,5 @@ namespace Ubpa {
std::unordered_map<CmptType, std::function<void(void*)>> destructors;
};
}

#include "detail/RTDCmptTraits.inl"
99 changes: 99 additions & 0 deletions include/UECS/detail/RTDCmptTraits.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#pragma once

#include "../RTDCmptTraits.h"

namespace Ubpa {
inline RTDCmptTraits& RTDCmptTraits::Instance() noexcept {
static RTDCmptTraits instance;
return instance;
}

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

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

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

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

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

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

template<typename Cmpt>
void RTDCmptTraits::Register() {
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_destructible_v<Cmpt>, "<Cmpt> must be destructible");

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

sizeofs[type] = sizeof(Cmpt);
alignments[type] = alignof(Cmpt);

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

template<typename Cmpt>
void RTDCmptTraits::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_move_constructible_v<Cmpt>)
move_constructors.erase(type);
if constexpr (!std::is_trivially_copy_constructible_v<Cmpt>)
copy_constructors.erase(type);
}

inline RTDCmptTraits& RTDCmptTraits::Deregister(CmptType type) noexcept {
sizeofs.erase(type);
alignments.erase(type);
default_constructors.erase(type);
copy_constructors.erase(type);
move_constructors.erase(type);
destructors.erase(type);
return *this;
}
}
2 changes: 1 addition & 1 deletion include/UECS/detail/RTSCmptTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace Ubpa {

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

private:
std::unordered_map<CmptType, size_t> sizeofs;
Expand Down
2 changes: 1 addition & 1 deletion include/UECS/detail/RTSCmptTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace Ubpa {
move_constructors[type] = move_constructor_target->second;
}

inline void RTSCmptTraits::Deregister(CmptType type) {
inline void RTSCmptTraits::Deregister(CmptType type) noexcept {
sizeofs.erase(type);
alignments.erase(type);
copy_constructors.erase(type);
Expand Down

0 comments on commit 8813bdb

Please sign in to comment.