get.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_GET_HPP
  11. #define BOOST_COMPUTE_FUNCTIONAL_GET_HPP
  12. #include <cstddef>
  13. #include <boost/compute/types/fundamental.hpp>
  14. #include <boost/compute/type_traits/scalar_type.hpp>
  15. namespace boost {
  16. namespace compute {
  17. namespace detail {
  18. // meta-function returning the result type for get<N>()
  19. template<size_t N, class Arg>
  20. struct get_result_type
  21. {
  22. typedef typename scalar_type<Arg>::type type;
  23. };
  24. template<size_t N, class Arg, class T>
  25. struct invoked_get
  26. {
  27. typedef typename get_result_type<N, T>::type result_type;
  28. invoked_get(const Arg &arg)
  29. : m_arg(arg)
  30. {
  31. }
  32. Arg m_arg;
  33. };
  34. } // end detail namespace
  35. /// Returns the \c N'th element of an aggregate type (e.g. scalarN,
  36. /// pair, tuple, etc.).
  37. ///
  38. /// \see \ref field "field<T>"
  39. template<size_t N>
  40. struct get
  41. {
  42. /// \internal_
  43. template<class> struct result;
  44. /// \internal_
  45. template<class F, class Arg>
  46. struct result<F(Arg)>
  47. {
  48. typedef typename detail::get_result_type<N, Arg>::type type;
  49. };
  50. template<class Arg>
  51. detail::invoked_get<
  52. N, Arg, typename boost::remove_cv<typename Arg::result_type>::type
  53. > operator()(const Arg &arg) const
  54. {
  55. typedef typename boost::remove_cv<typename Arg::result_type>::type T;
  56. return detail::invoked_get<N, Arg, T>(arg);
  57. }
  58. };
  59. } // end compute namespace
  60. } // end boost namespace
  61. #endif // BOOST_COMPUTE_FUNCTIONAL_GET_HPP