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 "../main/krossconfig.h"
24#include "object.h"
25#include "variant.h"
26#include "list.h"
27
28#include <tqstring.h>
29
30namespace Kross { namespace Api {
31
35 template<class OBJ>
36 struct ProxyArgTranslator {
37 OBJ* m_object;
38 ProxyArgTranslator(Kross::Api::Object* obj) {
39 m_object = Kross::Api::Object::fromObject<OBJ>(obj);
40 }
41 template<typename T>
42 inline operator T () {
43 return m_object->operator T();
44 }
45 };
46
50 struct ProxyRetTranslator {
51 template<class RETURNOBJ, typename TYPE>
52 inline static Object::Ptr cast(TYPE t) {
53 return RETURNOBJ::toObject(t);
54 }
55 };
56
95 template< class INSTANCE, // the objectinstance
96 typename METHOD, // the method-signature
97 class RETURNOBJ,// = Kross::Api::Object, // return-value
98 class ARG1OBJ = Kross::Api::Object, // first parameter-value
99 class ARG2OBJ = Kross::Api::Object, // second parameter-value
100 class ARG3OBJ = Kross::Api::Object, // theird parameter-value
101 class ARG4OBJ = Kross::Api::Object // forth parameter-value
102 >
103 class ProxyFunction : public Function
104 {
105 template<class PROXYFUNC, typename RETURNTYPE>
106 friend struct ProxyFunctionCaller;
107 private:
109 INSTANCE* m_instance;
111 const METHOD m_method;
112
114 TDESharedPtr<ARG1OBJ> m_defarg1;
116 TDESharedPtr<ARG2OBJ> m_defarg2;
118 TDESharedPtr<ARG3OBJ> m_defarg3;
120 TDESharedPtr<ARG4OBJ> m_defarg4;
121
126 template<class PROXYFUNC, typename RETURNTYPE>
127 struct ProxyFunctionCaller {
128 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3, Kross::Api::Object* arg4) {
129 return ProxyRetTranslator::cast<RETURNTYPE>(
130 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3), ProxyArgTranslator<ARG4OBJ>(arg4) )
131 );
132 }
133 };
134
140 template<class PROXYFUNC>
141 struct ProxyFunctionCaller<PROXYFUNC, void> {
142 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3, Kross::Api::Object* arg4) {
143 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG1OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3), ProxyArgTranslator<ARG4OBJ>(arg4) );
144 return 0; // void return-value
145 }
146 };
147
148 public:
149
157 ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0, ARG3OBJ* defarg3 = 0, ARG4OBJ* defarg4 = 0)
158 : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2), m_defarg3(defarg3), m_defarg4(defarg4) {}
159
170 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this,
171 args->item(0, m_defarg1),
172 args->item(1, m_defarg2),
173 args->item(2, m_defarg3),
174 args->item(3, m_defarg4)
175 );
176 }
177 };
178
182 template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ, class ARG2OBJ, class ARG3OBJ>
183 class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ, ARG3OBJ> : public Function
184 {
185 template<class PROXYFUNC, typename RETURNTYPE>
186 friend struct ProxyFunctionCaller;
187 private:
188 INSTANCE* m_instance;
189 const METHOD m_method;
190 TDESharedPtr<ARG1OBJ> m_defarg1;
191 TDESharedPtr<ARG2OBJ> m_defarg2;
192 TDESharedPtr<ARG3OBJ> m_defarg3;
193
194 template<class PROXYFUNC, typename RETURNTYPE>
195 struct ProxyFunctionCaller {
196 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3) {
197 return ProxyRetTranslator::cast<RETURNTYPE>(
198 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3) )
199 );
200 }
201 };
202
203 template<class PROXYFUNC>
204 struct ProxyFunctionCaller<PROXYFUNC, void> {
205 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2, Kross::Api::Object* arg3) {
206 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2), ProxyArgTranslator<ARG3OBJ>(arg3) );
207 return 0;
208 }
209 };
210
211 public:
212 ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0, ARG3OBJ* defarg3 = 0)
213 : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2), m_defarg3(defarg3) {}
215 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this,
216 args->item(0, m_defarg1), args->item(1, m_defarg2), args->item(2, m_defarg3)
217 );
218 }
219 };
220
224 template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ, class ARG2OBJ>
225 class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ, ARG2OBJ> : public Function
226 {
227 template<class PROXYFUNC, typename RETURNTYPE>
228 friend struct ProxyFunctionCaller;
229 private:
230 INSTANCE* m_instance;
231 const METHOD m_method;
232 TDESharedPtr<ARG1OBJ> m_defarg1;
233 TDESharedPtr<ARG2OBJ> m_defarg2;
234
235 template<class PROXYFUNC, typename RETURNTYPE>
236 struct ProxyFunctionCaller {
237 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2) {
238 return ProxyRetTranslator::cast<RETURNTYPE>(
239 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2) )
240 );
241 }
242 };
243
244 template<class PROXYFUNC>
245 struct ProxyFunctionCaller<PROXYFUNC, void> {
246 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1, Kross::Api::Object* arg2) {
247 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1), ProxyArgTranslator<ARG2OBJ>(arg2) );
248 return 0;
249 }
250 };
251
252 public:
253 ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0, ARG2OBJ* defarg2 = 0)
254 : m_instance(instance), m_method(method), m_defarg1(defarg1), m_defarg2(defarg2) {}
256 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this,
257 args->item(0, m_defarg1), args->item(1, m_defarg2)
258 );
259 }
260 };
261
265 template<class INSTANCE, typename METHOD, class RETURNOBJ, class ARG1OBJ>
266 class ProxyFunction<INSTANCE, METHOD, RETURNOBJ, ARG1OBJ> : public Function
267 {
268 template<class PROXYFUNC, typename RETURNTYPE>
269 friend struct ProxyFunctionCaller;
270 private:
271 INSTANCE* m_instance;
272 const METHOD m_method;
273 TDESharedPtr<ARG1OBJ> m_defarg1;
274
275 template<class PROXYFUNC, typename RETURNTYPE>
276 struct ProxyFunctionCaller {
277 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1) {
278 return ProxyRetTranslator::cast<RETURNTYPE>(
279 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1) )
280 );
281 }
282 };
283
284 template<class PROXYFUNC>
285 struct ProxyFunctionCaller<PROXYFUNC, void> {
286 inline static Object::Ptr exec(PROXYFUNC* self, Kross::Api::Object* arg1) {
287 ( (self->m_instance)->*(self->m_method) )( ProxyArgTranslator<ARG1OBJ>(arg1) );
288 return 0;
289 }
290 };
291
292 public:
293 ProxyFunction(INSTANCE* instance, const METHOD& method, ARG1OBJ* defarg1 = 0)
294 : m_instance(instance), m_method(method), m_defarg1(defarg1) {}
296 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this,
297 args->item(0, m_defarg1)
298 );
299 }
300 };
301
305 template<class INSTANCE, typename METHOD, class RETURNOBJ>
306 class ProxyFunction<INSTANCE, METHOD, RETURNOBJ> : public Function
307 {
308 template<class PROXYFUNC, typename RETURNTYPE>
309 friend struct ProxyFunctionCaller;
310 private:
311 INSTANCE* m_instance;
312 const METHOD m_method;
313
314 template<class PROXYFUNC, typename RETURNTYPE>
315 struct ProxyFunctionCaller {
316 inline static Object::Ptr exec(PROXYFUNC* self) {
317 return ProxyRetTranslator::cast<RETURNTYPE>(
318 ( (self->m_instance)->*(self->m_method) )()
319 );
320 }
321 };
322
323 template<class PROXYFUNC>
324 struct ProxyFunctionCaller<PROXYFUNC, void> {
325 inline static Object::Ptr exec(PROXYFUNC* self) {
326 ( (self->m_instance)->*(self->m_method) )();
327 return 0;
328 }
329 };
330
331 public:
332 ProxyFunction(INSTANCE* instance, const METHOD& method)
333 : m_instance(instance), m_method(method) {}
335 return ProxyFunctionCaller<ProxyFunction, RETURNOBJ>::exec(this);
336 }
337 };
338
339}}
340
341#endif
342
The base class for functions.
Definition function.h:36
TDESharedPtr< List > Ptr
Shared pointer to implement reference-counting.
Definition list.h:44
The common Object class all other object-classes are inheritated from.
Definition object.h:51
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:57
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:214
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:255
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:295
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:334
The ProxyFunction template-class is used to publish any C/C++ method (not only slots) of a struct or ...
Definition proxy.h:104
Object::Ptr call(List::Ptr args)
This method got called if the wrapped method should be executed.
Definition proxy.h:169
ProxyFunction(INSTANCE *instance, const METHOD &method, ARG1OBJ *defarg1=0, ARG2OBJ *defarg2=0, ARG3OBJ *defarg3=0, ARG4OBJ *defarg4=0)
Constructor.
Definition proxy.h:157
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:31