OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLRowData.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLROWDATA_HPP
12#define OPENXLSX_XLROWDATA_HPP
13
14#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
15# pragma warning(push)
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
18#endif // _MSC_VER
19
20// ===== External Includes ===== //
21#include <deque>
22#include <iterator>
23#include <list>
24#include <memory>
25#include <vector>
26
27// ===== OpenXLSX Includes ===== //
28#include "OpenXLSX-Exports.hpp"
29#include "XLCell.hpp"
30#include "XLConstants.hpp"
31#include "XLException.hpp"
32#include "XLIterator.hpp"
33#include "XLXmlParser.hpp"
34
35namespace OpenXLSX
36{
37 class XLRow;
38 class XLRowDataRange;
39
44 class OPENXLSX_EXPORT XLRowDataIterator
45 {
46 friend class XLRowDataRange;
47
48 public:
49 using iterator_category = std::forward_iterator_tag;
51 using difference_type = int64_t;
52 using pointer = XLCell*;
53 using reference = XLCell&;
54
59
65
71
78
85
91
96 XLRowDataIterator operator++(int); // NOLINT
97
103
109
115 bool operator==(const XLRowDataIterator& rhs) const;
116
122 bool operator!=(const XLRowDataIterator& rhs) const;
123
124 private:
130 XLRowDataIterator(const XLRowDataRange& rowDataRange, XLIteratorLocation loc);
131
132 std::unique_ptr<XLRowDataRange> m_dataRange;
133 XMLNode m_cellNode;
134 XLCell m_currentCell;
135 };
136
140 class OPENXLSX_EXPORT XLRowDataRange
141 {
142 friend class XLRowDataIterator;
143 friend class XLRowDataProxy;
144 friend class XLRow;
145
146 public:
151 XLRowDataRange(const XLRowDataRange& other);
152
158
163
170
177
182 uint16_t size() const;
183
189
195
203 template<typename T,
204 typename = std::enable_if_t<
205 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
206 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>, const char*> ||
207 std::is_same_v<std::decay_t<T>, char*> or std::is_same_v<T, XLDateTime>>>
209 {
210 // forward implementation to templated XLCellValue& XLCellValue::operator=(T value)
211 for (auto it = begin(); it != end(); ++it) it->value() = value;
212 return *this;
213 }
214
215 private:
223 explicit XLRowDataRange(const XMLNode& rowNode, uint16_t firstColumn, uint16_t lastColumn, const XLSharedStrings& sharedStrings);
224
228 explicit XLRowDataRange();
229
230 std::unique_ptr<XMLNode> m_rowNode;
231 uint16_t m_firstCol{1};
232 uint16_t m_lastCol{1};
233 XLSharedStringsRef m_sharedStrings;
234 };
235
240 class OPENXLSX_EXPORT XLRowDataProxy
241 {
242 friend class XLRow;
243
244 public:
249
255 XLRowDataProxy& operator=(const XLRowDataProxy& other);
256
262 XLRowDataProxy& operator=(const std::vector<XLCellValue>& values);
263
269 XLRowDataProxy& operator=(const std::vector<bool>& values);
270
278 template<typename T,
279 typename = std::enable_if_t<!std::is_same_v<T, XLRowDataProxy> &&
280 std::is_base_of_v<std::bidirectional_iterator_tag,
281 typename std::iterator_traits<typename T::iterator>::iterator_category>,
282 T>>
283 XLRowDataProxy& operator=(const T& values) // 2024-04-30: whitespace support
284 {
285 if (values.size() > MAX_COLS) throw XLOverflowError("Container size exceeds maximum number of columns.");
286 if (values.size() == 0) return *this;
287
288 // ===== If the container value_type is XLCellValue, the values can be copied directly.
289 if constexpr (std::is_same_v<typename T::value_type, XLCellValue>) {
290 // ===== First, delete the values in the first N columns.
291 deleteCellValues(static_cast<uint16_t>(values.size())); // 2024-04-30: whitespace support
292
293 // ===== Then, prepend new cell nodes to current row node
294 auto colNo = values.size();
295 for (auto value = values.rbegin(); value != values.rend(); ++value) { // NOLINT
296 prependCellValue(*value,
297 static_cast<uint16_t>(colNo)); // 2024-04-30: whitespace support: this is safe because only
298 // prependCellValue (with whitespace support) touches the row data
299 --colNo;
300 }
301 }
302
303 // ===== If the container value_type is a POD type, use the overloaded operator= on each cell.
304 else {
305 auto range = XLRowDataRange(*m_rowNode, 1, static_cast<uint16_t>(values.size()), getSharedStrings());
306 auto dst = range.begin(); // 2024-04-30: whitespace support: safe because XLRowDataRange::begin invokes whitespace-safe
307 // getCellNode for column 1
308 auto src = values.begin();
309
310 while (true) {
311 dst->value() = *src;
312 ++src;
313 if (src == values.end()) break;
314 ++dst; // 2024-04-30: whitespace support: XLRowDataIterator::operator++ is whitespace-safe
315 }
316 }
317
318 return *this;
319 }
320
321 // // BEGIN working template header
322 // template<
323 // typename T,
324 // typename std::enable_if<
325 // !std::is_same_v< T, XLRowDataProxy >
326 // and std::is_base_of_v< XMLNode, T >,
327 // T
328 // >::type* = nullptr
329 // >
330 // // END working template header
331 // XLRowDataProxy& operator=(const T& values)
332 // {
333 // using namespace std::literals::string_literals;
334 // throw XLInternalError( "templated XLRowDataProxy& operator=(const T& values) instantiated for an XMLNode ("s+ typeid(T).name() +
335 // "), this function must be implemented then"s );
336 //
337 // }
338
343 operator std::vector<XLCellValue>() const; // NOLINT
344
349 operator std::deque<XLCellValue>() const; // NOLINT
350
355 operator std::list<XLCellValue>() const; // NOLINT
356
365 template<typename Container,
366 typename =
367 std::enable_if_t<!std::is_same_v<Container, XLRowDataProxy> &&
368 std::is_base_of_v<std::bidirectional_iterator_tag,
369 typename std::iterator_traits<typename Container::iterator>::iterator_category>,
370 Container>>
371 explicit operator Container() const
372 { return convertContainer<Container>(); }
373
377 void clear();
378
379 private:
380 //---------- Private Member Functions ---------- //
381
387 XLRowDataProxy(XLRow* row, XMLNode* rowNode);
388
394 XLRowDataProxy(const XLRowDataProxy& other);
395
401 XLRowDataProxy(XLRowDataProxy&& other) noexcept;
402
409 XLRowDataProxy& operator=(XLRowDataProxy&& other) noexcept;
410
415 std::vector<XLCellValue> getValues() const;
416
422 const XLSharedStrings& getSharedStrings() const;
423
428 void deleteCellValues(uint16_t count);
429
435 void prependCellValue(const XLCellValue& value, uint16_t col);
436
446 template<typename Container,
447 typename =
448 std::enable_if_t<!std::is_same_v<Container, XLRowDataProxy> &&
449 std::is_base_of_v<std::bidirectional_iterator_tag,
450 typename std::iterator_traits<typename Container::iterator>::iterator_category>,
451 Container>>
452 Container convertContainer() const // 2024-04-30: whitespace support
453 {
454 Container c;
455 auto it = std::inserter(c, c.end());
456 for (const auto& v : getValues()) {
457 // ===== If the value_type of the container is XLCellValue, the value can be assigned directly.
458 if constexpr (std::is_same_v<typename Container::value_type, XLCellValue>) *it++ = v;
459
460 // ===== If the value_type is something else, the underlying value has to be extracted from the XLCellValue object.
461 // ===== Note that if the type contained in the XLCellValue object does not match the value_type, a bad_variant_access
462 // ===== exception will be thrown.
463 else
464 *it++ = v.get<typename Container::value_type>();
465 }
466 return c;
467 }
468
469 //---------- Private Member Variables ---------- //
470
471 XLRow* m_row{nullptr};
472 XMLNode* m_rowNode{nullptr};
473 };
474
475} // namespace OpenXLSX
476
477#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
478# pragma warning(pop)
479#endif // _MSC_VER
480
481#endif // OPENXLSX_XLROWDATA_HPP
Class encapsulating a cell value.
Definition XLCellValue.hpp:89
An implementation class encapsulating the properties and behaviours of a spreadsheet cell.
Definition XLCell.hpp:51
Definition XLException.hpp:42
std::forward_iterator_tag iterator_category
Definition XLRowData.hpp:49
bool operator!=(const XLRowDataIterator &rhs) const
Non-equality operator.
Definition XLRowData.cpp:77
XLCell value_type
Definition XLRowData.hpp:50
XLRowDataIterator(const XLRowDataIterator &other)
Copy constructor.
Definition XLRowData.cpp:26
friend class XLRowDataRange
Definition XLRowData.hpp:46
XLRowDataIterator & operator=(const XLRowDataIterator &other)
Copy assignment operator.
Definition XLRowData.cpp:32
XLRowDataIterator & operator++()
Pre-increment of the iterator.
Definition XLRowData.cpp:41
XLCell & reference
Definition XLRowData.hpp:53
pointer operator->()
Arrow operator.
Definition XLRowData.cpp:70
reference operator*()
Dereferencing operator.
Definition XLRowData.cpp:69
int64_t difference_type
Definition XLRowData.hpp:51
bool operator==(const XLRowDataIterator &rhs) const
Equality operator.
Definition XLRowData.cpp:71
XLRowDataIterator(XLRowDataIterator &&other) noexcept
Move constructor.
XLRowDataIterator & operator=(XLRowDataIterator &&other) noexcept
Move assignment operator.
XLCell * pointer
Definition XLRowData.hpp:52
The XLRowDataProxy is used as a proxy object when getting or setting row data. The class facilitates ...
Definition XLRowData.hpp:241
~XLRowDataProxy()
Destructor.
XLRowDataProxy & operator=(const XLRowDataProxy &other)
Copy assignment operator.
Definition XLRowData.cpp:124
friend class XLRow
Definition XLRowData.hpp:242
XLRowDataProxy & operator=(const T &values)
Templated assignment operator taking any container supporting bidirectional iterators.
Definition XLRowData.hpp:283
This class encapsulates the concept of a contiguous range of cells in a row.
Definition XLRowData.hpp:141
XLRowDataRange(const XLRowDataRange &other)
Copy constructor.
Definition XLRowData.cpp:99
XLRowDataRange(XLRowDataRange &&other) noexcept
Move constructor.
~XLRowDataRange()
Destructor.
XLRowDataIterator begin()
Get an iterator to the first element.
Definition XLRowData.cpp:117
XLRowDataRange & operator=(T value)
Templated assignment operator - assign value to all existing cells in the row.
Definition XLRowData.hpp:208
uint16_t size() const
Get the size (length) of the range.
Definition XLRowData.cpp:116
XLRowDataRange & operator=(XLRowDataRange &&other) noexcept
Move assignment operator.
XLRowDataRange & operator=(const XLRowDataRange &other)
Copy assignment operator.
Definition XLRowData.cpp:107
XLRowDataIterator end()
Get an iterator to (one-past) the last element.
Definition XLRowData.cpp:119
friend class XLRowDataIterator
Definition XLRowData.hpp:142
friend class XLRow
Definition XLRowData.hpp:144
friend class XLRowDataProxy
Definition XLRowData.hpp:143
The XLRow class represent a row in an Excel spreadsheet. Using XLRow objects, various row formatting ...
Definition XLRow.hpp:34
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:78
Definition IXLCellProvider.hpp:24
XLIteratorLocation
Definition XLIterator.hpp:17
constexpr uint16_t MAX_COLS
Definition XLConstants.hpp:18
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
std::reference_wrapper< const XLSharedStrings > XLSharedStringsRef
Definition XLSharedStrings.hpp:67