clover: Define helper classes for the new object model.
[mesa.git] / src / gallium / state_trackers / clover / core / object.hpp
1 //
2 // Copyright 2013 Francisco Jerez
3 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
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.
21 //
22
23 #ifndef CLOVER_CORE_OBJECT_HPP
24 #define CLOVER_CORE_OBJECT_HPP
25
26 #include <cassert>
27 #include <functional>
28 #include <vector>
29
30 #include "CL/cl.h"
31
32 #include "core/error.hpp"
33 #include "core/property.hpp"
34
35 ///
36 /// Main namespace of the CL state tracker.
37 ///
38 namespace clover {
39 ///
40 /// Class that represents a CL API object.
41 ///
42 template<typename T, typename S>
43 struct descriptor {
44 typedef T object_type;
45 typedef S descriptor_type;
46 };
47
48 struct default_tag;
49 struct wait_list_tag;
50 struct property_list_tag;
51
52 namespace detail {
53 template<typename T, typename D>
54 struct descriptor_traits {
55 typedef T object_type;
56
57 static void
58 validate(D *d) {
59 auto o = static_cast<typename D::object_type *>(d);
60 if (!o || !dynamic_cast<object_type *>(o))
61 throw invalid_object_error<T>();
62 }
63
64 static void
65 validate_list(D * const *ds, size_t n) {
66 if (!ds || !n)
67 throw error(CL_INVALID_VALUE);
68 }
69 };
70
71 template<typename D>
72 struct descriptor_traits<default_tag, D> {
73 typedef typename D::object_type object_type;
74
75 static void
76 validate(D *d) {
77 if (!d)
78 throw invalid_object_error<object_type>();
79 }
80
81 static void
82 validate_list(D *const *ds, size_t n) {
83 if (!ds || !n)
84 throw error(CL_INVALID_VALUE);
85 }
86 };
87
88 template<typename D>
89 struct descriptor_traits<wait_list_tag, D> {
90 typedef typename D::object_type object_type;
91
92 static void
93 validate(D *d) {
94 if (!d)
95 throw invalid_wait_list_error();
96 }
97
98 static void
99 validate_list(D *const *ds, size_t n) {
100 if (bool(ds) != bool(n))
101 throw invalid_wait_list_error();
102 }
103 };
104 }
105
106 ///
107 /// Get a Clover object from an API object.
108 ///
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.
114 ///
115 template<typename T = default_tag, typename D>
116 typename detail::descriptor_traits<T, D>::object_type &
117 obj(D *d) {
118 detail::descriptor_traits<T, D>::validate(d);
119
120 return static_cast<
121 typename detail::descriptor_traits<T, D>::object_type &>(*d);
122 }
123
124 ///
125 /// Get a pointer to a Clover object from an API object. Returns
126 /// \c NULL if its argument is \c NULL.
127 ///
128 /// \sa obj
129 ///
130 template<typename T = default_tag, typename D>
131 typename detail::descriptor_traits<T, D>::object_type *
132 pobj(D *d) {
133 if (d)
134 detail::descriptor_traits<T, D>::validate(d);
135
136 return static_cast<
137 typename detail::descriptor_traits<T, D>::object_type *>(d);
138 }
139
140 ///
141 /// Get an API object from a Clover object.
142 ///
143 template<typename O>
144 typename O::descriptor_type *
145 desc(O &o) {
146 return static_cast<typename O::descriptor_type *>(&o);
147 }
148
149 ///
150 /// Get an API object from a pointer to a Clover object.
151 ///
152 template<typename O>
153 typename O::descriptor_type *
154 desc(O *o) {
155 return static_cast<typename O::descriptor_type *>(o);
156 }
157
158 ///
159 /// Get a range of Clover objects from a range of API objects.
160 ///
161 /// \sa obj
162 ///
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));
168 }
169
170 ///
171 /// Get a range of API objects from a range of Clover objects.
172 ///
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) {
177 return desc(o);
178 }, os);
179 }
180 }
181
182 #endif