result_of.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_LAMBDA_RESULT_OF_HPP
  11. #define BOOST_COMPUTE_LAMBDA_RESULT_OF_HPP
  12. #include <boost/mpl/vector.hpp>
  13. #include <boost/proto/proto.hpp>
  14. #include <boost/compute/type_traits/common_type.hpp>
  15. namespace boost {
  16. namespace compute {
  17. namespace lambda {
  18. namespace mpl = boost::mpl;
  19. namespace proto = boost::proto;
  20. // meta-function returning the result type of a lambda expression
  21. template<class Expr,
  22. class Args = void,
  23. class Tags = typename proto::tag_of<Expr>::type>
  24. struct result_of
  25. {
  26. };
  27. // terminals
  28. template<class Expr, class Args>
  29. struct result_of<Expr, Args, proto::tag::terminal>
  30. {
  31. typedef typename proto::result_of::value<Expr>::type type;
  32. };
  33. // binary operators
  34. #define BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(tag) \
  35. template<class Expr, class Args> \
  36. struct result_of<Expr, Args, tag> \
  37. { \
  38. typedef typename proto::result_of::child_c<Expr, 0>::type left; \
  39. typedef typename proto::result_of::child_c<Expr, 1>::type right; \
  40. \
  41. typedef typename boost::common_type< \
  42. typename ::boost::compute::lambda::result_of< \
  43. left, \
  44. Args, \
  45. typename proto::tag_of<left>::type>::type, \
  46. typename ::boost::compute::lambda::result_of< \
  47. right, \
  48. Args, \
  49. typename proto::tag_of<right>::type>::type \
  50. >::type type; \
  51. };
  52. BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(proto::tag::plus)
  53. BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(proto::tag::minus)
  54. BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(proto::tag::multiplies)
  55. BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(proto::tag::divides)
  56. BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(proto::tag::modulus)
  57. BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(proto::tag::bitwise_and)
  58. BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(proto::tag::bitwise_or)
  59. BOOST_COMPUTE_LAMBDA_RESULT_OF_BINARY_OPERATOR(proto::tag::bitwise_xor)
  60. // comparision operators
  61. #define BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(tag) \
  62. template<class Expr, class Args> \
  63. struct result_of<Expr, Args, tag> \
  64. { \
  65. typedef bool type; \
  66. };
  67. BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(proto::tag::less)
  68. BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(proto::tag::greater)
  69. BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(proto::tag::less_equal)
  70. BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(proto::tag::greater_equal)
  71. BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(proto::tag::equal_to)
  72. BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(proto::tag::not_equal_to)
  73. BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(proto::tag::logical_and)
  74. BOOST_COMPUTE_LAMBDA_RESULT_OF_COMPARISON_OPERATOR(proto::tag::logical_or)
  75. // assignment operator
  76. template<class Expr, class Args>
  77. struct result_of<Expr, Args, proto::tag::assign>
  78. {
  79. typedef typename proto::result_of::child_c<Expr, 0>::type left;
  80. typedef typename proto::result_of::child_c<Expr, 1>::type right;
  81. typedef typename ::boost::compute::lambda::result_of<
  82. right, Args, typename proto::tag_of<right>::type
  83. >::type type;
  84. };
  85. // functions
  86. template<class Expr, class Args>
  87. struct result_of<Expr, Args, proto::tag::function>
  88. {
  89. typedef typename proto::result_of::child_c<Expr, 0>::type func_expr;
  90. typedef typename proto::result_of::value<func_expr>::type func;
  91. typedef typename func::template lambda_result<Expr, Args>::type type;
  92. };
  93. } // end lambda namespace
  94. } // end compute namespace
  95. } // end boost namespace
  96. #endif // BOOST_COMPUTE_LAMBDA_RESULT_OF_HPP