Browse Source

meta_handle no longer exposes try_cast

Michele Caini 6 years ago
parent
commit
4d1ad8f749
3 changed files with 9 additions and 46 deletions
  1. 6 6
      src/entt/meta/factory.hpp
  2. 0 16
      src/entt/meta/meta.hpp
  3. 3 24
      test/entt/meta/meta.cpp

+ 6 - 6
src/entt/meta/factory.hpp

@@ -90,8 +90,8 @@ bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any index
             using helper_type = meta_function_helper_t<decltype(Data)>;
             using data_type = std::tuple_element_t<!std::is_member_function_pointer_v<decltype(Data)>, typename helper_type::args_type>;
             static_assert(std::is_invocable_v<decltype(Data), Type &, data_type>);
+            auto *clazz = meta_any{handle}.try_cast<Type>();
             auto *direct = value.try_cast<data_type>();
-            auto *clazz = handle.try_cast<Type>();
 
             if(clazz && (direct || value.convert<data_type>())) {
                 std::invoke(Data, *clazz, direct ? *direct : value.cast<data_type>());
@@ -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<decltype(Data)>) {
             using data_type = std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>;
             static_assert(std::is_invocable_v<decltype(Data), Type *>);
-            auto *clazz = handle.try_cast<Type>();
+            auto *clazz = meta_any{handle}.try_cast<Type>();
 
             if constexpr(std::is_array_v<data_type>) {
                 using underlying_type = std::remove_extent_t<data_type>;
@@ -162,12 +162,12 @@ meta_any getter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any i
 
     if constexpr(std::is_function_v<std::remove_pointer_t<decltype(Data)>> || std::is_member_function_pointer_v<decltype(Data)>) {
         static_assert(std::is_invocable_v<decltype(Data), Type &>);
-        auto *clazz = handle.try_cast<Type>();
+        auto *clazz = meta_any{handle}.try_cast<Type>();
         return clazz ? dispatch(std::invoke(Data, *clazz)) : meta_any{};
     } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
         using data_type = std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>;
         static_assert(std::is_invocable_v<decltype(Data), Type *>);
-        auto *clazz = handle.try_cast<Type>();
+        auto *clazz = meta_any{handle}.try_cast<Type>();
 
         if constexpr(std::is_array_v<data_type>) {
             auto *idx = index.try_cast<std::size_t>();
@@ -217,7 +217,7 @@ meta_any invoke([[maybe_unused]] meta_handle handle, meta_any *args, std::index_
     if constexpr(std::is_function_v<std::remove_pointer_t<decltype(Candidate)>>) {
         return (std::get<Indexes>(direct) && ...) ? dispatch(std::get<Indexes>(direct)...) : meta_any{};
     } else {
-        auto *clazz = handle.try_cast<Type>();
+        auto *clazz = meta_any{handle}.try_cast<Type>();
         return (clazz && (std::get<Indexes>(direct) && ...)) ? dispatch(clazz, std::get<Indexes>(direct)...) : meta_any{};
     }
 }
@@ -564,7 +564,7 @@ public:
                 const auto valid = (handle.type() == internal::meta_info<Type>::resolve()->meta());
 
                 if(valid) {
-                    std::invoke(Func, *handle.try_cast<Type>());
+                    std::invoke(Func, *meta_any{handle}.try_cast<Type>());
                 }
 
                 return valid;

+ 0 - 16
src/entt/meta/meta.hpp

@@ -722,22 +722,6 @@ public:
         return const_cast<void *>(std::as_const(*this).data());
     }
 
-    /**
-     * @brief Tries to cast an instance to a given type.
-     * @tparam Type Type to which to cast the instance.
-     * @return A (possibly null) pointer to the underlying object.
-     */
-    template<typename Type>
-    const Type * try_cast() const ENTT_NOEXCEPT {
-        return internal::try_cast<Type>(node, instance);
-    }
-
-    /*! @copydoc try_cast */
-    template<typename Type>
-    Type * try_cast() ENTT_NOEXCEPT {
-        return const_cast<Type *>(std::as_const(*this).try_cast<Type>());
-    }
-
     /**
      * @brief Returns false if a handle is empty, true otherwise.
      * @return False if the handle is empty, true otherwise.

+ 3 - 24
test/entt/meta/meta.cpp

@@ -865,11 +865,8 @@ TEST_F(Meta, MetaHandleFromObject) {
 
     ASSERT_TRUE(handle);
     ASSERT_EQ(handle.type(), entt::resolve<empty_type>());
-    ASSERT_EQ(handle.try_cast<std::size_t>(), nullptr);
-    ASSERT_EQ(handle.try_cast<empty_type>(), &empty);
-    ASSERT_EQ(std::as_const(handle).try_cast<empty_type>(), &empty);
-    ASSERT_EQ(handle.data(), &empty);
     ASSERT_EQ(std::as_const(handle).data(), &empty);
+    ASSERT_EQ(handle.data(), &empty);
 }
 
 TEST_F(Meta, MetaHandleFromMetaAny) {
@@ -878,11 +875,8 @@ TEST_F(Meta, MetaHandleFromMetaAny) {
 
     ASSERT_TRUE(handle);
     ASSERT_EQ(handle.type(), entt::resolve<int>());
-    ASSERT_EQ(handle.try_cast<std::size_t>(), nullptr);
-    ASSERT_EQ(handle.try_cast<int>(), any.data());
-    ASSERT_EQ(std::as_const(handle).try_cast<int>(), any.data());
-    ASSERT_EQ(handle.data(), any.data());
     ASSERT_EQ(std::as_const(handle).data(), any.data());
+    ASSERT_EQ(handle.data(), any.data());
 }
 
 TEST_F(Meta, MetaHandleEmpty) {
@@ -890,23 +884,8 @@ TEST_F(Meta, MetaHandleEmpty) {
 
     ASSERT_FALSE(handle);
     ASSERT_FALSE(handle.type());
-    ASSERT_EQ(handle.try_cast<std::size_t>(), nullptr);
-    ASSERT_EQ(handle.try_cast<empty_type>(), nullptr);
-    ASSERT_EQ(handle.data(), nullptr);
     ASSERT_EQ(std::as_const(handle).data(), nullptr);
-}
-
-TEST_F(Meta, MetaHandleTryCast) {
-    derived_type derived{};
-    entt::meta_handle handle{derived};
-
-    ASSERT_TRUE(handle);
-    ASSERT_EQ(handle.type(), entt::resolve<derived_type>());
-    ASSERT_EQ(handle.try_cast<void>(), nullptr);
-    ASSERT_NE(handle.try_cast<base_type>(), nullptr);
-    ASSERT_EQ(handle.try_cast<derived_type>(), handle.data());
-    ASSERT_EQ(std::as_const(handle).try_cast<base_type>(), handle.try_cast<base_type>());
-    ASSERT_EQ(std::as_const(handle).try_cast<derived_type>(), handle.data());
+    ASSERT_EQ(handle.data(), nullptr);
 }
 
 TEST_F(Meta, MetaProp) {