for_each.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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: for_each.hpp 55648 2009-08-18 05:16:53Z agurtovoy $
  11. // $Date: 2009-08-18 01:16:53 -0400 (Tue, 18 Aug 2009) $
  12. // $Revision: 55648 $
  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_/unwrap.hpp>
  22. #include <boost/type_traits/is_same.hpp>
  23. #include <boost/utility/value_init.hpp>
  24. namespace boost { namespace mpl {
  25. namespace aux {
  26. template< bool done = true >
  27. struct for_each_impl
  28. {
  29. template<
  30. typename Iterator
  31. , typename LastIterator
  32. , typename TransformFunc
  33. , typename F
  34. >
  35. static void execute(
  36. Iterator*
  37. , LastIterator*
  38. , TransformFunc*
  39. , F
  40. )
  41. {
  42. }
  43. };
  44. template<>
  45. struct for_each_impl<false>
  46. {
  47. template<
  48. typename Iterator
  49. , typename LastIterator
  50. , typename TransformFunc
  51. , typename F
  52. >
  53. static void execute(
  54. Iterator*
  55. , LastIterator*
  56. , TransformFunc*
  57. , F f
  58. )
  59. {
  60. typedef typename deref<Iterator>::type item;
  61. typedef typename apply1<TransformFunc,item>::type arg;
  62. // dwa 2002/9/10 -- make sure not to invoke undefined behavior
  63. // when we pass arg.
  64. value_initialized<arg> x;
  65. aux::unwrap(f, 0)(boost::get(x));
  66. typedef typename mpl::next<Iterator>::type iter;
  67. for_each_impl<boost::is_same<iter,LastIterator>::value>
  68. ::execute( static_cast<iter*>(0), static_cast<LastIterator*>(0), static_cast<TransformFunc*>(0), f);
  69. }
  70. };
  71. } // namespace aux
  72. // agurt, 17/mar/02: pointer default parameters are necessary to workaround
  73. // MSVC 6.5 function template signature's mangling bug
  74. template<
  75. typename Sequence
  76. , typename TransformOp
  77. , typename F
  78. >
  79. inline
  80. void for_each(F f, Sequence* = 0, TransformOp* = 0)
  81. {
  82. BOOST_MPL_ASSERT(( is_sequence<Sequence> ));
  83. typedef typename begin<Sequence>::type first;
  84. typedef typename end<Sequence>::type last;
  85. aux::for_each_impl< boost::is_same<first,last>::value >
  86. ::execute(static_cast<first*>(0), static_cast<last*>(0), static_cast<TransformOp*>(0), f);
  87. }
  88. template<
  89. typename Sequence
  90. , typename F
  91. >
  92. inline
  93. void for_each(F f, Sequence* = 0)
  94. {
  95. for_each<Sequence, identity<> >(f);
  96. }
  97. }}
  98. #endif // BOOST_MPL_FOR_EACH_HPP_INCLUDED