diff --git a/TODO b/TODO index d02b0bda8..2a70d092e 100644 --- a/TODO +++ b/TODO @@ -18,6 +18,7 @@ - ... WIP: +* HP: registry::ctx can return entt::any objects directly * HP: merge view and view pack * HP: invalid view auto-refresh * HP: paginate pools diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 9f0a1d3ad..5b859d06c 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -13,6 +13,7 @@ #include #include "../config/config.h" #include "../core/algorithm.hpp" +#include "../core/any.hpp" #include "../core/fwd.hpp" #include "../core/type_info.hpp" #include "../core/type_traits.hpp" @@ -103,11 +104,6 @@ class basic_registry { bool (* exclude)(const id_type) ENTT_NOEXCEPT; }; - struct variable_data { - type_info info; - std::unique_ptr value; - }; - template [[nodiscard]] storage_type * assure() { const auto index = type_seq::value(); @@ -1485,8 +1481,8 @@ public: template Type & set(Args &&... args) { unset(); - vars.push_back(variable_data{type_id(), { new Type{std::forward(args)...}, [](void *instance) { delete static_cast(instance); } }}); - return *static_cast(vars.back().value.get()); + vars.push_back(entt::any{std::in_place_type, std::forward(args)...}); + return any_cast(vars.back()); } /** @@ -1495,9 +1491,7 @@ public: */ template void unset() { - vars.erase(std::remove_if(vars.begin(), vars.end(), [](auto &&var) { - return var.info.hash() == type_hash::value(); - }), vars.end()); + vars.erase(std::remove_if(vars.begin(), vars.end(), [type = type_id()](auto &&var) { return var.type() == type; }), vars.end()); } /** @@ -1525,14 +1519,15 @@ public: */ template [[nodiscard]] const Type * try_ctx() const { - auto it = std::find_if(vars.cbegin(), vars.cend(), [](auto &&var) { return var.info.hash() == type_hash::value(); }); - return it == vars.cend() ? nullptr : static_cast(it->value.get()); + 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); } /*! @copydoc try_ctx */ template [[nodiscard]] Type * try_ctx() { - return const_cast(std::as_const(*this).template try_ctx()); + 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); } /** @@ -1547,15 +1542,15 @@ public: */ template [[nodiscard]] const Type & ctx() const { - const auto *instance = try_ctx(); - ENTT_ASSERT(instance); - return *instance; + auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id()](auto &&var) { return var.type() == type; }); + return (ENTT_ASSERT(it != vars.cend()), any_cast(*it)); } /*! @copydoc ctx */ template [[nodiscard]] Type & ctx() { - return const_cast(std::as_const(*this).template ctx()); + auto it = std::find_if(vars.begin(), vars.end(), [type = type_id()](auto &&var) { return var.type() == type; }); + return (ENTT_ASSERT(it != vars.end()), any_cast(*it)); } /** @@ -1582,15 +1577,15 @@ public: template void ctx(Func func) const { for(auto pos = vars.size(); pos; --pos) { - func(vars[pos-1].info); + func(vars[pos-1].type()); } } private: + std::vector vars{}; std::vector pools{}; std::vector groups{}; std::vector entities{}; - std::vector vars{}; entity_type available{null}; };