for_each.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ALGORITHM_FOR_EACH_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_FOR_EACH_HPP_INCLUDED
  11. #include <boost/concept_check.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/concepts.hpp>
  15. #include <boost/utility/enable_if.hpp>
  16. #include <boost/ref.hpp>
  17. #include <algorithm>
  18. #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
  19. #include <xutility>
  20. #endif
  21. namespace boost
  22. {
  23. namespace range
  24. {
  25. #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
  26. namespace for_each_detail
  27. {
  28. template<typename Iterator, typename UnaryFunction>
  29. inline UnaryFunction
  30. for_each_impl(Iterator first, Iterator last, UnaryFunction fun,
  31. typename ::boost::enable_if<
  32. is_reference_wrapper<UnaryFunction>,
  33. void
  34. >::type* = 0)
  35. {
  36. typedef typename std::_Get_unchecked_type<Iterator>::type
  37. unchecked_iterator;
  38. unchecked_iterator unchecked_last = std::_Unchecked(last);
  39. for (unchecked_iterator unchecked_first = std::_Unchecked(first); first != last; ++first)
  40. fun.get()(*unchecked_first);
  41. return fun;
  42. }
  43. template<typename Iterator, typename UnaryFunction>
  44. inline UnaryFunction
  45. for_each_impl(Iterator first, Iterator last, UnaryFunction fn,
  46. typename disable_if<
  47. is_reference_wrapper<UnaryFunction>,
  48. void
  49. >::type* = 0)
  50. {
  51. return std::for_each<Iterator, UnaryFunction>(first, last, fn);
  52. }
  53. }
  54. #endif
  55. /// \brief template function for_each
  56. ///
  57. /// range-based version of the for_each std algorithm
  58. ///
  59. /// \pre SinglePassRange is a model of the SinglePassRangeConcept
  60. /// \pre UnaryFunction is a model of the UnaryFunctionConcept
  61. template< class SinglePassRange, class UnaryFunction >
  62. inline UnaryFunction for_each(SinglePassRange & rng, UnaryFunction fun)
  63. {
  64. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
  65. #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
  66. return for_each_detail::for_each_impl<
  67. typename range_iterator<SinglePassRange>::type,
  68. UnaryFunction
  69. >(boost::begin(rng), boost::end(rng), fun);
  70. #else
  71. return std::for_each<
  72. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange>::type,
  73. UnaryFunction
  74. >(boost::begin(rng),boost::end(rng),fun);
  75. #endif
  76. }
  77. /// \overload
  78. template< class SinglePassRange, class UnaryFunction >
  79. inline UnaryFunction for_each(const SinglePassRange& rng, UnaryFunction fun)
  80. {
  81. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
  82. #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
  83. return for_each_detail::for_each_impl<
  84. typename range_iterator<const SinglePassRange>::type,
  85. UnaryFunction
  86. >(boost::begin(rng), boost::end(rng), fun);
  87. #else
  88. return std::for_each<
  89. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange>::type,
  90. UnaryFunction
  91. >(boost::begin(rng), boost::end(rng), fun);
  92. #endif
  93. }
  94. } // namespace range
  95. using range::for_each;
  96. } // namespace boost
  97. #endif // include guard