///\file /****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl https://www.etlcpp.com Copyright(c) 2025 John Wellbelove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #ifndef ETL_INVOKE_INCLUDED #define ETL_INVOKE_INCLUDED #include "platform.h" #include "function_traits.h" #include "functional.h" #include "type_traits.h" #include "utility.h" #if ETL_USING_CPP11 namespace etl { //**************************************************************************** // invoke implementation //**************************************************************************** //**************************************************************************** /// Pointer to member function + reference_wrapper template < typename TFunction, typename TRefWrapper, typename... TArgs, typename = etl::enable_if_t< etl::is_member_function_pointer>::value && etl::is_reference_wrapper>::value>> ETL_CONSTEXPR auto invoke(TFunction&& f, TRefWrapper&& ref_wrapper, TArgs&&... args) -> decltype((ref_wrapper.get().*f)(etl::forward(args)...)) { return (ref_wrapper.get().*f)(etl::forward(args)...); } //**************************************************************************** /// Pointer to member function + pointer to object template < typename TFunction, typename TPtr, typename... TArgs, typename = etl::enable_if_t< etl::is_member_function_pointer>::value && etl::is_pointer>::value>> ETL_CONSTEXPR auto invoke(TFunction&& f, TPtr&& ptr, TArgs&&... args) -> decltype(((*etl::forward(ptr)).*f)(etl::forward(args)...)) { return ((*etl::forward(ptr)).*f)(etl::forward(args)...); } //**************************************************************************** /// Pointer to member function + object (or derived) reference template < typename TFunction, typename TObject, typename... TArgs, typename = etl::enable_if_t< etl::is_member_function_pointer>::value && !etl::is_pointer>::value && !is_reference_wrapper>::value>> ETL_CONSTEXPR auto invoke(TFunction&& f, TObject&& obj, TArgs&&... args) -> decltype((etl::forward(obj).*f)(etl::forward(args)...)) { return (etl::forward(obj).*f)(etl::forward(args)...); } //**************************************************************************** /// Pointer to member object + reference_wrapper template < typename TFunction, typename TRefWrapper, typename = etl::enable_if_t< etl::is_member_object_pointer>::value && etl::is_reference_wrapper>::value>> ETL_CONSTEXPR auto invoke(TFunction&& f, TRefWrapper&& ref_wrapper) -> decltype(ref_wrapper.get().*f) { return ref_wrapper.get().*f; } //**************************************************************************** /// Pointer to member object + pointer to object template < typename TFunction, typename TPtr, typename = etl::enable_if_t< etl::is_member_object_pointer>::value && etl::is_pointer>::value>> ETL_CONSTEXPR auto invoke(TFunction&& f, TPtr&& ptr) -> decltype(((*etl::forward(ptr)).*f)) { return ((*etl::forward(ptr)).*f); } //**************************************************************************** /// Pointer to member object + object (or derived) reference template < typename TFunction, typename TObject, typename = etl::enable_if_t< etl::is_member_object_pointer>::value && !etl::is_pointer>::value && !is_reference_wrapper>::value>> ETL_CONSTEXPR auto invoke(TFunction&& f, TObject&& obj) -> decltype(etl::forward(obj).*f) { return etl::forward(obj).*f; } //**************************************************************************** /// reference_wrapper callable (unwrap and call directly) template < typename TFunction, typename... TArgs, typename = etl::enable_if_t< etl::is_reference_wrapper>::value && !etl::is_member_pointer().get())>>::value>> ETL_CONSTEXPR auto invoke(TFunction&& f, TArgs&&... args) -> decltype(f.get()(etl::forward(args)...)) { return f.get()(etl::forward(args)...); } //**************************************************************************** /// reference_wrapper callable wrapping a member pointer (unwrap and /// re-invoke) template < typename TFunction, typename... TArgs, typename = etl::enable_if_t< etl::is_reference_wrapper>::value && etl::is_member_pointer().get())>>::value>, typename = void> ETL_CONSTEXPR auto invoke(TFunction&& f, TArgs&&... args) -> decltype(etl::invoke(f.get(), etl::forward(args)...)) { return etl::invoke(f.get(), etl::forward(args)...); } //**************************************************************************** /// General callable (function object / lambda / function pointer) template < typename TFunction, typename... TArgs, typename = etl::enable_if_t< !etl::is_member_pointer>::value && !etl::is_reference_wrapper>::value>> ETL_CONSTEXPR auto invoke(TFunction&& f, TArgs&&... args) -> decltype(etl::forward(f)(etl::forward(args)...)) { return etl::forward(f)(etl::forward(args)...); } //**************************************************************************** // is_invocable implementation //**************************************************************************** namespace private_invoke { //******************************************* // Core detection of invocability. // Succeeds if the invocation expression is well-formed. template struct is_invocable_expr { template static auto test(int) -> decltype((void)etl::invoke(etl::declval(), etl::declval()...), etl::true_type{}); template static etl::false_type test(...); using type = decltype(test(0)); static ETL_CONSTANT bool value = type::value; }; //******************************************* // Core detection of invocability. // Succeeds if the invocation expression is well-formed. // Using etl::type_list for the argument list. template struct is_invocable_expr> { template static auto test(int) -> decltype((void)etl::invoke(etl::declval(), etl::declval()...), etl::true_type{}); template static etl::false_type test(...); using type = decltype(test(0)); static ETL_CONSTANT bool value = type::value; }; //******************************************* // Result type of a valid invocation. template struct invoke_result_impl { template static auto test(int) -> decltype(etl::invoke(etl::declval(), etl::declval()...)); template static void test(...); using type = decltype(test(0)); }; //******************************************* // Result type of a valid invocation. template struct invoke_result_impl> { template static auto test(int) -> decltype(etl::invoke(etl::declval(), etl::declval()...)); template static void test(...); using type = decltype(test(0)); }; template using invoke_result_impl_t = typename invoke_result_impl::type; //******************************************* // Unwrap reference_wrapper to its underlying type T&, // forwarding to etl::unwrap_ref_decay for reference_wrapper detection. template >::value> struct unwrap_ref_callable { using type = TFunction; }; template struct unwrap_ref_callable { using type = etl::unwrap_ref_decay_t; }; template using unwrap_ref_callable_t = typename unwrap_ref_callable::type; //******************************************* // Map raw function type to pointer, and unwrap reference_wrapper // so that function_traits sees the actual callable type. template using effective_callable_t = etl::conditional_t< etl::is_function>::value, etl::add_pointer_t>, unwrap_ref_callable_t>; } // namespace private_invoke //**************************************************************************** /// invoke_result template struct invoke_result { using type = void; }; //******************************************* template struct invoke_result< TFunction, etl::void_t(), etl::declval()...))>, TArgs...> { private: using FC = private_invoke::effective_callable_t; public: using type = etl::conditional_t::value, private_invoke::invoke_result_impl_t, void>; }; //**************************************************************************** /// invoke_result> template struct invoke_result> { private: using FC = private_invoke::effective_callable_t; public: using type = etl::conditional_t::value, private_invoke::invoke_result_impl_t, void>; }; //******************************************* // Specialization to allow `etl::type_list<...>` as the second template // parameter. template struct invoke_result< TFunction, etl::void_t(), etl::declval()...))>, etl::type_list> { using type = decltype(etl::invoke(etl::declval(), etl::declval()...)); }; //******************************************* template using invoke_result_t = typename invoke_result::type; //**************************************************************************** /// is_invocable template struct is_invocable : etl::bool_constant< private_invoke::is_invocable_expr< private_invoke::effective_callable_t, TArgs...>::value> { }; //**************************************************************************** // Specialization to allow `etl::type_list<...>` as the second template // parameter. template struct is_invocable> : is_invocable { }; //**************************************************************************** // is_invocable_r template struct is_invocable_r : etl::conditional_t< etl::is_same::value, etl::is_invocable, etl::conditional_t< is_invocable::value, etl::bool_constant, TReturn>::value>, etl::false_type>> { }; //**************************************************************************** // Specialization to allow `etl::type_list<...>` as the second template // parameter. template struct is_invocable_r> : is_invocable_r { }; //**************************************************************************** // Specialization to allow `etl::type_list<...>` when there is a preceding // object argument. template struct is_invocable> : is_invocable { }; //**************************************************************************** // Specialization for is_invocable_r with a preceding object argument. template struct is_invocable_r> : is_invocable_r { }; #if ETL_USING_CPP17 //**************************************************************************** /// is_nothrow_invocable template struct is_nothrow_invocable : etl::bool_constant< etl::is_invocable::value && etl::function_traits< private_invoke::effective_callable_t>::is_noexcept> { }; //**************************************************************************** /// is_nothrow_invocable_r template struct is_nothrow_invocable_r : etl::bool_constant< etl::is_invocable_r::value && etl::function_traits< private_invoke::effective_callable_t>::is_noexcept && (etl::is_same::value || etl::is_nothrow_convertible, TReturn>::value)> { }; //**************************************************************************** /// is_nothrow_invocable_r> template struct is_nothrow_invocable_r> : etl::bool_constant< etl::is_invocable_r::value && etl::function_traits< private_invoke::effective_callable_t>::is_noexcept && (etl::is_same::value || etl::is_nothrow_convertible, TReturn>::value)> { }; //**************************************************************************** // Specialization to allow `etl::type_list<...>` when there is a preceding // object argument for the nothrow-with-return trait. template struct is_nothrow_invocable_r> : is_nothrow_invocable_r { }; #endif #if ETL_USING_CPP17 template inline constexpr bool is_invocable_v = is_invocable::value; template inline constexpr bool is_invocable_r_v = is_invocable_r::value; template inline constexpr bool is_nothrow_invocable_v = is_nothrow_invocable::value; template inline constexpr bool is_nothrow_invocable_r_v = is_nothrow_invocable_r::value; #endif } // namespace etl #endif // ETL_USING_CPP11 #endif // ETL_INVOKE_INCLUDED