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

any: as_ref() on empty returns empty

skypjack 3 недель назад
Родитель
Сommit
071febbc0c
3 измененных файлов с 48 добавлено и 1 удалено
  1. 12 1
      src/entt/core/any.hpp
  2. 20 0
      test/entt/core/any.cpp
  3. 16 0
      test/entt/meta/meta_any.cpp

+ 12 - 1
src/entt/core/any.hpp

@@ -484,7 +484,18 @@ public:
      */
     [[nodiscard]] basic_any as_ref() noexcept {
         basic_any other = std::as_const(*this).as_ref();
-        other.mode = (mode == any_policy::cref ? any_policy::cref : any_policy::ref);
+
+        switch(mode) {
+            using enum any_policy;
+        case cref:
+        case empty:
+            other.mode = mode;
+            break;
+        default:
+            other.mode = any_policy::ref;
+            break;
+        }
+
         return other;
     }
 

+ 20 - 0
test/entt/core/any.cpp

@@ -57,6 +57,26 @@ TEST(Any, Empty) {
     ASSERT_EQ(any.data(), nullptr);
 }
 
+TEST(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);
+}
+
 TEST(Any, HasValue) {
     entt::any any{};
 

+ 16 - 0
test/entt/meta/meta_any.cpp

@@ -131,6 +131,22 @@ TEST_F(MetaAny, Empty) {
     ASSERT_FALSE(std::as_const(any).as_associative_container());
 }
 
+TEST_F(MetaAny, EmptyAsRef) {
+    entt::meta_any any{};
+
+    ASSERT_FALSE(any);
+    ASSERT_FALSE(any.type());
+    ASSERT_EQ(any.base().data(), nullptr);
+    ASSERT_EQ(any, entt::meta_any{});
+
+    auto other = any.as_ref();
+
+    ASSERT_FALSE(other);
+    ASSERT_FALSE(other.type());
+    ASSERT_EQ(other.base().data(), nullptr);
+    ASSERT_EQ(other, entt::meta_any{});
+}
+
 TEST_F(MetaAny, Context) {
     entt::meta_any any{};
     const entt::meta_ctx ctx{};