value.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_CORE_VALUE_HPP
  8. #define BOOST_PHOENIX_CORE_VALUE_HPP
  9. #include <boost/phoenix/core/limits.hpp>
  10. #include <boost/phoenix/core/actor.hpp>
  11. #include <boost/phoenix/core/as_actor.hpp>
  12. #include <boost/phoenix/core/terminal.hpp>
  13. #include <boost/phoenix/core/is_value.hpp>
  14. #include <boost/utility/result_of.hpp>
  15. namespace boost { namespace phoenix
  16. {
  17. ////////////////////////////////////////////////////////////////////////////
  18. //
  19. // values
  20. //
  21. // function for evaluating values, e.g. val(123)
  22. //
  23. ////////////////////////////////////////////////////////////////////////////
  24. namespace expression
  25. {
  26. template <typename T>
  27. struct value
  28. : expression::terminal<T>
  29. {
  30. typedef
  31. typename expression::terminal<T>::type
  32. type;
  33. /*
  34. static const type make(T & t)
  35. {
  36. typename value<T>::type const e = {{t}};
  37. return e;
  38. }
  39. */
  40. };
  41. }
  42. template <typename T>
  43. inline
  44. typename expression::value<T>::type const
  45. val(T t)
  46. {
  47. return expression::value<T>::make(t);
  48. }
  49. // Identifies this Expr as a value.
  50. // I think this is wrong. It is identifying all actors as values.
  51. // Yes, it is giving false positives and needs a rethink.
  52. // And this gives no positives.
  53. //template <typename T>
  54. //struct is_value<expression::value<T> >
  55. // : mpl::true_
  56. //{};
  57. // Call out actor for special handling
  58. // Is this correct? It applies to any actor.
  59. // In which case why is it here?
  60. template<typename Expr>
  61. struct is_custom_terminal<actor<Expr> >
  62. : mpl::true_
  63. {};
  64. // Special handling for actor
  65. template<typename Expr>
  66. struct custom_terminal<actor<Expr> >
  67. {
  68. template <typename Sig>
  69. struct result;
  70. template <typename This, typename Actor, typename Context>
  71. struct result<This(Actor, Context)>
  72. : boost::remove_const<
  73. typename boost::remove_reference<
  74. typename evaluator::impl<Actor, Context, proto::empty_env>::result_type
  75. >::type
  76. >
  77. {};
  78. template <typename Context>
  79. typename result<custom_terminal(actor<Expr> const &, Context &)>::type
  80. operator()(actor<Expr> const & expr, Context & ctx) const
  81. {
  82. typedef typename result<custom_terminal(actor<Expr> const &, Context &)>::type result_type;
  83. result_type r = boost::phoenix::eval(expr, ctx);
  84. // std::cout << "Evaluating val() = " << r << std::endl;
  85. return r;
  86. }
  87. };
  88. namespace meta
  89. {
  90. template<typename T>
  91. struct const_ref
  92. : add_reference<typename add_const<T>::type>
  93. {};
  94. template<typename T>
  95. struct argument_type
  96. : mpl::eval_if_c<
  97. is_function<typename remove_pointer<T>::type>::value
  98. , mpl::identity<T>
  99. , const_ref<T>
  100. >
  101. {
  102. typedef T type;
  103. };
  104. template <typename T>
  105. struct decay
  106. {
  107. typedef T type;
  108. };
  109. template <typename T, int N>
  110. struct decay<T[N]> : decay<T const *> {};
  111. }
  112. template <typename T>
  113. struct as_actor<T, mpl::false_>
  114. {
  115. typedef typename expression::value<typename meta::decay<T>::type >::type type;
  116. static type
  117. convert(typename meta::argument_type<typename meta::decay<T>::type>::type t)
  118. {
  119. return expression::value<typename meta::decay<T>::type >::make(t);
  120. }
  121. };
  122. }}
  123. #endif