EnumIterator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_BASE_ENUMITERATOR_H
10 #define SQUID_BASE_ENUMITERATOR_H
11 
12 #include <iterator>
13 #include <type_traits>
14 
22 template <typename EnumType>
23 class EnumIteratorBase : public std::iterator<std::bidirectional_iterator_tag, EnumType>
24 {
25 protected:
26 #if HAVE_STD_UNDERLYING_TYPE
28 #else
29  typedef int iterator_type;
30 #endif
31 
32 public:
33  explicit EnumIteratorBase(EnumType e) : current(static_cast<iterator_type>(e)) {}
34 
35  bool operator==(const EnumIteratorBase &i) const {
36  return current == i.current;
37  }
38 
39  bool operator!=(const EnumIteratorBase &i) const {
40  return current != i.current;
41  }
42 
43  EnumType operator*() const {
44  return static_cast<EnumType>(current);
45  }
46 protected:
48 };
49 
64 template <typename EnumType>
65 class EnumIterator : public EnumIteratorBase<EnumType>
66 {
67 public:
68  explicit EnumIterator(EnumType e) : EnumIteratorBase<EnumType>(e) {}
69 
72  return *this;
73  }
74 
76  EnumIterator rv(*this);
78  return rv;
79  }
80 
83  return *this;
84  }
85 
87  EnumIterator rv(*this);
89  return rv;
90  }
91 };
92 
108 template <typename EnumType>
109 class ReverseEnumIterator : public EnumIteratorBase<EnumType>
110 {
111 public:
112  explicit ReverseEnumIterator(EnumType e) : EnumIteratorBase<EnumType>(e) {}
113 
114  // prefix increment
117  return *this;
118  }
119 
120  // postfix increment
122  ReverseEnumIterator rv(*this);
124  return rv;
125  }
126 
127  // prefix decrement
130  return *this;
131  }
132 
133  // postfix decrement
135  ReverseEnumIterator rv(*this);
137  return rv;
138  }
139 };
140 
154 template <typename EnumType>
156 {
157 public:
160  EnumRangeT(EnumType first, EnumType one_past_last) : begin_(first), end_(one_past_last) { }
161  iterator begin() const { return iterator(begin_);}
162  iterator end() const { return iterator(end_);}
165 private:
166  EnumType begin_;
167  EnumType end_;
168 };
169 
185 template <typename EnumType>
186 EnumRangeT<EnumType> EnumRange(EnumType begin, EnumType one_past_end)
187 {
188  return EnumRangeT<EnumType>(begin,one_past_end);
189 }
190 
218 template <typename EnumType>
219 class WholeEnum : public EnumRangeT<EnumType>
220 {
221 public:
222  WholeEnum() : EnumRangeT<EnumType>(EnumType::enumBegin_, EnumType::enumEnd_) {}
223 };
224 
225 #endif /* SQUID_BASE_ENUMITERATOR_H */
226 
ReverseEnumIterator & operator--(int)
Definition: EnumIterator.h:134
EnumIterator & operator++(int)
Definition: EnumIterator.h:75
iterator_type current
Definition: EnumIterator.h:47
int type
Definition: errorpage.cc:79
int i
Definition: membanger.c:49
EnumIterator & operator--(int)
Definition: EnumIterator.h:86
ReverseEnumIterator & operator++()
Definition: EnumIterator.h:115
ReverseEnumIterator & operator--()
Definition: EnumIterator.h:128
EnumRangeT(EnumType first, EnumType one_past_last)
Definition: EnumIterator.h:160
EnumIteratorBase(EnumType e)
Definition: EnumIterator.h:33
EnumType operator*() const
Definition: EnumIterator.h:43
iterator end() const
Definition: EnumIterator.h:162
EnumType end_
Definition: EnumIterator.h:167
iterator begin() const
Definition: EnumIterator.h:161
reverse_iterator rend() const
Definition: EnumIterator.h:164
EnumIterator & operator--()
Definition: EnumIterator.h:81
EnumIterator(EnumType e)
Definition: EnumIterator.h:68
ReverseEnumIterator & operator++(int)
Definition: EnumIterator.h:121
bool operator==(const EnumIteratorBase &i) const
Definition: EnumIterator.h:35
EnumRangeT< EnumType > EnumRange(EnumType begin, EnumType one_past_end)
Definition: EnumIterator.h:186
EnumType begin_
Definition: EnumIterator.h:166
EnumIterator & operator++()
Definition: EnumIterator.h:70
reverse_iterator rbegin() const
Definition: EnumIterator.h:163
ReverseEnumIterator(EnumType e)
Definition: EnumIterator.h:112
EnumIterator< EnumType > iterator
Definition: EnumIterator.h:158
ReverseEnumIterator< EnumType > reverse_iterator
Definition: EnumIterator.h:159
bool operator!=(const EnumIteratorBase &i) const
Definition: EnumIterator.h:39

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors