Merge pull request #324 from Crzyrndm/iterator-default-ctors

Default ctors for iterators
This commit is contained in:
Crzyrndm 2018-08-05 13:04:09 +12:00 committed by GitHub
commit ca8c93c696
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 126 additions and 85 deletions

View File

@ -53,6 +53,11 @@ public:
using pointer = worksheet *; using pointer = worksheet *;
using reference = worksheet; // intentionally value using reference = worksheet; // intentionally value
/// <summary>
/// Default Constructs a worksheet iterator
/// </summary>
worksheet_iterator() = default;
/// <summary> /// <summary>
/// Constructs a worksheet iterator from a workbook and sheet index. /// Constructs a worksheet iterator from a workbook and sheet index.
/// </summary> /// </summary>
@ -135,12 +140,12 @@ private:
/// <summary> /// <summary>
/// The target workbook of this iterator. /// The target workbook of this iterator.
/// </summary> /// </summary>
workbook *wb_; workbook *wb_ = nullptr;
/// <summary> /// <summary>
/// The index of the worksheet in wb_ this iterator is currently pointing to. /// The index of the worksheet in wb_ this iterator is currently pointing to.
/// </summary> /// </summary>
std::size_t index_; std::size_t index_ = 0;
}; };
@ -159,6 +164,11 @@ public:
using pointer = const worksheet *; using pointer = const worksheet *;
using reference = const worksheet; // intentionally value using reference = const worksheet; // intentionally value
/// <summary>
/// Default Constructs a worksheet iterator
/// </summary>
const_worksheet_iterator() = default;
/// <summary> /// <summary>
/// Constructs a worksheet iterator from a workbook and sheet index. /// Constructs a worksheet iterator from a workbook and sheet index.
/// </summary> /// </summary>
@ -234,12 +244,12 @@ private:
/// <summary> /// <summary>
/// The target workbook of this iterator. /// The target workbook of this iterator.
/// </summary> /// </summary>
const workbook *wb_; const workbook *wb_ = nullptr;
/// <summary> /// <summary>
/// The index of the worksheet in wb_ this iterator is currently pointing to. /// The index of the worksheet in wb_ this iterator is currently pointing to.
/// </summary> /// </summary>
std::size_t index_; std::size_t index_ = 0;
}; };
} // namespace xlnt } // namespace xlnt

View File

