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_IF_IMPL_HPP_INCLUDED
12 #define BOOST_RANGE_ADAPTOR_REPLACED_IF_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 Pred, class Value >
30 class replace_value_if
33 typedef const Value& result_type;
34 typedef const Value& first_argument_type;
37 // required to allow the iterator to be default constructible.
42 replace_value_if(const Pred& pred, const Value& to)
43 : m_impl(data(pred, to))
47 const Value& operator()(const Value& x) const
49 return m_impl->m_pred(x) ? m_impl->m_to : x;
55 data(const Pred& p, const Value& t)
63 boost::optional<data> m_impl;
66 template< class Pred, class R >
67 class replaced_if_range :
68 public boost::iterator_range<
69 boost::transform_iterator<
70 replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type >,
71 BOOST_DEDUCED_TYPENAME range_iterator<R>::type > >
74 typedef replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type > Fn;
76 typedef boost::iterator_range<
77 boost::transform_iterator<
78 replace_value_if< Pred, BOOST_DEDUCED_TYPENAME range_value<R>::type >,
79 BOOST_DEDUCED_TYPENAME range_iterator<R>::type > > base_t;
82 typedef BOOST_DEDUCED_TYPENAME range_value<R>::type value_type;
84 replaced_if_range( R& r, const Pred& pred, value_type to )
85 : base_t( make_transform_iterator( boost::begin(r), Fn(pred, to) ),
86 make_transform_iterator( boost::end(r), Fn(pred, to) ) )
90 template< class Pred, class T >
91 class replace_if_holder
94 replace_if_holder( const Pred& pred, const T& to )
95 : m_pred(pred), m_to(to)
98 const Pred& pred() const { return m_pred; }
99 const T& to() const { return m_to; }
106 template< class Pred, class SinglePassRange >
107 inline replaced_if_range<Pred, SinglePassRange>
110 const replace_if_holder<
112 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type>& f)
114 BOOST_RANGE_CONCEPT_ASSERT((
115 SinglePassRangeConcept<SinglePassRange>));
117 return replaced_if_range<Pred, SinglePassRange>(
118 r, f.pred(), f.to());
121 template< class Pred, class SinglePassRange >
122 inline replaced_if_range<Pred, const SinglePassRange>
124 const SinglePassRange& r,
125 const replace_if_holder<
127 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type>& f)
129 BOOST_RANGE_CONCEPT_ASSERT((
130 SinglePassRangeConcept<const SinglePassRange>));
132 return replaced_if_range<Pred, const SinglePassRange>(
133 r, f.pred(), f.to());
137 using range_detail::replaced_if_range;
143 const range_detail::forwarder2TU<range_detail::replace_if_holder>
145 range_detail::forwarder2TU<range_detail::replace_if_holder>();
148 template<class Pred, class SinglePassRange>
149 inline replaced_if_range<Pred, SinglePassRange>
150 replace_if(SinglePassRange& rng, Pred pred,
151 BOOST_DEDUCED_TYPENAME range_value<SinglePassRange>::type to)
153 BOOST_RANGE_CONCEPT_ASSERT((
154 SinglePassRangeConcept<SinglePassRange>));
156 return range_detail::replaced_if_range<Pred, SinglePassRange>(
160 template<class Pred, class SinglePassRange>
161 inline replaced_if_range<Pred, const SinglePassRange>
163 const SinglePassRange& rng,
165 BOOST_DEDUCED_TYPENAME range_value<const SinglePassRange>::type to)
167 BOOST_RANGE_CONCEPT_ASSERT((
168 SinglePassRangeConcept<const SinglePassRange>));
170 return range_detail::replaced_if_range<Pred, const SinglePassRange>(
177 #endif // include guard