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 {
operator_arrow_proxyboost::intrusive::detail::operator_arrow_proxy36    BOOST_INTRUSIVE_FORCEINLINE operator_arrow_proxy(const PseudoReference &px)
37       :  m_value(px)
38    {}
39 
operator ->boost::intrusive::detail::operator_arrow_proxy40    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 {
operator_arrow_proxyboost::intrusive::detail::operator_arrow_proxy50    BOOST_INTRUSIVE_FORCEINLINE operator_arrow_proxy(T &px)
51       :  m_value(px)
52    {}
53 
operator ->boost::intrusive::detail::operator_arrow_proxy54    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 
transform_iterator(const Iterator & it,const UnaryFunction & f=UnaryFunction ())71    explicit transform_iterator(const Iterator &it, const UnaryFunction &f = UnaryFunction())
72       :  members_(it, f)
73    {}
74 
transform_iterator()75    explicit transform_iterator()
76       :  members_()
77    {}
78 
get_it() const79    BOOST_INTRUSIVE_FORCEINLINE Iterator get_it() const
80    {  return members_.m_it;   }
81 
82    //Constructors
operator ++()83    BOOST_INTRUSIVE_FORCEINLINE transform_iterator& operator++()
84    { increment();   return *this;   }
85 
operator ++(int)86    BOOST_INTRUSIVE_FORCEINLINE transform_iterator operator++(int)
87    {
88       transform_iterator result (*this);
89       increment();
90       return result;
91    }
92 
operator ==(const transform_iterator & i,const transform_iterator & i2)93    BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const transform_iterator& i, const transform_iterator& i2)
94    { return i.equal(i2); }
95 
operator !=(const transform_iterator & i,const transform_iterator & i2)96    BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const transform_iterator& i, const transform_iterator& i2)
97    { return !(i == i2); }
98 
operator -(const transform_iterator & i,const transform_iterator & 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); }
101 
102    //Arithmetic
operator +=(typename Iterator::difference_type off)103    transform_iterator& operator+=(typename Iterator::difference_type off)
104    {  this->advance(off); return *this;   }
105 
operator +(typename Iterator::difference_type off) const106    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 
operator +(typename Iterator::difference_type off,const transform_iterator & right)113    BOOST_INTRUSIVE_FORCEINLINE friend transform_iterator operator+(typename Iterator::difference_type off, const transform_iterator& right)
114    {  return right + off; }
115 
operator -=(typename Iterator::difference_type off)116    BOOST_INTRUSIVE_FORCEINLINE transform_iterator& operator-=(typename Iterator::difference_type off)
117    {  this->advance(-off); return *this;   }
118 
operator -(typename Iterator::difference_type off) const119    BOOST_INTRUSIVE_FORCEINLINE transform_iterator operator-(typename Iterator::difference_type off) const
120    {  return *this + (-off);  }
121 
operator *() const122    BOOST_INTRUSIVE_FORCEINLINE typename UnaryFunction::result_type operator*() const
123    { return dereference(); }
124 
125    BOOST_INTRUSIVE_FORCEINLINE operator_arrow_proxy<typename UnaryFunction::result_type>
operator ->() const126       operator->() const
127    { return operator_arrow_proxy<typename UnaryFunction::result_type>(dereference());  }
128 
129    private:
130    struct members
131       :  UnaryFunction
132    {
membersboost::intrusive::detail::transform_iterator::members133       BOOST_INTRUSIVE_FORCEINLINE members(const Iterator &it, const UnaryFunction &f)
134          :  UnaryFunction(f), m_it(it)
135       {}
136 
membersboost::intrusive::detail::transform_iterator::members137       BOOST_INTRUSIVE_FORCEINLINE members()
138       {}
139 
140       Iterator m_it;
141    } members_;
142 
143 
increment()144    BOOST_INTRUSIVE_FORCEINLINE void increment()
145    { ++members_.m_it; }
146 
decrement()147    BOOST_INTRUSIVE_FORCEINLINE void decrement()
148    { --members_.m_it; }
149 
equal(const transform_iterator & other) const150    BOOST_INTRUSIVE_FORCEINLINE bool equal(const transform_iterator &other) const
151    {  return members_.m_it == other.members_.m_it;   }
152 
less(const transform_iterator & other) const153    BOOST_INTRUSIVE_FORCEINLINE bool less(const transform_iterator &other) const
154    {  return other.members_.m_it < members_.m_it;   }
155 
dereference() const156    typename UnaryFunction::result_type dereference() const
157    { return members_(*members_.m_it); }
158 
advance(typename Iterator::difference_type n)159    void advance(typename Iterator::difference_type n)
160    {  boost::intrusive::iterator_advance(members_.m_it, n); }
161 
distance_to(const transform_iterator & other) const162    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