dune-common  2.3.1
iteratorfacades.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_ITERATORFACADES_HH
5 #define DUNE_ITERATORFACADES_HH
6 #include <iterator>
7 #include "typetraits.hh"
8 
9 namespace Dune
10 {
135  template<class T, class V, class R = V&, class D = std::ptrdiff_t>
137  public std::iterator< std::forward_iterator_tag,
138  typename remove_const<V>::type, // std::iterator needs mutable value type
139  D,
140  V*,
141  R>
142  {
143 
144  public:
169  typedef T DerivedType;
170 
174  typedef V Value;
175 
179  typedef V* Pointer;
180 
184  typedef D DifferenceType;
185 
189  typedef R Reference;
190 
193  {
194  return static_cast<DerivedType const*>(this)->dereference();
195  }
196 
198  {
199  return &(static_cast<const DerivedType *>(this)->dereference());
200  }
201 
204  {
205  static_cast<DerivedType *>(this)->increment();
206  return *static_cast<DerivedType *>(this);
207  }
208 
211  {
212  DerivedType tmp(static_cast<DerivedType const&>(*this));
213  this->operator++();
214  return tmp;
215  }
216  };
217 
228  template<class T1, class V1, class R1, class D,
229  class T2, class V2, class R2>
230  inline typename EnableIfInterOperable<T1,T2,bool>::type
233  {
235  return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
236  else
237  return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
238  }
239 
250  template<class T1, class V1, class R1, class D,
251  class T2, class V2, class R2>
252  inline typename EnableIfInterOperable<T1,T2,bool>::type
255  {
257  return !static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
258  else
259  return !static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
260  }
261 
266  template<class T, class V, class R = V&, class D = std::ptrdiff_t>
268  public std::iterator< std::bidirectional_iterator_tag,
269  typename remove_const<V>::type, // std::iterator needs mutable value type
270  D,
271  V*,
272  R>
273  {
274 
275  public:
301  typedef T DerivedType;
302 
306  typedef V Value;
307 
311  typedef V* Pointer;
312 
316  typedef D DifferenceType;
317 
321  typedef R Reference;
322 
325  {
326  return static_cast<DerivedType const*>(this)->dereference();
327  }
328 
330  {
331  return &(static_cast<const DerivedType *>(this)->dereference());
332  }
333 
336  {
337  static_cast<DerivedType *>(this)->increment();
338  return *static_cast<DerivedType *>(this);
339  }
340 
343  {
344  DerivedType tmp(static_cast<DerivedType const&>(*this));
345  this->operator++();
346  return tmp;
347  }
348 
349 
352  {
353  static_cast<DerivedType *>(this)->decrement();
354  return *static_cast<DerivedType *>(this);
355  }
356 
359  {
360  DerivedType tmp(static_cast<DerivedType const&>(*this));
361  this->operator--();
362  return tmp;
363  }
364  };
365 
373  template<class T1, class V1, class R1, class D,
374  class T2, class V2, class R2>
375  inline typename enable_if<Conversion<T2,T1>::exists,bool>::type
378  {
379  return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
380  }
381 
390  template<class T1, class V1, class R1, class D,
391  class T2, class V2, class R2>
392  inline
393  typename enable_if<Conversion<T1,T2>::exists && !Conversion<T2,T1>::exists,
394  bool>::type
395  operator==(const BidirectionalIteratorFacade<T1,V1,R1,D>& lhs,
396  const BidirectionalIteratorFacade<T2,V2,R2,D>& rhs)
397  {
398  return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
399  }
400 
411  template<class T1, class V1, class R1, class D,
412  class T2, class V2, class R2>
413  inline typename EnableIfInterOperable<T1,T2,bool>::type
416  {
417  return !(lhs == rhs);
418  }
419 
424  template<class T, class V, class R = V&, class D = std::ptrdiff_t>
426  public std::iterator< std::random_access_iterator_tag,
427  typename remove_const<V>::type, // std::iterator needs mutable value type
428  D,
429  V*,
430  R>
431  {
432 
433  public:
467  typedef T DerivedType;
468 
472  typedef V Value;
473 
477  typedef V* Pointer;
478 
482  typedef D DifferenceType;
483 
487  typedef R Reference;
488 
491  {
492  return static_cast<DerivedType const*>(this)->dereference();
493  }
494 
496  {
497  return &(static_cast<const DerivedType *>(this)->dereference());
498  }
499 
506  {
507  return static_cast<const DerivedType *>(this)->elementAt(n);
508  }
509 
512  {
513  static_cast<DerivedType *>(this)->increment();
514  return *static_cast<DerivedType *>(this);
515  }
516 
519  {
520  DerivedType tmp(static_cast<DerivedType const&>(*this));
521  this->operator++();
522  return tmp;
523  }
524 
526  {
527  static_cast<DerivedType *>(this)->advance(n);
528  return *static_cast<DerivedType *>(this);
529  }
530 
532  {
533  DerivedType tmp(static_cast<DerivedType const&>(*this));
534  tmp.advance(n);
535  return tmp;
536  }
537 
538 
541  {
542  static_cast<DerivedType *>(this)->decrement();
543  return *static_cast<DerivedType *>(this);
544  }
545 
548  {
549  DerivedType tmp(static_cast<DerivedType const&>(*this));
550  this->operator--();
551  return tmp;
552  }
553 
555  {
556  static_cast<DerivedType *>(this)->advance(-n);
557  return *static_cast<DerivedType *>(this);
558  }
559 
561  {
562  DerivedType tmp(static_cast<DerivedType const&>(*this));
563  tmp.advance(-n);
564  return tmp;
565  }
566 
567 
568  };
569 
580  template<class T1, class V1, class R1, class D,
581  class T2, class V2, class R2>
582  inline typename EnableIfInterOperable<T1,T2,bool>::type
585  {
587  return static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
588  else
589  return static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
590  }
591 
602  template<class T1, class V1, class R1, class D,
603  class T2, class V2, class R2>
604  inline typename EnableIfInterOperable<T1,T2,bool>::type
607  {
609  return !static_cast<const T1&>(lhs).equals(static_cast<const T2&>(rhs));
610  else
611  return !static_cast<const T2&>(rhs).equals(static_cast<const T1&>(lhs));
612  }
613 
624  template<class T1, class V1, class R1, class D,
625  class T2, class V2, class R2>
626  inline typename EnableIfInterOperable<T1,T2,bool>::type
627  operator<(const RandomAccessIteratorFacade<T1,V1,R1,D>& lhs,
629  {
631  return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))>0;
632  else
633  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))<0;
634  }
635 
636 
647  template<class T1, class V1, class R1, class D,
648  class T2, class V2, class R2>
649  inline typename EnableIfInterOperable<T1,T2,bool>::type
650  operator<=(const RandomAccessIteratorFacade<T1,V1,R1,D>& lhs,
652  {
654  return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))>=0;
655  else
656  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))<=0;
657  }
658 
659 
670  template<class T1, class V1, class R1, class D,
671  class T2, class V2, class R2>
672  inline typename EnableIfInterOperable<T1,T2,bool>::type
675  {
677  return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))<0;
678  else
679  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))>0;
680  }
681 
692  template<class T1, class V1, class R1, class D,
693  class T2, class V2, class R2>
694  inline typename EnableIfInterOperable<T1,T2,bool>::type
697  {
699  return static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs))<=0;
700  else
701  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs))>=0;
702  }
703 
714  template<class T1, class V1, class R1, class D,
715  class T2, class V2, class R2>
716  inline typename EnableIfInterOperable<T1,T2,D>::type
719  {
721  return -static_cast<const T1&>(lhs).distanceTo(static_cast<const T2&>(rhs));
722  else
723  return static_cast<const T2&>(rhs).distanceTo(static_cast<const T1&>(lhs));
724  }
725 
727 }
728 #endif