// Copyright 2015-2019 Hans Dembinski // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_HISTOGRAM_DETAIL_DETECT_HPP #define BOOST_HISTOGRAM_DETAIL_DETECT_HPP #include #include // mp_and, mp_or #include // mp_not #include // mp_first #include #include #include // forward declaration namespace boost { namespace variant2 { template class variant; } // namespace variant2 } // namespace boost namespace boost { namespace histogram { namespace detail { template using void_t = void; struct detect_base { template static T&& val(); template static T& ref(); template static T const& cref(); }; #define BOOST_HISTOGRAM_DETAIL_DETECT(name, cond) \ template \ struct name##_impl : detect_base { \ template \ static mp11::mp_true test(T& t, decltype(cond, 0)); \ template \ static mp11::mp_false test(T&, float); \ using type = decltype(test(ref(), 0)); \ }; \ template \ using name = typename name##_impl::type #define BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(name, cond) \ template \ struct name##_impl : detect_base { \ template \ static mp11::mp_true test(T& t, U& u, decltype(cond, 0)); \ template \ static mp11::mp_false test(T&, U&, float); \ using type = decltype(test(ref(), ref(), 0)); \ }; \ template \ using name = typename name##_impl::type // reset has overloads, trying to get pmf in this case always fails BOOST_HISTOGRAM_DETAIL_DETECT(has_method_reset, t.reset(0)); BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable, t[0]); BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(is_transform, (t.inverse(t.forward(u)))); BOOST_HISTOGRAM_DETAIL_DETECT(is_indexable_container, (t[0], t.size(), std::begin(t), std::end(t))); BOOST_HISTOGRAM_DETAIL_DETECT(is_vector_like, (t[0], t.size(), t.resize(0), std::begin(t), std::end(t))); BOOST_HISTOGRAM_DETAIL_DETECT(is_array_like, (t[0], t.size(), std::tuple_size::value, std::begin(t), std::end(t))); BOOST_HISTOGRAM_DETAIL_DETECT(is_map_like, ((typename T::key_type*)nullptr, (typename T::mapped_type*)nullptr, std::begin(t), std::end(t))); // ok: is_axis is false for axis::variant, because T::index is templated BOOST_HISTOGRAM_DETAIL_DETECT(is_axis, (t.size(), &T::index)); BOOST_HISTOGRAM_DETAIL_DETECT(is_iterable, (std::begin(t), std::end(t))); BOOST_HISTOGRAM_DETAIL_DETECT(is_iterator, (typename std::iterator_traits::iterator_category{})); BOOST_HISTOGRAM_DETAIL_DETECT(is_streamable, (std::declval() << t)); BOOST_HISTOGRAM_DETAIL_DETECT(has_operator_preincrement, ++t); BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_equal, (cref() == u)); BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_radd, (t += u)); BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rsub, (t -= u)); BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rmul, (t *= u)); BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_operator_rdiv, (t /= u)); BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(has_method_eq, (cref().operator==(u))); BOOST_HISTOGRAM_DETAIL_DETECT(has_threading_support, (T::has_threading_support)); // stronger form of std::is_convertible that works with explicit operator T and ctors BOOST_HISTOGRAM_DETAIL_DETECT_BINARY(is_explicitly_convertible, static_cast(t)); BOOST_HISTOGRAM_DETAIL_DETECT(is_complete, sizeof(T)); template using is_storage = mp11::mp_and, has_method_reset, has_threading_support>; template using is_adaptible = mp11::mp_and>, mp11::mp_or, is_array_like, is_map_like>>; template struct is_tuple_impl : std::false_type {}; template struct is_tuple_impl> : std::true_type {}; template using is_tuple = typename is_tuple_impl::type; template struct is_variant_impl : std::false_type {}; template struct is_variant_impl> : std::true_type {}; template using is_variant = typename is_variant_impl::type; template struct is_axis_variant_impl : std::false_type {}; template struct is_axis_variant_impl> : std::true_type {}; template using is_axis_variant = typename is_axis_variant_impl::type; template using is_any_axis = mp11::mp_or, is_axis_variant>; template using is_sequence_of_axis = mp11::mp_and, is_axis>>; template using is_sequence_of_axis_variant = mp11::mp_and, is_axis_variant>>; template using is_sequence_of_any_axis = mp11::mp_and, is_any_axis>>; // poor-mans concept checks template >::value>> struct requires_storage {}; template , class = std::enable_if_t<(is_storage<_>::value || is_adaptible<_>::value)>> struct requires_storage_or_adaptible {}; template >::value>> struct requires_iterator {}; template >>::value>> struct requires_iterable {}; template >::value>> struct requires_axis {}; template >::value>> struct requires_any_axis {}; template >::value>> struct requires_sequence_of_axis {}; template >::value>> struct requires_sequence_of_axis_variant {}; template >::value>> struct requires_sequence_of_any_axis {}; template >>::value>> struct requires_axes {}; template , U>::value>> struct requires_transform {}; } // namespace detail } // namespace histogram } // namespace boost #endif