1 | // |
2 | // VarIterator.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Dynamic |
6 | // Module: VarIterator |
7 | // |
8 | // Definition of the VarIterator class. |
9 | // |
10 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_VarIterator_INCLUDED |
18 | #define Foundation_VarIterator_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Exception.h" |
22 | #include <iterator> |
23 | #include <algorithm> |
24 | |
25 | |
26 | namespace Poco { |
27 | namespace Dynamic { |
28 | |
29 | |
30 | class Var; |
31 | |
32 | |
33 | class Foundation_API VarIterator |
34 | /// VarIterator class. |
35 | { |
36 | public: |
37 | typedef std::bidirectional_iterator_tag iterator_category; |
38 | typedef Var value_type; |
39 | typedef std::ptrdiff_t difference_type; |
40 | typedef Var* pointer; |
41 | typedef Var& reference; |
42 | |
43 | static const std::size_t POSITION_END; |
44 | /// End position indicator. |
45 | |
46 | VarIterator(Var* pVar, bool positionEnd); |
47 | /// Creates the VarIterator and positions it at the end of |
48 | /// the recordset if positionEnd is true. Otherwise, it is |
49 | /// positioned at the beginning. |
50 | |
51 | VarIterator(const VarIterator& other); |
52 | /// Creates a copy of other VarIterator. |
53 | |
54 | ~VarIterator(); |
55 | /// Destroys the VarIterator. |
56 | |
57 | VarIterator& operator = (const VarIterator& other); |
58 | /// Assigns the other VarIterator. |
59 | |
60 | bool operator == (const VarIterator& other) const; |
61 | /// Equality operator. |
62 | |
63 | bool operator != (const VarIterator& other) const; |
64 | /// Inequality operator. |
65 | |
66 | Var& operator * () const; |
67 | /// Returns value at the current position. |
68 | |
69 | Var* operator -> () const; |
70 | /// Returns pointer to the value at current position. |
71 | |
72 | const VarIterator& operator ++ () const; |
73 | /// Advances by one position and returns current position. |
74 | |
75 | VarIterator operator ++ (int) const; |
76 | /// Advances by one position and returns copy of the iterator with |
77 | /// previous current position. |
78 | |
79 | const VarIterator& operator -- () const; |
80 | /// Goes back by one position and returns copy of the iterator with |
81 | /// previous current position. |
82 | |
83 | VarIterator operator -- (int) const; |
84 | /// Goes back by one position and returns previous current position. |
85 | |
86 | VarIterator operator + (std::size_t diff) const; |
87 | /// Returns a copy the VarIterator advanced by diff positions. |
88 | |
89 | VarIterator operator - (std::size_t diff) const; |
90 | /// Returns a copy the VarIterator backed by diff positions. |
91 | /// Throws RangeException if diff is larger than current position. |
92 | |
93 | void swap(VarIterator& other); |
94 | /// Swaps the VarIterator with another one. |
95 | |
96 | private: |
97 | VarIterator(); |
98 | |
99 | void increment() const; |
100 | /// Increments the iterator position by one. |
101 | /// Throws RangeException if position is out of range. |
102 | |
103 | void decrement() const; |
104 | /// Decrements the iterator position by one. |
105 | /// Throws RangeException if position is out of range. |
106 | |
107 | void setPosition(std::size_t pos) const; |
108 | /// Sets the iterator position. |
109 | /// Throws RangeException if position is out of range. |
110 | |
111 | Var* _pVar; |
112 | mutable std::size_t _position; |
113 | |
114 | friend class Var; |
115 | }; |
116 | |
117 | |
118 | /// |
119 | /// inlines |
120 | /// |
121 | |
122 | |
123 | inline bool VarIterator::operator == (const VarIterator& other) const |
124 | { |
125 | return _pVar == other._pVar && _position == other._position; |
126 | } |
127 | |
128 | |
129 | inline bool VarIterator::operator != (const VarIterator& other) const |
130 | { |
131 | return _pVar != other._pVar || _position != other._position; |
132 | } |
133 | |
134 | |
135 | } } // namespace Poco::Dynamic |
136 | |
137 | |
138 | namespace std |
139 | { |
140 | template<> |
141 | inline void swap<Poco::Dynamic::VarIterator>(Poco::Dynamic::VarIterator& s1, |
142 | Poco::Dynamic::VarIterator& s2) |
143 | /// Full template specialization of std:::swap for VarIterator |
144 | { |
145 | s1.swap(s2); |
146 | } |
147 | } |
148 | |
149 | |
150 | #endif // Foundation_VarIterator_INCLUDED |
151 | |