1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2012-2012.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/move for documentation.
10 //////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_MOVE_ITERATOR_HPP
15 #define BOOST_MOVE_ITERATOR_HPP
17 #ifndef BOOST_CONFIG_HPP
18 # include <boost/config.hpp>
21 #if defined(BOOST_HAS_PRAGMA_ONCE)
25 #include <boost/move/detail/config_begin.hpp>
26 #include <boost/move/detail/workaround.hpp> //forceinline
27 #include <boost/move/detail/iterator_traits.hpp>
28 #include <boost/move/utility_core.hpp>
32 //////////////////////////////////////////////////////////////////////////////
36 //////////////////////////////////////////////////////////////////////////////
38 //! Class template move_iterator is an iterator adaptor with the same behavior
39 //! as the underlying iterator except that its dereference operator implicitly
40 //! converts the value returned by the underlying iterator's dereference operator
41 //! to an rvalue reference. Some generic algorithms can be called with move
42 //! iterators to replace copying with moving.
47 typedef It iterator_type;
48 typedef typename boost::movelib::iterator_traits<iterator_type>::value_type value_type;
49 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
50 typedef value_type && reference;
52 typedef typename ::boost::move_detail::if_
53 < ::boost::has_move_emulation_enabled<value_type>
54 , ::boost::rv<value_type>&
55 , value_type & >::type reference;
58 typedef typename boost::movelib::iterator_traits<iterator_type>::difference_type difference_type;
59 typedef typename boost::movelib::iterator_traits<iterator_type>::iterator_category iterator_category;
61 BOOST_MOVE_FORCEINLINE move_iterator()
65 BOOST_MOVE_FORCEINLINE explicit move_iterator(const It &i)
70 BOOST_MOVE_FORCEINLINE move_iterator(const move_iterator<U>& u)
74 BOOST_MOVE_FORCEINLINE reference operator*() const
76 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES)
79 return ::boost::move(*m_it);
83 BOOST_MOVE_FORCEINLINE pointer operator->() const
86 BOOST_MOVE_FORCEINLINE move_iterator& operator++()
87 { ++m_it; return *this; }
89 BOOST_MOVE_FORCEINLINE move_iterator<iterator_type> operator++(int)
90 { move_iterator<iterator_type> tmp(*this); ++(*this); return tmp; }
92 BOOST_MOVE_FORCEINLINE move_iterator& operator--()
93 { --m_it; return *this; }
95 BOOST_MOVE_FORCEINLINE move_iterator<iterator_type> operator--(int)
96 { move_iterator<iterator_type> tmp(*this); --(*this); return tmp; }
98 move_iterator<iterator_type> operator+ (difference_type n) const
99 { return move_iterator<iterator_type>(m_it + n); }
101 BOOST_MOVE_FORCEINLINE move_iterator& operator+=(difference_type n)
102 { m_it += n; return *this; }
104 BOOST_MOVE_FORCEINLINE move_iterator<iterator_type> operator- (difference_type n) const
105 { return move_iterator<iterator_type>(m_it - n); }
107 BOOST_MOVE_FORCEINLINE move_iterator& operator-=(difference_type n)
108 { m_it -= n; return *this; }
110 BOOST_MOVE_FORCEINLINE reference operator[](difference_type n) const
112 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES)
115 return ::boost::move(m_it[n]);
119 BOOST_MOVE_FORCEINLINE friend bool operator==(const move_iterator& x, const move_iterator& y)
120 { return x.m_it == y.m_it; }
122 BOOST_MOVE_FORCEINLINE friend bool operator!=(const move_iterator& x, const move_iterator& y)
123 { return x.m_it != y.m_it; }
125 BOOST_MOVE_FORCEINLINE friend bool operator< (const move_iterator& x, const move_iterator& y)
126 { return x.m_it < y.m_it; }
128 BOOST_MOVE_FORCEINLINE friend bool operator<=(const move_iterator& x, const move_iterator& y)
129 { return x.m_it <= y.m_it; }
131 BOOST_MOVE_FORCEINLINE friend bool operator> (const move_iterator& x, const move_iterator& y)
132 { return x.m_it > y.m_it; }
134 BOOST_MOVE_FORCEINLINE friend bool operator>=(const move_iterator& x, const move_iterator& y)
135 { return x.m_it >= y.m_it; }
137 BOOST_MOVE_FORCEINLINE friend difference_type operator-(const move_iterator& x, const move_iterator& y)
138 { return x.m_it - y.m_it; }
140 BOOST_MOVE_FORCEINLINE friend move_iterator operator+(difference_type n, const move_iterator& x)
141 { return move_iterator(x.m_it + n); }
148 namespace move_detail {
151 struct is_move_iterator
153 static const bool value = false;
157 struct is_move_iterator< ::boost::move_iterator<I> >
159 static const bool value = true;
162 } //namespace move_detail {
164 //////////////////////////////////////////////////////////////////////////////
168 //////////////////////////////////////////////////////////////////////////////
171 //! <b>Returns</b>: move_iterator<It>(i).
173 inline move_iterator<It> make_move_iterator(const It &it)
174 { return move_iterator<It>(it); }
176 //////////////////////////////////////////////////////////////////////////////
178 // back_move_insert_iterator
180 //////////////////////////////////////////////////////////////////////////////
183 //! A move insert iterator that move constructs elements at the
184 //! back of a container
185 template <typename C> // C models Container
186 class back_move_insert_iterator
191 typedef C container_type;
192 typedef typename C::value_type value_type;
193 typedef typename C::reference reference;
194 typedef typename C::pointer pointer;
195 typedef typename C::difference_type difference_type;
196 typedef std::output_iterator_tag iterator_category;
198 explicit back_move_insert_iterator(C& x) : container_m(&x) { }
200 back_move_insert_iterator& operator=(reference x)
201 { container_m->push_back(boost::move(x)); return *this; }
203 back_move_insert_iterator& operator=(BOOST_RV_REF(value_type) x)
204 { reference rx = x; return this->operator=(rx); }
206 back_move_insert_iterator& operator*() { return *this; }
207 back_move_insert_iterator& operator++() { return *this; }
208 back_move_insert_iterator& operator++(int) { return *this; }
212 //! <b>Returns</b>: back_move_insert_iterator<C>(x).
213 template <typename C> // C models Container
214 inline back_move_insert_iterator<C> back_move_inserter(C& x)
216 return back_move_insert_iterator<C>(x);
219 //////////////////////////////////////////////////////////////////////////////
221 // front_move_insert_iterator
223 //////////////////////////////////////////////////////////////////////////////
225 //! A move insert iterator that move constructs elements int the
226 //! front of a container
227 template <typename C> // C models Container
228 class front_move_insert_iterator
233 typedef C container_type;
234 typedef typename C::value_type value_type;
235 typedef typename C::reference reference;
236 typedef typename C::pointer pointer;
237 typedef typename C::difference_type difference_type;
238 typedef std::output_iterator_tag iterator_category;
240 explicit front_move_insert_iterator(C& x) : container_m(&x) { }
242 front_move_insert_iterator& operator=(reference x)
243 { container_m->push_front(boost::move(x)); return *this; }
245 front_move_insert_iterator& operator=(BOOST_RV_REF(value_type) x)
246 { reference rx = x; return this->operator=(rx); }
248 front_move_insert_iterator& operator*() { return *this; }
249 front_move_insert_iterator& operator++() { return *this; }
250 front_move_insert_iterator& operator++(int) { return *this; }
254 //! <b>Returns</b>: front_move_insert_iterator<C>(x).
255 template <typename C> // C models Container
256 inline front_move_insert_iterator<C> front_move_inserter(C& x)
258 return front_move_insert_iterator<C>(x);
261 //////////////////////////////////////////////////////////////////////////////
263 // insert_move_iterator
265 //////////////////////////////////////////////////////////////////////////////
266 template <typename C> // C models Container
267 class move_insert_iterator
270 typename C::iterator pos_;
273 typedef C container_type;
274 typedef typename C::value_type value_type;
275 typedef typename C::reference reference;
276 typedef typename C::pointer pointer;
277 typedef typename C::difference_type difference_type;
278 typedef std::output_iterator_tag iterator_category;
280 explicit move_insert_iterator(C& x, typename C::iterator pos)
281 : container_m(&x), pos_(pos)
284 move_insert_iterator& operator=(reference x)
286 pos_ = container_m->insert(pos_, ::boost::move(x));
291 move_insert_iterator& operator=(BOOST_RV_REF(value_type) x)
292 { reference rx = x; return this->operator=(rx); }
294 move_insert_iterator& operator*() { return *this; }
295 move_insert_iterator& operator++() { return *this; }
296 move_insert_iterator& operator++(int) { return *this; }
300 //! <b>Returns</b>: move_insert_iterator<C>(x, it).
301 template <typename C> // C models Container
302 inline move_insert_iterator<C> move_inserter(C& x, typename C::iterator it)
304 return move_insert_iterator<C>(x, it);
307 } //namespace boost {
309 #include <boost/move/detail/config_end.hpp>
311 #endif //#ifndef BOOST_MOVE_ITERATOR_HPP