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

meta_any:
* introduce meta_any_policy
* deprecate ::owner member
* add ::policy member

Michele Caini 2 лет назад
Родитель
Сommit
c993388e5c
2 измененных файлов с 22 добавлено и 4 удалено
  1. 15 4
      src/entt/meta/meta.hpp
  2. 7 0
      test/entt/meta/meta_any.cpp

+ 15 - 4
src/entt/meta/meta.hpp

@@ -149,6 +149,9 @@ private:
     any storage{};
 };
 
+/*! @brief Possible modes of a meta any object. */
+using meta_any_policy = any_policy;
+
 /*! @brief Opaque wrapper for values of any type. */
 class meta_any {
     enum class operation : std::uint8_t {
@@ -197,7 +200,7 @@ class meta_any {
     }
 
     void release() {
-        if(node.dtor.dtor && owner()) {
+        if(node.dtor.dtor && (storage.policy() == any_policy::owner)) {
             node.dtor.dtor(storage.data());
         }
     }
@@ -455,7 +458,7 @@ public:
      */
     [[nodiscard]] bool allow_cast(const meta_type &type) {
         if(auto other = std::as_const(*this).allow_cast(type); other) {
-            if(other.owner()) {
+            if((other.storage.policy() == any_policy::owner)) {
                 std::swap(*this, other);
             }
 
@@ -593,8 +596,16 @@ public:
     }
 
     /*! @copydoc any::owner */
-    [[nodiscard]] bool owner() const noexcept {
-        return storage.owner();
+    [[deprecated("use policy() and meta_any_policy instead")]] [[nodiscard]] bool owner() const noexcept {
+        return (storage.policy() == any_policy::owner);
+    }
+
+    /**
+     * @brief Returns the current mode of a meta any object.
+     * @return The current mode of the meta any object.
+     */
+    [[nodiscard]] meta_any_policy policy() const noexcept {
+        return storage.policy();
     }
 
 private:

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

@@ -110,6 +110,7 @@ TEST_F(MetaAny, SBO) {
 
     ASSERT_TRUE(any);
     ASSERT_TRUE(any.owner());
+    ASSERT_EQ(any.policy(), entt::meta_any_policy::owner);
     ASSERT_FALSE(any.try_cast<std::size_t>());
     ASSERT_EQ(any.cast<char>(), 'c');
     ASSERT_NE(any.data(), nullptr);
@@ -123,6 +124,7 @@ TEST_F(MetaAny, NoSBO) {
 
     ASSERT_TRUE(any);
     ASSERT_TRUE(any.owner());
+    ASSERT_EQ(any.policy(), entt::meta_any_policy::owner);
     ASSERT_FALSE(any.try_cast<std::size_t>());
     ASSERT_EQ(any.cast<fat_t>(), instance);
     ASSERT_NE(any.data(), nullptr);
@@ -168,6 +170,7 @@ TEST_F(MetaAny, SBOAsRefConstruction) {
 
     ASSERT_TRUE(any);
     ASSERT_FALSE(any.owner());
+    ASSERT_EQ(any.policy(), entt::meta_any_policy::ref);
     ASSERT_EQ(any.type(), entt::resolve<int>());
 
     ASSERT_FALSE(any.try_cast<std::size_t>());
@@ -204,6 +207,7 @@ TEST_F(MetaAny, SBOAsConstRefConstruction) {
 
     ASSERT_TRUE(any);
     ASSERT_FALSE(any.owner());
+    ASSERT_EQ(any.policy(), entt::meta_any_policy::cref);
     ASSERT_EQ(any.type(), entt::resolve<int>());
 
     ASSERT_FALSE(any.try_cast<std::size_t>());
@@ -436,6 +440,7 @@ TEST_F(MetaAny, NoSBOAsRefConstruction) {
 
     ASSERT_TRUE(any);
     ASSERT_FALSE(any.owner());
+    ASSERT_EQ(any.policy(), entt::meta_any_policy::ref);
     ASSERT_EQ(any.type(), entt::resolve<fat_t>());
 
     ASSERT_FALSE(any.try_cast<std::size_t>());
@@ -470,6 +475,7 @@ TEST_F(MetaAny, NoSBOAsConstRefConstruction) {
 
     ASSERT_TRUE(any);
     ASSERT_FALSE(any.owner());
+    ASSERT_EQ(any.policy(), entt::meta_any_policy::cref);
     ASSERT_EQ(any.type(), entt::resolve<fat_t>());
 
     ASSERT_FALSE(any.try_cast<std::size_t>());
@@ -707,6 +713,7 @@ TEST_F(MetaAny, VoidInPlaceTypeConstruction) {
 
     ASSERT_TRUE(any);
     ASSERT_TRUE(any.owner());
+    ASSERT_EQ(any.policy(), entt::meta_any_policy::owner);
     ASSERT_FALSE(any.try_cast<char>());
     ASSERT_EQ(any.data(), nullptr);
     ASSERT_EQ(any.type(), entt::resolve<void>());