dune-common  2.3.1-rc1
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
void decrement()
Definition: genericiterator.hh:245
T Value
The value type of the iterator.
Definition: genericiterator.hh:176
R & type
Definition: genericiterator.hh:128
DifferenceType distanceTo(const MutableIterator &other) const
Definition: genericiterator.hh:258
const R type
Definition: genericiterator.hh:93
R type
Definition: genericiterator.hh:122
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:425
D DifferenceType
The type of the difference between two positions.
Definition: genericiterator.hh:181
void increment()
Definition: genericiterator.hh:240
R & type
Definition: genericiterator.hh:134
void advance(DifferenceType n)
Definition: genericiterator.hh:254
This file implements iterator facade classes for writing stl conformant iterators.
R Reference
The type of the reference to the values accessed.
Definition: genericiterator.hh:186
bool equals(const MutableIterator &other) const
Definition: genericiterator.hh:226
GenericIterator(const MutableIterator &other)
Copy constructor.
Definition: genericiterator.hh:210
GenericIterator(const ConstIterator &other)
Copy constructor.
Definition: genericiterator.hh:222
std::size_t position_
The current position in the buffer.
Definition: variablesizecommunicator.hh:136
DifferenceType distanceTo(const ConstIterator &other) const
Definition: genericiterator.hh:264
C Container
The type of container we are an iterator for.
Definition: genericiterator.hh:169
const R type
Definition: genericiterator.hh:87
const R & type
Definition: genericiterator.hh:105
Generic class for stl-conforming iterators for container classes with operator[]. ...
Definition: genericiterator.hh:150
const R & type
Definition: genericiterator.hh:99
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:85
bool equals(const ConstIterator &other) const
Definition: genericiterator.hh:231
Reference elementAt(DifferenceType i) const
Definition: genericiterator.hh:250
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:114
R type
Definition: genericiterator.hh:116
Reference dereference() const
Definition: genericiterator.hh:236
Removes a const qualifier while preserving others.
Definition: typetraits.hh:175
GenericIterator(Container &cont, DifferenceType pos)
Constructor.
Definition: genericiterator.hh:199