iterator.h
Go to the documentation of this file.
#include <wibble/amorph.h>
#include <wibble/mixin.h>
#ifndef WIBBLE_ITERATOR_H
#define WIBBLE_ITERATOR_H
namespace wibble {
typedef bool SortabilityTag;
template< typename T, typename I >
struct IteratorTraits {
typedef SortabilityTag Unsorted;
};
template< typename T >
struct IteratorTraits< T, typename std::set< T >::iterator > {
typedef SortabilityTag Sorted;
};
template< typename T >
struct IteratorTraits< T, typename std::multiset< T >::iterator > {
typedef SortabilityTag Sorted;
};
template< typename T >
struct IteratorInterface {
virtual T current() const = 0;
virtual void advance() = 0;
virtual ~IteratorInterface() {}
};
template< typename T >
struct IteratorProxy {
IteratorProxy( T _x ) : x( _x ) {}
T x;
const T *operator->() const { return &x; }
};
template< typename T, typename W >
struct IteratorMorph : Morph< IteratorMorph< T, W >, W, IteratorInterface< T > >
{
typedef W Wrapped;
IteratorMorph() {}
IteratorMorph( const Wrapped &w )
: Morph< IteratorMorph, Wrapped, IteratorInterface< T > >( w ) {}
virtual void advance() { this->wrapped().advance(); }
virtual T current() const { return this->wrapped().current(); }
};
template< typename T, typename Self >
struct IteratorMixin : mixin::Comparable< Self >
{
Self &self() { return *static_cast< const Self * >( this ); }
const Self &self() const { return *static_cast< const Self * >( this ); }
typedef T ElementType;
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T *pointer;
typedef T &reference;
typedef const T &const_reference;
IteratorProxy< T > operator->() const {
return IteratorProxy< T >(self().current()); }
Self next() const { Self n( self() ); n.advance(); return n; }
T operator*() const { return self().current(); }
Self &operator++() { self().advance(); return self(); }
Self operator++(int) {
Self tmp = self();
self().advance();
return tmp;
}
};
template< typename T, typename I >
typename IteratorTraits< T, I >::Unsorted isSortedT( I, I ) {
return false;
}
template< typename T, typename I >
typename IteratorTraits< T, I >::Sorted isSortedT( I, I ) {
return true;
}
template< typename T >
struct Iterator : Amorph< Iterator< T >, IteratorInterface< T >, 0 >,
IteratorMixin< T, Iterator< T > >
{
typedef Amorph< Iterator< T >, IteratorInterface< T >, 0 > Super;
typedef T ElementType;
Iterator( const IteratorInterface< T > &i ) : Super( i ) {}
Iterator() {}
bool operator<=( const Iterator &i ) const { return leq( i ); }
T current() const { return this->implInterface()->current(); }
virtual void advance() { this->implInterface()->advance(); }
template< typename C > operator Iterator< C >();
};
template< typename It >
struct StlIterator : IteratorMixin< typename It::value_type, StlIterator< It > >
{
typedef typename std::iterator_traits< It >::value_type Value;
StlIterator( It i ) : m_iterator( i ) {}
virtual void advance() { ++m_iterator; }
virtual Value current() const { return *m_iterator; }
bool operator==( const StlIterator< It > &o ) { return m_iterator == o.m_iterator; }
protected:
It m_iterator;
};
template< typename I >
Iterator< typename I::value_type > iterator( I i ) {
return StlIterator< I >( i );
}
}
#endif
wibble::IteratorMixin::operator*
T operator*() const
Definition: iterator.h:73
wibble::iterator
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
wibble::IteratorMixin::pointer
T * pointer
Definition: iterator.h:66
wibble::IteratorMixin::ElementType
T ElementType
Definition: iterator.h:61
wibble::IteratorTraits
Definition: iterator.h:17
wibble::IteratorTraits< T, typename std::set< T >::iterator >::Sorted
SortabilityTag Sorted
Definition: iterator.h:23
wibble::IteratorMixin::operator++
Self & operator++()
Definition: iterator.h:75
wibble::IteratorProxy
Definition: iterator.h:39
wibble::IteratorMorph
Definition: iterator.h:46
wibble::Iterator::ElementType
T ElementType
Definition: iterator.h:98
wibble::IteratorMixin::value_type
T value_type
Definition: iterator.h:64
wibble::IteratorMorph::advance
virtual void advance()
Definition: iterator.h:52
std
wibble::SortabilityTag
bool SortabilityTag
Definition: iterator.h:14
wibble::IteratorMixin::iterator_category
std::forward_iterator_tag iterator_category
Definition: iterator.h:63
wibble::IteratorInterface
Definition: iterator.h:32
wibble::IteratorTraits::Unsorted
SortabilityTag Unsorted
Definition: iterator.h:18
wibble::IteratorInterface::~IteratorInterface
virtual ~IteratorInterface()
Definition: iterator.h:35
amorph.h
-*- C++ -*-
wibble::Iterator::Iterator
Iterator()
Definition: iterator.h:101
wibble::StlIterator::advance
virtual void advance()
Definition: iterator.h:115
wibble::IteratorMorph::IteratorMorph
IteratorMorph(const Wrapped &w)
Definition: iterator.h:50
wibble::Amorph
Definition: amorph.h:272
wibble::IteratorMixin
Definition: iterator.h:57
wibble::Iterator::advance
virtual void advance()
Definition: iterator.h:105
mixin.h
wibble::IteratorMixin::operator++
Self operator++(int)
Definition: iterator.h:76
wibble::StlIterator::Value
std::iterator_traits< It >::value_type Value
Definition: iterator.h:113
wibble::IteratorTraits< T, typename std::multiset< T >::iterator >::Sorted
SortabilityTag Sorted
Definition: iterator.h:28
wibble::IteratorMorph::current
virtual T current() const
Definition: iterator.h:53
wibble::isSortedT
IteratorTraits< T, I >::Unsorted isSortedT(I, I)
Definition: iterator.h:84
wibble::Iterator
Definition: iterator.h:94
wibble::Iterator::operator<=
bool operator<=(const Iterator &i) const
Definition: iterator.h:102
wibble::IteratorMixin::difference_type
ptrdiff_t difference_type
Definition: iterator.h:65
wibble::mixin::Comparable
Definition: mixin.h:13
wibble::IteratorMixin::reference
T & reference
Definition: iterator.h:67
wibble::Iterator::Iterator
Iterator(const IteratorInterface< T > &i)
Definition: iterator.h:100
wibble::StlIterator::current
virtual Value current() const
Definition: iterator.h:116
wibble
Definition: amorph.h:17
wibble::IteratorProxy::x
T x
Definition: iterator.h:41
wibble::IteratorProxy::operator->
const T * operator->() const
Definition: iterator.h:42
wibble::IteratorMorph::Wrapped
W Wrapped
Definition: iterator.h:48
wibble::Iterator::Super
Amorph< Iterator< T >, IteratorInterface< T >, 0 > Super
Definition: iterator.h:97
wibble::StlIterator::StlIterator
StlIterator(It i)
Definition: iterator.h:114
wibble::Iterator::current
T current() const
Definition: iterator.h:104
wibble::IteratorMorph::IteratorMorph
IteratorMorph()
Definition: iterator.h:49
wibble::IteratorMixin::operator->
IteratorProxy< T > operator->() const
Definition: iterator.h:70
wibble::IteratorMixin::next
Self next() const
Definition: iterator.h:72
wibble::IteratorProxy::IteratorProxy
IteratorProxy(T _x)
Definition: iterator.h:40
wibble::IteratorMixin::const_reference
const T & const_reference
Definition: iterator.h:68
wibble::Morph
Definition: amorph.h:141
wibble::StlIterator::operator==
bool operator==(const StlIterator< It > &o)
Definition: iterator.h:117
wibble::StlIterator
Definition: iterator.h:111
wibble::StlIterator::m_iterator
It m_iterator
Definition: iterator.h:119