1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2013
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)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_INTRUSIVE_DETAIL_TRANSFORM_ITERATOR_HPP
14 #define BOOST_INTRUSIVE_DETAIL_TRANSFORM_ITERATOR_HPP
16 #ifndef BOOST_CONFIG_HPP
17 # include <boost/config.hpp>
20 #if defined(BOOST_HAS_PRAGMA_ONCE)
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>
33 template <class PseudoReference>
34 struct operator_arrow_proxy
36 BOOST_INTRUSIVE_FORCEINLINE operator_arrow_proxy(const PseudoReference &px)
40 BOOST_INTRUSIVE_FORCEINLINE 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;
48 struct operator_arrow_proxy<T&>
50 BOOST_INTRUSIVE_FORCEINLINE operator_arrow_proxy(T &px)
54 BOOST_INTRUSIVE_FORCEINLINE 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; }
61 template <class Iterator, class UnaryFunction>
62 class transform_iterator
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;
71 explicit transform_iterator(const Iterator &it, const UnaryFunction &f = UnaryFunction())
75 explicit transform_iterator()
79 BOOST_INTRUSIVE_FORCEINLINE Iterator get_it() const
80 { return members_.m_it; }
83 BOOST_INTRUSIVE_FORCEINLINE transform_iterator& operator++()
84 { increment(); return *this; }
86 BOOST_INTRUSIVE_FORCEINLINE transform_iterator operator++(int)
88 transform_iterator result (*this);
93 BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const transform_iterator& i, const transform_iterator& i2)
94 { return i.equal(i2); }
96 BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const transform_iterator& i, const transform_iterator& i2)
97 { return !(i == i2); }
99 BOOST_INTRUSIVE_FORCEINLINE friend typename Iterator::difference_type operator- (const transform_iterator& i, const transform_iterator& i2)
100 { return i2.distance_to(i); }
103 transform_iterator& operator+=(typename Iterator::difference_type off)
104 { this->advance(off); return *this; }
106 BOOST_INTRUSIVE_FORCEINLINE transform_iterator operator+(typename Iterator::difference_type off) const
108 transform_iterator other(*this);
113 BOOST_INTRUSIVE_FORCEINLINE friend transform_iterator operator+(typename Iterator::difference_type off, const transform_iterator& right)
114 { return right + off; }
116 BOOST_INTRUSIVE_FORCEINLINE transform_iterator& operator-=(typename Iterator::difference_type off)
117 { this->advance(-off); return *this; }
119 BOOST_INTRUSIVE_FORCEINLINE transform_iterator operator-(typename Iterator::difference_type off) const
120 { return *this + (-off); }
122 BOOST_INTRUSIVE_FORCEINLINE typename UnaryFunction::result_type operator*() const
123 { return dereference(); }
125 BOOST_INTRUSIVE_FORCEINLINE operator_arrow_proxy<typename UnaryFunction::result_type>
127 { return operator_arrow_proxy<typename UnaryFunction::result_type>(dereference()); }
133 BOOST_INTRUSIVE_FORCEINLINE members(const Iterator &it, const UnaryFunction &f)
134 : UnaryFunction(f), m_it(it)
137 BOOST_INTRUSIVE_FORCEINLINE members()
144 BOOST_INTRUSIVE_FORCEINLINE void increment()
147 BOOST_INTRUSIVE_FORCEINLINE void decrement()
150 BOOST_INTRUSIVE_FORCEINLINE bool equal(const transform_iterator &other) const
151 { return members_.m_it == other.members_.m_it; }
153 BOOST_INTRUSIVE_FORCEINLINE bool less(const transform_iterator &other) const
154 { return other.members_.m_it < members_.m_it; }
156 typename UnaryFunction::result_type dereference() const
157 { return members_(*members_.m_it); }
159 void advance(typename Iterator::difference_type n)
160 { boost::intrusive::iterator_advance(members_.m_it, n); }
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); }
167 } //namespace intrusive
170 #include <boost/intrusive/detail/config_end.hpp>
172 #endif //BOOST_INTRUSIVE_DETAIL_TRANSFORM_ITERATOR_HPP