1 // (C) Copyright David Abrahams 2002.
2 // (C) Copyright Jeremy Siek 2002.
3 // (C) Copyright Thomas Witt 2002.
4 // Permission to copy, use, modify,
5 // sell and distribute this software is granted provided this
6 // copyright notice appears in all copies. This software is provided
7 // "as is" without express or implied warranty, and with no claim as
8 // to its suitability for any purpose.
9 #ifndef BOOST_TRANSFORM_ITERATOR_23022003THW_HPP
10 #define BOOST_TRANSFORM_ITERATOR_23022003THW_HPP
12 #include <boost/function.hpp>
13 #include <boost/iterator.hpp>
14 #include <boost/iterator/detail/enable_if.hpp>
15 #include <boost/iterator/iterator_adaptor.hpp>
16 #include <boost/iterator/iterator_categories.hpp>
17 #include <boost/mpl/not.hpp>
18 #include <boost/mpl/bool.hpp>
19 #include <boost/type_traits/function_traits.hpp>
20 #include <boost/type_traits/is_const.hpp>
21 #include <boost/type_traits/is_class.hpp>
22 #include <boost/type_traits/is_function.hpp>
23 #include <boost/type_traits/is_reference.hpp>
24 #include <boost/type_traits/remove_const.hpp>
25 #include <boost/type_traits/remove_reference.hpp>
27 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
28 # include <boost/type_traits/is_base_and_derived.hpp>
31 #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;
42 template <class UnaryFunction>
43 struct function_object_result
45 typedef typename UnaryFunction::result_type type;
48 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
49 template <class Return, class Argument>
50 struct function_object_result<Return(*)(Argument)>
56 // Compute the iterator_adaptor instantiation to be used for transform_iterator
57 template <class UnaryFunction, class Iterator, class Reference, class Value>
58 struct transform_iterator_base
61 // By default, dereferencing the iterator yields the same as
62 // the function. Do we need to adjust the way
63 // function_object_result is computed for the standard
64 // proposal (e.g. using Doug's result_of)?
65 typedef typename ia_dflt_help<
67 , function_object_result<UnaryFunction>
70 // To get the default for Value: remove any reference on the
71 // result type, but retain any constness to signal
72 // non-writability. Note that if we adopt Thomas' suggestion
73 // to key non-writability *only* on the Reference argument,
74 // we'd need to strip constness here as well.
75 typedef typename ia_dflt_help<
77 , remove_reference<reference>
78 >::type cv_value_type;
81 typedef iterator_adaptor<
82 transform_iterator<UnaryFunction, Iterator, Reference, Value>
85 , use_default // Leave the traversal category alone
91 template <class UnaryFunction, class Iterator, class Reference, class Value>
92 class transform_iterator
93 : public detail::transform_iterator_base<UnaryFunction, Iterator, Reference, Value>::type
96 detail::transform_iterator_base<UnaryFunction, Iterator, Reference, Value>::type
99 friend class iterator_core_access;
102 transform_iterator() { }
104 transform_iterator(Iterator const& x, UnaryFunction f)
105 : super_t(x), m_f(f) { }
107 explicit transform_iterator(Iterator const& x)
110 // Pro8 is a little too aggressive about instantiating the
111 // body of this function.
112 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
113 // don't provide this constructor if UnaryFunction is a
114 // function pointer type, since it will be 0. Too dangerous.
115 BOOST_STATIC_ASSERT(is_class<UnaryFunction>::value);
120 class OtherUnaryFunction
121 , class OtherIterator
122 , class OtherReference
125 transform_iterator<OtherUnaryFunction, OtherIterator, OtherReference, OtherValue> const& t
126 , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
127 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
128 , typename enable_if_convertible<OtherUnaryFunction, UnaryFunction>::type* = 0
131 : super_t(t.base()), m_f(t.functor())
134 UnaryFunction functor() const
138 typename super_t::reference dereference() const
139 { return m_f(*this->base()); }
141 // Probably should be the initial base class so it can be
142 // optimized away via EBO if it is an empty class.
146 template <class UnaryFunction, class Iterator>
147 transform_iterator<UnaryFunction, Iterator>
148 make_transform_iterator(Iterator it, UnaryFunction fun)
150 return transform_iterator<UnaryFunction, Iterator>(it, fun);
153 // Version which allows explicit specification of the UnaryFunction
156 // This generator is not provided if UnaryFunction is a function
157 // pointer type, because it's too dangerous: the default-constructed
158 // function pointer in the iterator be 0, leading to a runtime
160 template <class UnaryFunction, class Iterator>
161 typename iterators::enable_if<
162 is_class<UnaryFunction> // We should probably find a cheaper test than is_class<>
163 , transform_iterator<UnaryFunction, Iterator>
165 make_transform_iterator(Iterator it)
167 return transform_iterator<UnaryFunction, Iterator>(it, UnaryFunction());
170 #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
171 template <class Return, class Argument, class Iterator>
172 transform_iterator< Return (*)(Argument), Iterator, Return>
173 make_transform_iterator(Iterator it, Return (*fun)(Argument))
175 return transform_iterator<Return (*)(Argument), Iterator, Return>(it, fun);
181 #include <boost/iterator/detail/config_undef.hpp>
183 #endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP