placeholders.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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_PLACEHOLDERS_HPP
  11. #define BOOST_COMPUTE_LAMBDA_PLACEHOLDERS_HPP
  12. #include <boost/mpl/has_xxx.hpp>
  13. #include <boost/compute/lambda/context.hpp>
  14. #include <boost/compute/lambda/result_of.hpp>
  15. namespace boost {
  16. namespace compute {
  17. namespace lambda {
  18. namespace mpl = boost::mpl;
  19. namespace proto = boost::proto;
  20. // lambda placeholders
  21. expression<proto::terminal<placeholder<0> >::type> const _1;
  22. expression<proto::terminal<placeholder<1> >::type> const _2;
  23. expression<proto::terminal<placeholder<2> >::type> const _3;
  24. namespace detail {
  25. BOOST_MPL_HAS_XXX_TRAIT_DEF(result_type)
  26. template<class T, bool HasResultType>
  27. struct terminal_type_impl;
  28. template<class T>
  29. struct terminal_type_impl<T, true>
  30. {
  31. typedef typename T::result_type type;
  32. };
  33. template<class T>
  34. struct terminal_type_impl<T, false>
  35. {
  36. typedef T type;
  37. };
  38. template<class T>
  39. struct terminal_type
  40. {
  41. typedef typename terminal_type_impl<T, has_result_type<T>::value>::type type;
  42. };
  43. } // end detail namespace
  44. // result_of placeholders
  45. template<class Args>
  46. struct result_of<expression<proto::terminal<placeholder<0> >::type>, Args, proto::tag::terminal>
  47. {
  48. typedef typename boost::tuples::element<0, Args>::type arg_type;
  49. typedef typename detail::terminal_type<arg_type>::type type;
  50. };
  51. template<class Args>
  52. struct result_of<expression<proto::terminal<placeholder<1> >::type>, Args, proto::tag::terminal>
  53. {
  54. typedef typename boost::tuples::element<1, Args>::type arg_type;
  55. typedef typename detail::terminal_type<arg_type>::type type;
  56. };
  57. template<class Args>
  58. struct result_of<expression<proto::terminal<placeholder<2> >::type>, Args, proto::tag::terminal>
  59. {
  60. typedef typename boost::tuples::element<2, Args>::type arg_type;
  61. typedef typename detail::terminal_type<arg_type>::type type;
  62. };
  63. } // end lambda namespace
  64. // lift lambda placeholders up to the boost::compute namespace
  65. using lambda::_1;
  66. using lambda::_2;
  67. using lambda::_3;
  68. } // end compute namespace
  69. } // end boost namespace
  70. #endif // BOOST_COMPUTE_LAMBDA_PLACEHOLDERS_HPP