From 8e6bbc4a6c7182a0d8c1c9b46ebe5858585e12d9 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 3 Feb 2025 17:48:09 +0100 Subject: [PATCH] meta: as_ref for void types still returns a valid meta_any --- TODO | 1 - src/entt/meta/meta.hpp | 2 +- test/entt/meta/meta_any.cpp | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index bbbda9142..790a5e800 100644 --- a/TODO +++ b/TODO @@ -40,4 +40,3 @@ TODO: * allow passing arguments to meta setter/getter (we can fallback on meta invoke for everything probably) * delegate/sigh: forward connect/disconnect from & to * * a few more tests on the improved shrink_to_fit for sparse arrays -* meta_any::as_ref for void type invalidates the object (operator bool: true -> false) diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index f7948e48e..e27461771 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -212,7 +212,7 @@ class meta_any { meta_any(const meta_any &other, any ref) noexcept : storage{std::move(ref)}, ctx{other.ctx} { - if(storage) { + if(storage || !other.storage) { node = other.node; vtable = other.vtable; } diff --git a/test/entt/meta/meta_any.cpp b/test/entt/meta/meta_any.cpp index 0207c67af..88a653219 100644 --- a/test/entt/meta/meta_any.cpp +++ b/test/entt/meta/meta_any.cpp @@ -858,6 +858,35 @@ TEST_F(MetaAny, VoidInPlaceTypeConstruction) { ASSERT_NE(entt::meta_any{3}, any); } +TEST_F(MetaAny, VoidAsRefConstruction) { + entt::meta_any any{std::in_place_type}; + + ASSERT_TRUE(any); + ASSERT_FALSE(any.base().owner()); + ASSERT_EQ(any.base().policy(), entt::any_policy::empty); + ASSERT_EQ(any.type(), entt::resolve()); + + ASSERT_FALSE(any.try_cast()); + ASSERT_EQ(any.base().data(), nullptr); + + ASSERT_EQ(any, entt::meta_any{std::in_place_type}); + ASSERT_EQ(entt::meta_any{std::in_place_type}, any); + ASSERT_NE(entt::meta_any{3}, any); + + any = entt::meta_any{std::in_place_type}; + + ASSERT_TRUE(any); + ASSERT_EQ(any.type(), entt::resolve()); + ASSERT_EQ(any.base().data(), nullptr); + + auto other = any.as_ref(); + + ASSERT_TRUE(other); + ASSERT_EQ(any.type(), entt::resolve()); + ASSERT_EQ(any.base().data(), nullptr); + ASSERT_EQ(other.base().data(), any.base().data()); +} + TEST_F(MetaAny, VoidCopyConstruction) { const entt::meta_any any{std::in_place_type}; entt::meta_any other{any}; @@ -1201,8 +1230,8 @@ TEST_F(MetaAny, AsRef) { cref = std::as_const(any).as_ref(); ASSERT_TRUE(any); - ASSERT_FALSE(ref); - ASSERT_FALSE(cref); + ASSERT_TRUE(ref); + ASSERT_TRUE(cref); } ENTT_DEBUG_TEST_F(MetaAnyDeathTest, AsRef) {