TLA Line data Source code
1 : // Copyright (c) 2014-2022 Thomas Fussell
2 : // Copyright (c) 2024-2025 xlnt-community
3 : //
4 : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : // of this software and associated documentation files (the "Software"), to deal
6 : // in the Software without restriction, including without limitation the rights
7 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : // copies of the Software, and to permit persons to whom the Software is
9 : // furnished to do so, subject to the following conditions:
10 : //
11 : // The above copyright notice and this permission notice shall be included in
12 : // all copies or substantial portions of the Software.
13 : //
14 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : // THE SOFTWARE
21 : //
22 : // @license: http://www.opensource.org/licenses/mit-license.php
23 : // @author: see AUTHORS file
24 :
25 : #pragma once
26 :
27 : #include <cstddef> // std::ptrdiff_t
28 :
29 : #include <xlnt/xlnt_config.hpp>
30 : #include <xlnt/cell/cell_reference.hpp>
31 : #include <xlnt/worksheet/major_order.hpp>
32 : #include <xlnt/worksheet/range_reference.hpp>
33 : #include <xlnt/worksheet/worksheet.hpp>
34 :
35 : namespace xlnt {
36 :
37 : class cell_vector;
38 :
39 : /// <summary>
40 : /// An iterator used by worksheet and range for traversing
41 : /// a 2D grid of cells by row/column then across that row/column.
42 : /// </summary>
43 : class XLNT_API range_iterator
44 : {
45 : public:
46 : /// <summary>
47 : /// iterator tags required for use with standard algorithms and adapters
48 : /// </summary>
49 : using iterator_category = std::bidirectional_iterator_tag;
50 : using value_type = cell_vector;
51 : using difference_type = std::ptrdiff_t;
52 : using pointer = cell_vector *;
53 : using reference = cell_vector; // intentionally value
54 :
55 : /// <summary>
56 : /// Default constructs a range iterator
57 : /// </summary>
58 CBC 2 : range_iterator() = default;
59 :
60 : /// <summary>
61 : /// Constructs a range iterator on a worksheet, cell pointing to the current
62 : /// row or column, range bounds, an order, and whether or not to skip null column/rows.
63 : /// </summary>
64 : range_iterator(worksheet &ws, const cell_reference &cursor,
65 : const range_reference &bounds, major_order order, bool skip_null);
66 :
67 : /// <summary>
68 : /// Default copy constructor.
69 : /// </summary>
70 22 : range_iterator(const range_iterator &) = default;
71 :
72 : /// <summary>
73 : /// Default assignment operator.
74 : /// </summary>
75 : range_iterator &operator=(const range_iterator &) = default;
76 :
77 : /// <summary>
78 : /// Default move constructor.
79 : /// </summary>
80 : range_iterator(range_iterator &&) = default;
81 :
82 : /// <summary>
83 : /// Default move assignment operator.
84 : /// </summary>
85 1 : range_iterator &operator=(range_iterator &&) = default;
86 :
87 : /// <summary>
88 : /// Default destructor
89 : /// </summary>
90 : ~range_iterator() = default;
91 :
92 : /// <summary>
93 : /// Dereference the iterator to return a column or row.
94 : /// </summary>
95 : reference operator*();
96 :
97 : /// <summary>
98 : /// Dereference the iterator to return a column or row.
99 : /// </summary>
100 : const reference operator*() const;
101 :
102 : /// <summary>
103 : /// Returns true if this iterator is equivalent to other.
104 : /// </summary>
105 : bool operator==(const range_iterator &other) const;
106 :
107 : /// <summary>
108 : /// Returns true if this iterator is not equivalent to other.
109 : /// </summary>
110 : bool operator!=(const range_iterator &other) const;
111 :
112 : /// <summary>
113 : /// Pre-decrement the iterator to point to the previous row/column.
114 : /// </summary>
115 : range_iterator &operator--();
116 :
117 : /// <summary>
118 : /// Post-decrement the iterator to point to the previous row/column.
119 : /// </summary>
120 : range_iterator operator--(int);
121 :
122 : /// <summary>
123 : /// Pre-increment the iterator to point to the next row/column.
124 : /// </summary>
125 : range_iterator &operator++();
126 :
127 : /// <summary>
128 : /// Post-increment the iterator to point to the next row/column.
129 : /// </summary>
130 : range_iterator operator++(int);
131 :
132 : private:
133 : /// <summary>
134 : /// If true, empty rows and cells will be skipped when iterating with this iterator
135 : /// </summary>
136 : bool skip_null_ = false;
137 :
138 : /// <summary>
139 : /// Whether rows or columns should be iterated over first
140 : /// </summary>
141 : major_order order_ = major_order::column;
142 :
143 : /// <summary>
144 : /// The worksheet
145 : /// </summary>
146 : worksheet ws_;
147 :
148 : /// <summary>
149 : /// The first cell in the current row/column
150 : /// </summary>
151 : cell_reference cursor_;
152 :
153 : /// <summary>
154 : /// The bounds of the range
155 : /// </summary>
156 : range_reference bounds_;
157 : };
158 :
159 : /// <summary>
160 : /// A const version of range_iterator which does not allow modification
161 : /// to the dereferenced cell_vector.
162 : /// </summary>
163 : class XLNT_API const_range_iterator
164 : {
165 : public:
166 : /// <summary>
167 : /// this iterator meets the interface requirements of bidirection_iterator
168 : /// </summary>
169 : using iterator_category = std::bidirectional_iterator_tag;
170 : using value_type = const cell_vector;
171 : using difference_type = std::ptrdiff_t;
172 : using pointer = const cell_vector *;
173 : using reference = const cell_vector; // intentionally value
174 :
175 : /// <summary>
176 : /// Default constructs a range iterator
177 : /// </summary>
178 2 : const_range_iterator() = default;
179 :
180 : /// <summary>
181 : /// Constructs a range iterator on a worksheet, cell pointing to the current
182 : /// row or column, range bounds, an order, and whether or not to skip null column/rows.
183 : /// </summary>
184 : const_range_iterator(const worksheet &ws, const cell_reference &cursor,
185 : const range_reference &bounds, major_order order, bool skip_null);
186 :
187 : /// <summary>
188 : /// Default copy constructor.
189 : /// </summary>
190 : const_range_iterator(const const_range_iterator &) = default;
191 :
192 : /// <summary>
193 : /// Default assignment operator.
194 : /// </summary>
195 : const_range_iterator &operator=(const const_range_iterator &) = default;
196 :
197 : /// <summary>
198 : /// Default move constructor.
199 : /// </summary>
200 : const_range_iterator(const_range_iterator &&) = default;
201 :
202 : /// <summary>
203 : /// Default move assignment operator.
204 : /// </summary>
205 : const_range_iterator &operator=(const_range_iterator &&) = default;
206 :
207 : /// <summary>
208 : /// Default destructor
209 : /// </summary>
210 : ~const_range_iterator() = default;
211 :
212 : /// <summary>
213 : /// Dereferennce the iterator to return the current column/row.
214 : /// </summary>
215 : const reference operator*() const;
216 :
217 : /// <summary>
218 : /// Returns true if this iterator is equivalent to other.
219 : /// </summary>
220 : bool operator==(const const_range_iterator &other) const;
221 :
222 : /// <summary>
223 : /// Returns true if this iterator is not equivalent to other.
224 : /// </summary>
225 : bool operator!=(const const_range_iterator &other) const;
226 :
227 : /// <summary>
228 : /// Pre-decrement the iterator to point to the next row/column.
229 : /// </summary>
230 : const_range_iterator &operator--();
231 :
232 : /// <summary>
233 : /// Post-decrement the iterator to point to the next row/column.
234 : /// </summary>
235 : const_range_iterator operator--(int);
236 :
237 : /// <summary>
238 : /// Pre-increment the iterator to point to the next row/column.
239 : /// </summary>
240 : const_range_iterator &operator++();
241 :
242 : /// <summary>
243 : /// Post-increment the iterator to point to the next row/column.
244 : /// </summary>
245 : const_range_iterator operator++(int);
246 :
247 : private:
248 : /// <summary>
249 : /// If true, empty rows and cells will be skipped when iterating with this iterator
250 : /// </summary>
251 : bool skip_null_ = false;
252 :
253 : /// <summary>
254 : /// Determines whether iteration should move through rows or columns first
255 : /// </summary>
256 : major_order order_ = major_order::column;
257 :
258 : /// <summary>
259 : /// The implementation of the worksheet this iterator points to
260 : /// </summary>
261 : detail::worksheet_impl *ws_ = nullptr;
262 :
263 : /// <summary>
264 : /// The first cell in the current row or column this iterator points to
265 : /// </summary>
266 : cell_reference cursor_;
267 :
268 : /// <summary>
269 : /// The range this iterator starts and ends in
270 : /// </summary>
271 : range_reference bounds_;
272 : };
273 :
274 : } // namespace xlnt
|