##// END OF EJS Templates
fixed deriving C++ classes...
florianlink -
r122:fb2e671067ff
parent child
Show More
@@ -1,452 +1,454
1 1 /*
2 2 *
3 3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
4 4 *
5 5 * This library is free software; you can redistribute it and/or
6 6 * modify it under the terms of the GNU Lesser General Public
7 7 * License as published by the Free Software Foundation; either
8 8 * version 2.1 of the License, or (at your option) any later version.
9 9 *
10 10 * This library is distributed in the hope that it will be useful,
11 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 13 * Lesser General Public License for more details.
14 14 *
15 15 * Further, this software is distributed without any warranty that it is
16 16 * free of the rightful claim of any third person regarding infringement
17 17 * or the like. Any license provided herein, whether implied or
18 18 * otherwise, applies only to this software file. Patent licenses, if
19 19 * any, provided herein do not apply to combinations of this program with
20 20 * other software, or any other product whatsoever.
21 21 *
22 22 * You should have received a copy of the GNU Lesser General Public
23 23 * License along with this library; if not, write to the Free Software
24 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 25 *
26 26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
27 27 * 28359 Bremen, Germany or:
28 28 *
29 29 * http://www.mevis.de
30 30 *
31 31 */
32 32
33 33 //----------------------------------------------------------------------------------
34 34 /*!
35 35 // \file PythonQtClassWrapper.cpp
36 36 // \author Florian Link
37 37 // \author Last changed by $Author: florian $
38 38 // \date 2006-05
39 39 */
40 40 //----------------------------------------------------------------------------------
41 41
42 42 #include "PythonQtClassWrapper.h"
43 43 #include <QObject>
44 44
45 45 #include "PythonQt.h"
46 46 #include "PythonQtSlot.h"
47 47 #include "PythonQtClassInfo.h"
48 48 #include "PythonQtConversion.h"
49 49 #include "PythonQtInstanceWrapper.h"
50 50
51 51 static PyObject* PythonQtInstanceWrapper_invert(PythonQtInstanceWrapper* wrapper)
52 52 {
53 53 PyObject* result = NULL;
54 54 static QByteArray memberName = "__invert__";
55 55 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
56 56 if (opSlot._type == PythonQtMemberInfo::Slot) {
57 57 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
58 58 }
59 59 return result;
60 60 }
61 61
62 62 static int PythonQtInstanceWrapper_nonzero(PythonQtInstanceWrapper* wrapper)
63 63 {
64 64 int result = (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
65 65 if (result) {
66 66 static QByteArray memberName = "__nonzero__";
67 67 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
68 68 if (opSlot._type == PythonQtMemberInfo::Slot) {
69 69 PyObject* resultObj = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
70 70 if (resultObj == Py_False) {
71 71 result = 0;
72 72 }
73 73 Py_XDECREF(resultObj);
74 74 }
75 75 }
76 76 return result;
77 77 }
78 78
79 79
80 80 static PyObject* PythonQtInstanceWrapper_binaryfunc(PythonQtInstanceWrapper* wrapper, PyObject* other, const QByteArray& opName, const QByteArray& fallbackOpName = QByteArray())
81 81 {
82 82 PyObject* result = NULL;
83 83 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(opName);
84 84 if (opSlot._type == PythonQtMemberInfo::Slot) {
85 85 // TODO get rid of tuple
86 86 PyObject* args = PyTuple_New(1);
87 87 Py_INCREF(other);
88 88 PyTuple_SET_ITEM(args, 0, other);
89 89 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, args, NULL, wrapper->_wrappedPtr);
90 90 Py_DECREF(args);
91 91 if (!result && !fallbackOpName.isEmpty()) {
92 92 // try fallback if we did not get a result
93 93 result = PythonQtInstanceWrapper_binaryfunc(wrapper, other, fallbackOpName);
94 94 }
95 95 }
96 96 return result;
97 97 }
98 98
99 99 #define BINARY_OP(NAME) \
100 100 static PyObject* PythonQtInstanceWrapper_ ## NAME(PythonQtInstanceWrapper* wrapper, PyObject* other) \
101 101 { \
102 102 static const QByteArray opName("__" #NAME "__"); \
103 103 return PythonQtInstanceWrapper_binaryfunc(wrapper, other, opName); \
104 104 }
105 105
106 106 #define BINARY_OP_INPLACE(NAME) \
107 107 static PyObject* PythonQtInstanceWrapper_i ## NAME(PythonQtInstanceWrapper* wrapper, PyObject* other) \
108 108 { \
109 109 static const QByteArray opName("__i" #NAME "__"); \
110 110 static const QByteArray fallbackName("__" #NAME "__"); \
111 111 return PythonQtInstanceWrapper_binaryfunc(wrapper, other, opName, fallbackName); \
112 112 }
113 113
114 114 BINARY_OP(add)
115 115 BINARY_OP(sub)
116 116 BINARY_OP(mul)
117 117 BINARY_OP(div)
118 118 BINARY_OP(and)
119 119 BINARY_OP(or)
120 120 BINARY_OP(xor)
121 121 BINARY_OP(mod)
122 122 BINARY_OP(lshift)
123 123 BINARY_OP(rshift)
124 124
125 125 BINARY_OP_INPLACE(add)
126 126 BINARY_OP_INPLACE(sub)
127 127 BINARY_OP_INPLACE(mul)
128 128 BINARY_OP_INPLACE(div)
129 129 BINARY_OP_INPLACE(and)
130 130 BINARY_OP_INPLACE(or)
131 131 BINARY_OP_INPLACE(xor)
132 132 BINARY_OP_INPLACE(mod)
133 133 BINARY_OP_INPLACE(lshift)
134 134 BINARY_OP_INPLACE(rshift)
135 135
136 136 static void initializeSlots(PythonQtClassWrapper* wrap)
137 137 {
138 138 int typeSlots = wrap->classInfo()->typeSlots();
139 139 if (typeSlots) {
140 140 if (typeSlots & PythonQt::Type_Add) {
141 141 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_add;
142 142 }
143 143 if (typeSlots & PythonQt::Type_Subtract) {
144 144 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_sub;
145 145 }
146 146 if (typeSlots & PythonQt::Type_Multiply) {
147 147 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul;
148 148 }
149 149 if (typeSlots & PythonQt::Type_Divide) {
150 150 wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div;
151 151 wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div;
152 152 }
153 153 if (typeSlots & PythonQt::Type_And) {
154 154 wrap->_base.as_number.nb_and = (binaryfunc)PythonQtInstanceWrapper_and;
155 155 }
156 156 if (typeSlots & PythonQt::Type_Or) {
157 157 wrap->_base.as_number.nb_or = (binaryfunc)PythonQtInstanceWrapper_or;
158 158 }
159 159 if (typeSlots & PythonQt::Type_Xor) {
160 160 wrap->_base.as_number.nb_xor = (binaryfunc)PythonQtInstanceWrapper_xor;
161 161 }
162 162 if (typeSlots & PythonQt::Type_Mod) {
163 163 wrap->_base.as_number.nb_remainder = (binaryfunc)PythonQtInstanceWrapper_mod;
164 164 }
165 165 if (typeSlots & PythonQt::Type_LShift) {
166 166 wrap->_base.as_number.nb_lshift = (binaryfunc)PythonQtInstanceWrapper_lshift;
167 167 }
168 168 if (typeSlots & PythonQt::Type_RShift) {
169 169 wrap->_base.as_number.nb_rshift = (binaryfunc)PythonQtInstanceWrapper_rshift;
170 170 }
171 171
172 172 if (typeSlots & PythonQt::Type_InplaceAdd) {
173 173 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_iadd;
174 174 }
175 175 if (typeSlots & PythonQt::Type_InplaceSubtract) {
176 176 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_isub;
177 177 }
178 178 if (typeSlots & PythonQt::Type_InplaceMultiply) {
179 179 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_imul;
180 180 }
181 181 if (typeSlots & PythonQt::Type_InplaceDivide) {
182 182 wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
183 183 wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
184 184 }
185 185 if (typeSlots & PythonQt::Type_InplaceAnd) {
186 186 wrap->_base.as_number.nb_inplace_and = (binaryfunc)PythonQtInstanceWrapper_iand;
187 187 }
188 188 if (typeSlots & PythonQt::Type_InplaceOr) {
189 189 wrap->_base.as_number.nb_inplace_or = (binaryfunc)PythonQtInstanceWrapper_ior;
190 190 }
191 191 if (typeSlots & PythonQt::Type_InplaceXor) {
192 192 wrap->_base.as_number.nb_inplace_xor = (binaryfunc)PythonQtInstanceWrapper_ixor;
193 193 }
194 194 if (typeSlots & PythonQt::Type_InplaceMod) {
195 195 wrap->_base.as_number.nb_inplace_remainder = (binaryfunc)PythonQtInstanceWrapper_imod;
196 196 }
197 197 if (typeSlots & PythonQt::Type_InplaceLShift) {
198 198 wrap->_base.as_number.nb_inplace_lshift = (binaryfunc)PythonQtInstanceWrapper_ilshift;
199 199 }
200 200 if (typeSlots & PythonQt::Type_InplaceRShift) {
201 201 wrap->_base.as_number.nb_inplace_rshift = (binaryfunc)PythonQtInstanceWrapper_irshift;
202 202 }
203 203 if (typeSlots & PythonQt::Type_Invert) {
204 204 wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert;
205 205 }
206 206 if (typeSlots & PythonQt::Type_NonZero) {
207 207 wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero;
208 208 }
209 209 }
210 210 }
211 211
212 212 static PyObject* PythonQtClassWrapper_alloc(PyTypeObject *self, Py_ssize_t nitems)
213 213 {
214 214 // call the default type alloc
215 215 PyObject* obj = PyType_Type.tp_alloc(self, nitems);
216 216
217 217 // take current class type, if we are called via newPythonQtClassWrapper()
218 218 PythonQtClassWrapper* wrap = (PythonQtClassWrapper*)obj;
219 219 wrap->_classInfo = PythonQt::priv()->currentClassInfoForClassWrapperCreation();
220 initializeSlots(wrap);
220 if (wrap->_classInfo) {
221 initializeSlots(wrap);
222 }
221 223
222 224 return obj;
223 225 }
224 226
225 227
226 228 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args, PyObject* kwds)
227 229 {
228 230 // call the default type init
229 231 if (PyType_Type.tp_init((PyObject *)self, args, kwds) < 0) {
230 232 return -1;
231 233 }
232 234
233 235 // if we have no CPP class information, try our base class
234 236 if (!self->classInfo()) {
235 237 PyTypeObject* superType = ((PyTypeObject *)self)->tp_base;
236 238
237 239 if (!superType || (superType->ob_type != &PythonQtClassWrapper_Type)) {
238 240 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
239 241 return -1;
240 242 }
241 243
242 244 // take the class info from the superType
243 245 self->_classInfo = ((PythonQtClassWrapper*)superType)->classInfo();
244 246 }
245 247
246 248 return 0;
247 249 }
248 250
249 251 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
250 252 {
251 253 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
252 254 }
253 255
254 256 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
255 257 {
256 258 return PythonQt::self()->helpCalled(type->classInfo());
257 259 }
258 260
259 261 PyObject *PythonQtClassWrapper__init__(PythonQtClassWrapper *type, PyObject *args)
260 262 {
261 263 Py_ssize_t argc = PyTuple_Size(args);
262 264 if (argc>0) {
263 265 // we need to call __init__ of the instance
264 266 PyObject* self = PyTuple_GET_ITEM(args, 0);
265 267 if (PyObject_TypeCheck(self, (PyTypeObject*)type->classInfo()->pythonQtClassWrapper())) {
266 268 PyObject* newargs = PyTuple_New(argc-1);
267 269 for (int i = 0;i<argc-1; i++) {
268 270 PyTuple_SET_ITEM(newargs, i,PyTuple_GET_ITEM(args, i+1));
269 271 }
270 272 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
271 273 int result = PythonQtInstanceWrapper_init(wrapper, newargs, NULL);
272 274 Py_DECREF(newargs);
273 275 if (result==0) {
274 276 Py_INCREF(Py_None);
275 277 return Py_None;
276 278 } else {
277 279 // init failed!
278 280 }
279 281 } else {
280 282 // self not of correct type!
281 283 }
282 284 } else {
283 285 // wrong number of args
284 286 }
285 287 return NULL;
286 288 }
287 289
288 290 static PyMethodDef PythonQtClassWrapper_methods[] = {
289 291 {"__init__", (PyCFunction)PythonQtClassWrapper__init__, METH_VARARGS,
290 292 "Return the classname of the object"
291 293 },
292 294 {"className", (PyCFunction)PythonQtClassWrapper_classname, METH_NOARGS,
293 295 "Return the classname of the object"
294 296 },
295 297 {"help", (PyCFunction)PythonQtClassWrapper_help, METH_NOARGS,
296 298 "Shows the help of available methods for this class"
297 299 },
298 300 {NULL, NULL, 0 , NULL} /* Sentinel */
299 301 };
300 302
301 303
302 304 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
303 305 {
304 306 const char *attributeName;
305 307 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
306 308
307 309 if ((attributeName = PyString_AsString(name)) == NULL) {
308 310 return NULL;
309 311 }
310 312 if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) {
311 313 return NULL;
312 314 }
313 315
314 316 if (qstrcmp(attributeName, "__dict__")==0) {
315 317 PyObject* dict = ((PyTypeObject *)wrapper)->tp_dict;
316 318 if (!wrapper->classInfo()) {
317 319 Py_INCREF(dict);
318 320 return dict;
319 321 }
320 322 dict = PyDict_Copy(dict);
321 323
322 324 QStringList l = wrapper->classInfo()->memberList(false);
323 325 foreach (QString name, l) {
324 326 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
325 327 if (o) {
326 328 PyDict_SetItemString(dict, name.toLatin1().data(), o);
327 329 Py_DECREF(o);
328 330 } else {
329 331 // it must have been a property or child, which we do not know as a class object...
330 332 }
331 333 }
332 334 if (wrapper->classInfo()->constructors()) {
333 335 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[0], obj);
334 336 PyDict_SetItemString(dict, "__init__", func);
335 337 Py_DECREF(func);
336 338 }
337 339 for (int i = 1;i<3;i++) {
338 340 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[i], obj);
339 341 PyDict_SetItemString(dict, PythonQtClassWrapper_methods[i].ml_name, func);
340 342 Py_DECREF(func);
341 343 }
342 344 return dict;
343 345 }
344 346
345 347 if (wrapper->classInfo()) {
346 348 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
347 349 if (member._type == PythonQtMemberInfo::EnumValue) {
348 350 PyObject* enumValue = member._enumValue;
349 351 Py_INCREF(enumValue);
350 352 return enumValue;
351 353 } else if (member._type == PythonQtMemberInfo::EnumWrapper) {
352 354 PyObject* enumWrapper = member._enumWrapper;
353 355 Py_INCREF(enumWrapper);
354 356 return enumWrapper;
355 357 } else if (member._type == PythonQtMemberInfo::Slot) {
356 358 // we return all slots, even the instance slots, since they are callable as unbound slots with self argument
357 359 return PythonQtSlotFunction_New(member._slot, obj, NULL);
358 360 }
359 361 }
360 362
361 363 // look for the interal methods (className(), help())
362 364 PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName);
363 365 if (internalMethod) {
364 366 return internalMethod;
365 367 }
366 368 PyErr_Clear();
367 369
368 370 // look in super
369 371 PyObject* superAttr = PyType_Type.tp_getattro(obj, name);
370 372 if (superAttr) {
371 373 return superAttr;
372 374 }
373 375
374 376 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
375 377 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
376 378 return NULL;
377 379 }
378 380
379 381 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
380 382 {
381 383 return PyType_Type.tp_setattro(obj,name,value);
382 384 }
383 385
384 386 /*
385 387 static PyObject * PythonQtClassWrapper_repr(PyObject * obj)
386 388 {
387 389 PythonQtClassWrapper* wrapper = (PythonQtClassWrapper*)obj;
388 390 if (wrapper->classInfo()->isCPPWrapper()) {
389 391 const QMetaObject* meta = wrapper->classInfo()->metaObject();
390 392 if (!meta) {
391 393 QObject* decorator = wrapper->classInfo()->decorator();
392 394 if (decorator) {
393 395 meta = decorator->metaObject();
394 396 }
395 397 }
396 398 if (meta) {
397 399 return PyString_FromFormat("%s Class (C++ wrapped by %s)", wrapper->classInfo()->className(), meta->className());
398 400 } else {
399 401 return PyString_FromFormat("%s Class (C++ unwrapped)", wrapper->classInfo()->className());
400 402 }
401 403 } else {
402 404 return PyString_FromFormat("%s Class", wrapper->classInfo()->className());
403 405 }
404 406 }
405 407
406 408 */
407 409
408 410 PyTypeObject PythonQtClassWrapper_Type = {
409 411 PyObject_HEAD_INIT(NULL)
410 412 0, /*ob_size*/
411 413 "PythonQt.PythonQtClassWrapper", /*tp_name*/
412 414 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
413 415 0, /*tp_itemsize*/
414 416 0, /*tp_dealloc*/
415 417 0, /*tp_print*/
416 418 0, /*tp_getattr*/
417 419 0, /*tp_setattr*/
418 420 0, /*tp_compare*/
419 421 0, //PythonQtClassWrapper_repr, /*tp_repr*/
420 422 0, /*tp_as_number*/
421 423 0, /*tp_as_sequence*/
422 424 0, /*tp_as_mapping*/
423 425 0, /*tp_hash */
424 426 0, /*tp_call*/
425 427 0, /*tp_str*/
426 428 PythonQtClassWrapper_getattro, /*tp_getattro*/
427 429 PythonQtClassWrapper_setattro, /*tp_setattro*/
428 430 0, /*tp_as_buffer*/
429 431 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
430 432 0, /* tp_doc */
431 433 0, /* tp_traverse */
432 434 0, /* tp_clear */
433 435 0, /* tp_richcompare */
434 436 0, /* tp_weaklistoffset */
435 437 0, /* tp_iter */
436 438 0, /* tp_iternext */
437 439 0, /* tp_methods */
438 440 0, /* tp_members */
439 441 0, /* tp_getset */
440 442 0, /* tp_base */
441 443 0, /* tp_dict */
442 444 0, /* tp_descr_get */
443 445 0, /* tp_descr_set */
444 446 0, /* tp_dictoffset */
445 447 (initproc)PythonQtClassWrapper_init, /* tp_init */
446 448 PythonQtClassWrapper_alloc, /* tp_alloc */
447 449 0, /* tp_new */
448 450 0, /* tp_free */
449 451 };
450 452
451 453 //-------------------------------------------------------
452 454
General Comments 0
You need to be logged in to leave comments. Login now