]> git.lyx.org Git - lyx.git/blob - boost/boost/iterator/transform_iterator.hpp
Boost 1.31.0
[lyx.git] / 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 // 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
11
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>
26
27 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
28 # include <boost/type_traits/is_base_and_derived.hpp>
29
30 #endif 
31 #include <boost/iterator/detail/config_def.hpp>
32
33
34 namespace boost
35 {
36   template <class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
37   class transform_iterator;
38
39   namespace detail 
40   {
41
42     template <class UnaryFunction>
43     struct function_object_result
44     {
45       typedef typename UnaryFunction::result_type type;
46     };
47
48 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
49     template <class Return, class Argument>
50     struct function_object_result<Return(*)(Argument)>
51     {
52       typedef Return type;
53     };
54 #endif
55
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
59     {
60      private:
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<
66             Reference
67           , function_object_result<UnaryFunction>
68         >::type reference;
69
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<
76             Value
77           , remove_reference<reference>
78         >::type cv_value_type;
79
80      public:
81         typedef iterator_adaptor<
82             transform_iterator<UnaryFunction, Iterator, Reference, Value>
83           , Iterator
84           , cv_value_type
85           , use_default    // Leave the traversal category alone
86           , reference
87         > type;
88     };
89   }
90
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
94   {
95     typedef typename
96     detail::transform_iterator_base<UnaryFunction, Iterator, Reference, Value>::type
97     super_t;
98
99     friend class iterator_core_access;
100
101   public:
102     transform_iterator() { }
103
104     transform_iterator(Iterator const& x, UnaryFunction f)
105       : super_t(x), m_f(f) { }
106
107     explicit transform_iterator(Iterator const& x)
108       : super_t(x)
109     {
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);
116 #endif 
117     }
118
119     template<
120         class OtherUnaryFunction
121       , class OtherIterator
122       , class OtherReference
123       , class OtherValue>
124     transform_iterator(
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
129 #endif 
130     )
131       : super_t(t.base()), m_f(t.functor())
132    {}
133
134     UnaryFunction functor() const
135       { return m_f; }
136
137   private:
138     typename super_t::reference dereference() const
139     { return m_f(*this->base()); }
140
141     // Probably should be the initial base class so it can be
142     // optimized away via EBO if it is an empty class.
143     UnaryFunction m_f;
144   };
145
146   template <class UnaryFunction, class Iterator>
147   transform_iterator<UnaryFunction, Iterator>
148   make_transform_iterator(Iterator it, UnaryFunction fun)
149   {
150       return transform_iterator<UnaryFunction, Iterator>(it, fun);
151   }
152
153   // Version which allows explicit specification of the UnaryFunction
154   // type.
155   //
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
159   // crash.
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>
164   >::type
165   make_transform_iterator(Iterator it)
166   {
167       return transform_iterator<UnaryFunction, Iterator>(it, UnaryFunction());
168   }
169
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))
174   {
175     return transform_iterator<Return (*)(Argument), Iterator, Return>(it, fun);
176   }
177 #endif
178
179 } // namespace boost
180
181 #include <boost/iterator/detail/config_undef.hpp>
182
183 #endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP