OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef GPU_NP_UTILS_NP_DISPATCHER_H_ | |
6 #define GPU_NP_UTILS_NP_DISPATCHER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "gpu/np_utils/np_utils.h" | |
11 #include "gpu/np_utils/np_headers.h" | |
12 | |
13 // Dispatchers make regular member functions available as NPObject methods. | |
14 // Usage: | |
15 // | |
16 // class MyNPObject : public DefaultNPObject<NPObject> { | |
17 // public: | |
18 // int MyMethod(bool a, float b); | |
19 // NP_UTILS_BEGIN_DISPATCHER_CHAIN(MyNPObject, DispatchedNPObject) | |
20 // NP_UTILS_DISPATCHER(MyMethod, int(bool, float)) | |
21 // NP_UTILS_END_DISPATCHER_CHAIN | |
22 // }; | |
23 // | |
24 // Multiple member functions may be listed in the dispatcher chain. Inheritance | |
25 // is supported. The following types are supported as return types and parameter | |
26 // types: | |
27 // * bool | |
28 // * int | |
29 // * float | |
30 // * double | |
31 // * std::string | |
32 // * NPObject* | |
33 // | |
34 | |
35 // These macros are used to make dispatcher chains. | |
36 #define NP_UTILS_NP_UTILS_DISPATCHER_JOIN2(a, b) a ## b | |
37 #define NP_UTILS_DISPATCHER_JOIN(a, b) NP_UTILS_NP_UTILS_DISPATCHER_JOIN2(a, b) | |
38 #define NP_UTILS_DISPATCHER_UNIQUE \ | |
39 NP_UTILS_DISPATCHER_JOIN(dispatcher, __LINE__) | |
40 | |
41 #define NP_UTILS_BEGIN_DISPATCHER_CHAIN(Class, BaseClass) \ | |
42 static ::np_utils::BaseNPDispatcher* GetDispatcherChain() { \ | |
43 typedef Class ThisClass; \ | |
44 ::np_utils::BaseNPDispatcher* top_dispatcher = \ | |
45 BaseClass::GetDispatcherChain(); \ | |
46 | |
47 #define NP_UTILS_DISPATCHER(name, Signature) \ | |
48 static ::np_utils::NPDispatcher<ThisClass, Signature> \ | |
49 NP_UTILS_DISPATCHER_UNIQUE( \ | |
50 top_dispatcher, \ | |
51 #name, \ | |
52 &ThisClass::name); \ | |
53 top_dispatcher = &NP_UTILS_DISPATCHER_UNIQUE; \ | |
54 | |
55 #define NP_UTILS_END_DISPATCHER_CHAIN \ | |
56 return top_dispatcher; \ | |
57 } \ | |
58 bool HasMethod(NPIdentifier name) { \ | |
59 return ::np_utils::DispatcherHasMethodHelper( \ | |
60 GetDispatcherChain(), this, name); \ | |
61 } \ | |
62 bool Invoke(NPIdentifier name, \ | |
63 const NPVariant* args, \ | |
64 uint32_t num_args, \ | |
65 NPVariant* result) { \ | |
66 return ::np_utils::DispatcherInvokeHelper(GetDispatcherChain(), \ | |
67 this, \ | |
68 name, \ | |
69 args, \ | |
70 num_args, \ | |
71 result); \ | |
72 } \ | |
73 bool Enumerate(NPIdentifier** names, uint32_t* num_names) { \ | |
74 return ::np_utils::DispatcherEnumerateHelper(GetDispatcherChain(), \ | |
75 this, \ | |
76 names, \ | |
77 num_names); \ | |
78 } \ | |
79 | |
80 namespace np_utils { | |
81 | |
82 class BaseNPDispatcher { | |
83 public: | |
84 BaseNPDispatcher(BaseNPDispatcher* next, const NPUTF8* name); | |
85 | |
86 virtual ~BaseNPDispatcher(); | |
87 | |
88 BaseNPDispatcher* next() const { | |
89 return next_; | |
90 } | |
91 | |
92 NPIdentifier name() const { | |
93 return name_; | |
94 } | |
95 | |
96 virtual int num_args() const = 0; | |
97 | |
98 virtual bool Invoke(NPObject* object, | |
99 const NPVariant* args, | |
100 uint32_t num_args, | |
101 NPVariant* result) = 0; | |
102 | |
103 private: | |
104 BaseNPDispatcher* next_; | |
105 NPIdentifier name_; | |
106 DISALLOW_COPY_AND_ASSIGN(BaseNPDispatcher); | |
107 }; | |
108 | |
109 bool DispatcherHasMethodHelper(BaseNPDispatcher* chain, | |
110 NPObject* object, | |
111 NPIdentifier name); | |
112 | |
113 bool DispatcherInvokeHelper(BaseNPDispatcher* chain, | |
114 NPObject* object, | |
115 NPIdentifier name, | |
116 const NPVariant* args, | |
117 uint32_t num_args, | |
118 NPVariant* result); | |
119 | |
120 bool DispatcherEnumerateHelper(BaseNPDispatcher* chain, | |
121 NPObject* object, | |
122 NPIdentifier** names, | |
123 uint32_t* num_names); | |
124 | |
125 // This class should never be instantiated. It is always specialized. Attempting | |
126 // to instantiate it results in a compilation error. This might mean an | |
127 // attempt to instantiate a dispatcher with more parameters than have been | |
128 // specialized for. See the specialization code below. | |
129 template <typename NPObjectType, typename FunctionType> | |
130 struct NPDispatcher { | |
131 }; | |
132 | |
133 #define TO_NPVARIANT(index) \ | |
134 T##index n##index; \ | |
135 if (!NPVariantToValue(&n##index, args[index])) \ | |
136 return false; \ | |
137 | |
138 #define NUM_PARAMS 0 | |
139 #define PARAM_TYPENAMES | |
140 #define PARAM_TYPES | |
141 #define PARAM_NAMES | |
142 #define PARAM_DECLS // NOLINT | |
143 | |
144 #define PARAM_TO_NVPARIANT_CONVERSIONS \ | |
145 | |
146 #include "gpu/np_utils/np_dispatcher_specializations.h" // NOLINT | |
147 | |
148 | |
149 #define NUM_PARAMS 1 | |
150 #define PARAM_TYPENAMES , typename T0 | |
151 #define PARAM_TYPES T0 | |
152 #define PARAM_NAMES n0 | |
153 #define PARAM_DECLS T0 n0; // NOLINT | |
154 | |
155 #define PARAM_TO_NVPARIANT_CONVERSIONS \ | |
156 TO_NPVARIANT(0); \ | |
157 | |
158 #include "gpu/np_utils/np_dispatcher_specializations.h" // NOLINT | |
159 | |
160 | |
161 #define NUM_PARAMS 2 | |
162 #define PARAM_TYPENAMES , typename T0, typename T1 | |
163 #define PARAM_TYPES T0, T1 | |
164 #define PARAM_NAMES n0, n1 | |
165 #define PARAM_DECLS T0 n0; T1 n1; // NOLINT | |
166 | |
167 #define PARAM_TO_NVPARIANT_CONVERSIONS \ | |
168 TO_NPVARIANT(0); \ | |
169 TO_NPVARIANT(1); \ | |
170 | |
171 #include "gpu/np_utils/np_dispatcher_specializations.h" // NOLINT | |
172 | |
173 | |
174 #define NUM_PARAMS 3 | |
175 #define PARAM_TYPENAMES , typename T0, typename T1, typename T2 | |
176 #define PARAM_TYPES T0, T1, T2 | |
177 #define PARAM_NAMES n0, n1, n2 | |
178 #define PARAM_DECLS T0 n0; T1 n1; T2 n2; // NOLINT | |
179 | |
180 #define PARAM_TO_NVPARIANT_CONVERSIONS \ | |
181 TO_NPVARIANT(0); \ | |
182 TO_NPVARIANT(1); \ | |
183 TO_NPVARIANT(2); \ | |
184 | |
185 #include "gpu/np_utils/np_dispatcher_specializations.h" // NOLINT | |
186 | |
187 | |
188 #define NUM_PARAMS 4 | |
189 #define PARAM_TYPENAMES , typename T0, typename T1, typename T2, typename T3 | |
190 #define PARAM_TYPES T0, T1, T2, T3 | |
191 #define PARAM_NAMES n0, n1, n2, n3 | |
192 #define PARAM_DECLS T0 n0; T1 n1; T2 n2; T3 n3; // NOLINT | |
193 | |
194 #define PARAM_TO_NVPARIANT_CONVERSIONS \ | |
195 TO_NPVARIANT(0); \ | |
196 TO_NPVARIANT(1); \ | |
197 TO_NPVARIANT(2); \ | |
198 TO_NPVARIANT(3); \ | |
199 | |
200 #include "gpu/np_utils/np_dispatcher_specializations.h" // NOLINT | |
201 | |
202 | |
203 #define NUM_PARAMS 5 | |
204 #define PARAM_TYPENAMES , typename T0, typename T1, typename T2, typename T3, \ | |
205 typename T4 | |
206 #define PARAM_TYPES T0, T1, T2, T3, T4 | |
207 #define PARAM_NAMES n0, n1, n2, n3, n4 | |
208 #define PARAM_DECLS T0 n0; T1 n1; T2 n2; T3 n3; T4 n4; // NOLINT | |
209 | |
210 #define PARAM_TO_NVPARIANT_CONVERSIONS \ | |
211 TO_NPVARIANT(0); \ | |
212 TO_NPVARIANT(1); \ | |
213 TO_NPVARIANT(2); \ | |
214 TO_NPVARIANT(3); \ | |
215 TO_NPVARIANT(4); \ | |
216 | |
217 #include "gpu/np_utils/np_dispatcher_specializations.h" // NOLINT | |
218 | |
219 | |
220 #undef TO_NPVARIANT | |
221 | |
222 } // namespace np_utils | |
223 | |
224 #endif // GPU_NP_UTILS_NP_DISPATCHER_H_ | |
OLD | NEW |