Simplify wxGridSelectionRange to provide only iterators

This class was a strange hybrid of a container/view/range and iterator,
as it both provided begin()/end() container-like methods and
iterator-like methods for dereferencing/advancing.

Simplify this by removing the latter part and making this class really
just a range, with its own iterator class in order to avoid leaking the
exact type of the iterator used in the API.

Note that while it's now completely trivial, it is still useful as it
isolates the application code from the vector used to store the selected
blocks currently and will allow to change internal representation in the
future without breaking the existing code.
This commit is contained in:
Vadim Zeitlin
2020-04-04 19:22:02 +02:00
parent 7231a6a855
commit 0a5a904d8d
3 changed files with 97 additions and 60 deletions

View File

@@ -19,6 +19,10 @@
#include "wx/scrolwin.h"
#if wxUSE_STD_CONTAINERS_COMPATIBLY
#include <iterator>
#endif
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
@@ -869,40 +873,45 @@ struct wxGridBlockDiffResult
};
// ----------------------------------------------------------------------------
// wxGridSelectionRange: the range of grid selection blocks
// wxGridSelectionRange: a range of grid blocks that can be iterated over
// ----------------------------------------------------------------------------
class wxGridSelectionRange
{
typedef wxVector<wxGridBlockCoords>::const_iterator iterator_impl;
public:
typedef wxVector<wxGridBlockCoords>::const_iterator iterator;
wxGridSelectionRange() :
m_begin(),
m_end(),
m_it()
class iterator
{
}
public:
#if wxUSE_STD_CONTAINERS_COMPATIBLY
typedef std::forward_iterator_tag iterator_category;
#endif
typedef ptrdiff_t difference_type;
typedef wxGridBlockCoords value_type;
typedef const value_type& reference;
// Get the current selection block coordinates.
const wxGridBlockCoords& GetBlockCoords() const
{
static wxGridBlockCoords empty;
return Valid() ? *m_it : empty;
}
iterator() : m_it() { }
// Iterate to the next block.
void Next()
{
if ( Valid() )
++m_it;
}
reference operator*() const { return *m_it; }
// Whether the iterator is valid.
bool Valid() const
{
return m_it != m_end;
}
iterator& operator++()
{ ++m_it; return *this; }
iterator operator++(int)
{ iterator tmp = *this; ++m_it; return tmp; }
bool operator==(const iterator& it) const
{ return m_it == it.m_it; }
bool operator!=(const iterator& it) const
{ return m_it != it.m_it; }
private:
explicit iterator(iterator_impl it) : m_it(it) { }
iterator_impl m_it;
friend class wxGridSelectionRange;
};
iterator begin() const
{
@@ -915,16 +924,20 @@ public:
}
private:
wxGridSelectionRange(iterator begin, iterator end) :
wxGridSelectionRange() :
m_begin(),
m_end()
{
}
wxGridSelectionRange(iterator_impl begin, iterator_impl end) :
m_begin(begin),
m_end(end),
m_it(begin)
m_end(end)
{
}
const iterator m_begin;
const iterator m_end;
iterator m_it;
friend class wxGrid;
};