fix doc example typo
[boost.git] / boost / interprocess / detail / transform_iterator.hpp
blobeb081c2c724db8f6dada26e5225c7bddf20d8738
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2008.
4 // (C) Copyright Gennaro Prota 2003 - 2004.
5 //
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)
9 //
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 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
18 # pragma once
19 #endif
21 #include <boost/interprocess/detail/config_begin.hpp>
22 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/interprocess_fwd.hpp>
26 #include <iterator>
27 #include <boost/interprocess/detail/type_traits.hpp>
29 namespace boost {
30 namespace interprocess {
32 template <class PseudoReference>
33 struct operator_arrow_proxy
35 operator_arrow_proxy(const PseudoReference &px)
36 : m_value(px)
39 PseudoReference* operator->() const { return &m_value; }
40 // This function is needed for MWCW and BCC, which won't call operator->
41 // again automatically per 13.3.1.2 para 8
42 // operator T*() const { return &m_value; }
43 mutable PseudoReference m_value;
46 template <class T>
47 struct operator_arrow_proxy<T&>
49 operator_arrow_proxy(T &px)
50 : m_value(px)
53 T* operator->() const { return &m_value; }
54 // This function is needed for MWCW and BCC, which won't call operator->
55 // again automatically per 13.3.1.2 para 8
56 // operator T*() const { return &m_value; }
57 mutable T &m_value;
60 template <class Iterator, class UnaryFunction>
61 class transform_iterator
62 : public UnaryFunction
63 , public std::iterator
64 < typename Iterator::iterator_category
65 , typename detail::remove_reference<typename UnaryFunction::result_type>::type
66 , typename Iterator::difference_type
67 , operator_arrow_proxy<typename UnaryFunction::result_type>
68 , typename UnaryFunction::result_type>
70 public:
71 explicit transform_iterator(const Iterator &it, const UnaryFunction &f = UnaryFunction())
72 : UnaryFunction(f), m_it(it)
75 explicit transform_iterator()
76 : UnaryFunction(), m_it()
79 //Constructors
80 transform_iterator& operator++()
81 { increment(); return *this; }
83 transform_iterator operator++(int)
85 transform_iterator result (*this);
86 increment();
87 return result;
90 friend bool operator== (const transform_iterator& i, const transform_iterator& i2)
91 { return i.equal(i2); }
93 friend bool operator!= (const transform_iterator& i, const transform_iterator& i2)
94 { return !(i == i2); }
97 friend bool operator> (const transform_iterator& i, const transform_iterator& i2)
98 { return i2 < i; }
100 friend bool operator<= (const transform_iterator& i, const transform_iterator& i2)
101 { return !(i > i2); }
103 friend bool operator>= (const transform_iterator& i, const transform_iterator& i2)
104 { return !(i < i2); }
106 friend typename Iterator::difference_type operator- (const transform_iterator& i, const transform_iterator& i2)
107 { return i2.distance_to(i); }
109 //Arithmetic
110 transform_iterator& operator+=(typename Iterator::difference_type off)
111 { this->advance(off); return *this; }
113 transform_iterator operator+(typename Iterator::difference_type off) const
115 transform_iterator other(*this);
116 other.advance(off);
117 return other;
120 friend transform_iterator operator+(typename Iterator::difference_type off, const transform_iterator& right)
121 { return right + off; }
123 transform_iterator& operator-=(typename Iterator::difference_type off)
124 { this->advance(-off); return *this; }
126 transform_iterator operator-(typename Iterator::difference_type off) const
127 { return *this + (-off); }
129 typename UnaryFunction::result_type operator*() const
130 { return dereference(); }
132 operator_arrow_proxy<typename UnaryFunction::result_type>
133 operator->() const
134 { return operator_arrow_proxy<typename UnaryFunction::result_type>(dereference()); }
136 Iterator & base()
137 { return m_it; }
139 const Iterator & base() const
140 { return m_it; }
142 private:
143 Iterator m_it;
145 void increment()
146 { ++m_it; }
148 void decrement()
149 { --m_it; }
151 bool equal(const transform_iterator &other) const
152 { return m_it == other.m_it; }
154 bool less(const transform_iterator &other) const
155 { return other.m_it < m_it; }
157 typename UnaryFunction::result_type dereference() const
158 { return UnaryFunction::operator()(*m_it); }
160 void advance(typename Iterator::difference_type n)
161 { std::advance(m_it, n); }
163 typename Iterator::difference_type distance_to(const transform_iterator &other)const
164 { return std::distance(other.m_it, m_it); }
167 template <class Iterator, class UnaryFunc>
168 transform_iterator<Iterator, UnaryFunc>
169 make_transform_iterator(Iterator it, UnaryFunc fun)
171 return transform_iterator<Iterator, UnaryFunc>(it, fun);
174 } //namespace interprocess {
175 } //namespace boost {
177 #include <boost/interprocess/detail/config_end.hpp>
179 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_TRANSFORM_ITERATORS_HPP