From b0d3a84d1e87f3c3da6223835d05f3f88cfde96d Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 1 Mar 2019 16:25:46 +0100 Subject: [PATCH] setters/getters correctly handle references now (#fix 196) --- src/entt/meta/meta.hpp | 52 ++++++++++++++++++----------------------- test/entt/meta/meta.cpp | 22 ++++++++++++++++- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 1866f8f02..9d171d5a1 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -2034,49 +2034,43 @@ inline meta_any construct(meta_any * const args, std::index_sequence template bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any &any) { + bool accepted = false; + if constexpr(Const) { return false; - } else if constexpr(std::is_function_v> || std::is_member_pointer_v) { - auto execute = [&handle, &any](auto *tag) { - using data_type = std::remove_pointer_t; - const bool accepted = any.can_cast() || any.convert(); - auto *clazz = handle.try_cast(); + } else if constexpr(std::is_function_v> || std::is_member_function_pointer_v) { + using helper_type = meta_function_helper>; + using data_type = std::decay_t, typename helper_type::args_type>>; + accepted = any.can_cast() || any.convert(); + auto *clazz = handle.try_cast(); - if(accepted && clazz) { - if constexpr(std::is_function_v>) { - Data(*clazz, any.cast()); - } else if constexpr(std::is_member_function_pointer_v) { - (clazz->*Data)(any.cast()); - } else /* if constexpr(std::is_member_object_pointer_v) */ { - clazz->*Data = any.cast(); - } + if(accepted && clazz) { + if constexpr(std::is_function_v>) { + Data(*clazz, any.cast()); + } else if constexpr(std::is_member_function_pointer_v) { + (clazz->*Data)(any.cast()); } + } + } else if constexpr(std::is_member_object_pointer_v) { + using data_type = std::decay_t().*Data)>; + static_assert(std::is_invocable_v); + accepted = any.can_cast() || any.convert(); + auto *clazz = handle.try_cast(); - return accepted; - }; - - if constexpr(std::is_function_v> || std::is_member_function_pointer_v) { - using helper_type = meta_function_helper>; - using data_type = std::tuple_element_t, typename helper_type::args_type>; - data_type *tag = nullptr; - return execute(tag); - } else /* if constexpr(std::is_member_object_pointer_v) */ { - using data_type = std::decay_t().*Data)>; - static_assert(std::is_invocable_v); - data_type *tag = nullptr; - return execute(tag); + if(accepted && clazz) { + clazz->*Data = any.cast(); } } else { static_assert(std::is_pointer_v); using data_type = std::decay_t; - const bool accepted = any.can_cast() || any.convert(); + accepted = any.can_cast() || any.convert(); if(accepted) { *Data = any.cast(); } - - return accepted; } + + return accepted; } diff --git a/test/entt/meta/meta.cpp b/test/entt/meta/meta.cpp index 86a7ee7a2..652467edb 100644 --- a/test/entt/meta/meta.cpp +++ b/test/entt/meta/meta.cpp @@ -86,6 +86,9 @@ struct setter_getter_type { int setter(int value) { return this->value = value; } int getter() { return value; } + int setter_with_ref(const int &value) { return this->value = value; } + const int & getter_with_ref() { return value; } + static int static_setter(setter_getter_type &type, int value) { return type.value = value; } static int static_getter(const setter_getter_type &type) { return type.value; } }; @@ -159,7 +162,8 @@ struct Meta: public ::testing::Test { entt::reflect("setter_getter") .data<&setter_getter_type::static_setter, &setter_getter_type::static_getter>("x") .data<&setter_getter_type::setter, &setter_getter_type::getter>("y") - .data<&setter_getter_type::static_setter, &setter_getter_type::getter>("z"); + .data<&setter_getter_type::static_setter, &setter_getter_type::getter>("z") + .data<&setter_getter_type::setter_with_ref, &setter_getter_type::getter_with_ref>("w"); entt::reflect("an_abstract_type", std::make_pair(properties::prop_bool, false)) .data<&an_abstract_type::i>("i") @@ -960,6 +964,22 @@ TEST_F(Meta, MetaDataSetterGetterAsMemberFunctions) { ASSERT_EQ(data.get(instance).cast(), 42); } +TEST_F(Meta, MetaDataSetterGetterWithRefAsMemberFunctions) { + auto data = entt::resolve().data("w"); + setter_getter_type instance{}; + + ASSERT_TRUE(data); + ASSERT_NE(data, entt::meta_data{}); + ASSERT_EQ(data.parent(), entt::resolve("setter_getter")); + ASSERT_EQ(data.type(), entt::resolve()); + ASSERT_STREQ(data.name(), "w"); + ASSERT_FALSE(data.is_const()); + ASSERT_FALSE(data.is_static()); + ASSERT_EQ(data.get(instance).cast(), 0); + ASSERT_TRUE(data.set(instance, 42)); + ASSERT_EQ(data.get(instance).cast(), 42); +} + TEST_F(Meta, MetaDataSetterGetterMixed) { auto data = entt::resolve().data("z"); setter_getter_type instance{};