for_each.hpp 3.0 KB

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