some rather tricky adjustments to PtrDerefIter

...allowing to use that custom iterator together
with STL algorithms
This commit is contained in:
Fischlurch 2011-11-19 17:46:24 +01:00
parent ca689ae5e0
commit 1f966a02ec
2 changed files with 40 additions and 4 deletions

View file

@ -408,7 +408,7 @@ namespace lib {
typedef typename RemovePtr<TY>::Type ValueType;
template<class T2>
struct SimilarIter
struct SimilarIter ///< rebind to a similarly structured Iterator with value type T2
{
typedef Iter<T2,CON> Type;
};
@ -445,7 +445,12 @@ namespace lib {
public:
typedef typename IT::value_type pointer;
typedef typename RemovePtr<pointer>::Type value_type;
typedef value_type& reference;
typedef value_type& reference;
// for use with STL algorithms
typedef void difference_type;
typedef std::forward_iterator_tag iterator_category;
// the purpose of the following typedefs is to ease building a correct "const iterator"
@ -462,6 +467,7 @@ namespace lib {
/** PtrDerefIter is always created
* by wrapping an existing iterator.
*/
explicit
PtrDerefIter (IT srcIter)
: i_(srcIter)
{ }
@ -485,9 +491,31 @@ namespace lib {
operator= (PtrDerefIter<WrappedIterType> const& ref)
{
i_ = reinterpret_cast<IT const&> (ref.getBase());
return *this;
}
/** explicit builder to allow creating a const variant from the basic srcIter type.
* Again, the reason necessitating this "backdoor" is that we want to swallow one level
* of indirection. Generally speaking \code const T ** \endcode is not the same as
* \code T * const * \endcode, but in our specific case the API ensures that a
* PtrDerefIter<WrappedConstIterType> only exposes const elements.
*/
static PtrDerefIter
build_by_cast (WrappedIterType const& srcIter)
{
return PtrDerefIter (reinterpret_cast<IT const&> (srcIter));
}
static PtrDerefIter
nil()
{
return PtrDerefIter (IT());
}
/* === lumiera forward iterator concept === */

View file

@ -48,8 +48,10 @@
#include "include/logging.h"
#include "lib/iter-adapter.hpp"
#include "lib/error.hpp"
#include "lib/util.hpp"
#include <vector>
#include <algorithm>
#include <boost/noncopyable.hpp>
@ -155,9 +157,9 @@ namespace lib {
typedef ConstIterType const_iterator;
iterator begin() { return iterator (allPtrs()); }
const_iterator begin() const { return const_iterator (allPtrs()); }
const_iterator begin() const { return const_iterator::build_by_cast (allPtrs()); }
iterator end() { return iterator ( RIter() ); }
const_iterator end() const { return const_iterator (RcIter() ); }
const_iterator end() const { return const_iterator::nil(); }
@ -188,6 +190,12 @@ namespace lib {
{
return RIter (_Vec::begin(), _Vec::end());
}
RIter
allPtrs () const
{
_Vec& elements = util::unConst(*this);
return RIter (elements.begin(), elements.end());
}
};