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_TUPLE_HPP
12 #define BOOST_COMPUTE_TYPES_TUPLE_HPP
17 #include <boost/preprocessor/enum.hpp>
18 #include <boost/preprocessor/expr_if.hpp>
19 #include <boost/preprocessor/repetition.hpp>
20 #include <boost/tuple/tuple.hpp>
22 #include <boost/compute/config.hpp>
23 #include <boost/compute/functional/get.hpp>
24 #include <boost/compute/type_traits/type_name.hpp>
25 #include <boost/compute/detail/meta_kernel.hpp>
27 #ifndef BOOST_COMPUTE_NO_STD_TUPLE
35 // meta_kernel operators for boost::tuple literals
36 #define BOOST_COMPUTE_PRINT_ELEM(z, n, unused) \
37 BOOST_PP_EXPR_IF(n, << ", ") \
38 << kernel.make_lit(boost::get<n>(x))
40 #define BOOST_COMPUTE_PRINT_TUPLE(z, n, unused) \
41 template<BOOST_PP_ENUM_PARAMS(n, class T)> \
43 operator<<(meta_kernel &kernel, \
44 const boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> &x) \
48 << type_name<boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> >() \
51 BOOST_PP_REPEAT(n, BOOST_COMPUTE_PRINT_ELEM, ~) \
55 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_PRINT_TUPLE, ~)
57 #undef BOOST_COMPUTE_PRINT_TUPLE
58 #undef BOOST_COMPUTE_PRINT_ELEM
60 // inject_type() specializations for boost::tuple
61 #define BOOST_COMPUTE_INJECT_TYPE(z, n, unused) \
62 kernel.inject_type<T ## n>();
64 #define BOOST_COMPUTE_INJECT_DECL(z, n, unused) \
65 << " " << type_name<T ## n>() << " v" #n ";\n"
67 #define BOOST_COMPUTE_INJECT_IMPL(z, n, unused) \
68 template<BOOST_PP_ENUM_PARAMS(n, class T)> \
69 struct inject_type_impl<boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> > \
71 void operator()(meta_kernel &kernel) \
73 typedef boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> tuple_type; \
74 BOOST_PP_REPEAT(n, BOOST_COMPUTE_INJECT_TYPE, ~) \
75 std::stringstream declaration; \
76 declaration << "typedef struct {\n" \
77 BOOST_PP_REPEAT(n, BOOST_COMPUTE_INJECT_DECL, ~) \
78 << "} " << type_name<tuple_type>() << ";\n"; \
79 kernel.add_type_declaration<tuple_type>(declaration.str()); \
83 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_INJECT_IMPL, ~)
85 #undef BOOST_COMPUTE_INJECT_IMPL
86 #undef BOOST_COMPUTE_INJECT_DECL
87 #undef BOOST_COMPUTE_INJECT_TYPE
89 #ifdef BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
90 // type_name() specializations for boost::tuple (without variadic templates)
91 #define BOOST_COMPUTE_PRINT_TYPE(z, n, unused) \
92 + type_name<T ## n>() + "_"
94 #define BOOST_COMPUTE_PRINT_TYPE_NAME(z, n, unused) \
95 template<BOOST_PP_ENUM_PARAMS(n, class T)> \
96 struct type_name_trait<boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> > \
98 static const char* value() \
100 static std::string name = \
101 std::string("boost_tuple_") \
102 BOOST_PP_REPEAT(n, BOOST_COMPUTE_PRINT_TYPE, ~) \
104 return name.c_str(); \
108 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_PRINT_TYPE_NAME, ~)
110 #undef BOOST_COMPUTE_PRINT_TYPE_NAME
111 #undef BOOST_COMPUTE_PRINT_TYPE
114 template<size_t N, class T, class... Rest>
115 struct write_tuple_type_names
117 void operator()(std::ostream &os)
119 os << type_name<T>() << "_";
120 write_tuple_type_names<N-1, Rest...>()(os);
124 template<class T, class... Rest>
125 struct write_tuple_type_names<1, T, Rest...>
127 void operator()(std::ostream &os)
129 os << type_name<T>();
133 // type_name<> specialization for boost::tuple<...> (with variadic templates)
135 struct type_name_trait<boost::tuple<T...>>
137 static const char* value()
139 static std::string str = make_type_name();
144 static std::string make_type_name()
146 typedef typename boost::tuple<T...> tuple_type;
150 write_tuple_type_names<
151 boost::tuples::length<tuple_type>::value, T...
157 #endif // BOOST_COMPUTE_NO_VARIADIC_TEMPLATES
159 #ifndef BOOST_COMPUTE_NO_STD_TUPLE
160 // type_name<> specialization for std::tuple<T...>
162 struct type_name_trait<std::tuple<T...>>
164 static const char* value()
166 static std::string str = make_type_name();
171 static std::string make_type_name()
173 typedef typename std::tuple<T...> tuple_type;
177 write_tuple_type_names<
178 std::tuple_size<tuple_type>::value, T...
184 #endif // BOOST_COMPUTE_NO_STD_TUPLE
186 // get<N>() result type specialization for boost::tuple<>
187 #define BOOST_COMPUTE_GET_RESULT_TYPE(z, n, unused) \
188 template<size_t N, BOOST_PP_ENUM_PARAMS(n, class T)> \
189 struct get_result_type<N, boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> > \
191 typedef typename boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> T; \
192 typedef typename boost::tuples::element<N, T>::type type; \
195 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_GET_RESULT_TYPE, ~)
197 #undef BOOST_COMPUTE_GET_RESULT_TYPE
200 // get<N>() specialization for boost::tuple<>
201 #define BOOST_COMPUTE_GET_N(z, n, unused) \
202 template<size_t N, class Arg, BOOST_PP_ENUM_PARAMS(n, class T)> \
203 inline meta_kernel& operator<<(meta_kernel &kernel, \
204 const invoked_get<N, Arg, boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> > &expr) \
206 typedef typename boost::tuple<BOOST_PP_ENUM_PARAMS(n, T)> T; \
207 BOOST_STATIC_ASSERT(N < size_t(boost::tuples::length<T>::value)); \
208 kernel.inject_type<T>(); \
209 return kernel << expr.m_arg << ".v" << uint_(N); \
212 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_GET_N, ~)
214 #undef BOOST_COMPUTE_GET_N
216 } // end detail namespace
217 } // end compute namespace
218 } // end boost namespace
220 #endif // BOOST_COMPUTE_TYPES_TUPLE_HPP