1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_TYPES_PAIR_HPP
12 #define BOOST_COMPUTE_TYPES_PAIR_HPP
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>
26 // meta_kernel operator for std::pair literals
27 template<class T1, class T2>
29 operator<<(meta_kernel &kernel, const std::pair<T1, T2> &x)
31 kernel << "(" << type_name<std::pair<T1, T2> >() << ")"
32 << "{" << kernel.make_lit(x.first) << ", "
33 << kernel.make_lit(x.second) << "}";
38 // inject_type() specialization for std::pair
39 template<class T1, class T2>
40 struct inject_type_impl<std::pair<T1, T2> >
42 void operator()(meta_kernel &kernel)
44 typedef std::pair<T1, T2> pair_type;
46 kernel.inject_type<T1>();
47 kernel.inject_type<T2>();
49 kernel.add_type_declaration<pair_type>(type_definition<pair_type>());
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> >
60 template<class T1, class T2>
61 struct get_result_type<1, std::pair<T1, T2> >
66 // get<N>() specialization for std::pair<>
67 template<size_t N, class Arg, class T1, class T2>
68 inline meta_kernel& operator<<(meta_kernel &kernel,
69 const invoked_get<N, Arg, std::pair<T1, T2> > &expr)
71 kernel.inject_type<std::pair<T1, T2> >();
73 return kernel << expr.m_arg << (N == 0 ? ".first" : ".second");
76 } // end detail namespace
80 // type_name() specialization for std::pair
81 template<class T1, class T2>
82 struct type_name_trait<std::pair<T1, T2> >
84 static const char* value()
86 static std::string name =
87 std::string("_pair_") +
88 type_name<T1>() + "_" + type_name<T2>() +
95 // type_definition() specialization for std::pair
96 template<class T1, class T2>
97 struct type_definition_trait<std::pair<T1, T2> >
99 static std::string value()
101 typedef std::pair<T1, T2> pair_type;
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";
109 return declaration.str();
113 } // end detail namespace
114 } // end compute namespace
115 } // end boost namespace
117 #endif // BOOST_COMPUTE_TYPES_PAIR_HPP