From f41b914197595ad0fffc9dfd62159ab60135ac0f Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 17 Mar 2023 08:45:58 +0100 Subject: [PATCH] meta: allow updating values on meta properties --- src/entt/meta/meta.hpp | 8 ++++++++ test/entt/meta/meta_prop.cpp | 13 ++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 395a4a5fc..ac36ff3f2 100644 --- a/src/entt/meta/meta.hpp +++ b/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. diff --git a/test/entt/meta/meta_prop.cpp b/test/entt/meta/meta_prop.cpp index 92071c5c8..9c90cff70 100644 --- a/test/entt/meta/meta_prop.cpp +++ b/test/entt/meta/meta_prop.cpp @@ -49,7 +49,18 @@ TEST_F(MetaProp, Functionalities) { auto prop = entt::resolve().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(), nullptr); + ASSERT_EQ(cvalue.try_cast(), nullptr); + + ASSERT_NE(value.try_cast(), nullptr); + ASSERT_NE(cvalue.try_cast(), nullptr); + + ASSERT_EQ(value, 42); + ASSERT_EQ(cvalue, 42); } TEST_F(MetaProp, FromBase) {