@ -30,6 +30,7 @@
#include <xlnt/xlnt_config.hpp> #include <xlnt/xlnt_config.hpp>
#include <xlnt/cell/cell_reference.hpp> #include <xlnt/cell/cell_reference.hpp>
#include <xlnt/worksheet/range_reference.hpp> #include <xlnt/worksheet/range_reference.hpp>
#include <xlnt/worksheet/major_order.hpp>
#include <xlnt/worksheet/worksheet.hpp> #include <xlnt/worksheet/worksheet.hpp>
namespace xlnt { namespace xlnt {
@ -55,6 +56,11 @@ public:
using pointer = cell *; using pointer = cell *;
using reference = cell; // intentionally value using reference = cell; // intentionally value
/// <summary>
/// Default constructs a cell_iterator
/// </summary>
cell_iterator() = default;
/// <summary> /// <summary>
/// Constructs a cell_iterator from a worksheet, range, and iteration settings. /// Constructs a cell_iterator from a worksheet, range, and iteration settings.
/// </summary> /// </summary>
@ -132,6 +138,27 @@ public:
cell_iterator operator++(int); cell_iterator operator++(int);
private: private:
/// <summary>
/// If true, cells that don't exist in the worksheet will be skipped during iteration.
/// </summary>
bool skip_null_ = false;
/// <summary>
/// If true, when on the last column, the cursor will continue to the next row
/// (and vice versa when iterating in column-major order) until reaching the
/// bottom right corner of the range.
/// </summary>
bool wrap_ = false;
/// <summary>
/// The order this iterator will move, by column or by row. Note that
/// this has the opposite meaning as in a range_iterator because after
/// getting a row-major range_iterator, the row-major cell_iterator will
/// iterate over a column and vice versa.
/// </summary>
major_order order_ = major_order::column;
/// <summary> /// <summary>
/// The worksheet this iterator will return cells from. /// The worksheet this iterator will return cells from.
/// </summary> /// </summary>
@ -146,26 +173,6 @@ private:
/// The range of cells this iterator is restricted to /// The range of cells this iterator is restricted to
/// </summary> /// </summary>
range_reference bounds_; range_reference bounds_;
/// <summary>
/// The order this iterator will move, by column or by row. Note that
/// this has the opposite meaning as in a range_iterator because after
/// getting a row-major range_iterator, the row-major cell_iterator will
/// iterate over a column and vice versa.
/// </summary>
major_order order_;
/// <summary>
/// If true, cells that don't exist in the worksheet will be skipped during iteration.
/// </summary>
bool skip_null_;
/// <summary>
/// If true, when on the last column, the cursor will continue to the next row
/// (and vice versa when iterating in column-major order) until reaching the
/// bottom right corner of the range.
/// </summary>
bool wrap_;
}; };
/// <summary> /// <summary>
@ -183,6 +190,11 @@ public:
using pointer = const cell *; using pointer = const cell *;
using reference = const cell; // intentionally value using reference = const cell; // intentionally value
/// <summary>
/// Default constructs a cell_iterator
/// </summary>
const_cell_iterator() = default;
/// <summary> /// <summary>
/// Constructs a cell_iterator from a worksheet, range, and iteration settings. /// Constructs a cell_iterator from a worksheet, range, and iteration settings.
/// </summary> /// </summary>
@ -254,6 +266,26 @@ public:
const_cell_iterator operator++(int); const_cell_iterator operator++(int);
private: private:
/// <summary>
/// If true, cells that don't exist in the worksheet will be skipped during iteration.
/// </summary>
bool skip_null_ = false;
/// <summary>
/// If true, when on the last column, the cursor will continue to the next row
/// (and vice versa when iterating in column-major order) until reaching the
/// bottom right corner of the range.
/// </summary>
bool wrap_ = false;
/// <summary>
/// The order this iterator will move, by column or by row. Note that
/// this has the opposite meaning as in a range_iterator because after
/// getting a row-major range_iterator, the row-major cell_iterator will
/// iterate over a column and vice versa.
/// </summary>
major_order order_ = major_order::column;
/// <summary> /// <summary>
/// The worksheet this iterator will return cells from. /// The worksheet this iterator will return cells from.
/// </summary> /// </summary>
@ -268,26 +300,6 @@ private:
/// The range of cells this iterator is restricted to /// The range of cells this iterator is restricted to
/// </summary> /// </summary>
range_reference bounds_; range_reference bounds_;
/// <summary>
/// The order this iterator will move, by column or by row. Note that
/// this has the opposite meaning as in a range_iterator because after
/// getting a row-major range_iterator, the row-major cell_iterator will
/// iterate over a column and vice versa.
/// </summary>
major_order order_;
/// <summary>
/// If true, cells that don't exist in the worksheet will be skipped during iteration.
/// </summary>
bool skip_null_;
/// <summary>
/// If true, when on the last column, the cursor will continue to the next row
/// (and vice versa when iterating in column-major order) until reaching the
/// bottom right corner of the range.
/// </summary>
bool wrap_;
}; };
} // namespace xlnt } // namespace xlnt

View File

