dune-common  2.4.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 #ifndef DUNE_GENERICITERATOR_HH
4 #define DUNE_GENERICITERATOR_HH
5 
7 #include <cassert>
8 
9 namespace Dune {
10 
83  template<class R>
85  {
86  typedef const R type;
87  };
88 
89  template<class R>
90  struct const_reference<const R>
91  {
92  typedef const R type;
93  };
94 
95  template<class R>
96  struct const_reference<R&>
97  {
98  typedef const R& type;
99  };
100 
101  template<class R>
102  struct const_reference<const R&>
103  {
104  typedef const R& type;
105  };
106 
112  template<class R>
114  {
115  typedef R type;
116  };
117 
118  template<class R>
119  struct mutable_reference<const R>
120  {
121  typedef R type;
122  };
123 
124  template<class R>
125  struct mutable_reference<R&>
126  {
127  typedef R& type;
128  };
129 
130  template<class R>
131  struct mutable_reference<const R&>
132  {
133  typedef R& type;
134  };
135 
147  template<class C, class T, class R=T&, class D = std::ptrdiff_t,
148  template<class,class,class,class> class IteratorFacade=RandomAccessIteratorFacade>
150  public IteratorFacade<GenericIterator<C,T,R,D,IteratorFacade>,T,R,D>
151  {
152  friend class GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade>;
153  friend class GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade>;
154 
155  typedef GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade> MutableIterator;
156  typedef GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade> ConstIterator;
157 
158  public:
159 
168  typedef C Container;
169 
175  typedef T Value;
176 
180  typedef D DifferenceType;
181 
185  typedef R Reference;
186 
187  // Constructors needed by the base iterators
188  GenericIterator() : container_(0), position_(0)
189  {}
190 
199  : container_(&cont), position_(pos)
200  {}
201 
209  GenericIterator(const MutableIterator& other) : container_(other.container_), position_(other.position_)
210  {}
211 
221  GenericIterator(const ConstIterator& other) : container_(other.container_), position_(other.position_)
222  {}
223 
224  // Methods needed by the forward iterator
225  bool equals(const MutableIterator & other) const
226  {
227  return position_ == other.position_ && container_ == other.container_;
228  }
229 
230  bool equals(const ConstIterator & other) const
231  {
232  return position_ == other.position_ && container_ == other.container_;
233  }
234 
236  return container_->operator[](position_);
237  }
238 
239  void increment(){
240  ++position_;
241  }
242 
243  // Additional function needed by BidirectionalIterator
244  void decrement(){
245  --position_;
246  }
247 
248  // Additional function needed by RandomAccessIterator
250  return container_->operator[](position_+i);
251  }
252 
254  position_=position_+n;
255  }
256 
258  {
259  assert(other.container_==container_);
260  return other.position_ - position_;
261  }
262 
264  {
265  assert(other.container_==container_);
266  return other.position_ - position_;
267  }
268 
269  private:
270  Container *container_;
271  DifferenceType position_;
272  };
273 
276 } // end namespace Dune
277 
278 #endif
GenericIterator(const ConstIterator &other)
Copy constructor.
Definition: genericiterator.hh:221
Reference dereference() const
Definition: genericiterator.hh:235
const R type
Definition: genericiterator.hh:86
R & type
Definition: genericiterator.hh:133
GenericIterator(const MutableIterator &other)
Copy constructor.
Definition: genericiterator.hh:209
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:424
Generic class for stl-conforming iterators for container classes with operator[]. ...
Definition: genericiterator.hh:149
R type
Definition: genericiterator.hh:121
This file implements iterator facade classes for writing stl conformant iterators.
DifferenceType distanceTo(const MutableIterator &other) const
Definition: genericiterator.hh:257
R & type
Definition: genericiterator.hh:127
void increment()
Definition: genericiterator.hh:239
const R & type
Definition: genericiterator.hh:98
get the 'mutable' version of a reference to a const object
Definition: genericiterator.hh:113
Dune namespace.
Definition: alignment.hh:9
void decrement()
Definition: genericiterator.hh:244
T Value
The value type of the iterator.
Definition: genericiterator.hh:175
C Container
The type of container we are an iterator for.
Definition: genericiterator.hh:168
DifferenceType distanceTo(const ConstIterator &other) const
Definition: genericiterator.hh:263
bool equals(const MutableIterator &other) const
Definition: genericiterator.hh:225
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:84
R Reference
The type of the reference to the values accessed.
Definition: genericiterator.hh:185
Reference elementAt(DifferenceType i) const
Definition: genericiterator.hh:249
void advance(DifferenceType n)
Definition: genericiterator.hh:253
const R & type
Definition: genericiterator.hh:104
R type
Definition: genericiterator.hh:115
std::size_t position_
The current position in the buffer.
Definition: variablesizecommunicator.hh:136
D DifferenceType
The type of the difference between two positions.
Definition: genericiterator.hh:180
const R type
Definition: genericiterator.hh:92
bool equals(const ConstIterator &other) const
Definition: genericiterator.hh:230
GenericIterator(Container &cont, DifferenceType pos)
Constructor.
Definition: genericiterator.hh:198