diff --git a/src/entt/meta/factory.hpp b/src/entt/meta/factory.hpp index dd1456cff..095ef9019 100644 --- a/src/entt/meta/factory.hpp +++ b/src/entt/meta/factory.hpp @@ -76,59 +76,35 @@ template template -[[nodiscard]] bool setter([[maybe_unused]] meta_any instance, [[maybe_unused]] meta_any index, [[maybe_unused]] meta_any value) { +[[nodiscard]] bool setter([[maybe_unused]] meta_any instance, [[maybe_unused]] meta_any value) { bool accepted = false; if constexpr(std::is_function_v>> || std::is_member_function_pointer_v) { using helper_type = meta_function_helper_t; using data_type = std::tuple_element_t, typename helper_type::args_type>; - static_assert(std::is_invocable_v, "Invalid function or member function"); - auto * const clazz = instance.try_cast(); - auto * const direct = value.try_cast(); - if(clazz && (direct || value.convert())) { - std::invoke(Data, *clazz, direct ? *direct : value.cast()); - accepted = true; + if(auto * const clazz = instance.try_cast(); clazz) { + if(auto * const direct = value.try_cast(); direct || value.convert()) { + std::invoke(Data, *clazz, direct ? *direct : value.cast()); + accepted = true; + } } } else if constexpr(std::is_member_object_pointer_v) { using data_type = std::remove_cv_t().*Data)>>; - static_assert(std::is_invocable_v, "Invalid data member"); - auto * const clazz = instance.try_cast(); - if constexpr(std::is_array_v) { - using underlying_type = std::remove_extent_t; - auto * const direct = value.try_cast(); - auto * const idx = index.try_cast(); - - if(clazz && idx && (direct || value.convert())) { - std::invoke(Data, clazz)[*idx] = direct ? *direct : value.cast(); - accepted = true; - } - } else { - auto * const direct = value.try_cast(); - - if(clazz && (direct || value.convert())) { - std::invoke(Data, clazz) = (direct ? *direct : value.cast()); - accepted = true; + if constexpr(!std::is_array_v) { + if(auto * const clazz = instance.try_cast(); clazz) { + if(auto * const direct = value.try_cast(); direct || value.convert()) { + std::invoke(Data, clazz) = (direct ? *direct : value.cast()); + accepted = true; + } } } } else { - static_assert(std::is_pointer_v, "Invalid pointer to data type"); using data_type = std::remove_cv_t>; - if constexpr(std::is_array_v) { - using underlying_type = std::remove_extent_t; - auto * const direct = value.try_cast(); - auto * const idx = index.try_cast(); - - if(idx && (direct || value.convert())) { - (*Data)[*idx] = (direct ? *direct : value.cast()); - accepted = true; - } - } else { - auto * const direct = value.try_cast(); - - if(direct || value.convert()) { + if constexpr(!std::is_array_v) { + if(auto * const direct = value.try_cast(); direct || value.convert()) { *Data = (direct ? *direct : value.cast()); accepted = true; } @@ -140,7 +116,7 @@ template template -[[nodiscard]] meta_any getter([[maybe_unused]] meta_any instance, [[maybe_unused]] meta_any index) { +[[nodiscard]] meta_any getter([[maybe_unused]] meta_any instance) { auto dispatch = [](auto &&value) { if constexpr(std::is_same_v) { return meta_any{std::in_place_type, std::forward(value)}; @@ -153,24 +129,18 @@ template }; if constexpr(std::is_function_v>> || std::is_member_function_pointer_v) { - static_assert(std::is_invocable_v, "Invalid function or member function"); auto * const clazz = instance.try_cast(); return clazz ? dispatch(std::invoke(Data, *clazz)) : meta_any{}; } else if constexpr(std::is_member_object_pointer_v) { - using data_type = std::remove_cv_t().*Data)>>; - static_assert(std::is_invocable_v, "Invalid data member"); - auto * const clazz = instance.try_cast(); - - if constexpr(std::is_array_v) { - auto * const idx = index.try_cast(); - return (clazz && idx) ? dispatch(std::invoke(Data, clazz)[*idx]) : meta_any{}; + if constexpr(std::is_array_v().*Data)>>>) { + return meta_any{}; } else { + auto * const clazz = instance.try_cast(); return clazz ? dispatch(std::invoke(Data, clazz)) : meta_any{}; } } else if constexpr(std::is_pointer_v>) { if constexpr(std::is_array_v>) { - auto * const idx = index.try_cast(); - return idx ? dispatch((*Data)[*idx]) : meta_any{}; + return meta_any{}; } else { return dispatch(*Data); } @@ -666,7 +636,7 @@ public: false, &internal::meta_info::resolve, []() -> decltype(internal::meta_data_node::set) { - if constexpr(std::is_same_v || (std::is_member_object_pointer_v && std::is_const_v)) { + if constexpr(std::is_same_v || (std::is_member_object_pointer_v && std::is_const_v)) { return nullptr; } else { return &internal::setter; diff --git a/src/entt/meta/internal.hpp b/src/entt/meta/internal.hpp index 4ca20d741..83bbc67bf 100644 --- a/src/entt/meta/internal.hpp +++ b/src/entt/meta/internal.hpp @@ -77,8 +77,8 @@ struct meta_data_node { meta_prop_node * prop; const bool is_static; meta_type_node *(* const type)() ENTT_NOEXCEPT; - bool(* const set)(meta_any, meta_any, meta_any); - meta_any(* const get)(meta_any, meta_any); + bool(* const set)(meta_any, meta_any); + meta_any(* const get)(meta_any); }; diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 56d12a0cc..93b0ad812 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -711,28 +711,7 @@ struct meta_data { */ template bool set(meta_handle instance, Type &&value) const { - return node->set && node->set(*instance, {}, std::forward(value)); - } - - /** - * @brief Sets the i-th element of a given array. - * - * It must be possible to cast the instance to the parent type of the meta - * data. Otherwise, invoking the setter results in an undefined - * behavior.
- * The type of the value must be such that a cast or conversion to the array - * type is possible. Otherwise, invoking the setter does nothing. - * - * @tparam Type Type of value to assign. - * @param instance An opaque instance of the underlying type. - * @param index Position of the underlying element to set. - * @param value Parameter to use to set the underlying element. - * @return True in case of success, false otherwise. - */ - template - bool set(meta_handle instance, std::size_t index, Type &&value) const { - ENTT_ASSERT(index < node->type()->extent); - return node->set && node->set(*instance, index, std::forward(value)); + return node->set && node->set(*instance, std::forward(value)); } /** @@ -745,22 +724,7 @@ struct meta_data { * @return A meta any containing the value of the underlying variable. */ [[nodiscard]] meta_any get(meta_handle instance) const { - return node->get(*instance, {}); - } - - /** - * @brief Gets the i-th element of a given array. - * - * It must be possible to cast the instance to the parent type of the meta - * data. Otherwise, invoking the getter results in an undefined behavior. - * - * @param instance An opaque instance of the underlying type. - * @param index Position of the underlying element to get. - * @return A meta any containing the value of the underlying element. - */ - [[nodiscard]] meta_any get(meta_handle instance, std::size_t index) const { - ENTT_ASSERT(index < node->type()->extent); - return node->get(*instance, index); + return node->get(*instance); } /*! @copydoc meta_ctor::prop */ diff --git a/test/entt/meta/meta_data.cpp b/test/entt/meta/meta_data.cpp index 4c2637ee4..1b907112a 100644 --- a/test/entt/meta/meta_data.cpp +++ b/test/entt/meta/meta_data.cpp @@ -54,7 +54,7 @@ struct setter_getter_t { struct array_t { static inline int global[3]; - int local[3]; + int local[5]; }; enum class property_t { @@ -347,10 +347,6 @@ TEST_F(MetaData, SetterGetterReadOnlyDataMember) { TEST_F(MetaData, ArrayStatic) { auto data = entt::resolve().data("global"_hs); - array_t::global[0] = 3; - array_t::global[1] = 5; - array_t::global[2] = 7; - ASSERT_TRUE(data); ASSERT_EQ(data.parent(), entt::resolve_id("array"_hs)); ASSERT_EQ(data.type(), entt::resolve()); @@ -359,46 +355,22 @@ TEST_F(MetaData, ArrayStatic) { ASSERT_TRUE(data.is_static()); ASSERT_TRUE(data.type().is_array()); ASSERT_EQ(data.type().extent(), 3); - ASSERT_EQ(data.get({}, 0).cast(), 3); - ASSERT_EQ(data.get({}, 1).cast(), 5); - ASSERT_EQ(data.get({}, 2).cast(), 7); - ASSERT_FALSE(data.set({}, 0, 'c')); - ASSERT_EQ(data.get({}, 0).cast(), 3); - ASSERT_TRUE(data.set({}, 0, data.get({}, 0).cast()+2)); - ASSERT_TRUE(data.set({}, 1, data.get({}, 1).cast()+2)); - ASSERT_TRUE(data.set({}, 2, data.get({}, 2).cast()+2)); - ASSERT_EQ(data.get({}, 0).cast(), 5); - ASSERT_EQ(data.get({}, 1).cast(), 7); - ASSERT_EQ(data.get({}, 2).cast(), 9); + ASSERT_FALSE(data.get({})); } TEST_F(MetaData, Array) { auto data = entt::resolve().data("local"_hs); array_t instance; - instance.local[0] = 3; - instance.local[1] = 5; - instance.local[2] = 7; - ASSERT_TRUE(data); ASSERT_EQ(data.parent(), entt::resolve_id("array"_hs)); - ASSERT_EQ(data.type(), entt::resolve()); + ASSERT_EQ(data.type(), entt::resolve()); ASSERT_EQ(data.id(), "local"_hs); ASSERT_FALSE(data.is_const()); ASSERT_FALSE(data.is_static()); ASSERT_TRUE(data.type().is_array()); - ASSERT_EQ(data.type().extent(), 3); - ASSERT_EQ(data.get(instance, 0).cast(), 3); - ASSERT_EQ(data.get(instance, 1).cast(), 5); - ASSERT_EQ(data.get(instance, 2).cast(), 7); - ASSERT_FALSE(data.set(instance, 0, 'c')); - ASSERT_EQ(data.get(instance, 0).cast(), 3); - ASSERT_TRUE(data.set(instance, 0, data.get(instance, 0).cast()+2)); - ASSERT_TRUE(data.set(instance, 1, data.get(instance, 1).cast()+2)); - ASSERT_TRUE(data.set(instance, 2, data.get(instance, 2).cast()+2)); - ASSERT_EQ(data.get(instance, 0).cast(), 5); - ASSERT_EQ(data.get(instance, 1).cast(), 7); - ASSERT_EQ(data.get(instance, 2).cast(), 9); + ASSERT_EQ(data.type().extent(), 5); + ASSERT_FALSE(data.get(instance)); } TEST_F(MetaData, AsVoid) {