Chris@16: // Boost.Range library Chris@16: // Chris@16: // Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. Use, modification and Chris@16: // distribution is subject to the Boost Software License, Version Chris@16: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see http://www.boost.org/libs/range/ Chris@16: // Chris@16: Chris@16: #ifndef BOOST_RANGE_ADAPTOR_TRANSFORMED_HPP Chris@16: #define BOOST_RANGE_ADAPTOR_TRANSFORMED_HPP Chris@16: Chris@16: #include Chris@101: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace range_detail Chris@16: { Chris@101: // A type generator to produce the transform_iterator type conditionally Chris@101: // including a wrapped predicate as appropriate. Chris@101: template Chris@101: struct transform_iterator_gen Chris@101: { Chris@101: typedef transform_iterator< Chris@101: typename default_constructible_unary_fn_gen< Chris@101: P, Chris@101: typename transform_iterator::reference Chris@101: >::type, Chris@101: It Chris@101: > type; Chris@101: }; Chris@16: Chris@16: template< class F, class R > Chris@16: struct transformed_range : Chris@16: public boost::iterator_range< Chris@101: typename transform_iterator_gen< Chris@101: F, typename range_iterator::type>::type> Chris@16: { Chris@16: private: Chris@101: typedef typename transform_iterator_gen< Chris@101: F, typename range_iterator::type>::type transform_iter_t; Chris@101: Chris@101: typedef boost::iterator_range base; Chris@16: Chris@16: public: Chris@101: typedef typename default_constructible_unary_fn_gen< Chris@101: F, Chris@101: typename transform_iterator< Chris@101: F, Chris@101: typename range_iterator::type Chris@101: >::reference Chris@101: >::type transform_fn_type; Chris@101: Chris@16: typedef R source_range_type; Chris@16: Chris@101: transformed_range(transform_fn_type f, R& r) Chris@101: : base(transform_iter_t(boost::begin(r), f), Chris@101: transform_iter_t(boost::end(r), f)) Chris@101: { Chris@101: } Chris@16: }; Chris@16: Chris@16: template< class T > Chris@16: struct transform_holder : holder Chris@16: { Chris@16: transform_holder( T r ) : holder(r) Chris@101: { Chris@101: } Chris@16: }; Chris@16: Chris@101: template< class SinglePassRange, class UnaryFunction > Chris@101: inline transformed_range Chris@101: operator|( SinglePassRange& r, Chris@16: const transform_holder& f ) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return transformed_range( f.val, r ); Chris@16: } Chris@16: Chris@101: template< class SinglePassRange, class UnaryFunction > Chris@101: inline transformed_range Chris@101: operator|( const SinglePassRange& r, Chris@16: const transform_holder& f ) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return transformed_range( Chris@101: f.val, r); Chris@16: } Chris@16: Chris@16: } // 'range_detail' Chris@16: Chris@16: using range_detail::transformed_range; Chris@16: Chris@16: namespace adaptors Chris@16: { Chris@16: namespace Chris@16: { Chris@16: const range_detail::forwarder Chris@16: transformed = Chris@16: range_detail::forwarder(); Chris@16: } Chris@16: Chris@101: template Chris@101: inline transformed_range Chris@101: transform(SinglePassRange& rng, UnaryFunction fn) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return transformed_range(fn, rng); Chris@16: } Chris@16: Chris@101: template Chris@101: inline transformed_range Chris@101: transform(const SinglePassRange& rng, UnaryFunction fn) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return transformed_range( Chris@101: fn, rng); Chris@16: } Chris@16: } // 'adaptors' Chris@16: Chris@16: } Chris@16: Chris@16: #endif