dune-common  2.2.0
genericiterator.hh
Go to the documentation of this file.
1 // $Id: genericiterator.hh 6785 2012-05-31 22:07:47Z sander $
2 #ifndef DUNE_GENERICITERATOR_HH
3 #define DUNE_GENERICITERATOR_HH
4 
6 #include <cassert>
7 
8 namespace Dune {
9 
82  template<class R>
84  {
85  typedef const R type;
86  };
87 
88  template<class R>
89  struct const_reference<const R>
90  {
91  typedef const R type;
92  };
93 
94  template<class R>
95  struct const_reference<R&>
96  {
97  typedef const R& type;
98  };
99 
100  template<class R>
101  struct const_reference<const R&>
102  {
103  typedef const R& type;
104  };
105 
111  template<class R>
113  {
114  typedef R type;
115  };
116 
117  template<class R>
118  struct mutable_reference<const R>
119  {
120  typedef R type;
121  };
122 
123  template<class R>
124  struct mutable_reference<R&>
125  {
126  typedef R& type;
127  };
128 
129  template<class R>
130  struct mutable_reference<const R&>
131  {
132  typedef R& type;
133  };
134 
146  template<class C, class T, class R=T&, class D = std::ptrdiff_t,
147  template<class,class,class,class> class IteratorFacade=RandomAccessIteratorFacade>
149  public IteratorFacade<GenericIterator<C,T,R,D,IteratorFacade>,T,R,D>
150 {
151  friend class GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade>;
152  friend class GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade>;
153 
154  typedef GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade> MutableIterator;
155  typedef GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade> ConstIterator;
156 
157 public:
158 
167  typedef C Container;
168 
174  typedef T Value;
175 
179  typedef D DifferenceType;
180 
184  typedef R Reference;
185 
186  // Constructors needed by the base iterators
187  GenericIterator(): container_(0), position_(0)
188  {}
189 
198  : container_(&cont), position_(pos)
199  {}
200 
208  GenericIterator(const MutableIterator& other): container_(other.container_), position_(other.position_)
209  {}
210 
220  GenericIterator(const ConstIterator& other): container_(other.container_), position_(other.position_)
221  {}
222 
223  // Methods needed by the forward iterator
224  bool equals(const MutableIterator & other) const
225  {
226  return position_ == other.position_ && container_ == other.container_;
227  }
228 
229  bool equals(const ConstIterator & other) const
230  {
231  return position_ == other.position_ && container_ == other.container_;
232  }
233 
235  return container_->operator[](position_);
236  }
237 
238  void increment(){
239  ++position_;
240  }
241 
242  // Additional function needed by BidirectionalIterator
243  void decrement(){
244  --position_;
245  }
246 
247  // Additional function needed by RandomAccessIterator
249  return container_->operator[](position_+i);
250  }
251 
253  position_=position_+n;
254  }
255 
257  {
258  assert(other.container_==container_);
259  return other.position_ - position_;
260  }
261 
263  {
264  assert(other.container_==container_);
265  return other.position_ - position_;
266  }
267 
268 private:
269  Container *container_;
270  DifferenceType position_;
271 };
272 
275 } // end namespace Dune
276 
277 #endif