2 // Copyright 2012 Francisco Jerez
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef CLOVER_API_UTIL_HPP
24 #define CLOVER_API_UTIL_HPP
31 #include "core/base.hpp"
32 #include "pipe/p_compiler.h"
36 /// Return a matrix (a container of containers) in \a buf with
37 /// argument and bounds checking. Intended to be used by
38 /// implementations of \a clGetXXXInfo().
40 template<typename T, typename V>
42 matrix_property(void *buf, size_t size, size_t *size_ret, const V& v) {
43 if (buf && size < sizeof(T *) * v.size())
44 return CL_INVALID_VALUE;
47 *size_ret = sizeof(T *) * v.size();
50 for_each([](typename V::value_type src, T *dst) {
52 std::copy(src.begin(), src.end(), dst);
54 v.begin(), v.end(), (T **)buf);
60 /// Return a vector in \a buf with argument and bounds checking.
61 /// Intended to be used by implementations of \a clGetXXXInfo().
63 template<typename T, typename V>
65 vector_property(void *buf, size_t size, size_t *size_ret, const V& v) {
66 if (buf && size < sizeof(T) * v.size())
67 return CL_INVALID_VALUE;
70 *size_ret = sizeof(T) * v.size();
72 std::copy(v.begin(), v.end(), (T *)buf);
78 /// Return a scalar in \a buf with argument and bounds checking.
79 /// Intended to be used by implementations of \a clGetXXXInfo().
83 scalar_property(void *buf, size_t size, size_t *size_ret, T v) {
84 return vector_property<T>(buf, size, size_ret, std::vector<T>(1, v));
88 /// Return a string in \a buf with argument and bounds checking.
89 /// Intended to be used by implementations of \a clGetXXXInfo().
92 string_property(void *buf, size_t size, size_t *size_ret,
93 const std::string &v) {
94 if (buf && size < v.size() + 1)
95 return CL_INVALID_VALUE;
98 *size_ret = v.size() + 1;
100 std::strcpy((char *)buf, v.c_str());
106 /// Convert a NULL-terminated property list into an std::map.
110 property_map(const T *props) {
113 while (props && *props) {
118 throw clover::error(CL_INVALID_PROPERTY);
120 m.insert({ key, value });
127 /// Convert an std::map into a NULL-terminated property list.
131 property_vector(const std::map<T, T> &m) {
135 v.push_back(p.first);
136 v.push_back(p.second);
144 /// Return an error code in \a p if non-zero.
147 ret_error(cl_int *p, const clover::error &e) {
153 /// Return a reference-counted object in \a p if non-zero.
154 /// Otherwise release object ownership.
156 template<typename T, typename S>
158 ret_object(T p, S v) {