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"
36 /// Main namespace of the CL state tracker.
40 /// Class that represents a CL API object.
42 template<typename T, typename S>
44 typedef T object_type;
45 typedef S descriptor_type;
50 struct property_list_tag;
53 template<typename T, typename D>
54 struct descriptor_traits {
55 typedef T object_type;
59 auto o = static_cast<typename D::object_type *>(d);
60 if (!o || !dynamic_cast<object_type *>(o))
61 throw invalid_object_error<T>();
65 validate_list(D * const *ds, size_t n) {
67 throw error(CL_INVALID_VALUE);
72 struct descriptor_traits<default_tag, D> {
73 typedef typename D::object_type object_type;
78 throw invalid_object_error<object_type>();
82 validate_list(D *const *ds, size_t n) {
84 throw error(CL_INVALID_VALUE);
89 struct descriptor_traits<wait_list_tag, D> {
90 typedef typename D::object_type object_type;
95 throw invalid_wait_list_error();
99 validate_list(D *const *ds, size_t n) {
100 if (bool(ds) != bool(n))
101 throw invalid_wait_list_error();
107 /// Get a Clover object from an API object.
109 /// \a T can either be the Clover object type to return or a \a tag
110 /// object to select some special validation behavior by means of a
111 /// specialization of the detail::descriptor_traits template. The
112 /// default behavior is to infer the most general Clover object
113 /// type for the given API object.
115 template<typename T = default_tag, typename D>
116 typename detail::descriptor_traits<T, D>::object_type &
118 detail::descriptor_traits<T, D>::validate(d);
121 typename detail::descriptor_traits<T, D>::object_type &>(*d);
125 /// Get a pointer to a Clover object from an API object. Returns
126 /// \c NULL if its argument is \c NULL.
130 template<typename T = default_tag, typename D>
131 typename detail::descriptor_traits<T, D>::object_type *
134 detail::descriptor_traits<T, D>::validate(d);
137 typename detail::descriptor_traits<T, D>::object_type *>(d);
141 /// Get an API object from a Clover object.
144 typename O::descriptor_type *
146 return static_cast<typename O::descriptor_type *>(&o);
150 /// Get an API object from a pointer to a Clover object.
153 typename O::descriptor_type *
155 return static_cast<typename O::descriptor_type *>(o);
159 /// Get a range of Clover objects from a range of API objects.
163 template<typename T = default_tag, typename D>
164 ref_vector<typename detail::descriptor_traits<T, D>::object_type>
165 objs(D *const *ds, size_t n) {
166 detail::descriptor_traits<T, D>::validate_list(ds, n);
167 return map(obj<T, D>, range(ds, n));
171 /// Get a range of API objects from a range of Clover objects.
173 template<typename Os>
174 std::vector<typename Os::value_type::descriptor_type *>
175 descs(const Os &os) {
176 return map([](typename Os::value_type &o) {
183 public clover::descriptor<clover::context, _cl_context> {};
185 struct _cl_device_id :
186 public clover::descriptor<clover::device, _cl_device_id> {};
188 struct _cl_platform_id :
189 public clover::descriptor<clover::platform, _cl_platform_id> {};