Quellcode durchsuchen

meta: simplified meta_any::try_cast

Michele Caini vor 4 Jahren
Ursprung
Commit
e49aa0d424
1 geänderte Dateien mit 11 neuen und 7 gelöschten Zeilen
  1. 11 7
      src/entt/meta/meta.hpp

+ 11 - 7
src/entt/meta/meta.hpp

@@ -415,15 +415,19 @@ public:
     /*! @copydoc try_cast */
     template<typename Type>
     [[nodiscard]] Type * try_cast() {
-        if(node) {
-            if(const auto info = type_id<Type>(); node->info == info) {
-                return any_cast<Type>(&storage);
-            } else if(const auto *base = visit<&internal::meta_type_node::base>(info, node); base) {
-                return static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(base->cast(static_cast<constness_as_t<any, Type> &>(storage).data())));
+        if constexpr(std::is_const_v<Type>) {
+            return std::as_const(*this).try_cast<Type>();
+        } else {
+            if(node) {
+                if(const auto info = type_id<Type>(); node->info == info) {
+                    return any_cast<Type>(&storage);
+                } else if(const auto *base = visit<&internal::meta_type_node::base>(info, node); base) {
+                    return const_cast<Type *>(static_cast<const Type *>(base->cast(storage.data())));
+                }
             }
-        }
 
-        return nullptr;
+            return nullptr;
+        }
     }
 
     /**