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
32 #include "core/error.hpp"
33 #include "core/property.hpp"
34 #include "api/dispatch.hpp"
35 #include "util/macros.h"
38 /// Main namespace of the CL state tracker.
42 /// Class that represents a CL API object.
44 template<typename T, typename S>
46 typedef T object_type;
47 typedef S descriptor_type;
49 descriptor() : dispatch(&_dispatch) {
50 static_assert(std::is_standard_layout<descriptor_type>::value,
51 "ICD requires CL API objects to be standard layout.");
54 const cl_icd_dispatch *dispatch;
58 struct allow_empty_tag;
60 struct property_list_tag;
63 template<typename T, typename D>
64 struct descriptor_traits {
65 typedef T object_type;
69 auto o = static_cast<typename D::object_type *>(d);
70 if (!o || o->dispatch != &_dispatch ||
71 !dynamic_cast<object_type *>(o))
72 throw invalid_object_error<T>();
76 validate_list(D * const *ds, size_t n) {
78 throw error(CL_INVALID_VALUE);
83 struct descriptor_traits<default_tag, D> {
84 typedef typename D::object_type object_type;
88 if (!d || d->dispatch != &_dispatch)
89 throw invalid_object_error<object_type>();
93 validate_list(D *const *ds, size_t n) {
95 throw error(CL_INVALID_VALUE);
100 struct descriptor_traits<allow_empty_tag, D> {
101 typedef typename D::object_type object_type;
105 if (!d || d->dispatch != &_dispatch)
106 throw invalid_object_error<object_type>();
110 validate_list(D *const *ds, size_t n) {
111 if (bool(ds) != bool(n))
112 throw error(CL_INVALID_VALUE);
117 struct descriptor_traits<wait_list_tag, D> {
118 typedef typename D::object_type object_type;
122 if (!d || d->dispatch != &_dispatch)
123 throw invalid_wait_list_error();
127 validate_list(D *const *ds, size_t n) {
128 if (bool(ds) != bool(n))
129 throw invalid_wait_list_error();
135 /// Get a Clover object from an API object performing object
138 /// \a T can either be the Clover object type to return or a \a tag
139 /// object to select some special validation behavior by means of a
140 /// specialization of the detail::descriptor_traits template. The
141 /// default behavior is to infer the most general Clover object
142 /// type for the given API object.
144 template<typename T = default_tag, typename D>
145 typename detail::descriptor_traits<T, D>::object_type &
147 detail::descriptor_traits<T, D>::validate(d);
150 typename detail::descriptor_traits<T, D>::object_type &>(*d);
154 /// Get a pointer to a Clover object from an API object performing
155 /// object validation. Returns \c NULL if its argument is \c NULL.
159 template<typename T = default_tag, typename D>
160 typename detail::descriptor_traits<T, D>::object_type *
163 detail::descriptor_traits<T, D>::validate(d);
166 typename detail::descriptor_traits<T, D>::object_type *>(d);
170 /// Get an API object from a Clover object.
173 typename O::descriptor_type *
175 return static_cast<typename O::descriptor_type *>(&o);
179 /// Get an API object from a pointer to a Clover object.
182 typename O::descriptor_type *
184 return static_cast<typename O::descriptor_type *>(o);
188 /// Get a range of Clover objects from a range of API objects
189 /// performing object validation.
193 template<typename T = default_tag, typename D>
194 ref_vector<typename detail::descriptor_traits<T, D>::object_type>
195 objs(D *const *ds, size_t n) {
196 detail::descriptor_traits<T, D>::validate_list(ds, n);
197 return map(obj<T, D>, range(ds, n));
201 /// Get a range of API objects from a range of Clover objects.
203 template<typename Os>
204 std::vector<typename Os::value_type::descriptor_type *>
205 descs(const Os &os) {
206 return map([](typename Os::value_type &o) {
213 public clover::descriptor<clover::context, _cl_context> {};
215 struct _cl_device_id :
216 public clover::descriptor<clover::device, _cl_device_id> {};
219 public clover::descriptor<clover::event, _cl_event> {};
222 public clover::descriptor<clover::kernel, _cl_kernel> {};
225 public clover::descriptor<clover::memory_obj, _cl_mem> {};
227 struct _cl_platform_id :
228 public clover::descriptor<clover::platform, _cl_platform_id> {};
231 public clover::descriptor<clover::program, _cl_program> {};
233 struct _cl_command_queue :
234 public clover::descriptor<clover::command_queue, _cl_command_queue> {};
237 public clover::descriptor<clover::sampler, _cl_sampler> {};