unpack.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_FUNCTIONAL_DETAIL_UNPACK_HPP
  11. #define BOOST_COMPUTE_FUNCTIONAL_DETAIL_UNPACK_HPP
  12. #include <boost/compute/functional/get.hpp>
  13. #include <boost/compute/type_traits/is_vector_type.hpp>
  14. #include <boost/compute/type_traits/result_of.hpp>
  15. #include <boost/compute/type_traits/vector_size.hpp>
  16. #include <boost/compute/detail/meta_kernel.hpp>
  17. namespace boost {
  18. namespace compute {
  19. namespace detail {
  20. template<class Function, class Arg, size_t Arity>
  21. struct invoked_unpacked
  22. {
  23. invoked_unpacked(const Function &f, const Arg &arg)
  24. : m_function(f),
  25. m_arg(arg)
  26. {
  27. }
  28. Function m_function;
  29. Arg m_arg;
  30. };
  31. template<class Function, class Arg, size_t Arity>
  32. inline meta_kernel& operator<<(meta_kernel &k, const invoked_unpacked<Function, Arg, Arity> &expr);
  33. template<class Function, class Arg>
  34. inline meta_kernel& operator<<(meta_kernel &k, const invoked_unpacked<Function, Arg, 1> &expr)
  35. {
  36. return k << expr.m_function(get<0>()(expr.m_arg));
  37. }
  38. template<class Function, class Arg>
  39. inline meta_kernel& operator<<(meta_kernel &k, const invoked_unpacked<Function, Arg, 2> &expr)
  40. {
  41. return k << expr.m_function(get<0>()(expr.m_arg), get<1>()(expr.m_arg));
  42. }
  43. template<class Function, class Arg>
  44. inline meta_kernel& operator<<(meta_kernel &k, const invoked_unpacked<Function, Arg, 3> &expr)
  45. {
  46. return k << expr.m_function(get<0>()(expr.m_arg), get<1>()(expr.m_arg), get<2>()(expr.m_arg));
  47. }
  48. template<class Function>
  49. struct unpacked
  50. {
  51. template<class T, class Enable = void>
  52. struct aggregate_length
  53. {
  54. BOOST_STATIC_CONSTANT(size_t, value = boost::tuples::length<T>::value);
  55. };
  56. template<class T>
  57. struct aggregate_length<T, typename enable_if<is_vector_type<T> >::type>
  58. {
  59. BOOST_STATIC_CONSTANT(size_t, value = vector_size<T>::value);
  60. };
  61. template<class TupleArg, size_t TupleSize>
  62. struct result_impl {};
  63. template<class TupleArg>
  64. struct result_impl<TupleArg, 1>
  65. {
  66. typedef typename detail::get_result_type<0, TupleArg>::type T1;
  67. typedef typename boost::compute::result_of<Function(T1)>::type type;
  68. };
  69. template<class TupleArg>
  70. struct result_impl<TupleArg, 2>
  71. {
  72. typedef typename detail::get_result_type<0, TupleArg>::type T1;
  73. typedef typename detail::get_result_type<1, TupleArg>::type T2;
  74. typedef typename boost::compute::result_of<Function(T1, T2)>::type type;
  75. };
  76. template<class TupleArg>
  77. struct result_impl<TupleArg, 3>
  78. {
  79. typedef typename detail::get_result_type<0, TupleArg>::type T1;
  80. typedef typename detail::get_result_type<1, TupleArg>::type T2;
  81. typedef typename detail::get_result_type<2, TupleArg>::type T3;
  82. typedef typename boost::compute::result_of<Function(T1, T2, T3)>::type type;
  83. };
  84. template<class Signature>
  85. struct result {};
  86. template<class This, class Arg>
  87. struct result<This(Arg)>
  88. {
  89. typedef typename result_impl<Arg, aggregate_length<Arg>::value>::type type;
  90. };
  91. unpacked(const Function &f)
  92. : m_function(f)
  93. {
  94. }
  95. template<class Arg>
  96. detail::invoked_unpacked<
  97. Function, Arg, aggregate_length<typename Arg::result_type>::value
  98. >
  99. operator()(const Arg &arg) const
  100. {
  101. return detail::invoked_unpacked<
  102. Function,
  103. Arg,
  104. aggregate_length<typename Arg::result_type>::value
  105. >(m_function, arg);
  106. }
  107. Function m_function;
  108. };
  109. template<class Function>
  110. inline unpacked<Function> unpack(const Function &f)
  111. {
  112. return unpacked<Function>(f);
  113. }
  114. } // end detail namespace
  115. } // end compute namespace
  116. } // end boost namespace
  117. #endif // BOOST_COMPUTE_FUNCTIONAL_DETAIL_UNPACK_HPP