Просмотр исходного кода

any: fix as_ref mode for empty objects

skypjack 17 часов назад
Родитель
Сommit
b43f7efebf
3 измененных файлов с 10 добавлено и 22 удалено
  1. 0 1
      TODO
  2. 2 13
      src/entt/core/any.hpp
  3. 8 8
      test/entt/core/any.cpp

+ 0 - 1
TODO

@@ -24,4 +24,3 @@ TODO:
 * finish the imgui viewer/editor!
 * entity io
 * storage.cpp update_from_destructor triggers natvis errors
-* const any::as_ref sets the wrong mode on empty objects

+ 2 - 13
src/entt/core/any.hpp

@@ -489,18 +489,7 @@ public:
      */
     [[nodiscard]] basic_any as_ref() noexcept {
         basic_any other = stl::as_const(*this).as_ref();
-
-        switch(mode) {
-            using enum any_policy;
-        case cref:
-        case empty:
-            other.mode = mode;
-            break;
-        default:
-            other.mode = any_policy::ref;
-            break;
-        }
-
+        other.mode = (has_value() && (mode != any_policy::cref)) ? any_policy::ref : mode;
         return other;
     }
 
@@ -510,7 +499,7 @@ public:
         other.instance = data();
         other.vtable = vtable;
         other.underlying_type = underlying_type;
-        other.mode = any_policy::cref;
+        other.mode = has_value() ? any_policy::cref : mode;
         return other;
     }
 

+ 8 - 8
test/entt/core/any.cpp

@@ -63,20 +63,20 @@ TEST_F(Any, Empty) {
 TEST_F(Any, EmptyAsRef) {
     entt::any any{};
 
-    ASSERT_FALSE(any);
-    ASSERT_FALSE(any.owner());
-    ASSERT_EQ(any.policy(), entt::any_policy::empty);
-    ASSERT_EQ(any.info(), entt::type_id<void>());
-    ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
-    ASSERT_EQ(any.data(), nullptr);
-
     auto other = any.as_ref();
 
     ASSERT_FALSE(other);
     ASSERT_FALSE(other.owner());
     ASSERT_EQ(other.policy(), entt::any_policy::empty);
     ASSERT_EQ(other.info(), entt::type_id<void>());
-    ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
+    ASSERT_EQ(other.data(), nullptr);
+
+    other = std::as_const(any).as_ref();
+
+    ASSERT_FALSE(other);
+    ASSERT_FALSE(other.owner());
+    ASSERT_EQ(other.policy(), entt::any_policy::empty);
+    ASSERT_EQ(other.info(), entt::type_id<void>());
     ASSERT_EQ(other.data(), nullptr);
 }