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/detail/enable_if.hpp>
11 #include <boost/iterator/iterator_adaptor.hpp>
12 #include <boost/iterator/iterator_categories.hpp>
13 #include <boost/mpl/not.hpp>
14 #include <boost/mpl/bool.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>
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>
36 template <class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
37 class transform_iterator;
41 // Compute the iterator_adaptor instantiation to be used for transform_iterator
42 template <class UnaryFunc, class Iterator, class Reference, class Value>
43 struct transform_iterator_base
46 // By default, dereferencing the iterator yields the same as
48 typedef typename ia_dflt_help<
50 #ifdef BOOST_RESULT_OF_USE_TR1
51 , result_of<const UnaryFunc(typename std::iterator_traits<Iterator>::reference)>
53 , result_of<const UnaryFunc&(typename std::iterator_traits<Iterator>::reference)>
57 // To get the default for Value: remove any reference on the
58 // result type, but retain any constness to signal
59 // non-writability. Note that if we adopt Thomas' suggestion
60 // to key non-writability *only* on the Reference argument,
61 // we'd need to strip constness here as well.
62 typedef typename ia_dflt_help<
64 , remove_reference<reference>
65 >::type cv_value_type;
68 typedef iterator_adaptor<
69 transform_iterator<UnaryFunc, Iterator, Reference, Value>
72 , use_default // Leave the traversal category alone
78 template <class UnaryFunc, class Iterator, class Reference, class Value>
79 class transform_iterator
80 : public boost::iterators::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
83 boost::iterators::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
86 friend class iterator_core_access;
89 transform_iterator() { }
91 transform_iterator(Iterator const& x, UnaryFunc f)
92 : super_t(x), m_f(f) { }
94 explicit transform_iterator(Iterator const& x)
97 // Pro8 is a little too aggressive about instantiating the
98 // body of this function.
99 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
100 // don't provide this constructor if UnaryFunc is a
101 // function pointer type, since it will be 0. Too dangerous.
102 BOOST_STATIC_ASSERT(is_class<UnaryFunc>::value);
107 class OtherUnaryFunction
108 , class OtherIterator
109 , class OtherReference
112 transform_iterator<OtherUnaryFunction, OtherIterator, OtherReference, OtherValue> const& t
113 , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
114 #if !BOOST_WORKAROUND(BOOST_MSVC, == 1310)
115 , typename enable_if_convertible<OtherUnaryFunction, UnaryFunc>::type* = 0
118 : super_t(t.base()), m_f(t.functor())
121 UnaryFunc functor() const
125 typename super_t::reference dereference() const
126 { return m_f(*this->base()); }
128 // Probably should be the initial base class so it can be
129 // optimized away via EBO if it is an empty class.
133 template <class UnaryFunc, class Iterator>
134 inline transform_iterator<UnaryFunc, Iterator>
135 make_transform_iterator(Iterator it, UnaryFunc fun)
137 return transform_iterator<UnaryFunc, Iterator>(it, fun);
140 // Version which allows explicit specification of the UnaryFunc
143 // This generator is not provided if UnaryFunc is a function
144 // pointer type, because it's too dangerous: the default-constructed
145 // function pointer in the iterator be 0, leading to a runtime
147 template <class UnaryFunc, class Iterator>
148 inline typename iterators::enable_if<
149 is_class<UnaryFunc> // We should probably find a cheaper test than is_class<>
150 , transform_iterator<UnaryFunc, Iterator>
152 make_transform_iterator(Iterator it)
154 return transform_iterator<UnaryFunc, Iterator>(it, UnaryFunc());
157 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
158 template <class Return, class Argument, class Iterator>
159 inline transform_iterator< Return (*)(Argument), Iterator, Return>
160 make_transform_iterator(Iterator it, Return (*fun)(Argument))
162 return transform_iterator<Return (*)(Argument), Iterator, Return>(it, fun);
166 } // namespace iterators
168 using iterators::transform_iterator;
169 using iterators::make_transform_iterator;
173 #include <boost/iterator/detail/config_undef.hpp>
175 #endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP