]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/histogram/detail/detect.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / histogram / detail / detect.hpp
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
7 #ifndef BOOST_HISTOGRAM_DETAIL_DETECT_HPP
8 #define BOOST_HISTOGRAM_DETAIL_DETECT_HPP
9
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
14 #include <iterator>
15 #include <tuple>
16 #include <type_traits>
17
18 // forward declaration
19 namespace boost {
20 namespace variant2 {
21 template <class...>
22 class variant;
23 } // namespace variant2
24 } // namespace boost
25
26 namespace boost {
27 namespace histogram {
28 namespace detail {
29
30 template <class...>
31 using void_t = void;
32
33 struct detect_base {
34 template <class T>
35 static T&& val();
36 template <class T>
37 static T& ref();
38 template <class T>
39 static T const& cref();
40 };
41
42 #define BOOST_HISTOGRAM_DETAIL_DETECT(name, cond) \
43 template <class U> \
44 struct name##_impl : detect_base { \
45 template <class T> \
46 static mp11::mp_true test(T& t, decltype(cond, 0)); \
47 template <class T> \
48 static mp11::mp_false test(T&, float); \
49 using type = decltype(test<U>(ref<U>(), 0)); \
50 }; \
51 template <class T> \
52 using name = typename name##_impl<T>::type
53
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)); \
62 }; \
63 template <class T, class U = T> \
64 using name = typename name##_impl<T, U>::type
65
66 // reset has overloads, trying to get pmf in this case always fails
67 BOOST_HISTOGRAM_DETAIL_DETECT(has_method_reset, t.reset(0));
68
69 BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable, t[0]);
70
71 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(is_transform, (t.inverse(t.forward(u))));
72
73 BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable_container,
74 (t[0], t.size(), std::begin(t), std::end(t)));
75
76 BOOST_HISTOGRAM_DETAIL_DETECT(is_vector_like,
77 (t[0], t.size(), t.resize(0), std::begin(t), std::end(t)));
78
79 BOOST_HISTOGRAM_DETAIL_DETECT(is_array_like, (t[0], t.size(), std::tuple_size<T>::value,
80 std::begin(t), std::end(t)));
81
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)));
85
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));
88
89 BOOST_HISTOGRAM_DETAIL_DETECT(is_iterable, (std::begin(t), std::end(t)));
90
91 BOOST_HISTOGRAM_DETAIL_DETECT(is_iterator,
92 (typename std::iterator_traits<T>::iterator_category{}));
93
94 BOOST_HISTOGRAM_DETAIL_DETECT(is_streamable, (std::declval<std::ostream&>() << t));
95
96 BOOST_HISTOGRAM_DETAIL_DETECT(has_operator_preincrement, ++t);
97
98 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_equal, (cref<T>() == u));
99
100 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_radd, (t += u));
101
102 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rsub, (t -= u));
103
104 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rmul, (t *= u));
105
106 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rdiv, (t /= u));
107
108 BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_method_eq, (cref<T>().operator==(u)));
109
110 BOOST_HISTOGRAM_DETAIL_DETECT(has_threading_support, (T::has_threading_support));
111
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));
114
115 BOOST_HISTOGRAM_DETAIL_DETECT(is_complete, sizeof(T));
116
117 template <class T>
118 using is_storage = mp11::mp_and<is_indexable_container<T>, has_method_reset<T>,
119 has_threading_support<T>>;
120
121 template <class T>
122 using is_adaptible =
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>>>;
125
126 template <class T>
127 struct is_tuple_impl : std::false_type {};
128
129 template <class... Ts>
130 struct is_tuple_impl<std::tuple<Ts...>> : std::true_type {};
131
132 template <class T>
133 using is_tuple = typename is_tuple_impl<T>::type;
134
135 template <class T>
136 struct is_variant_impl : std::false_type {};
137
138 template <class... Ts>
139 struct is_variant_impl<boost::variant2::variant<Ts...>> : std::true_type {};
140
141 template <class T>
142 using is_variant = typename is_variant_impl<T>::type;
143
144 template <class T>
145 struct is_axis_variant_impl : std::false_type {};
146
147 template <class... Ts>
148 struct is_axis_variant_impl<axis::variant<Ts...>> : std::true_type {};
149
150 template <class T>
151 using is_axis_variant = typename is_axis_variant_impl<T>::type;
152
153 template <class T>
154 using is_any_axis = mp11::mp_or<is_axis<T>, is_axis_variant<T>>;
155
156 template <class T>
157 using is_sequence_of_axis = mp11::mp_and<is_iterable<T>, is_axis<mp11::mp_first<T>>>;
158
159 template <class T>
160 using is_sequence_of_axis_variant =
161 mp11::mp_and<is_iterable<T>, is_axis_variant<mp11::mp_first<T>>>;
162
163 template <class T>
164 using is_sequence_of_any_axis =
165 mp11::mp_and<is_iterable<T>, is_any_axis<mp11::mp_first<T>>>;
166
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 {};
170
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 {};
174
175 template <class T, class = std::enable_if_t<is_iterator<std::decay_t<T>>::value>>
176 struct requires_iterator {};
177
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 {};
181
182 template <class T, class = std::enable_if_t<is_axis<std::decay_t<T>>::value>>
183 struct requires_axis {};
184
185 template <class T, class = std::enable_if_t<is_any_axis<std::decay_t<T>>::value>>
186 struct requires_any_axis {};
187
188 template <class T, class = std::enable_if_t<is_sequence_of_axis<std::decay_t<T>>::value>>
189 struct requires_sequence_of_axis {};
190
191 template <class T,
192 class = std::enable_if_t<is_sequence_of_axis_variant<std::decay_t<T>>::value>>
193 struct requires_sequence_of_axis_variant {};
194
195 template <class T,
196 class = std::enable_if_t<is_sequence_of_any_axis<std::decay_t<T>>::value>>
197 struct requires_sequence_of_any_axis {};
198
199 template <class T,
200 class = std::enable_if_t<is_any_axis<mp11::mp_first<std::decay_t<T>>>::value>>
201 struct requires_axes {};
202
203 template <class T, class U,
204 class = std::enable_if_t<is_transform<std::decay_t<T>, U>::value>>
205 struct requires_transform {};
206
207 } // namespace detail
208 } // namespace histogram
209 } // namespace boost
210
211 #endif