1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2015.
4 // (C) Copyright Gennaro Prota 2003 - 2004.
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // See http://www.boost.org/libs/interprocess for documentation.
12 //////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTERPROCESS_DETAIL_TRANSFORM_ITERATORS_HPP
15 #define BOOST_INTERPROCESS_DETAIL_TRANSFORM_ITERATORS_HPP
17 #ifndef BOOST_CONFIG_HPP
18 # include <boost/config.hpp>
21 #if defined(BOOST_HAS_PRAGMA_ONCE)
25 #include <boost/interprocess/detail/config_begin.hpp>
26 #include <boost/interprocess/detail/workaround.hpp>
29 #include <boost/interprocess/interprocess_fwd.hpp>
30 // interprocess/detail
31 #include <boost/interprocess/detail/type_traits.hpp>
33 #include <boost/container/detail/iterator.hpp>
36 namespace interprocess {
38 template <class PseudoReference>
39 struct operator_arrow_proxy
41 operator_arrow_proxy(const PseudoReference &px)
45 PseudoReference* operator->() const { return &m_value; }
46 // This function is needed for MWCW and BCC, which won't call operator->
47 // again automatically per 13.3.1.2 para 8
48 // operator T*() const { return &m_value; }
49 mutable PseudoReference m_value;
53 struct operator_arrow_proxy<T&>
55 operator_arrow_proxy(T &px)
59 T* operator->() const { return const_cast<T*>(&m_value); }
60 // This function is needed for MWCW and BCC, which won't call operator->
61 // again automatically per 13.3.1.2 para 8
62 // operator T*() const { return &m_value; }
66 template <class Iterator, class UnaryFunction>
67 class transform_iterator
68 : public UnaryFunction
71 typedef typename ::boost::container::iterator_traits<Iterator>::iterator_category iterator_category;
72 typedef typename ipcdetail::remove_reference<typename UnaryFunction::result_type>::type value_type;
73 typedef typename ::boost::container::iterator_traits<Iterator>::difference_type difference_type;
74 typedef operator_arrow_proxy<typename UnaryFunction::result_type> pointer;
75 typedef typename UnaryFunction::result_type reference;
77 explicit transform_iterator(const Iterator &it, const UnaryFunction &f = UnaryFunction())
78 : UnaryFunction(f), m_it(it)
81 explicit transform_iterator()
82 : UnaryFunction(), m_it()
86 transform_iterator& operator++()
87 { increment(); return *this; }
89 transform_iterator operator++(int)
91 transform_iterator result (*this);
96 transform_iterator& operator--()
97 { decrement(); return *this; }
99 transform_iterator operator--(int)
101 transform_iterator result (*this);
106 friend bool operator== (const transform_iterator& i, const transform_iterator& i2)
107 { return i.equal(i2); }
109 friend bool operator!= (const transform_iterator& i, const transform_iterator& i2)
110 { return !(i == i2); }
112 friend bool operator< (const transform_iterator& i, const transform_iterator& i2)
115 friend bool operator> (const transform_iterator& i, const transform_iterator& i2)
118 friend bool operator<= (const transform_iterator& i, const transform_iterator& i2)
119 { return !(i > i2); }
121 friend bool operator>= (const transform_iterator& i, const transform_iterator& i2)
122 { return !(i < i2); }
124 friend difference_type operator- (const transform_iterator& i, const transform_iterator& i2)
125 { return i2.distance_to(i); }
128 transform_iterator& operator+=(difference_type off)
129 { this->advance(off); return *this; }
131 transform_iterator operator+(difference_type off) const
133 transform_iterator other(*this);
138 friend transform_iterator operator+(difference_type off, const transform_iterator& right)
139 { return right + off; }
141 transform_iterator& operator-=(difference_type off)
142 { this->advance(-off); return *this; }
144 transform_iterator operator-(difference_type off) const
145 { return *this + (-off); }
147 typename UnaryFunction::result_type operator*() const
148 { return dereference(); }
150 typename UnaryFunction::result_type operator[](difference_type off) const
151 { return UnaryFunction::operator()(m_it[off]); }
153 operator_arrow_proxy<typename UnaryFunction::result_type>
155 { return operator_arrow_proxy<typename UnaryFunction::result_type>(dereference()); }
160 const Iterator & base() const
172 bool equal(const transform_iterator &other) const
173 { return m_it == other.m_it; }
175 bool less(const transform_iterator &other) const
176 { return other.m_it < m_it; }
178 typename UnaryFunction::result_type dereference() const
179 { return UnaryFunction::operator()(*m_it); }
181 void advance(difference_type n)
182 { ::boost::container::iterator_advance(m_it, n); }
184 difference_type distance_to(const transform_iterator &other)const
185 { return ::boost::container::iterator_distance(other.m_it, m_it); }
188 template <class Iterator, class UnaryFunc>
189 transform_iterator<Iterator, UnaryFunc>
190 make_transform_iterator(Iterator it, UnaryFunc fun)
192 return transform_iterator<Iterator, UnaryFunc>(it, fun);
195 } //namespace interprocess {
196 } //namespace boost {
198 #include <boost/interprocess/detail/config_end.hpp>
200 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_TRANSFORM_ITERATORS_HPP