1 // Copyright 2015-2019 Hans Dembinski
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)
7 #ifndef BOOST_HISTOGRAM_DETAIL_DETECT_HPP
8 #define BOOST_HISTOGRAM_DETAIL_DETECT_HPP
10 #include <boost/histogram/fwd.hpp>
11 #include <boost/mp11/function.hpp> // mp_and, mp_or
12 #include <boost/mp11/integral.hpp> // mp_not
13 #include <boost/mp11/list.hpp> // mp_first
16 #include <type_traits>
18 // forward declaration
23 } // namespace variant2
39 static T const& cref();
42 #define BOOST_HISTOGRAM_DETAIL_DETECT(name, cond) \
44 struct name##_impl : detect_base { \
46 static mp11::mp_true test(T& t, decltype(cond, 0)); \
48 static mp11::mp_false test(T&, float); \
49 using type = decltype(test<U>(ref<U>(), 0)); \
52 using name = typename name##_impl<T>::type
54 #define BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(name, cond) \
55 template <class V, class W> \
56 struct name##_impl : detect_base { \
57 template <class T, class U> \
58 static mp11::mp_true test(T& t, U& u, decltype(cond, 0)); \
59 template <class T, class U> \
60 static mp11::mp_false test(T&, U&, float); \
61 using type = decltype(test<V, W>(ref<V>(), ref<W>(), 0)); \
63 template <class T, class U = T> \
64 using name = typename name##_impl<T, U>::type
66 // reset has overloads, trying to get pmf in this case always fails
67 BOOST_HISTOGRAM_DETAIL_DETECT(has_method_reset, t.reset(0));
69 BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable, t[0]);
71 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(is_transform, (t.inverse(t.forward(u))));
73 BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable_container,
74 (t[0], t.size(), std::begin(t), std::end(t)));
76 BOOST_HISTOGRAM_DETAIL_DETECT(is_vector_like,
77 (t[0], t.size(), t.resize(0), std::begin(t), std::end(t)));
79 BOOST_HISTOGRAM_DETAIL_DETECT(is_array_like, (t[0], t.size(), std::tuple_size<T>::value,
80 std::begin(t), std::end(t)));
82 BOOST_HISTOGRAM_DETAIL_DETECT(is_map_like, ((typename T::key_type*)nullptr,
83 (typename T::mapped_type*)nullptr,
84 std::begin(t), std::end(t)));
86 // ok: is_axis is false for axis::variant, because T::index is templated
87 BOOST_HISTOGRAM_DETAIL_DETECT(is_axis, (t.size(), &T::index));
89 BOOST_HISTOGRAM_DETAIL_DETECT(is_iterable, (std::begin(t), std::end(t)));
91 BOOST_HISTOGRAM_DETAIL_DETECT(is_iterator,
92 (typename std::iterator_traits<T>::iterator_category{}));
94 BOOST_HISTOGRAM_DETAIL_DETECT(is_streamable, (std::declval<std::ostream&>() << t));
96 BOOST_HISTOGRAM_DETAIL_DETECT(has_operator_preincrement, ++t);
98 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_equal, (cref<T>() == u));
100 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_radd, (t += u));
102 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rsub, (t -= u));
104 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rmul, (t *= u));
106 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rdiv, (t /= u));
108 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_method_eq, (cref<T>().operator==(u)));
110 BOOST_HISTOGRAM_DETAIL_DETECT(has_threading_support, (T::has_threading_support));
112 // stronger form of std::is_convertible that works with explicit operator T and ctors
113 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(is_explicitly_convertible, static_cast<U>(t));
115 BOOST_HISTOGRAM_DETAIL_DETECT(is_complete, sizeof(T));
118 using is_storage = mp11::mp_and<is_indexable_container<T>, has_method_reset<T>,
119 has_threading_support<T>>;
123 mp11::mp_and<mp11::mp_not<is_storage<T>>,
124 mp11::mp_or<is_vector_like<T>, is_array_like<T>, is_map_like<T>>>;
127 struct is_tuple_impl : std::false_type {};
129 template <class... Ts>
130 struct is_tuple_impl<std::tuple<Ts...>> : std::true_type {};
133 using is_tuple = typename is_tuple_impl<T>::type;
136 struct is_variant_impl : std::false_type {};
138 template <class... Ts>
139 struct is_variant_impl<boost::variant2::variant<Ts...>> : std::true_type {};
142 using is_variant = typename is_variant_impl<T>::type;
145 struct is_axis_variant_impl : std::false_type {};
147 template <class... Ts>
148 struct is_axis_variant_impl<axis::variant<Ts...>> : std::true_type {};
151 using is_axis_variant = typename is_axis_variant_impl<T>::type;
154 using is_any_axis = mp11::mp_or<is_axis<T>, is_axis_variant<T>>;
157 using is_sequence_of_axis = mp11::mp_and<is_iterable<T>, is_axis<mp11::mp_first<T>>>;
160 using is_sequence_of_axis_variant =
161 mp11::mp_and<is_iterable<T>, is_axis_variant<mp11::mp_first<T>>>;
164 using is_sequence_of_any_axis =
165 mp11::mp_and<is_iterable<T>, is_any_axis<mp11::mp_first<T>>>;
167 // poor-mans concept checks
168 template <class T, class = std::enable_if_t<is_storage<std::decay_t<T>>::value>>
169 struct requires_storage {};
171 template <class T, class _ = std::decay_t<T>,
172 class = std::enable_if_t<(is_storage<_>::value || is_adaptible<_>::value)>>
173 struct requires_storage_or_adaptible {};
175 template <class T, class = std::enable_if_t<is_iterator<std::decay_t<T>>::value>>
176 struct requires_iterator {};
178 template <class T, class = std::enable_if_t<
179 is_iterable<std::remove_cv_t<std::remove_reference_t<T>>>::value>>
180 struct requires_iterable {};
182 template <class T, class = std::enable_if_t<is_axis<std::decay_t<T>>::value>>
183 struct requires_axis {};
185 template <class T, class = std::enable_if_t<is_any_axis<std::decay_t<T>>::value>>
186 struct requires_any_axis {};
188 template <class T, class = std::enable_if_t<is_sequence_of_axis<std::decay_t<T>>::value>>
189 struct requires_sequence_of_axis {};
192 class = std::enable_if_t<is_sequence_of_axis_variant<std::decay_t<T>>::value>>
193 struct requires_sequence_of_axis_variant {};
196 class = std::enable_if_t<is_sequence_of_any_axis<std::decay_t<T>>::value>>
197 struct requires_sequence_of_any_axis {};
200 class = std::enable_if_t<is_any_axis<mp11::mp_first<std::decay_t<T>>>::value>>
201 struct requires_axes {};
203 template <class T, class U,
204 class = std::enable_if_t<is_transform<std::decay_t<T>, U>::value>>
205 struct requires_transform {};
207 } // namespace detail
208 } // namespace histogram