xlnt
range_iterator.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2024 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 
43 class XLNT_API range_iterator
44 {
45 public:
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 
58  range_iterator() = default;
59 
64  range_iterator(worksheet &ws, const cell_reference &cursor,
65  const range_reference &bounds, major_order order, bool skip_null);
66 
70  range_iterator(const range_iterator &) = default;
71 
75  range_iterator &operator=(const range_iterator &) = default;
76 
80  range_iterator(range_iterator &&) = default;
81 
85  range_iterator &operator=(range_iterator &&) = default;
86 
90  ~range_iterator() = default;
91 
95  reference operator*();
96 
100  const reference operator*() const;
101 
105  bool operator==(const range_iterator &other) const;
106 
110  bool operator!=(const range_iterator &other) const;
111 
115  range_iterator &operator--();
116 
120  range_iterator operator--(int);
121 
125  range_iterator &operator++();
126 
130  range_iterator operator++(int);
131 
132 private:
136  bool skip_null_ = false;
137 
141  major_order order_ = major_order::column;
142 
146  worksheet ws_;
147 
151  cell_reference cursor_;
152 
156  range_reference bounds_;
157 };
158 
163 class XLNT_API const_range_iterator
164 {
165 public:
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 
178  const_range_iterator() = default;
179 
184  const_range_iterator(const worksheet &ws, const cell_reference &cursor,
185  const range_reference &bounds, major_order order, bool skip_null);
186 
190  const_range_iterator(const const_range_iterator &) = default;
191 
195  const_range_iterator &operator=(const const_range_iterator &) = default;
196 
201 
205  const_range_iterator &operator=(const_range_iterator &&) = default;
206 
210  ~const_range_iterator() = default;
211 
215  const reference operator*() const;
216 
220  bool operator==(const const_range_iterator &other) const;
221 
225  bool operator!=(const const_range_iterator &other) const;
226 
230  const_range_iterator &operator--();
231 
235  const_range_iterator operator--(int);
236 
240  const_range_iterator &operator++();
241 
245  const_range_iterator operator++(int);
246 
247 private:
251  bool skip_null_ = false;
252 
256  major_order order_ = major_order::column;
257 
261  detail::worksheet_impl *ws_;
262 
266  cell_reference cursor_;
267 
271  range_reference bounds_;
272 };
273 
274 } // namespace xlnt
A const version of range_iterator which does not allow modification to the dereferenced cell_vector...
Definition: range_iterator.hpp:163
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
std::bidirectional_iterator_tag iterator_category
this iterator meets the interface requirements of bidirection_iterator
Definition: range_iterator.hpp:169
std::bidirectional_iterator_tag iterator_category
iterator tags required for use with standard algorithms and adapters
Definition: range_iterator.hpp:49
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
An iterator used by worksheet and range for traversing a 2D grid of cells by row/column then across t...
Definition: range_iterator.hpp:43
A cell vector is a linear (1D) range of cells, either vertical or horizontal depending on the major o...
Definition: cell_vector.hpp:47
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