option.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  49. using bit = bitset<(1 << Pos)>;
  50. #else
  51. struct bit;
  52. #endif
  53. /// All options off.
  54. using none_t = bitset<0>;
  55. /// Axis has an underflow bin. Mutually exclusive with `circular`.
  56. using underflow_t = bit<0>;
  57. /// Axis has overflow bin.
  58. using overflow_t = bit<1>;
  59. /// Axis is circular. Mutually exclusive with `growth` and `underflow`.
  60. using circular_t = bit<2>;
  61. /// Axis can grow. Mutually exclusive with `circular`.
  62. using growth_t = bit<3>;
  63. constexpr none_t none{}; ///< Instance of `none_t`.
  64. constexpr underflow_t underflow{}; ///< Instance of `underflow_t`.
  65. constexpr overflow_t overflow{}; ///< Instance of `overflow_t`.
  66. constexpr circular_t circular{}; ///< Instance of `circular_t`.
  67. constexpr growth_t growth{}; ///< Instance of `growth_t`.
  68. } // namespace option
  69. } // namespace axis
  70. } // namespace histogram
  71. } // namespace boost
  72. #endif