Add entity weak_ptr expiration check

This commit is contained in:
Riyyi
2021-01-13 14:59:17 +01:00
parent 9b2df60b68
commit eb754877ff
2 changed files with 14 additions and 0 deletions
+8
View File
@@ -5,12 +5,14 @@ namespace Inferno {
Entity::Entity(const std::shared_ptr<entt::registry>& registry)
: m_registry(registry)
{
expired();
m_entity = m_registry.lock()->create();
}
Entity::Entity(const std::shared_ptr<entt::registry>& registry, entt::entity handle)
: m_registry(registry)
{
expired();
ASSERT(m_registry.lock()->valid(handle), "Can't construct entity from invalid handle");
m_entity = handle;
}
@@ -18,10 +20,16 @@ namespace Inferno {
Entity::Entity(const std::shared_ptr<entt::registry>& registry, uint32_t handle)
: m_registry(registry)
{
expired();
ASSERT(m_registry.lock()->valid(entt::entity(handle)), "Can't construct entity from invalid handle");
m_entity = entt::entity(handle);
}
void Entity::expired() const
{
ASSERT(!m_registry.expired(), "Entity registry expired");
}
void Entity::valid() const
{
ASSERT(m_registry.lock()->valid(m_entity));
+6
View File
@@ -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, uint32_t handle);
void expired() const;
void valid() const;
template<typename... T>
[[nodiscard]] bool has() const
{
expired();
valid();
return m_registry.lock()->has<T...>(m_entity);
}
@@ -29,6 +31,7 @@ namespace Inferno {
template<typename... T>
[[nodiscard]] bool any() const
{
expired();
valid();
return m_registry.lock()->any<T...>(m_entity);
}
@@ -36,6 +39,7 @@ namespace Inferno {
template<typename T, typename... P>
T& add(P&&... parameters) const
{
expired();
valid();
return m_registry.lock()->emplace_or_replace<T>(m_entity, std::forward<P>(parameters)...);
};
@@ -43,6 +47,7 @@ namespace Inferno {
template<typename T>
size_t remove() const
{
expired();
valid();
return m_registry.lock()->remove_if_exists<T>(m_entity);
}
@@ -50,6 +55,7 @@ namespace Inferno {
template<typename T, typename... P>
T& get(P&&... parameters) const
{
expired();
valid();
return m_registry.lock()->get_or_emplace<T>(m_entity, std::forward<P>(parameters)...);
}