make_tuple.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <[email protected]>
  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_MAKE_TUPLE_HPP
  11. #define BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_HPP
  12. #include <boost/preprocessor/repetition.hpp>
  13. #include <boost/compute/config.hpp>
  14. #include <boost/compute/types/tuple.hpp>
  15. namespace boost {
  16. namespace compute {
  17. namespace lambda {
  18. namespace detail {
  19. // function wrapper for make_tuple() in lambda expressions
  20. struct make_tuple_func
  21. {
  22. template<class Expr, class Args, int N>
  23. struct make_tuple_result_type;
  24. #define BOOST_COMPUTE_MAKE_TUPLE_RESULT_GET_ARG(z, n, unused) \
  25. typedef typename proto::result_of::child_c<Expr, BOOST_PP_INC(n)>::type BOOST_PP_CAT(Arg, n);
  26. #define BOOST_COMPUTE_MAKE_TUPLE_RESULT_GET_ARG_TYPE(z, n, unused) \
  27. typedef typename lambda::result_of<BOOST_PP_CAT(Arg, n), Args>::type BOOST_PP_CAT(T, n);
  28. #define BOOST_COMPUTE_MAKE_TUPLE_RESULT_TYPE(z, n, unused) \
  29. template<class Expr, class Args> \
  30. struct make_tuple_result_type<Expr, Args, n> \
  31. { \
  32. BOOST_PP_REPEAT(n, BOOST_COMPUTE_MAKE_TUPLE_RESULT_GET_ARG, ~) \
  33. BOOST_PP_REPEAT(n, BOOST_COMPUTE_MAKE_TUPLE_RESULT_GET_ARG_TYPE, ~) \
  34. typedef boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> type; \
  35. };
  36. BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_MAKE_TUPLE_RESULT_TYPE, ~)
  37. #undef BOOST_COMPUTE_MAKE_TUPLE_RESULT_GET_ARG
  38. #undef BOOST_COMPUTE_MAKE_TUPLE_RESULT_GET_ARG_TYPE
  39. #undef BOOST_COMPUTE_MAKE_TUPLE_RESULT_TYPE
  40. template<class Expr, class Args>
  41. struct lambda_result
  42. {
  43. typedef typename make_tuple_result_type<
  44. Expr, Args, proto::arity_of<Expr>::value - 1
  45. >::type type;
  46. };
  47. #define BOOST_COMPUTE_MAKE_TUPLE_GET_ARG_TYPE(z, n, unused) \
  48. typedef typename lambda::result_of< \
  49. BOOST_PP_CAT(Arg, n), typename Context::args_tuple \
  50. >::type BOOST_PP_CAT(T, n);
  51. #define BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_APPLY_ARG(z, n, unused) \
  52. BOOST_PP_COMMA_IF(n) BOOST_PP_CAT(const Arg, n) BOOST_PP_CAT(&arg, n)
  53. #define BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_APPLY_EVAL_ARG(z, n, unused) \
  54. BOOST_PP_EXPR_IF(n, ctx.stream << ", ";) proto::eval(BOOST_PP_CAT(arg, n), ctx);
  55. #define BOOST_COMPUTE_MAKE_TUPLE_APPLY(z, n, unused) \
  56. template<class Context, BOOST_PP_ENUM_PARAMS(n, class Arg)> \
  57. static void apply(Context &ctx, BOOST_PP_REPEAT(n, BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_APPLY_ARG, ~)) \
  58. { \
  59. BOOST_PP_REPEAT(n, BOOST_COMPUTE_MAKE_TUPLE_GET_ARG_TYPE, ~) \
  60. typedef typename boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> tuple_type; \
  61. ctx.stream.template inject_type<tuple_type>(); \
  62. ctx.stream << "((" << type_name<tuple_type>() << "){"; \
  63. BOOST_PP_REPEAT(n, BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_APPLY_EVAL_ARG, ~) \
  64. ctx.stream << "})"; \
  65. }
  66. BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_MAKE_TUPLE_APPLY, ~)
  67. #undef BOOST_COMPUTE_MAKE_TUPLE_GET_ARG_TYPE
  68. #undef BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_APPLY_ARG
  69. #undef BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_APPLY_EVAL_ARG
  70. #undef BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_APPLY
  71. };
  72. } // end detail namespace
  73. #define BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_ARG(z, n, unused) \
  74. BOOST_PP_COMMA_IF(n) BOOST_PP_CAT(const Arg, n) BOOST_PP_CAT(&arg, n)
  75. #define BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_ARG_TYPE(z, n, unused) \
  76. BOOST_PP_COMMA_IF(n) BOOST_PP_CAT(const Arg, n) &
  77. #define BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_REF_ARG(z, n, unused) \
  78. BOOST_PP_COMMA_IF(n) ::boost::ref(BOOST_PP_CAT(arg, n))
  79. #define BOOST_COMPUTE_LAMBDA_MAKE_TUPLE(z, n, unused) \
  80. template<BOOST_PP_ENUM_PARAMS(n, class Arg)> \
  81. inline typename proto::result_of::make_expr< \
  82. proto::tag::function, \
  83. detail::make_tuple_func, \
  84. BOOST_PP_REPEAT(n, BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_ARG_TYPE, ~) \
  85. >::type \
  86. make_tuple(BOOST_PP_REPEAT(n, BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_ARG, ~)) \
  87. { \
  88. return proto::make_expr<proto::tag::function>( \
  89. detail::make_tuple_func(), \
  90. BOOST_PP_REPEAT(n, BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_REF_ARG, ~) \
  91. ); \
  92. }
  93. BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_LAMBDA_MAKE_TUPLE, ~)
  94. #undef BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_ARG
  95. #undef BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_ARG_TYPE
  96. #undef BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_REF_ARG
  97. #undef BOOST_COMPUTE_LAMBDA_MAKE_TUPLE
  98. } // end lambda namespace
  99. } // end compute namespace
  100. } // end boost namespace
  101. #endif // BOOST_COMPUTE_LAMBDA_MAKE_TUPLE_HPP