1 // Copyright 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_ARGUMENT_TRAITS_HPP
8 #define BOOST_HISTOGRAM_DETAIL_ARGUMENT_TRAITS_HPP
10 #include <boost/histogram/fwd.hpp>
11 #include <boost/mp11/algorithm.hpp>
12 #include <boost/mp11/integral.hpp>
13 #include <boost/mp11/list.hpp>
21 struct is_weight_impl : mp11::mp_false {};
24 struct is_weight_impl<weight_type<T>> : mp11::mp_true {};
27 using is_weight = is_weight_impl<T>;
30 struct is_sample_impl : mp11::mp_false {};
33 struct is_sample_impl<sample_type<T>> : mp11::mp_true {};
36 using is_sample = is_sample_impl<T>;
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)>>>;
44 struct sample_args_impl<-1, L> {
45 using type = std::tuple<>;
48 template <std::size_t NArgs, std::size_t Start, int WeightPos, int SamplePos,
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;
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);
68 argument_traits_holder<(size_ - (weight_ < size_) - (sample_ < size_)),
69 (weight_ < size_ && sample_ < size_ &&
70 (weight_ + sample_ < 2)
72 : ((weight_ == 0 || sample_ == 0) ? 1 : 0)),
73 wpos_, spos_, typename sample_args_impl<spos_, list_>::type>;
76 template <class... Ts>
77 using argument_traits = typename argument_traits_impl<Ts...>::type;
80 } // namespace histogram