option.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef BOOST_HISTOGRAM_AXIS_OPTION_HPP
  7. #define BOOST_HISTOGRAM_AXIS_OPTION_HPP
  8. #include <type_traits>
  9. /**
  10. \file option.hpp Options for builtin axis types.
  11. Options `circular` and `growth` are mutually exclusive.
  12. Options `circular` and `underflow` are mutually exclusive.
  13. */
  14. namespace boost {
  15. namespace histogram {
  16. namespace axis {
  17. namespace option {
  18. /// Holder of axis options.
  19. template <unsigned Bits>
  20. struct bitset : std::integral_constant<unsigned, Bits> {
  21. /// Returns true if all option flags in the argument are set and false otherwise.
  22. template <unsigned B>
  23. static constexpr auto test(bitset<B>) {
  24. // B + 0 needed to avoid false positive -Wtautological-compare in gcc-6
  25. return std::integral_constant<bool, static_cast<bool>((Bits & B) == (B + 0))>{};
  26. }
  27. };
  28. /// Set union of the axis option arguments.
  29. template <unsigned B1, unsigned B2>
  30. constexpr auto operator|(bitset<B1>, bitset<B2>) {
  31. return bitset<(B1 | B2)>{};
  32. }
  33. /// Set intersection of the option arguments.
  34. template <unsigned B1, unsigned B2>
  35. constexpr auto operator&(bitset<B1>, bitset<B2>) {
  36. return bitset<(B1 & B2)>{};
  37. }
  38. /// Set difference of the option arguments.
  39. template <unsigned B1, unsigned B2>
  40. constexpr auto operator-(bitset<B1>, bitset<B2>) {
  41. return bitset<(B1 & ~B2)>{};
  42. }
  43. /**
  44. Single option flag.
  45. @tparam Pos position of the bit in the set.
  46. */
  47. template <unsigned Pos>
  48. struct bit : bitset<(1 << Pos)> {};
  49. /// All options off.
  50. using none_t = bitset<0>;
  51. /// Axis has an underflow bin. Mutually exclusive with `circular`.
  52. using underflow_t = bit<0>;
  53. /// Axis has overflow bin.
  54. using overflow_t = bit<1>;
  55. /// Axis is circular. Mutually exclusive with `growth` and `underflow`.
  56. using circular_t = bit<2>;
  57. /// Axis can grow. Mutually exclusive with `circular`.
  58. using growth_t = bit<3>;
  59. constexpr none_t none{}; ///< Instance of `none_t`.
  60. constexpr underflow_t underflow{}; ///< Instance of `underflow_t`.
  61. constexpr overflow_t overflow{}; ///< Instance of `overflow_t`.
  62. constexpr circular_t circular{}; ///< Instance of `circular_t`.
  63. constexpr growth_t growth{}; ///< Instance of `growth_t`.
  64. } // namespace option
  65. } // namespace axis
  66. } // namespace histogram
  67. } // namespace boost
  68. #endif