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
10 #include <boost/iterator.hpp>
11 #include <boost/iterator/detail/enable_if.hpp>
12 #include <boost/iterator/iterator_adaptor.hpp>
13 #include <boost/iterator/iterator_categories.hpp>
14 #include <boost/mpl/not.hpp>
15 #include <boost/mpl/bool.hpp>
16 #include <boost/type_traits/function_traits.hpp>
17 #include <boost/type_traits/is_const.hpp>
18 #include <boost/type_traits/is_class.hpp>
19 #include <boost/type_traits/is_function.hpp>
20 #include <boost/type_traits/is_reference.hpp>
21 #include <boost/type_traits/remove_const.hpp>
22 #include <boost/type_traits/remove_reference.hpp>
24 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
25 # include <boost/type_traits/is_base_and_derived.hpp>
28 #include <boost/iterator/detail/config_def.hpp>
33 template <class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
34 class transform_iterator;
39 template <class UnaryFunc>
40 struct function_object_result
42 typedef typename UnaryFunc::result_type type;
45 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
46 template <class Return, class Argument>
47 struct function_object_result<Return(*)(Argument)>
53 // Compute the iterator_adaptor instantiation to be used for transform_iterator
54 template <class UnaryFunc, class Iterator, class Reference, class Value>
55 struct transform_iterator_base
58 // By default, dereferencing the iterator yields the same as
59 // the function. Do we need to adjust the way
60 // function_object_result is computed for the standard
61 // proposal (e.g. using Doug's result_of)?
62 typedef typename ia_dflt_help<
64 , function_object_result<UnaryFunc>
67 // To get the default for Value: remove any reference on the
68 // result type, but retain any constness to signal
69 // non-writability. Note that if we adopt Thomas' suggestion
70 // to key non-writability *only* on the Reference argument,
71 // we'd need to strip constness here as well.
72 typedef typename ia_dflt_help<
74 , remove_reference<reference>
75 >::type cv_value_type;
78 typedef iterator_adaptor<
79 transform_iterator<UnaryFunc, Iterator, Reference, Value>
82 , use_default // Leave the traversal category alone
88 template <class UnaryFunc, class Iterator, class Reference, class Value>
89 class transform_iterator
90 : public boost::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
93 boost::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
96 friend class iterator_core_access;
99 transform_iterator() { }
101 transform_iterator(Iterator const& x, UnaryFunc f)
102 : super_t(x), m_f(f) { }
104 explicit transform_iterator(Iterator const& x)
107 // Pro8 is a little too aggressive about instantiating the
108 // body of this function.
109 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
110 // don't provide this constructor if UnaryFunc is a
111 // function pointer type, since it will be 0. Too dangerous.
112 BOOST_STATIC_ASSERT(is_class<UnaryFunc>::value);
117 class OtherUnaryFunction
118 , class OtherIterator
119 , class OtherReference
122 transform_iterator<OtherUnaryFunction, OtherIterator, OtherReference, OtherValue> const& t
123 , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
124 #if !BOOST_WORKAROUND(BOOST_MSVC, == 1310)
125 , typename enable_if_convertible<OtherUnaryFunction, UnaryFunc>::type* = 0
128 : super_t(t.base()), m_f(t.functor())
131 UnaryFunc functor() const
135 typename super_t::reference dereference() const
136 { return m_f(*this->base()); }
138 // Probably should be the initial base class so it can be
139 // optimized away via EBO if it is an empty class.
143 template <class UnaryFunc, class Iterator>
144 transform_iterator<UnaryFunc, Iterator>
145 make_transform_iterator(Iterator it, UnaryFunc fun)
147 return transform_iterator<UnaryFunc, Iterator>(it, fun);
150 // Version which allows explicit specification of the UnaryFunc
153 // This generator is not provided if UnaryFunc is a function
154 // pointer type, because it's too dangerous: the default-constructed
155 // function pointer in the iterator be 0, leading to a runtime
157 template <class UnaryFunc, class Iterator>
158 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
161 typename iterators::enable_if<
163 is_class<UnaryFunc> // We should probably find a cheaper test than is_class<>
164 , transform_iterator<UnaryFunc, Iterator>
165 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
169 make_transform_iterator(Iterator it)
171 return transform_iterator<UnaryFunc, Iterator>(it, UnaryFunc());
174 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
175 template <class Return, class Argument, class Iterator>
176 transform_iterator< Return (*)(Argument), Iterator, Return>
177 make_transform_iterator(Iterator it, Return (*fun)(Argument))
179 return transform_iterator<Return (*)(Argument), Iterator, Return>(it, fun);
185 #include <boost/iterator/detail/config_undef.hpp>
187 #endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP