/* * Copyright (c) Meta Platforms, Inc. and affiliates. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #if __cplusplus < 201703L #error "This header requires C++17" #endif #include #include #include #include #include namespace executorch { namespace extension { // This extension has a lot of generic internal names like "size"; use a unique // internal namespace to avoid conflicts with other extensions. namespace kernel_util_internal { // Check if a given type is a function template struct is_function_type : std::false_type {}; template struct is_function_type : std::true_type {}; template using is_function_type_t = typename is_function_type::type; // A compile-time wrapper around a function pointer template struct CompileTimeFunctionPointer final { static_assert( is_function_type::value, "EXECUTORCH_FN can only wrap function types."); using FuncType = FuncType_; static constexpr FuncType* func_ptr() { return func_ptr_; } }; // Check if a given type is a compile-time function pointer template struct is_compile_time_function_pointer : std::false_type {}; template struct is_compile_time_function_pointer< CompileTimeFunctionPointer> : std::true_type {}; #define EXECUTORCH_FN_TYPE(func) \ ::executorch::extension::kernel_util_internal::CompileTimeFunctionPointer< \ std::remove_pointer_t>, \ func> #define EXECUTORCH_FN(func) EXECUTORCH_FN_TYPE(func)() /** * strip_class: helper to remove the class type from pointers to `operator()`. */ template struct strip_class {}; template struct strip_class { using type = Result(Args...); }; template struct strip_class { using type = Result(Args...); }; template using strip_class_t = typename strip_class::type; /** * Access information about result type or arguments from a function type. * Example: * using A = function_traits::return_type // A == int * using A = function_traits::parameter_types::tuple_type * // A == tuple */ template struct function_traits { static_assert( !std::is_same::value, "In function_traits, Func must be a plain function type."); }; template struct function_traits { using func_type = Result(Args...); using return_type = Result; using parameter_types = typelist; static constexpr auto number_of_parameters = sizeof...(Args); }; /** * infer_function_traits: creates a `function_traits` type for a simple * function (pointer) or functor (lambda/struct). Currently does not support * class methods. */ template struct infer_function_traits { using type = function_traits>; }; template struct infer_function_traits { using type = function_traits; }; template struct infer_function_traits { using type = function_traits; }; template using infer_function_traits_t = typename infer_function_traits::type; } // namespace kernel_util_internal } // namespace extension } // namespace executorch