dune-common  2.2.1
iteratorfacades.hh
Go to the documentation of this file.
1 // $Id: iteratorfacades.hh 6155 2010-10-05 23:11:05Z joe $
2 #ifndef DUNE_ITERATORFACADES_HH
3 #define DUNE_ITERATORFACADES_HH
4 #include<iterator>
5 #include"typetraits.hh"
6 
7 namespace Dune
8 {
133  template<class T, class V, class R = V&, class D = std::ptrdiff_t>
135  public std::iterator< std::forward_iterator_tag,
136  typename remove_const<V>::type, // std::iterator needs mutable value type
137  D,
138  V*,
139  R>
140  {
141 
142  public:
164  typedef T DerivedType;
165 
169  typedef V Value;
170 
174  typedef V* Pointer;
175 
179  typedef D DifferenceType;
180 
184  typedef R Reference;
185 
188  {
189  return static_cast<DerivedType const*>(this)->dereference();
190  }
191 
193  {
194  return &(static_cast<const DerivedType *>(this)->dereference());
195  }
196 
199  {
200  static_cast<DerivedType *>(this)->increment();
201  return *static_cast<DerivedType *>(this);
202  }
203 
206  {
207  DerivedType tmp(static_cast<DerivedType const&>(*this));
208  this->operator++();
209  return tmp;
210  }
211  };
212 
223  template<class T1, class V1, class R1, class D,
224  class T2, class V2, class R2>
225  inline typename EnableIfInterOperable<T1,T2,bool>::type
228  {
230  return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
231  else
232  return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
233  }
234 
245  template<class T1, class V1, class R1, class D,
246  class T2, class V2, class R2>
247  inline typename EnableIfInterOperable<T1,T2,bool>::type
250  {
252  return !static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
253  else
254  return !static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
255  }
256 
261  template<class T, class V, class R = V&, class D = std::ptrdiff_t>
263  public std::iterator< std::bidirectional_iterator_tag,
264  typename remove_const<V>::type, // std::iterator needs mutable value type
265  D,
266  V*,
267  R>
268  {
269 
270  public:
296  typedef T DerivedType;
297 
301  typedef V Value;
302 
306  typedef V* Pointer;
307 
311  typedef D DifferenceType;
312 
316  typedef R Reference;
317 
320  {
321  return static_cast<DerivedType const*>(this)->dereference();
322  }
323 
325  {
326  return &(static_cast<const DerivedType *>(this)->dereference());
327  }
328 
331  {
332  static_cast<DerivedType *>(this)->increment();
333  return *static_cast<DerivedType *>(this);
334  }
335 
338  {
339  DerivedType tmp(static_cast<DerivedType const&>(*this));
340  this->operator++();
341  return tmp;
342  }
343 
344 
347  {
348  static_cast<DerivedType *>(this)->decrement();
349  return *static_cast<DerivedType *>(this);
350  }
351 
354  {
355  DerivedType tmp(static_cast<DerivedType const&>(*this));
356  this->operator--();
357  return tmp;
358  }
359  };
360 
368  template<class T1, class V1, class R1, class D,
369  class T2, class V2, class R2>
370  inline typename enable_if<Conversion<T2,T1>::exists,bool>::type
373  {
374  return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
375  }
376 
385  template<class T1, class V1, class R1, class D,
386  class T2, class V2, class R2>
387  inline
388  typename enable_if<Conversion<T1,T2>::exists && !Conversion<T2,T1>::exists,
389  bool>::type
390  operator==(const BidirectionalIteratorFacade<T1,V1,R1,D>& lhs,
391  const BidirectionalIteratorFacade<T2,V2,R2,D>& rhs)
392  {
393  return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
394  }
395 
406  template<class T1, class V1, class R1, class D,
407  class T2, class V2, class R2>
408  inline typename EnableIfInterOperable<T1,T2,bool>::type
411  {
412  return !(lhs == rhs);
413  }
414 
419  template<class T, class V, class R = V&, class D = std::ptrdiff_t>
421  public std::iterator< std::random_access_iterator_tag,
422  typename remove_const<V>::type, // std::iterator needs mutable value type
423  D,
424  V*,
425  R>
426  {
427 
428  public:
462  typedef T DerivedType;
463 
467  typedef V Value;
468 
472  typedef V* Pointer;
473 
477  typedef D DifferenceType;
478 
482  typedef R Reference;
483 
486  {
487  return static_cast<DerivedType const*>(this)->dereference();
488  }
489 
491  {
492  return &(static_cast<const DerivedType *>(this)->dereference());
493  }
494 
501  {
502  return static_cast<const DerivedType *>(this)->elementAt(n);
503  }
504 
507  {
508  static_cast<DerivedType *>(this)->increment();
509  return *static_cast<DerivedType *>(this);
510  }
511 
514  {
515  DerivedType tmp(static_cast<DerivedType const&>(*this));
516  this->operator++();
517  return tmp;
518  }
519 
521  {
522  static_cast<DerivedType *>(this)->advance(n);
523  return *static_cast<DerivedType *>(this);
524  }
525 
527  {
528  DerivedType tmp(static_cast<DerivedType const&>(*this));
529  tmp.advance(n);
530  return tmp;
531  }
532 
533 
536  {
537  static_cast<DerivedType *>(this)->decrement();
538  return *static_cast<DerivedType *>(this);
539  }
540 
543  {
544  DerivedType tmp(static_cast<DerivedType const&>(*this));
545  this->operator--();
546  return tmp;
547  }
548 
550  {
551  static_cast<DerivedType *>(this)->advance(-n);
552  return *static_cast<DerivedType *>(this);
553  }
554 
556  {
557  DerivedType tmp(static_cast<DerivedType const&>(*this));
558  tmp.advance(-n);
559  return tmp;
560  }
561 
562 
563  };
564 
575  template<class T1, class V1, class R1, class D,
576  class T2, class V2, class R2>
577  inline typename EnableIfInterOperable<T1,T2,bool>::type
580  {
582  return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
583  else
584  return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
585  }
586 
597  template<class T1, class V1, class R1, class D,
598  class T2, class V2, class R2>
599  inline typename EnableIfInterOperable<T1,T2,bool>::type
602  {
604  return !static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
605  else
606  return !static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
607  }
608 
619  template<class T1, class V1, class R1, class D,
620  class T2, class V2, class R2>
621  inline typename EnableIfInterOperable<T1,T2,bool>::type
622  operator<(const RandomAccessIteratorFacade<T1,V1,R1,D>& lhs,
624  {
626  return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))>0;
627  else
628  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))<0;
629  }
630 
631 
642  template<class T1, class V1, class R1, class D,
643  class T2, class V2, class R2>
644  inline typename EnableIfInterOperable<T1,T2,bool>::type
645  operator<=(const RandomAccessIteratorFacade<T1,V1,R1,D>& lhs,
647  {
649  return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))>=0;
650  else
651  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))<=0;
652  }
653 
654 
665  template<class T1, class V1, class R1, class D,
666  class T2, class V2, class R2>
667  inline typename EnableIfInterOperable<T1,T2,bool>::type
670  {
672  return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))<0;
673  else
674  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))>0;
675  }
676 
687  template<class T1, class V1, class R1, class D,
688  class T2, class V2, class R2>
689  inline typename EnableIfInterOperable<T1,T2,bool>::type
692  {
694  return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))<=0;
695  else
696  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))>=0;
697  }
698 
709  template<class T1, class V1, class R1, class D,
710  class T2, class V2, class R2>
711  inline typename EnableIfInterOperable<T1,T2,D>::type
714  {
716  return -static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs));
717  else
718  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs));
719  }
720 
722 }
723 #endif