iterator_wrapper.h

Go to the documentation of this file.
00001 /* $Id$ */
00002 #ifndef ITERATOR_WRAPPER_H
00003 #define ITERATOR_WRAPPER_H
00004 
00008 namespace Tumble {
00009   namespace iterators {
00010 
00011         /* Define the movement operators (++, +=, +, --, -=, -) to
00012            just forward on. */
00013 #define define_move_operators \
00014         iterator& operator++ ()          { ++it_; return *this; } \
00015         iterator& operator+= (int i)     { it_ += i; return *this; } \
00016         iterator operator+ (int i) const { return iterator(it_ + i); } \
00017  \
00018         iterator& operator-- ()          { --it_; return *this; } \
00019         iterator& operator-= (int i)     { it_ -= i; return *this; } \
00020         iterator operator- (int i) const { return iterator(it_ - i); }
00021 
00022         /* Define the relational operators (<, <=, ==, !=, >=, >)
00023            and the difference operator to simply forward. */
00024 #define define_relational_operators \
00025         difference_type operator- (const iterator& other) const { \
00026           return it_ - other.it_; \
00027         } \
00028         bool operator<(const iterator& other) const { \
00029           return it_ < other.it_; \
00030         } \
00031         bool operator<=(const iterator& other) const { \
00032           return it_ <= other.it_; \
00033         } \
00034         bool operator==(const iterator& other) const { \
00035           return it_ == other.it_; \
00036         } \
00037         bool operator!=(const iterator& other) const { \
00038           return it_ != other.it_; \
00039         } \
00040         bool operator>(const iterator& other) const { \
00041           return it_ > other.it_; \
00042         } \
00043         bool operator>=(const iterator& other) const { \
00044           return it_ >= other.it_; \
00045         }
00046 
00047 
00048 
00055     template<class old_iterator, class output_type, class output_ref = output_type&>
00056       class caster {
00057         old_iterator it_;
00058         typedef caster iterator;
00059 
00060         public:
00061         /* Inherit some but not all traits. */
00062         typedef typename std::iterator_traits<old_iterator>::difference_type
00063           difference_type;
00064         typedef typename std::iterator_traits<old_iterator>::iterator_category
00065           iterator_category;
00066         typedef output_type  value_type;
00067         typedef output_ref   reference;
00068         typedef output_type* pointer;
00069 
00070         caster () { } // leave the iterator undefined.
00071         caster (const old_iterator& it) : it_(it) { }
00072 
00073         reference operator*() const {
00074           return static_cast<reference>(*it_);
00075         }
00076         reference operator[](const difference_type& i) const {
00077           return static_cast<reference>(it_[i]);
00078         }
00079 
00080         old_iterator base() { return it_; }
00081 
00082         define_move_operators;
00083         define_relational_operators;
00084       };
00085 
00092     template<class old_iterator, class output_type, class output_ref = output_type&>
00093       class dereferencer {
00094         old_iterator it_;
00095         typedef dereferencer iterator;
00096 
00097         public:
00098         /* Inherit some but not all traits. */
00099         typedef typename std::iterator_traits<old_iterator>::difference_type
00100           difference_type;
00101         typedef typename std::iterator_traits<old_iterator>::iterator_category
00102           iterator_category;
00103         typedef output_type  value_type;
00104         typedef output_ref   reference;
00105         typedef output_type* pointer;
00106 
00107         dereferencer () { } // leave the iterator undefined
00108         dereferencer (const old_iterator& it) : it_(it) { }
00109 
00110         reference operator*() const {
00111           return **it_;
00112         }
00113         reference operator[](const difference_type& i) const {
00114           return *it_[i];
00115         }
00116 
00117         old_iterator base() { return it_; }
00118 
00119         define_move_operators;
00120         define_relational_operators;
00121       };
00122 
00129     template<class old_iterator, class output_type, class output_ref = output_type&>
00130       class dedereferencer {
00131         old_iterator it_;
00132         typedef dedereferencer iterator;
00133 
00134         public:
00135         /* Inherit some but not all traits. */
00136         typedef typename std::iterator_traits<old_iterator>::difference_type
00137           difference_type;
00138         typedef typename std::iterator_traits<old_iterator>::iterator_category
00139           iterator_category;
00140         typedef output_type  value_type;
00141         typedef output_ref   reference;
00142         typedef output_type* pointer;
00143 
00144         dedereferencer () { } // leave the iterator undefined
00145         dedereferencer (const old_iterator& it) : it_(it) { }
00146 
00147         reference operator*() const {
00148           return **(*it_);
00149         }
00150         reference operator[](const difference_type& i) const {
00151           return **(it_[i]);
00152         }
00153 
00154         old_iterator base() { return it_; }
00155 
00156         define_move_operators;
00157         define_relational_operators;
00158       };
00159   } // namespace iterators.
00160 } // namespace Tumble.
00161 
00162 #endif

Generated on Mon May 24 09:53:30 2010 for TUMBLE by  doxygen 1.5.2