pair.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_TYPES_PAIR_HPP
  11. #define BOOST_COMPUTE_TYPES_PAIR_HPP
  12. #include <string>
  13. #include <utility>
  14. #include <boost/compute/functional/get.hpp>
  15. #include <boost/compute/type_traits/type_definition.hpp>
  16. #include <boost/compute/type_traits/type_name.hpp>
  17. #include <boost/compute/detail/meta_kernel.hpp>
  18. namespace boost {
  19. namespace compute {
  20. namespace detail {
  21. // meta_kernel operator for std::pair literals
  22. template<class T1, class T2>
  23. inline meta_kernel&
  24. operator<<(meta_kernel &kernel, const std::pair<T1, T2> &x)
  25. {
  26. kernel << "(" << type_name<std::pair<T1, T2> >() << ")"
  27. << "{" << kernel.make_lit(x.first) << ", "
  28. << kernel.make_lit(x.second) << "}";
  29. return kernel;
  30. }
  31. // inject_type() specialization for std::pair
  32. template<class T1, class T2>
  33. struct inject_type_impl<std::pair<T1, T2> >
  34. {
  35. void operator()(meta_kernel &kernel)
  36. {
  37. typedef std::pair<T1, T2> pair_type;
  38. kernel.inject_type<T1>();
  39. kernel.inject_type<T2>();
  40. kernel.add_type_declaration<pair_type>(type_definition<pair_type>());
  41. }
  42. };
  43. // get<N>() result type specialization for std::pair<>
  44. template<class T1, class T2>
  45. struct get_result_type<0, std::pair<T1, T2> >
  46. {
  47. typedef T1 type;
  48. };
  49. template<class T1, class T2>
  50. struct get_result_type<1, std::pair<T1, T2> >
  51. {
  52. typedef T2 type;
  53. };
  54. // get<N>() specialization for std::pair<>
  55. template<size_t N, class Arg, class T1, class T2>
  56. inline meta_kernel& operator<<(meta_kernel &kernel,
  57. const invoked_get<N, Arg, std::pair<T1, T2> > &expr)
  58. {
  59. kernel.inject_type<std::pair<T1, T2> >();
  60. return kernel << expr.m_arg << (N == 0 ? ".first" : ".second");
  61. }
  62. } // end detail namespace
  63. namespace detail {
  64. // type_name() specialization for std::pair
  65. template<class T1, class T2>
  66. struct type_name_trait<std::pair<T1, T2> >
  67. {
  68. static const char* value()
  69. {
  70. static std::string name =
  71. std::string("_pair_") +
  72. type_name<T1>() + "_" + type_name<T2>() +
  73. "_t";
  74. return name.c_str();
  75. }
  76. };
  77. // type_definition() specialization for std::pair
  78. template<class T1, class T2>
  79. struct type_definition_trait<std::pair<T1, T2> >
  80. {
  81. static std::string value()
  82. {
  83. typedef std::pair<T1, T2> pair_type;
  84. std::stringstream declaration;
  85. declaration << "typedef struct {\n"
  86. << " " << type_name<T1>() << " first;\n"
  87. << " " << type_name<T2>() << " second;\n"
  88. << "} " << type_name<pair_type>() << ";\n";
  89. return declaration.str();
  90. }
  91. };
  92. } // end detail namespace
  93. } // end compute namespace
  94. } // end boost namespace
  95. #endif // BOOST_COMPUTE_TYPES_PAIR_HPP