]> git.lyx.org Git - lyx.git/blob - boost/boost/iterator/transform_iterator.hpp
Don't allow newline characters in document settings.
[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 // 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
9
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>
24
25
26 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
27 # include <boost/type_traits/is_base_and_derived.hpp>
28
29 #endif 
30 #include <boost/iterator/detail/config_def.hpp>
31
32
33 namespace boost
34 {
35   template <class UnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
36   class transform_iterator;
37
38   namespace detail 
39   {
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
43     {
44      private:
45         // By default, dereferencing the iterator yields the same as
46         // the function.
47         typedef typename ia_dflt_help<
48             Reference
49           , result_of<UnaryFunc(typename std::iterator_traits<Iterator>::reference)>
50         >::type reference;
51
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<
58             Value
59           , remove_reference<reference>
60         >::type cv_value_type;
61
62      public:
63         typedef iterator_adaptor<
64             transform_iterator<UnaryFunc, Iterator, Reference, Value>
65           , Iterator
66           , cv_value_type
67           , use_default    // Leave the traversal category alone
68           , reference
69         > type;
70     };
71   }
72
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
76   {
77     typedef typename
78     boost::detail::transform_iterator_base<UnaryFunc, Iterator, Reference, Value>::type
79     super_t;
80
81     friend class iterator_core_access;
82
83   public:
84     transform_iterator() { }
85
86     transform_iterator(Iterator const& x, UnaryFunc f)
87       : super_t(x), m_f(f) { }
88
89     explicit transform_iterator(Iterator const& x)
90       : super_t(x)
91     {
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);
98 #endif 
99     }
100
101     template <
102         class OtherUnaryFunction
103       , class OtherIterator
104       , class OtherReference
105       , class OtherValue>
106     transform_iterator(
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
111 #endif 
112     )
113       : super_t(t.base()), m_f(t.functor())
114    {}
115
116     UnaryFunc functor() const
117       { return m_f; }
118
119   private:
120     typename super_t::reference dereference() const
121     { return m_f(*this->base()); }
122
123     // Probably should be the initial base class so it can be
124     // optimized away via EBO if it is an empty class.
125     UnaryFunc m_f;
126   };
127
128   template <class UnaryFunc, class Iterator>
129   transform_iterator<UnaryFunc, Iterator>
130   make_transform_iterator(Iterator it, UnaryFunc fun)
131   {
132       return transform_iterator<UnaryFunc, Iterator>(it, fun);
133   }
134
135   // Version which allows explicit specification of the UnaryFunc
136   // type.
137   //
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
141   // crash.
142   template <class UnaryFunc, class Iterator>
143 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
144   typename mpl::if_<
145 #else 
146   typename iterators::enable_if<
147 #endif 
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)
151     , int[3]
152 #endif 
153   >::type
154   make_transform_iterator(Iterator it)
155   {
156       return transform_iterator<UnaryFunc, Iterator>(it, UnaryFunc());
157   }
158
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))
163   {
164     return transform_iterator<Return (*)(Argument), Iterator, Return>(it, fun);
165   }
166 #endif
167
168 } // namespace boost
169
170 #include <boost/iterator/detail/config_undef.hpp>
171
172 #endif // BOOST_TRANSFORM_ITERATOR_23022003THW_HPP