Procházet zdrojové kódy

meta: dereferencing a pointer-like object which converts to bool works in all cases (false implies empty meta_any)

Michele Caini před 4 roky
rodič
revize
c89bf30e05
2 změnil soubory, kde provedl 19 přidání a 1 odebrání
  1. 8 1
      src/entt/meta/meta.hpp
  2. 11 0
      test/entt/meta/meta_pointer.cpp

+ 8 - 1
src/entt/meta/meta.hpp

@@ -171,7 +171,14 @@ class meta_any {
                         *static_cast<meta_any *>(other) = any_cast<Type>(value);
                     } else if constexpr(!std::is_same_v<std::remove_const_t<typename std::pointer_traits<Type>::element_type>, void>) {
                         using in_place_type = decltype(adl_meta_pointer_like<Type>::dereference(any_cast<const Type &>(value)));
-                        static_cast<meta_any *>(other)->emplace<in_place_type>(adl_meta_pointer_like<Type>::dereference(any_cast<const Type &>(value)));
+
+                        if constexpr(std::is_constructible_v<bool, Type>) {
+                            if(const auto &pointer_like = any_cast<const Type &>(value); pointer_like) {
+                                static_cast<meta_any *>(other)->emplace<in_place_type>(adl_meta_pointer_like<Type>::dereference(pointer_like));
+                            }
+                        } else {
+                            static_cast<meta_any *>(other)->emplace<in_place_type>(adl_meta_pointer_like<Type>::dereference(any_cast<const Type &>(value)));
+                        }
                     }
                 }
                 break;

+ 11 - 0
test/entt/meta/meta_pointer.cpp

@@ -400,3 +400,14 @@ TEST(MetaPointerLike, DereferenceArray) {
     ASSERT_FALSE(*array);
     ASSERT_FALSE(*array_of_array);
 }
+
+TEST(MetaPointerLike, DereferenceVerifiableNullPointerLike) {
+    auto test = [](entt::meta_any any) {
+        ASSERT_TRUE(any);
+        ASSERT_FALSE(*any);
+    };
+
+    test(entt::meta_any{static_cast<int *>(nullptr)});
+    test(entt::meta_any{std::shared_ptr<int>{}});
+    test(entt::meta_any{std::unique_ptr<int>{}});
+}