diff --git a/src/entt/config/config.h b/src/entt/config/config.h index c2276c55d..ebdac506e 100644 --- a/src/entt/config/config.h +++ b/src/entt/config/config.h @@ -69,6 +69,11 @@ static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) = #endif +#ifndef ENTT_REGISTRY_CONTEXT_GUARD + #define ENTT_REGISTRY_CONTEXT_GUARD() +#endif + + #ifndef ENTT_STANDARD_CPP # if defined __clang__ || defined __GNUC__ # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__ diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 29deabaaf..e1eb85c70 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -1596,6 +1596,7 @@ class basic_registry { template Type & set(Args &&... args) { unset(); + ENTT_REGISTRY_CONTEXT_GUARD() vars.emplace_back(std::in_place_type, std::forward(args)...); return any_cast(vars.back()); } @@ -1606,6 +1607,7 @@ class basic_registry { */ template void unset() { + ENTT_REGISTRY_CONTEXT_GUARD() vars.erase(std::remove_if(vars.begin(), vars.end(), [type = type_id()](auto &&var) { return var.type() == type; }), vars.end()); } @@ -1634,6 +1636,7 @@ class basic_registry { */ template [[nodiscard]] std::add_const_t * try_ctx() const { + ENTT_REGISTRY_CONTEXT_GUARD() auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id()](auto &&var) { return var.type() == type; }); return it == vars.cend() ? nullptr : any_cast>(&*it); } @@ -1641,6 +1644,7 @@ class basic_registry { /*! @copydoc try_ctx */ template [[nodiscard]] Type * try_ctx() { + ENTT_REGISTRY_CONTEXT_GUARD() auto it = std::find_if(vars.begin(), vars.end(), [type = type_id()](auto &&var) { return var.type() == type; }); return it == vars.end() ? nullptr : any_cast(&*it); } @@ -1657,6 +1661,7 @@ class basic_registry { */ template [[nodiscard]] std::add_const_t & ctx() const { + ENTT_REGISTRY_CONTEXT_GUARD() auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id()](auto &&var) { return var.type() == type; }); ENTT_ASSERT(it != vars.cend(), "Invalid instance"); return any_cast &>(*it); @@ -1665,6 +1670,7 @@ class basic_registry { /*! @copydoc ctx */ template [[nodiscard]] Type & ctx() { + ENTT_REGISTRY_CONTEXT_GUARD() auto it = std::find_if(vars.begin(), vars.end(), [type = type_id()](auto &&var) { return var.type() == type; }); ENTT_ASSERT(it != vars.end(), "Invalid instance"); return any_cast(*it); @@ -1693,6 +1699,7 @@ class basic_registry { */ template void ctx(Func func) const { + ENTT_REGISTRY_CONTEXT_GUARD() for(auto pos = vars.size(); pos; --pos) { func(vars[pos-1].type()); }