Skip to content

Commit

Permalink
Merge pull request tfussell#608 from imgspc/iter_has_value
Browse files Browse the repository at this point in the history
Add has_value to cell_iterator
  • Loading branch information
tfussell authored Jan 9, 2022
2 parents 0246c7b + 5ff9808 commit d88c901
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/xlnt/worksheet/cell_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ class XLNT_API cell_iterator
/// </summary>
cell_iterator operator++(int);

/// <summary>
/// When iterating over a range that doesn't ignore null cells, operator*()
/// will throw when trying to access the cells that are null. This method
/// checks the existence of a cell.
/// </summary>
bool has_value() const;

private:
/// <summary>
/// If true, cells that don't exist in the worksheet will be skipped during iteration.
Expand Down Expand Up @@ -263,6 +270,13 @@ class XLNT_API const_cell_iterator
/// </summary>
const_cell_iterator operator++(int);

/// <summary>
/// When iterating over a range that doesn't ignore null cells, operator*()
/// will throw when trying to access the cells that are null. This method
/// checks the existence of a cell.
/// </summary>
bool has_value() const;

private:
/// <summary>
/// If true, cells that don't exist in the worksheet will be skipped during iteration.
Expand Down
8 changes: 8 additions & 0 deletions source/worksheet/cell_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,12 @@ const const_cell_iterator::reference const_cell_iterator::operator*() const
{
return ws_.cell(cursor_);
}

bool cell_iterator::has_value() const{
return ws_.has_cell(cursor_);
}

bool const_cell_iterator::has_value() const{
return ws_.has_cell(cursor_);
}
} // namespace xlnt
26 changes: 26 additions & 0 deletions tests/worksheet/worksheet_test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class worksheet_test_suite : public test_suite
register_test(test_lowest_row_or_props);
register_test(test_highest_row);
register_test(test_highest_row_or_props);
register_test(test_iterator_has_value);
register_test(test_const_iterators);
register_test(test_const_reverse_iterators);
register_test(test_column_major_iterators);
Expand Down Expand Up @@ -586,6 +587,31 @@ class worksheet_test_suite : public test_suite
xlnt_assert_equals(ws.highest_row_or_props(), 11);
}

void test_iterator_has_value()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();

// make a worksheet with a blank row and column
ws.cell("A1").value("A1");
ws.cell("A3").value("A3");
ws.cell("C1").value("C1");

xlnt_assert_equals(ws.columns(false)[0].begin().has_value(), true);
xlnt_assert_equals(ws.rows(false)[0].begin().has_value(), true);

xlnt_assert_equals(ws.columns(false)[1].begin().has_value(), false);
xlnt_assert_equals(ws.rows(false)[1].begin().has_value(), false);

// also test const interators.
xlnt_assert_equals(ws.columns(false)[0].cbegin().has_value(), true);
xlnt_assert_equals(ws.rows(false)[0].cbegin().has_value(), true);

xlnt_assert_equals(ws.columns(false)[1].cbegin().has_value(), false);
xlnt_assert_equals(ws.rows(false)[1].cbegin().has_value(), false);

}

void test_const_iterators()
{
xlnt::workbook wb;
Expand Down

0 comments on commit d88c901

Please sign in to comment.