// https://github.com/kunitoki/LuaBridge3 // Copyright 2020, Lucio Asnaghi // Copyright 2020, Dmitry Tarakanov // Copyright 2019, George Tokmaji // Copyright 2012, Vinnie Falco // SPDX-License-Identifier: MIT #pragma once #include "Config.h" #include #include namespace luabridge { namespace detail { //================================================================================================= /** * @brief Invokes undefined behavior when an unreachable part of the code is reached. * * An implementation may use this to optimize impossible code branches away (typically, in optimized builds) or to trap them to prevent * further execution (typically, in debug builds). */ [[noreturn]] inline void unreachable() { #if defined(__GNUC__) // GCC, Clang, ICC __builtin_unreachable(); #elif defined(_MSC_VER) // MSVC __assume(false); #endif } //================================================================================================= /** * @brief Provides the member typedef type which is the type referred to by T with its topmost cv-qualifiers removed. */ template< class T > struct remove_cvref { typedef std::remove_cv_t> type; }; template using remove_cvref_t = typename remove_cvref::type; //================================================================================================= /** * @brief Generic function traits. * * @tparam IsMember True if the function is a member function pointer. * @tparam IsConst True if the function is const. * @tparam R Return type of the function. * @tparam Args Arguments types as variadic parameter pack. */ template struct function_traits_base { using result_type = R; using argument_types = std::tuple; static constexpr auto arity = sizeof...(Args); static constexpr auto is_member = IsMember; static constexpr auto is_const = IsConst; }; template struct function_traits_impl; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; #if defined(_MSC_VER) && defined(_M_IX86) // Windows: WINAPI (a.k.a. __stdcall) function pointers (32bit only). template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; template struct function_traits_impl : function_traits_base { }; #endif template struct functor_traits_impl : function_traits_impl { }; //================================================================================================= /** * @brief Traits class for callable objects (e.g. function pointers, lambdas) * * @tparam F Callable object. */ template struct function_traits : std::conditional_t, detail::functor_traits_impl, detail::function_traits_impl> { }; //================================================================================================= /** * @brief Deduces the argument type of a callble object or void in case it has no argument. * * @tparam I Argument index. * @tparam F Callable object. */ template struct function_argument_or_void { using type = void; }; template struct function_argument_or_void::argument_types>>> { using type = std::tuple_element_t::argument_types>; }; template using function_argument_or_void_t = typename function_argument_or_void::type; //================================================================================================= /** * @brief Deduces the return type of a callble object. * * @tparam F Callable object. */ template using function_result_t = typename function_traits::result_type; /** * @brief Deduces the argument type of a callble object. * * @tparam I Argument index. * @tparam F Callable object. */ template using function_argument_t = std::tuple_element_t::argument_types>; /** * @brief Deduces the arguments type of a callble object. * * @tparam F Callable object. */ template using function_arguments_t = typename function_traits::argument_types; /** * @brief An integral constant expression that gives the number of arguments accepted by the callable object. * * @tparam F Callable object. */ template static constexpr std::size_t function_arity_v = function_traits::arity; /** * @brief An boolean constant expression that checks if the callable object is a member function. * * @tparam F Callable object. */ template static constexpr bool function_is_member_v = function_traits::is_member; /** * @brief An boolean constant expression that checks if the callable object is const. * * @tparam F Callable object. */ template static constexpr bool function_is_const_v = function_traits::is_const; //================================================================================================= /** * @brief Detect if we T is a callable object. * * @tparam T Potentially callable object. */ template struct is_callable { static constexpr bool value = false; }; template struct is_callable> { static constexpr bool value = true; }; template struct is_callable && std::is_function_v>>> { static constexpr bool value = true; }; template struct is_callable>> { static constexpr bool value = true; }; template inline static constexpr bool is_callable_v = is_callable::value; //================================================================================================= /** * @brief Detect if we T is a const member function pointer. * * @tparam T Potentially const member function pointer. */ template struct is_const_member_function_pointer { static constexpr bool value = false; }; template struct is_const_member_function_pointer { static constexpr bool value = false; }; template struct is_const_member_function_pointer { static constexpr bool value = true; }; template struct is_const_member_function_pointer { static constexpr bool value = false; }; template struct is_const_member_function_pointer { static constexpr bool value = true; }; template inline static constexpr bool is_const_member_function_pointer_v = is_const_member_function_pointer::value; //================================================================================================= /** * @brief Detect if T is a lua cfunction pointer. * * @tparam T Potentially lua cfunction pointer. */ template struct is_cfunction_pointer { static constexpr bool value = false; }; template <> struct is_cfunction_pointer { static constexpr bool value = true; }; template inline static constexpr bool is_cfunction_pointer_v = is_cfunction_pointer::value; //================================================================================================= /** * @brief Detect if T is a member lua cfunction pointer. * * @tparam T Potentially member lua cfunction pointer. */ template struct is_member_cfunction_pointer { static constexpr bool value = false; }; template struct is_member_cfunction_pointer { static constexpr bool value = true; }; template struct is_member_cfunction_pointer { static constexpr bool value = true; }; template inline static constexpr bool is_member_cfunction_pointer_v = is_member_cfunction_pointer::value; /** * @brief Detect if T is a const member lua cfunction pointer. * * @tparam T Potentially const member lua cfunction pointer. */ template struct is_const_member_cfunction_pointer { static constexpr bool value = false; }; template struct is_const_member_cfunction_pointer { static constexpr bool value = false; }; template struct is_const_member_cfunction_pointer { static constexpr bool value = true; }; template inline static constexpr bool is_const_member_cfunction_pointer_v = is_const_member_cfunction_pointer::value; //================================================================================================= /** * @brief Detect if T is a member or non member lua cfunction pointer. * * @tparam T Potentially member or non member lua cfunction pointer. */ template inline static constexpr bool is_any_cfunction_pointer_v = is_cfunction_pointer_v || is_member_cfunction_pointer_v; //================================================================================================= /** * @brief A constexpr check for proxy_member functions. * * @tparam T Type where the callable should be able to operate. * @tparam F Callable object. */ template inline static constexpr bool is_proxy_member_function_v = !std::is_member_function_pointer_v && std::is_same_v>>>; template inline static constexpr bool is_const_proxy_function_v = is_proxy_member_function_v && std::is_const_v>>>; //================================================================================================= /** * @brief An integral constant expression that gives the number of arguments excluding one type (usually used with lua_State*) accepted by the callable object. * * @tparam F Callable object. */ template struct function_arity_excluding { }; template < class... Ts, class ExclusionType> struct function_arity_excluding, ExclusionType> : std::integral_constant, ExclusionType> ? 0 : 1))> { }; template inline static constexpr std::size_t function_arity_excluding_v = function_arity_excluding, ExclusionType>::value; /** * @brief An integral constant expression that gives the number of arguments excluding one type (usually used with lua_State*) accepted by the callable object. * * @tparam F Callable object. */ template struct member_function_arity_excluding { }; template struct member_function_arity_excluding, ExclusionType, std::enable_if_t>> : std::integral_constant, ExclusionType> ? 0 : 1))> { }; template struct member_function_arity_excluding, ExclusionType, std::enable_if_t>> : std::integral_constant, ExclusionType> ? 0 : 1)) - 1> { }; template inline static constexpr std::size_t member_function_arity_excluding_v = member_function_arity_excluding, ExclusionType>::value; //================================================================================================= /** * @brief Detectors for const and non const functions in packs and counting them. */ template static constexpr bool is_const_function = detail::is_const_member_function_pointer_v || (detail::function_arity_v > 0 && detail::is_const_proxy_function_v); template inline static constexpr std::size_t const_functions_count = (0 + ... + (is_const_function ? 1 : 0)); template inline static constexpr std::size_t non_const_functions_count = (0 + ... + (is_const_function ? 0 : 1)); //================================================================================================= /** * @brief Simple make_tuple alternative that doesn't decay the types. * * @tparam Types Argument types that will compose the tuple. */ template constexpr auto tupleize(Types&&... types) { return std::tuple(std::forward(types)...); } //================================================================================================= /** * @brief Remove first type from tuple. */ template struct remove_first_type { }; template struct remove_first_type> { using type = std::tuple; }; template using remove_first_type_t = typename remove_first_type::type; } // namespace detail } // namespace luabridge