if.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #ifndef PHOENIX_STATEMENT_IF_HPP
  7. #define PHOENIX_STATEMENT_IF_HPP
  8. #include <boost/spirit/home/phoenix/core/composite.hpp>
  9. #include <boost/spirit/home/phoenix/core/as_actor.hpp>
  10. #include <boost/spirit/home/phoenix/core/compose.hpp>
  11. #if defined(BOOST_MSVC)
  12. # pragma warning(push)
  13. # pragma warning(disable:4355)
  14. #endif
  15. namespace boost { namespace phoenix
  16. {
  17. struct if_else_eval
  18. {
  19. template <typename Env, typename Cond, typename Then, typename Else>
  20. struct result
  21. {
  22. typedef void type;
  23. };
  24. template <
  25. typename RT, typename Env
  26. , typename Cond, typename Then, typename Else>
  27. static void
  28. eval(Env const& env, Cond& cond, Then& then, Else& else_)
  29. {
  30. if (cond.eval(env))
  31. then.eval(env);
  32. else
  33. else_.eval(env);
  34. }
  35. };
  36. struct if_eval
  37. {
  38. template <typename Env, typename Cond, typename Then>
  39. struct result
  40. {
  41. typedef void type;
  42. };
  43. template <typename RT, typename Env, typename Cond, typename Then>
  44. static void
  45. eval(Env const& env, Cond& cond, Then& then)
  46. {
  47. if (cond.eval(env))
  48. then.eval(env);
  49. }
  50. };
  51. template <typename Cond, typename Then>
  52. struct if_composite;
  53. template <typename Cond, typename Then>
  54. struct else_gen
  55. {
  56. else_gen(if_composite<Cond, Then> const& source)
  57. : source(source) {}
  58. template <typename Else>
  59. actor<typename as_composite<if_else_eval, Cond, Then, Else>::type>
  60. operator[](Else const& else_) const
  61. {
  62. return compose<if_else_eval>(
  63. fusion::at_c<0>(source) // cond
  64. , fusion::at_c<1>(source) // then
  65. , else_ // else
  66. );
  67. }
  68. if_composite<Cond, Then> const& source;
  69. private:
  70. // silence MSVC warning C4512: assignment operator could not be generated
  71. else_gen& operator= (else_gen const&);
  72. };
  73. template <typename Cond, typename Then>
  74. struct if_composite : composite<if_eval, fusion::vector<Cond, Then> >
  75. {
  76. if_composite(Cond const& cond, Then const& then)
  77. : composite<if_eval, fusion::vector<Cond, Then> >(cond, then)
  78. , else_(*this) {}
  79. else_gen<Cond, Then> else_;
  80. private:
  81. // silence MSVC warning C4512: assignment operator could not be generated
  82. if_composite& operator= (if_composite const&);
  83. };
  84. template <typename Cond>
  85. struct if_gen
  86. {
  87. if_gen(Cond const& cond)
  88. : cond(cond) {}
  89. template <typename Then>
  90. actor<if_composite<Cond, typename as_actor<Then>::type> >
  91. operator[](Then const& then) const
  92. {
  93. return actor<if_composite<Cond, typename as_actor<Then>::type> >(
  94. cond, as_actor<Then>::convert(then));
  95. }
  96. Cond cond;
  97. };
  98. template <typename Cond>
  99. inline if_gen<typename as_actor<Cond>::type>
  100. if_(Cond const& cond)
  101. {
  102. return if_gen<typename as_actor<Cond>::type>(
  103. as_actor<Cond>::convert(cond));
  104. }
  105. }}
  106. #if defined(BOOST_MSVC)
  107. # pragma warning(pop)
  108. #endif
  109. #endif