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