member_iterator.h
Go to the documentation of this file.00001 #ifndef VVLIB_UTIL_MEMBER_ITERATOR_H
00002 #define VVLIB_UTIL_MEMBER_ITERATOR_H
00003
00009 #include <config.h>
00010 #include <iterator>
00011
00012 namespace util
00013 {
00048 template <class Iterator, class T,
00049 T std::iterator_traits<Iterator>::value_type::* member,
00050 class Reference = T&, class Pointer = T*>
00051 struct SelectMemberIterator
00052 {
00056 typedef Iterator base_iterator;
00060 typedef typename std::iterator_traits<Iterator>::iterator_category iterator_category;
00064 typedef T value_type;
00068 typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
00072 typedef Reference reference;
00076 typedef Pointer pointer;
00077
00081 SelectMemberIterator() {}
00082
00086 SelectMemberIterator(const base_iterator& i)
00087 : it(i)
00088 { }
00089
00093 SelectMemberIterator(const SelectMemberIterator& other)
00094 : it(other.it)
00095 {}
00096
00097 #ifdef USE_CXX0X
00098 SelectMemberIterator(SelectMemberIterator&& other) = default;
00099 #endif
00100
00102
00103
00104 bool operator==(const SelectMemberIterator& other) const
00105 { return it == other.it; }
00106
00107 bool operator!=(const SelectMemberIterator& other) const
00108 { return it != other.it; }
00109
00113 SelectMemberIterator& operator=(const SelectMemberIterator& copy)
00114 { it = copy.it; return *this; }
00115
00116 #ifdef USE_CXX0X
00117 SelectMemberIterator& operator=(SelectMemberIterator&& other) = default;
00118 #endif
00119
00123 SelectMemberIterator& operator++() { ++it; return *this; }
00127 SelectMemberIterator& operator++(int) { SelectMemberIterator tmp(*this); ++it; return tmp; }
00128
00132 reference operator*() { return (*it).*member; }
00136 const reference operator*() const { return (*it).*member; }
00137
00141 pointer operator->() { return &((*it).*member); }
00145 const pointer operator->() const { return &((*it).*member); }
00147
00149
00150
00153 SelectMemberIterator& operator--() { --it; return *this; }
00157 SelectMemberIterator& operator--(int) { SelectMemberIterator tmp(*this); --it; return tmp; }
00159
00161
00162
00165 SelectMemberIterator& operator+=(difference_type n)
00166 { it += n; return *this; }
00167
00171 SelectMemberIterator& operator-=(difference_type n)
00172 { it -= n; return *this; }
00173
00174 bool operator<(const SelectMemberIterator& other) const
00175 { return it < other.it; }
00176
00177 bool operator>(const SelectMemberIterator& other) const
00178 { return it > other.it; }
00179
00180 bool operator<=(const SelectMemberIterator& other) const
00181 { return it <= other.it; }
00182
00183 bool operator>=(const SelectMemberIterator& other) const
00184 { return it >= other.it; }
00186
00188
00189 friend SelectMemberIterator operator+(const SelectMemberIterator& it, difference_type n)
00190 { SelectMemberIterator tmp(it); tmp += n; return tmp; }
00191
00192 friend SelectMemberIterator operator+(difference_type n, const SelectMemberIterator& it )
00193 { SelectMemberIterator tmp(it); tmp += n; return tmp; }
00194
00195 friend SelectMemberIterator operator-(const SelectMemberIterator& it, difference_type n )
00196 { SelectMemberIterator tmp(it); tmp -= n; return tmp; }
00197
00198 friend SelectMemberIterator operator-(difference_type n, const SelectMemberIterator& it )
00199 { SelectMemberIterator tmp(it); tmp -= n; return tmp; }
00200
00204 friend difference_type operator-(const SelectMemberIterator& last, const SelectMemberIterator& first)
00205 { return last.it - first.it; }
00207
00211 base_iterator base() const { return it; }
00212
00213 protected:
00217 base_iterator it;
00218 };
00219
00220 template <typename MapType>
00221 struct KeyIterator
00222 {
00223 typedef SelectMemberIterator<typename MapType::iterator,
00224 const typename MapType::key_type,
00225 &MapType::value_type::first> type;
00226 };
00227
00228 template <typename MapType>
00229 struct MappedIterator
00230 {
00231 typedef SelectMemberIterator<typename MapType::iterator,
00232 const typename MapType::mapped_type,
00233 &MapType::value_type::second> type;
00234 };
00235
00236 }
00237
00238 #endif // VVLIB_UTIL_MEMBER_ITERATOR_H
00239