##// END OF EJS Templates
added destructor creation for shells...
florianlink -
r178:12aa9315d296
parent child
Show More
@@ -1,312 +1,314
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
3 ** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
6 **
7 ** This file is part of the Qt Script Generator project on Qt Labs.
7 ** This file is part of the Qt Script Generator project on Qt Labs.
8 **
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
14 ** this package.
15 **
15 **
16 ** GNU Lesser General Public License Usage
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
27 **
28 ** If you have questions regarding the use of this file, please contact
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
29 ** Nokia at qt-info@nokia.com.
30 **
30 **
31 **
31 **
32 **
32 **
33 **
33 **
34 **
34 **
35 **
35 **
36 **
36 **
37 **
37 **
38 ** $QT_END_LICENSE$
38 ** $QT_END_LICENSE$
39 **
39 **
40 ****************************************************************************/
40 ****************************************************************************/
41
41
42 #include "shellheadergenerator.h"
42 #include "shellheadergenerator.h"
43 #include "fileout.h"
43 #include "fileout.h"
44
44
45 #include <QtCore/QDir>
45 #include <QtCore/QDir>
46
46
47 #include <qdebug.h>
47 #include <qdebug.h>
48
48
49 QString ShellHeaderGenerator::fileNameForClass(const AbstractMetaClass *meta_class) const
49 QString ShellHeaderGenerator::fileNameForClass(const AbstractMetaClass *meta_class) const
50 {
50 {
51 return QString("PythonQtWrapper_%1.h").arg(meta_class->name());
51 return QString("PythonQtWrapper_%1.h").arg(meta_class->name());
52 }
52 }
53
53
54
54
55 void ShellHeaderGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *field)
55 void ShellHeaderGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *field)
56 {
56 {
57 const AbstractMetaFunction *setter = field->setter();
57 const AbstractMetaFunction *setter = field->setter();
58 const AbstractMetaFunction *getter = field->getter();
58 const AbstractMetaFunction *getter = field->getter();
59
59
60 // static fields are not supported (yet?)
60 // static fields are not supported (yet?)
61 if (setter->isStatic()) return;
61 if (setter->isStatic()) return;
62
62
63 // Uuid data4 did not work (TODO: move to typesystem...(
63 // Uuid data4 did not work (TODO: move to typesystem...(
64 if (field->enclosingClass()->name()=="QUuid" && setter->name()=="data4") return;
64 if (field->enclosingClass()->name()=="QUuid" && setter->name()=="data4") return;
65 if (field->enclosingClass()->name()=="QIPv6Address") return;
65 if (field->enclosingClass()->name()=="QIPv6Address") return;
66
66
67 if (!field->type()->isConstant()) {
67 if (!field->type()->isConstant()) {
68 writeFunctionSignature(s, setter, 0, QString(),
68 writeFunctionSignature(s, setter, 0, QString(),
69 Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | ShowStatic | UnderscoreSpaces));
69 Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | ShowStatic | UnderscoreSpaces));
70 s << "{ theWrappedObject->" << field->name() << " = " << setter->arguments()[0]->argumentName() << "; }\n";
70 s << "{ theWrappedObject->" << field->name() << " = " << setter->arguments()[0]->argumentName() << "; }\n";
71 }
71 }
72
72
73 writeFunctionSignature(s, getter, 0, QString(),
73 writeFunctionSignature(s, getter, 0, QString(),
74 Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
74 Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
75 s << "{ return theWrappedObject->" << field->name() << "; }\n";
75 s << "{ return theWrappedObject->" << field->name() << "; }\n";
76 }
76 }
77
77
78 void ShellHeaderGenerator::write(QTextStream &s, const AbstractMetaClass *meta_class)
78 void ShellHeaderGenerator::write(QTextStream &s, const AbstractMetaClass *meta_class)
79 {
79 {
80 QString builtIn = ShellGenerator::isBuiltIn(meta_class->name())?"_builtin":"";
80 QString builtIn = ShellGenerator::isBuiltIn(meta_class->name())?"_builtin":"";
81 QString pro_file_name = meta_class->package().replace(".", "_") + builtIn + "/" + meta_class->package().replace(".", "_") + builtIn + ".pri";
81 QString pro_file_name = meta_class->package().replace(".", "_") + builtIn + "/" + meta_class->package().replace(".", "_") + builtIn + ".pri";
82 priGenerator->addHeader(pro_file_name, fileNameForClass(meta_class));
82 priGenerator->addHeader(pro_file_name, fileNameForClass(meta_class));
83 setupGenerator->addClass(meta_class->package().replace(".", "_") + builtIn, meta_class);
83 setupGenerator->addClass(meta_class->package().replace(".", "_") + builtIn, meta_class);
84
84
85 QString include_block = "PYTHONQTWRAPPER_" + meta_class->name().toUpper() + "_H";
85 QString include_block = "PYTHONQTWRAPPER_" + meta_class->name().toUpper() + "_H";
86
86
87 s << "#ifndef " << include_block << endl
87 s << "#ifndef " << include_block << endl
88 << "#define " << include_block << endl << endl;
88 << "#define " << include_block << endl << endl;
89
89
90 Include inc = meta_class->typeEntry()->include();
90 Include inc = meta_class->typeEntry()->include();
91 ShellGenerator::writeInclude(s, inc);
91 ShellGenerator::writeInclude(s, inc);
92
92
93 s << "#include <QObject>" << endl << endl;
93 s << "#include <QObject>" << endl << endl;
94 s << "#include <PythonQt.h>" << endl << endl;
94 s << "#include <PythonQt.h>" << endl << endl;
95
95
96 IncludeList list = meta_class->typeEntry()->extraIncludes();
96 IncludeList list = meta_class->typeEntry()->extraIncludes();
97 qSort(list.begin(), list.end());
97 qSort(list.begin(), list.end());
98 foreach (const Include &inc, list) {
98 foreach (const Include &inc, list) {
99 ShellGenerator::writeInclude(s, inc);
99 ShellGenerator::writeInclude(s, inc);
100 }
100 }
101 s << endl;
101 s << endl;
102
102
103 AbstractMetaFunctionList ctors = meta_class->queryFunctions(AbstractMetaClass::Constructors
103 AbstractMetaFunctionList ctors = meta_class->queryFunctions(AbstractMetaClass::Constructors
104 | AbstractMetaClass::WasVisible
104 | AbstractMetaClass::WasVisible
105 | AbstractMetaClass::NotRemovedFromTargetLang);
105 | AbstractMetaClass::NotRemovedFromTargetLang);
106
106
107 // Shell-------------------------------------------------------------------
107 // Shell-------------------------------------------------------------------
108 if (meta_class->generateShellClass()) {
108 if (meta_class->generateShellClass()) {
109
109
110 AbstractMetaFunctionList virtualsForShell = getVirtualFunctionsForShell(meta_class);
110 AbstractMetaFunctionList virtualsForShell = getVirtualFunctionsForShell(meta_class);
111
111
112 s << "class " << shellClassName(meta_class)
112 s << "class " << shellClassName(meta_class)
113 << " : public " << meta_class->qualifiedCppName() << endl << "{" << endl;
113 << " : public " << meta_class->qualifiedCppName() << endl << "{" << endl;
114 s << "public:" << endl;
114 s << "public:" << endl;
115 foreach(AbstractMetaFunction* fun, ctors) {
115 foreach(AbstractMetaFunction* fun, ctors) {
116 s << " ";
116 s << " ";
117 writeFunctionSignature(s, fun, 0,"PythonQtShell_",
117 writeFunctionSignature(s, fun, 0,"PythonQtShell_",
118 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
118 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
119 s << ":" << meta_class->qualifiedCppName() << "(";
119 s << ":" << meta_class->qualifiedCppName() << "(";
120 QString scriptFunctionName = fun->originalName();
120 QString scriptFunctionName = fun->originalName();
121 AbstractMetaArgumentList args = fun->arguments();
121 AbstractMetaArgumentList args = fun->arguments();
122 for (int i = 0; i < args.size(); ++i) {
122 for (int i = 0; i < args.size(); ++i) {
123 if (i > 0)
123 if (i > 0)
124 s << ", ";
124 s << ", ";
125 s << args.at(i)->argumentName();
125 s << args.at(i)->argumentName();
126 }
126 }
127 s << "),_wrapper(NULL) {};" << endl;
127 s << "),_wrapper(NULL) {};" << endl;
128 }
128 }
129 s << endl;
129 s << endl;
130 s << " ~" << shellClassName(meta_class) << "();" << endl;
131 s << endl;
130
132
131 foreach(AbstractMetaFunction* fun, virtualsForShell) {
133 foreach(AbstractMetaFunction* fun, virtualsForShell) {
132 s << "virtual ";
134 s << "virtual ";
133 writeFunctionSignature(s, fun, 0, QString(),
135 writeFunctionSignature(s, fun, 0, QString(),
134 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
136 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
135 s << ";" << endl;
137 s << ";" << endl;
136 }
138 }
137 s << endl;
139 s << endl;
138 s << " PythonQtInstanceWrapper* _wrapper; " << endl;
140 s << " PythonQtInstanceWrapper* _wrapper; " << endl;
139
141
140 s << "};" << endl << endl;
142 s << "};" << endl << endl;
141 }
143 }
142
144
143 // Promoter-------------------------------------------------------------------
145 // Promoter-------------------------------------------------------------------
144 AbstractMetaFunctionList promoteFunctions = getProtectedFunctionsThatNeedPromotion(meta_class);
146 AbstractMetaFunctionList promoteFunctions = getProtectedFunctionsThatNeedPromotion(meta_class);
145 if (!promoteFunctions.isEmpty()) {
147 if (!promoteFunctions.isEmpty()) {
146 s << "class " << promoterClassName(meta_class)
148 s << "class " << promoterClassName(meta_class)
147 << " : public " << meta_class->qualifiedCppName() << endl << "{ public:" << endl;
149 << " : public " << meta_class->qualifiedCppName() << endl << "{ public:" << endl;
148
150
149 foreach(AbstractMetaFunction* fun, promoteFunctions) {
151 foreach(AbstractMetaFunction* fun, promoteFunctions) {
150 s << "inline ";
152 s << "inline ";
151 writeFunctionSignature(s, fun, 0, "promoted_",
153 writeFunctionSignature(s, fun, 0, "promoted_",
152 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
154 Option(IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
153 s << " { ";
155 s << " { ";
154 QString scriptFunctionName = fun->originalName();
156 QString scriptFunctionName = fun->originalName();
155 AbstractMetaArgumentList args = fun->arguments();
157 AbstractMetaArgumentList args = fun->arguments();
156 if (fun->type())
158 if (fun->type())
157 s << "return ";
159 s << "return ";
158 s << meta_class->qualifiedCppName() << "::";
160 s << meta_class->qualifiedCppName() << "::";
159 s << fun->originalName() << "(";
161 s << fun->originalName() << "(";
160 for (int i = 0; i < args.size(); ++i) {
162 for (int i = 0; i < args.size(); ++i) {
161 if (i > 0)
163 if (i > 0)
162 s << ", ";
164 s << ", ";
163 s << args.at(i)->argumentName();
165 s << args.at(i)->argumentName();
164 }
166 }
165 s << "); }" << endl;
167 s << "); }" << endl;
166 }
168 }
167
169
168 s << "};" << endl << endl;
170 s << "};" << endl << endl;
169 }
171 }
170
172
171 // Wrapper-------------------------------------------------------------------
173 // Wrapper-------------------------------------------------------------------
172
174
173 s << "class " << wrapperClassName(meta_class)
175 s << "class " << wrapperClassName(meta_class)
174 << " : public QObject" << endl
176 << " : public QObject" << endl
175 << "{ Q_OBJECT" << endl;
177 << "{ Q_OBJECT" << endl;
176
178
177 s << "public:" << endl;
179 s << "public:" << endl;
178
180
179 AbstractMetaEnumList enums1 = meta_class->enums();
181 AbstractMetaEnumList enums1 = meta_class->enums();
180 AbstractMetaEnumList enums;
182 AbstractMetaEnumList enums;
181 QList<FlagsTypeEntry*> flags;
183 QList<FlagsTypeEntry*> flags;
182 foreach(AbstractMetaEnum* enum1, enums1) {
184 foreach(AbstractMetaEnum* enum1, enums1) {
183 // catch gadgets and enums that are not exported on QObjects...
185 // catch gadgets and enums that are not exported on QObjects...
184 if (enum1->wasPublic() && (!meta_class->isQObject() || !enum1->hasQEnumsDeclaration())) {
186 if (enum1->wasPublic() && (!meta_class->isQObject() || !enum1->hasQEnumsDeclaration())) {
185 enums << enum1;
187 enums << enum1;
186 if (enum1->typeEntry()->flags()) {
188 if (enum1->typeEntry()->flags()) {
187 flags << enum1->typeEntry()->flags();
189 flags << enum1->typeEntry()->flags();
188 }
190 }
189 }
191 }
190 }
192 }
191 if (enums.count()) {
193 if (enums.count()) {
192 s << "Q_ENUMS(";
194 s << "Q_ENUMS(";
193 foreach(AbstractMetaEnum* enum1, enums) {
195 foreach(AbstractMetaEnum* enum1, enums) {
194 s << enum1->name() << " ";
196 s << enum1->name() << " ";
195 }
197 }
196 s << ")" << endl;
198 s << ")" << endl;
197
199
198 if (flags.count()) {
200 if (flags.count()) {
199 s << "Q_FLAGS(";
201 s << "Q_FLAGS(";
200 foreach(FlagsTypeEntry* flag1, flags) {
202 foreach(FlagsTypeEntry* flag1, flags) {
201 QString origName = flag1->originalName();
203 QString origName = flag1->originalName();
202 int idx = origName.lastIndexOf("::");
204 int idx = origName.lastIndexOf("::");
203 if (idx!= -1) {
205 if (idx!= -1) {
204 origName = origName.mid(idx+2);
206 origName = origName.mid(idx+2);
205 }
207 }
206 s << origName << " ";
208 s << origName << " ";
207 }
209 }
208 s << ")" << endl;
210 s << ")" << endl;
209 }
211 }
210
212
211 foreach(AbstractMetaEnum* enum1, enums) {
213 foreach(AbstractMetaEnum* enum1, enums) {
212 s << "enum " << enum1->name() << "{" << endl;
214 s << "enum " << enum1->name() << "{" << endl;
213 bool first = true;
215 bool first = true;
214 foreach(AbstractMetaEnumValue* value, enum1->values()) {
216 foreach(AbstractMetaEnumValue* value, enum1->values()) {
215 if (first) { first = false; }
217 if (first) { first = false; }
216 else { s << ", "; }
218 else { s << ", "; }
217 s << " " << value->name() << " = " << meta_class->qualifiedCppName() << "::" << value->name();
219 s << " " << value->name() << " = " << meta_class->qualifiedCppName() << "::" << value->name();
218 }
220 }
219 s << "};" << endl;
221 s << "};" << endl;
220 }
222 }
221 if (flags.count()) {
223 if (flags.count()) {
222 foreach(AbstractMetaEnum* enum1, enums) {
224 foreach(AbstractMetaEnum* enum1, enums) {
223 if (enum1->typeEntry()->flags()) {
225 if (enum1->typeEntry()->flags()) {
224 QString origName = enum1->typeEntry()->flags()->originalName();
226 QString origName = enum1->typeEntry()->flags()->originalName();
225 int idx = origName.lastIndexOf("::");
227 int idx = origName.lastIndexOf("::");
226 if (idx!= -1) {
228 if (idx!= -1) {
227 origName = origName.mid(idx+2);
229 origName = origName.mid(idx+2);
228 }
230 }
229 s << "Q_DECLARE_FLAGS("<< origName << ", " << enum1->name() <<")"<<endl;
231 s << "Q_DECLARE_FLAGS("<< origName << ", " << enum1->name() <<")"<<endl;
230 }
232 }
231 }
233 }
232 }
234 }
233 }
235 }
234 s << "public slots:" << endl;
236 s << "public slots:" << endl;
235 if (meta_class->generateShellClass() || !meta_class->isAbstract()) {
237 if (meta_class->generateShellClass() || !meta_class->isAbstract()) {
236
238
237 bool copyConstructorSeen = false;
239 bool copyConstructorSeen = false;
238 bool defaultConstructorSeen = false;
240 bool defaultConstructorSeen = false;
239 foreach (const AbstractMetaFunction *fun, ctors) {
241 foreach (const AbstractMetaFunction *fun, ctors) {
240 if (!fun->isPublic() || fun->isAbstract()) { continue; }
242 if (!fun->isPublic() || fun->isAbstract()) { continue; }
241 s << meta_class->qualifiedCppName() << "* ";
243 s << meta_class->qualifiedCppName() << "* ";
242 writeFunctionSignature(s, fun, 0, "new_",
244 writeFunctionSignature(s, fun, 0, "new_",
243 Option(IncludeDefaultExpression | OriginalName | ShowStatic));
245 Option(IncludeDefaultExpression | OriginalName | ShowStatic));
244 s << ";" << endl;
246 s << ";" << endl;
245 if (fun->arguments().size()==1 && meta_class->qualifiedCppName() == fun->arguments().at(0)->type()->typeEntry()->qualifiedCppName()) {
247 if (fun->arguments().size()==1 && meta_class->qualifiedCppName() == fun->arguments().at(0)->type()->typeEntry()->qualifiedCppName()) {
246 copyConstructorSeen = true;
248 copyConstructorSeen = true;
247 }
249 }
248 if (fun->arguments().size()==0) {
250 if (fun->arguments().size()==0) {
249 defaultConstructorSeen = true;
251 defaultConstructorSeen = true;
250 }
252 }
251 }
253 }
252
254
253 if (meta_class->typeEntry()->isValue()
255 if (meta_class->typeEntry()->isValue()
254 && !copyConstructorSeen && defaultConstructorSeen) {
256 && !copyConstructorSeen && defaultConstructorSeen) {
255 QString className = meta_class->generateShellClass()?shellClassName(meta_class):meta_class->qualifiedCppName();
257 QString className = meta_class->generateShellClass()?shellClassName(meta_class):meta_class->qualifiedCppName();
256 s << meta_class->qualifiedCppName() << "* new_" << meta_class->name() << "(const " << meta_class->qualifiedCppName() << "& other) {" << endl;
258 s << meta_class->qualifiedCppName() << "* new_" << meta_class->name() << "(const " << meta_class->qualifiedCppName() << "& other) {" << endl;
257 s << className << "* a = new " << className << "();" << endl;
259 s << className << "* a = new " << className << "();" << endl;
258 s << "*((" << meta_class->qualifiedCppName() << "*)a) = other;" << endl;
260 s << "*((" << meta_class->qualifiedCppName() << "*)a) = other;" << endl;
259 s << "return a; }" << endl;
261 s << "return a; }" << endl;
260 }
262 }
261 }
263 }
262 if (meta_class->hasPublicDestructor() && !meta_class->isNamespace()) {
264 if (meta_class->hasPublicDestructor() && !meta_class->isNamespace()) {
263 s << "void delete_" << meta_class->name() << "(" << meta_class->qualifiedCppName() << "* obj) { delete obj; } ";
265 s << "void delete_" << meta_class->name() << "(" << meta_class->qualifiedCppName() << "* obj) { delete obj; } ";
264 s << endl;
266 s << endl;
265 }
267 }
266 if (meta_class->name()=="QTreeWidgetItem") {
268 if (meta_class->name()=="QTreeWidgetItem") {
267 s << "bool py_hasOwner(QTreeWidgetItem* theWrappedObject) { return theWrappedObject->treeWidget()!=NULL || theWrappedObject->parent()!=NULL; }" << endl;
269 s << "bool py_hasOwner(QTreeWidgetItem* theWrappedObject) { return theWrappedObject->treeWidget()!=NULL || theWrappedObject->parent()!=NULL; }" << endl;
268 } else if (meta_class->name()=="QGraphicsItem") {
270 } else if (meta_class->name()=="QGraphicsItem") {
269 s << "bool py_hasOwner(QGraphicsItem* theWrappedObject) { return theWrappedObject->scene()!=NULL || theWrappedObject->parentItem()!=NULL; }" << endl;
271 s << "bool py_hasOwner(QGraphicsItem* theWrappedObject) { return theWrappedObject->scene()!=NULL || theWrappedObject->parentItem()!=NULL; }" << endl;
270 }
272 }
271
273
272 AbstractMetaFunctionList functions = getFunctionsToWrap(meta_class);
274 AbstractMetaFunctionList functions = getFunctionsToWrap(meta_class);
273
275
274 foreach (const AbstractMetaFunction *function, functions) {
276 foreach (const AbstractMetaFunction *function, functions) {
275 if (!function->isSlot() || function->isVirtual()) {
277 if (!function->isSlot() || function->isVirtual()) {
276 s << " ";
278 s << " ";
277 writeFunctionSignature(s, function, 0, QString(),
279 writeFunctionSignature(s, function, 0, QString(),
278 Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
280 Option(ConvertReferenceToPtr | FirstArgIsWrappedObject| IncludeDefaultExpression | OriginalName | ShowStatic | UnderscoreSpaces));
279 s << ";" << endl;
281 s << ";" << endl;
280 }
282 }
281 }
283 }
282 if (meta_class->hasDefaultToStringFunction() || meta_class->hasToStringCapability()) {
284 if (meta_class->hasDefaultToStringFunction() || meta_class->hasToStringCapability()) {
283 s << " QString py_toString(" << meta_class->qualifiedCppName() << "*);" << endl;
285 s << " QString py_toString(" << meta_class->qualifiedCppName() << "*);" << endl;
284 }
286 }
285 if (meta_class->hasDefaultIsNull()) {
287 if (meta_class->hasDefaultIsNull()) {
286 s << " bool __nonzero__(" << meta_class->qualifiedCppName() << "* obj) { return !obj->isNull(); }" << endl;
288 s << " bool __nonzero__(" << meta_class->qualifiedCppName() << "* obj) { return !obj->isNull(); }" << endl;
287 }
289 }
288
290
289 // Field accessors
291 // Field accessors
290 foreach (const AbstractMetaField *field, meta_class->fields()) {
292 foreach (const AbstractMetaField *field, meta_class->fields()) {
291 if (field->isPublic()) {
293 if (field->isPublic()) {
292 writeFieldAccessors(s, field);
294 writeFieldAccessors(s, field);
293 }
295 }
294 }
296 }
295
297
296 writeInjectedCode(s, meta_class);
298 writeInjectedCode(s, meta_class);
297
299
298
300
299 s << "};" << endl << endl
301 s << "};" << endl << endl
300 << "#endif // " << include_block << endl;
302 << "#endif // " << include_block << endl;
301
303
302 }
304 }
303
305
304 void ShellHeaderGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaClass *meta_class)
306 void ShellHeaderGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaClass *meta_class)
305 {
307 {
306 CodeSnipList code_snips = meta_class->typeEntry()->codeSnips();
308 CodeSnipList code_snips = meta_class->typeEntry()->codeSnips();
307 foreach (const CodeSnip &cs, code_snips) {
309 foreach (const CodeSnip &cs, code_snips) {
308 if (cs.language == TypeSystem::PyWrapperDeclaration) {
310 if (cs.language == TypeSystem::PyWrapperDeclaration) {
309 s << cs.code() << endl;
311 s << cs.code() << endl;
310 }
312 }
311 }
313 }
312 }
314 }
@@ -1,318 +1,324
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
3 ** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
6 **
7 ** This file is part of the Qt Script Generator project on Qt Labs.
7 ** This file is part of the Qt Script Generator project on Qt Labs.
8 **
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
14 ** this package.
15 **
15 **
16 ** GNU Lesser General Public License Usage
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
27 **
28 ** If you have questions regarding the use of this file, please contact
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
29 ** Nokia at qt-info@nokia.com.
30 **
30 **
31 **
31 **
32 **
32 **
33 **
33 **
34 **
34 **
35 **
35 **
36 **
36 **
37 **
37 **
38 ** $QT_END_LICENSE$
38 ** $QT_END_LICENSE$
39 **
39 **
40 ****************************************************************************/
40 ****************************************************************************/
41
41
42 #include "shellimplgenerator.h"
42 #include "shellimplgenerator.h"
43 #include "reporthandler.h"
43 #include "reporthandler.h"
44 #include "fileout.h"
44 #include "fileout.h"
45 #include <iostream>
45 #include <iostream>
46
46
47 extern void declareFunctionMetaTypes(QTextStream &stream,
47 extern void declareFunctionMetaTypes(QTextStream &stream,
48 const AbstractMetaFunctionList &functions,
48 const AbstractMetaFunctionList &functions,
49 QSet<QString> &registeredTypeNames);
49 QSet<QString> &registeredTypeNames);
50
50
51 QString ShellImplGenerator::fileNameForClass(const AbstractMetaClass *meta_class) const
51 QString ShellImplGenerator::fileNameForClass(const AbstractMetaClass *meta_class) const
52 {
52 {
53 return QString("PythonQtWrapper_%1.cpp").arg(meta_class->name());
53 return QString("PythonQtWrapper_%1.cpp").arg(meta_class->name());
54 }
54 }
55
55
56 static bool include_less_than(const Include &a, const Include &b)
56 static bool include_less_than(const Include &a, const Include &b)
57 {
57 {
58 return a.name < b.name;
58 return a.name < b.name;
59 }
59 }
60
60
61 static void writeHelperCode(QTextStream &s, const AbstractMetaClass *)
61 static void writeHelperCode(QTextStream &s, const AbstractMetaClass *)
62 {
62 {
63 }
63 }
64
64
65
65
66
66
67 void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_class)
67 void ShellImplGenerator::write(QTextStream &s, const AbstractMetaClass *meta_class)
68 {
68 {
69 QString builtIn = ShellGenerator::isBuiltIn(meta_class->name())?"_builtin":"";
69 QString builtIn = ShellGenerator::isBuiltIn(meta_class->name())?"_builtin":"";
70 QString pro_file_name = meta_class->package().replace(".", "_") + builtIn + "/" + meta_class->package().replace(".", "_") + builtIn + ".pri";
70 QString pro_file_name = meta_class->package().replace(".", "_") + builtIn + "/" + meta_class->package().replace(".", "_") + builtIn + ".pri";
71 priGenerator->addSource(pro_file_name, fileNameForClass(meta_class));
71 priGenerator->addSource(pro_file_name, fileNameForClass(meta_class));
72
72
73 s << "#include \"PythonQtWrapper_" << meta_class->name() << ".h\"" << endl << endl;
73 s << "#include \"PythonQtWrapper_" << meta_class->name() << ".h\"" << endl << endl;
74
74
75 s << "#include <PythonQtSignalReceiver.h>" << endl;
75 s << "#include <PythonQtSignalReceiver.h>" << endl;
76 s << "#include <PythonQtMethodInfo.h>" << endl;
76 s << "#include <PythonQtMethodInfo.h>" << endl;
77 s << "#include <PythonQtConversion.h>" << endl;
77 s << "#include <PythonQtConversion.h>" << endl;
78
78
79 //if (!meta_class->generateShellClass())
79 //if (!meta_class->generateShellClass())
80 // return;
80 // return;
81
81
82 IncludeList list = meta_class->typeEntry()->extraIncludes();
82 IncludeList list = meta_class->typeEntry()->extraIncludes();
83 qSort(list.begin(), list.end());
83 qSort(list.begin(), list.end());
84 foreach (const Include &inc, list) {
84 foreach (const Include &inc, list) {
85 ShellGenerator::writeInclude(s, inc);
85 ShellGenerator::writeInclude(s, inc);
86 }
86 }
87 s << endl;
87 s << endl;
88
88
89 writeHelperCode(s, meta_class);
89 writeHelperCode(s, meta_class);
90
90
91 // find constructors
91 // find constructors
92 AbstractMetaFunctionList ctors;
92 AbstractMetaFunctionList ctors;
93 ctors = meta_class->queryFunctions(AbstractMetaClass::Constructors
93 ctors = meta_class->queryFunctions(AbstractMetaClass::Constructors
94 | AbstractMetaClass::WasVisible
94 | AbstractMetaClass::WasVisible
95 | AbstractMetaClass::NotRemovedFromTargetLang);
95 | AbstractMetaClass::NotRemovedFromTargetLang);
96 // find member functions
96 // find member functions
97 AbstractMetaFunctionList functions = getFunctionsToWrap(meta_class);
97 AbstractMetaFunctionList functions = getFunctionsToWrap(meta_class);
98
98
99 // write metatype declarations
99 // write metatype declarations
100 {
100 {
101 // QSet<QString> registeredTypeNames = m_qmetatype_declared_typenames;
101 // QSet<QString> registeredTypeNames = m_qmetatype_declared_typenames;
102 // declareFunctionMetaTypes(s, functions, registeredTypeNames);
102 // declareFunctionMetaTypes(s, functions, registeredTypeNames);
103 // s << endl;
103 // s << endl;
104 }
104 }
105
105
106 if (meta_class->generateShellClass()) {
106 if (meta_class->generateShellClass()) {
107
108 s << shellClassName(meta_class) << "::~" << shellClassName(meta_class) << "() {" << endl;
109 s << " PythonQtPrivate* priv = PythonQt::priv();" << endl;
110 s << " if (priv) { priv->shellClassDeleted(this); }" << endl;
111 s << "}" << endl;
112
107 AbstractMetaFunctionList virtualsForShell = getVirtualFunctionsForShell(meta_class);
113 AbstractMetaFunctionList virtualsForShell = getVirtualFunctionsForShell(meta_class);
108 foreach (const AbstractMetaFunction *fun, virtualsForShell) {
114 foreach (const AbstractMetaFunction *fun, virtualsForShell) {
109 bool hasReturnValue = (fun->type());
115 bool hasReturnValue = (fun->type());
110 writeFunctionSignature(s, fun, meta_class, QString(),
116 writeFunctionSignature(s, fun, meta_class, QString(),
111 Option(OriginalName | ShowStatic | UnderscoreSpaces),
117 Option(OriginalName | ShowStatic | UnderscoreSpaces),
112 "PythonQtShell_");
118 "PythonQtShell_");
113 s << endl << "{" << endl;
119 s << endl << "{" << endl;
114
120
115 Option typeOptions = Option(OriginalName | UnderscoreSpaces | SkipName);
121 Option typeOptions = Option(OriginalName | UnderscoreSpaces | SkipName);
116 AbstractMetaArgumentList args = fun->arguments();
122 AbstractMetaArgumentList args = fun->arguments();
117
123
118 s << "if (_wrapper) {" << endl;
124 s << "if (_wrapper) {" << endl;
119 s << " PyObject* obj = PyObject_GetAttrString((PyObject*)_wrapper, \"" << fun->name() << "\");" << endl;
125 s << " PyObject* obj = PyObject_GetAttrString((PyObject*)_wrapper, \"" << fun->name() << "\");" << endl;
120 s << " PyErr_Clear();" << endl;
126 s << " PyErr_Clear();" << endl;
121 s << " if (obj && !PythonQtSlotFunction_Check(obj)) {" << endl;
127 s << " if (obj && !PythonQtSlotFunction_Check(obj)) {" << endl;
122 s << " static const char* argumentList[] ={\"";
128 s << " static const char* argumentList[] ={\"";
123 if (hasReturnValue) {
129 if (hasReturnValue) {
124 // write the arguments, return type first
130 // write the arguments, return type first
125 writeTypeInfo(s, fun->type(), typeOptions);
131 writeTypeInfo(s, fun->type(), typeOptions);
126 }
132 }
127 s << "\"";
133 s << "\"";
128 for (int i = 0; i < args.size(); ++i) {
134 for (int i = 0; i < args.size(); ++i) {
129 s << " , \"";
135 s << " , \"";
130 writeTypeInfo(s, args.at(i)->type(), typeOptions);
136 writeTypeInfo(s, args.at(i)->type(), typeOptions);
131 s << "\"";
137 s << "\"";
132 }
138 }
133 s << "};" << endl;
139 s << "};" << endl;
134 s << " static const PythonQtMethodInfo* methodInfo = PythonQtMethodInfo::getCachedMethodInfoFromArgumentList(" << QString::number(args.size()+1) << ", argumentList);" << endl;
140 s << " static const PythonQtMethodInfo* methodInfo = PythonQtMethodInfo::getCachedMethodInfoFromArgumentList(" << QString::number(args.size()+1) << ", argumentList);" << endl;
135
141
136 if (hasReturnValue) {
142 if (hasReturnValue) {
137 s << " ";
143 s << " ";
138 writeTypeInfo(s, fun->type(), typeOptions);
144 writeTypeInfo(s, fun->type(), typeOptions);
139 s << " returnValue;" << endl;
145 s << " returnValue;" << endl;
140 // TODO: POD init to default is missing...
146 // TODO: POD init to default is missing...
141 }
147 }
142 s << " void* args[" << QString::number(args.size()+1) << "] = {NULL";
148 s << " void* args[" << QString::number(args.size()+1) << "] = {NULL";
143 for (int i = 0; i < args.size(); ++i) {
149 for (int i = 0; i < args.size(); ++i) {
144 s << ", (void*)&" << args.at(i)->argumentName();
150 s << ", (void*)&" << args.at(i)->argumentName();
145 }
151 }
146 s << "};" << endl;
152 s << "};" << endl;
147
153
148 s << " PyObject* result = PythonQtSignalTarget::call(obj, methodInfo, args, true);" << endl;
154 s << " PyObject* result = PythonQtSignalTarget::call(obj, methodInfo, args, true);" << endl;
149 if (hasReturnValue) {
155 if (hasReturnValue) {
150 s << " if (result) {" << endl;
156 s << " if (result) {" << endl;
151 s << " args[0] = PythonQtConv::ConvertPythonToQt(methodInfo->parameters().at(0), result, false, NULL, &returnValue);" << endl;
157 s << " args[0] = PythonQtConv::ConvertPythonToQt(methodInfo->parameters().at(0), result, false, NULL, &returnValue);" << endl;
152 s << " if (args[0]!=&returnValue) {" << endl;
158 s << " if (args[0]!=&returnValue) {" << endl;
153 s << " if (args[0]==NULL) {" << endl;
159 s << " if (args[0]==NULL) {" << endl;
154 s << " PythonQt::priv()->handleVirtualOverloadReturnError(\"" << fun->name() << "\", methodInfo, result);" << endl;
160 s << " PythonQt::priv()->handleVirtualOverloadReturnError(\"" << fun->name() << "\", methodInfo, result);" << endl;
155 s << " } else {" << endl;
161 s << " } else {" << endl;
156 s << " returnValue = *((";
162 s << " returnValue = *((";
157 writeTypeInfo(s, fun->type(), typeOptions);
163 writeTypeInfo(s, fun->type(), typeOptions);
158 s << "*)args[0]);" << endl;
164 s << "*)args[0]);" << endl;
159 s << " }" << endl;
165 s << " }" << endl;
160 s << " }" << endl;
166 s << " }" << endl;
161 s << " }" << endl;
167 s << " }" << endl;
162 }
168 }
163 s << " if (result) { Py_DECREF(result); } " << endl;
169 s << " if (result) { Py_DECREF(result); } " << endl;
164 s << " Py_DECREF(obj);" << endl;
170 s << " Py_DECREF(obj);" << endl;
165 if (hasReturnValue) {
171 if (hasReturnValue) {
166 s << " return returnValue;" << endl;
172 s << " return returnValue;" << endl;
167 } else {
173 } else {
168 s << " return;" << endl;
174 s << " return;" << endl;
169 }
175 }
170 s << " }" << endl;
176 s << " }" << endl;
171 s << "}" << endl;
177 s << "}" << endl;
172
178
173 s << " ";
179 s << " ";
174 if (fun->isAbstract()) {
180 if (fun->isAbstract()) {
175 if (fun->type()) {
181 if (fun->type()) {
176 // return empty default object
182 // return empty default object
177 s << "return ";
183 s << "return ";
178 if (fun->type()->indirections()>0) {
184 if (fun->type()->indirections()>0) {
179 s << "0;";
185 s << "0;";
180 } else {
186 } else {
181 writeTypeInfo(s, fun->type(), typeOptions);
187 writeTypeInfo(s, fun->type(), typeOptions);
182 s << "();";
188 s << "();";
183 }
189 }
184 }
190 }
185 } else {
191 } else {
186 if (fun->type()) {
192 if (fun->type()) {
187 s << "return ";
193 s << "return ";
188 }
194 }
189 s << meta_class->qualifiedCppName() << "::";
195 s << meta_class->qualifiedCppName() << "::";
190 s << fun->originalName() << "(";
196 s << fun->originalName() << "(";
191 for (int i = 0; i < args.size(); ++i) {
197 for (int i = 0; i < args.size(); ++i) {
192 if (i > 0)
198 if (i > 0)
193 s << ", ";
199 s << ", ";
194 s << args.at(i)->argumentName();
200 s << args.at(i)->argumentName();
195 }
201 }
196 s << ");";
202 s << ");";
197 }
203 }
198 s << endl << "}" << endl;
204 s << endl << "}" << endl;
199 }
205 }
200 }
206 }
201
207
202 if (meta_class->generateShellClass() || !meta_class->isAbstract()) {
208 if (meta_class->generateShellClass() || !meta_class->isAbstract()) {
203
209
204 // write constructors
210 // write constructors
205 foreach (const AbstractMetaFunction *ctor, ctors) {
211 foreach (const AbstractMetaFunction *ctor, ctors) {
206 if (!ctor->isPublic() || ctor->isAbstract()) { continue; }
212 if (!ctor->isPublic() || ctor->isAbstract()) { continue; }
207
213
208 s << meta_class->qualifiedCppName() << "* ";
214 s << meta_class->qualifiedCppName() << "* ";
209 s << "PythonQtWrapper_" << meta_class->name() << "::";
215 s << "PythonQtWrapper_" << meta_class->name() << "::";
210 writeFunctionSignature(s, ctor, 0, "new_", Option(OriginalName | ShowStatic));
216 writeFunctionSignature(s, ctor, 0, "new_", Option(OriginalName | ShowStatic));
211 s << endl;
217 s << endl;
212 s << "{ " << endl;
218 s << "{ " << endl;
213 s << "return new " << (meta_class->generateShellClass()?shellClassName(meta_class):meta_class->qualifiedCppName()) << "(";
219 s << "return new " << (meta_class->generateShellClass()?shellClassName(meta_class):meta_class->qualifiedCppName()) << "(";
214 AbstractMetaArgumentList args = ctor->arguments();
220 AbstractMetaArgumentList args = ctor->arguments();
215 for (int i = 0; i < args.size(); ++i) {
221 for (int i = 0; i < args.size(); ++i) {
216 if (i > 0)
222 if (i > 0)
217 s << ", ";
223 s << ", ";
218 s << args.at(i)->argumentName();
224 s << args.at(i)->argumentName();
219 }
225 }
220 s << ");" << " }" << endl << endl;
226 s << ");" << " }" << endl << endl;
221 }
227 }
222 }
228 }
223
229
224 QString wrappedObject = " (*theWrappedObject)";
230 QString wrappedObject = " (*theWrappedObject)";
225
231
226 // write member functions
232 // write member functions
227 for (int i = 0; i < functions.size(); ++i) {
233 for (int i = 0; i < functions.size(); ++i) {
228 AbstractMetaFunction *fun = functions.at(i);
234 AbstractMetaFunction *fun = functions.at(i);
229 bool needsWrapping = (!fun->isSlot() || fun->isVirtual());
235 bool needsWrapping = (!fun->isSlot() || fun->isVirtual());
230 if (!needsWrapping) {
236 if (!needsWrapping) {
231 continue;
237 continue;
232 }
238 }
233 writeFunctionSignature(s, fun, meta_class, QString(),
239 writeFunctionSignature(s, fun, meta_class, QString(),
234 Option(ConvertReferenceToPtr | FirstArgIsWrappedObject | OriginalName | ShowStatic | UnderscoreSpaces),
240 Option(ConvertReferenceToPtr | FirstArgIsWrappedObject | OriginalName | ShowStatic | UnderscoreSpaces),
235 "PythonQtWrapper_");
241 "PythonQtWrapper_");
236 s << endl << "{" << endl;
242 s << endl << "{" << endl;
237 s << " ";
243 s << " ";
238 if (ShellGenerator::isSpecialStreamingOperator(fun)) {
244 if (ShellGenerator::isSpecialStreamingOperator(fun)) {
239 s << fun->arguments().at(0)->argumentName();
245 s << fun->arguments().at(0)->argumentName();
240 if (fun->originalName().startsWith("operator>>")) {
246 if (fun->originalName().startsWith("operator>>")) {
241 s << " >> ";
247 s << " >> ";
242 } else {
248 } else {
243 s << " << ";
249 s << " << ";
244 }
250 }
245 s << wrappedObject;
251 s << wrappedObject;
246 } else {
252 } else {
247 QString scriptFunctionName = fun->originalName();
253 QString scriptFunctionName = fun->originalName();
248 AbstractMetaArgumentList args = fun->arguments();
254 AbstractMetaArgumentList args = fun->arguments();
249 // call the C++ implementation
255 // call the C++ implementation
250 if (fun->type()) {
256 if (fun->type()) {
251 s << "return ";
257 s << "return ";
252 // call the C++ implementation
258 // call the C++ implementation
253 if (fun->type()->isReference()) {
259 if (fun->type()->isReference()) {
254 s << "&";
260 s << "&";
255 }
261 }
256 }
262 }
257 s << "(";
263 s << "(";
258 if (scriptFunctionName.startsWith("operator>>")) {
264 if (scriptFunctionName.startsWith("operator>>")) {
259 s << wrappedObject << " >>" << args.at(0)->argumentName();
265 s << wrappedObject << " >>" << args.at(0)->argumentName();
260 } else if (scriptFunctionName.startsWith("operator<<")) {
266 } else if (scriptFunctionName.startsWith("operator<<")) {
261 s << wrappedObject << " <<" << args.at(0)->argumentName();
267 s << wrappedObject << " <<" << args.at(0)->argumentName();
262 } else if (scriptFunctionName.startsWith("operator[]")) {
268 } else if (scriptFunctionName.startsWith("operator[]")) {
263 s << wrappedObject << "[" << args.at(0)->argumentName() << "]";
269 s << wrappedObject << "[" << args.at(0)->argumentName() << "]";
264 } else if (scriptFunctionName.startsWith("operator") && args.size()==1) {
270 } else if (scriptFunctionName.startsWith("operator") && args.size()==1) {
265 QString op = scriptFunctionName.mid(8);
271 QString op = scriptFunctionName.mid(8);
266 s << wrappedObject << op << " " << args.at(0)->argumentName();
272 s << wrappedObject << op << " " << args.at(0)->argumentName();
267 } else {
273 } else {
268 if (fun->isStatic()) {
274 if (fun->isStatic()) {
269 s << meta_class->qualifiedCppName() << "::";
275 s << meta_class->qualifiedCppName() << "::";
270 } else {
276 } else {
271 if (!fun->isPublic() || fun->isVirtual()) {
277 if (!fun->isPublic() || fun->isVirtual()) {
272 s << " ((" << promoterClassName(meta_class) << "*)theWrappedObject)->promoted_";
278 s << " ((" << promoterClassName(meta_class) << "*)theWrappedObject)->promoted_";
273 } else {
279 } else {
274 s << " theWrappedObject->";
280 s << " theWrappedObject->";
275 }
281 }
276 }
282 }
277 s << fun->originalName() << "(";
283 s << fun->originalName() << "(";
278 for (int i = 0; i < args.size(); ++i) {
284 for (int i = 0; i < args.size(); ++i) {
279 if (i > 0)
285 if (i > 0)
280 s << ", ";
286 s << ", ";
281 s << args.at(i)->argumentName();
287 s << args.at(i)->argumentName();
282 }
288 }
283 s << ")";
289 s << ")";
284 }
290 }
285 s << ")";
291 s << ")";
286 }
292 }
287 s << ";" << endl;
293 s << ";" << endl;
288
294
289 s << "}" << endl << endl;
295 s << "}" << endl << endl;
290 }
296 }
291
297
292 if (meta_class->hasDefaultToStringFunction()) {
298 if (meta_class->hasDefaultToStringFunction()) {
293 s << "QString PythonQtWrapper_" << meta_class->name() << "::py_toString(" << meta_class->qualifiedCppName() << "* obj) { return obj->toString(); }" << endl;
299 s << "QString PythonQtWrapper_" << meta_class->name() << "::py_toString(" << meta_class->qualifiedCppName() << "* obj) { return obj->toString(); }" << endl;
294 } else if (meta_class->hasToStringCapability()) {
300 } else if (meta_class->hasToStringCapability()) {
295 FunctionModelItem fun = meta_class->hasToStringCapability();
301 FunctionModelItem fun = meta_class->hasToStringCapability();
296 int indirections = fun->arguments().at(1)->type().indirections();
302 int indirections = fun->arguments().at(1)->type().indirections();
297 QString deref = QLatin1String(indirections == 0 ? "*" : "");
303 QString deref = QLatin1String(indirections == 0 ? "*" : "");
298 s << "QString PythonQtWrapper_" << meta_class->name() << "::py_toString(" << meta_class->qualifiedCppName() << "* obj) {" << endl;
304 s << "QString PythonQtWrapper_" << meta_class->name() << "::py_toString(" << meta_class->qualifiedCppName() << "* obj) {" << endl;
299 s << " QString result;" << endl;
305 s << " QString result;" << endl;
300 s << " QDebug d(&result);" << endl;
306 s << " QDebug d(&result);" << endl;
301 s << " d << " << deref << "obj;" << endl;
307 s << " d << " << deref << "obj;" << endl;
302 s << " return result;" << endl;
308 s << " return result;" << endl;
303 s << "}" << endl << endl;
309 s << "}" << endl << endl;
304 }
310 }
305
311
306 writeInjectedCode(s, meta_class);
312 writeInjectedCode(s, meta_class);
307
313
308 }
314 }
309
315
310 void ShellImplGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaClass *meta_class)
316 void ShellImplGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaClass *meta_class)
311 {
317 {
312 CodeSnipList code_snips = meta_class->typeEntry()->codeSnips();
318 CodeSnipList code_snips = meta_class->typeEntry()->codeSnips();
313 foreach (const CodeSnip &cs, code_snips) {
319 foreach (const CodeSnip &cs, code_snips) {
314 if (cs.language == TypeSystem::PyWrapperCode) {
320 if (cs.language == TypeSystem::PyWrapperCode) {
315 s << cs.code() << endl;
321 s << cs.code() << endl;
316 }
322 }
317 }
323 }
318 }
324 }
General Comments 0
You need to be logged in to leave comments. Login now