try_catch.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*==============================================================================
  2. Copyright (c) 2005-2007 Dan Marsden
  3. Copyright (c) 2005-2010 Joel de Guzman
  4. Copyright (c) 2010 Thomas Heller
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #ifndef BOOST_PHOENIX_STATEMENT_TRY_CATCH_HPP
  9. #define BOOST_PHOENIX_STATEMENT_TRY_CATCH_HPP
  10. #include <boost/phoenix/core/limits.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/proto/functional/fusion/pop_front.hpp>
  16. #ifdef _MSC_VER
  17. #pragma warning(push)
  18. #pragma warning(disable: 4355) // 'this' : used in base member initializer list
  19. #endif
  20. namespace boost { namespace phoenix
  21. {
  22. template <typename Expr>
  23. struct try_catch_actor;
  24. template <typename Exception>
  25. struct catch_exception
  26. {
  27. typedef Exception type;
  28. };
  29. namespace tag
  30. {
  31. struct try_catch {};
  32. struct catch_ {};
  33. struct catch_all {};
  34. }
  35. namespace expression
  36. {
  37. template <
  38. typename Try
  39. , BOOST_PHOENIX_typename_A_void(BOOST_PHOENIX_CATCH_LIMIT)
  40. , typename Dummy = void
  41. >
  42. struct try_catch;
  43. // bring in the expression definitions
  44. #include <boost/phoenix/statement/detail/try_catch_expression.hpp>
  45. template <typename A0, typename A1>
  46. struct catch_
  47. : proto::binary_expr<tag::catch_, A0, A1>
  48. {};
  49. template <typename A0>
  50. struct catch_all
  51. : proto::unary_expr<tag::catch_all, A0>
  52. {};
  53. }
  54. namespace rule
  55. {
  56. struct catch_
  57. : expression::catch_<
  58. proto::terminal<catch_exception<proto::_> >
  59. , meta_grammar
  60. >
  61. {};
  62. struct catch_all
  63. : expression::catch_all<
  64. meta_grammar
  65. >
  66. {};
  67. struct try_catch
  68. : proto::or_<
  69. expression::try_catch<
  70. meta_grammar
  71. , proto::vararg<rule::catch_>
  72. >
  73. , expression::try_catch<
  74. meta_grammar
  75. , rule::catch_all
  76. >
  77. , expression::try_catch<
  78. meta_grammar
  79. , proto::vararg<rule::catch_>
  80. , rule::catch_all
  81. >
  82. >
  83. {};
  84. }
  85. template <typename Dummy>
  86. struct meta_grammar::case_<tag::try_catch, Dummy>
  87. : enable_rule<rule::try_catch, Dummy>
  88. {};
  89. struct try_catch_eval
  90. {
  91. typedef void result_type;
  92. template <typename Try, typename Context>
  93. void operator()(Try const &, Context const &) const
  94. {}
  95. // bring in the operator overloads
  96. #include <boost/phoenix/statement/detail/try_catch_eval.hpp>
  97. };
  98. template <typename Dummy>
  99. struct default_actions::when<rule::try_catch, Dummy>
  100. : call<try_catch_eval, Dummy>
  101. {};
  102. namespace detail
  103. {
  104. struct try_catch_is_nullary
  105. : proto::or_<
  106. proto::when<
  107. phoenix::rule::catch_all
  108. , proto::call<
  109. evaluator(
  110. proto::_child_c<0>
  111. , proto::_data
  112. , proto::make<proto::empty_env()>
  113. )
  114. >
  115. >
  116. , proto::when<
  117. phoenix::rule::catch_
  118. , proto::call<
  119. evaluator(
  120. proto::_child_c<1>
  121. , proto::_data
  122. , proto::make<proto::empty_env()>
  123. )
  124. >
  125. >
  126. , proto::when<
  127. phoenix::rule::try_catch
  128. , proto::make<
  129. mpl::and_<
  130. proto::call<
  131. evaluator(
  132. proto::_child_c<0>
  133. , proto::_data
  134. , proto::make<proto::empty_env()>
  135. )
  136. >
  137. , proto::fold<
  138. proto::call<
  139. proto::functional::pop_front(proto::_)
  140. >
  141. , proto::make<mpl::true_()>
  142. , proto::make<
  143. mpl::and_<
  144. proto::_state
  145. , proto::call<
  146. try_catch_is_nullary(
  147. proto::_
  148. , proto::make<proto::empty_env()>
  149. , proto::_data
  150. )
  151. >
  152. >()
  153. >
  154. >
  155. >()
  156. >
  157. >
  158. >
  159. {};
  160. template <
  161. typename TryCatch
  162. , typename Exception
  163. , typename Expr
  164. , long Arity = proto::arity_of<TryCatch>::value
  165. >
  166. struct catch_push_back;
  167. template <typename TryCatch, typename Exception, typename Expr>
  168. struct catch_push_back<TryCatch, Exception, Expr, 1>
  169. {
  170. typedef
  171. typename proto::result_of::make_expr<
  172. phoenix::tag::catch_
  173. , proto::basic_default_domain
  174. , catch_exception<Exception>
  175. , Expr
  176. >::type
  177. catch_expr;
  178. typedef
  179. phoenix::expression::try_catch<
  180. TryCatch
  181. , catch_expr
  182. >
  183. gen_type;
  184. typedef typename gen_type::type type;
  185. static type make(TryCatch const & try_catch, Expr const & catch_)
  186. {
  187. return
  188. gen_type::make(
  189. try_catch
  190. , proto::make_expr<
  191. phoenix::tag::catch_
  192. , proto::basic_default_domain
  193. >(catch_exception<Exception>(), catch_)
  194. );
  195. }
  196. };
  197. template <
  198. typename TryCatch
  199. , typename Expr
  200. , long Arity = proto::arity_of<TryCatch>::value
  201. >
  202. struct catch_all_push_back;
  203. template <typename TryCatch, typename Expr>
  204. struct catch_all_push_back<TryCatch, Expr, 1>
  205. {
  206. typedef
  207. typename proto::result_of::make_expr<
  208. phoenix::tag::catch_all
  209. , proto::basic_default_domain
  210. , Expr
  211. >::type
  212. catch_expr;
  213. typedef
  214. phoenix::expression::try_catch<
  215. TryCatch
  216. , catch_expr
  217. >
  218. gen_type;
  219. typedef typename gen_type::type type;
  220. static type make(TryCatch const& try_catch, Expr const& catch_)
  221. {
  222. return
  223. gen_type::make(
  224. try_catch
  225. , proto::make_expr<
  226. phoenix::tag::catch_all
  227. , proto::basic_default_domain
  228. >(catch_)
  229. );
  230. }
  231. };
  232. #include <boost/phoenix/statement/detail/catch_push_back.hpp>
  233. }
  234. template <typename Dummy>
  235. struct is_nullary::when<rule::try_catch, Dummy>
  236. : proto::call<
  237. detail::try_catch_is_nullary(
  238. proto::_
  239. , proto::make<proto::empty_env()>
  240. , _context
  241. )
  242. >
  243. {};
  244. template <typename TryCatch, typename Exception>
  245. struct catch_gen
  246. {
  247. catch_gen(TryCatch const& try_catch) : try_catch(try_catch) {}
  248. template <typename Expr>
  249. typename boost::disable_if<
  250. proto::matches<
  251. typename proto::result_of::child_c<
  252. TryCatch
  253. , proto::arity_of<TryCatch>::value - 1
  254. >::type
  255. , rule::catch_all
  256. >
  257. , typename detail::catch_push_back<TryCatch, Exception, Expr>::type
  258. >::type
  259. operator[](Expr const& expr) const
  260. {
  261. return
  262. detail::catch_push_back<TryCatch, Exception, Expr>::make(
  263. try_catch, expr
  264. );
  265. }
  266. TryCatch const & try_catch;
  267. };
  268. template <typename TryCatch>
  269. struct catch_all_gen
  270. {
  271. catch_all_gen(TryCatch const& try_catch) : try_catch(try_catch) {}
  272. template <typename Expr>
  273. typename boost::disable_if<
  274. proto::matches<
  275. typename proto::result_of::child_c<
  276. TryCatch
  277. , proto::arity_of<TryCatch>::value - 1
  278. >::type
  279. , rule::catch_all
  280. >
  281. , typename detail::catch_all_push_back<TryCatch, Expr>::type
  282. >::type
  283. operator[](Expr const& expr) const
  284. {
  285. return detail::catch_all_push_back<TryCatch, Expr>::make(
  286. try_catch, expr
  287. );
  288. }
  289. TryCatch const & try_catch;
  290. };
  291. template <
  292. typename Expr
  293. >
  294. struct try_catch_actor;
  295. template <typename Expr>
  296. struct try_catch_actor
  297. : actor<Expr>
  298. {
  299. typedef try_catch_actor<Expr> that_type;
  300. typedef actor<Expr> base_type;
  301. try_catch_actor(base_type const& expr)
  302. : base_type(expr)
  303. , catch_all(*this)
  304. {
  305. }
  306. template <typename Exception>
  307. catch_gen<that_type, Exception> const
  308. catch_() const
  309. {
  310. return catch_gen<that_type, Exception>(*this);
  311. }
  312. catch_all_gen<that_type> const catch_all;
  313. };
  314. struct try_gen
  315. {
  316. template <typename Try>
  317. typename expression::try_catch<Try>::type const
  318. operator[](Try const & try_) const
  319. {
  320. return expression::try_catch<Try>::make(try_);
  321. }
  322. };
  323. #ifndef BOOST_PHOENIX_NO_PREDEFINED_TERMINALS
  324. try_gen const try_ = {};
  325. #endif
  326. }}
  327. #ifdef _MSC_VER
  328. #pragma warning(pop)
  329. #endif
  330. #endif