| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2007-2013 |
| 4 | // |
| 5 | // Distributed under the Boost Software License, Version 1.0. |
| 6 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | // |
| 9 | // See http://www.boost.org/libs/intrusive for documentation. |
| 10 | // |
| 11 | ///////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | #ifndef BOOST_INTRUSIVE_DETAIL_TRANSFORM_ITERATOR_HPP |
| 14 | #define BOOST_INTRUSIVE_DETAIL_TRANSFORM_ITERATOR_HPP |
| 15 | |
| 16 | #ifndef BOOST_CONFIG_HPP |
| 17 | # include <boost/config.hpp> |
| 18 | #endif |
| 19 | |
| 20 | #if defined(BOOST_HAS_PRAGMA_ONCE) |
| 21 | # pragma once |
| 22 | #endif |
| 23 | |
| 24 | #include <boost/intrusive/detail/config_begin.hpp> |
| 25 | #include <boost/intrusive/detail/workaround.hpp> |
| 26 | #include <boost/intrusive/detail/mpl.hpp> |
| 27 | #include <boost/intrusive/detail/iterator.hpp> |
| 28 | |
| 29 | namespace boost { |
| 30 | namespace intrusive { |
| 31 | namespace detail { |
| 32 | |
| 33 | template <class PseudoReference> |
| 34 | struct operator_arrow_proxy |
| 35 | { |
| 36 | inline operator_arrow_proxy(const PseudoReference &px) |
| 37 | : m_value(px) |
| 38 | {} |
| 39 | |
| 40 | inline PseudoReference* operator->() const { return &m_value; } |
| 41 | // This function is needed for MWCW and BCC, which won't call operator-> |
| 42 | // again automatically per 13.3.1.2 para 8 |
| 43 | // operator T*() const { return &m_value; } |
| 44 | mutable PseudoReference m_value; |
| 45 | }; |
| 46 | |
| 47 | template <class T> |
| 48 | struct operator_arrow_proxy<T&> |
| 49 | { |
| 50 | inline operator_arrow_proxy(T &px) |
| 51 | : m_value(px) |
| 52 | {} |
| 53 | |
| 54 | inline T* operator->() const { return &m_value; } |
| 55 | // This function is needed for MWCW and BCC, which won't call operator-> |
| 56 | // again automatically per 13.3.1.2 para 8 |
| 57 | // operator T*() const { return &m_value; } |
| 58 | T &m_value; |
| 59 | }; |
| 60 | |
| 61 | template <class Iterator, class UnaryFunction> |
| 62 | class transform_iterator |
| 63 | { |
| 64 | public: |
| 65 | typedef typename Iterator::iterator_category iterator_category; |
| 66 | typedef typename detail::remove_reference<typename UnaryFunction::result_type>::type value_type; |
| 67 | typedef typename Iterator::difference_type difference_type; |
| 68 | typedef operator_arrow_proxy<typename UnaryFunction::result_type> pointer; |
| 69 | typedef typename UnaryFunction::result_type reference; |
| 70 | |
| 71 | explicit transform_iterator(const Iterator &it, const UnaryFunction &f = UnaryFunction()) |
| 72 | : members_(it, f) |
| 73 | {} |
| 74 | |
| 75 | explicit transform_iterator() |
| 76 | : members_() |
| 77 | {} |
| 78 | |
| 79 | inline Iterator get_it() const |
| 80 | { return members_.m_it; } |
| 81 | |
| 82 | //Constructors |
| 83 | inline transform_iterator& operator++() |
| 84 | { increment(); return *this; } |
| 85 | |
| 86 | inline transform_iterator operator++(int) |
| 87 | { |
| 88 | transform_iterator result (*this); |
| 89 | increment(); |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | inline friend bool operator== (const transform_iterator& i, const transform_iterator& i2) |
| 94 | { return i.equal(other: i2); } |
| 95 | |
| 96 | inline friend bool operator!= (const transform_iterator& i, const transform_iterator& i2) |
| 97 | { return !(i == i2); } |
| 98 | |
| 99 | inline friend typename Iterator::difference_type operator- (const transform_iterator& i, const transform_iterator& i2) |
| 100 | { return i2.distance_to(other: i); } |
| 101 | |
| 102 | //Arithmetic |
| 103 | transform_iterator& operator+=(typename Iterator::difference_type off) |
| 104 | { this->advance(n: off); return *this; } |
| 105 | |
| 106 | inline transform_iterator operator+(typename Iterator::difference_type off) const |
| 107 | { |
| 108 | transform_iterator other(*this); |
| 109 | other.advance(n: off); |
| 110 | return other; |
| 111 | } |
| 112 | |
| 113 | inline friend transform_iterator operator+(typename Iterator::difference_type off, const transform_iterator& right) |
| 114 | { return right + off; } |
| 115 | |
| 116 | inline transform_iterator& operator-=(typename Iterator::difference_type off) |
| 117 | { this->advance(n: -off); return *this; } |
| 118 | |
| 119 | inline transform_iterator operator-(typename Iterator::difference_type off) const |
| 120 | { return *this + (-off); } |
| 121 | |
| 122 | inline typename UnaryFunction::result_type operator*() const |
| 123 | { return dereference(); } |
| 124 | |
| 125 | inline operator_arrow_proxy<typename UnaryFunction::result_type> |
| 126 | operator->() const |
| 127 | { return operator_arrow_proxy<typename UnaryFunction::result_type>(dereference()); } |
| 128 | |
| 129 | private: |
| 130 | struct members |
| 131 | : UnaryFunction |
| 132 | { |
| 133 | inline members(const Iterator &it, const UnaryFunction &f) |
| 134 | : UnaryFunction(f), m_it(it) |
| 135 | {} |
| 136 | |
| 137 | inline members() |
| 138 | {} |
| 139 | |
| 140 | Iterator m_it; |
| 141 | } members_; |
| 142 | |
| 143 | |
| 144 | inline void increment() |
| 145 | { ++members_.m_it; } |
| 146 | |
| 147 | inline void decrement() |
| 148 | { --members_.m_it; } |
| 149 | |
| 150 | inline bool equal(const transform_iterator &other) const |
| 151 | { return members_.m_it == other.members_.m_it; } |
| 152 | |
| 153 | inline bool less(const transform_iterator &other) const |
| 154 | { return other.members_.m_it < members_.m_it; } |
| 155 | |
| 156 | typename UnaryFunction::result_type dereference() const |
| 157 | { return members_(*members_.m_it); } |
| 158 | |
| 159 | void advance(typename Iterator::difference_type n) |
| 160 | { boost::intrusive::iterator_advance(members_.m_it, n); } |
| 161 | |
| 162 | typename Iterator::difference_type distance_to(const transform_iterator &other)const |
| 163 | { return boost::intrusive::iterator_distance(other.members_.m_it, members_.m_it); } |
| 164 | }; |
| 165 | |
| 166 | } //namespace detail |
| 167 | } //namespace intrusive |
| 168 | } //namespace boost |
| 169 | |
| 170 | #include <boost/intrusive/detail/config_end.hpp> |
| 171 | |
| 172 | #endif //BOOST_INTRUSIVE_DETAIL_TRANSFORM_ITERATOR_HPP |
| 173 | |