1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 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_DETAIL_GET_OBJECT_INFO_HPP
12 #define BOOST_COMPUTE_DETAIL_GET_OBJECT_INFO_HPP
17 #include <boost/preprocessor/seq/for_each.hpp>
18 #include <boost/preprocessor/tuple/elem.hpp>
20 #include <boost/throw_exception.hpp>
22 #include <boost/compute/cl.hpp>
23 #include <boost/compute/exception/opencl_error.hpp>
29 template<class Function, class Object, class AuxInfo>
30 struct bound_info_function
32 bound_info_function(Function function, Object object, AuxInfo aux_info)
33 : m_function(function),
40 cl_int operator()(Info info, size_t size, void *value, size_t *size_ret) const
42 return m_function(m_object, m_aux_info, info, size, value, size_ret);
50 template<class Function, class Object>
51 struct bound_info_function<Function, Object, void>
53 bound_info_function(Function function, Object object)
54 : m_function(function),
60 cl_int operator()(Info info, size_t size, void *value, size_t *size_ret) const
62 return m_function(m_object, info, size, value, size_ret);
69 template<class Function, class Object>
70 inline bound_info_function<Function, Object, void>
71 bind_info_function(Function f, Object o)
73 return bound_info_function<Function, Object, void>(f, o);
76 template<class Function, class Object, class AuxInfo>
77 inline bound_info_function<Function, Object, AuxInfo>
78 bind_info_function(Function f, Object o, AuxInfo j)
80 return bound_info_function<Function, Object, AuxInfo>(f, o, j);
83 // default implementation
85 struct get_object_info_impl
87 template<class Function, class Info>
88 T operator()(Function function, Info info) const
92 cl_int ret = function(info, sizeof(T), &value, 0);
93 if(ret != CL_SUCCESS){
94 BOOST_THROW_EXCEPTION(opencl_error(ret));
101 // specialization for bool
103 struct get_object_info_impl<bool>
105 template<class Function, class Info>
106 bool operator()(Function function, Info info) const
110 cl_int ret = function(info, sizeof(cl_bool), &value, 0);
111 if(ret != CL_SUCCESS){
112 BOOST_THROW_EXCEPTION(opencl_error(ret));
115 return value == CL_TRUE;
119 // specialization for std::string
121 struct get_object_info_impl<std::string>
123 template<class Function, class Info>
124 std::string operator()(Function function, Info info) const
128 cl_int ret = function(info, 0, 0, &size);
129 if(ret != CL_SUCCESS){
130 BOOST_THROW_EXCEPTION(opencl_error(ret));
134 return std::string();
137 std::string value(size - 1, 0);
139 ret = function(info, size, &value[0], 0);
140 if(ret != CL_SUCCESS){
141 BOOST_THROW_EXCEPTION(opencl_error(ret));
148 // specialization for std::vector<T>
150 struct get_object_info_impl<std::vector<T> >
152 template<class Function, class Info>
153 std::vector<T> operator()(Function function, Info info) const
157 cl_int ret = function(info, 0, 0, &size);
158 if(ret != CL_SUCCESS){
159 BOOST_THROW_EXCEPTION(opencl_error(ret));
162 std::vector<T> vector(size / sizeof(T));
163 ret = function(info, size, &vector[0], 0);
164 if(ret != CL_SUCCESS){
165 BOOST_THROW_EXCEPTION(opencl_error(ret));
172 // returns the value (of type T) from the given clGet*Info() function call.
173 template<class T, class Function, class Object, class Info>
174 inline T get_object_info(Function f, Object o, Info i)
176 return get_object_info_impl<T>()(bind_info_function(f, o), i);
179 template<class T, class Function, class Object, class Info, class AuxInfo>
180 inline T get_object_info(Function f, Object o, Info i, AuxInfo j)
182 return get_object_info_impl<T>()(bind_info_function(f, o, j), i);
185 // returns the value type for the clGet*Info() call on Object with Enum.
186 template<class Object, int Enum>
187 struct get_object_info_type;
189 // defines the object::get_info<Enum>() specialization
190 #define BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATION(object_type, result_type, value) \
192 template<> struct get_object_info_type<object_type, value> { typedef result_type type; }; \
194 template<> inline result_type object_type::get_info<value>() const \
196 return get_info<result_type>(value); \
199 // used by BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS()
200 #define BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_IMPL(r, data, elem) \
201 BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATION( \
202 data, BOOST_PP_TUPLE_ELEM(2, 0, elem), BOOST_PP_TUPLE_ELEM(2, 1, elem) \
205 // defines the object::get_info<Enum>() specialization for each
206 // (result_type, value) tuple in seq for object_type.
207 #define BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(object_type, seq) \
208 BOOST_PP_SEQ_FOR_EACH( \
209 BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_IMPL, object_type, seq \
212 } // end detail namespace
213 } // end compute namespace
214 } // end boost namespace
216 #endif // BOOST_COMPUTE_DETAIL_GET_OBJECT_INFO_HPP