wibble  1.1
iterator.h
Go to the documentation of this file.
1 
6 #include <wibble/amorph.h>
7 #include <wibble/mixin.h>
8 
9 #ifndef WIBBLE_ITERATOR_H
10 #define WIBBLE_ITERATOR_H
11 
12 namespace wibble {
13 
14 typedef bool SortabilityTag;
15 
16 template< typename T, typename I >
19 };
20 
21 template< typename T >
22 struct IteratorTraits< T, typename std::set< T >::iterator > {
24 };
25 
26 template< typename T >
27 struct IteratorTraits< T, typename std::multiset< T >::iterator > {
29 };
30 
31 template< typename T >
33  virtual T current() const = 0;
34  virtual void advance() = 0;
35  virtual ~IteratorInterface() {}
36 };
37 
38 template< typename T >
39 struct IteratorProxy {
40  IteratorProxy( T _x ) : x( _x ) {}
41  T x;
42  const T *operator->() const { return &x; }
43 };
44 
45 template< typename T, typename W >
46 struct IteratorMorph : Morph< IteratorMorph< T, W >, W, IteratorInterface< T > >
47 {
48  typedef W Wrapped;
50  IteratorMorph( const Wrapped &w )
51  : Morph< IteratorMorph, Wrapped, IteratorInterface< T > >( w ) {}
52  virtual void advance() { this->wrapped().advance(); }
53  virtual T current() const { return this->wrapped().current(); }
54 };
55 
56 template< typename T, typename Self >
58 {
59  Self &self() { return *static_cast< const Self * >( this ); }
60  const Self &self() const { return *static_cast< const Self * >( this ); }
61  typedef T ElementType;
62 
63  typedef std::forward_iterator_tag iterator_category;
64  typedef T value_type;
65  typedef ptrdiff_t difference_type;
66  typedef T *pointer;
67  typedef T &reference;
68  typedef const T &const_reference;
69 
71  return IteratorProxy< T >(self().current()); }
72  Self next() const { Self n( self() ); n.advance(); return n; }
73  T operator*() const { return self().current(); }
74 
75  Self &operator++() { self().advance(); return self(); }
76  Self operator++(int) {
77  Self tmp = self();
78  self().advance();
79  return tmp;
80  }
81 };
82 
83 template< typename T, typename I >
85  return false;
86 }
87 
88 template< typename T, typename I >
90  return true;
91 }
92 
93 template< typename T >
94 struct Iterator : Amorph< Iterator< T >, IteratorInterface< T >, 0 >,
95  IteratorMixin< T, Iterator< T > >
96 {
98  typedef T ElementType;
99 
100  Iterator( const IteratorInterface< T > &i ) : Super( i ) {}
101  Iterator() {}
102  bool operator<=( const Iterator &i ) const { return leq( i ); }
103 
104  T current() const { return this->implInterface()->current(); }
105  virtual void advance() { this->implInterface()->advance(); }
106 
107  template< typename C > operator Iterator< C >();
108 };
109 
110 template< typename It >
111 struct StlIterator : IteratorMixin< typename It::value_type, StlIterator< It > >
112 {
113  typedef typename std::iterator_traits< It >::value_type Value;
114  StlIterator( It i ) : m_iterator( i ) {}
115  virtual void advance() { ++m_iterator; }
116  virtual Value current() const { return *m_iterator; }
117  bool operator==( const StlIterator< It > &o ) { return m_iterator == o.m_iterator; }
118 protected:
120 };
121 
122 template< typename I >
124  return StlIterator< I >( i );
125 }
126 
127 }
128 
129 #endif