argument_traits.hpp 2.5 KB

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