Squid Web Cache master
Loading...
Searching...
No Matches
EnumIterator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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_SRC_BASE_ENUMITERATOR_H
10#define SQUID_SRC_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
76
78 EnumIterator rv(*this);
80 return rv;
81 }
82
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
121
122 // postfix increment
128
129 // prefix decrement
134
135 // postfix decrement
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
230template <auto First, auto Last, class F>
231constexpr void
233{
234 using enumType = decltype(First);
235 using underlyingType = typename std::underlying_type<enumType>::type;
236 // std::integral_constant trick makes f(First) argument a constexpr
237 f(std::integral_constant<enumType, First>()); // including when First is Last
238 if constexpr (First < Last)
239 ConstexprForEnum<enumType(static_cast<underlyingType>(First) + 1), Last>(f);
240}
241
242#endif /* SQUID_SRC_BASE_ENUMITERATOR_H */
243
constexpr void ConstexprForEnum(F &&f)
EnumRangeT< EnumType > EnumRange(EnumType begin, EnumType one_past_end)
EnumType operator*() const
bool operator==(const EnumIteratorBase &i) const
EnumType & reference
bool operator!=(const EnumIteratorBase &i) const
iterator_type current
EnumType * pointer
EnumIteratorBase(EnumType e)
std::ptrdiff_t difference_type
std::underlying_type< EnumType >::type iterator_type
std::bidirectional_iterator_tag iterator_category
EnumType value_type
EnumIterator & operator--()
EnumIterator & operator--(int)
EnumIterator & operator++(int)
EnumIterator(EnumType e)
EnumIterator & operator++()
iterator end() const
EnumType begin_
ReverseEnumIterator< EnumType > reverse_iterator
EnumRangeT(EnumType first, EnumType one_past_last)
iterator begin() const
EnumType end_
reverse_iterator rbegin() const
EnumIterator< EnumType > iterator
reverse_iterator rend() const
ReverseEnumIterator(EnumType e)
ReverseEnumIterator & operator++()
ReverseEnumIterator & operator--()
ReverseEnumIterator & operator++(int)
ReverseEnumIterator & operator--(int)
@ enumEnd_
Definition icp_opcode.h:40
@ enumBegin_
Definition icp_opcode.h:14