explicit entity and component ctors

This commit is contained in:
2019-10-15 13:27:49 +07:00
parent 67fc2054a4
commit 1f5d08a675
2 changed files with 33 additions and 14 deletions

View File

@@ -859,7 +859,7 @@ namespace ecs_hpp
{ {
class entity final { class entity final {
public: public:
entity(registry& owner) noexcept; explicit entity(registry& owner) noexcept;
entity(registry& owner, entity_id id) noexcept; entity(registry& owner, entity_id id) noexcept;
entity(const entity&) = default; entity(const entity&) = default;
@@ -952,7 +952,7 @@ namespace ecs_hpp
public: public:
const_entity(const entity& ent) noexcept; const_entity(const entity& ent) noexcept;
const_entity(const registry& owner) noexcept; explicit const_entity(const registry& owner) noexcept;
const_entity(const registry& owner, entity_id id) noexcept; const_entity(const registry& owner, entity_id id) noexcept;
const_entity(const const_entity&) = default; const_entity(const const_entity&) = default;
@@ -1019,7 +1019,7 @@ namespace ecs_hpp
template < typename T > template < typename T >
class component final { class component final {
public: public:
component(const entity& owner) noexcept; explicit component(const entity& owner) noexcept;
component(const component&) = default; component(const component&) = default;
component& operator=(const component&) = default; component& operator=(const component&) = default;
@@ -1030,7 +1030,7 @@ namespace ecs_hpp
entity& owner() noexcept; entity& owner() noexcept;
const entity& owner() const noexcept; const entity& owner() const noexcept;
bool remove() noexcept; bool valid() const noexcept;
bool exists() const noexcept; bool exists() const noexcept;
template < typename... Args > template < typename... Args >
@@ -1039,6 +1039,8 @@ namespace ecs_hpp
template < typename... Args > template < typename... Args >
T& ensure(Args&&... args); T& ensure(Args&&... args);
bool remove() noexcept;
T& get(); T& get();
const T& get() const; const T& get() const;
@@ -1092,7 +1094,7 @@ namespace ecs_hpp
class const_component final { class const_component final {
public: public:
const_component(const component<T>& comp) noexcept; const_component(const component<T>& comp) noexcept;
const_component(const const_entity& owner) noexcept; explicit const_component(const const_entity& owner) noexcept;
const_component(const const_component&) = default; const_component(const const_component&) = default;
const_component& operator=(const const_component&) = default; const_component& operator=(const const_component&) = default;
@@ -1102,6 +1104,7 @@ namespace ecs_hpp
const const_entity& owner() const noexcept; const const_entity& owner() const noexcept;
bool valid() const noexcept;
bool exists() const noexcept; bool exists() const noexcept;
const T& get() const; const T& get() const;
@@ -1283,7 +1286,6 @@ namespace ecs_hpp
}; };
public: public:
registry() = default; registry() = default;
~registry() noexcept = default;
registry(const registry& other) = delete; registry(const registry& other) = delete;
registry& operator=(const registry& other) = delete; registry& operator=(const registry& other) = delete;
@@ -1788,8 +1790,8 @@ namespace ecs_hpp
} }
template < typename T > template < typename T >
bool component<T>::remove() noexcept { bool component<T>::valid() const noexcept {
return owner_.remove_component<T>(); return owner_.valid();
} }
template < typename T > template < typename T >
@@ -1800,15 +1802,18 @@ namespace ecs_hpp
template < typename T > template < typename T >
template < typename... Args > template < typename... Args >
T& component<T>::assign(Args&&... args) { T& component<T>::assign(Args&&... args) {
return owner_.assign_component<T>( return owner_.assign_component<T>(std::forward<Args>(args)...);
std::forward<Args>(args)...);
} }
template < typename T > template < typename T >
template < typename... Args > template < typename... Args >
T& component<T>::ensure(Args&&... args) { T& component<T>::ensure(Args&&... args) {
return owner_.ensure_component<T>( return owner_.ensure_component<T>(std::forward<Args>(args)...);
std::forward<Args>(args)...); }
template < typename T >
bool component<T>::remove() noexcept {
return owner_.remove_component<T>();
} }
template < typename T > template < typename T >
@@ -1903,6 +1908,11 @@ namespace ecs_hpp
return owner_; return owner_;
} }
template < typename T >
bool const_component<T>::valid() const noexcept {
return owner_.valid();
}
template < typename T > template < typename T >
bool const_component<T>::exists() const noexcept { bool const_component<T>::exists() const noexcept {
return std::as_const(owner_).template exists_component<T>(); return std::as_const(owner_).template exists_component<T>();
@@ -2212,12 +2222,12 @@ namespace ecs_hpp
template < typename T > template < typename T >
component<T> registry::wrap_component(const const_uentity& ent) noexcept { component<T> registry::wrap_component(const const_uentity& ent) noexcept {
return {wrap_entity(ent)}; return component<T>{wrap_entity(ent)};
} }
template < typename T > template < typename T >
const_component<T> registry::wrap_component(const const_uentity& ent) const noexcept { const_component<T> registry::wrap_component(const const_uentity& ent) const noexcept {
return {wrap_entity(ent)}; return const_component<T>{wrap_entity(ent)};
} }
inline entity registry::create_entity() { inline entity registry::create_entity() {

View File

@@ -435,6 +435,15 @@ TEST_CASE("registry") {
} }
} }
SECTION("components") { SECTION("components") {
{
ecs::registry w;
ecs::entity e{w};
ecs::component<position_c> c1{e};
ecs::const_component<velocity_c> c2{e};
REQUIRE_FALSE(e.valid());
REQUIRE_FALSE(c1.valid());
REQUIRE_FALSE(c2.valid());
}
{ {
ecs::registry w; ecs::registry w;
ecs::entity e1 = w.create_entity(); ecs::entity e1 = w.create_entity();