source: NonGTP/Boost/boost/mpl/for_each.hpp @ 857

Revision 857, 2.7 KB checked in by igarcia, 19 years ago (diff)
Line 
1
2#ifndef BOOST_MPL_FOR_EACH_HPP_INCLUDED
3#define BOOST_MPL_FOR_EACH_HPP_INCLUDED
4
5// Copyright Aleksey Gurtovoy 2000-2004
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// $Source: /cvsroot/boost/boost/boost/mpl/for_each.hpp,v $
14// $Date: 2004/09/04 01:33:46 $
15// $Revision: 1.9 $
16
17#include <boost/mpl/begin_end.hpp>
18#include <boost/mpl/apply.hpp>
19#include <boost/mpl/bool.hpp>
20#include <boost/mpl/next_prior.hpp>
21#include <boost/mpl/deref.hpp>
22#include <boost/mpl/identity.hpp>
23#include <boost/mpl/aux_/unwrap.hpp>
24
25#include <boost/type_traits/is_same.hpp>
26#include <boost/utility/value_init.hpp>
27
28namespace boost { namespace mpl {
29
30namespace aux {
31
32template< bool done = true >
33struct for_each_impl
34{
35    template<
36          typename Iterator
37        , typename LastIterator
38        , typename TransformFunc
39        , typename F
40        >
41    static void execute(
42          Iterator*
43        , LastIterator*
44        , TransformFunc*
45        , F
46        )
47    {
48    }
49};
50
51template<>
52struct for_each_impl<false>
53{
54    template<
55          typename Iterator
56        , typename LastIterator
57        , typename TransformFunc
58        , typename F
59        >
60    static void execute(
61          Iterator*
62        , LastIterator*
63        , TransformFunc*
64        , F f
65        )
66    {
67        typedef typename deref<Iterator>::type item;
68        typedef typename apply1<TransformFunc,item>::type arg;
69   
70        // dwa 2002/9/10 -- make sure not to invoke undefined behavior
71        // when we pass arg.
72        value_initialized<arg> x;
73        aux::unwrap(f, 0)(boost::get(x));
74       
75        typedef typename mpl::next<Iterator>::type iter;
76        for_each_impl<boost::is_same<iter,LastIterator>::value>
77            ::execute((iter*)0, (LastIterator*)0, (TransformFunc*)0, f);
78    }
79};
80
81} // namespace aux
82
83// agurt, 17/mar/02: pointer default parameters are necessary to workaround
84// MSVC 6.5 function template signature's mangling bug
85template<
86      typename Sequence
87    , typename TransformOp
88    , typename F
89    >
90inline
91void for_each(F f, Sequence* = 0, TransformOp* = 0)
92{
93    typedef typename begin<Sequence>::type first;
94    typedef typename end<Sequence>::type last;
95
96    aux::for_each_impl< boost::is_same<first,last>::value >
97        ::execute((first*)0, (last*)0, (TransformOp*)0, f);
98}
99
100template<
101      typename Sequence
102    , typename F
103    >
104inline
105void for_each(F f, Sequence* = 0)
106{
107    for_each<Sequence, identity<> >(f);
108}
109
110}}
111
112#endif // BOOST_MPL_FOR_EACH_HPP_INCLUDED
Note: See TracBrowser for help on using the repository browser.