Go to the documentation of this file.00001
00006 #include <wibble/amorph.h>
00007 #include <wibble/mixin.h>
00008
00009 #ifndef WIBBLE_ITERATOR_H
00010 #define WIBBLE_ITERATOR_H
00011
00012 namespace wibble {
00013
00014 typedef bool SortabilityTag;
00015
00016 template< typename T, typename I >
00017 struct IteratorTraits {
00018 typedef SortabilityTag Unsorted;
00019 };
00020
00021 template< typename T >
00022 struct IteratorTraits< T, typename std::set< T >::iterator > {
00023 typedef SortabilityTag Sorted;
00024 };
00025
00026 template< typename T >
00027 struct IteratorTraits< T, typename std::multiset< T >::iterator > {
00028 typedef SortabilityTag Sorted;
00029 };
00030
00031 template< typename T >
00032 struct IteratorInterface {
00033 virtual T current() const = 0;
00034 virtual void advance() = 0;
00035 virtual ~IteratorInterface() {}
00036 };
00037
00038 template< typename T >
00039 struct IteratorProxy {
00040 IteratorProxy( T _x ) : x( _x ) {}
00041 T x;
00042 const T *operator->() const { return &x; }
00043 };
00044
00045 template< typename T, typename W >
00046 struct IteratorMorph : Morph< IteratorMorph< T, W >, W, IteratorInterface< T > >
00047 {
00048 typedef W Wrapped;
00049 IteratorMorph() {}
00050 IteratorMorph( const Wrapped &w )
00051 : Morph< IteratorMorph, Wrapped, IteratorInterface< T > >( w ) {}
00052 virtual void advance() { this->wrapped().advance(); }
00053 virtual T current() const { return this->wrapped().current(); }
00054 };
00055
00056 template< typename T, typename Self >
00057 struct IteratorMixin : mixin::Comparable< Self >
00058 {
00059 Self &self() { return *static_cast< const Self * >( this ); }
00060 const Self &self() const { return *static_cast< const Self * >( this ); }
00061 typedef T ElementType;
00062
00063 typedef std::forward_iterator_tag iterator_category;
00064 typedef T value_type;
00065 typedef ptrdiff_t difference_type;
00066 typedef T *pointer;
00067 typedef T &reference;
00068 typedef const T &const_reference;
00069
00070 IteratorProxy< T > operator->() const {
00071 return IteratorProxy< T >(self().current()); }
00072 Self next() const { Self n( self() ); n.advance(); return n; }
00073 T operator*() const { return self().current(); }
00074
00075 Self &operator++() { self().advance(); return self(); }
00076 Self operator++(int) {
00077 Self tmp = self();
00078 self().advance();
00079 return tmp;
00080 }
00081 };
00082
00083 template< typename T, typename I >
00084 typename IteratorTraits< T, I >::Unsorted isSortedT( I, I ) {
00085 return false;
00086 }
00087
00088 template< typename T, typename I >
00089 typename IteratorTraits< T, I >::Sorted isSortedT( I, I ) {
00090 return true;
00091 }
00092
00093 template< typename T >
00094 struct Iterator : Amorph< Iterator< T >, IteratorInterface< T >, 0 >,
00095 IteratorMixin< T, Iterator< T > >
00096 {
00097 typedef Amorph< Iterator< T >, IteratorInterface< T >, 0 > Super;
00098 typedef T ElementType;
00099
00100 Iterator( const IteratorInterface< T > &i ) : Super( i ) {}
00101 Iterator() {}
00102 bool operator<=( const Iterator &i ) const { return leq( i ); }
00103
00104 T current() const { return this->implInterface()->current(); }
00105 virtual void advance() { this->implInterface()->advance(); }
00106
00107 template< typename C > operator Iterator< C >();
00108 };
00109
00110 template< typename It >
00111 struct StlIterator : IteratorMixin< typename It::value_type, StlIterator< It > >
00112 {
00113 typedef typename std::iterator_traits< It >::value_type Value;
00114 StlIterator( It i ) : m_iterator( i ) {}
00115 virtual void advance() { ++m_iterator; }
00116 virtual Value current() const { return *m_iterator; }
00117 bool operator==( const StlIterator< It > &o ) { return m_iterator == o.m_iterator; }
00118 protected:
00119 It m_iterator;
00120 };
00121
00122 template< typename I >
00123 Iterator< typename I::value_type > iterator( I i ) {
00124 return StlIterator< I >( i );
00125 }
00126
00127 }
00128
00129 #endif