##// END OF EJS Templates
cleared cached signature hash to avoid dangling pointers...
florianlink -
r203:64a9f70b03c9
parent child
Show More
@@ -1,377 +1,378
1 1 /*
2 2 *
3 3 * Copyright (C) 2010 MeVis Medical Solutions AG 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 Medical Solutions AG, 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 PythonQtMethodInfo.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 "PythonQtMethodInfo.h"
43 43 #include "PythonQtClassInfo.h"
44 44 #include <iostream>
45 45
46 46 QHash<QByteArray, PythonQtMethodInfo*> PythonQtMethodInfo::_cachedSignatures;
47 47 QHash<QByteArray, QByteArray> PythonQtMethodInfo::_parameterNameAliases;
48 48
49 49 PythonQtMethodInfo::PythonQtMethodInfo(const QMetaMethod& meta, PythonQtClassInfo* classInfo)
50 50 {
51 51 #ifdef PYTHONQT_DEBUG
52 52 QByteArray sig(meta.signature());
53 53 sig = sig.mid(sig.indexOf('('));
54 54 QByteArray fullSig = QByteArray(meta.typeName()) + " " + sig;
55 55 std::cout << "caching " << fullSig.data() << std::endl;
56 56 #endif
57 57
58 58 ParameterInfo type;
59 59 fillParameterInfo(type, QByteArray(meta.typeName()), classInfo);
60 60 _parameters.append(type);
61 61 QList<QByteArray> names = meta.parameterTypes();
62 62 foreach (const QByteArray& name, names) {
63 63 fillParameterInfo(type, name, classInfo);
64 64 _parameters.append(type);
65 65 }
66 66 }
67 67
68 68 PythonQtMethodInfo::PythonQtMethodInfo(const QByteArray& typeName, const QList<QByteArray>& args)
69 69 {
70 70 ParameterInfo type;
71 71 fillParameterInfo(type, typeName, NULL);
72 72 _parameters.append(type);
73 73 foreach (const QByteArray& name, args) {
74 74 fillParameterInfo(type, name, NULL);
75 75 _parameters.append(type);
76 76 }
77 77 }
78 78
79 79 const PythonQtMethodInfo* PythonQtMethodInfo::getCachedMethodInfo(const QMetaMethod& signal, PythonQtClassInfo* classInfo)
80 80 {
81 81 QByteArray sig(signal.signature());
82 82 sig = sig.mid(sig.indexOf('('));
83 83 QByteArray fullSig = QByteArray(signal.typeName()) + " " + sig;
84 84 PythonQtMethodInfo* result = _cachedSignatures.value(fullSig);
85 85 if (!result) {
86 86 result = new PythonQtMethodInfo(signal, classInfo);
87 87 _cachedSignatures.insert(fullSig, result);
88 88 }
89 89 return result;
90 90 }
91 91
92 92 const PythonQtMethodInfo* PythonQtMethodInfo::getCachedMethodInfoFromArgumentList(int numArgs, const char** args)
93 93 {
94 94 QByteArray typeName = args[0];
95 95 QList<QByteArray> arguments;
96 96 QByteArray fullSig = typeName;
97 97 fullSig += "(";
98 98 for (int i =1;i<numArgs; i++) {
99 99 if (i>1) {
100 100 fullSig += ",";
101 101 }
102 102 QByteArray arg(args[i]);
103 103 fullSig += arg;
104 104 arguments << arg;
105 105 }
106 106 fullSig += ")";
107 107 PythonQtMethodInfo* result = _cachedSignatures.value(fullSig);
108 108 if (!result) {
109 109 result = new PythonQtMethodInfo(typeName, arguments);
110 110 _cachedSignatures.insert(fullSig, result);
111 111 }
112 112 return result;
113 113 }
114 114
115 115 void PythonQtMethodInfo::fillParameterInfo(ParameterInfo& type, const QByteArray& orgName, PythonQtClassInfo* classInfo)
116 116 {
117 117 QByteArray name = orgName;
118 118
119 119 type.enumWrapper = NULL;
120 120
121 121 int len = name.length();
122 122 if (len>0) {
123 123 if (strncmp(name.constData(), "const ", 6)==0) {
124 124 name = name.mid(6);
125 125 len -= 6;
126 126 type.isConst = true;
127 127 } else {
128 128 type.isConst = false;
129 129 }
130 130 char pointerCount = 0;
131 131 bool hadReference = false;
132 132 // remove * and & from the end of the string, handle & and * the same way
133 133 while (name.at(len-1) == '*') {
134 134 len--;
135 135 pointerCount++;
136 136 }
137 137 while (name.at(len-1) == '&') {
138 138 len--;
139 139 hadReference = true;
140 140 }
141 141 if (len!=name.length()) {
142 142 name = name.left(len);
143 143 }
144 144 type.pointerCount = pointerCount;
145 145
146 146 QByteArray alias = _parameterNameAliases.value(name);
147 147 if (!alias.isEmpty()) {
148 148 name = alias;
149 149 }
150 150
151 151 type.typeId = nameToType(name);
152 152 if ((type.pointerCount == 0) && type.typeId == Unknown) {
153 153 type.typeId = QMetaType::type(name.constData());
154 154 if (type.typeId == QMetaType::Void) {
155 155 type.typeId = Unknown;
156 156 }
157 157 }
158 158 type.name = name;
159 159
160 160 if (type.typeId == PythonQtMethodInfo::Unknown || type.typeId >= QMetaType::User) {
161 161 bool isLocalEnum;
162 162 // TODOXXX: make use of this flag!
163 163 type.enumWrapper = PythonQtClassInfo::findEnumWrapper(type.name, classInfo, &isLocalEnum);
164 164 }
165 165 } else {
166 166 type.typeId = QMetaType::Void;
167 167 type.pointerCount = 0;
168 168 type.isConst = false;
169 169 }
170 170 }
171 171
172 172 int PythonQtMethodInfo::nameToType(const char* name)
173 173 {
174 174 if (_parameterTypeDict.isEmpty()) {
175 175 // we could also use QMetaType::nameToType, but that does a string compare search
176 176 // and does not support QVariant
177 177
178 178 // QMetaType names
179 179 _parameterTypeDict.insert("long", QMetaType::Long);
180 180 _parameterTypeDict.insert("int", QMetaType::Int);
181 181 _parameterTypeDict.insert("short", QMetaType::Short);
182 182 _parameterTypeDict.insert("char", QMetaType::Char);
183 183 _parameterTypeDict.insert("ulong", QMetaType::ULong);
184 184 _parameterTypeDict.insert("unsigned long", QMetaType::ULong);
185 185 _parameterTypeDict.insert("uint", QMetaType::UInt);
186 186 _parameterTypeDict.insert("unsigned int", QMetaType::UInt);
187 187 _parameterTypeDict.insert("ushort", QMetaType::UShort);
188 188 _parameterTypeDict.insert("unsigned short", QMetaType::UShort);
189 189 _parameterTypeDict.insert("uchar", QMetaType::UChar);
190 190 _parameterTypeDict.insert("unsigned char", QMetaType::UChar);
191 191 _parameterTypeDict.insert("bool", QMetaType::Bool);
192 192 _parameterTypeDict.insert("float", QMetaType::Float);
193 193 _parameterTypeDict.insert("double", QMetaType::Double);
194 194 _parameterTypeDict.insert("qreal", QMetaType::Double);
195 195 _parameterTypeDict.insert("QChar", QMetaType::QChar);
196 196 _parameterTypeDict.insert("QByteArray", QMetaType::QByteArray);
197 197 _parameterTypeDict.insert("QString", QMetaType::QString);
198 198 _parameterTypeDict.insert("", QMetaType::Void);
199 199 _parameterTypeDict.insert("void", QMetaType::Void);
200 200
201 201 // GL types
202 202 _parameterTypeDict.insert("GLenum", QMetaType::UInt);
203 203 _parameterTypeDict.insert("GLboolean", QMetaType::UChar);
204 204 _parameterTypeDict.insert("GLbitfield", QMetaType::UInt);
205 205 _parameterTypeDict.insert("GLbyte", QMetaType::Char);
206 206 _parameterTypeDict.insert("GLubyte", QMetaType::UChar);
207 207 _parameterTypeDict.insert("GLshort", QMetaType::Short);
208 208 _parameterTypeDict.insert("GLushort", QMetaType::UShort);
209 209 _parameterTypeDict.insert("GLint", QMetaType::Int);
210 210 _parameterTypeDict.insert("GLuint", QMetaType::UInt);
211 211 _parameterTypeDict.insert("GLsizei", QMetaType::UInt);
212 212 _parameterTypeDict.insert("GLclampf", QMetaType::Float);
213 213 _parameterTypeDict.insert("GLfloat", QMetaType::Float);
214 214 _parameterTypeDict.insert("GLclampd", QMetaType::Double);
215 215 _parameterTypeDict.insert("GLdouble", QMetaType::Double);
216 216 _parameterTypeDict.insert("GLvoid", QMetaType::Void);
217 217 if (QT_POINTER_SIZE == 8) {
218 218 _parameterTypeDict.insert("qgl_GLintptr", QMetaType::LongLong);
219 219 _parameterTypeDict.insert("qgl_GLsizeiptr", QMetaType::LongLong);
220 220 } else {
221 221 _parameterTypeDict.insert("qgl_GLintptr", QMetaType::Int);
222 222 _parameterTypeDict.insert("qgl_GLsizeiptr", QMetaType::Int);
223 223 }
224 224
225 225 // QVariant names
226 226 _parameterTypeDict.insert("Q_LLONG", QMetaType::LongLong);
227 227 _parameterTypeDict.insert("Q_ULLONG", QMetaType::ULongLong);
228 228 _parameterTypeDict.insert("qlonglong", QMetaType::LongLong);
229 229 _parameterTypeDict.insert("qulonglong", QMetaType::ULongLong);
230 230 _parameterTypeDict.insert("qint64", QMetaType::LongLong);
231 231 _parameterTypeDict.insert("quint64", QMetaType::ULongLong);
232 232 _parameterTypeDict.insert("QVariantMap", QMetaType::QVariantMap);
233 233 _parameterTypeDict.insert("QVariantList", QMetaType::QVariantList);
234 234 _parameterTypeDict.insert("QMap<QString,QVariant>", QMetaType::QVariantMap);
235 235 _parameterTypeDict.insert("QList<QVariant>", QMetaType::QVariantList);
236 236 _parameterTypeDict.insert("QStringList", QMetaType::QStringList);
237 237 _parameterTypeDict.insert("QBitArray", QMetaType::QBitArray);
238 238 _parameterTypeDict.insert("QDate", QMetaType::QDate);
239 239 _parameterTypeDict.insert("QTime", QMetaType::QTime);
240 240 _parameterTypeDict.insert("QDateTime", QMetaType::QDateTime);
241 241 _parameterTypeDict.insert("QUrl", QMetaType::QUrl);
242 242 _parameterTypeDict.insert("QLocale", QMetaType::QLocale);
243 243 _parameterTypeDict.insert("QRect", QMetaType::QRect);
244 244 _parameterTypeDict.insert("QRectF", QMetaType::QRectF);
245 245 _parameterTypeDict.insert("QSize", QMetaType::QSize);
246 246 _parameterTypeDict.insert("QSizeF", QMetaType::QSizeF);
247 247 _parameterTypeDict.insert("QLine", QMetaType::QLine);
248 248 _parameterTypeDict.insert("QLineF", QMetaType::QLineF);
249 249 _parameterTypeDict.insert("QPoint", QMetaType::QPoint);
250 250 _parameterTypeDict.insert("QPointF", QMetaType::QPointF);
251 251 _parameterTypeDict.insert("QRegExp", QMetaType::QRegExp);
252 252 _parameterTypeDict.insert("QFont", QMetaType::QFont);
253 253 _parameterTypeDict.insert("QPixmap", QMetaType::QPixmap);
254 254 _parameterTypeDict.insert("QBrush", QMetaType::QBrush);
255 255 _parameterTypeDict.insert("QColor", QMetaType::QColor);
256 256 _parameterTypeDict.insert("QCursor", QMetaType::QCursor);
257 257 _parameterTypeDict.insert("QPalette", QMetaType::QPalette);
258 258 _parameterTypeDict.insert("QIcon", QMetaType::QIcon);
259 259 _parameterTypeDict.insert("QImage", QMetaType::QImage);
260 260 _parameterTypeDict.insert("QRegion", QMetaType::QRegion);
261 261 _parameterTypeDict.insert("QBitmap", QMetaType::QBitmap);
262 262 _parameterTypeDict.insert("QSizePolicy", QMetaType::QSizePolicy);
263 263 _parameterTypeDict.insert("QKeySequence", QMetaType::QKeySequence);
264 264 _parameterTypeDict.insert("QPen", QMetaType::QPen);
265 265 _parameterTypeDict.insert("QTextLength", QMetaType::QTextLength);
266 266 _parameterTypeDict.insert("QTextFormat", QMetaType::QTextFormat);
267 267 _parameterTypeDict.insert("QMatrix", QMetaType::QMatrix);
268 268 _parameterTypeDict.insert("QVariant", PythonQtMethodInfo::Variant);
269 269 // own special types... (none so far, could be e.g. ObjectList
270 270 }
271 271 QHash<QByteArray, int>::const_iterator it = _parameterTypeDict.find(name);
272 272 if (it!=_parameterTypeDict.end()) {
273 273 return it.value();
274 274 } else {
275 275 return PythonQtMethodInfo::Unknown;
276 276 }
277 277 }
278 278
279 279 void PythonQtMethodInfo::cleanupCachedMethodInfos()
280 280 {
281 281 QHashIterator<QByteArray, PythonQtMethodInfo *> i(_cachedSignatures);
282 282 while (i.hasNext()) {
283 283 delete i.next().value();
284 284 }
285 _cachedSignatures.clear();
285 286 }
286 287
287 288 void PythonQtMethodInfo::addParameterTypeAlias(const QByteArray& alias, const QByteArray& name)
288 289 {
289 290 _parameterNameAliases.insert(alias, name);
290 291 }
291 292
292 293 //-------------------------------------------------------------------------------------------------
293 294
294 295 void PythonQtSlotInfo::deleteOverloadsAndThis()
295 296 {
296 297 PythonQtSlotInfo* cur = this;
297 298 while(cur->nextInfo()) {
298 299 PythonQtSlotInfo* next = cur->nextInfo();
299 300 delete cur;
300 301 cur = next;
301 302 }
302 303 }
303 304
304 305
305 306 QString PythonQtSlotInfo::fullSignature()
306 307 {
307 308 bool skipFirstArg = isInstanceDecorator();
308 309 QString result = _meta.typeName();
309 310 QByteArray sig = slotName();
310 311 QList<QByteArray> names = _meta.parameterNames();
311 312
312 313 bool isStatic = false;
313 314 bool isConstructor = false;
314 315 bool isDestructor = false;
315 316
316 317 if (_type == ClassDecorator) {
317 318 if (sig.startsWith("new_")) {
318 319 sig = sig.mid(4);
319 320 isConstructor = true;
320 321 } else if (sig.startsWith("delete_")) {
321 322 sig = sig.mid(7);
322 323 isDestructor = true;
323 324 } else if(sig.startsWith("static_")) {
324 325 isStatic = true;
325 326 sig = sig.mid(7);
326 327 int idx = sig.indexOf("_");
327 328 if (idx>=0) {
328 329 sig = sig.mid(idx+1);
329 330 }
330 331 }
331 332 }
332 333
333 334 result += QByteArray(" ") + sig;
334 335 result += "(";
335 336
336 337 int lastEntry = _parameters.count()-1;
337 338 for (int i = skipFirstArg?2:1; i<_parameters.count(); i++) {
338 339 if (_parameters.at(i).isConst) {
339 340 result += "const ";
340 341 }
341 342 result += _parameters.at(i).name;
342 343 if (_parameters.at(i).pointerCount) {
343 344 QByteArray stars;
344 345 stars.fill('*', _parameters.at(i).pointerCount);
345 346 result += stars;
346 347 }
347 348 if (!names.at(i-1).isEmpty()) {
348 349 result += " ";
349 350 result += names.at(i-1);
350 351 }
351 352 if (i!=lastEntry) {
352 353 result += ", ";
353 354 }
354 355 }
355 356 result += ")";
356 357
357 358 if (isStatic) {
358 359 result = QString("static ") + result;
359 360 }
360 361 if (isConstructor) {
361 362 // result = QString("constructor ") + result;
362 363 }
363 364 if (isDestructor) {
364 365 result = QString("~") + result;
365 366 }
366 367 return result;
367 368 }
368 369
369 370
370 371 QByteArray PythonQtSlotInfo::slotName()
371 372 {
372 373 QByteArray sig(_meta.signature());
373 374 int idx = sig.indexOf('(');
374 375 sig = sig.left(idx);
375 376 return sig;
376 377 }
377 378
General Comments 0
You need to be logged in to leave comments. Login now