xlnt
cell_iterator.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024 xlnt-community
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE
22 //
23 // @license: http://www.opensource.org/licenses/mit-license.php
24 // @author: see AUTHORS file
25 
26 #pragma once
27 
28 #include <cstddef> // std::ptrdiff_t
29 #include <iterator>
30 
31 #include <xlnt/xlnt_config.hpp>
32 #include <xlnt/cell/cell_reference.hpp>
33 #include <xlnt/worksheet/major_order.hpp>
34 #include <xlnt/worksheet/range_reference.hpp>
35 #include <xlnt/worksheet/worksheet.hpp>
36 
37 namespace xlnt {
38 
39 enum class major_order;
40 
41 class cell;
42 class cell_reference;
43 class range_reference;
44 
48 class XLNT_API cell_iterator
49 {
50 public:
54  using iterator_category = std::bidirectional_iterator_tag;
55  using value_type = cell;
56  using difference_type = std::ptrdiff_t;
57  using pointer = cell *;
58  using reference = cell; // intentionally value
59 
63  cell_iterator() = default;
64 
68  cell_iterator(worksheet ws, const cell_reference &start_cell,
69  const range_reference &limits, major_order order, bool skip_null, bool wrap);
70 
74  cell_iterator(const cell_iterator &) = default;
75 
79  cell_iterator &operator=(const cell_iterator &) = default;
80 
84  cell_iterator(cell_iterator &&) = default;
85 
89  cell_iterator &operator=(cell_iterator &&) = default;
90 
94  ~cell_iterator() = default;
95 
99  reference operator*();
100 
104  const reference operator*() const;
105 
109  bool operator==(const cell_iterator &other) const;
110 
114  bool operator!=(const cell_iterator &other) const;
115 
120  cell_iterator &operator--();
121 
126  cell_iterator operator--(int);
127 
132  cell_iterator &operator++();
133 
138  cell_iterator operator++(int);
139 
145  bool has_value() const;
146 
147 private:
151  bool skip_null_ = false;
152 
158  bool wrap_ = false;
159 
166  major_order order_ = major_order::column;
167 
171  worksheet ws_;
172 
176  cell_reference cursor_;
177 
181  range_reference bounds_;
182 };
183 
187 class XLNT_API const_cell_iterator
188 {
189 public:
193  using iterator_category = std::bidirectional_iterator_tag;
194  using value_type = const cell;
195  using difference_type = std::ptrdiff_t;
196  using pointer = const cell *;
197  using reference = const cell; // intentionally value
198 
202  const_cell_iterator() = default;
203 
207  const_cell_iterator(worksheet ws, const cell_reference &start_cell,
208  const range_reference &limits, major_order order, bool skip_null, bool wrap);
209 
213  const_cell_iterator(const const_cell_iterator &) = default;
214 
218  const_cell_iterator &operator=(const const_cell_iterator &) = default;
219 
224 
228  const_cell_iterator &operator=(const_cell_iterator &&) = default;
229 
233  ~const_cell_iterator() = default;
234 
238  const reference operator*() const;
239 
243  bool operator==(const const_cell_iterator &other) const;
244 
248  bool operator!=(const const_cell_iterator &other) const;
249 
254  const_cell_iterator &operator--();
255 
260  const_cell_iterator operator--(int);
261 
266  const_cell_iterator &operator++();
267 
272  const_cell_iterator operator++(int);
273 
279  bool has_value() const;
280 
281 private:
285  bool skip_null_ = false;
286 
292  bool wrap_ = false;
293 
300  major_order order_ = major_order::column;
301 
305  worksheet ws_;
306 
310  cell_reference cursor_;
311 
315  range_reference bounds_;
316 };
317 
318 } // namespace xlnt
A cell iterator iterates over a 1D range by row or by column.
Definition: cell_iterator.hpp:187
std::bidirectional_iterator_tag iterator_category
iterator tags required for use with standard algorithms and adapters
Definition: cell_iterator.hpp:193
std::bidirectional_iterator_tag iterator_category
iterator tags required for use with standard algorithms and adapters
Definition: cell_iterator.hpp:54
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
A worksheet is a 2D array of cells starting with cell A1 in the top-left corner and extending indefin...
Definition: worksheet.hpp:77
major_order
Defines whether iterating a range returns columns or rows sequentially.
Definition: major_order.hpp:34
bool operator==(std::nullptr_t, const cell &cell)
Returns true if this cell is uninitialized.
An object used to refer to a cell. References have two parts, the column and the row. In Excel, the reference string A1 refers to the top-left-most cell. A cell_reference can be initialized from a string of this form or a 1-indexed ordered pair of the form column, row.
Definition: cell_reference.hpp:60
Describes a unit of data in a worksheet at a specific coordinate and its associated properties...
Definition: cell.hpp:84
A cell iterator iterates over a 1D range by row or by column.
Definition: cell_iterator.hpp:48
bool operator!=(const std::string &reference_string, const range_reference &ref)
Returns true if the string representation of the range is not equivalent to ref.
A range_reference describes a rectangular area of a worksheet with positive width and height defined ...
Definition: range_reference.hpp:36