EnumIterator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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
22template <typename EnumType>
24{
25protected:
26#if HAVE_STD_UNDERLYING_TYPE
28#else
29 typedef int iterator_type;
30#endif
31
32public:
33 using iterator_category = std::bidirectional_iterator_tag;
34 using value_type = EnumType;
35 using difference_type = std::ptrdiff_t;
36 using pointer = EnumType *;
37 using reference = EnumType &;
38
39 explicit EnumIteratorBase(EnumType e) : current(static_cast<iterator_type>(e)) {}
40
41 bool operator==(const EnumIteratorBase &i) const {
42 return current == i.current;
43 }
44
45 bool operator!=(const EnumIteratorBase &i) const {
46 return current != i.current;
47 }
48
49 EnumType operator*() const {
50 return static_cast<EnumType>(current);
51 }
52protected:
54};
55
70template <typename EnumType>
71class EnumIterator : public EnumIteratorBase<EnumType>
72{
73public:
74 explicit EnumIterator(EnumType e) : EnumIteratorBase<EnumType>(e) {}
75
78 return *this;
79 }
80
82 EnumIterator rv(*this);
84 return rv;
85 }
86
89 return *this;
90 }
91
93 EnumIterator rv(*this);
95 return rv;
96 }
97};
98
114template <typename EnumType>
115class ReverseEnumIterator : public EnumIteratorBase<EnumType>
116{
117public:
118 explicit ReverseEnumIterator(EnumType e) : EnumIteratorBase<EnumType>(e) {}
119
120 // prefix increment
123 return *this;
124 }
125
126 // postfix increment
128 ReverseEnumIterator rv(*this);
130 return rv;
131 }
132
133 // prefix decrement
136 return *this;
137 }
138
139 // postfix decrement
141 ReverseEnumIterator rv(*this);
143 return rv;
144 }
145};
146
160template <typename EnumType>
162{
163public:
166 EnumRangeT(EnumType first, EnumType one_past_last) : begin_(first), end_(one_past_last) { }
167 iterator begin() const { return iterator(begin_);}
168 iterator end() const { return iterator(end_);}
171private:
172 EnumType begin_;
173 EnumType end_;
174};
175
191template <typename EnumType>
192EnumRangeT<EnumType> EnumRange(EnumType begin, EnumType one_past_end)
193{
194 return EnumRangeT<EnumType>(begin,one_past_end);
195}
196
224template <typename EnumType>
225class WholeEnum : public EnumRangeT<EnumType>
226{
227public:
228 WholeEnum() : EnumRangeT<EnumType>(EnumType::enumBegin_, EnumType::enumEnd_) {}
229};
230
231#endif /* SQUID_BASE_ENUMITERATOR_H */
232
EnumRangeT< EnumType > EnumRange(EnumType begin, EnumType one_past_end)
Definition: EnumIterator.h:192
EnumType operator*() const
Definition: EnumIterator.h:49
bool operator==(const EnumIteratorBase &i) const
Definition: EnumIterator.h:41
EnumType & reference
Definition: EnumIterator.h:37
bool operator!=(const EnumIteratorBase &i) const
Definition: EnumIterator.h:45
iterator_type current
Definition: EnumIterator.h:53
EnumType * pointer
Definition: EnumIterator.h:36
EnumIteratorBase(EnumType e)
Definition: EnumIterator.h:39
std::ptrdiff_t difference_type
Definition: EnumIterator.h:35
std::bidirectional_iterator_tag iterator_category
Definition: EnumIterator.h:33
EnumType value_type
Definition: EnumIterator.h:34
EnumIterator & operator--()
Definition: EnumIterator.h:87
EnumIterator & operator--(int)
Definition: EnumIterator.h:92
EnumIterator & operator++(int)
Definition: EnumIterator.h:81
EnumIterator(EnumType e)
Definition: EnumIterator.h:74
EnumIterator & operator++()
Definition: EnumIterator.h:76
iterator end() const
Definition: EnumIterator.h:168
EnumType begin_
Definition: EnumIterator.h:172
ReverseEnumIterator< EnumType > reverse_iterator
Definition: EnumIterator.h:165
EnumRangeT(EnumType first, EnumType one_past_last)
Definition: EnumIterator.h:166
iterator begin() const
Definition: EnumIterator.h:167
EnumType end_
Definition: EnumIterator.h:173
reverse_iterator rbegin() const
Definition: EnumIterator.h:169
EnumIterator< EnumType > iterator
Definition: EnumIterator.h:164
reverse_iterator rend() const
Definition: EnumIterator.h:170
ReverseEnumIterator(EnumType e)
Definition: EnumIterator.h:118
ReverseEnumIterator & operator++()
Definition: EnumIterator.h:121
ReverseEnumIterator & operator--()
Definition: EnumIterator.h:134
ReverseEnumIterator & operator++(int)
Definition: EnumIterator.h:127
ReverseEnumIterator & operator--(int)
Definition: EnumIterator.h:140
int type
Definition: errorpage.cc:152

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors