function_traits.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #ifndef RTTR_FUNCTION_TRAITS_H_
  28. #define RTTR_FUNCTION_TRAITS_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/misc/misc_type_traits.h"
  31. #include "rttr/detail/misc/std_type_traits.h"
  32. #include <type_traits>
  33. #include <functional>
  34. #include <tuple>
  35. namespace rttr
  36. {
  37. namespace detail
  38. {
  39. /////////////////////////////////////////////////////////////////////////////////////
  40. template<typename T>
  41. struct is_function_ptr : std::integral_constant<bool, std::is_pointer<T>::value &&
  42. std::is_function<::rttr::detail::remove_pointer_t<T>>::value>
  43. {
  44. };
  45. /////////////////////////////////////////////////////////////////////////////////////
  46. // snippet provided by K-ballo
  47. struct helper
  48. {
  49. void operator()(...);
  50. };
  51. template <typename T>
  52. struct helper_composed: T, helper
  53. {};
  54. template <void (helper::*) (...)>
  55. struct member_function_holder
  56. {};
  57. template <typename T, typename Ambiguous = member_function_holder<&helper::operator()> >
  58. struct is_functor_impl : std::true_type
  59. {};
  60. template <typename T>
  61. struct is_functor_impl<T, member_function_holder<&helper_composed<T>::operator()> > : std::false_type
  62. {};
  63. /*!
  64. * \brief Returns true whether the given type T is a functor.
  65. * i.e. func(...); That can be free function, lambdas or function objects.
  66. */
  67. template <typename T>
  68. struct is_functor : conditional_t<std::is_class<T>::value,
  69. is_functor_impl<T>,
  70. std::false_type>
  71. {};
  72. template<typename R, typename... Args>
  73. struct is_functor<R (*)(Args...)> : std::true_type {};
  74. template<typename R, typename... Args>
  75. struct is_functor<R (&)(Args...)> : std::true_type {};
  76. #ifndef RTTR_NO_CXX17_NOEXCEPT_FUNC_TYPE
  77. template<typename R, typename... Args>
  78. struct is_functor<R (*)(Args...) noexcept> : std::true_type {};
  79. template<typename R, typename... Args>
  80. struct is_functor<R (&)(Args...) noexcept> : std::true_type {};
  81. #endif
  82. /////////////////////////////////////////////////////////////////////////////////////
  83. /////////////////////////////////////////////////////////////////////////////////////
  84. template <typename T>
  85. struct function_traits : function_traits< decltype(&T::operator()) > {};
  86. template<typename R, typename... Args>
  87. struct function_traits<R (Args...)>
  88. {
  89. static RTTR_CONSTEXPR_OR_CONST size_t arg_count = sizeof...(Args);
  90. using return_type = R;
  91. using arg_types = std::tuple<Args...>;
  92. };
  93. template<typename R, typename... Args>
  94. struct function_traits<R (*)(Args...)> : function_traits<R (Args...)> { };
  95. template<typename R, typename... Args>
  96. struct function_traits<R (&)(Args...)> : function_traits<R (Args...)> { };
  97. template<typename R, typename C, typename... Args>
  98. struct function_traits<R (C::*)(Args...)> : function_traits<R (Args...)> { using class_type = C; };
  99. template<typename R, typename C, typename... Args>
  100. struct function_traits<R (C::*)(Args...) const> : function_traits<R (Args...)> { using class_type = C; };
  101. template<typename R, typename C, typename... Args>
  102. struct function_traits<R (C::*)(Args...) volatile> : function_traits<R (Args...)> { using class_type = C; };
  103. template<typename R, typename C, typename... Args>
  104. struct function_traits<R (C::*)(Args...) const volatile> : function_traits<R (Args...)> {using class_type = C; };
  105. #ifndef RTTR_NO_CXX17_NOEXCEPT_FUNC_TYPE
  106. template<typename R, typename... Args>
  107. struct function_traits<R (*)(Args...) noexcept> : function_traits<R (Args...)> { };
  108. template<typename R, typename... Args>
  109. struct function_traits<R (&)(Args...) noexcept> : function_traits<R (Args...)> { };
  110. template<typename R, typename C, typename... Args>
  111. struct function_traits<R (C::*)(Args...) noexcept> : function_traits<R (Args...)> { using class_type = C; };
  112. template<typename R, typename C, typename... Args>
  113. struct function_traits<R (C::*)(Args...) const noexcept> : function_traits<R (Args...)> { using class_type = C; };
  114. template<typename R, typename C, typename... Args>
  115. struct function_traits<R (C::*)(Args...) volatile noexcept> : function_traits<R (Args...)> { using class_type = C; };
  116. template<typename R, typename C, typename... Args>
  117. struct function_traits<R (C::*)(Args...) const volatile noexcept> : function_traits<R (Args...)> {using class_type = C; };
  118. #endif
  119. template<typename T>
  120. struct function_traits<std::function<T>> : function_traits<T> {};
  121. /////////////////////////////////////////////////////////////////////////////////////
  122. // use it like e.g:
  123. // param_types<F, 0>::type
  124. template<typename F, size_t Index>
  125. struct param_types
  126. {
  127. using type = typename std::tuple_element<Index, typename function_traits<F>::arg_types>::type;
  128. };
  129. template<typename F, size_t Index>
  130. using param_types_t = typename param_types<F, Index>::type;
  131. /////////////////////////////////////////////////////////////////////////////////////
  132. template<typename F>
  133. struct is_void_func : conditional_t< std::is_same<typename function_traits<F>::return_type, void>::value,
  134. std::true_type,
  135. std::false_type
  136. >
  137. {
  138. };
  139. /////////////////////////////////////////////////////////////////////////////////////
  140. // returns an std::true_type, when the given type F is a function type; otherwise an std::false_type.
  141. template<typename F>
  142. using is_function = std::integral_constant<bool, std::is_member_function_pointer<F>::value ||
  143. std::is_function<F>::value ||
  144. is_functor<F>::value
  145. >;
  146. /////////////////////////////////////////////////////////////////////////////////////
  147. } // end namespace detail
  148. } // end namespace rttr
  149. #endif // RTTR_FUNCTION_TRAITS_H_