3 // Copyright Neil Groves 2007. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org/libs/range/
11 #ifndef BOOST_RANGE_ADAPTOR_REPLACED_IMPL_HPP_INCLUDED
12 #define BOOST_RANGE_ADAPTOR_REPLACED_IMPL_HPP_INCLUDED
14 #include <boost/config.hpp>
15 #include <boost/range/adaptor/argument_fwd.hpp>
16 #include <boost/range/iterator_range.hpp>
17 #include <boost/range/begin.hpp>
18 #include <boost/range/end.hpp>
19 #include <boost/range/value_type.hpp>
20 #include <boost/range/concepts.hpp>
21 #include <boost/iterator/iterator_adaptor.hpp>
22 #include <boost/iterator/transform_iterator.hpp>
23 #include <boost/optional/optional.hpp>
27 namespace range_detail
29 template< class Value >
33 typedef const Value& result_type;
34 typedef const Value& first_argument_type;
37 // The default constructor is required to allow the transform
38 // iterator to properly model the iterator concept.
43 replace_value(const Value& from, const Value& to)
44 : m_impl(data(from, to))
48 const Value& operator()(const Value& x) const
50 return (x == m_impl->m_from) ? m_impl->m_to : x;
56 data(const Value& from, const Value& to)
65 boost::optional<data> m_impl;
69 class replaced_range :
70 public boost::iterator_range<
71 boost::transform_iterator<
72 replace_value< BOOST_DEDUCED_TYPENAME range_value<R>::type >,
73 BOOST_DEDUCED_TYPENAME range_iterator<R>::type > >
76 typedef replace_value< BOOST_DEDUCED_TYPENAME range_value<R>::type > Fn;
78 typedef boost::iterator_range<
79 boost::transform_iterator<
80 replace_value< BOOST_DEDUCED_TYPENAME range_value<R>::type >,
81 BOOST_DEDUCED_TYPENAME range_iterator<R>::type > > base_t;
84 typedef BOOST_DEDUCED_TYPENAME range_value<R>::type value_type;
86 replaced_range( R& r, value_type from, value_type to )
87 : base_t( make_transform_iterator( boost::begin(r), Fn(from, to) ),
88 make_transform_iterator( boost::end(r), Fn(from, to) ) )
93 class replace_holder : public holder2<T>
96 replace_holder( const T& from, const T& to )
97 : holder2<T>(from, to)
101 void operator=(const replace_holder&);
104 template< class SinglePassRange >
105 inline replaced_range<SinglePassRange>
108 const replace_holder<
109 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type>& f )
111 BOOST_RANGE_CONCEPT_ASSERT((
112 SinglePassRangeConcept<SinglePassRange>));
114 return replaced_range<SinglePassRange>(r, f.val1, f.val2);
117 template< class SinglePassRange >
118 inline replaced_range<const SinglePassRange>
120 const SinglePassRange& r,
121 const replace_holder<
122 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type>& f)
124 BOOST_RANGE_CONCEPT_ASSERT((
125 SinglePassRangeConcept<const SinglePassRange>));
127 return replaced_range<const SinglePassRange>(r, f.val1, f.val2);
131 using range_detail::replaced_range;
137 const range_detail::forwarder2<range_detail::replace_holder>
139 range_detail::forwarder2<range_detail::replace_holder>();
142 template<class SinglePassRange>
143 inline replaced_range<SinglePassRange>
144 replace(SinglePassRange& rng,
145 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type from,
146 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type to)
148 BOOST_RANGE_CONCEPT_ASSERT((
149 SinglePassRangeConcept<SinglePassRange>));
151 return replaced_range<SinglePassRange>(rng, from, to);
154 template<class SinglePassRange>
155 inline replaced_range<const SinglePassRange>
156 replace(const SinglePassRange& rng,
157 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type from,
158 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type to)
160 BOOST_RANGE_CONCEPT_ASSERT((
161 SinglePassRangeConcept<const SinglePassRange>));
163 return replaced_range<const SinglePassRange>(rng, from ,to);
169 #endif // include guard