@ -51,6 +51,11 @@ public:
using pointer = cell_vector *; using pointer = cell_vector *;
using reference = cell_vector; // intentionally value using reference = cell_vector; // intentionally value
/// <summary>
/// Default constructs a range iterator
/// </summary>
range_iterator() = default;
/// <summary> /// <summary>
/// Constructs a range iterator on a worksheet, cell pointing to the current /// Constructs a range iterator on a worksheet, cell pointing to the current
/// row or column, range bounds, an order, and whether or not to skip null column/rows. /// row or column, range bounds, an order, and whether or not to skip null column/rows.
@ -124,6 +129,16 @@ public:
range_iterator operator++(int); range_iterator operator++(int);
private: private:
/// <summary>
/// If true, empty rows and cells will be skipped when iterating with this iterator
/// </summary>
bool skip_null_ = false;
/// <summary>
/// Whether rows or columns should be iterated over first
/// </summary>
major_order order_ = major_order::column;
/// <summary> /// <summary>
/// The worksheet /// The worksheet
/// </summary> /// </summary>
@ -138,16 +153,6 @@ private:
/// The bounds of the range /// The bounds of the range
/// </summary> /// </summary>
range_reference bounds_; range_reference bounds_;
/// <summary>
/// Whether rows or columns should be iterated over first
/// </summary>
major_order order_;
/// <summary>
/// If true, empty rows and cells will be skipped when iterating with this iterator
/// </summary>
bool skip_null_;
}; };
/// <summary> /// <summary>
@ -166,6 +171,11 @@ public:
using pointer = const cell_vector *; using pointer = const cell_vector *;
using reference = const cell_vector; // intentionally value using reference = const cell_vector; // intentionally value
/// <summary>
/// Default constructs a range iterator
/// </summary>
const_range_iterator() = default;
/// <summary> /// <summary>
/// Constructs a range iterator on a worksheet, cell pointing to the current /// Constructs a range iterator on a worksheet, cell pointing to the current
/// row or column, range bounds, an order, and whether or not to skip null column/rows. /// row or column, range bounds, an order, and whether or not to skip null column/rows.
@ -234,6 +244,16 @@ public:
const_range_iterator operator++(int); const_range_iterator operator++(int);
private: private:
/// <summary>
/// If true, empty rows and cells will be skipped when iterating with this iterator
/// </summary>
bool skip_null_ = false;
/// <summary>
/// Determines whether iteration should move through rows or columns first
/// </summary>
major_order order_ = major_order::column;
/// <summary> /// <summary>
/// The implementation of the worksheet this iterator points to /// The implementation of the worksheet this iterator points to
/// </summary> /// </summary>
@ -248,16 +268,6 @@ private:
/// The range this iterator starts and ends in /// The range this iterator starts and ends in
/// </summary> /// </summary>
range_reference bounds_; range_reference bounds_;
/// <summary>
/// Determines whether iteration should move through rows or columns first
/// </summary>
major_order order_;
/// <summary>
/// If true, empty rows and cells will be skipped when iterating with this iterator
/// </summary>
bool skip_null_;
}; };
} // namespace xlnt } // namespace xlnt

View File

