lib

proxy.h
1/***************************************************************************
2 * proxy.h
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#ifndef KROSS_API_PROXY_H
21#define KROSS_API_PROXY_H
22
23#include "object.h"
24#include "variant.h"
25#include "list.h"
26
27#include <tqstring.h>
28
29namespace Kross { namespace Api {
30
34 template<class OBJ>
35 struct ProxyArgTranslator {
36 OBJ* m_object;
37 ProxyArgTranslator(Kross::Api::Object* obj) {
39 }
40 template<typename T>
41 inline operator T () {
42 return m_object->operator T();
43 }
44 };
45
49 struct ProxyRetTranslator {
50 template<class RETURNOBJ, typename TYPE>
51 inline static Object::Ptr cast(TYPE t) {
52 return RETURNOBJ::toObject(t);
53 }
54 };
55
94 template< class INSTANCE, // the objectinstance
95 typename METHOD, // the method-signature
96 class RETURNOBJ,// = Kross::Api::Object, // return-value
97 class ARG1OBJ = Kross::Api::Object, // first parameter-value
98 class ARG2OBJ = Kross::Api::Object, // second parameter-value
99 class ARG3OBJ = Kross::Api::Object, // theird parameter-value
100 class ARG4OBJ = Kross::Api::Object // forth parameter-value
101 >
102 class ProxyFunction : public Function
103 {
104 template<class PROXYFUNC, typename RETURNTYPE>
105 friend struct ProxyFunctionCaller;
106 private:
108 INSTANCE* m_instance;
110 const METHOD m_method;
111
113 TDESharedPtr<ARG1OBJ> m_defarg1;
115 TDESharedPtr<ARG2OBJ> m_defarg2;
117 TDESharedPtr<ARG3OBJ> m_defarg3;
119 TDESharedPtr<ARG4OBJ> m_defarg4;
120
125 template<class PROXYFUNC, typename RETURNTYPE>
126 struct ProxyFunctionCaller {
127 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3, Kross::Api::Object* arg4) {
128 return ProxyRetTranslator::cast<RETURNTYPE>(
129 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3), ProxyArgTranslator<ARG4OBJ>(arg4) )
130 );
131 }
132 };
133
139 template<class PROXYFUNC>
140 struct ProxyFunctionCaller<PROXYFUNC, void> {
141 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3, Kross::Api::Object* arg4) {
142 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG1OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3), ProxyArgTranslator<ARG4OBJ>(arg4) );
143 return 0; // void return-value
144 }
145 };
146
147 public:
148
156 ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0, ARG3OBJ* defarg3 = 0, ARG4OBJ* defarg4 = 0)
157 : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2), m_defarg3(defarg3), m_defarg4(defarg4) {}
158
169 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this,
170 args->item(0, m_defarg1),
171 args->item(1, m_defarg2),
172 args->item(2, m_defarg3),
173 args->item(3, m_defarg4)
174 );
175 }
176 };
177
181 template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ>
182 class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ, ARG3OBJ> : public Function
183 {
184 template<class PROXYFUNC, typename RETURNTYPE>
185 friend struct ProxyFunctionCaller;
186 private:
187 INSTANCE* m_instance;
188 const METHOD m_method;
189 TDESharedPtr<ARG1OBJ> m_defarg1;
190 TDESharedPtr<ARG2OBJ> m_defarg2;
191 TDESharedPtr<ARG3OBJ> m_defarg3;
192
193 template<class PROXYFUNC, typename RETURNTYPE>
194 struct ProxyFunctionCaller {
195 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3) {
196 return ProxyRetTranslator::cast<RETURNTYPE>(
197 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3) )
198 );
199 }
200 };
201
202 template<class PROXYFUNC>
203 struct ProxyFunctionCaller<PROXYFUNC, void> {
204 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3) {
205 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3) );
206 return 0;
207 }
208 };
209
210 public:
211 ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0, ARG3OBJ* defarg3 = 0)
212 : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2), m_defarg3(defarg3) {}
214 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this,
215 args->item(0, m_defarg1), args->item(1, m_defarg2), args->item(2, m_defarg3)
216 );
217 }
218 };
219
223 template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ, class ARG2OBJ>
224 class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ> : public Function
225 {
226 template<class PROXYFUNC, typename RETURNTYPE>
227 friend struct ProxyFunctionCaller;
228 private:
229 INSTANCE* m_instance;
230 const METHOD m_method;
231 TDESharedPtr<ARG1OBJ> m_defarg1;
232 TDESharedPtr<ARG2OBJ> m_defarg2;
233
234 template<class PROXYFUNC, typename RETURNTYPE>
235 struct ProxyFunctionCaller {
236 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2) {
237 return ProxyRetTranslator::cast<RETURNTYPE>(
238 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2) )
239 );
240 }
241 };
242
243 template<class PROXYFUNC>
244 struct ProxyFunctionCaller<PROXYFUNC, void> {
245 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2) {
246 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2) );
247 return 0;
248 }
249 };
250
251 public:
252 ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0)
253 : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2) {}
255 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this,
256 args->item(0, m_defarg1), args->item(1, m_defarg2)
257 );
258 }
259 };
260
264 template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ>
265 class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ> : public Function
266 {
267 template<class PROXYFUNC, typename RETURNTYPE>
268 friend struct ProxyFunctionCaller;
269 private:
270 INSTANCE* m_instance;
271 const METHOD m_method;
272 TDESharedPtr<ARG1OBJ> m_defarg1;
273
274 template<class PROXYFUNC, typename RETURNTYPE>
275 struct ProxyFunctionCaller {
276 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1) {
277 return ProxyRetTranslator::cast<RETURNTYPE>(
278 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1) )
279 );
280 }
281 };
282
283 template<class PROXYFUNC>
284 struct ProxyFunctionCaller<PROXYFUNC, void> {
285 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1) {
286 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1) );
287 return 0;
288 }
289 };
290
291 public:
292 ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0)
293 : m_instance(instance), m_method(method), m_defarg1(defarg1) {}
295 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this,
296 args->item(0, m_defarg1)
297 );
298 }
299 };
300
304 template<class INSTANCE, typename METHOD, class RETURNOBJ>
305 class ProxyFunction<INSTANCE, METHOD, RETURNOBJ> : public Function
306 {
307 template<class PROXYFUNC, typename RETURNTYPE>
308 friend struct ProxyFunctionCaller;
309 private:
310 INSTANCE* m_instance;
311 const METHOD m_method;
312
313 template<class PROXYFUNC, typename RETURNTYPE>
314 struct ProxyFunctionCaller {
315 inline static Object::Ptr exec(PROXYFUNC* self) {
316 return ProxyRetTranslator::cast<RETURNTYPE>(
317 ( (self->m_instance)->*(self->m_method) )()
318 );
319 }
320 };
321
322 template<class PROXYFUNC>
323 struct ProxyFunctionCaller<PROXYFUNC, void> {
324 inline static Object::Ptr exec(PROXYFUNC* self) {
325 ( (self->m_instance)->*(self->m_method) )();
326 return 0;
327 }
328 };
329
330 public:
331 ProxyFunction(INSTANCE* instance, const METHOD& method)
332 : m_instance(instance), m_method(method) {}
334 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this);
335 }
336 };
337
338}}
339
340#endif
341
The base class for functions.
Definition function.h:35
TDESharedPtr< List > Ptr
Shared pointer to implement reference-counting.
Definition list.h:46
The common Object class all other object-classes are inheritated from.
Definition object.h:50
static T * fromObject(Object::Ptr object)
Try to convert the Object instance to the template class T.
Definition object.h:140
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:56
Object::Ptr call(List::Ptr args)
Each function needs to implement the call-method which will be executed if the function itself should...
Definition proxy.h:213
Object::Ptr call(List::Ptr args)
Each function needs to implement the call-method which will be executed if the function itself should...
Definition proxy.h:254
Object::Ptr call(List::Ptr args)
Each function needs to implement the call-method which will be executed if the function itself should...
Definition proxy.h:294
Object::Ptr call(List::Ptr)
Each function needs to implement the call-method which will be executed if the function itself should...
Definition proxy.h:333
Object::Ptr call(List::Ptr args)
This method got called if the wrapped method should be executed.
Definition proxy.h:168
ProxyFunction(INSTANCE *instance, const METHOD &method, ARG1OBJ *defarg1=0, ARG2OBJ *defarg2=0, ARG3OBJ *defarg3=0, ARG4OBJ *defarg4=0)
Constructor.
Definition proxy.h:156
The common Kross API used as common codebase.
Definition callable.h:33
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:33