1 // Copyright 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_ARGUMENT_TRAITS_HPP 8 #define BOOST_HISTOGRAM_DETAIL_ARGUMENT_TRAITS_HPP 9 10 #include <boost/histogram/fwd.hpp> 11 #include <boost/mp11/algorithm.hpp> 12 #include <boost/mp11/integral.hpp> 13 #include <boost/mp11/list.hpp> 14 #include <tuple> 15 16 namespace boost { 17 namespace histogram { 18 namespace detail { 19 20 template <class T> 21 struct is_weight_impl : mp11::mp_false {}; 22 23 template <class T> 24 struct is_weight_impl<weight_type<T>> : mp11::mp_true {}; 25 26 template <class T> 27 using is_weight = is_weight_impl<T>; 28 29 template <class T> 30 struct is_sample_impl : mp11::mp_false {}; 31 32 template <class T> 33 struct is_sample_impl<sample_type<T>> : mp11::mp_true {}; 34 35 template <class T> 36 using is_sample = is_sample_impl<T>; 37 38 template <int Idx, class L> 39 struct sample_args_impl { 40 using type = mp11::mp_first<std::decay_t<mp11::mp_at_c<L, (Idx >= 0 ? Idx : 0)>>>; 41 }; 42 43 template <class L> 44 struct sample_args_impl<-1, L> { 45 using type = std::tuple<>; 46 }; 47 48 template <std::size_t NArgs, std::size_t Start, int WeightPos, int SamplePos, 49 class SampleArgs> 50 struct argument_traits_holder { 51 using nargs = mp11::mp_size_t<NArgs>; 52 using start = mp11::mp_size_t<Start>; 53 using wpos = mp11::mp_int<WeightPos>; 54 using spos = mp11::mp_int<SamplePos>; 55 using sargs = SampleArgs; 56 }; 57 58 template <class... Ts> 59 struct argument_traits_impl { 60 using list_ = mp11::mp_list<Ts...>; 61 static constexpr std::size_t size_ = sizeof...(Ts); 62 static constexpr std::size_t weight_ = mp11::mp_find_if<list_, is_weight>::value; 63 static constexpr std::size_t sample_ = mp11::mp_find_if<list_, is_sample>::value; 64 static constexpr int spos_ = (sample_ < size_ ? static_cast<int>(sample_) : -1); 65 static constexpr int wpos_ = (weight_ < size_ ? static_cast<int>(weight_) : -1); 66 67 using type = 68 argument_traits_holder<(size_ - (weight_ < size_) - (sample_ < size_)), 69 (weight_ < size_ && sample_ < size_ && 70 (weight_ + sample_ < 2) 71 ? 2 72 : ((weight_ == 0 || sample_ == 0) ? 1 : 0)), 73 wpos_, spos_, typename sample_args_impl<spos_, list_>::type>; 74 }; 75 76 template <class... Ts> 77 using argument_traits = typename argument_traits_impl<Ts...>::type; 78 79 } // namespace detail 80 } // namespace histogram 81 } // namespace boost 82 83 #endif 84