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 | BOOST_INTRUSIVE_FORCEINLINE operator_arrow_proxy(const PseudoReference &px) |
37 | : m_value(px) |
38 | {} |
39 | |
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; |
45 | }; |
46 | |
47 | template <class T> |
48 | struct operator_arrow_proxy<T&> |
49 | { |
50 | BOOST_INTRUSIVE_FORCEINLINE operator_arrow_proxy(T &px) |
51 | : m_value(px) |
52 | {} |
53 | |
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; } |
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 | BOOST_INTRUSIVE_FORCEINLINE Iterator get_it() const |
80 | { return members_.m_it; } |
81 | |
82 | //Constructors |
83 | BOOST_INTRUSIVE_FORCEINLINE transform_iterator& operator++() |
84 | { increment(); return *this; } |
85 | |
86 | BOOST_INTRUSIVE_FORCEINLINE transform_iterator operator++(int) |
87 | { |
88 | transform_iterator result (*this); |
89 | increment(); |
90 | return result; |
91 | } |
92 | |
93 | BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const transform_iterator& i, const transform_iterator& i2) |
94 | { return i.equal(i2); } |
95 | |
96 | BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const transform_iterator& i, const transform_iterator& i2) |
97 | { return !(i == i2); } |
98 | |
99 | BOOST_INTRUSIVE_FORCEINLINE friend typename Iterator::difference_type operator- (const transform_iterator& i, const transform_iterator& i2) |
100 | { return i2.distance_to(i); } |
101 | |
102 | //Arithmetic |
103 | transform_iterator& operator+=(typename Iterator::difference_type off) |
104 | { this->advance(off); return *this; } |
105 | |
106 | BOOST_INTRUSIVE_FORCEINLINE transform_iterator operator+(typename Iterator::difference_type off) const |
107 | { |
108 | transform_iterator other(*this); |
109 | other.advance(off); |
110 | return other; |
111 | } |
112 | |
113 | BOOST_INTRUSIVE_FORCEINLINE friend transform_iterator operator+(typename Iterator::difference_type off, const transform_iterator& right) |
114 | { return right + off; } |
115 | |
116 | BOOST_INTRUSIVE_FORCEINLINE transform_iterator& operator-=(typename Iterator::difference_type off) |
117 | { this->advance(-off); return *this; } |
118 | |
119 | BOOST_INTRUSIVE_FORCEINLINE transform_iterator operator-(typename Iterator::difference_type off) const |
120 | { return *this + (-off); } |
121 | |
122 | BOOST_INTRUSIVE_FORCEINLINE typename UnaryFunction::result_type operator*() const |
123 | { return dereference(); } |
124 | |
125 | BOOST_INTRUSIVE_FORCEINLINE 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 | BOOST_INTRUSIVE_FORCEINLINE members(const Iterator &it, const UnaryFunction &f) |
134 | : UnaryFunction(f), m_it(it) |
135 | {} |
136 | |
137 | BOOST_INTRUSIVE_FORCEINLINE members() |
138 | {} |
139 | |
140 | Iterator m_it; |
141 | } members_; |
142 | |
143 | |
144 | BOOST_INTRUSIVE_FORCEINLINE void increment() |
145 | { ++members_.m_it; } |
146 | |
147 | BOOST_INTRUSIVE_FORCEINLINE void decrement() |
148 | { --members_.m_it; } |
149 | |
150 | BOOST_INTRUSIVE_FORCEINLINE bool equal(const transform_iterator &other) const |
151 | { return members_.m_it == other.members_.m_it; } |
152 | |
153 | BOOST_INTRUSIVE_FORCEINLINE 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 | |