diff --git a/TODO b/TODO index c31c9f61d..27754d888 100644 --- a/TODO +++ b/TODO @@ -37,3 +37,4 @@ TODO: * allow passing arguments to meta setter/getter (we can fallback on meta invoke probably) * meta: remove ctx arg from utilities (handle/any), container ctors/funcs (it), func invoke (any, with check) * meta: remove ctx member from meta_data, meta_func, ... (use handles and the like) +* meta: do not rebind args or instances internally diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index f43663a47..58c1ad07b 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -1017,7 +1017,7 @@ struct meta_func { * @return A wrapper containing the returned value, if any. */ meta_any invoke(meta_handle instance, meta_any *const args, const size_type sz) const { - return ((node.invoke != nullptr) && (sz == arity())) ? node.invoke(*ctx, meta_handle{*ctx, std::move(instance)}, args) : meta_any{meta_ctx_arg, *ctx}; + return ((node.invoke != nullptr) && (sz == arity())) ? node.invoke(meta_handle{*ctx, std::move(instance)}, args) : meta_any{meta_ctx_arg, *ctx}; } /** @@ -1438,7 +1438,7 @@ public: if(node.details) { if(auto *elem = internal::find_member<&internal::meta_func_node::id>(node.details->func, id); elem != nullptr) { if(const auto *candidate = lookup(args, sz, (instance->base().policy() == any_policy::cref), [curr = elem]() mutable { return (curr != nullptr) ? std::exchange(curr, curr->next.get()) : nullptr; }); candidate) { - return candidate->invoke(*ctx, meta_handle{*ctx, std::move(instance)}, args); + return candidate->invoke(meta_handle{*ctx, std::move(instance)}, args); } } } diff --git a/src/entt/meta/node.hpp b/src/entt/meta/node.hpp index 28cba4d55..f06a10fa9 100644 --- a/src/entt/meta/node.hpp +++ b/src/entt/meta/node.hpp @@ -112,7 +112,7 @@ struct meta_func_node { size_type arity{0u}; meta_type_node (*ret)(const meta_context &) noexcept {}; meta_type (*arg)(const meta_ctx &, const size_type) noexcept {}; - meta_any (*invoke)(const meta_ctx &, meta_handle, meta_any *const){}; + meta_any (*invoke)(meta_handle, meta_any *const){}; std::shared_ptr next; meta_custom_node custom{}; }; diff --git a/src/entt/meta/utility.hpp b/src/entt/meta/utility.hpp index 1ce8179db..78062088f 100644 --- a/src/entt/meta/utility.hpp +++ b/src/entt/meta/utility.hpp @@ -204,26 +204,26 @@ template } template -[[nodiscard]] meta_any meta_invoke(const meta_ctx &ctx, [[maybe_unused]] meta_handle instance, Candidate &&candidate, [[maybe_unused]] meta_any *const args, std::index_sequence) { +[[nodiscard]] meta_any meta_invoke(meta_handle instance, Candidate &&candidate, [[maybe_unused]] meta_any *const args, std::index_sequence) { using descriptor = meta_function_helper_t>; // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) - waiting for C++20 (and std::span) if constexpr(std::is_invocable_v, const Type &, type_list_element_t...>) { if(const auto *const clazz = instance->try_cast(); clazz && ((args + Index)->allow_cast>() && ...)) { - return meta_invoke_with_args(ctx, std::forward(candidate), *clazz, (args + Index)->cast>()...); + return meta_invoke_with_args(instance->context(), std::forward(candidate), *clazz, (args + Index)->cast>()...); } } else if constexpr(std::is_invocable_v, Type &, type_list_element_t...>) { if(auto *const clazz = instance->try_cast(); clazz && ((args + Index)->allow_cast>() && ...)) { - return meta_invoke_with_args(ctx, std::forward(candidate), *clazz, (args + Index)->cast>()...); + return meta_invoke_with_args(instance->context(), std::forward(candidate), *clazz, (args + Index)->cast>()...); } } else { if(((args + Index)->allow_cast>() && ...)) { - return meta_invoke_with_args(ctx, std::forward(candidate), (args + Index)->cast>()...); + return meta_invoke_with_args(instance->context(), std::forward(candidate), (args + Index)->cast>()...); } } // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) - return meta_any{meta_ctx_arg, ctx}; + return meta_any{meta_ctx_arg, instance->context()}; } template @@ -356,27 +356,6 @@ template return meta_getter(meta_handle{ctx, std::move(instance)}); } -/** - * @brief Tries to _invoke_ an object given a list of erased parameters. - * - * @warning - * The context provided is used only for the return type.
- * It's up to the caller to bind the arguments to the right context(s). - * - * @tparam Type Reflected type to which the object to _invoke_ is associated. - * @tparam Policy Optional policy (no policy set by default). - * @param ctx The context from which to search for meta types. - * @tparam Candidate The type of the actual object to _invoke_. - * @param instance An opaque instance of the underlying type, if required. - * @param candidate The actual object to _invoke_. - * @param args Parameters to use to _invoke_ the object. - * @return A meta any containing the returned value, if any. - */ -template -[[nodiscard]] std::enable_if_t, meta_any> meta_invoke(const meta_ctx &ctx, meta_handle instance, Candidate &&candidate, meta_any *const args) { - return internal::meta_invoke(ctx, std::move(instance), std::forward(candidate), args, std::make_index_sequence>::args_type::size>{}); -} - /** * @brief Tries to _invoke_ an object given a list of erased parameters. * @tparam Type Reflected type to which the object to _invoke_ is associated. @@ -389,27 +368,23 @@ template */ template [[nodiscard]] std::enable_if_t, meta_any> meta_invoke(meta_handle instance, Candidate &&candidate, meta_any *const args) { - return meta_invoke(locator::value_or(), std::move(instance), std::forward(candidate), args); + return internal::meta_invoke(std::move(instance), std::forward(candidate), args, std::make_index_sequence>::args_type::size>{}); } /** - * @brief Tries to invoke a function given a list of erased parameters. - * - * @warning - * The context provided is used only for the return type.
- * It's up to the caller to bind the arguments to the right context(s). - * - * @tparam Type Reflected type to which the function is associated. - * @tparam Candidate The actual function to invoke. + * @brief Tries to _invoke_ an object given a list of erased parameters. + * @tparam Type Reflected type to which the object to _invoke_ is associated. * @tparam Policy Optional policy (no policy set by default). * @param ctx The context from which to search for meta types. + * @tparam Candidate The type of the actual object to _invoke_. * @param instance An opaque instance of the underlying type, if required. - * @param args Parameters to use to invoke the function. + * @param candidate The actual object to _invoke_. + * @param args Parameters to use to _invoke_ the object. * @return A meta any containing the returned value, if any. */ -template -[[nodiscard]] std::enable_if_t, meta_any> meta_invoke(const meta_ctx &ctx, meta_handle instance, meta_any *const args) { - return internal::meta_invoke(ctx, std::move(instance), Candidate, args, std::make_index_sequence>::args_type::size>{}); +template +[[deprecated("a context is no longer required, it is inferred from the meta_handle")]] [[nodiscard]] std::enable_if_t, meta_any> meta_invoke(const meta_ctx &ctx, meta_handle instance, Candidate &&candidate, meta_any *const args) { + return meta_invoke(meta_handle{ctx, std::move(instance)}, std::forward(candidate), args); } /** @@ -423,7 +398,22 @@ template */ template [[nodiscard]] std::enable_if_t, meta_any> meta_invoke(meta_handle instance, meta_any *const args) { - return meta_invoke(locator::value_or(), std::move(instance), args); + return internal::meta_invoke(std::move(instance), Candidate, args, std::make_index_sequence>::args_type::size>{}); +} + +/** + * @brief Tries to invoke a function given a list of erased parameters. + * @tparam Type Reflected type to which the function is associated. + * @tparam Candidate The actual function to invoke. + * @tparam Policy Optional policy (no policy set by default). + * @param ctx The context from which to search for meta types. + * @param instance An opaque instance of the underlying type, if required. + * @param args Parameters to use to invoke the function. + * @return A meta any containing the returned value, if any. + */ +template +[[deprecated("a context is no longer required, it is inferred from the meta_handle")]] [[nodiscard]] std::enable_if_t, meta_any> meta_invoke(const meta_ctx &ctx, meta_handle instance, meta_any *const args) { + return meta_invoke(meta_handle{ctx, std::move(instance)}, args); } /** @@ -474,10 +464,11 @@ template template [[nodiscard]] meta_any meta_construct(const meta_ctx &ctx, Candidate &&candidate, meta_any *const args) { if constexpr(meta_function_helper_t::is_static || std::is_class_v>>) { - return internal::meta_invoke(ctx, {}, std::forward(candidate), args, std::make_index_sequence>::args_type::size>{}); + return internal::meta_invoke(meta_handle{meta_ctx_arg, ctx}, std::forward(candidate), args, std::make_index_sequence>::args_type::size>{}); } else { + meta_any handle{ctx, args->as_ref()}; // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - waiting for C++20 (and std::span) - return internal::meta_invoke(ctx, *args, std::forward(candidate), args + 1u, std::make_index_sequence>::args_type::size>{}); + return internal::meta_invoke(handle, std::forward(candidate), args + 1u, std::make_index_sequence>::args_type::size>{}); } }