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