2 // Copyright 2013 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_CORE_OBJECT_HPP
24 #define CLOVER_CORE_OBJECT_HPP
30 #define CL_TARGET_OPENCL_VERSION 220
33 #include "core/error.hpp"
34 #include "core/property.hpp"
35 #include "api/dispatch.hpp"
36 #include "util/macros.h"
39 /// Main namespace of the CL state tracker.
43 /// Class that represents a CL API object.
45 template<typename T, typename S>
47 typedef T object_type;
48 typedef S descriptor_type;
50 descriptor() : dispatch(&_dispatch) {
51 static_assert(std::is_standard_layout<descriptor_type>::value,
52 "ICD requires CL API objects to be standard layout.");
55 const cl_icd_dispatch *dispatch;
59 struct allow_empty_tag;
61 struct property_list_tag;
64 template<typename T, typename D>
65 struct descriptor_traits {
66 typedef T object_type;
70 auto o = static_cast<typename D::object_type *>(d);
71 if (!o || o->dispatch != &_dispatch ||
72 !dynamic_cast<object_type *>(o))
73 throw invalid_object_error<T>();
77 validate_list(D * const *ds, size_t n) {
79 throw error(CL_INVALID_VALUE);
84 struct descriptor_traits<default_tag, D> {
85 typedef typename D::object_type object_type;
89 if (!d || d->dispatch != &_dispatch)
90 throw invalid_object_error<object_type>();
94 validate_list(D *const *ds, size_t n) {
96 throw error(CL_INVALID_VALUE);
101 struct descriptor_traits<allow_empty_tag, D> {
102 typedef typename D::object_type object_type;
106 if (!d || d->dispatch != &_dispatch)
107 throw invalid_object_error<object_type>();
111 validate_list(D *const *ds, size_t n) {
112 if (bool(ds) != bool(n))
113 throw error(CL_INVALID_VALUE);
118 struct descriptor_traits<wait_list_tag, D> {
119 typedef typename D::object_type object_type;
123 if (!d || d->dispatch != &_dispatch)
124 throw invalid_wait_list_error();
128 validate_list(D *const *ds, size_t n) {
129 if (bool(ds) != bool(n))
130 throw invalid_wait_list_error();
136 /// Get a Clover object from an API object performing object
139 /// \a T can either be the Clover object type to return or a \a tag
140 /// object to select some special validation behavior by means of a
141 /// specialization of the detail::descriptor_traits template. The
142 /// default behavior is to infer the most general Clover object
143 /// type for the given API object.
145 template<typename T = default_tag, typename D>
146 typename detail::descriptor_traits<T, D>::object_type &
148 detail::descriptor_traits<T, D>::validate(d);
151 typename detail::descriptor_traits<T, D>::object_type &>(*d);
155 /// Get a pointer to a Clover object from an API object performing
156 /// object validation. Returns \c NULL if its argument is \c NULL.
160 template<typename T = default_tag, typename D>
161 typename detail::descriptor_traits<T, D>::object_type *
164 detail::descriptor_traits<T, D>::validate(d);
167 typename detail::descriptor_traits<T, D>::object_type *>(d);
171 /// Get an API object from a Clover object.
174 typename O::descriptor_type *
176 return static_cast<typename O::descriptor_type *>(&o);
180 /// Get an API object from a pointer to a Clover object.
183 typename O::descriptor_type *
185 return static_cast<typename O::descriptor_type *>(o);
189 /// Get a range of Clover objects from a range of API objects
190 /// performing object validation.
194 template<typename T = default_tag, typename D>
195 ref_vector<typename detail::descriptor_traits<T, D>::object_type>
196 objs(D *const *ds, size_t n) {
197 detail::descriptor_traits<T, D>::validate_list(ds, n);
198 return map(obj<T, D>, range(ds, n));
202 /// Get a range of API objects from a range of Clover objects.
204 template<typename Os>
205 std::vector<typename Os::value_type::descriptor_type *>
206 descs(const Os &os) {
207 return map([](typename Os::value_type &o) {
214 public clover::descriptor<clover::context, _cl_context> {};
216 struct _cl_device_id :
217 public clover::descriptor<clover::device, _cl_device_id> {};
220 public clover::descriptor<clover::event, _cl_event> {};
223 public clover::descriptor<clover::kernel, _cl_kernel> {};
226 public clover::descriptor<clover::memory_obj, _cl_mem> {};
228 struct _cl_platform_id :
229 public clover::descriptor<clover::platform, _cl_platform_id> {};
232 public clover::descriptor<clover::program, _cl_program> {};
234 struct _cl_command_queue :
235 public clover::descriptor<clover::command_queue, _cl_command_queue> {};
238 public clover::descriptor<clover::sampler, _cl_sampler> {};