From 0ac07e2e83beeebba7b9f21f70f6858d57a4dae8 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 10 Aug 2020 17:23:45 +0200 Subject: [PATCH] meta: reset is now part of meta_type --- docs/md/meta.md | 2 +- src/entt/meta/ctx.hpp | 16 ++------- src/entt/meta/factory.hpp | 41 ----------------------- src/entt/meta/meta.hpp | 51 +++++++++++++++++++++++++---- test/entt/meta/meta_type.cpp | 31 ++++++++---------- test/lib/meta/lib.cpp | 5 +-- test/lib/meta_plugin/plugin.cpp | 5 +-- test/lib/meta_plugin_std/plugin.cpp | 5 +-- 8 files changed, 71 insertions(+), 85 deletions(-) diff --git a/docs/md/meta.md b/docs/md/meta.md index 232c33c9b..01f0afe2e 100644 --- a/docs/md/meta.md +++ b/docs/md/meta.md @@ -878,7 +878,7 @@ objects from it and making its identifier no longer visible. The underlying node will remain available though, as if it were implicitly generated: ```cpp -entt::meta().reset(); +entt::resolve().reset(); ``` The type can be re-registered later with a completely different name and form. diff --git a/src/entt/meta/ctx.hpp b/src/entt/meta/ctx.hpp index 260f643a0..5eb1170f9 100644 --- a/src/entt/meta/ctx.hpp +++ b/src/entt/meta/ctx.hpp @@ -4,7 +4,6 @@ #include "../core/attribute.h" #include "../config/config.h" -#include "internal.hpp" namespace entt { @@ -19,6 +18,9 @@ namespace entt { namespace internal { +struct meta_type_node; + + struct ENTT_API meta_context { // we could use the lines below but VS2017 returns with an ICE if combined with ENTT_API despite the code being valid C++ // inline static meta_type_node *local = nullptr; @@ -33,18 +35,6 @@ struct ENTT_API meta_context { static meta_type_node **chain = &local(); return chain; } - - static void detach(const meta_type_node *node) ENTT_NOEXCEPT { - auto **it = global(); - - while(*it && *it != node) { - it = &(*it)->next; - } - - if(*it) { - *it = (*it)->next; - } - } }; diff --git a/src/entt/meta/factory.hpp b/src/entt/meta/factory.hpp index 625c18e7a..424e536ef 100644 --- a/src/entt/meta/factory.hpp +++ b/src/entt/meta/factory.hpp @@ -12,7 +12,6 @@ #include "../core/fwd.hpp" #include "../core/type_info.hpp" #include "../core/type_traits.hpp" -#include "../core/utility.hpp" #include "internal.hpp" #include "meta.hpp" #include "policy.hpp" @@ -691,46 +690,6 @@ public: return meta_factory>{&node.prop}; } - - /** - * @brief Resets a meta type and all its parts. - * - * This function resets a meta type and all its data members, member - * functions and properties, as well as its constructors, destructors and - * conversion functions if any.
- * Base classes aren't reset but the link between the two types is removed. - * - * @return An extended meta factory for the given type. - */ - auto reset() ENTT_NOEXCEPT { - auto * const node = internal::meta_info::resolve(); - - internal::meta_context::detach(node); - - const auto unregister_all = y_combinator{ - [](auto &&self, auto **curr, auto... member) { - while(*curr) { - auto *prev = *curr; - (self(&(prev->*member)), ...); - *curr = prev->next; - prev->next = nullptr; - } - } - }; - - unregister_all(&node->prop); - unregister_all(&node->base); - unregister_all(&node->conv); - unregister_all(&node->ctor, &internal::meta_ctor_node::prop); - unregister_all(&node->data, &internal::meta_data_node::prop); - unregister_all(&node->func, &internal::meta_func_node::prop); - - node->id = {}; - node->next = nullptr; - node->dtor = nullptr; - - return meta_factory{&node->prop}; - } }; diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 7b66a20ec..b9f167050 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -11,6 +11,7 @@ #include #include "../config/config.h" #include "../core/fwd.hpp" +#include "../core/utility.hpp" #include "ctx.hpp" #include "internal.hpp" #include "range.hpp" @@ -467,7 +468,7 @@ public: private: internal::meta_storage storage; - const internal::meta_type_node *node; + internal::meta_type_node *node; dereference_operator_type *deref; meta_sequence_container(* seq_factory)(void *); meta_associative_container(* assoc_factory)(void *); @@ -985,7 +986,7 @@ public: using size_type = typename node_type::size_type; /*! @copydoc meta_prop::meta_prop */ - meta_type(const node_type *curr = nullptr) ENTT_NOEXCEPT + meta_type(node_type *curr = nullptr) ENTT_NOEXCEPT : node{curr} {} @@ -1344,13 +1345,51 @@ public: return (!node && !other.node) || (node && other.node && node->type_id == other.node->type_id); } - /*! @brief Removes a meta object from the list of searchable types. */ - void detach() ENTT_NOEXCEPT { - internal::meta_context::detach(node); + /** + * @brief Resets a meta type and all its parts. + * + * This function resets a meta type and all its data members, member + * functions and properties, as well as its constructors, destructors and + * conversion functions if any.
+ * Base classes aren't reset but the link between the two types is removed. + * + * The meta type is also removed from the list of searchable types. + */ + void reset() ENTT_NOEXCEPT { + auto** it = internal::meta_context::global(); + + while (*it && *it != node) { + it = &(*it)->next; + } + + if(*it) { + *it = (*it)->next; + } + + const auto unregister_all = y_combinator{ + [](auto &&self, auto **curr, auto... member) { + while(*curr) { + auto *prev = *curr; + (self(&(prev->*member)), ...); + *curr = prev->next; + prev->next = nullptr; + } + } + }; + + unregister_all(&node->prop); + unregister_all(&node->base); + unregister_all(&node->conv); + unregister_all(&node->ctor, &internal::meta_ctor_node::prop); + unregister_all(&node->data, &internal::meta_data_node::prop); + unregister_all(&node->func, &internal::meta_func_node::prop); + + node->id = {}; + node->dtor = nullptr; } private: - const node_type *node; + node_type *node; }; diff --git a/test/entt/meta/meta_type.cpp b/test/entt/meta/meta_type.cpp index 120cd80d1..1c359d652 100644 --- a/test/entt/meta/meta_type.cpp +++ b/test/entt/meta/meta_type.cpp @@ -308,20 +308,15 @@ TEST_F(MetaType, ConstructCastAndConvert) { ASSERT_EQ(any.cast().value, 42); } -TEST_F(MetaType, Detach) { +TEST_F(MetaType, Reset) { ASSERT_TRUE(entt::resolve_id("clazz"_hs)); - for(auto curr: entt::resolve()) { - if(curr.id() == "clazz"_hs) { - curr.detach(); - break; - } - } + entt::resolve_id("clazz"_hs).reset(); ASSERT_FALSE(entt::resolve_id("clazz"_hs)); - ASSERT_EQ(entt::resolve().id(), "clazz"_hs); - ASSERT_EQ(entt::resolve().prop(property_t::value).value().cast(), 42); - ASSERT_TRUE(entt::resolve().data("value"_hs)); + ASSERT_NE(entt::resolve().id(), "clazz"_hs); + ASSERT_FALSE(entt::resolve().prop(property_t::value)); + ASSERT_FALSE(entt::resolve().data("value"_hs)); entt::meta().type("clazz"_hs); @@ -413,14 +408,14 @@ TEST_F(MetaType, PropertiesAndCornerCases) { TEST_F(MetaType, ResetAndReRegistrationAfterReset) { ASSERT_NE(*entt::internal::meta_context::global(), nullptr); - entt::meta().reset(); - entt::meta().reset(); - entt::meta().reset(); - entt::meta().reset(); - entt::meta().reset(); - entt::meta().reset(); - entt::meta().reset(); - entt::meta().reset(); + entt::resolve().reset(); + entt::resolve().reset(); + entt::resolve().reset(); + entt::resolve().reset(); + entt::resolve().reset(); + entt::resolve().reset(); + entt::resolve().reset(); + entt::resolve().reset(); ASSERT_FALSE(entt::resolve_id("double"_hs)); ASSERT_FALSE(entt::resolve_id("base"_hs)); diff --git a/test/lib/meta/lib.cpp b/test/lib/meta/lib.cpp index 7555f480a..b06a27bbe 100644 --- a/test/lib/meta/lib.cpp +++ b/test/lib/meta/lib.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "types.h" position create_position(int x, int y) { @@ -23,8 +24,8 @@ ENTT_API void set_up() { } ENTT_API void tear_down() { - entt::meta().reset(); - entt::meta().reset(); + entt::resolve().reset(); + entt::resolve().reset(); } ENTT_API entt::meta_any wrap_int(int value) { diff --git a/test/lib/meta_plugin/plugin.cpp b/test/lib/meta_plugin/plugin.cpp index 667c7ef9d..c311ed4df 100644 --- a/test/lib/meta_plugin/plugin.cpp +++ b/test/lib/meta_plugin/plugin.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "types.h" position create_position(int x, int y) { @@ -23,8 +24,8 @@ void set_up() { } void tear_down() { - entt::meta().reset(); - entt::meta().reset(); + entt::resolve().reset(); + entt::resolve().reset(); } CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) { diff --git a/test/lib/meta_plugin_std/plugin.cpp b/test/lib/meta_plugin_std/plugin.cpp index 667c7ef9d..c311ed4df 100644 --- a/test/lib/meta_plugin_std/plugin.cpp +++ b/test/lib/meta_plugin_std/plugin.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "types.h" position create_position(int x, int y) { @@ -23,8 +24,8 @@ void set_up() { } void tear_down() { - entt::meta().reset(); - entt::meta().reset(); + entt::resolve().reset(); + entt::resolve().reset(); } CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {