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
12 #include <boost/concept_check.hpp>
13 #include <boost/range/begin.hpp>
14 #include <boost/range/end.hpp>
15 #include <boost/range/concepts.hpp>
16 #include <boost/utility/enable_if.hpp>
17 #include <boost/ref.hpp>
18 #include <algorithm>
19
20 #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
21 #include <xutility>
22 #endif
23
24 namespace boost
25 {
26 namespace range
27 {
28
29 #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
30 namespace for_each_detail
31 {
32 template<typename Iterator, typename UnaryFunction>
33 inline UnaryFunction
for_each_impl(Iterator first,Iterator last,UnaryFunction fun,typename::boost::enable_if<is_reference_wrapper<UnaryFunction>,void>::type * =0)34 for_each_impl(Iterator first, Iterator last, UnaryFunction fun,
35 typename ::boost::enable_if<
36 is_reference_wrapper<UnaryFunction>,
37 void
38 >::type* = 0)
39 {
40 typedef typename std::_Get_unchecked_type<Iterator>::type
41 unchecked_iterator;
42
43 unchecked_iterator unchecked_last = std::_Unchecked(last);
44 for (unchecked_iterator unchecked_first = std::_Unchecked(first); first != last; ++first)
45 fun.get()(*unchecked_first);
46
47 return fun;
48 }
49
50 template<typename Iterator, typename UnaryFunction>
51 inline UnaryFunction
for_each_impl(Iterator first,Iterator last,UnaryFunction fn,typename disable_if<is_reference_wrapper<UnaryFunction>,void>::type * =0)52 for_each_impl(Iterator first, Iterator last, UnaryFunction fn,
53 typename disable_if<
54 is_reference_wrapper<UnaryFunction>,
55 void
56 >::type* = 0)
57 {
58 return std::for_each<Iterator, UnaryFunction>(first, last, fn);
59 }
60 }
61 #endif
62
63 /// \brief template function for_each
64 ///
65 /// range-based version of the for_each std algorithm
66 ///
67 /// \pre SinglePassRange is a model of the SinglePassRangeConcept
68 /// \pre UnaryFunction is a model of the UnaryFunctionConcept
69 template< class SinglePassRange, class UnaryFunction >
for_each(SinglePassRange & rng,UnaryFunction fun)70 inline UnaryFunction for_each(SinglePassRange & rng, UnaryFunction fun)
71 {
72 BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
73
74 #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
75 return for_each_detail::for_each_impl<
76 typename range_iterator<SinglePassRange>::type,
77 UnaryFunction
78 >(boost::begin(rng), boost::end(rng), fun);
79 #else
80 return std::for_each<
81 BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange>::type,
82 UnaryFunction
83 >(boost::begin(rng),boost::end(rng),fun);
84 #endif
85 }
86
87 /// \overload
88 template< class SinglePassRange, class UnaryFunction >
for_each(const SinglePassRange & rng,UnaryFunction fun)89 inline UnaryFunction for_each(const SinglePassRange& rng, UnaryFunction fun)
90 {
91 BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
92
93 #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
94 return for_each_detail::for_each_impl<
95 typename range_iterator<const SinglePassRange>::type,
96 UnaryFunction
97 >(boost::begin(rng), boost::end(rng), fun);
98 #else
99 return std::for_each<
100 BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange>::type,
101 UnaryFunction
102 >(boost::begin(rng), boost::end(rng), fun);
103 #endif
104 }
105
106 } // namespace range
107 using range::for_each;
108 } // namespace boost
109
110 #endif // include guard
111