utility.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. @Copyright Barrett Adair 2015-2017
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  5. */
  6. #ifndef BOOST_CLBL_TRTS_DETAIL_UTILITY_HPP
  7. #define BOOST_CLBL_TRTS_DETAIL_UTILITY_HPP
  8. #include <boost/callable_traits/detail/config.hpp>
  9. #include <boost/callable_traits/detail/sfinae_errors.hpp>
  10. #include <boost/callable_traits/detail/qualifier_flags.hpp>
  11. namespace boost { namespace callable_traits { namespace detail {
  12. struct cdecl_tag{};
  13. struct stdcall_tag{};
  14. struct fastcall_tag{};
  15. struct pascal_tag{};
  16. struct invalid_type { invalid_type() = delete; };
  17. struct reference_error { reference_error() = delete; };
  18. template<typename T>
  19. using error_type = typename std::conditional<
  20. std::is_reference<T>::value, reference_error, invalid_type>::type;
  21. #ifdef BOOST_CLBL_TRTS_DISABLE_ABOMINABLE_FUNCTIONS
  22. struct abominable_functions_not_supported_on_this_compiler{};
  23. #endif
  24. // used to convey "this type doesn't matter" in code
  25. struct dummy {};
  26. // used as return type in failed SFINAE tests
  27. struct substitution_failure : std::false_type{};
  28. template<bool Value>
  29. using bool_type = std::integral_constant<bool, Value>;
  30. // shorthand for std::tuple_element
  31. template<std::size_t I, typename Tup>
  32. using at = typename std::tuple_element<I, Tup>::type;
  33. template<typename T, typename Class>
  34. using add_member_pointer = T Class::*;
  35. template<typename L, typename R, typename ErrorType>
  36. using fail_when_same = fail_if<std::is_same<L, R>::value, ErrorType>;
  37. template<typename T, typename ErrorType,
  38. typename U = typename std::remove_reference<T>::type>
  39. using try_but_fail_if_invalid = sfinae_try<T,
  40. fail_when_same<U, invalid_type, ErrorType>,
  41. fail_when_same<U, reference_error,
  42. reference_type_not_supported_by_this_metafunction>>;
  43. template<typename T, typename ErrorType,
  44. typename U = typename std::remove_reference<T>::type,
  45. bool is_reference_error = std::is_same<reference_error, U>::value>
  46. using fail_if_invalid = fail_if<
  47. std::is_same<U, invalid_type>::value || is_reference_error,
  48. typename std::conditional<is_reference_error,
  49. reference_type_not_supported_by_this_metafunction, ErrorType>::type>;
  50. template<typename T, typename Fallback>
  51. using fallback_if_invalid = typename std::conditional<
  52. std::is_same<T, invalid_type>::value, Fallback, T>::type;
  53. template<typename T, template<class> class Alias, typename U = Alias<T>>
  54. struct force_sfinae {
  55. using type = U;
  56. };
  57. template<typename T>
  58. using shallow_decay = typename std::remove_cv<
  59. typename std::remove_reference<T>::type>::type;
  60. template<typename T>
  61. struct is_reference_wrapper_t {
  62. using type = std::false_type;
  63. };
  64. template<typename T>
  65. struct is_reference_wrapper_t<std::reference_wrapper<T>> {
  66. using type = std::true_type;
  67. };
  68. template<typename T>
  69. using is_reference_wrapper =
  70. typename is_reference_wrapper_t<shallow_decay<T>>::type;
  71. template<typename T, typename = std::true_type>
  72. struct unwrap_reference_t {
  73. using type = T;
  74. };
  75. template<typename T>
  76. struct unwrap_reference_t<T, is_reference_wrapper<T>> {
  77. using type = decltype(std::declval<T>().get());
  78. };
  79. // removes std::reference_wrapper
  80. template<typename T>
  81. using unwrap_reference = typename unwrap_reference_t<T>::type;
  82. }}} // namespace boost::callable_traits::detail
  83. #endif // #ifndef BOOST_CLBL_TRTS_DETAIL_UTILITY_HPP