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>
23 #include <boost/utility/result_of.hpp>
26 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
27 # include <boost/type_traits/is_base_and_derived.hpp>
30 #include <boost/iterator/detail/config_def.hpp>
35 template <class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
36 class transform_iterator;
40 // Compute the iterator_adaptor instantiation to be used for transform_iterator
41 template <class UnaryFunc, class Iterator, class Reference, class Value>
42 struct transform_iterator_base
45 // By default, dereferencing the iterator yields the same as
47 typedef typename ia_dflt_help<
49 , result_of<UnaryFunc(typename std::iterator_traits<Iterator>::reference)>
52 // To get the default for Value: remove any reference on the
53 // result type, but retain any constness to signal
54 // non-writability. Note that if we adopt Thomas' suggestion
55 // to key non-writability *only* on the Reference argument,
56 // we'd need to strip constness here as well.
57 typedef typename ia_dflt_help<
59 , remove_reference<reference>
60 >::type cv_value_type;
63 typedef iterator_adaptor<
64 transform_iterator<UnaryFunc, Iterator, Reference, Value>
67 , use_default // Leave the traversal category alone
73 template <class UnaryFunc, class Iterator, class Reference, class Value>
74 class transform_iterator
75 : public boost::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
78 boost::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
81 friend class iterator_core_access;
84 transform_iterator() { }
86 transform_iterator(Iterator const& x, UnaryFunc f)
87 : super_t(x), m_f(f) { }
89 explicit transform_iterator(Iterator const& x)
92 // Pro8 is a little too aggressive about instantiating the
93 // body of this function.
94 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
95 // don't provide this constructor if UnaryFunc is a
96 // function pointer type, since it will be 0. Too dangerous.
97 BOOST_STATIC_ASSERT(is_class<UnaryFunc>::value);
102 class OtherUnaryFunction
103 , class OtherIterator
104 , class OtherReference
107 transform_iterator<OtherUnaryFunction, OtherIterator, OtherReference, OtherValue> const& t
108 , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
109 #if !BOOST_WORKAROUND(BOOST_MSVC, == 1310)
110 , typename enable_if_convertible<OtherUnaryFunction, UnaryFunc>::type* = 0
113 : super_t(t.base()), m_f(t.functor())
116 UnaryFunc functor() const
120 typename super_t::reference dereference() const
121 { return m_f(*this->base()); }
123 // Probably should be the initial base class so it can be
124 // optimized away via EBO if it is an empty class.
128 template <class UnaryFunc, class Iterator>
129 transform_iterator<UnaryFunc, Iterator>
130 make_transform_iterator(Iterator it, UnaryFunc fun)
132 return transform_iterator<UnaryFunc, Iterator>(it, fun);
135 // Version which allows explicit specification of the UnaryFunc
138 // This generator is not provided if UnaryFunc is a function
139 // pointer type, because it's too dangerous: the default-constructed
140 // function pointer in the iterator be 0, leading to a runtime
142 template <class UnaryFunc, class Iterator>
143 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
146 typename iterators::enable_if<
148 is_class<UnaryFunc> // We should probably find a cheaper test than is_class<>
149 , transform_iterator<UnaryFunc, Iterator>
150 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
154 make_transform_iterator(Iterator it)
156 return transform_iterator<UnaryFunc, Iterator>(it, UnaryFunc());
159 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
160 template <class Return, class Argument, class Iterator>
161 transform_iterator< Return (*)(Argument), Iterator, Return>
162 make_transform_iterator(Iterator it, Return (*fun)(Argument))
164 return transform_iterator<Return (*)(Argument), Iterator, Return>(it, fun);
170 #include <boost/iterator/detail/config_undef.hpp>
172 #endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP