lambda.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #ifndef BOOST_PHOENIX_SCOPE_LAMBDA_HPP
  2. #define BOOST_PHOENIX_SCOPE_LAMBDA_HPP
  3. #include <boost/phoenix/core/limits.hpp>
  4. #include <boost/fusion/include/transform.hpp>
  5. #include <boost/fusion/include/as_vector.hpp>
  6. #include <boost/mpl/int.hpp>
  7. #include <boost/phoenix/core/call.hpp>
  8. #include <boost/phoenix/core/expression.hpp>
  9. #include <boost/phoenix/core/meta_grammar.hpp>
  10. #include <boost/phoenix/scope/local_variable.hpp>
  11. #include <boost/phoenix/scope/scoped_environment.hpp>
  12. #if !defined(BOOST_PHOENIX_DONT_USE_PREPROCESSED_FILES)
  13. #include <boost/phoenix/scope/preprocessed/lambda.hpp>
  14. #else
  15. #if defined(__WAVE__) && defined(BOOST_PHOENIX_CREATE_PREPROCESSED_FILES)
  16. #pragma wave option(preserve: 2, line: 0, output: "preprocessed/lambda_" BOOST_PHOENIX_LIMIT_STR ".hpp")
  17. #endif
  18. /*==============================================================================
  19. Copyright (c) 2001-2010 Joel de Guzman
  20. Copyright (c) 2004 Daniel Wallin
  21. Copyright (c) 2010 Thomas Heller
  22. Distributed under the Boost Software License, Version 1.0. (See accompanying
  23. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  24. ==============================================================================*/
  25. #if defined(__WAVE__) && defined(BOOST_PHOENIX_CREATE_PREPROCESSED_FILES)
  26. #pragma wave option(preserve: 1)
  27. #endif
  28. BOOST_PHOENIX_DEFINE_EXPRESSION(
  29. (boost)(phoenix)(lambda_actor)
  30. , (proto::terminal<proto::_>) // Locals
  31. (proto::terminal<proto::_>) // Map
  32. (meta_grammar) // Lambda
  33. )
  34. BOOST_PHOENIX_DEFINE_EXPRESSION(
  35. (boost)(phoenix)(lambda)
  36. , (proto::terminal<proto::_>) // OuterEnv
  37. (proto::terminal<proto::_>) // Locals
  38. (proto::terminal<proto::_>) // Map
  39. (meta_grammar) // Lambda
  40. )
  41. namespace boost { namespace phoenix
  42. {
  43. struct lambda_eval
  44. {
  45. BOOST_PROTO_CALLABLE()
  46. template <typename Sig>
  47. struct result;
  48. template <
  49. typename This
  50. , typename OuterEnv
  51. , typename Locals
  52. , typename Map
  53. , typename Lambda
  54. , typename Context
  55. >
  56. struct result<This(OuterEnv, Locals, Map, Lambda, Context)>
  57. {
  58. typedef
  59. typename proto::detail::uncvref<
  60. typename proto::result_of::value<
  61. OuterEnv
  62. >::type
  63. >::type
  64. outer_env_type;
  65. typedef
  66. typename proto::detail::uncvref<
  67. typename proto::result_of::value<
  68. Locals
  69. >::type
  70. >::type
  71. locals_type;
  72. typedef
  73. typename proto::detail::uncvref<
  74. typename proto::result_of::value<
  75. Map
  76. >::type
  77. >::type
  78. map_type;
  79. typedef
  80. typename proto::detail::uncvref<
  81. typename result_of::env<Context>::type
  82. >::type
  83. env_type;
  84. typedef
  85. typename result_of::eval<
  86. Lambda
  87. , typename result_of::context<
  88. scoped_environment<
  89. env_type
  90. , outer_env_type
  91. , locals_type
  92. , map_type
  93. >
  94. , typename result_of::actions<
  95. Context
  96. >::type
  97. >::type
  98. >::type
  99. type;
  100. };
  101. template <typename OuterEnv, typename Locals, typename Map, typename Lambda, typename Context>
  102. typename result<lambda_eval(OuterEnv const &, Locals const &, Map const &, Lambda const &, Context const &)>::type
  103. operator()(OuterEnv const & outer_env, Locals const & locals, Map const &, Lambda const & lambda, Context const & ctx) const
  104. {
  105. typedef
  106. typename proto::detail::uncvref<
  107. typename proto::result_of::value<
  108. OuterEnv
  109. >::type
  110. >::type
  111. outer_env_type;
  112. typedef
  113. typename proto::detail::uncvref<
  114. typename proto::result_of::value<
  115. Locals
  116. >::type
  117. >::type
  118. locals_type;
  119. typedef
  120. typename proto::detail::uncvref<
  121. typename proto::result_of::value<
  122. Map
  123. >::type
  124. >::type
  125. map_type;
  126. typedef
  127. typename proto::detail::uncvref<
  128. typename result_of::env<Context>::type
  129. >::type
  130. env_type;
  131. scoped_environment<
  132. env_type
  133. , outer_env_type
  134. , locals_type
  135. , map_type
  136. >
  137. env(phoenix::env(ctx), proto::value(outer_env), proto::value(locals));
  138. return eval(lambda, phoenix::context(env, phoenix::actions(ctx)));
  139. }
  140. };
  141. template <typename Dummy>
  142. struct default_actions::when<rule::lambda, Dummy>
  143. : call<lambda_eval, Dummy>
  144. {};
  145. template <typename Dummy>
  146. struct is_nullary::when<rule::lambda, Dummy>
  147. : proto::call<
  148. evaluator(
  149. proto::_child_c<3>
  150. , proto::call<
  151. functional::context(
  152. proto::make<
  153. mpl::true_()
  154. >
  155. , proto::make<
  156. detail::scope_is_nullary_actions()
  157. >
  158. )
  159. >
  160. , proto::make<
  161. proto::empty_env()
  162. >
  163. )
  164. >
  165. {};
  166. template <typename Dummy>
  167. struct is_nullary::when<rule::lambda_actor, Dummy>
  168. : proto::or_<
  169. proto::when<
  170. expression::lambda_actor<
  171. proto::terminal<vector0<> >
  172. , proto::terminal<proto::_>
  173. , meta_grammar
  174. >
  175. , mpl::true_()
  176. >
  177. , proto::when<
  178. expression::lambda_actor<
  179. proto::terminal<proto::_>
  180. , proto::terminal<proto::_>
  181. , meta_grammar
  182. >
  183. , proto::fold<
  184. proto::call<proto::_value(proto::_child_c<0>)>
  185. , proto::make<mpl::true_()>
  186. , proto::make<
  187. mpl::and_<
  188. proto::_state
  189. , proto::call<
  190. evaluator(
  191. proto::_
  192. , _context
  193. , proto::make<proto::empty_env()>
  194. )
  195. >
  196. >()
  197. >
  198. >
  199. >
  200. >
  201. {};
  202. struct lambda_actor_eval
  203. {
  204. template <typename Sig>
  205. struct result;
  206. template <typename This, typename Vars, typename Map, typename Lambda, typename Context>
  207. struct result<This(Vars, Map, Lambda, Context)>
  208. {
  209. typedef
  210. typename proto::detail::uncvref<
  211. typename result_of::env<Context>::type
  212. >::type
  213. env_type;
  214. typedef
  215. typename proto::detail::uncvref<
  216. typename result_of::actions<Context>::type
  217. >::type
  218. actions_type;
  219. typedef
  220. typename proto::detail::uncvref<
  221. typename proto::result_of::value<Vars>::type
  222. >::type
  223. vars_type;
  224. typedef typename
  225. detail::result_of::initialize_locals<
  226. vars_type
  227. , Context
  228. >::type
  229. locals_type;
  230. typedef
  231. typename expression::lambda<
  232. env_type
  233. , locals_type
  234. , Map
  235. , Lambda
  236. >::type const
  237. type;
  238. };
  239. template <
  240. typename Vars
  241. , typename Map
  242. , typename Lambda
  243. , typename Context
  244. >
  245. typename result<
  246. lambda_actor_eval(Vars const&, Map const &, Lambda const&, Context const &)
  247. >::type const
  248. operator()(Vars const& vars, Map const& map, Lambda const& lambda, Context const & ctx) const
  249. {
  250. typedef
  251. typename proto::detail::uncvref<
  252. typename result_of::env<Context>::type
  253. >::type
  254. env_type;
  255. /*typedef
  256. typename proto::detail::uncvref<
  257. typename result_of::actions<Context>::type
  258. >::type
  259. actions_type;*/
  260. typedef
  261. typename proto::detail::uncvref<
  262. typename proto::result_of::value<Vars>::type
  263. >::type
  264. vars_type;
  265. /*typedef
  266. typename proto::detail::uncvref<
  267. typename proto::result_of::value<Map>::type
  268. >::type
  269. map_type;*/
  270. typedef typename
  271. detail::result_of::initialize_locals<
  272. vars_type
  273. , Context
  274. >::type
  275. locals_type;
  276. locals_type locals = initialize_locals(proto::value(vars), ctx);
  277. return
  278. expression::
  279. lambda<env_type, locals_type, Map, Lambda>::
  280. make(phoenix::env(ctx), locals, map, lambda);
  281. }
  282. };
  283. template <typename Dummy>
  284. struct default_actions::when<rule::lambda_actor, Dummy>
  285. : call<lambda_actor_eval, Dummy>
  286. {};
  287. template <typename Locals = void, typename Map = void, typename Dummy = void>
  288. struct lambda_actor_gen;
  289. template <>
  290. struct lambda_actor_gen<void, void, void>
  291. {
  292. template <typename Expr>
  293. typename expression::lambda_actor<vector0<>, detail::map_local_index_to_tuple<>, Expr>::type const
  294. operator[](Expr const & expr) const
  295. {
  296. typedef vector0<> locals_type;
  297. typedef detail::map_local_index_to_tuple<> map_type;
  298. return expression::lambda_actor<locals_type, map_type, Expr>::make(locals_type(), map_type(), expr);
  299. }
  300. };
  301. template <typename Locals, typename Map>
  302. struct lambda_actor_gen<Locals, Map>
  303. {
  304. lambda_actor_gen(Locals const & locals_)
  305. : locals(locals_)
  306. {}
  307. lambda_actor_gen(lambda_actor_gen const & o)
  308. : locals(o.locals)
  309. {};
  310. template <typename Expr>
  311. typename expression::lambda_actor<
  312. Locals
  313. , Map
  314. , Expr
  315. >::type const
  316. operator[](Expr const & expr) const
  317. {
  318. return expression::lambda_actor<Locals, Map, Expr>::make(locals, Map(), expr);
  319. }
  320. Locals locals;
  321. };
  322. struct lambda_local_gen
  323. : lambda_actor_gen<>
  324. {
  325. lambda_actor_gen<> const
  326. operator()() const
  327. {
  328. return lambda_actor_gen<>();
  329. }
  330. #define BOOST_PHOENIX_SCOPE_ACTOR_GEN_NAME lambda_actor_gen
  331. #define BOOST_PHOENIX_SCOPE_ACTOR_GEN_FUNCTION operator()
  332. #define BOOST_PHOENIX_SCOPE_ACTOR_GEN_CONST const
  333. #include <boost/phoenix/scope/detail/local_gen.hpp>
  334. #undef BOOST_PHOENIX_SCOPE_ACTOR_GEN_NAME
  335. #undef BOOST_PHOENIX_SCOPE_ACTOR_GEN_FUNCTION
  336. #undef BOOST_PHOENIX_SCOPE_ACTOR_GEN_CONST
  337. };
  338. typedef lambda_local_gen lambda_type;
  339. lambda_local_gen const lambda = lambda_local_gen();
  340. }}
  341. #if defined(__WAVE__) && defined(BOOST_PHOENIX_CREATE_PREPROCESSED_FILES)
  342. #pragma wave option(output: null)
  343. #endif
  344. #endif
  345. #endif