// Boost.Range library // // Copyright Neil Groves 2007. Use, modification and // distribution is subject to the Boost Software License, Version // 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // For more information, see http://www.boost.org/libs/range/ // #ifndef BOOST_RANGE_ADAPTOR_REPLACED_IMPL_HPP_INCLUDED #define BOOST_RANGE_ADAPTOR_REPLACED_IMPL_HPP_INCLUDED #include #include #include #include #include #include #include #include #include #include namespace boost { namespace range_detail { template< class Value > class replace_value { public: typedef const Value& result_type; typedef const Value& first_argument_type; // Rationale: // The default constructor is required to allow the transform // iterator to properly model the iterator concept. replace_value() { } replace_value(const Value& from, const Value& to) : m_impl(data(from, to)) { } const Value& operator()(const Value& x) const { return (x == m_impl->m_from) ? m_impl->m_to : x; } private: struct data { data(const Value& from, const Value& to) : m_from(from) , m_to(to) { } Value m_from; Value m_to; }; boost::optional m_impl; }; template< class R > class replaced_range : public boost::iterator_range< boost::transform_iterator< replace_value< BOOST_DEDUCED_TYPENAME range_value::type >, BOOST_DEDUCED_TYPENAME range_iterator::type > > { private: typedef replace_value< BOOST_DEDUCED_TYPENAME range_value::type > Fn; typedef boost::iterator_range< boost::transform_iterator< replace_value< BOOST_DEDUCED_TYPENAME range_value::type >, BOOST_DEDUCED_TYPENAME range_iterator::type > > base_t; public: typedef BOOST_DEDUCED_TYPENAME range_value::type value_type; replaced_range( R& r, value_type from, value_type to ) : base_t( make_transform_iterator( boost::begin(r), Fn(from, to) ), make_transform_iterator( boost::end(r), Fn(from, to) ) ) { } }; template< class T > class replace_holder : public holder2 { public: replace_holder( const T& from, const T& to ) : holder2(from, to) { } private: // not assignable void operator=(const replace_holder&); }; template< class SinglePassRange, class Value > inline replaced_range operator|(SinglePassRange& r, const replace_holder& f) { BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept)); return replaced_range(r, f.val1, f.val2); } template< class SinglePassRange, class Value > inline replaced_range operator|(const SinglePassRange& r, const replace_holder& f) { BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept)); return replaced_range(r, f.val1, f.val2); } } // 'range_detail' using range_detail::replaced_range; namespace adaptors { namespace { const range_detail::forwarder2 replaced = range_detail::forwarder2(); } template< class SinglePassRange, class Value > inline replaced_range replace(SinglePassRange& rng, Value from, Value to) { BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept)); return replaced_range(rng, from, to); } template< class SinglePassRange, class Value > inline replaced_range replace(const SinglePassRange& rng, Value from, Value to) { BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept)); return replaced_range(rng, from ,to); } } // 'adaptors' } // 'boost' #endif // include guard