switch.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_SWITCH_HPP
  8. #define BOOST_PHOENIX_STATEMENT_SWITCH_HPP
  9. #include <boost/phoenix/core/limits.hpp>
  10. #include <boost/fusion/iterator/advance.hpp>
  11. #include <boost/phoenix/core/call.hpp>
  12. #include <boost/phoenix/core/expression.hpp>
  13. #include <boost/phoenix/core/meta_grammar.hpp>
  14. #include <boost/phoenix/core/is_nullary.hpp>
  15. #include <boost/phoenix/support/iterate.hpp>
  16. #include <boost/proto/make_expr.hpp>
  17. #include <boost/proto/fusion.hpp>
  18. #ifdef _MSC_VER
  19. #pragma warning(push)
  20. #pragma warning(disable: 4065) // switch statement contains 'default' but no 'case' labels
  21. #endif
  22. BOOST_PHOENIX_DEFINE_EXPRESSION(
  23. (boost)(phoenix)(switch_case)
  24. , (proto::terminal<proto::_>)
  25. (meta_grammar)
  26. )
  27. BOOST_PHOENIX_DEFINE_EXPRESSION(
  28. (boost)(phoenix)(switch_default_case)
  29. , (meta_grammar)
  30. )
  31. namespace boost { namespace phoenix
  32. {
  33. namespace detail
  34. {
  35. struct switch_case_grammar;
  36. struct switch_case_with_default_grammar;
  37. struct switch_grammar
  38. : proto::or_<
  39. proto::when<
  40. detail::switch_case_grammar
  41. , mpl::false_()
  42. >
  43. , proto::when<
  44. detail::switch_case_with_default_grammar
  45. , mpl::true_()
  46. >
  47. >
  48. {};
  49. }
  50. namespace detail
  51. {
  52. struct switch_case_is_nullary
  53. : proto::or_<
  54. proto::when<
  55. proto::comma<
  56. switch_case_is_nullary
  57. , proto::or_<phoenix::rule::switch_default_case, phoenix::rule::switch_case>
  58. >
  59. , mpl::and_<
  60. switch_case_is_nullary(
  61. proto::_child_c<0>
  62. , proto::_state
  63. )
  64. , switch_case_is_nullary(
  65. proto::_child_c<1>
  66. , proto::_state
  67. )
  68. >()
  69. >
  70. , proto::when<
  71. proto::or_<phoenix::rule::switch_default_case, phoenix::rule::switch_case>
  72. , evaluator(proto::_child_c<0>, proto::_state)
  73. >
  74. >
  75. {};
  76. struct switch_case_grammar
  77. : proto::or_<
  78. proto::comma<switch_case_grammar, phoenix::rule::switch_case>
  79. , proto::when<phoenix::rule::switch_case, proto::_>
  80. >
  81. {};
  82. struct switch_case_with_default_grammar
  83. : proto::or_<
  84. proto::comma<switch_case_grammar, phoenix::rule::switch_default_case>
  85. , proto::when<phoenix::rule::switch_default_case, proto::_>
  86. >
  87. {};
  88. struct switch_size
  89. : proto::or_<
  90. proto::when<
  91. proto::comma<switch_size, proto::_>
  92. , mpl::next<switch_size(proto::_left)>()
  93. >
  94. , proto::when<proto::_, mpl::int_<1>()>
  95. >
  96. {};
  97. }
  98. }}
  99. BOOST_PHOENIX_DEFINE_EXPRESSION(
  100. (boost)(phoenix)(switch_)
  101. , (meta_grammar) // Cond
  102. (detail::switch_grammar) // Cases
  103. )
  104. namespace boost { namespace phoenix {
  105. template <typename Dummy>
  106. struct is_nullary::when<rule::switch_, Dummy>
  107. : proto::and_<
  108. evaluator(proto::_child_c<0>, _context)
  109. , detail::switch_case_is_nullary(proto::_child_c<1>, _context)
  110. >
  111. {};
  112. struct switch_eval
  113. {
  114. typedef void result_type;
  115. template <typename Context>
  116. result_type
  117. operator()(Context const &) const
  118. {
  119. }
  120. template <typename Cond, typename Cases, typename Context>
  121. result_type
  122. operator()(Cond const & cond, Cases const & cases, Context const & ctx) const
  123. {
  124. this->evaluate(
  125. ctx
  126. , cond
  127. , cases
  128. , typename detail::switch_size::impl<Cases, int, proto::empty_env>::result_type()
  129. , typename detail::switch_grammar::impl<Cases, int, proto::empty_env>::result_type()
  130. );
  131. }
  132. private:
  133. template <typename Context, typename Cond, typename Cases>
  134. result_type
  135. evaluate(
  136. Context const & ctx
  137. , Cond const & cond
  138. , Cases const & cases
  139. , mpl::int_<1>
  140. , mpl::false_
  141. ) const
  142. {
  143. typedef
  144. typename proto::result_of::value<
  145. typename proto::result_of::child_c<
  146. Cases
  147. , 0
  148. >::type
  149. >::type
  150. case_label;
  151. switch(boost::phoenix::eval(cond, ctx))
  152. {
  153. case case_label::value:
  154. boost::phoenix::eval(proto::child_c<1>(cases), ctx);
  155. }
  156. }
  157. template <typename Context, typename Cond, typename Cases>
  158. result_type
  159. evaluate(
  160. Context const & ctx
  161. , Cond const & cond
  162. , Cases const & cases
  163. , mpl::int_<1>
  164. , mpl::true_
  165. ) const
  166. {
  167. switch(boost::phoenix::eval(cond, ctx))
  168. {
  169. default:
  170. boost::phoenix::eval(proto::child_c<0>(cases), ctx);
  171. }
  172. }
  173. // Bring in the evaluation functions
  174. #include <boost/phoenix/statement/detail/switch.hpp>
  175. };
  176. template <typename Dummy>
  177. struct default_actions::when<rule::switch_, Dummy>
  178. : call<switch_eval>
  179. {};
  180. template <int N, typename A>
  181. inline
  182. typename proto::result_of::make_expr<
  183. tag::switch_case
  184. , proto::basic_default_domain
  185. , mpl::int_<N>
  186. , A
  187. >::type const
  188. case_(A const & a)
  189. {
  190. return
  191. proto::make_expr<
  192. tag::switch_case
  193. , proto::basic_default_domain
  194. >(
  195. mpl::int_<N>()
  196. , a
  197. );
  198. }
  199. template <typename A>
  200. inline
  201. typename proto::result_of::make_expr<
  202. tag::switch_default_case
  203. , proto::basic_default_domain
  204. , A
  205. >::type const
  206. default_(A const& a)
  207. {
  208. return
  209. proto::make_expr<
  210. tag::switch_default_case
  211. , proto::basic_default_domain
  212. >(a);
  213. }
  214. template <typename Cond>
  215. struct switch_gen
  216. {
  217. switch_gen(Cond const& cond_) : cond(cond_) {}
  218. template <typename Cases>
  219. typename expression::switch_<
  220. Cond
  221. , Cases
  222. >::type
  223. operator[](Cases const& cases) const
  224. {
  225. return
  226. this->generate(
  227. cases
  228. , proto::matches<Cases, detail::switch_grammar>()
  229. );
  230. }
  231. private:
  232. Cond const& cond;
  233. template <typename Cases>
  234. typename expression::switch_<
  235. Cond
  236. , Cases
  237. >::type
  238. generate(Cases const & cases, mpl::true_) const
  239. {
  240. return expression::switch_<Cond, Cases>::make(cond, cases);
  241. }
  242. template <typename Cases>
  243. typename expression::switch_<
  244. Cond
  245. , Cases
  246. >::type
  247. generate(Cases const &, mpl::false_) const
  248. {
  249. BOOST_MPL_ASSERT_MSG(
  250. false
  251. , INVALID_SWITCH_CASE_STATEMENT
  252. , (Cases)
  253. );
  254. }
  255. };
  256. template <typename Cond>
  257. inline
  258. switch_gen<Cond> const
  259. switch_(Cond const& cond)
  260. {
  261. return switch_gen<Cond>(cond);
  262. }
  263. }}
  264. #ifdef _MSC_VER
  265. #pragma warning(pop)
  266. #endif
  267. #endif