Files
boost_mpl/include/boost/mpl/for_each.hpp
T

113 lines
2.5 KiB
C++
Raw Normal View History

2002-09-15 22:13:24 +00:00
#ifndef BOOST_MPL_FOR_EACH_HPP_INCLUDED
#define BOOST_MPL_FOR_EACH_HPP_INCLUDED
2004-09-02 15:41:37 +00:00
// Copyright Aleksey Gurtovoy 2000-2004
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/mpl for documentation.
2002-12-30 08:12:55 +00:00
2004-09-02 15:41:37 +00:00
// $Source$
// $Date$
// $Revision$
2002-09-15 22:13:24 +00:00
2004-09-02 15:41:37 +00:00
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/aux_/unwrap.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/utility/value_init.hpp>
namespace boost { namespace mpl {
2002-09-15 22:13:24 +00:00
namespace aux {
2004-09-02 15:41:37 +00:00
template< bool done = true >
2002-09-17 13:50:13 +00:00
struct for_each_impl
{
template<
2002-12-30 08:12:55 +00:00
typename Iterator
, typename LastIterator
, typename TransformFunc
, typename F
>
2002-09-17 13:50:13 +00:00
static void execute(
2002-12-30 08:12:55 +00:00
Iterator*
2002-09-17 13:50:13 +00:00
, LastIterator*
, TransformFunc*
, F
)
{
}
};
2004-09-02 15:41:37 +00:00
template<>
2002-09-17 13:50:13 +00:00
struct for_each_impl<false>
2002-09-15 22:13:24 +00:00
{
2002-09-17 13:50:13 +00:00
template<
2002-12-30 08:12:55 +00:00
typename Iterator
, typename LastIterator
, typename TransformFunc
, typename F
>
2002-09-17 13:50:13 +00:00
static void execute(
2002-12-30 08:12:55 +00:00
Iterator*
2002-09-17 13:50:13 +00:00
, LastIterator*
, TransformFunc*
, F f
)
{
2004-09-02 15:41:37 +00:00
typedef typename deref<Iterator>::type item;
2002-09-17 13:50:13 +00:00
typedef typename apply1<TransformFunc,item>::type arg;
// dwa 2002/9/10 -- make sure not to invoke undefined behavior
// when we pass arg.
value_initialized<arg> x;
2003-01-16 22:41:06 +00:00
aux::unwrap(f, 0)(boost::get(x));
2002-09-17 13:50:13 +00:00
typedef typename mpl::next<Iterator>::type iter;
2004-09-02 15:41:37 +00:00
for_each_impl<boost::is_same<iter,LastIterator>::value>
::execute((iter*)0, (LastIterator*)0, (TransformFunc*)0, f);
2002-09-17 13:50:13 +00:00
}
};
2002-09-15 22:13:24 +00:00
} // namespace aux
2002-12-30 08:12:55 +00:00
// agurt, 17/mar/02: pointer default parameters are necessary to workaround
// MSVC 6.5 function template signature's mangling bug
2002-09-15 22:13:24 +00:00
template<
typename Sequence
, typename TransformOp
, typename F
>
inline
void for_each(F f, Sequence* = 0, TransformOp* = 0)
{
typedef typename begin<Sequence>::type first;
typedef typename end<Sequence>::type last;
2002-09-17 13:50:13 +00:00
2004-09-02 15:41:37 +00:00
aux::for_each_impl< boost::is_same<first,last>::value >
::execute((first*)0, (last*)0, (TransformOp*)0, f);
2002-09-15 22:13:24 +00:00
}
2003-01-16 22:41:06 +00:00
template<
typename Sequence
, typename F
>
inline
void for_each(F f, Sequence* = 0)
{
for_each<Sequence, identity<> >(f);
}
2004-09-02 15:41:37 +00:00
}}
2002-09-15 22:13:24 +00:00
#endif // BOOST_MPL_FOR_EACH_HPP_INCLUDED