EnumIterator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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 typedef typename std::underlying_type<EnumType>::type iterator_type;
27
28public:
29 using iterator_category = std::bidirectional_iterator_tag;
30 using value_type = EnumType;
31 using difference_type = std::ptrdiff_t;
32 using pointer = EnumType *;
33 using reference = EnumType &;
34
35 explicit EnumIteratorBase(EnumType e) : current(static_cast<iterator_type>(e)) {}
36
37 bool operator==(const EnumIteratorBase &i) const {
38 return current == i.current;
39 }
40
41 bool operator!=(const EnumIteratorBase &i) const {
42 return current != i.current;
43 }
44
45 EnumType operator*() const {
46 return static_cast<EnumType>(current);
47 }
48protected:
50};
51
66template <typename EnumType>
67class EnumIterator : public EnumIteratorBase<EnumType>
68{
69public:
70 explicit EnumIterator(EnumType e) : EnumIteratorBase<EnumType>(e) {}
71
74 return *this;
75 }
76
78 EnumIterator rv(*this);
80 return rv;
81 }
82
85 return *this;
86 }
87
89 EnumIterator rv(*this);
91 return rv;
92 }
93};
94
110template <typename EnumType>
111class ReverseEnumIterator : public EnumIteratorBase<EnumType>
112{
113public:
114 explicit ReverseEnumIterator(EnumType e) : EnumIteratorBase<EnumType>(e) {}
115
116 // prefix increment
119 return *this;
120 }
121
122 // postfix increment
124 ReverseEnumIterator rv(*this);
126 return rv;
127 }
128
129 // prefix decrement
132 return *this;
133 }
134
135 // postfix decrement
137 ReverseEnumIterator rv(*this);
139 return rv;
140 }
141};
142
156template <typename EnumType>
158{
159public:
162 EnumRangeT(EnumType first, EnumType one_past_last) : begin_(first), end_(one_past_last) { }
163 iterator begin() const { return iterator(begin_);}
164 iterator end() const { return iterator(end_);}
167private:
168 EnumType begin_;
169 EnumType end_;
170};
171
187template <typename EnumType>
188EnumRangeT<EnumType> EnumRange(EnumType begin, EnumType one_past_end)
189{
190 return EnumRangeT<EnumType>(begin,one_past_end);
191}
192
220template <typename EnumType>
221class WholeEnum : public EnumRangeT<EnumType>
222{
223public:
224 WholeEnum() : EnumRangeT<EnumType>(EnumType::enumBegin_, EnumType::enumEnd_) {}
225};
226
227#endif /* SQUID_BASE_ENUMITERATOR_H */
228
EnumRangeT< EnumType > EnumRange(EnumType begin, EnumType one_past_end)
Definition: EnumIterator.h:188
EnumType operator*() const
Definition: EnumIterator.h:45
bool operator==(const EnumIteratorBase &i) const
Definition: EnumIterator.h:37
EnumType & reference
Definition: EnumIterator.h:33
bool operator!=(const EnumIteratorBase &i) const
Definition: EnumIterator.h:41
iterator_type current
Definition: EnumIterator.h:49
EnumType * pointer
Definition: EnumIterator.h:32
EnumIteratorBase(EnumType e)
Definition: EnumIterator.h:35
std::ptrdiff_t difference_type
Definition: EnumIterator.h:31
std::underlying_type< EnumType >::type iterator_type
Definition: EnumIterator.h:26
std::bidirectional_iterator_tag iterator_category
Definition: EnumIterator.h:29
EnumType value_type
Definition: EnumIterator.h:30
EnumIterator & operator--()
Definition: EnumIterator.h:83
EnumIterator & operator--(int)
Definition: EnumIterator.h:88
EnumIterator & operator++(int)
Definition: EnumIterator.h:77
EnumIterator(EnumType e)
Definition: EnumIterator.h:70
EnumIterator & operator++()
Definition: EnumIterator.h:72
iterator end() const
Definition: EnumIterator.h:164
EnumType begin_
Definition: EnumIterator.h:168
ReverseEnumIterator< EnumType > reverse_iterator
Definition: EnumIterator.h:161
EnumRangeT(EnumType first, EnumType one_past_last)
Definition: EnumIterator.h:162
iterator begin() const
Definition: EnumIterator.h:163
EnumType end_
Definition: EnumIterator.h:169
reverse_iterator rbegin() const
Definition: EnumIterator.h:165
EnumIterator< EnumType > iterator
Definition: EnumIterator.h:160
reverse_iterator rend() const
Definition: EnumIterator.h:166
ReverseEnumIterator(EnumType e)
Definition: EnumIterator.h:114
ReverseEnumIterator & operator++()
Definition: EnumIterator.h:117
ReverseEnumIterator & operator--()
Definition: EnumIterator.h:130
ReverseEnumIterator & operator++(int)
Definition: EnumIterator.h:123
ReverseEnumIterator & operator--(int)
Definition: EnumIterator.h:136

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors