EnumIterator.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iterator>
4 #include <type_traits>
5 
6 #include <boost/operators.hpp>
7 
8 namespace boca
9 {
10 
15 template<typename Enum_>
16 // class EnumIterator : boost::random_access_iteratable<EnumIterator<Enum_>, Enum_ *, Enum_, Enum_>
17 class EnumIterator : boost::bidirectional_iteratable<EnumIterator<Enum_>, Enum_ *>, boost::less_than_comparable<EnumIterator<Enum_>>
18 {
19 
20  using size_type = std::size_t;
21 
22 // using difference_type = Enum_;
23 
24  using value_type = Enum_;
25 
26  using reference = Enum_;
27 
28  using pointer = Enum_ *;
29 
30 // using iterator_category = std::random_access_iterator_tag;
31  using iterator_category = std::bidirectional_iterator_tag;
32 
33  using underlying_type = typename std::underlying_type<Enum_>::type;
34 
35 public:
36 
45  constexpr EnumIterator() : enum_() {}
46 
50  constexpr explicit EnumIterator(Enum_ value) : enum_(value) {}
52 
61  void Set(Enum_ value)
62  {
63  enum_ = value;
64  }
66 
75  constexpr bool operator<(EnumIterator const &enum_iterator) const
76  {
77  return enum_ < enum_iterator.enum_;
78  }
79 
83  constexpr bool operator==(EnumIterator const &enum_iterator) const
84  {
85  return enum_ == enum_iterator.enum_;
86  }
87 
91  EnumIterator &operator+=(size_type size)
92  {
93  Add(size);
94  return *this;
95  }
96 
100  EnumIterator &operator-=(size_type size)
101  {
102  Substract(size);
103  return *this;
104  }
105 
110  {
111  Add();
112  return *this;
113  }
114 
119  {
120  Substract();
121  return *this;
122  }
123 
124 // /**
125 // * @brief substruction
126 // */
127 // constexpr difference_type operator-(EnumIterator const& enum_iterator) const
128 // {
129 // return static_cast<underlying_type>(enum_) - static_cast<underlying_type>(enum_iterator.enum_);
130 // }
131 
135  constexpr reference operator*() const
136  {
137  return enum_;
138  }
140 
141  constexpr friend void swap(EnumIterator const &lhs, EnumIterator const &rhs)
142  {
143  std::swap(lhs.enum_, rhs.enum_);
144  }
145 
146 private:
147 
148  Enum_ enum_;
149 
150  void Add(size_type size = 1)
151  {
152  enum_ = static_cast<Enum_>(static_cast<underlying_type>(enum_) + size);
153  }
154 
155  void Substract(size_type size = 1)
156  {
157  enum_ = static_cast<Enum_>(static_cast<underlying_type>(enum_) - size);
158  }
159 
160 };
161 
162 }
Enables the use of strongly typed enumerators as iterators.
Definition: EnumIterator.hh:17
constexpr reference operator*() const
substruction
Definition: EnumIterator.hh:135
EnumIterator & operator-=(size_type size)
decrement by integer
Definition: EnumIterator.hh:100
EnumIterator & operator--()
decrement
Definition: EnumIterator.hh:118
constexpr bool operator<(EnumIterator const &enum_iterator) const
Less than comparison.
Definition: EnumIterator.hh:75
constexpr EnumIterator(Enum_ value)
Constructor from enum.
Definition: EnumIterator.hh:50
Boosted Collider Analysis.
Definition: Analysis.hh:15
EnumIterator & operator+=(size_type size)
increment by integer
Definition: EnumIterator.hh:91
constexpr bool operator==(EnumIterator const &enum_iterator) const
equality comparison
Definition: EnumIterator.hh:83
EnumIterator & operator++()
increment
Definition: EnumIterator.hh:109
void Set(Enum_ value)
Set enum.
Definition: EnumIterator.hh:61
constexpr EnumIterator()
Default constructor.
Definition: EnumIterator.hh:45
constexpr friend void swap(EnumIterator const &lhs, EnumIterator const &rhs)
Definition: EnumIterator.hh:141