detect.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2015-2019 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_DETAIL_DETECT_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_DETECT_HPP
  8. #include <boost/histogram/fwd.hpp>
  9. #include <boost/mp11/algorithm.hpp>
  10. #include <boost/mp11/function.hpp>
  11. #include <boost/mp11/utility.hpp>
  12. #include <iterator>
  13. #include <tuple>
  14. #include <type_traits>
  15. // forward declaration
  16. namespace boost {
  17. namespace variant2 {
  18. template <class...>
  19. class variant;
  20. } // namespace variant2
  21. } // namespace boost
  22. namespace boost {
  23. namespace histogram {
  24. namespace detail {
  25. #define BOOST_HISTOGRAM_DETAIL_DETECT(name, cond) \
  26. template <class T> \
  27. using name##_impl = decltype(cond); \
  28. template <class T> \
  29. using name = typename boost::mp11::mp_valid<name##_impl, T>::type
  30. #define BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(name, cond) \
  31. template <class T, class U> \
  32. using name##_impl = decltype(cond); \
  33. template <class T, class U = T> \
  34. using name = typename boost::mp11::mp_valid<name##_impl, T, U>::type
  35. // metadata has overloads, trying to get pmf in this case always fails
  36. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_metadata, (std::declval<T&>().metadata()));
  37. // resize has overloads, trying to get pmf in this case always fails
  38. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_resize, (std::declval<T&>().resize(0)));
  39. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_size, &T::size);
  40. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_clear, &T::clear);
  41. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_lower, &T::lower);
  42. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_value, &T::value);
  43. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_update, &T::update);
  44. // reset has overloads, trying to get pmf in this case always fails
  45. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_reset, (std::declval<T>().reset(0)));
  46. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_options, &T::options);
  47. BOOST_HISTOGRAM_DETAIL_DETECT(has_allocator, &T::get_allocator);
  48. BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable, (std::declval<T&>()[0]));
  49. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(
  50. is_transform,
  51. (std::declval<T&>().inverse(std::declval<T&>().forward(std::declval<U>()))));
  52. BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable_container, (std::declval<T>()[0], &T::size,
  53. std::begin(std::declval<T>()),
  54. std::end(std::declval<T>())));
  55. BOOST_HISTOGRAM_DETAIL_DETECT(is_vector_like,
  56. (std::declval<T>()[0], &T::size,
  57. std::declval<T>().resize(0), std::begin(std::declval<T>()),
  58. std::end(std::declval<T>())));
  59. BOOST_HISTOGRAM_DETAIL_DETECT(is_array_like,
  60. (std::declval<T>()[0], &T::size, std::tuple_size<T>::value,
  61. std::begin(std::declval<T>()),
  62. std::end(std::declval<T>())));
  63. BOOST_HISTOGRAM_DETAIL_DETECT(is_map_like, (std::declval<typename T::key_type>(),
  64. std::declval<typename T::mapped_type>(),
  65. std::begin(std::declval<T>()),
  66. std::end(std::declval<T>())));
  67. // ok: is_axis is false for axis::variant, because T::index is templated
  68. BOOST_HISTOGRAM_DETAIL_DETECT(is_axis, (&T::size, &T::index));
  69. BOOST_HISTOGRAM_DETAIL_DETECT(is_iterable, (std::begin(std::declval<T&>()),
  70. std::end(std::declval<T&>())));
  71. BOOST_HISTOGRAM_DETAIL_DETECT(is_iterator,
  72. (typename std::iterator_traits<T>::iterator_category()));
  73. BOOST_HISTOGRAM_DETAIL_DETECT(is_streamable,
  74. (std::declval<std::ostream&>() << std::declval<T&>()));
  75. BOOST_HISTOGRAM_DETAIL_DETECT(has_operator_preincrement, (++std::declval<T&>()));
  76. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_equal, (std::declval<const T&>() ==
  77. std::declval<const U>()));
  78. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_radd,
  79. (std::declval<T&>() += std::declval<U>()));
  80. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rsub,
  81. (std::declval<T&>() -= std::declval<U>()));
  82. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rmul,
  83. (std::declval<T&>() *= std::declval<U>()));
  84. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rdiv,
  85. (std::declval<T&>() /= std::declval<U>()));
  86. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(
  87. has_method_eq, (std::declval<const T>().operator==(std::declval<const U>())));
  88. BOOST_HISTOGRAM_DETAIL_DETECT(has_threading_support, (T::has_threading_support));
  89. template <class T>
  90. using is_storage = mp11::mp_and<is_indexable_container<T>, has_method_reset<T>,
  91. has_threading_support<T>>;
  92. template <class T>
  93. using is_adaptible =
  94. mp11::mp_and<mp11::mp_not<is_storage<T>>,
  95. mp11::mp_or<is_vector_like<T>, is_array_like<T>, is_map_like<T>>>;
  96. template <class T>
  97. struct is_tuple_impl : mp11::mp_false {};
  98. template <class... Ts>
  99. struct is_tuple_impl<std::tuple<Ts...>> : mp11::mp_true {};
  100. template <class T>
  101. using is_tuple = typename is_tuple_impl<T>::type;
  102. template <class T>
  103. struct is_variant_impl : mp11::mp_false {};
  104. template <class... Ts>
  105. struct is_variant_impl<boost::variant2::variant<Ts...>> : mp11::mp_true {};
  106. template <class T>
  107. using is_variant = typename is_variant_impl<T>::type;
  108. template <class T>
  109. struct is_axis_variant_impl : mp11::mp_false {};
  110. template <class... Ts>
  111. struct is_axis_variant_impl<axis::variant<Ts...>> : mp11::mp_true {};
  112. template <class T>
  113. using is_axis_variant = typename is_axis_variant_impl<T>::type;
  114. template <class T>
  115. using is_any_axis = mp11::mp_or<is_axis<T>, is_axis_variant<T>>;
  116. template <class T>
  117. using is_sequence_of_axis = mp11::mp_and<is_iterable<T>, is_axis<mp11::mp_first<T>>>;
  118. template <class T>
  119. using is_sequence_of_axis_variant =
  120. mp11::mp_and<is_iterable<T>, is_axis_variant<mp11::mp_first<T>>>;
  121. template <class T>
  122. using is_sequence_of_any_axis =
  123. mp11::mp_and<is_iterable<T>, is_any_axis<mp11::mp_first<T>>>;
  124. // poor-mans concept checks
  125. template <class T, class = std::enable_if_t<is_storage<std::decay_t<T>>::value>>
  126. struct requires_storage {};
  127. template <class T, class _ = std::decay_t<T>,
  128. class = std::enable_if_t<(is_storage<_>::value || is_adaptible<_>::value)>>
  129. struct requires_storage_or_adaptible {};
  130. template <class T, class = std::enable_if_t<is_iterator<std::decay_t<T>>::value>>
  131. struct requires_iterator {};
  132. template <class T, class = std::enable_if_t<
  133. is_iterable<std::remove_cv_t<std::remove_reference_t<T>>>::value>>
  134. struct requires_iterable {};
  135. template <class T, class = std::enable_if_t<is_axis<std::decay_t<T>>::value>>
  136. struct requires_axis {};
  137. template <class T, class = std::enable_if_t<is_any_axis<std::decay_t<T>>::value>>
  138. struct requires_any_axis {};
  139. template <class T, class = std::enable_if_t<is_sequence_of_axis<std::decay_t<T>>::value>>
  140. struct requires_sequence_of_axis {};
  141. template <class T,
  142. class = std::enable_if_t<is_sequence_of_axis_variant<std::decay_t<T>>::value>>
  143. struct requires_sequence_of_axis_variant {};
  144. template <class T,
  145. class = std::enable_if_t<is_sequence_of_any_axis<std::decay_t<T>>::value>>
  146. struct requires_sequence_of_any_axis {};
  147. template <class T,
  148. class = std::enable_if_t<is_any_axis<mp11::mp_first<std::decay_t<T>>>::value>>
  149. struct requires_axes {};
  150. template <class T, class U, class = std::enable_if_t<std::is_convertible<T, U>::value>>
  151. struct requires_convertible {};
  152. template <class T, class U,
  153. class = std::enable_if_t<is_transform<std::decay_t<T>, U>::value>>
  154. struct requires_transform {};
  155. } // namespace detail
  156. } // namespace histogram
  157. } // namespace boost
  158. #endif