Преглед изворни кода

meta: as_ref for void types still returns a valid meta_any

Michele Caini пре 1 година
родитељ
комит
8e6bbc4a6c
3 измењених фајлова са 32 додато и 4 уклоњено
  1. 0 1
      TODO
  2. 1 1
      src/entt/meta/meta.hpp
  3. 31 2
      test/entt/meta/meta_any.cpp

+ 0 - 1
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)

+ 1 - 1
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;
         }

+ 31 - 2
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<void>};
+
+    ASSERT_TRUE(any);
+    ASSERT_FALSE(any.base().owner());
+    ASSERT_EQ(any.base().policy(), entt::any_policy::empty);
+    ASSERT_EQ(any.type(), entt::resolve<void>());
+
+    ASSERT_FALSE(any.try_cast<std::size_t>());
+    ASSERT_EQ(any.base().data(), nullptr);
+
+    ASSERT_EQ(any, entt::meta_any{std::in_place_type<void>});
+    ASSERT_EQ(entt::meta_any{std::in_place_type<void>}, any);
+    ASSERT_NE(entt::meta_any{3}, any);
+
+    any = entt::meta_any{std::in_place_type<void>};
+
+    ASSERT_TRUE(any);
+    ASSERT_EQ(any.type(), entt::resolve<void>());
+    ASSERT_EQ(any.base().data(), nullptr);
+
+    auto other = any.as_ref();
+
+    ASSERT_TRUE(other);
+    ASSERT_EQ(any.type(), entt::resolve<void>());
+    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<void>};
     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) {