From 80b444e531817f24f5503be4715de6797f289bcf Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sun, 8 Sep 2019 23:43:43 +0200 Subject: [PATCH] added meta_any constructor for the unmanaged object taken from a meta_handle --- src/entt/meta/factory.hpp | 12 +++++----- src/entt/meta/meta.hpp | 34 +++++++++++++++++++++------- test/entt/meta/meta.cpp | 47 +++++++++++++++++++++++++-------------- 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/entt/meta/factory.hpp b/src/entt/meta/factory.hpp index 1edc194d7..d3d0fa281 100644 --- a/src/entt/meta/factory.hpp +++ b/src/entt/meta/factory.hpp @@ -91,7 +91,7 @@ bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any index using data_type = std::tuple_element_t, typename helper_type::args_type>; static_assert(std::is_invocable_v); auto *direct = value.try_cast(); - auto *clazz = handle.data(); + auto *clazz = handle.try_cast(); if(clazz && (direct || value.convert())) { std::invoke(Data, *clazz, direct ? *direct : value.cast()); @@ -100,7 +100,7 @@ bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any index } else if constexpr(std::is_member_object_pointer_v) { using data_type = std::remove_cv_t().*Data)>>; static_assert(std::is_invocable_v); - auto *clazz = handle.data(); + auto *clazz = handle.try_cast(); if constexpr(std::is_array_v) { using underlying_type = std::remove_extent_t; @@ -162,12 +162,12 @@ meta_any getter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any i if constexpr(std::is_function_v> || std::is_member_function_pointer_v) { static_assert(std::is_invocable_v); - auto *clazz = handle.data(); + auto *clazz = handle.try_cast(); return clazz ? dispatch(std::invoke(Data, *clazz)) : meta_any{}; } else if constexpr(std::is_member_object_pointer_v) { using data_type = std::remove_cv_t().*Data)>>; static_assert(std::is_invocable_v); - auto *clazz = handle.data(); + auto *clazz = handle.try_cast(); if constexpr(std::is_array_v) { auto *idx = index.try_cast(); @@ -217,7 +217,7 @@ meta_any invoke([[maybe_unused]] meta_handle handle, meta_any *args, std::index_ if constexpr(std::is_function_v>) { return (std::get(direct) && ...) ? dispatch(std::get(direct)...) : meta_any{}; } else { - auto *clazz = handle.data(); + auto *clazz = handle.try_cast(); return (clazz && (std::get(direct) && ...)) ? dispatch(clazz, std::get(direct)...) : meta_any{}; } } @@ -564,7 +564,7 @@ public: const auto valid = (handle.type() == internal::meta_info::resolve()->meta()); if(valid) { - std::invoke(Func, *handle.data()); + std::invoke(Func, *handle.try_cast()); } return valid; diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 5e2b31f23..8fe0f7c9b 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -15,7 +15,7 @@ namespace entt { class meta_any; -struct meta_handle; +class meta_handle; class meta_prop; class meta_base; class meta_conv; @@ -298,7 +298,7 @@ inline auto ctor(std::index_sequence, const meta_type_node *node) EN */ class meta_any { /*! @brief A meta handle is allowed to _inherit_ from a meta any. */ - friend struct meta_handle; + friend class meta_handle; using storage_type = std::aligned_storage_t; using copy_fn_type = void *(storage_type &, const void *); @@ -406,6 +406,12 @@ public: instance = &type.get(); } + /** + * @brief Constructs a meta any from a meta handle object. + * @param handle A reference to an object to use to initialize the meta any. + */ + inline meta_any(meta_handle handle) ENTT_NOEXCEPT; + /** * @brief Constructs a meta any from a given value. * @tparam Type Type of object to use to initialize the container. @@ -649,7 +655,7 @@ public: private: storage_type storage; void *instance; - internal::meta_type_node *node; + const internal::meta_type_node *node; destroy_fn_type *destroy_fn; copy_fn_type *copy_fn; steal_fn_type *steal_fn; @@ -666,7 +672,11 @@ private: * responsible for ensuring that the target object remains alive for the entire * interval of use of the handle. */ -struct meta_handle { +class meta_handle { + /*! @brief A meta any is allowed to _inherit_ from a meta handle. */ + friend class meta_any; + +public: /*! @brief Default constructor. */ meta_handle() ENTT_NOEXCEPT : node{nullptr}, @@ -718,14 +728,14 @@ struct meta_handle { * @return A (possibly null) pointer to the underlying object. */ template - const Type * data() const ENTT_NOEXCEPT { + const Type * try_cast() const ENTT_NOEXCEPT { return internal::try_cast(node, instance); } - /*! @copydoc data */ + /*! @copydoc try_cast */ template - Type * data() ENTT_NOEXCEPT { - return const_cast(std::as_const(*this).data()); + Type * try_cast() ENTT_NOEXCEPT { + return const_cast(std::as_const(*this).try_cast()); } /** @@ -1967,6 +1977,14 @@ inline bool operator!=(const meta_type &lhs, const meta_type &rhs) ENTT_NOEXCEPT } +inline meta_any::meta_any(meta_handle handle) ENTT_NOEXCEPT + : meta_any{} +{ + node = handle.node; + instance = handle.instance; +} + + inline meta_type meta_any::type() const ENTT_NOEXCEPT { return node ? node->meta() : meta_type{}; } diff --git a/test/entt/meta/meta.cpp b/test/entt/meta/meta.cpp index c97e9ca96..cab4dc3fd 100644 --- a/test/entt/meta/meta.cpp +++ b/test/entt/meta/meta.cpp @@ -247,6 +247,22 @@ TEST_F(Meta, Resolve) { ASSERT_TRUE(found); } +TEST_F(Meta, MetaAnyFromMetaHandle) { + int value = 42; + entt::meta_handle handle{value}; + entt::meta_any any{handle}; + any.cast() = 3; + + ASSERT_TRUE(any); + ASSERT_EQ(any.type(), entt::resolve()); + ASSERT_EQ(any.try_cast(), nullptr); + ASSERT_EQ(any.try_cast(), handle.data()); + ASSERT_EQ(std::as_const(any).try_cast(), handle.data()); + ASSERT_EQ(any.data(), handle.data()); + ASSERT_EQ(std::as_const(any).data(), handle.data()); + ASSERT_EQ(value, 3); +} + TEST_F(Meta, MetaAnySBO) { entt::meta_any any{'c'}; @@ -849,9 +865,9 @@ TEST_F(Meta, MetaHandleFromObject) { ASSERT_TRUE(handle); ASSERT_EQ(handle.type(), entt::resolve()); - ASSERT_EQ(handle.data(), nullptr); - ASSERT_EQ(handle.data(), &empty); - ASSERT_EQ(std::as_const(handle).data(), &empty); + ASSERT_EQ(handle.try_cast(), nullptr); + ASSERT_EQ(handle.try_cast(), &empty); + ASSERT_EQ(std::as_const(handle).try_cast(), &empty); ASSERT_EQ(handle.data(), &empty); ASSERT_EQ(std::as_const(handle).data(), &empty); } @@ -862,9 +878,9 @@ TEST_F(Meta, MetaHandleFromMetaAny) { ASSERT_TRUE(handle); ASSERT_EQ(handle.type(), entt::resolve()); - ASSERT_EQ(handle.data(), nullptr); - ASSERT_EQ(handle.data(), any.data()); - ASSERT_EQ(std::as_const(handle).data(), any.data()); + ASSERT_EQ(handle.try_cast(), nullptr); + ASSERT_EQ(handle.try_cast(), any.data()); + ASSERT_EQ(std::as_const(handle).try_cast(), any.data()); ASSERT_EQ(handle.data(), any.data()); ASSERT_EQ(std::as_const(handle).data(), any.data()); } @@ -874,26 +890,23 @@ TEST_F(Meta, MetaHandleEmpty) { ASSERT_FALSE(handle); ASSERT_FALSE(handle.type()); - ASSERT_EQ(handle.data(), nullptr); - ASSERT_EQ(handle.data(), nullptr); + ASSERT_EQ(handle.try_cast(), nullptr); + ASSERT_EQ(handle.try_cast(), nullptr); ASSERT_EQ(handle.data(), nullptr); ASSERT_EQ(std::as_const(handle).data(), nullptr); } -TEST_F(Meta, MetaHandleData) { +TEST_F(Meta, MetaHandleTryCast) { derived_type derived{}; - base_type *base = &derived; entt::meta_handle handle{derived}; ASSERT_TRUE(handle); ASSERT_EQ(handle.type(), entt::resolve()); - ASSERT_EQ(handle.data(), nullptr); - ASSERT_EQ(handle.data(), base); - ASSERT_EQ(handle.data(), &derived); - ASSERT_EQ(std::as_const(handle).data(), base); - ASSERT_EQ(std::as_const(handle).data(), &derived); - ASSERT_EQ(handle.data(), &derived); - ASSERT_EQ(std::as_const(handle).data(), &derived); + ASSERT_EQ(handle.try_cast(), nullptr); + ASSERT_NE(handle.try_cast(), nullptr); + ASSERT_EQ(handle.try_cast(), handle.data()); + ASSERT_EQ(std::as_const(handle).try_cast(), handle.try_cast()); + ASSERT_EQ(std::as_const(handle).try_cast(), handle.data()); } TEST_F(Meta, MetaProp) {