Browse Source

meta: avoid pointless lookups in try_cast (#1201)

Paolo Monteverde 1 year ago
parent
commit
5ef4a2f22a
2 changed files with 5 additions and 5 deletions
  1. 3 3
      src/entt/meta/meta.hpp
  2. 2 2
      src/entt/meta/node.hpp

+ 3 - 3
src/entt/meta/meta.hpp

@@ -438,7 +438,7 @@ public:
      */
      */
     template<typename Type>
     template<typename Type>
     [[nodiscard]] const Type *try_cast() const {
     [[nodiscard]] const Type *try_cast() const {
-        const auto other = internal::resolve<std::remove_cv_t<Type>>(internal::meta_context::from(*ctx));
+        const auto &other = type_id<std::remove_cv_t<Type>>();
         return static_cast<const Type *>(internal::try_cast(internal::meta_context::from(*ctx), node, other, storage.data()));
         return static_cast<const Type *>(internal::try_cast(internal::meta_context::from(*ctx), node, other, storage.data()));
     }
     }
 
 
@@ -448,7 +448,7 @@ public:
         if constexpr(std::is_const_v<Type>) {
         if constexpr(std::is_const_v<Type>) {
             return std::as_const(*this).try_cast<std::remove_const_t<Type>>();
             return std::as_const(*this).try_cast<std::remove_const_t<Type>>();
         } else {
         } else {
-            const auto other = internal::resolve<std::remove_cv_t<Type>>(internal::meta_context::from(*ctx));
+            const auto &other = type_id<std::remove_cv_t<Type>>();
             return static_cast<Type *>(const_cast<void *>(internal::try_cast(internal::meta_context::from(*ctx), node, other, storage.data())));
             return static_cast<Type *>(const_cast<void *>(internal::try_cast(internal::meta_context::from(*ctx), node, other, storage.data())));
         }
         }
     }
     }
@@ -1304,7 +1304,7 @@ public:
      */
      */
     [[nodiscard]] bool can_cast(const meta_type &other) const noexcept {
     [[nodiscard]] bool can_cast(const meta_type &other) const noexcept {
         // casting this is UB in all cases but we aren't going to use the resulting pointer, so...
         // casting this is UB in all cases but we aren't going to use the resulting pointer, so...
-        return (internal::try_cast(internal::meta_context::from(*ctx), node, other.node, this) != nullptr);
+        return (other.node.info != nullptr) && (internal::try_cast(internal::meta_context::from(*ctx), node, *other.node.info, this) != nullptr);
     }
     }
 
 
     /**
     /**

+ 2 - 2
src/entt/meta/node.hpp

@@ -198,8 +198,8 @@ template<typename... Args>
     return value(context);
     return value(context);
 }
 }
 
 
-[[nodiscard]] inline const void *try_cast(const meta_context &context, const meta_type_node &from, const meta_type_node &to, const void *instance) noexcept {
-    if((from.info != nullptr) && (to.info != nullptr) && *from.info == *to.info) {
+[[nodiscard]] inline const void *try_cast(const meta_context &context, const meta_type_node &from, const type_info &to, const void *instance) noexcept {
+    if((from.info != nullptr) && *from.info == to) {
         return instance;
         return instance;
     }
     }