detect.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/function.hpp> // mp_and, mp_or
  10. #include <boost/mp11/integral.hpp> // mp_not
  11. #include <boost/mp11/list.hpp> // mp_first
  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 U> \
  27. struct name##_impl { \
  28. typedef char yes[1]; \
  29. typedef char no[2]; \
  30. template <class T> \
  31. static yes& test(T& t, decltype(cond, 0)); \
  32. template <class T> \
  33. static no& test(T&, float); \
  34. using type = \
  35. std::integral_constant<bool, (sizeof(test(std::declval<U&>(), 0)) == 1)>; \
  36. }; \
  37. template <class T> \
  38. using name = typename name##_impl<T>::type
  39. #define BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(name, cond) \
  40. template <class V, class W> \
  41. struct name##_impl { \
  42. typedef char yes[1]; \
  43. typedef char no[2]; \
  44. template <class T, class U> \
  45. static yes& test(decltype(cond, 0)); \
  46. template <class, class> \
  47. static no& test(float); \
  48. using type = std::integral_constant<bool, (sizeof(test<V, W>(0)) == 1)>; \
  49. }; \
  50. template <class T, class U = T> \
  51. using name = typename name##_impl<T, U>::type
  52. // reset has overloads, trying to get pmf in this case always fails
  53. BOOST_HISTOGRAM_DETAIL_DETECT(has_method_reset, t.reset(0));
  54. BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable, t[0]);
  55. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(
  56. is_transform,
  57. (std::declval<T&>().inverse(std::declval<T&>().forward(std::declval<U>()))));
  58. BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable_container,
  59. (t[0], t.size(), std::begin(t), std::end(t)));
  60. BOOST_HISTOGRAM_DETAIL_DETECT(is_vector_like,
  61. (t[0], t.size(), t.resize(0), std::begin(t), std::end(t)));
  62. BOOST_HISTOGRAM_DETAIL_DETECT(is_array_like, (t[0], t.size(), std::tuple_size<T>::value,
  63. std::begin(t), std::end(t)));
  64. BOOST_HISTOGRAM_DETAIL_DETECT(is_map_like, ((typename T::key_type*)nullptr,
  65. (typename T::mapped_type*)nullptr,
  66. std::begin(t), std::end(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(t), std::end(t)));
  70. BOOST_HISTOGRAM_DETAIL_DETECT(is_iterator,
  71. (typename std::iterator_traits<T>::iterator_category{}));
  72. BOOST_HISTOGRAM_DETAIL_DETECT(is_streamable, (std::declval<std::ostream&>() << t));
  73. BOOST_HISTOGRAM_DETAIL_DETECT(has_operator_preincrement, ++t);
  74. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_equal, (std::declval<const T&>() ==
  75. std::declval<const U&>()));
  76. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_radd,
  77. (std::declval<T&>() += std::declval<U>()));
  78. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rsub,
  79. (std::declval<T&>() -= std::declval<U>()));
  80. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rmul,
  81. (std::declval<T&>() *= std::declval<U>()));
  82. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rdiv,
  83. (std::declval<T&>() /= std::declval<U>()));
  84. BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(
  85. has_method_eq, (std::declval<const T&>().operator==(std::declval<const U&>())));
  86. BOOST_HISTOGRAM_DETAIL_DETECT(has_threading_support, (T::has_threading_support));
  87. template <class T>
  88. using is_storage = mp11::mp_and<is_indexable_container<T>, has_method_reset<T>,
  89. has_threading_support<T>>;
  90. template <class T>
  91. using is_adaptible =
  92. mp11::mp_and<mp11::mp_not<is_storage<T>>,
  93. mp11::mp_or<is_vector_like<T>, is_array_like<T>, is_map_like<T>>>;
  94. template <class T>
  95. struct is_tuple_impl : std::false_type {};
  96. template <class... Ts>
  97. struct is_tuple_impl<std::tuple<Ts...>> : std::true_type {};
  98. template <class T>
  99. using is_tuple = typename is_tuple_impl<T>::type;
  100. template <class T>
  101. struct is_variant_impl : std::false_type {};
  102. template <class... Ts>
  103. struct is_variant_impl<boost::variant2::variant<Ts...>> : std::true_type {};
  104. template <class T>
  105. using is_variant = typename is_variant_impl<T>::type;
  106. template <class T>
  107. struct is_axis_variant_impl : std::false_type {};
  108. template <class... Ts>
  109. struct is_axis_variant_impl<axis::variant<Ts...>> : std::true_type {};
  110. template <class T>
  111. using is_axis_variant = typename is_axis_variant_impl<T>::type;
  112. template <class T>
  113. using is_any_axis = mp11::mp_or<is_axis<T>, is_axis_variant<T>>;
  114. template <class T>
  115. using is_sequence_of_axis = mp11::mp_and<is_iterable<T>, is_axis<mp11::mp_first<T>>>;
  116. template <class T>
  117. using is_sequence_of_axis_variant =
  118. mp11::mp_and<is_iterable<T>, is_axis_variant<mp11::mp_first<T>>>;
  119. template <class T>
  120. using is_sequence_of_any_axis =
  121. mp11::mp_and<is_iterable<T>, is_any_axis<mp11::mp_first<T>>>;
  122. // poor-mans concept checks
  123. template <class T, class = std::enable_if_t<is_storage<std::decay_t<T>>::value>>
  124. struct requires_storage {};
  125. template <class T, class _ = std::decay_t<T>,
  126. class = std::enable_if_t<(is_storage<_>::value || is_adaptible<_>::value)>>
  127. struct requires_storage_or_adaptible {};
  128. template <class T, class = std::enable_if_t<is_iterator<std::decay_t<T>>::value>>
  129. struct requires_iterator {};
  130. template <class T, class = std::enable_if_t<
  131. is_iterable<std::remove_cv_t<std::remove_reference_t<T>>>::value>>
  132. struct requires_iterable {};
  133. template <class T, class = std::enable_if_t<is_axis<std::decay_t<T>>::value>>
  134. struct requires_axis {};
  135. template <class T, class = std::enable_if_t<is_any_axis<std::decay_t<T>>::value>>
  136. struct requires_any_axis {};
  137. template <class T, class = std::enable_if_t<is_sequence_of_axis<std::decay_t<T>>::value>>
  138. struct requires_sequence_of_axis {};
  139. template <class T,
  140. class = std::enable_if_t<is_sequence_of_axis_variant<std::decay_t<T>>::value>>
  141. struct requires_sequence_of_axis_variant {};
  142. template <class T,
  143. class = std::enable_if_t<is_sequence_of_any_axis<std::decay_t<T>>::value>>
  144. struct requires_sequence_of_any_axis {};
  145. template <class T,
  146. class = std::enable_if_t<is_any_axis<mp11::mp_first<std::decay_t<T>>>::value>>
  147. struct requires_axes {};
  148. template <class T, class U,
  149. class = std::enable_if_t<is_transform<std::decay_t<T>, U>::value>>
  150. struct requires_transform {};
  151. } // namespace detail
  152. } // namespace histogram
  153. } // namespace boost
  154. #endif