Chris@16
|
1 // Boost.Range library
|
Chris@16
|
2 //
|
Chris@16
|
3 // Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. Use, modification and
|
Chris@16
|
4 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 // For more information, see http://www.boost.org/libs/range/
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_RANGE_ADAPTOR_TRANSFORMED_HPP
|
Chris@16
|
12 #define BOOST_RANGE_ADAPTOR_TRANSFORMED_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/range/adaptor/argument_fwd.hpp>
|
Chris@101
|
15 #include <boost/range/detail/default_constructible_unary_fn.hpp>
|
Chris@16
|
16 #include <boost/range/iterator_range.hpp>
|
Chris@101
|
17 #include <boost/range/concepts.hpp>
|
Chris@16
|
18 #include <boost/iterator/transform_iterator.hpp>
|
Chris@16
|
19 #include <boost/utility/result_of.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost
|
Chris@16
|
22 {
|
Chris@16
|
23 namespace range_detail
|
Chris@16
|
24 {
|
Chris@101
|
25 // A type generator to produce the transform_iterator type conditionally
|
Chris@101
|
26 // including a wrapped predicate as appropriate.
|
Chris@101
|
27 template<typename P, typename It>
|
Chris@101
|
28 struct transform_iterator_gen
|
Chris@101
|
29 {
|
Chris@101
|
30 typedef transform_iterator<
|
Chris@101
|
31 typename default_constructible_unary_fn_gen<
|
Chris@101
|
32 P,
|
Chris@101
|
33 typename transform_iterator<P, It>::reference
|
Chris@101
|
34 >::type,
|
Chris@101
|
35 It
|
Chris@101
|
36 > type;
|
Chris@101
|
37 };
|
Chris@16
|
38
|
Chris@16
|
39 template< class F, class R >
|
Chris@16
|
40 struct transformed_range :
|
Chris@16
|
41 public boost::iterator_range<
|
Chris@101
|
42 typename transform_iterator_gen<
|
Chris@101
|
43 F, typename range_iterator<R>::type>::type>
|
Chris@16
|
44 {
|
Chris@16
|
45 private:
|
Chris@101
|
46 typedef typename transform_iterator_gen<
|
Chris@101
|
47 F, typename range_iterator<R>::type>::type transform_iter_t;
|
Chris@101
|
48
|
Chris@101
|
49 typedef boost::iterator_range<transform_iter_t> base;
|
Chris@16
|
50
|
Chris@16
|
51 public:
|
Chris@101
|
52 typedef typename default_constructible_unary_fn_gen<
|
Chris@101
|
53 F,
|
Chris@101
|
54 typename transform_iterator<
|
Chris@101
|
55 F,
|
Chris@101
|
56 typename range_iterator<R>::type
|
Chris@101
|
57 >::reference
|
Chris@101
|
58 >::type transform_fn_type;
|
Chris@101
|
59
|
Chris@16
|
60 typedef R source_range_type;
|
Chris@16
|
61
|
Chris@101
|
62 transformed_range(transform_fn_type f, R& r)
|
Chris@101
|
63 : base(transform_iter_t(boost::begin(r), f),
|
Chris@101
|
64 transform_iter_t(boost::end(r), f))
|
Chris@101
|
65 {
|
Chris@101
|
66 }
|
Chris@16
|
67 };
|
Chris@16
|
68
|
Chris@16
|
69 template< class T >
|
Chris@16
|
70 struct transform_holder : holder<T>
|
Chris@16
|
71 {
|
Chris@16
|
72 transform_holder( T r ) : holder<T>(r)
|
Chris@101
|
73 {
|
Chris@101
|
74 }
|
Chris@16
|
75 };
|
Chris@16
|
76
|
Chris@101
|
77 template< class SinglePassRange, class UnaryFunction >
|
Chris@101
|
78 inline transformed_range<UnaryFunction,SinglePassRange>
|
Chris@101
|
79 operator|( SinglePassRange& r,
|
Chris@16
|
80 const transform_holder<UnaryFunction>& f )
|
Chris@16
|
81 {
|
Chris@101
|
82 BOOST_RANGE_CONCEPT_ASSERT((
|
Chris@101
|
83 SinglePassRangeConcept<SinglePassRange>));
|
Chris@101
|
84
|
Chris@101
|
85 return transformed_range<UnaryFunction,SinglePassRange>( f.val, r );
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@101
|
88 template< class SinglePassRange, class UnaryFunction >
|
Chris@101
|
89 inline transformed_range<UnaryFunction, const SinglePassRange>
|
Chris@101
|
90 operator|( const SinglePassRange& r,
|
Chris@16
|
91 const transform_holder<UnaryFunction>& f )
|
Chris@16
|
92 {
|
Chris@101
|
93 BOOST_RANGE_CONCEPT_ASSERT((
|
Chris@101
|
94 SinglePassRangeConcept<const SinglePassRange>));
|
Chris@101
|
95
|
Chris@101
|
96 return transformed_range<UnaryFunction, const SinglePassRange>(
|
Chris@101
|
97 f.val, r);
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 } // 'range_detail'
|
Chris@16
|
101
|
Chris@16
|
102 using range_detail::transformed_range;
|
Chris@16
|
103
|
Chris@16
|
104 namespace adaptors
|
Chris@16
|
105 {
|
Chris@16
|
106 namespace
|
Chris@16
|
107 {
|
Chris@16
|
108 const range_detail::forwarder<range_detail::transform_holder>
|
Chris@16
|
109 transformed =
|
Chris@16
|
110 range_detail::forwarder<range_detail::transform_holder>();
|
Chris@16
|
111 }
|
Chris@16
|
112
|
Chris@101
|
113 template<class UnaryFunction, class SinglePassRange>
|
Chris@101
|
114 inline transformed_range<UnaryFunction, SinglePassRange>
|
Chris@101
|
115 transform(SinglePassRange& rng, UnaryFunction fn)
|
Chris@16
|
116 {
|
Chris@101
|
117 BOOST_RANGE_CONCEPT_ASSERT((
|
Chris@101
|
118 SinglePassRangeConcept<SinglePassRange>));
|
Chris@101
|
119
|
Chris@101
|
120 return transformed_range<UnaryFunction, SinglePassRange>(fn, rng);
|
Chris@16
|
121 }
|
Chris@16
|
122
|
Chris@101
|
123 template<class UnaryFunction, class SinglePassRange>
|
Chris@101
|
124 inline transformed_range<UnaryFunction, const SinglePassRange>
|
Chris@101
|
125 transform(const SinglePassRange& rng, UnaryFunction fn)
|
Chris@16
|
126 {
|
Chris@101
|
127 BOOST_RANGE_CONCEPT_ASSERT((
|
Chris@101
|
128 SinglePassRangeConcept<const SinglePassRange>));
|
Chris@101
|
129
|
Chris@101
|
130 return transformed_range<UnaryFunction, const SinglePassRange>(
|
Chris@101
|
131 fn, rng);
|
Chris@16
|
132 }
|
Chris@16
|
133 } // 'adaptors'
|
Chris@16
|
134
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 #endif
|