paludis Version 3.0.2
The one true package mangler
enum_iterator.hh
1/* vim: set sw=4 sts=4 et foldmethod=syntax : */
2
3/*
4 * Copyright (c) 2009 Ciaran McCreesh
5 *
6 * This file is part of the Paludis package manager. Paludis is free software;
7 * you can redistribute it and/or modify it under the terms of the GNU General
8 * Public License version 2, as published by the Free Software Foundation.
9 *
10 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <paludis/util/enum_iterator-fwd.hh>
21#include <paludis/util/operators.hh>
22#include <iterator>
23
24#ifndef PALUDIS_GUARD_PALUDIS_UTIL_ENUM_ITERATOR_HH
25#define PALUDIS_GUARD_PALUDIS_UTIL_ENUM_ITERATOR_HH 1
26
27namespace paludis
28{
29 /**
30 * An iterator for iterating over enums.
31 *
32 * \since 0.41
33 * \ingroup g_iterator
34 */
35 template <typename E_>
36 class EnumIterator :
38 {
39 private:
40 E_ _value;
41
42 public:
43 ///\name Basic operations
44 ///\{
45
46 EnumIterator() :
47 _value(static_cast<E_>(0))
48 {
49 }
50
51 EnumIterator(const EnumIterator & other) :
52 _value(other._value)
53 {
54 }
55
56 explicit EnumIterator(const E_ e) :
57 _value(e)
58 {
59 }
60
61
62 EnumIterator & operator= (const EnumIterator & other)
63 {
64 _value = other._value;
65 return *this;
66 }
67
68 ///\}
69
70 ///\name Standard library typedefs
71 ///\{
72
73 typedef E_ & value_type;
74
75 typedef E_ & reference;
76 typedef const E_ & const_reference;
77
78 typedef E_ * pointer;
79 typedef const E_ * const_pointer;
80
81 typedef std::ptrdiff_t difference_type;
82 typedef std::forward_iterator_tag iterator_category;
83
84 ///\}
85
86 ///\name Increment
87 ///\{
88
89 EnumIterator & operator++ ()
90 {
91 _value = static_cast<E_>(_value + 1);
92 return *this;
93 }
94
95 EnumIterator operator++ (int)
96 {
97 EnumIterator result(*this);
98 ++result;
99 return result;
100 }
101
102 ///\}
103
104 ///\name Dereference
105 ///\{
106
107 pointer operator-> ()
108 {
109 return &_value;
110 }
111
112 reference operator* ()
113 {
114 return _value;
115 }
116
117 const_pointer operator-> () const
118 {
119 return &_value;
120 }
121
122 const_reference operator* () const
123 {
124 return _value;
125 }
126
127 ///\}
128
129 ///\name Comparisons
130
131 bool operator== (const EnumIterator & other) const
132 {
133 return **this == *other;
134 }
135
136 bool operator< (const EnumIterator & other) const
137 {
138 return **this < *other;
139 }
140
141 ///\}
142 };
143
144 template <typename E_>
145 EnumIterator<E_> enum_iterator(const E_ e)
146 {
147 return EnumIterator<E_>(e);
148 }
149}
150
151#endif
Definition enum_iterator.hh:38
Definition about_metadata-fwd.hh:24