From c89bf30e05c454fc7856f8c76a34c7424a7d1ec8 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 15 Mar 2022 08:29:33 +0100 Subject: [PATCH] meta: dereferencing a pointer-like object which converts to bool works in all cases (false implies empty meta_any) --- src/entt/meta/meta.hpp | 9 ++++++++- test/entt/meta/meta_pointer.cpp | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index d35d417aa..d302fef4d 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -171,7 +171,14 @@ class meta_any { *static_cast(other) = any_cast(value); } else if constexpr(!std::is_same_v::element_type>, void>) { using in_place_type = decltype(adl_meta_pointer_like::dereference(any_cast(value))); - static_cast(other)->emplace(adl_meta_pointer_like::dereference(any_cast(value))); + + if constexpr(std::is_constructible_v) { + if(const auto &pointer_like = any_cast(value); pointer_like) { + static_cast(other)->emplace(adl_meta_pointer_like::dereference(pointer_like)); + } + } else { + static_cast(other)->emplace(adl_meta_pointer_like::dereference(any_cast(value))); + } } } break; diff --git a/test/entt/meta/meta_pointer.cpp b/test/entt/meta/meta_pointer.cpp index fc9765b66..a436a934f 100644 --- a/test/entt/meta/meta_pointer.cpp +++ b/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(nullptr)}); + test(entt::meta_any{std::shared_ptr{}}); + test(entt::meta_any{std::unique_ptr{}}); +}