/src/contrib/boost/spirit/home/phoenix/scope/lambda.hpp

http://pythonocc.googlecode.com/ · C++ Header · 176 lines · 144 code · 24 blank · 8 comment · 0 complexity · 9b37270d3d2f179d61bb0a9cd817b1f4 MD5 · raw file

  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Copyright (c) 2004 Daniel Wallin
  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 PHOENIX_SCOPE_LAMBDA_HPP
  8. #define PHOENIX_SCOPE_LAMBDA_HPP
  9. #include <boost/spirit/home/phoenix/core/limits.hpp>
  10. #include <boost/spirit/home/phoenix/core/composite.hpp>
  11. #include <boost/spirit/home/phoenix/scope/scoped_environment.hpp>
  12. #include <boost/spirit/home/phoenix/scope/detail/local_variable.hpp>
  13. #include <boost/spirit/home/phoenix/detail/local_reference.hpp>
  14. #include <boost/spirit/home/phoenix/core/actor.hpp>
  15. #include <boost/fusion/include/transform.hpp>
  16. #include <boost/fusion/include/as_vector.hpp>
  17. namespace boost { namespace phoenix
  18. {
  19. template <typename Base, typename OuterEnv, typename Locals, typename Map>
  20. struct lambda_eval : Base
  21. {
  22. template <typename Env>
  23. struct result
  24. {
  25. typedef typename Base::template
  26. result<scoped_environment<Env, OuterEnv, Locals, Map> >::type
  27. result_type;
  28. typedef typename
  29. detail::unwrap_local_reference<result_type>::type
  30. type;
  31. };
  32. lambda_eval(
  33. Base const& base
  34. , OuterEnv const& outer_env
  35. , Locals const& locals)
  36. : Base(base)
  37. , outer_env(outer_env)
  38. , locals(locals) {}
  39. template <typename Env>
  40. typename result<Env>::type
  41. eval(Env const& env) const
  42. {
  43. typedef typename result<Env>::type RT;
  44. return RT(Base::eval(
  45. scoped_environment<Env, OuterEnv, Locals, Map>(
  46. env, outer_env, locals)));
  47. }
  48. OuterEnv outer_env;
  49. mutable Locals locals;
  50. };
  51. template <typename Base, typename Vars, typename Map>
  52. struct lambda_actor
  53. {
  54. typedef typename
  55. mpl::fold<
  56. Vars
  57. , mpl::false_
  58. , detail::compute_no_nullary
  59. >::type
  60. no_nullary;
  61. template <typename Env>
  62. struct result
  63. {
  64. typedef typename
  65. fusion::result_of::as_vector<
  66. typename fusion::result_of::transform<
  67. Vars
  68. , detail::initialize_local<Env>
  69. >::type
  70. >::type
  71. locals_type;
  72. typedef actor<lambda_eval<Base, Env, locals_type, Map> > type;
  73. };
  74. lambda_actor(Base const& f, Vars const& vars)
  75. : f(f), vars(vars) {}
  76. template <typename Env>
  77. typename result<Env>::type
  78. eval(Env const& env) const
  79. {
  80. typedef typename result<Env>::type result_type;
  81. return result_type(
  82. f, env, fusion::as_vector(
  83. fusion::transform(
  84. vars
  85. , detail::initialize_local<Env>(env)
  86. )));
  87. }
  88. Base f;
  89. Vars vars;
  90. };
  91. template <typename Vars, typename Map>
  92. struct lambda_actor_gen
  93. {
  94. template <typename Base>
  95. actor<lambda_actor<Base, Vars, Map> > const
  96. operator[](actor<Base> const& f) const
  97. {
  98. return lambda_actor<Base, Vars, Map>(f, vars);
  99. }
  100. lambda_actor_gen(Vars const& vars)
  101. : vars(vars) {}
  102. Vars vars;
  103. };
  104. template <typename Key>
  105. struct local_variable; // forward
  106. struct assign_eval; // forward
  107. struct lambda_gen
  108. : lambda_actor_gen<
  109. fusion::vector<>
  110. , detail::map_local_index_to_tuple<> >
  111. {
  112. typedef
  113. lambda_actor_gen<
  114. fusion::vector<>
  115. , detail::map_local_index_to_tuple<> >
  116. base_type;
  117. lambda_gen()
  118. : base_type(fusion::vector<>())
  119. {
  120. }
  121. template <typename K0, typename V0>
  122. lambda_actor_gen<
  123. fusion::vector<V0>
  124. , detail::map_local_index_to_tuple<K0>
  125. >
  126. operator()(
  127. actor<composite<assign_eval, fusion::vector<local_variable<K0>, V0> > > const& a0
  128. ) const
  129. {
  130. return fusion::vector<V0>(fusion::at_c<1>(a0));
  131. }
  132. template <typename K0, typename K1, typename V0, typename V1>
  133. lambda_actor_gen<
  134. fusion::vector<V0, V1>
  135. , detail::map_local_index_to_tuple<K0, K1>
  136. >
  137. operator()(
  138. actor<composite<assign_eval, fusion::vector<local_variable<K0>, V0> > > const& a0
  139. , actor<composite<assign_eval, fusion::vector<local_variable<K1>, V1> > > const& a1
  140. ) const
  141. {
  142. return fusion::vector<V0, V1>(fusion::at_c<1>(a0), fusion::at_c<1>(a1));
  143. }
  144. // Bring in the rest...
  145. #define PHOENIX_LOCAL_GEN_NAME lambda_actor_gen
  146. #include <boost/spirit/home/phoenix/scope/detail/local_gen.hpp>
  147. #undef PHOENIX_LOCAL_GEN_NAME
  148. };
  149. lambda_gen const lambda = lambda_gen();
  150. }}
  151. #endif