Merge branch 'dev' into dev-Issue298

This commit is contained in:
Crzyrndm 2018-07-13 11:21:13 +12:00 committed by GitHub
commit 1ab25fa7fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 357 additions and 122 deletions

View File

@ -37,18 +37,22 @@ class worksheet;
// because one needs to point at a const workbook and the other needs
// to point at a non-const workbook stored as a member variable, respectively.
/// <summary>
/// Alias the parent class of this iterator to increase clarity.
/// </summary>
using ws_iter_type = std::iterator<std::bidirectional_iterator_tag,
worksheet, std::ptrdiff_t, worksheet *, worksheet>;
/// <summary>
/// An iterator which is used to iterate over the worksheets in a workbook.
/// </summary>
class XLNT_API worksheet_iterator : public ws_iter_type
class XLNT_API worksheet_iterator
{
public:
/// <summary>
/// iterator tags required for use with standard algorithms and adapters
/// </summary>
using iterator_category = std::bidirectional_iterator_tag;
using value_type = worksheet;
using difference_type = std::ptrdiff_t;
using pointer = worksheet *;
using reference = worksheet; // intentionally value
/// <summary>
/// Constructs a worksheet iterator from a workbook and sheet index.
/// </summary>
@ -57,19 +61,41 @@ public:
/// <summary>
/// Copy constructs a worksheet iterator from another iterator.
/// </summary>
worksheet_iterator(const worksheet_iterator &);
worksheet_iterator(const worksheet_iterator &) = default;
/// <summary>
/// Assigns the iterator so that it points to the same worksheet in the same workbook.
/// Copy assigns the iterator so that it points to the same worksheet in the same workbook.
/// </summary>
worksheet_iterator &operator=(const worksheet_iterator &);
worksheet_iterator &operator=(const worksheet_iterator &) = default;
/// <summary>
/// Move constructs a worksheet iterator from a temporary iterator.
/// </summary>
worksheet_iterator(worksheet_iterator &&) = default;
/// <summary>
/// Move assign the iterator from a temporary iterator
/// </summary>
worksheet_iterator &operator=(worksheet_iterator &&) = default;
/// <summary>
/// Default destructor
/// </summary>
~worksheet_iterator() = default;
/// <summary>
/// Dereferences the iterator to return the worksheet it is pointing to.
/// If the iterator points to one-past-the-end of the workbook, an invalid_parameter
/// exception will be thrown.
/// </summary>
worksheet operator*();
reference operator*();
/// <summary>
/// Dereferences the iterator to return the worksheet it is pointing to.
/// If the iterator points to one-past-the-end of the workbook, an invalid_parameter
/// exception will be thrown.
/// </summary>
const reference operator*() const;
/// <summary>
/// Returns true if this iterator points to the same worksheet as comparand.
@ -93,11 +119,23 @@ public:
/// </summary>
worksheet_iterator &operator++();
/// <summary>
/// Post-decrement the iterator's internal workseet index. Returns a copy of the
/// iterator as it was before being incremented.
/// </summary>
worksheet_iterator operator--(int);
/// <summary>
/// Pre-decrement the iterator's internal workseet index. Returns a refernce
/// to the same iterator.
/// </summary>
worksheet_iterator &operator--();
private:
/// <summary>
/// The target workbook of this iterator.
/// </summary>
workbook &wb_;
workbook *wb_;
/// <summary>
/// The index of the worksheet in wb_ this iterator is currently pointing to.
@ -106,18 +144,21 @@ private:
};
/// <summary>
/// Alias the parent class of this iterator to increase clarity.
/// </summary>
using c_ws_iter_type = std::iterator<std::bidirectional_iterator_tag,
const worksheet, std::ptrdiff_t, const worksheet *, const worksheet>;
/// <summary>
/// An iterator which is used to iterate over the worksheets in a const workbook.
/// </summary>
class XLNT_API const_worksheet_iterator : public c_ws_iter_type
class XLNT_API const_worksheet_iterator
{
public:
/// <summary>
/// iterator tags required for use with standard algorithms and adapters
/// </summary>
using iterator_category = std::bidirectional_iterator_tag;
using value_type = const worksheet;
using difference_type = std::ptrdiff_t;
using pointer = const worksheet *;
using reference = const worksheet; // intentionally value
/// <summary>
/// Constructs a worksheet iterator from a workbook and sheet index.
/// </summary>
@ -126,19 +167,34 @@ public:
/// <summary>
/// Copy constructs a worksheet iterator from another iterator.
/// </summary>
const_worksheet_iterator(const const_worksheet_iterator &);
const_worksheet_iterator(const const_worksheet_iterator &) = default;
/// <summary>
/// Assigns the iterator so that it points to the same worksheet in the same workbook.
/// Copy assigns the iterator so that it points to the same worksheet in the same workbook.
/// </summary>
const_worksheet_iterator &operator=(const const_worksheet_iterator &);
const_worksheet_iterator &operator=(const const_worksheet_iterator &) = default;
/// <summary>
/// Move constructs a worksheet iterator from a temporary iterator.
/// </summary>
const_worksheet_iterator(const_worksheet_iterator &&) = default;
/// <summary>
/// Move assigns the iterator from a temporary iterator
/// </summary>
const_worksheet_iterator &operator=(const_worksheet_iterator &&) = default;
/// <summary>
/// Default destructor
/// </summary>
~const_worksheet_iterator() = default;
/// <summary>
/// Dereferences the iterator to return the worksheet it is pointing to.
/// If the iterator points to one-past-the-end of the workbook, an invalid_parameter
/// exception will be thrown.
/// </summary>
const worksheet operator*();
const reference operator*() const;
/// <summary>
/// Returns true if this iterator points to the same worksheet as comparand.
@ -162,11 +218,23 @@ public:
/// </summary>
const_worksheet_iterator &operator++();
/// <summary>
/// Post-decrement the iterator's internal workseet index. Returns a copy of the
/// iterator as it was before being incremented.
/// </summary>
const_worksheet_iterator operator--(int);
/// <summary>
/// Pre-decrement the iterator's internal workseet index. Returns a refernce
/// to the same iterator.
/// </summary>
const_worksheet_iterator &operator--();
private:
/// <summary>
/// The target workbook of this iterator.
/// </summary>
const workbook &wb_;
const workbook *wb_;
/// <summary>
/// The index of the worksheet in wb_ this iterator is currently pointing to.

View File

@ -40,18 +40,21 @@ class cell;
class cell_reference;
class range_reference;
/// <summary>
/// Alias the parent class of this iterator to increase clarity.
/// </summary>
using c_iter_type = std::iterator<std::bidirectional_iterator_tag,
cell, std::ptrdiff_t, cell *, cell>;
/// <summary>
/// A cell iterator iterates over a 1D range by row or by column.
/// </summary>
class XLNT_API cell_iterator : public c_iter_type
class XLNT_API cell_iterator
{
public:
/// <summary>
/// iterator tags required for use with standard algorithms and adapters
/// </summary>
using iterator_category = std::bidirectional_iterator_tag;
using value_type = cell;
using difference_type = std::ptrdiff_t;
using pointer = cell *;
using reference = cell; // intentionally value
/// <summary>
/// Constructs a cell_iterator from a worksheet, range, and iteration settings.
/// </summary>
@ -61,17 +64,38 @@ public:
/// <summary>
/// Constructs a cell_iterator as a copy of an existing cell_iterator.
/// </summary>
cell_iterator(const cell_iterator &other);
cell_iterator(const cell_iterator &) = default;
/// <summary>
/// Assigns this iterator to match the data in rhs.
/// </summary>
cell_iterator &operator=(const cell_iterator &rhs) = default;
cell_iterator &operator=(const cell_iterator &) = default;
/// <summary>
/// Constructs a cell_iterator by moving from a cell_iterator temporary
/// </summary>
cell_iterator(cell_iterator &&) = default;
/// <summary>
/// Assigns this iterator to from a cell_iterator temporary
/// </summary>
cell_iterator &operator=(cell_iterator &&) = default;
/// <summary>
/// destructor for const_cell_iterator
/// </summary>
~cell_iterator() = default;
/// <summary>
/// Dereferences this iterator to return the cell it points to.
/// </summary>
cell operator*();
reference operator*();
/// <summary>
/// Dereferences this iterator to return the cell it points to.
/// </summary>
const reference operator*() const;
/// <summary>
/// Returns true if this iterator is equivalent to other.
@ -135,7 +159,7 @@ private:
/// 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
@ -144,18 +168,21 @@ private:
bool wrap_;
};
/// <summary>
/// Alias the parent class of this iterator to increase clarity.
/// </summary>
using cc_iter_type = std::iterator<std::bidirectional_iterator_tag,
const cell, std::ptrdiff_t, const cell *, const cell>;
/// <summary>
/// A cell iterator iterates over a 1D range by row or by column.
/// </summary>
class XLNT_API const_cell_iterator : public cc_iter_type
class XLNT_API const_cell_iterator
{
public:
/// <summary>
/// iterator tags required for use with standard algorithms and adapters
/// </summary>
using iterator_category = std::bidirectional_iterator_tag;
using value_type = const cell;
using difference_type = std::ptrdiff_t;
using pointer = const cell *;
using reference = const cell; // intentionally value
/// <summary>
/// Constructs a cell_iterator from a worksheet, range, and iteration settings.
/// </summary>
@ -163,19 +190,34 @@ public:
const range_reference &limits, major_order order, bool skip_null, bool wrap);
/// <summary>
/// Constructs a cell_iterator as a copy of an existing cell_iterator.
/// Constructs a const_cell_iterator as a copy of an existing cell_iterator.
/// </summary>
const_cell_iterator(const const_cell_iterator &other);
const_cell_iterator(const const_cell_iterator &) = default;
/// <summary>
/// Assigns this iterator to match the data in rhs.
/// </summary>
const_cell_iterator &operator=(const const_cell_iterator &) = default;
/// <summary>
/// Constructs a const_cell_iterator by moving from a const_cell_iterator temporary
/// </summary>
const_cell_iterator(const_cell_iterator &&) = default;
/// <summary>
/// Assigns this iterator to from a const_cell_iterator temporary
/// </summary>
const_cell_iterator &operator=(const_cell_iterator &&) = default;
/// <summary>
/// destructor for const_cell_iterator
/// </summary>
~const_cell_iterator() = default;
/// <summary>
/// Dereferences this iterator to return the cell it points to.
/// </summary>
const cell operator*() const;
const reference operator*() const;
/// <summary>
/// Returns true if this iterator is equivalent to other.

View File

@ -35,19 +35,22 @@ namespace xlnt {
class cell_vector;
/// <summary>
/// Alias the parent class of this iterator to increase clarity.
/// </summary>
using r_iter_type = std::iterator<std::bidirectional_iterator_tag,
cell_vector, std::ptrdiff_t, cell_vector *, cell_vector>;
/// <summary>
/// An iterator used by worksheet and range for traversing
/// a 2D grid of cells by row/column then across that row/column.
/// </summary>
class XLNT_API range_iterator : public r_iter_type
class XLNT_API range_iterator
{
public:
/// <summary>
/// iterator tags required for use with standard algorithms and adapters
/// </summary>
using iterator_category = std::bidirectional_iterator_tag;
using value_type = cell_vector;
using difference_type = std::ptrdiff_t;
using pointer = cell_vector *;
using reference = cell_vector; // intentionally value
/// <summary>
/// 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.
@ -56,20 +59,40 @@ public:
const range_reference &bounds, major_order order, bool skip_null);
/// <summary>
/// Copy constructor.
/// Default copy constructor.
/// </summary>
range_iterator(const range_iterator &other);
/// <summary>
/// Dereference the iterator to return a column or row.
/// </summary>
cell_vector operator*() const;
range_iterator(const range_iterator &) = default;
/// <summary>
/// Default assignment operator.
/// </summary>
range_iterator &operator=(const range_iterator &) = default;
/// <summary>
/// Default move constructor.
/// </summary>
range_iterator(range_iterator &&) = default;
/// <summary>
/// Default move assignment operator.
/// </summary>
range_iterator &operator=(range_iterator &&) = default;
/// <summary>
/// Default destructor
/// </summary>
~range_iterator() = default;
/// <summary>
/// Dereference the iterator to return a column or row.
/// </summary>
reference operator*();
/// <summary>
/// Dereference the iterator to return a column or row.
/// </summary>
const reference operator*() const;
/// <summary>
/// Returns true if this iterator is equivalent to other.
/// </summary>
@ -127,19 +150,22 @@ private:
bool skip_null_;
};
/// <summary>
/// Alias the parent class of this iterator to increase clarity.
/// </summary>
using cr_iter_type = std::iterator<std::bidirectional_iterator_tag,
const cell_vector, std::ptrdiff_t, const cell_vector *, const cell_vector>;
/// <summary>
/// A const version of range_iterator which does not allow modification
/// to the dereferenced cell_vector.
/// </summary>
class XLNT_API const_range_iterator : public cr_iter_type
class XLNT_API const_range_iterator
{
public:
/// <summary>
/// this iterator meets the interface requirements of bidirection_iterator
/// </summary>
using iterator_category = std::bidirectional_iterator_tag;
using value_type = const cell_vector;
using difference_type = std::ptrdiff_t;
using pointer = const cell_vector *;
using reference = const cell_vector; // intentionally value
/// <summary>
/// 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.
@ -148,20 +174,35 @@ public:
const range_reference &bounds, major_order order, bool skip_null);
/// <summary>
/// Copy constructor.
/// Default copy constructor.
/// </summary>
const_range_iterator(const const_range_iterator &other);
/// <summary>
/// Dereferennce the iterator to return the current column/row.
/// </summary>
const cell_vector operator*() const;
const_range_iterator(const const_range_iterator &) = default;
/// <summary>
/// Default assignment operator.
/// </summary>
const_range_iterator &operator=(const const_range_iterator &) = default;
/// <summary>
/// Default move constructor.
/// </summary>
const_range_iterator(const_range_iterator &&) = default;
/// <summary>
/// Default move assignment operator.
/// </summary>
const_range_iterator &operator=(const_range_iterator &&) = default;
/// <summary>
/// Default destructor
/// </summary>
~const_range_iterator() = default;
/// <summary>
/// Dereferennce the iterator to return the current column/row.
/// </summary>
const reference operator*() const;
/// <summary>
/// Returns true if this iterator is equivalent to other.
/// </summary>

View File

@ -864,11 +864,7 @@ void xlsx_producer::write_shared_string_table(const relationship & /*rel*/)
{
write_start_element(xmlns, "si");
write_start_element(xmlns, "t");
if (string.runs().front().preserve_space)
{
write_attribute(xml::qname(xmlns_xml, "space"), "preserve");
}
write_characters(string.plain_text(), has_trailing_whitespace(string.plain_text()));
write_characters(string.plain_text(),string.runs().front().preserve_space);
write_end_element(xmlns, "t");
write_end_element(xmlns, "si");

View File

@ -28,18 +28,18 @@
namespace xlnt {
worksheet_iterator::worksheet_iterator(workbook &wb, std::size_t index)
: wb_(wb), index_(index)
: wb_(&wb), index_(index)
{
}
worksheet_iterator::worksheet_iterator(const worksheet_iterator &rhs)
: wb_(rhs.wb_), index_(rhs.index_)
worksheet_iterator::reference worksheet_iterator::operator*()
{
return (*wb_)[index_];
}
worksheet worksheet_iterator::operator*()
const worksheet_iterator::reference worksheet_iterator::operator*() const
{
return wb_[index_];
return (*wb_)[index_];
}
worksheet_iterator &worksheet_iterator::operator++()
@ -50,11 +50,24 @@ worksheet_iterator &worksheet_iterator::operator++()
worksheet_iterator worksheet_iterator::operator++(int)
{
worksheet_iterator old(wb_, index_);
worksheet_iterator old(*wb_, index_);
++*this;
return old;
}
worksheet_iterator &worksheet_iterator::operator--()
{
--index_;
return *this;
}
worksheet_iterator worksheet_iterator::operator--(int)
{
worksheet_iterator old(*wb_, index_);
--(*this);
return old;
}
bool worksheet_iterator::operator==(const worksheet_iterator &comparand) const
{
return index_ == comparand.index_ && wb_ == comparand.wb_;
@ -65,25 +78,14 @@ bool worksheet_iterator::operator!=(const worksheet_iterator &comparand) const
return !(*this == comparand);
}
worksheet_iterator &worksheet_iterator::operator=(const worksheet_iterator &other)
{
index_ = other.index_;
return *this;
}
const_worksheet_iterator::const_worksheet_iterator(const workbook &wb, std::size_t index)
: wb_(wb), index_(index)
: wb_(&wb), index_(index)
{
}
const_worksheet_iterator::const_worksheet_iterator(const const_worksheet_iterator &rhs)
: wb_(rhs.wb_), index_(rhs.index_)
const const_worksheet_iterator::reference const_worksheet_iterator::operator*() const
{
}
const worksheet const_worksheet_iterator::operator*()
{
return wb_.sheet_by_index(index_);
return wb_->sheet_by_index(index_);
}
const_worksheet_iterator &const_worksheet_iterator::operator++()
@ -94,11 +96,24 @@ const_worksheet_iterator &const_worksheet_iterator::operator++()
const_worksheet_iterator const_worksheet_iterator::operator++(int)
{
const_worksheet_iterator old(wb_, index_);
const_worksheet_iterator old(*wb_, index_);
++*this;
return old;
}
const_worksheet_iterator &const_worksheet_iterator::operator--()
{
--index_;
return *this;
}
const_worksheet_iterator const_worksheet_iterator::operator--(int)
{
const_worksheet_iterator old(*wb_, index_);
--(*this);
return old;
}
bool const_worksheet_iterator::operator==(const const_worksheet_iterator &comparand) const
{
return index_ == comparand.index_ && wb_ == comparand.wb_;

View File

@ -58,16 +58,6 @@ const_cell_iterator::const_cell_iterator(worksheet ws, const cell_reference &cur
}
}
cell_iterator::cell_iterator(const cell_iterator &other)
{
*this = other;
}
const_cell_iterator::const_cell_iterator(const const_cell_iterator &other)
{
*this = other;
}
bool cell_iterator::operator==(const cell_iterator &other) const
{
return ws_ == other.ws_
@ -275,14 +265,18 @@ const_cell_iterator const_cell_iterator::operator++(int)
return old;
}
cell cell_iterator::operator*()
cell_iterator::reference cell_iterator::operator*()
{
return ws_.cell(cursor_);
}
const cell const_cell_iterator::operator*() const
const cell_iterator::reference cell_iterator::operator*() const
{
return ws_.cell(cursor_);
}
const const_cell_iterator::reference const_cell_iterator::operator*() const
{
return ws_.cell(cursor_);
}
} // namespace xlnt

View File

@ -28,7 +28,12 @@
namespace xlnt {
cell_vector range_iterator::operator*() const
range_iterator::reference range_iterator::operator*()
{
return cell_vector(ws_, cursor_, bounds_, order_, skip_null_, false);
}
const range_iterator::reference range_iterator::operator*() const
{
return cell_vector(ws_, cursor_, bounds_, order_, skip_null_, false);
}
@ -47,11 +52,6 @@ range_iterator::range_iterator(worksheet &ws, const cell_reference &cursor,
}
}
range_iterator::range_iterator(const range_iterator &other)
{
*this = other;
}
bool range_iterator::operator==(const range_iterator &other) const
{
return ws_ == other.ws_
@ -168,11 +168,6 @@ const_range_iterator::const_range_iterator(const worksheet &ws, const cell_refer
}
}
const_range_iterator::const_range_iterator(const const_range_iterator &other)
{
*this = other;
}
bool const_range_iterator::operator==(const const_range_iterator &other) const
{
return ws_ == other.ws_
@ -274,7 +269,7 @@ const_range_iterator const_range_iterator::operator++(int)
return old;
}
const cell_vector const_range_iterator::operator*() const
const const_range_iterator::reference const_range_iterator::operator*() const
{
return cell_vector(ws_, cursor_, bounds_, order_, skip_null_, false);
}

View File

@ -46,6 +46,7 @@ public:
register_test(test_index_operator);
register_test(test_contains);
register_test(test_iter);
register_test(test_const_iter);
register_test(test_get_index);
register_test(test_get_sheet_names);
register_test(test_add_named_range);
@ -164,6 +165,89 @@ public:
{
xlnt_assert_equals(ws.title(), "Sheet1");
}
xlnt::workbook wb2;
auto iter = wb.begin();
auto iter_copy(iter); // copy ctor
xlnt_assert_equals(iter, iter_copy);
auto begin_2(wb2.begin());
xlnt_assert_differs(begin_2, iter);
iter = begin_2; // copy assign
xlnt_assert_equals(iter, begin_2);
iter = wb.begin();
iter = std::move(begin_2);
xlnt_assert_equals(iter, wb2.begin());
auto citer = wb.cbegin();
auto citer_copy(citer); // copy ctor
xlnt_assert_equals(citer, citer_copy);
auto cbegin_2(wb2.cbegin());
xlnt_assert_differs(cbegin_2, citer);
citer = cbegin_2; // copy assign
xlnt_assert_equals(citer, cbegin_2);
citer = wb.cbegin();
citer = std::move(cbegin_2);
xlnt_assert_equals(citer, wb2.cbegin());
wb2.create_sheet(); // wb2 iterators assumed invalidated
iter = wb2.begin();
xlnt_assert_equals((*iter).title(), "Sheet1");
++iter;
xlnt_assert_differs(iter, wb2.begin());
xlnt_assert_equals((*iter).title(), "Sheet2");
auto temp = --iter;
xlnt_assert_equals((*temp).title(), "Sheet1");
xlnt_assert_equals((*iter).title(), "Sheet1");
iter++;
xlnt_assert_equals((*iter).title(), "Sheet2");
temp = iter++;
xlnt_assert_equals((*temp).title(), "Sheet2");
xlnt_assert_equals(iter, wb2.end());
iter = temp--;
xlnt_assert_equals((*iter).title(), "Sheet2");
xlnt_assert_equals((*temp).title(), "Sheet1");
}
void test_const_iter()
{
const xlnt::workbook wb;
for (auto ws : wb)
{
xlnt_assert_equals(ws.title(), "Sheet1");
}
xlnt::workbook wb2;
auto iter = wb.cbegin();
auto iter_copy(iter); // copy ctor
xlnt_assert_equals(iter, iter_copy);
auto begin_2(wb2.cbegin());
xlnt_assert_differs(begin_2, iter);
iter = begin_2; // copy assign
xlnt_assert_equals(iter, begin_2);
iter = wb.cbegin();
iter = std::move(begin_2);
xlnt_assert_equals(iter, wb2.cbegin());
// wb2 iterators assumed invalidated
wb2.create_sheet();
iter = wb2.cbegin();
xlnt_assert_equals((*iter).title(), "Sheet1");
++iter;
xlnt_assert_differs(iter, wb2.cbegin());
xlnt_assert_equals((*iter).title(), "Sheet2");
auto temp = --iter;
xlnt_assert_equals((*temp).title(), "Sheet1");
xlnt_assert_equals((*iter).title(), "Sheet1");
iter++;
xlnt_assert_equals((*iter).title(), "Sheet2");
temp = iter++;
xlnt_assert_equals((*temp).title(), "Sheet2");
xlnt_assert_equals(iter, wb2.cend());
iter = temp--;
xlnt_assert_equals((*iter).title(), "Sheet2");
xlnt_assert_equals((*temp).title(), "Sheet1");
}
void test_get_index()