function_eval.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Copyright (c) 2015 Kohei Takahashi
  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_CORE_DETAIL_FUNCTION_EVAL_HPP
  8. #define BOOST_PHOENIX_CORE_DETAIL_FUNCTION_EVAL_HPP
  9. #include <boost/phoenix/core/limits.hpp>
  10. #include <boost/phoenix/support/iterate.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/utility/result_of.hpp>
  15. #ifndef BOOST_PHOENIX_NO_VARIADIC_FUNCTION_EVAL
  16. # include <boost/mpl/if.hpp>
  17. # include <boost/type_traits/is_reference.hpp>
  18. #endif
  19. #ifdef BOOST_PHOENIX_NO_VARIADIC_EXPRESSION
  20. # include <boost/phoenix/core/detail/cpp03/function_eval_expr.hpp>
  21. #else
  22. BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG(
  23. (boost)(phoenix)(detail)(function_eval)
  24. , (meta_grammar)(meta_grammar)
  25. , _
  26. )
  27. #endif
  28. namespace boost { namespace phoenix {
  29. namespace detail
  30. {
  31. template <typename T>
  32. T& help_rvalue_deduction(T& x)
  33. {
  34. return x;
  35. }
  36. template <typename T>
  37. T const& help_rvalue_deduction(T const& x)
  38. {
  39. return x;
  40. }
  41. struct function_eval
  42. {
  43. template <typename Sig>
  44. struct result;
  45. #ifdef BOOST_PHOENIX_NO_VARIADIC_FUNCTION_EVAL
  46. template <typename This, typename F, typename Context>
  47. struct result<This(F, Context)>
  48. {
  49. typedef typename
  50. remove_reference<
  51. typename boost::result_of<evaluator(F, Context)>::type
  52. >::type
  53. fn;
  54. typedef typename boost::result_of<fn()>::type type;
  55. };
  56. template <typename F, typename Context>
  57. typename result<function_eval(F const&, Context const&)>::type
  58. operator()(F const & f, Context const & ctx) const
  59. {
  60. return boost::phoenix::eval(f, ctx)();
  61. }
  62. template <typename F, typename Context>
  63. typename result<function_eval(F &, Context const&)>::type
  64. operator()(F & f, Context const & ctx) const
  65. {
  66. return boost::phoenix::eval(f, ctx)();
  67. }
  68. #include <boost/phoenix/core/detail/cpp03/function_eval.hpp>
  69. #else
  70. template <typename, typename, typename...> struct result_impl;
  71. template <typename F, typename... A, typename Head, typename... Tail>
  72. struct result_impl<F, void(A...), Head, Tail...>
  73. : result_impl<F, void(A..., Head), Tail...>
  74. {
  75. };
  76. template <typename F, typename... A, typename Context>
  77. struct result_impl<F, void(A...), Context>
  78. {
  79. typedef typename
  80. remove_reference<
  81. typename boost::result_of<evaluator(F, Context)>::type
  82. >::type
  83. fn;
  84. template <typename T>
  85. struct result_of_evaluator
  86. {
  87. typedef typename boost::add_reference<
  88. typename boost::add_const<
  89. typename boost::result_of<
  90. boost::phoenix::evaluator(T, Context)
  91. >::type
  92. >::type
  93. >::type type;
  94. };
  95. typedef typename
  96. boost::result_of<
  97. fn(typename result_of_evaluator<A>::type...)
  98. >::type
  99. type;
  100. static type call(F f, A... a, Context ctx)
  101. {
  102. return boost::phoenix::eval(f, ctx)(help_rvalue_deduction(boost::phoenix::eval(a, ctx))...);
  103. }
  104. };
  105. template <typename This, typename F, typename... A>
  106. struct result<This(F, A...)>
  107. : result_impl<F, void(), A...>
  108. {
  109. };
  110. template <typename F, typename... A>
  111. typename result<
  112. function_eval(
  113. F const &
  114. , typename mpl::if_<is_reference<A>, A, A const &>::type...
  115. )
  116. >::type
  117. // 'A &... a, Context const &ctx' doesn't work as intended: type deduction always fail.
  118. operator()(F && f, A &&... a) const
  119. {
  120. return
  121. result<
  122. function_eval(
  123. typename mpl::if_<is_reference<F>, F, F const &>::type
  124. , typename mpl::if_<is_reference<A>, A, A const &>::type...
  125. )
  126. >::call(f, a...);
  127. }
  128. #endif
  129. };
  130. }
  131. template <typename Dummy>
  132. struct default_actions::when<detail::rule::function_eval, Dummy>
  133. : phoenix::call<detail::function_eval>
  134. {};
  135. }}
  136. #endif