dune-common  2.3.1
genericiterator.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // $Id$
4 #ifndef DUNE_GENERICITERATOR_HH
5 #define DUNE_GENERICITERATOR_HH
6 
8 #include <cassert>
9 
10 namespace Dune {
11 
84  template<class R>
86  {
87  typedef const R type;
88  };
89 
90  template<class R>
91  struct const_reference<const R>
92  {
93  typedef const R type;
94  };
95 
96  template<class R>
97  struct const_reference<R&>
98  {
99  typedef const R& type;
100  };
101 
102  template<class R>
103  struct const_reference<const R&>
104  {
105  typedef const R& type;
106  };
107 
113  template<class R>
115  {
116  typedef R type;
117  };
118 
119  template<class R>
120  struct mutable_reference<const R>
121  {
122  typedef R type;
123  };
124 
125  template<class R>
126  struct mutable_reference<R&>
127  {
128  typedef R& type;
129  };
130 
131  template<class R>
132  struct mutable_reference<const R&>
133  {
134  typedef R& type;
135  };
136 
148  template<class C, class T, class R=T&, class D = std::ptrdiff_t,
149  template<class,class,class,class> class IteratorFacade=RandomAccessIteratorFacade>
151  public IteratorFacade<GenericIterator<C,T,R,D,IteratorFacade>,T,R,D>
152  {
153  friend class GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade>;
154  friend class GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade>;
155 
156  typedef GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade> MutableIterator;
157  typedef GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade> ConstIterator;
158 
159  public:
160 
169  typedef C Container;
170 
176  typedef T Value;
177 
181  typedef D DifferenceType;
182 
186  typedef R Reference;
187 
188  // Constructors needed by the base iterators
189  GenericIterator() : container_(0), position_(0)
190  {}
191 
200  : container_(&cont), position_(pos)
201  {}
202 
210  GenericIterator(const MutableIterator& other) : container_(other.container_), position_(other.position_)
211  {}
212 
222  GenericIterator(const ConstIterator& other) : container_(other.container_), position_(other.position_)
223  {}
224 
225  // Methods needed by the forward iterator
226  bool equals(const MutableIterator & other) const
227  {
228  return position_ == other.position_ && container_ == other.container_;
229  }
230 
231  bool equals(const ConstIterator & other) const
232  {
233  return position_ == other.position_ && container_ == other.container_;
234  }
235 
237  return container_->operator[](position_);
238  }
239 
240  void increment(){
241  ++position_;
242  }
243 
244  // Additional function needed by BidirectionalIterator
245  void decrement(){
246  --position_;
247  }
248 
249  // Additional function needed by RandomAccessIterator
251  return container_->operator[](position_+i);
252  }
253 
255  position_=position_+n;
256  }
257 
259  {
260  assert(other.container_==container_);
261  return other.position_ - position_;
262  }
263 
265  {
266  assert(other.container_==container_);
267  return other.position_ - position_;
268  }
269 
270  private:
271  Container *container_;
272  DifferenceType position_;
273  };
274 
277 } // end namespace Dune
278 
279 #endif