@ -30,12 +30,12 @@ namespace xlnt {
cell_iterator::cell_iterator(worksheet ws, const cell_reference &cursor, cell_iterator::cell_iterator(worksheet ws, const cell_reference &cursor,
const range_reference &bounds, major_order order, bool skip_null, bool wrap) const range_reference &bounds, major_order order, bool skip_null, bool wrap)
: ws_(ws), : skip_null_(skip_null),
cursor_(cursor), wrap_(wrap),
bounds_(bounds),
order_(order), order_(order),
skip_null_(skip_null), ws_(ws),
wrap_(wrap) cursor_(cursor),
bounds_(bounds)
{ {
if (skip_null && !ws.has_cell(cursor_)) if (skip_null && !ws.has_cell(cursor_))
{ {
@ -45,12 +45,12 @@ cell_iterator::cell_iterator(worksheet ws, const cell_reference &cursor,
const_cell_iterator::const_cell_iterator(worksheet ws, const cell_reference &cursor, const_cell_iterator::const_cell_iterator(worksheet ws, const cell_reference &cursor,
const range_reference &bounds, major_order order, bool skip_null, bool wrap) const range_reference &bounds, major_order order, bool skip_null, bool wrap)
: ws_(ws), : skip_null_(skip_null),
cursor_(cursor), wrap_(wrap),
bounds_(bounds),
order_(order), order_(order),
skip_null_(skip_null), ws_(ws),
wrap_(wrap) cursor_(cursor),
bounds_(bounds)
{ {
if (skip_null && !ws.has_cell(cursor_)) if (skip_null && !ws.has_cell(cursor_))
{ {

View File

@ -40,11 +40,11 @@ const range_iterator::reference range_iterator::operator*() const
range_iterator::range_iterator(worksheet &ws, const cell_reference &cursor, range_iterator::range_iterator(worksheet &ws, const cell_reference &cursor,
const range_reference &bounds, major_order order, bool skip_null) const range_reference &bounds, major_order order, bool skip_null)
: ws_(ws), : skip_null_(skip_null),
cursor_(cursor),
bounds_(bounds),
order_(order), order_(order),
skip_null_(skip_null) ws_(ws),
cursor_(cursor),
bounds_(bounds)
{ {
if (skip_null_ && (**this).empty()) if (skip_null_ && (**this).empty())
{ {
@ -156,11 +156,11 @@ range_iterator range_iterator::operator++(int)
const_range_iterator::const_range_iterator(const worksheet &ws, const cell_reference &cursor, const_range_iterator::const_range_iterator(const worksheet &ws, const cell_reference &cursor,
const range_reference &bounds, major_order order, bool skip_null) const range_reference &bounds, major_order order, bool skip_null)
: ws_(ws.d_), : skip_null_(skip_null),
cursor_(cursor),
bounds_(bounds),
order_(order), order_(order),
skip_null_(skip_null) ws_(ws.d_),
cursor_(cursor),
bounds_(bounds)
{ {
if (skip_null_ && (**this).empty()) if (skip_null_ && (**this).empty())
{ {

View File

@ -214,6 +214,8 @@ public:
iter = temp--; iter = temp--;
xlnt_assert_equals((*iter).title(), "Sheet2"); xlnt_assert_equals((*iter).title(), "Sheet2");
xlnt_assert_equals((*temp).title(), "Sheet1"); xlnt_assert_equals((*temp).title(), "Sheet1");
xlnt_assert_equals(xlnt::worksheet_iterator{}, xlnt::worksheet_iterator{});
} }
void test_const_iter() void test_const_iter()
@ -255,6 +257,8 @@ public:
iter = temp--; iter = temp--;
xlnt_assert_equals((*iter).title(), "Sheet2"); xlnt_assert_equals((*iter).title(), "Sheet2");
xlnt_assert_equals((*temp).title(), "Sheet1"); xlnt_assert_equals((*temp).title(), "Sheet1");
xlnt_assert_equals(xlnt::const_worksheet_iterator{}, xlnt::const_worksheet_iterator{});
} }
void test_get_index() void test_get_index()

View File

@ -27,10 +27,9 @@
#include <xlnt/cell/hyperlink.hpp> #include <xlnt/cell/hyperlink.hpp>
#include <xlnt/workbook/workbook.hpp> #include <xlnt/workbook/workbook.hpp>
#include <xlnt/worksheet/column_properties.hpp> #include <xlnt/worksheet/column_properties.hpp>
#include <xlnt/worksheet/row_properties.hpp>
#include <xlnt/worksheet/range.hpp>
#include <xlnt/worksheet/worksheet.hpp>
#include <xlnt/worksheet/header_footer.hpp> #include <xlnt/worksheet/header_footer.hpp>
#include <xlnt/worksheet/range.hpp>
#include <xlnt/worksheet/row_properties.hpp>
#include <xlnt/worksheet/worksheet.hpp> #include <xlnt/worksheet/worksheet.hpp>
#include <helpers/test_suite.hpp> #include <helpers/test_suite.hpp>
@ -608,6 +607,9 @@ public:
xlnt_assert_equals(cell.value<std::string>(), cell.reference().to_string()); xlnt_assert_equals(cell.value<std::string>(), cell.reference().to_string());
} }
} }
xlnt_assert_equals(xlnt::const_cell_iterator{}, xlnt::const_cell_iterator{});
xlnt_assert_equals(xlnt::const_range_iterator{}, xlnt::const_range_iterator{});
} }
void test_const_reverse_iterators() void test_const_reverse_iterators()
@ -1052,6 +1054,9 @@ public:
const_range_iter++; const_range_iter++;
const_range_iter--; const_range_iter--;
xlnt_assert_equals(const_range_iter, const_range.begin()); xlnt_assert_equals(const_range_iter, const_range.begin());
xlnt_assert_equals(xlnt::cell_iterator{}, xlnt::cell_iterator{});
xlnt_assert_equals(xlnt::range_iterator{}, xlnt::range_iterator{});
} }
void test_range_reference() void test_range_reference()