Browse Source

meta: allow updating values on meta properties

Michele Caini 3 years ago
parent
commit
f41b914197
2 changed files with 20 additions and 1 deletions
  1. 8 0
      src/entt/meta/meta.hpp
  2. 12 1
      test/entt/meta/meta_prop.cpp

+ 8 - 0
src/entt/meta/meta.hpp

@@ -763,6 +763,14 @@ struct meta_prop {
         return node->value ? node->type(internal::meta_context::from(*ctx)).from_void(*ctx, nullptr, node->value.get()) : meta_any{meta_ctx_arg, *ctx};
     }
 
+    /**
+     * @brief Returns the stored value by reference.
+     * @return A wrapper containing the value stored with the property.
+     */
+    [[nodiscard]] meta_any value() {
+        return node->value ? node->type(internal::meta_context::from(*ctx)).from_void(*ctx, node->value.get(), nullptr) : meta_any{meta_ctx_arg, *ctx};
+    }
+
     /**
      * @brief Returns true if an object is valid, false otherwise.
      * @return True if the object is valid, false otherwise.

+ 12 - 1
test/entt/meta/meta_prop.cpp

@@ -49,7 +49,18 @@ TEST_F(MetaProp, Functionalities) {
     auto prop = entt::resolve<base_1_t>().prop("int"_hs);
 
     ASSERT_TRUE(prop);
-    ASSERT_EQ(prop.value(), 42);
+
+    auto value = prop.value();
+    auto cvalue = std::as_const(prop).value();
+
+    ASSERT_NE(value.try_cast<int>(), nullptr);
+    ASSERT_EQ(cvalue.try_cast<int>(), nullptr);
+
+    ASSERT_NE(value.try_cast<const int>(), nullptr);
+    ASSERT_NE(cvalue.try_cast<const int>(), nullptr);
+
+    ASSERT_EQ(value, 42);
+    ASSERT_EQ(cvalue, 42);
 }
 
 TEST_F(MetaProp, FromBase) {