worksheet_iterator - rule of 5/0

For rule of 5/0, where no implementation is required, all 5 operations have been declared as defaulted. This is less likely to forget definitions for all 5 if required
- removed forwarding of copy ctor to assignment (which was defaulted already) in favour of defaulted copy ctor
- added defaulted move assignment/ctor and destructor

Changed workbook reference to a pointer to allow tests to compile (reference isn't rebindable so defaulted assignment is equivalent to deleted)
This commit is contained in:
Crzyrndm 2018-05-31 15:56:07 +12:00
parent eaf3c2a773
commit a47985b2db
2 changed files with 42 additions and 28 deletions

View File

@ -61,12 +61,27 @@ 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.
/// </summary>
worksheet_iterator &operator=(const worksheet_iterator &);
worksheet_iterator &operator=(const worksheet_iterator &) = default;
/// <summary>
/// Copy constructs a worksheet iterator from another iterator.
/// </summary>
worksheet_iterator(worksheet_iterator &&) = default;
/// <summary>
/// Assigns the iterator so that it points to the same worksheet in the same workbook.
/// </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.
@ -101,7 +116,7 @@ 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.
@ -133,12 +148,27 @@ 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.
/// </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 another iterator.
/// </summary>
const_worksheet_iterator(const_worksheet_iterator &&) = default;
/// <summary>
/// Assigns the iterator from a temporary
/// </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.
@ -173,7 +203,7 @@ 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

@ -28,18 +28,13 @@
namespace xlnt {
worksheet_iterator::worksheet_iterator(workbook &wb, std::size_t index)
: wb_(wb), index_(index)
{
}
worksheet_iterator::worksheet_iterator(const worksheet_iterator &rhs)
: wb_(rhs.wb_), index_(rhs.index_)
: wb_(&wb), index_(index)
{
}
worksheet worksheet_iterator::operator*()
{
return wb_[index_];
return (*wb_)[index_];
}
worksheet_iterator &worksheet_iterator::operator++()
@ -50,7 +45,7 @@ worksheet_iterator &worksheet_iterator::operator++()
worksheet_iterator worksheet_iterator::operator++(int)
{
worksheet_iterator old(wb_, index_);
worksheet_iterator old(*wb_, index_);
++*this;
return old;
}
@ -65,25 +60,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)
{
}
const_worksheet_iterator::const_worksheet_iterator(const const_worksheet_iterator &rhs)
: wb_(rhs.wb_), index_(rhs.index_)
: wb_(&wb), index_(index)
{
}
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,7 +78,7 @@ 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;
}