for.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*==============================================================================
  2. Copyright (c) 2001-2010 Joel de Guzman
  3. Copyright (c) 2010 Thomas Heller
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_PHOENIX_STATEMENT_FOR_HPP
  8. #define BOOST_PHOENIX_STATEMENT_FOR_HPP
  9. #include <boost/phoenix/core/limits.hpp>
  10. #include <boost/phoenix/core/call.hpp>
  11. #include <boost/phoenix/core/expression.hpp>
  12. #include <boost/phoenix/core/meta_grammar.hpp>
  13. BOOST_PHOENIX_DEFINE_EXPRESSION(
  14. (boost)(phoenix)(for_)
  15. , (meta_grammar) // Cond
  16. (meta_grammar) // Init
  17. (meta_grammar) // Step
  18. (meta_grammar) // Do
  19. )
  20. namespace boost { namespace phoenix
  21. {
  22. struct for_eval
  23. {
  24. typedef void result_type;
  25. template <
  26. typename Init
  27. , typename Cond
  28. , typename Step
  29. , typename Do
  30. , typename Context
  31. >
  32. result_type
  33. operator()(
  34. Init const& init
  35. , Cond const& cond
  36. , Step const& step
  37. , Do const& do_it
  38. , Context const & ctx
  39. ) const
  40. {
  41. for(boost::phoenix::eval(init, ctx); boost::phoenix::eval(cond, ctx); boost::phoenix::eval(step, ctx))
  42. boost::phoenix::eval(do_it, ctx);
  43. }
  44. };
  45. template <typename Dummy>
  46. struct default_actions::when<rule::for_, Dummy>
  47. : call<for_eval, Dummy>
  48. {};
  49. template <typename Init, typename Cond, typename Step>
  50. struct for_gen
  51. {
  52. for_gen(Init const& init_, Cond const& cond_, Step const& step_)
  53. : init(init_), cond(cond_), step(step_) {}
  54. template <typename Do>
  55. typename expression::for_<Init, Cond, Step, Do>::type const
  56. operator[](Do const& do_it) const
  57. {
  58. return
  59. expression::
  60. for_<Init, Cond, Step, Do>::
  61. make(init, cond, step, do_it);
  62. }
  63. Init init;
  64. Cond cond;
  65. Step step;
  66. };
  67. template <typename Init, typename Cond, typename Step>
  68. inline
  69. for_gen<Init, Cond, Step> const
  70. for_(Init const& init, Cond const& cond, Step const& step)
  71. {
  72. return for_gen<Init, Cond, Step>(init, cond, step);
  73. }
  74. }}
  75. #endif