Browse Source

Add entity weak_ptr expiration check

master
Riyyi 4 years ago
parent
commit
eb754877ff
  1. 8
      inferno/src/inferno/scene/entity.cpp
  2. 6
      inferno/src/inferno/scene/entity.h

8
inferno/src/inferno/scene/entity.cpp

@ -5,12 +5,14 @@ namespace Inferno {
Entity::Entity(const std::shared_ptr<entt::registry>& registry) Entity::Entity(const std::shared_ptr<entt::registry>& registry)
: m_registry(registry) : m_registry(registry)
{ {
expired();
m_entity = m_registry.lock()->create(); m_entity = m_registry.lock()->create();
} }
Entity::Entity(const std::shared_ptr<entt::registry>& registry, entt::entity handle) Entity::Entity(const std::shared_ptr<entt::registry>& registry, entt::entity handle)
: m_registry(registry) : m_registry(registry)
{ {
expired();
ASSERT(m_registry.lock()->valid(handle), "Can't construct entity from invalid handle"); ASSERT(m_registry.lock()->valid(handle), "Can't construct entity from invalid handle");
m_entity = handle; m_entity = handle;
} }
@ -18,10 +20,16 @@ namespace Inferno {
Entity::Entity(const std::shared_ptr<entt::registry>& registry, uint32_t handle) Entity::Entity(const std::shared_ptr<entt::registry>& registry, uint32_t handle)
: m_registry(registry) : m_registry(registry)
{ {
expired();
ASSERT(m_registry.lock()->valid(entt::entity(handle)), "Can't construct entity from invalid handle"); ASSERT(m_registry.lock()->valid(entt::entity(handle)), "Can't construct entity from invalid handle");
m_entity = entt::entity(handle); m_entity = entt::entity(handle);
} }
void Entity::expired() const
{
ASSERT(!m_registry.expired(), "Entity registry expired");
}
void Entity::valid() const void Entity::valid() const
{ {
ASSERT(m_registry.lock()->valid(m_entity)); ASSERT(m_registry.lock()->valid(m_entity));

6
inferno/src/inferno/scene/entity.h

@ -17,11 +17,13 @@ namespace Inferno {
Entity(const std::shared_ptr<entt::registry>& registry, entt::entity handle); Entity(const std::shared_ptr<entt::registry>& registry, entt::entity handle);
Entity(const std::shared_ptr<entt::registry>& registry, uint32_t handle); Entity(const std::shared_ptr<entt::registry>& registry, uint32_t handle);
void expired() const;
void valid() const; void valid() const;
template<typename... T> template<typename... T>
[[nodiscard]] bool has() const [[nodiscard]] bool has() const
{ {
expired();
valid(); valid();
return m_registry.lock()->has<T...>(m_entity); return m_registry.lock()->has<T...>(m_entity);
} }
@ -29,6 +31,7 @@ namespace Inferno {
template<typename... T> template<typename... T>
[[nodiscard]] bool any() const [[nodiscard]] bool any() const
{ {
expired();
valid(); valid();
return m_registry.lock()->any<T...>(m_entity); return m_registry.lock()->any<T...>(m_entity);
} }
@ -36,6 +39,7 @@ namespace Inferno {
template<typename T, typename... P> template<typename T, typename... P>
T& add(P&&... parameters) const T& add(P&&... parameters) const
{ {
expired();
valid(); valid();
return m_registry.lock()->emplace_or_replace<T>(m_entity, std::forward<P>(parameters)...); return m_registry.lock()->emplace_or_replace<T>(m_entity, std::forward<P>(parameters)...);
}; };
@ -43,6 +47,7 @@ namespace Inferno {
template<typename T> template<typename T>
size_t remove() const size_t remove() const
{ {
expired();
valid(); valid();
return m_registry.lock()->remove_if_exists<T>(m_entity); return m_registry.lock()->remove_if_exists<T>(m_entity);
} }
@ -50,6 +55,7 @@ namespace Inferno {
template<typename T, typename... P> template<typename T, typename... P>
T& get(P&&... parameters) const T& get(P&&... parameters) const
{ {
expired();
valid(); valid();
return m_registry.lock()->get_or_emplace<T>(m_entity, std::forward<P>(parameters)...); return m_registry.lock()->get_or_emplace<T>(m_entity, std::forward<P>(parameters)...);
} }

Loading…
Cancel
Save