transformed.hpp 4.4 KB

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