1 
2 #ifndef BOOST_MPL_FOR_EACH_HPP_INCLUDED
3 #define BOOST_MPL_FOR_EACH_HPP_INCLUDED
4 
5 // Copyright Aleksey Gurtovoy 2000-2008
6 //
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // See http://www.boost.org/libs/mpl for documentation.
12 
13 // $Id: for_each.hpp 55648 2009-08-18 05:16:53Z agurtovoy $
14 // $Date: 2009-08-17 22:16:53 -0700 (Mon, 17 Aug 2009) $
15 // $Revision: 55648 $
16 
17 #include <boost/mpl/is_sequence.hpp>
18 #include <boost/mpl/begin_end.hpp>
19 #include <boost/mpl/apply.hpp>
20 #include <boost/mpl/bool.hpp>
21 #include <boost/mpl/next_prior.hpp>
22 #include <boost/mpl/deref.hpp>
23 #include <boost/mpl/identity.hpp>
24 #include <boost/mpl/assert.hpp>
25 #include <boost/mpl/aux_/unwrap.hpp>
26 
27 #include <boost/type_traits/is_same.hpp>
28 #include <boost/utility/value_init.hpp>
29 
30 namespace boost { namespace mpl {
31 
32 namespace aux {
33 
34 template< bool done = true >
35 struct for_each_impl
36 {
37     template<
38           typename Iterator
39         , typename LastIterator
40         , typename TransformFunc
41         , typename F
42         >
executeboost::mpl::aux::for_each_impl43     static void execute(
44           Iterator*
45         , LastIterator*
46         , TransformFunc*
47         , F
48         )
49     {
50     }
51 };
52 
53 template<>
54 struct for_each_impl<false>
55 {
56     template<
57           typename Iterator
58         , typename LastIterator
59         , typename TransformFunc
60         , typename F
61         >
executeboost::mpl::aux::for_each_impl62     static void execute(
63           Iterator*
64         , LastIterator*
65         , TransformFunc*
66         , F f
67         )
68     {
69         typedef typename deref<Iterator>::type item;
70         typedef typename apply1<TransformFunc,item>::type arg;
71 
72         // dwa 2002/9/10 -- make sure not to invoke undefined behavior
73         // when we pass arg.
74         value_initialized<arg> x;
75         aux::unwrap(f, 0)(boost::get(x));
76 
77         typedef typename mpl::next<Iterator>::type iter;
78         for_each_impl<boost::is_same<iter,LastIterator>::value>
79             ::execute( static_cast<iter*>(0), static_cast<LastIterator*>(0), static_cast<TransformFunc*>(0), f);
80     }
81 };
82 
83 } // namespace aux
84 
85 // agurt, 17/mar/02: pointer default parameters are necessary to workaround
86 // MSVC 6.5 function template signature's mangling bug
87 template<
88       typename Sequence
89     , typename TransformOp
90     , typename F
91     >
92 inline
for_each(F f,Sequence * =0,TransformOp * =0)93 void for_each(F f, Sequence* = 0, TransformOp* = 0)
94 {
95     BOOST_MPL_ASSERT(( is_sequence<Sequence> ));
96 
97     typedef typename begin<Sequence>::type first;
98     typedef typename end<Sequence>::type last;
99 
100     aux::for_each_impl< boost::is_same<first,last>::value >
101         ::execute(static_cast<first*>(0), static_cast<last*>(0), static_cast<TransformOp*>(0), f);
102 }
103 
104 template<
105       typename Sequence
106     , typename F
107     >
108 inline
for_each(F f,Sequence * =0)109 void for_each(F f, Sequence* = 0)
110 {
111     for_each<Sequence, identity<> >(f);
112 }
113 
114 }}
115 
116 #endif // BOOST_MPL_FOR_EACH_HPP_INCLUDED
117