TLA Line data Source code
1 : // Copyright (c) 2014-2022 Thomas Fussell
2 : // Copyright (c) 2010-2015 openpyxl
3 : // Copyright (c) 2024-2025 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 : #pragma once
26 :
27 : #include <cstddef>
28 : #include <iterator>
29 :
30 : #include <xlnt/xlnt_config.hpp>
31 :
32 : namespace xlnt {
33 :
34 : class workbook;
35 : class worksheet;
36 :
37 : // Note: There are const and non-const implementations of this iterator
38 : // because one needs to point at a const workbook and the other needs
39 : // to point at a non-const workbook stored as a member variable, respectively.
40 :
41 : /// <summary>
42 : /// An iterator which is used to iterate over the worksheets in a workbook.
43 : /// </summary>
44 : class XLNT_API worksheet_iterator
45 : {
46 : public:
47 : /// <summary>
48 : /// iterator tags required for use with standard algorithms and adapters
49 : /// </summary>
50 : using iterator_category = std::bidirectional_iterator_tag;
51 : using value_type = worksheet;
52 : using difference_type = std::ptrdiff_t;
53 : using pointer = worksheet *;
54 : using reference = worksheet; // intentionally value
55 :
56 : /// <summary>
57 : /// Default Constructs a worksheet iterator
58 : /// </summary>
59 CBC 2 : worksheet_iterator() = default;
60 :
61 : /// <summary>
62 : /// Constructs a worksheet iterator from a workbook and sheet index.
63 : /// </summary>
64 : worksheet_iterator(workbook &wb, std::size_t index);
65 :
66 : /// <summary>
67 : /// Copy constructs a worksheet iterator from another iterator.
68 : /// </summary>
69 : worksheet_iterator(const worksheet_iterator &) = default;
70 :
71 : /// <summary>
72 : /// Copy assigns the iterator so that it points to the same worksheet in the same workbook.
73 : /// </summary>
74 : worksheet_iterator &operator=(const worksheet_iterator &) = default;
75 :
76 : /// <summary>
77 : /// Move constructs a worksheet iterator from a temporary iterator.
78 : /// </summary>
79 : worksheet_iterator(worksheet_iterator &&) = default;
80 :
81 : /// <summary>
82 : /// Move assign the iterator from a temporary iterator
83 : /// </summary>
84 : worksheet_iterator &operator=(worksheet_iterator &&) = default;
85 :
86 : /// <summary>
87 : /// Default destructor
88 : /// </summary>
89 : ~worksheet_iterator() = default;
90 :
91 : /// <summary>
92 : /// Dereferences the iterator to return the worksheet it is pointing to.
93 : /// If the iterator points to one-past-the-end of the workbook, an invalid_parameter
94 : /// exception will be thrown.
95 : /// </summary>
96 : reference operator*();
97 :
98 : /// <summary>
99 : /// Dereferences the iterator to return the worksheet it is pointing to.
100 : /// If the iterator points to one-past-the-end of the workbook, an invalid_parameter
101 : /// exception will be thrown.
102 : /// </summary>
103 : const reference operator*() const;
104 :
105 : /// <summary>
106 : /// Returns true if this iterator points to the same worksheet as comparand.
107 : /// </summary>
108 : bool operator==(const worksheet_iterator &comparand) const;
109 :
110 : /// <summary>
111 : /// Returns true if this iterator doesn't point to the same worksheet as comparand.
112 : /// </summary>
113 : bool operator!=(const worksheet_iterator &comparand) const;
114 :
115 : /// <summary>
116 : /// Post-increment the iterator's internal workseet index. Returns a copy of the
117 : /// iterator as it was before being incremented.
118 : /// </summary>
119 : worksheet_iterator operator++(int);
120 :
121 : /// <summary>
122 : /// Pre-increment the iterator's internal workseet index. Returns a refernce
123 : /// to the same iterator.
124 : /// </summary>
125 : worksheet_iterator &operator++();
126 :
127 : /// <summary>
128 : /// Post-decrement the iterator's internal workseet index. Returns a copy of the
129 : /// iterator as it was before being incremented.
130 : /// </summary>
131 : worksheet_iterator operator--(int);
132 :
133 : /// <summary>
134 : /// Pre-decrement the iterator's internal workseet index. Returns a refernce
135 : /// to the same iterator.
136 : /// </summary>
137 : worksheet_iterator &operator--();
138 :
139 : private:
140 : /// <summary>
141 : /// The target workbook of this iterator.
142 : /// </summary>
143 : workbook *wb_ = nullptr;
144 :
145 : /// <summary>
146 : /// The index of the worksheet in wb_ this iterator is currently pointing to.
147 : /// </summary>
148 : std::size_t index_ = 0;
149 : };
150 :
151 : /// <summary>
152 : /// An iterator which is used to iterate over the worksheets in a const workbook.
153 : /// </summary>
154 : class XLNT_API const_worksheet_iterator
155 : {
156 : public:
157 : /// <summary>
158 : /// iterator tags required for use with standard algorithms and adapters
159 : /// </summary>
160 : using iterator_category = std::bidirectional_iterator_tag;
161 : using value_type = const worksheet;
162 : using difference_type = std::ptrdiff_t;
163 : using pointer = const worksheet *;
164 : using reference = const worksheet; // intentionally value
165 :
166 : /// <summary>
167 : /// Default Constructs a worksheet iterator
168 : /// </summary>
169 2 : const_worksheet_iterator() = default;
170 :
171 : /// <summary>
172 : /// Constructs a worksheet iterator from a workbook and sheet index.
173 : /// </summary>
174 : const_worksheet_iterator(const workbook &wb, std::size_t index);
175 :
176 : /// <summary>
177 : /// Copy constructs a worksheet iterator from another iterator.
178 : /// </summary>
179 : const_worksheet_iterator(const const_worksheet_iterator &) = default;
180 :
181 : /// <summary>
182 : /// Copy assigns the iterator so that it points to the same worksheet in the same workbook.
183 : /// </summary>
184 : const_worksheet_iterator &operator=(const const_worksheet_iterator &) = default;
185 :
186 : /// <summary>
187 : /// Move constructs a worksheet iterator from a temporary iterator.
188 : /// </summary>
189 : const_worksheet_iterator(const_worksheet_iterator &&) = default;
190 :
191 : /// <summary>
192 : /// Move assigns the iterator from a temporary iterator
193 : /// </summary>
194 : const_worksheet_iterator &operator=(const_worksheet_iterator &&) = default;
195 :
196 : /// <summary>
197 : /// Default destructor
198 : /// </summary>
199 : ~const_worksheet_iterator() = default;
200 :
201 : /// <summary>
202 : /// Dereferences the iterator to return the worksheet it is pointing to.
203 : /// If the iterator points to one-past-the-end of the workbook, an invalid_parameter
204 : /// exception will be thrown.
205 : /// </summary>
206 : const reference operator*() const;
207 :
208 : /// <summary>
209 : /// Returns true if this iterator points to the same worksheet as comparand.
210 : /// </summary>
211 : bool operator==(const const_worksheet_iterator &comparand) const;
212 :
213 : /// <summary>
214 : /// Returns true if this iterator doesn't point to the same worksheet as comparand.
215 : /// </summary>
216 : bool operator!=(const const_worksheet_iterator &comparand) const;
217 :
218 : /// <summary>
219 : /// Post-increment the iterator's internal workseet index. Returns a copy of the
220 : /// iterator as it was before being incremented.
221 : /// </summary>
222 : const_worksheet_iterator operator++(int);
223 :
224 : /// <summary>
225 : /// Pre-increment the iterator's internal workseet index. Returns a refernce
226 : /// to the same iterator.
227 : /// </summary>
228 : const_worksheet_iterator &operator++();
229 :
230 : /// <summary>
231 : /// Post-decrement the iterator's internal workseet index. Returns a copy of the
232 : /// iterator as it was before being incremented.
233 : /// </summary>
234 : const_worksheet_iterator operator--(int);
235 :
236 : /// <summary>
237 : /// Pre-decrement the iterator's internal workseet index. Returns a refernce
238 : /// to the same iterator.
239 : /// </summary>
240 : const_worksheet_iterator &operator--();
241 :
242 : private:
243 : /// <summary>
244 : /// The target workbook of this iterator.
245 : /// </summary>
246 : const workbook *wb_ = nullptr;
247 :
248 : /// <summary>
249 : /// The index of the worksheet in wb_ this iterator is currently pointing to.
250 : /// </summary>
251 : std::size_t index_ = 0;
252 : };
253 :
254 : } // namespace xlnt
|