1 | // |
---|---|
2 | // VarIterator.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Dynamic |
6 | // Module: VarIterator |
7 | // |
8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Dynamic/VarIterator.h" |
16 | #include "Poco/Dynamic/Var.h" |
17 | //#include "Poco/Dynamic/Struct.h" |
18 | #undef min |
19 | #undef max |
20 | #include <limits> |
21 | |
22 | |
23 | namespace Poco { |
24 | namespace Dynamic { |
25 | |
26 | |
27 | const std::size_t VarIterator::POSITION_END = std::numeric_limits<std::size_t>::max(); |
28 | |
29 | |
30 | VarIterator::VarIterator(Var* pVar, bool positionEnd): |
31 | _pVar(pVar), |
32 | _position(positionEnd ? POSITION_END : 0) |
33 | { |
34 | } |
35 | |
36 | |
37 | VarIterator::VarIterator(const VarIterator& other): |
38 | _pVar(other._pVar), |
39 | _position(other._position) |
40 | { |
41 | } |
42 | |
43 | |
44 | VarIterator::~VarIterator() |
45 | { |
46 | } |
47 | |
48 | |
49 | VarIterator& VarIterator::operator = (const VarIterator& other) |
50 | { |
51 | VarIterator tmp(other); |
52 | swap(tmp); |
53 | return *this; |
54 | } |
55 | |
56 | |
57 | void VarIterator::swap(VarIterator& other) |
58 | { |
59 | using std::swap; |
60 | |
61 | swap(_pVar, other._pVar); |
62 | swap(_position, other._position); |
63 | } |
64 | |
65 | |
66 | void VarIterator::increment() const |
67 | { |
68 | if (POSITION_END == _position) |
69 | throw RangeException("End of iterator reached."); |
70 | |
71 | if (_position < _pVar->size() - 1) |
72 | ++_position; |
73 | else |
74 | _position = POSITION_END; |
75 | } |
76 | |
77 | |
78 | void VarIterator::decrement() const |
79 | { |
80 | if (0 == _position) |
81 | throw RangeException("Beginning of iterator reached."); |
82 | else if (POSITION_END == _position) |
83 | _position = _pVar->size() - 1; |
84 | else |
85 | --_position; |
86 | } |
87 | |
88 | |
89 | void VarIterator::setPosition(std::size_t pos) const |
90 | { |
91 | if (_position == pos) return; |
92 | |
93 | if (pos < _pVar->size()) |
94 | _position = pos; |
95 | else if (pos == _pVar->size()) |
96 | _position = POSITION_END; |
97 | else |
98 | throw RangeException("Invalid position argument."); |
99 | } |
100 | |
101 | |
102 | Var& VarIterator::operator * () const |
103 | { |
104 | if (POSITION_END == _position) |
105 | throw InvalidAccessException("End of iterator reached."); |
106 | |
107 | return _pVar->operator[](_position); |
108 | } |
109 | |
110 | |
111 | Var* VarIterator::operator -> () const |
112 | { |
113 | if (POSITION_END == _position) |
114 | throw InvalidAccessException("End of iterator reached."); |
115 | |
116 | return &_pVar->operator[](_position); |
117 | } |
118 | |
119 | |
120 | const VarIterator& VarIterator::operator ++ () const |
121 | { |
122 | increment(); |
123 | return *this; |
124 | } |
125 | |
126 | |
127 | VarIterator VarIterator::operator ++ (int) const |
128 | { |
129 | VarIterator old(*this); |
130 | increment(); |
131 | return old; |
132 | } |
133 | |
134 | |
135 | const VarIterator& VarIterator::operator -- () const |
136 | { |
137 | decrement(); |
138 | return *this; |
139 | } |
140 | |
141 | |
142 | VarIterator VarIterator::operator -- (int) const |
143 | { |
144 | VarIterator old(*this); |
145 | decrement(); |
146 | return old; |
147 | } |
148 | |
149 | |
150 | VarIterator VarIterator::operator + (std::size_t diff) const |
151 | { |
152 | VarIterator ri(*this); |
153 | ri.setPosition(_position + diff); |
154 | return ri; |
155 | } |
156 | |
157 | |
158 | VarIterator VarIterator::operator - (std::size_t diff) const |
159 | { |
160 | if (diff > _position) throw RangeException("Invalid position argument."); |
161 | VarIterator ri(*this); |
162 | ri.setPosition(_position - diff); |
163 | return ri; |
164 | } |
165 | |
166 | |
167 | } } // namespace Poco::Dynamic |
168 |