Browse Source

meta: refine try_convert/allow_cast

skypjack 5 months ago
parent
commit
715e5318de
2 changed files with 20 additions and 18 deletions
  1. 16 12
      src/entt/meta/meta.hpp
  2. 4 6
      src/entt/meta/node.hpp

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

@@ -467,17 +467,7 @@ public:
      * @param type Meta type to which the cast is requested.
      * @return True if there exists a viable conversion, false otherwise.
      */
-    [[nodiscard]] bool allow_cast(const meta_type &type) {
-        if(auto other = std::as_const(*this).allow_cast(type); other) {
-            if(other.storage.owner()) {
-                std::swap(*this, other);
-            }
-
-            return true;
-        }
-
-        return false;
-    }
+    [[nodiscard]] bool allow_cast(const meta_type &type);
 
     /**
      * @brief Converts an object in such a way that a given cast becomes viable.
@@ -1338,7 +1328,7 @@ public:
      * @return True if the conversion is allowed, false otherwise.
      */
     [[nodiscard]] bool can_convert(const meta_type &other) const noexcept {
-        return (internal::try_convert(internal::meta_context::from(*ctx), fetch_node(), other.info().hash(), other.is_arithmetic() || other.is_enum(), nullptr, [](const void *, auto &&...args) { return ((static_cast<void>(args), 1) + ... + 0u); }) != 0u);
+        return ((info() == other.info()) || (internal::try_convert(internal::meta_context::from(*ctx), fetch_node(), other.info().hash(), other.is_arithmetic() || other.is_enum(), nullptr, [](const void *, auto &&...args) { return ((static_cast<void>(args), 1) + ... + 0u); }) != 0u));
     }
 
     /**
@@ -1598,6 +1588,20 @@ bool meta_any::set(const id_type id, Type &&value) {
     return meta_any{};
 }
 
+[[nodiscard]] inline bool meta_any::allow_cast(const meta_type &type) {
+    if(storage.info() == type.info()) {
+        return true;
+    } else if(auto other = std::as_const(*this).allow_cast(type); other) {
+        if(other.storage.owner()) {
+            std::swap(*this, other);
+        }
+
+        return true;
+    }
+
+    return false;
+}
+
 inline bool meta_any::assign(const meta_any &other) {
     if(storage.info() == other.storage.info()) {
         return storage.assign(other.storage);

+ 4 - 6
src/entt/meta/node.hpp

@@ -215,10 +215,6 @@ template<typename... Args>
 
 template<typename Func>
 [[nodiscard]] inline auto try_convert(const meta_context &context, const meta_type_node &from, const id_type to, const bool arithmetic_or_enum, const void *instance, Func func) {
-    if(from.info && from.info->hash() == to) {
-        return func(instance, from);
-    }
-
     if(from.details) {
         for(auto &&elem: from.details->conv) {
             if(elem.type == to) {
@@ -227,8 +223,10 @@ template<typename Func>
         }
 
         for(auto &&curr: from.details->base) {
-            if(auto other = try_convert(context, curr.resolve(context), to, arithmetic_or_enum, curr.cast(instance), func); other) {
-                return other;
+            if(const void *other = curr.cast(instance); curr.type == to) {
+                return func(other, curr.resolve(context));
+            } else if(auto elem = try_convert(context, curr.resolve(context), to, arithmetic_or_enum, curr.cast(instance), func); elem) {
+                return elem;
             }
         }
     }