]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/iterator/transform_iterator.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / iterator / transform_iterator.hpp
1 // (C) Copyright David Abrahams 2002.
2 // (C) Copyright Jeremy Siek 2002.
3 // (C) Copyright Thomas Witt 2002.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_TRANSFORM_ITERATOR_23022003THW_HPP
8 #define BOOST_TRANSFORM_ITERATOR_23022003THW_HPP
9
10 #include <boost/config.hpp>
11 #include <boost/config/workaround.hpp>
12 #include <boost/iterator/detail/enable_if.hpp>
13 #include <boost/iterator/iterator_adaptor.hpp>
14 #include <boost/iterator/iterator_categories.hpp>
15 #include <boost/type_traits/function_traits.hpp>
16 #include <boost/type_traits/is_const.hpp>
17 #include <boost/type_traits/is_class.hpp>
18 #include <boost/type_traits/is_function.hpp>
19 #include <boost/type_traits/is_reference.hpp>
20 #include <boost/type_traits/remove_const.hpp>
21 #include <boost/type_traits/remove_reference.hpp>
22 #include <boost/utility/result_of.hpp>
23
24 #include <iterator>
25
26 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
27 #include <boost/type_traits/is_base_and_derived.hpp>
28 #endif
29
30 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
31 #include <boost/static_assert.hpp>
32 #endif
33
34 #include <boost/iterator/detail/config_def.hpp>
35
36
37 namespace boost {
38 namespace iterators {
39
40 template <class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
41 class transform_iterator;
42
43 namespace detail
44 {
45 // Compute the iterator_adaptor instantiation to be used for transform_iterator
46 template <class UnaryFunc, class Iterator, class Reference, class Value>
47 struct transform_iterator_base
48 {
49 private:
50 // By default, dereferencing the iterator yields the same as
51 // the function.
52 typedef typename ia_dflt_help<
53 Reference
54 #ifdef BOOST_RESULT_OF_USE_TR1
55 , result_of<const UnaryFunc(typename std::iterator_traits<Iterator>::reference)>
56 #else
57 , result_of<const UnaryFunc&(typename std::iterator_traits<Iterator>::reference)>
58 #endif
59 >::type reference;
60
61 // To get the default for Value: remove any reference on the
62 // result type, but retain any constness to signal
63 // non-writability. Note that if we adopt Thomas' suggestion
64 // to key non-writability *only* on the Reference argument,
65 // we'd need to strip constness here as well.
66 typedef typename ia_dflt_help<
67 Value
68 , remove_reference<reference>
69 >::type cv_value_type;
70
71 public:
72 typedef iterator_adaptor<
73 transform_iterator<UnaryFunc, Iterator, Reference, Value>
74 , Iterator
75 , cv_value_type
76 , use_default // Leave the traversal category alone
77 , reference
78 > type;
79 };
80 }
81
82 template <class UnaryFunc, class Iterator, class Reference, class Value>
83 class transform_iterator
84 : public boost::iterators::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
85 {
86 typedef typename
87 boost::iterators::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
88 super_t;
89
90 friend class iterator_core_access;
91
92 public:
93 transform_iterator() { }
94
95 transform_iterator(Iterator const& x, UnaryFunc f)
96 : super_t(x), m_f(f) { }
97
98 explicit transform_iterator(Iterator const& x)
99 : super_t(x)
100 {
101 // Pro8 is a little too aggressive about instantiating the
102 // body of this function.
103 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
104 // don't provide this constructor if UnaryFunc is a
105 // function pointer type, since it will be 0. Too dangerous.
106 BOOST_STATIC_ASSERT(is_class<UnaryFunc>::value);
107 #endif
108 }
109
110 template <
111 class OtherUnaryFunction
112 , class OtherIterator
113 , class OtherReference
114 , class OtherValue>
115 transform_iterator(
116 transform_iterator<OtherUnaryFunction, OtherIterator, OtherReference, OtherValue> const& t
117 , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
118 #if !BOOST_WORKAROUND(BOOST_MSVC, == 1310)
119 , typename enable_if_convertible<OtherUnaryFunction, UnaryFunc>::type* = 0
120 #endif
121 )
122 : super_t(t.base()), m_f(t.functor())
123 {}
124
125 UnaryFunc functor() const
126 { return m_f; }
127
128 private:
129 typename super_t::reference dereference() const
130 { return m_f(*this->base()); }
131
132 // Probably should be the initial base class so it can be
133 // optimized away via EBO if it is an empty class.
134 UnaryFunc m_f;
135 };
136
137 template <class UnaryFunc, class Iterator>
138 inline transform_iterator<UnaryFunc, Iterator>
139 make_transform_iterator(Iterator it, UnaryFunc fun)
140 {
141 return transform_iterator<UnaryFunc, Iterator>(it, fun);
142 }
143
144 // Version which allows explicit specification of the UnaryFunc
145 // type.
146 //
147 // This generator is not provided if UnaryFunc is a function
148 // pointer type, because it's too dangerous: the default-constructed
149 // function pointer in the iterator be 0, leading to a runtime
150 // crash.
151 template <class UnaryFunc, class Iterator>
152 inline typename iterators::enable_if<
153 is_class<UnaryFunc> // We should probably find a cheaper test than is_class<>
154 , transform_iterator<UnaryFunc, Iterator>
155 >::type
156 make_transform_iterator(Iterator it)
157 {
158 return transform_iterator<UnaryFunc, Iterator>(it, UnaryFunc());
159 }
160
161 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
162 template <class Return, class Argument, class Iterator>
163 inline transform_iterator< Return (*)(Argument), Iterator, Return>
164 make_transform_iterator(Iterator it, Return (*fun)(Argument))
165 {
166 return transform_iterator<Return (*)(Argument), Iterator, Return>(it, fun);
167 }
168 #endif
169
170 } // namespace iterators
171
172 using iterators::transform_iterator;
173 using iterators::make_transform_iterator;
174
175 } // namespace boost
176
177 #include <boost/iterator/detail/config_undef.hpp>
178
179 #endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP