Mercurial > projects > qtd
annotate generator/cppimplgenerator.cpp @ 114:547cd31fa12a
CMake: improve of the windows support for ldc.
author | SokoL_SD |
---|---|
date | Wed, 03 Jun 2009 15:53:24 +0000 |
parents | 23e208cef962 |
children | a860544c5ee8 |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved. | |
4 ** | |
5 ** This file is part of Qt Jambi. | |
6 ** | |
7 ** * Commercial Usage | |
8 * Licensees holding valid Qt Commercial licenses may use this file in | |
9 * accordance with the Qt Commercial License Agreement provided with the | |
10 * Software or, alternatively, in accordance with the terms contained in | |
11 * a written agreement between you and Nokia. | |
12 * | |
13 * | |
14 * GNU General Public License Usage | |
15 * Alternatively, this file may be used under the terms of the GNU | |
16 * General Public License versions 2.0 or 3.0 as published by the Free | |
17 * Software Foundation and appearing in the file LICENSE.GPL included in | |
18 * the packaging of this file. Please review the following information | |
19 * to ensure GNU General Public Licensing requirements will be met: | |
20 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and | |
21 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special | |
22 * exception, Nokia gives you certain additional rights. These rights | |
23 * are described in the Nokia Qt GPL Exception version 1.2, included in | |
24 * the file GPL_EXCEPTION.txt in this package. | |
25 * | |
26 * Qt for Windows(R) Licensees | |
27 * As a special exception, Nokia, as the sole copyright holder for Qt | |
28 * Designer, grants users of the Qt/Eclipse Integration plug-in the | |
29 * right for the Qt/Eclipse Integration to link to functionality | |
30 * provided by Qt Designer and its related libraries. | |
31 * | |
32 * | |
33 * If you are unsure which license is appropriate for your use, please | |
34 * contact the sales department at qt-sales@nokia.com. | |
35 | |
36 ** | |
37 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
38 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
39 ** | |
40 ****************************************************************************/ | |
41 | |
42 #include "cppimplgenerator.h" | |
43 #include "dgenerator.h" | |
44 #include "reporthandler.h" | |
45 | |
46 #include <QDir> | |
47 #include <QtDebug> | |
48 #include <QVariant> | |
49 #include <iostream> | |
50 #define VOID_POINTER_ORDINAL 8 | |
51 | |
52 static Indentor INDENT; | |
53 | |
54 QString jni_signature(const AbstractMetaFunction *function, JNISignatureFormat format) | |
55 { | |
56 QString returned = "("; | |
57 AbstractMetaArgumentList arguments = function->arguments(); | |
58 foreach (const AbstractMetaArgument *argument, arguments) { | |
59 if (!function->argumentRemoved(argument->argumentIndex() + 1)) { | |
60 QString modified_type = function->typeReplaced(argument->argumentIndex()+1); | |
61 | |
62 if (modified_type.isEmpty()) | |
63 returned += jni_signature(argument->type(), format); | |
64 else | |
65 returned += jni_signature(modified_type, format); | |
66 } | |
67 } | |
68 | |
69 returned += ")"; | |
70 | |
71 QString modified_type = function->typeReplaced(0); | |
72 if (modified_type.isEmpty()) | |
73 returned += jni_signature(function->type(), format); | |
74 else | |
75 returned += jni_signature(modified_type, format); | |
76 | |
77 return returned; | |
78 } | |
79 | |
80 QString jni_signature(const QString &_full_name, JNISignatureFormat format) | |
81 { | |
82 QString signature; | |
83 QString full_name = _full_name; | |
84 | |
85 if (full_name.endsWith("[]")) { | |
86 full_name.chop(2); | |
87 signature = "["; | |
88 } | |
89 | |
90 int start = 0, end = -1; | |
91 while ( (start = full_name.indexOf("<")) >= 0 && (end = full_name.indexOf(">")) >= 0 ) { | |
92 full_name.remove(start, end - start + 1); | |
93 } | |
94 | |
95 static QHash<QString, QString> table; | |
96 if (table.isEmpty()) { | |
97 table["boolean"] = "Z"; | |
98 table["byte"] = "B"; | |
99 table["char"] = "C"; | |
100 table["short"] = "S"; | |
101 table["int"] = "I"; | |
102 table["long"] = "J"; | |
103 table["float"] = "F"; | |
104 table["double"] = "D"; | |
105 } | |
106 | |
107 if (format == Underscores) | |
108 signature.replace("[", "_3"); | |
109 | |
110 if (table.contains(full_name)) { | |
111 signature += table[full_name]; | |
112 } else if (format == Underscores) { | |
113 signature.replace("[", "_3"); | |
114 signature += "L"; | |
115 signature += QString(full_name).replace("_", "_1").replace('.', '_').replace("$", "_00024"); | |
116 signature += "_2"; | |
117 } else { | |
118 signature += "L"; | |
119 signature += QString(full_name).replace('.', '/'); | |
120 signature += ";"; | |
121 } | |
122 | |
123 return signature; | |
124 } | |
125 | |
126 QString jni_signature(const AbstractMetaType *java_type, JNISignatureFormat format) | |
127 { | |
128 if (!java_type) | |
129 return "V"; | |
130 | |
131 if (java_type->isArray()) { | |
132 return "_3" + jni_signature(java_type->arrayElementType(), format); | |
133 } else if (java_type->isNativePointer()) { | |
134 if (format == Underscores) | |
135 return "Lcom_trolltech_qt_QNativePointer_2"; | |
136 else | |
137 return "Lcom/trolltech/qt/QNativePointer;"; | |
138 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags() | |
139 || (format == Underscores && (java_type->isEnum() || java_type->isFlags()))) { | |
140 return "I"; | |
141 } else if (java_type->isThread()) { | |
142 if (format == Underscores) | |
143 return "Ljava_lang_Thread_2"; | |
144 else | |
145 return "Ljava/lang/Thread;"; | |
146 } | |
147 | |
148 | |
149 | |
150 QString name = java_type->name(); | |
151 if (java_type->isObject()) { | |
152 if (const InterfaceTypeEntry *ie | |
153 = static_cast<const ObjectTypeEntry *>(java_type->typeEntry())->designatedInterface()) | |
154 name = ie->targetLangName(); | |
155 } else if (java_type->isTargetLangEnum()) { | |
156 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
157 name = et->javaQualifier() + "$" + et->targetLangName(); | |
158 | |
159 } else if (java_type->isTargetLangFlags()) { | |
160 const FlagsTypeEntry *ft = static_cast<const FlagsTypeEntry *>(java_type->typeEntry()); | |
161 name = ft->originator()->javaQualifier() + "$" + ft->targetLangName(); | |
162 } | |
163 | |
164 return jni_signature( (java_type->package().isEmpty() ? QString() : java_type->package() + ".") + name, format); | |
165 } | |
166 | |
167 static QHash<QString, QString> table; | |
168 QString default_return_statement_qt(const AbstractMetaType *java_type, Generator::Option options = Generator::NoOption) | |
169 { | |
170 QString returnStr = ((options & Generator::NoReturnStatement) == 0 ? "return" : ""); | |
171 if (!java_type) | |
172 return returnStr; | |
173 | |
174 if (table.isEmpty()) { | |
175 table["bool"] = "false"; | |
176 table["byte"] = "0"; | |
177 table["char"] = "0"; | |
178 table["short"] = "0"; | |
179 table["int"] = "0"; | |
180 table["long"] = "0"; | |
181 table["float"] = "0f"; | |
182 table["double"] = "0.0"; | |
183 table["java.lang.Object"] = "0"; | |
184 } | |
185 | |
186 QString signature = table.value(java_type->typeEntry()->targetLangName()); | |
187 | |
188 if (!signature.isEmpty()) | |
189 return returnStr + " " + signature; | |
190 | |
191 Q_ASSERT(!java_type->isPrimitive()); | |
192 if (java_type->isJObjectWrapper()) | |
193 return returnStr + " JObjectWrapper()"; | |
194 if (java_type->isVariant()) | |
195 return returnStr + " QVariant()"; | |
196 if (java_type->isTargetLangString()) | |
197 return returnStr + " QString()"; | |
198 if (java_type->isTargetLangChar()) | |
199 return returnStr + " QChar()"; | |
200 else if (java_type->isEnum()) | |
201 return returnStr + " " + java_type->typeEntry()->name() + "(0)"; | |
202 else if (java_type->isContainer() && ((ContainerTypeEntry *)java_type->typeEntry())->type() == ContainerTypeEntry::StringListContainer) | |
203 return returnStr + " " + java_type->typeEntry()->name() + "()"; | |
204 else if (java_type->isValue() || java_type->isContainer()) | |
205 return returnStr + " " + java_type->cppSignature() + "()"; | |
206 else | |
207 return returnStr + " 0"; | |
208 } | |
209 | |
210 QString default_return_statement_java(const AbstractMetaType *java_type) | |
211 { | |
212 if (!java_type) | |
213 return "return"; | |
214 if (java_type->isArray()) | |
215 return "return null"; | |
216 | |
217 if (table.isEmpty()) { | |
218 table["boolean"] = "false"; | |
219 table["byte"] = "0"; | |
220 table["char"] = "0"; | |
221 table["short"] = "0"; | |
222 table["int"] = "0"; | |
223 table["long"] = "0"; | |
224 table["float"] = "0f"; | |
225 table["double"] = "0.0"; | |
226 table["java.lang.Object"] = "0"; | |
227 } | |
228 | |
229 QString signature = table.value(java_type->typeEntry()->targetLangName()); | |
230 if (!signature.isEmpty()) | |
231 return "return " + signature; | |
232 | |
233 Q_ASSERT(!java_type->isPrimitive()); | |
234 return "return 0"; | |
235 } | |
236 | |
237 /* Used to decide how which of the Call[Xxx]Method functions to call | |
238 */ | |
239 QByteArray jniTypeName(const QString &name) { | |
240 static QHash<QString, const char *> table; | |
241 if (table.isEmpty()) { | |
242 table["jboolean"] = "Boolean"; | |
243 table["jbyte"] = "Byte"; | |
244 table["jchar"] = "Char"; | |
245 table["jshort"] = "Short"; | |
246 table["jint"] = "Int"; | |
247 table["jlong"] = "Long"; | |
248 table["jfloat"] = "Float"; | |
249 table["jdouble"] = "Double"; | |
250 table["jobject"] = "Object"; | |
251 } | |
252 | |
253 return table[name]; | |
254 } | |
255 | |
256 QByteArray jniName(const QString &name) { | |
257 TypeEntry *entry = TypeDatabase::instance()->findType(name); | |
258 if (entry) | |
259 return entry->name().toLatin1(); | |
260 else | |
261 return "void *"; | |
262 } | |
263 | |
264 QString CppImplGenerator::jniReturnName(const AbstractMetaFunction *java_function, uint options, bool d_export) | |
265 { | |
20 | 266 AbstractMetaType *f_type = java_function->type(); |
267 | |
268 QString return_type = translateType(f_type, EnumAsInts, d_export); | |
1 | 269 QString new_return_type = java_function->typeReplaced(0); |
270 if (!new_return_type.isEmpty()) { | |
20 | 271 if(f_type && f_type->isPrimitive()) { |
272 if (d_export) | |
273 return_type = new_return_type; | |
274 } else | |
275 return_type = jniName(new_return_type); | |
1 | 276 } |
277 | |
278 // qtd | |
279 if (f_type) { | |
81 | 280 if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 281 return_type = "void"; |
282 else if (f_type->isObject() || f_type->isReference() || f_type->isValue() || f_type->isQObject()) | |
283 return_type = "void*"; | |
284 if (f_type->isVariant()) | |
285 return_type = "void*"; | |
286 } | |
287 | |
288 if (options & CppImplGenerator::ExternC && java_function->isConstructor()) | |
289 return_type = "void*"; | |
290 if (options & CppImplGenerator::ExternC && f_type) | |
291 if (f_type->isTargetLangString() || f_type->typeEntry()->isContainer()) | |
292 return_type = "void"; | |
293 // qtd end | |
294 | |
295 return return_type; | |
296 } | |
297 | |
298 QString CppImplGenerator::jniReturnType(const AbstractMetaType *f_type, uint options) | |
299 { | |
300 QString return_type = translateType(f_type, EnumAsInts); | |
301 | |
302 // qtd | |
303 if (f_type) { | |
304 if (f_type->typeEntry()->isStructInD()) | |
305 return_type = f_type->typeEntry()->qualifiedCppName(); | |
306 else if (f_type->isObject() || f_type->isReference() || f_type->isValue() || f_type->isQObject()) | |
307 return_type = "void*"; | |
308 if (f_type->isVariant()) | |
309 return_type = "void*"; | |
310 } | |
311 | |
312 if (options & CppImplGenerator::ExternC && f_type) | |
313 if (f_type->isTargetLangString()) | |
314 return_type = "void"; | |
315 // qtd end | |
316 return return_type; | |
317 } | |
318 | |
319 QByteArray jniTypeName(const AbstractMetaType *java_type) | |
320 { | |
321 if (!java_type) { | |
322 return "Void"; | |
323 } else if (java_type->isTargetLangChar()) { | |
324 return "Char"; | |
325 } else if (java_type->isPrimitive()) { | |
326 return jniTypeName(java_type->typeEntry()->jniName()); | |
327 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags()) { | |
328 return "Int"; | |
329 } else { | |
330 return "Object"; | |
331 } | |
332 } | |
333 | |
334 QByteArray newXxxArray(const AbstractMetaType *java_type) | |
335 { | |
336 return "New" + jniTypeName(java_type) + "Array"; | |
337 } | |
338 | |
339 QByteArray setXxxArrayElement(const AbstractMetaType *java_type) | |
340 { | |
341 Q_ASSERT(java_type); | |
342 return "Set" + jniTypeName(java_type) + "ArrayElement"; | |
343 } | |
344 | |
345 QByteArray getXxxArrayElement(const AbstractMetaType *java_type) | |
346 { | |
347 Q_ASSERT(java_type); | |
348 return "Get" + jniTypeName(java_type) + "ArrayElement"; | |
349 } | |
350 | |
351 QByteArray getXxxArrayRegion(const AbstractMetaType *java_type) | |
352 { | |
353 Q_ASSERT(java_type); | |
354 return "Get" + jniTypeName(java_type) + "ArrayRegion"; | |
355 } | |
356 | |
357 QByteArray setXxxArrayRegion(const AbstractMetaType *java_type) | |
358 { | |
359 Q_ASSERT(java_type); | |
360 return "Set" + jniTypeName(java_type) + "ArrayRegion"; | |
361 } | |
362 | |
363 QByteArray callXxxMethod(const AbstractMetaType *java_type) | |
364 { | |
365 return "Call" + jniTypeName(java_type) + "Method"; | |
366 } | |
367 | |
368 QByteArray callXxxMethod(const QString &name) { | |
369 TypeEntry *entry = TypeDatabase::instance()->findType(name); | |
370 if (entry && entry->isPrimitive()) | |
371 return "Call" + jniTypeName(entry->jniName()) + "Method"; | |
372 else | |
373 return "CallObjectMethod"; | |
374 } | |
375 | |
376 QString jni_function_signature(QString package, QString class_name, | |
377 const QString &function_name, | |
378 const QString &return_type, | |
379 const QString &mangled_arguments = QString(), | |
380 uint options = CppImplGenerator::StandardJNISignature) | |
381 { | |
382 QString s; | |
383 | |
384 if (options & CppImplGenerator::ExternC) | |
385 s += "extern \"C\" DLL_PUBLIC "; | |
386 /* qtd | |
387 if (options & CppImplGenerator::JNIExport) | |
388 s += "Q_DECL_EXPORT "; | |
389 */ | |
390 if (options & CppImplGenerator::ReturnType) { | |
391 s += return_type; | |
392 s += " "; | |
393 } | |
394 /* qtd | |
395 if (options & CppImplGenerator::JNIExport) | |
396 s += "JNICALL QTJAMBI_FUNCTION_PREFIX("; | |
397 | |
398 s += "Java_"; | |
399 | |
400 s += package.replace(".", "_"); // qtd .replace("_", "_1") | |
401 s += '_'; | |
402 s += class_name; // qtd .replace("_", "_1"); | |
403 s += '_'; | |
404 */ | |
405 s += QString(function_name); //.replace("_", "_1"); | |
406 // s += mangled_arguments; | |
407 | |
408 /* qtd | |
409 if (options & CppImplGenerator::JNIExport) | |
410 s += ")"; | |
411 */ | |
412 return s; | |
413 } | |
414 | |
415 QString CppImplGenerator::fileNameForClass(const AbstractMetaClass *java_class) const | |
416 { | |
417 return QString("%1_shell.cpp").arg(java_class->name()); | |
418 } | |
419 | |
420 void CppImplGenerator::writeSignalFunction(QTextStream &s, const AbstractMetaFunction *signal, const AbstractMetaClass *cls, | |
421 int pos) | |
422 { | |
423 writeFunctionSignature(s, signal, cls, signalWrapperPrefix(), | |
424 Option(OriginalName | OriginalTypeDescription), | |
425 "QtJambi_SignalWrapper_"); | |
426 s << endl << "{" << endl; | |
427 { | |
428 AbstractMetaArgumentList arguments = signal->arguments(); | |
429 Indentation indent(INDENT); | |
430 | |
431 if (arguments.size() > 0) | |
432 s << INDENT << "jvalue arguments[" << arguments.size() << "];" << endl; | |
433 else | |
434 s << INDENT << "jvalue *arguments = 0;" << endl; | |
435 s << INDENT << "JNIEnv *__jni_env = qtjambi_current_environment();" << endl | |
436 << INDENT << "__jni_env->PushLocalFrame(100);" << endl; | |
437 | |
438 for (int i=0; i<arguments.size(); ++i) { | |
439 const AbstractMetaArgument *argument = arguments.at(i); | |
440 writeQtToJava(s, | |
441 argument->type(), | |
442 argument->indexedName(), | |
443 "__java_" + argument->indexedName(), | |
444 signal, | |
445 argument->argumentIndex() + 1, | |
446 BoxedPrimitive); | |
447 s << INDENT << "arguments[" << i << "].l = __java_" << argument->indexedName() << ";" << endl; | |
448 } | |
449 s << INDENT << "qtjambi_call_java_signal(__jni_env, m_signals[" << pos << "], arguments);" | |
450 << endl; | |
451 | |
452 s << INDENT << "__jni_env->PopLocalFrame(0);" << endl; | |
453 | |
454 if (signal->type() != 0) | |
455 s << INDENT << default_return_statement_qt(signal->type()) << ";" << endl; | |
456 } | |
457 s << "}" << endl << endl; | |
458 | |
459 if (signal->implementingClass() == signal->ownerClass()) | |
460 writeFinalFunction(s, signal, cls); | |
461 } | |
462 | |
463 bool CppImplGenerator::hasCustomDestructor(const AbstractMetaClass *java_class) const | |
464 { | |
465 return !java_class->isQObject() && !java_class->typeEntry()->isValue(); | |
466 } | |
467 | |
468 void writeQtdEntityFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
469 { | |
21 | 470 // if (!(java_class->typeEntry()->isObject() || java_class->typeEntry()->isQObject())) |
471 // return; | |
1 | 472 if (!java_class->hasVirtualFunctions()) |
473 return; | |
474 | |
475 s << "extern \"C\" DLL_PUBLIC void *__" << java_class->name() << "_entity(void *q_ptr)" << endl; | |
476 s << "{" << endl; | |
477 { | |
478 Indentation indent(INDENT); | |
479 s << INDENT << "Qtd_QObjectEntity* a = dynamic_cast<Qtd_QObjectEntity*>((" << java_class->qualifiedCppName() << "*)q_ptr);" << endl | |
480 << INDENT << "if (a != NULL)" << endl | |
481 << INDENT << " return a->d_entity();" << endl | |
482 << INDENT << "else" << endl | |
483 << INDENT << " return NULL;" << endl; | |
484 } | |
485 s << "}" << endl << endl; | |
486 } | |
487 | |
488 void CppImplGenerator::writeInterfaceCasts(QTextStream &s, const AbstractMetaClass *java_class) | |
489 { | |
490 // pointers to native interface objects for classes that implement interfaces | |
491 // initializing | |
492 AbstractMetaClassList interfaces = java_class->interfaces(); | |
493 if (!interfaces.isEmpty()) { | |
494 for (int i=0; i<interfaces.size(); ++i) { | |
495 AbstractMetaClass *iface = interfaces.at(i); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
496 |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
497 // in case of renamed class |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
498 InterfaceTypeEntry *ite = static_cast<InterfaceTypeEntry*>(iface->typeEntry()); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
499 QString real_name = ite->origin()->qualifiedCppName(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
500 |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
501 s << "extern \"C\" DLL_PUBLIC " << real_name << "* qtd_" << java_class->name() << "_cast_to_" << iface->qualifiedCppName() |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
502 << "(" << java_class->qualifiedCppName() << " *ptr)" << endl << "{" << endl; |
1 | 503 Indentation indent(INDENT); |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
504 s << INDENT << "return dynamic_cast<" << real_name << "*>(ptr);" << endl; |
1 | 505 s << "}" << endl << endl; |
506 } | |
507 } | |
508 } | |
509 | |
510 void CppImplGenerator::writeInitCallbacks(QTextStream &s, const AbstractMetaClass *java_class) | |
511 { | |
512 QString initArgs = "pfunc_abstr *virts"; | |
513 if (java_class->isQObject()) | |
514 initArgs += ", pfunc_abstr *sigs, pfunc_abstr qobj_del"; | |
515 | |
516 s << "extern \"C\" DLL_PUBLIC void qtd_" << java_class->name() | |
517 << QString("_initCallBacks(%1) {").arg(initArgs) << endl; | |
518 | |
519 // virtual functions handlers | |
520 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions(); | |
521 for (int pos = 0; pos<virtualFunctions.size(); ++pos) { | |
522 const AbstractMetaFunction *function = virtualFunctions.at(pos); | |
523 if (!notWrappedYet(function)) { // qtd2 | |
524 s << " " << function->marshalledName() << "_dispatch = " | |
525 "(pf" << function->marshalledName() << "_dispatch) virts[" << pos << "];" << endl; | |
526 } | |
527 } | |
528 | |
529 // D-side signal callbacks | |
530 AbstractMetaFunctionList signal_funcs = signalFunctions(java_class); | |
531 for(int i = 0; i < signal_funcs.size(); i++) | |
532 s << " emit_callbacks_" << java_class->name() << "[" << i << "] = (EmitCallback)" | |
533 "sigs[" << i << "];" << endl; | |
534 | |
535 if (java_class->isQObject()) | |
536 s << " qtd_D_" << java_class->name() << "_delete = " | |
537 "(qtd_pf_D_" << java_class->name() << "_delete)qobj_del;" << endl; | |
538 | |
539 | |
540 s << "}" << endl; | |
541 } | |
542 | |
543 | |
544 void CppImplGenerator::write(QTextStream &s, const AbstractMetaClass *java_class) | |
545 { | |
546 | |
547 bool shellClass = java_class->generateShellClass(); | |
548 | |
549 // Includes | |
550 writeExtraIncludes(s, java_class); | |
551 bool shellInclude = (java_class->generateShellClass() | |
552 || java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromShell).size() > 0); | |
553 | |
554 // need to include QPainter for all widgets... | |
555 { | |
556 const AbstractMetaClass *qwidget = java_class; | |
557 while (qwidget && qwidget->name() != "QWidget") { | |
558 qwidget = qwidget->baseClass(); | |
559 } | |
560 if (qwidget) | |
561 s << "#include <QPainter>" << endl << endl; | |
562 } | |
563 /* | |
564 #if defined(QTJAMBI_DEBUG_TOOLS) | |
565 s << "#include <qtjambidebugtools_p.h>" << endl << endl; | |
566 #endif | |
567 */ | |
568 if (shellInclude) | |
569 s << "#include \"" << java_class->name() << "_shell" << ".h\"" << endl; | |
570 /* qtd | |
571 if (java_class->isQObject()) | |
572 s << "#include <qtdynamicmetaobject.h>" << endl; | |
573 */ | |
574 s << "#include <iostream>" << endl; | |
575 Include inc = java_class->typeEntry()->include(); | |
576 if (!inc.name.isEmpty()) { | |
577 s << "#include "; | |
578 if (inc.type == Include::IncludePath) | |
579 s << "<"; | |
580 else | |
581 s << "\""; | |
582 s << inc.name; | |
583 if (inc.type == Include::IncludePath) | |
584 s << ">"; | |
585 else | |
586 s << "\""; | |
587 s << endl; | |
588 } | |
589 s << endl; // qtd | |
590 s << "#include \"qtd_core.h\"" << endl | |
591 << "#include \"ArrayOpsPrimitive.h\"" << endl | |
592 << "#include \"ArrayOps_qt_core.h\"" << endl; | |
593 | |
594 // qtd2 hack!! | |
37 | 595 if (java_class->package() == "qt.svg") |
1 | 596 s << "#include \"ArrayOps_qt_gui.h\"" << endl; |
597 | |
35 | 598 QString pkg_name = QString(java_class->package()).replace(".", "_"); |
599 s << QString("#include \"ArrayOps_%1.h\"").arg(pkg_name) << endl; | |
600 | |
1 | 601 s << endl; |
602 | |
603 writeInterfaceCasts(s, java_class); | |
604 | |
605 /* qtd2 | |
606 writeShellSignatures(s, java_class); | |
607 | |
608 writeDefaultConstructedValues(s, java_class); | |
609 | |
610 if (hasCustomDestructor(java_class)) */ | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
611 writeFinalDestructor(s, java_class); |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
612 |
107 | 613 if (java_class->isQObject()) |
614 writeSignalsHandling(s, java_class); | |
615 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
616 if (shellClass) { |
1 | 617 foreach (AbstractMetaFunction *function, java_class->functions()) { |
618 if (function->isConstructor() && !function->isPrivate()) | |
619 writeShellConstructor(s, function); | |
620 } | |
621 writeShellDestructor(s, java_class); | |
622 | |
623 writeQtdEntityFunction(s, java_class); | |
624 | |
625 if (java_class->isQObject()) | |
626 writeQObjectFunctions(s, java_class); | |
627 | |
628 // Virtual overrides | |
629 s << "// Virtual overrides" << endl; | |
630 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions(); | |
631 for (int pos = 0; pos<virtualFunctions.size(); ++pos) { | |
632 const AbstractMetaFunction *function = virtualFunctions.at(pos); | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
633 // qtd writeShellFunction(s, function, java_class, pos); |
1 | 634 writeShellVirtualFunction(s, function, java_class, pos); |
635 } | |
636 | |
637 if (cpp_shared) | |
638 writeInitCallbacks(s, java_class); | |
639 | |
640 // Functions in shell class | |
641 s << "// Functions in shell class" << endl; | |
642 AbstractMetaFunctionList shellFunctions = java_class->nonVirtualShellFunctions(); | |
643 for (int i=0; i<shellFunctions.size(); ++i) { | |
644 const AbstractMetaFunction *function = shellFunctions.at(i); | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
645 writeShellFunction(s, function, java_class, -1); |
1 | 646 } |
647 | |
648 // Write public overrides for functions that are protected in the base class | |
649 // so they can be accessed from the native callback | |
650 s << "// public overrides for functions that are protected in the base class" << endl; | |
651 AbstractMetaFunctionList public_override_functions = java_class->publicOverrideFunctions(); | |
652 foreach (AbstractMetaFunction *function, public_override_functions) { | |
653 if(notWrappedYet(function)) | |
654 continue; | |
655 writePublicFunctionOverride(s, function, java_class); | |
656 } | |
657 | |
658 // Write virtual function overries used to decide on static/virtual calls | |
659 s << "// Write virtual function overries used to decide on static/virtual calls" << endl; | |
660 AbstractMetaFunctionList virtual_functions = java_class->virtualOverrideFunctions(); | |
661 foreach (const AbstractMetaFunction *function, virtual_functions) { | |
662 if(notWrappedYet(function)) | |
663 continue; | |
664 writeVirtualFunctionOverride(s, function, java_class); | |
665 } | |
666 } | |
667 | |
668 writeExtraFunctions(s, java_class); | |
669 /* qtd2 | |
670 writeToStringFunction(s, java_class); | |
671 | |
672 if (java_class->hasCloneOperator()) { | |
673 writeCloneFunction(s, java_class); | |
674 } | |
675 | |
676 // Signals | |
677 AbstractMetaFunctionList signal_functions = signalFunctions(java_class); | |
678 for (int i=0; i<signal_functions.size(); ++i) | |
679 writeSignalFunction(s, signal_functions.at(i), java_class, i); | |
680 */ | |
681 s << "// ---externC---" << endl; | |
682 | |
683 // Native callbacks (all java functions require native callbacks) | |
684 AbstractMetaFunctionList class_funcs = java_class->functionsInTargetLang(); | |
685 foreach (AbstractMetaFunction *function, class_funcs) { | |
686 if (!function->isEmptyFunction()) | |
687 writeFinalFunction(s, function, java_class); | |
688 } | |
689 s << "// ---externC---end" << endl; | |
690 | |
691 | |
692 class_funcs = java_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
693 foreach (AbstractMetaFunction *function, class_funcs) { | |
694 if (function->implementingClass() != java_class) { | |
695 writeFinalFunction(s, function, java_class); | |
696 } | |
697 } | |
698 | |
699 // Field accessors | |
700 s << "// Field accessors" << endl; | |
701 foreach (AbstractMetaField *field, java_class->fields()) { | |
702 if (field->wasPublic() || (field->wasProtected() && !java_class->isFinal())) | |
703 writeFieldAccessors(s, field); | |
704 } | |
705 /* | |
706 s << "// writeFromNativeFunction" << endl; | |
707 writeFromNativeFunction(s, java_class); | |
708 | |
709 if (java_class->isQObject()) | |
710 writeOriginalMetaObjectFunction(s, java_class); | |
711 | |
712 if (java_class->typeEntry()->isValue()) | |
713 writeFromArrayFunction(s, java_class); | |
714 | |
715 // generate the __qt_cast_to_Xxx functions | |
716 if (!java_class->isNamespace() && !java_class->isInterface()) { | |
717 AbstractMetaClassList interfaces = java_class->interfaces(); | |
718 foreach (AbstractMetaClass *iface, interfaces) | |
719 writeInterfaceCastFunction(s, java_class, iface); | |
720 } | |
721 | |
722 writeSignalInitialization(s, java_class); | |
723 */ | |
724 // qtd writeJavaLangObjectOverrideFunctions(s, java_class); | |
725 | |
726 s << endl << endl; | |
727 | |
728 QString pro_file_name = java_class->package().replace(".", "_") + "/" + java_class->package().replace(".", "_") + ".pri"; | |
729 priGenerator->addSource(pro_file_name, fileNameForClass(java_class)); | |
730 } | |
731 | |
732 void CppImplGenerator::writeVirtualDispatchFunction(QTextStream &s, const AbstractMetaFunction *function, bool d_export) | |
733 { | |
734 uint options2 = ReturnType | ExternC; | |
735 QString return_type = jniReturnName(function, options2); | |
736 QString f_name = function->marshalledName() + "_dispatch"; | |
737 | |
738 if(!d_export) | |
739 s << "extern \"C\" "; | |
740 | |
741 if (!cpp_shared || d_export) { | |
742 s << return_type << " " << f_name; | |
743 writeVirtualDispatchArguments(s, function, d_export); | |
744 if(!d_export) | |
745 s << ";"; | |
746 } else if (cpp_shared) { | |
747 s << "typedef " << return_type << " " << "(*pf" << f_name << ")"; | |
748 writeVirtualDispatchArguments(s, function, false); | |
749 s << ";" << endl | |
750 << "pf" << function->marshalledName() << "_dispatch " | |
751 << function->marshalledName() << "_dispatch;"; | |
752 } | |
753 | |
754 s << endl; | |
755 } | |
756 | |
757 void CppImplGenerator::writeShellVirtualFunction(QTextStream &s, const AbstractMetaFunction *function, | |
758 const AbstractMetaClass *implementor, int id) | |
759 { | |
760 // ---------------------------- | |
761 if(notWrappedYet(function)) | |
762 return; | |
763 | |
764 AbstractMetaType *f_type = function->type(); | |
765 QString new_return_type = function->typeReplaced(0); | |
766 bool has_function_type = ((f_type != 0 | |
767 || !new_return_type.isEmpty()) | |
768 && new_return_type != "void"); | |
769 | |
770 writeVirtualDispatchFunction(s, function); | |
771 | |
772 writeFunctionSignature(s, function, implementor, QString(), OriginalName); | |
773 | |
774 s << endl | |
775 << "{" << endl; | |
776 | |
777 Option options = NoOption; | |
778 | |
779 Indentation indent(INDENT); | |
780 //bool static_call = !(options & VirtualCall); | |
781 //s << "std::cout << \"entering " << function->marshalledName() << " \\n\"; " << endl; | |
782 | |
783 if (f_type) { | |
784 if (f_type->isTargetLangString()) | |
104 | 785 s << INDENT << "DArray ret_str;" << endl; |
81 | 786 |
1 | 787 if (f_type->name() == "QModelIndex") |
788 s << INDENT << "QModelIndexAccessor __d_return_value;" << endl; | |
81 | 789 else if (f_type->typeEntry()->isStructInD()) |
790 s << INDENT << f_type->typeEntry()->qualifiedCppName() << " __d_return_value;" << endl; | |
791 | |
1 | 792 if (f_type->isContainer()) |
793 s << INDENT << "void* __d_return_value;" << endl | |
794 << INDENT << "size_t __d_return_value_size;" << endl; | |
795 } | |
796 | |
797 AbstractMetaArgumentList arguments = function->arguments(); | |
798 foreach (AbstractMetaArgument *argument, arguments) { | |
799 if (!function->argumentRemoved(argument->argumentIndex()+1)) { | |
800 if (!argument->type()->isPrimitive() | |
801 || !function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
802 if(argument->type()->isContainer()) { | |
803 QString arg_name = argument->indexedName(); | |
804 s << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl | |
805 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name); | |
806 | |
807 writeQtToJava(s, | |
808 argument->type(), | |
809 arg_name, | |
810 "__d_" + arg_name, | |
811 function, | |
812 argument->argumentIndex() + 1, | |
813 Option(VirtualDispatch)); | |
814 } | |
815 } | |
816 } | |
817 } | |
818 | |
819 if ((options & NoReturnStatement) == 0) | |
820 s << INDENT; | |
821 /* qtd if (function->isAbstract() && static_call) { | |
822 s << default_return_statement_qt(function->type(), options) << ";" << endl; | |
823 } else */ | |
824 { | |
825 if (f_type) { | |
826 if (f_type->isTargetLangString() || f_type->isContainer()) | |
827 ; | |
828 else if ((f_type->isValue() && !f_type->typeEntry()->isStructInD()) || | |
829 f_type->isVariant() ) | |
830 s << f_type->name() << " *__qt_return_value = (" << f_type->name() << "*) "; | |
831 else if (f_type->isObject() || f_type->isQObject()) | |
832 s << "return (" << f_type->name() <<"*) "; | |
81 | 833 else if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 834 ; |
835 else if ((options & NoReturnStatement) == 0) | |
836 s << "return "; | |
837 | |
838 if (f_type->isEnum() || f_type->isFlags()) | |
839 s << "(" << f_type->typeEntry()->qualifiedCppName() <<") "; | |
840 } | |
841 | |
842 s << function->marshalledName() << "_dispatch(" | |
843 << "this->d_entity()"; | |
844 | |
845 if (f_type) { | |
846 if (f_type->isTargetLangString()) | |
104 | 847 s << ", &ret_str"; |
81 | 848 if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 849 s << ", &__d_return_value"; |
850 if (f_type->isContainer()) | |
851 s << ", &__d_return_value, &__d_return_value_size"; | |
852 } | |
853 | |
854 if (function->arguments().size() > 0) | |
855 s << ", "; | |
856 writeFunctionCallArguments(s, function, QString(), Option(options | ForceEnumCast | SkipRemovedArguments | ExcludeConst | VirtualDispatch)); | |
857 | |
858 s << ");" << endl; | |
859 | |
860 //s << "std::cout << \"leaving " << function->marshalledName() << " \\n\"; " << endl; | |
861 | |
862 if (f_type) { | |
863 if (f_type->name() == "QModelIndex") { | |
864 s << INDENT << "QModelIndex __qt_return_value = qtd_to_QModelIndex( __d_return_value );" << endl; | |
865 #ifdef Q_OS_WIN32 | |
866 s << "__qtd_dummy();" << endl; // hack!!! | |
867 #endif | |
868 s << INDENT << "return __qt_return_value;" << endl; | |
81 | 869 } else if (f_type->typeEntry()->isStructInD()) |
870 s << INDENT << "return __d_return_value;" << endl; | |
1 | 871 |
872 if (f_type->isContainer()) { | |
873 writeJavaToQt(s, f_type, "__qt_return_value", "__d_return_value", | |
874 function, 0, GlobalRefJObject); | |
875 s << INDENT << "return __qt_return_value;" << endl; | |
876 } | |
877 | |
878 if (f_type->isTargetLangString()) | |
104 | 879 s << INDENT << "return " << "QString::fromUtf8((const char*) ret_str.ptr, ret_str.length);" << endl; |
1 | 880 |
881 if ( ( f_type->isValue() && !f_type->typeEntry()->isStructInD() ) || f_type->isVariant() ) | |
882 s << INDENT << "return " << f_type->name() << "(*__qt_return_value);" << endl; //" __qt_return_value = "; | |
883 } | |
884 } | |
885 | |
886 s << "}" << endl << endl; | |
887 // ---------------------------- | |
888 } | |
889 | |
890 void CppImplGenerator::writeVirtualDispatchArguments(QTextStream &s, const AbstractMetaFunction *d_function, bool d_export) | |
891 { | |
892 uint nativeArgCount = 0; | |
893 AbstractMetaType *ret_type = d_function->type(); | |
894 | |
895 s << "(void *d_entity"; | |
896 | |
897 if (ret_type) { | |
104 | 898 if (ret_type->isTargetLangString()) { |
899 if (d_export) | |
900 s << ", string* ret_str"; | |
901 else | |
902 s << ", DArray* ret_str"; | |
903 } | |
1 | 904 if (ret_type->name() == "QModelIndex") |
905 s << ", QModelIndexAccessor *__d_return_value"; | |
81 | 906 else if(ret_type->typeEntry()->isStructInD()) |
907 s << ", " << ret_type->typeEntry()->qualifiedCppName() << " *__d_return_value"; | |
908 | |
1 | 909 if (ret_type->isContainer()) |
910 s << ", void** __d_arr_ptr, size_t* __d_arr_size"; | |
911 } | |
912 | |
913 if (d_function->arguments().size() > 0) | |
914 s << ", "; | |
915 | |
916 | |
917 // the function arguments | |
918 AbstractMetaArgumentList arguments = d_function->arguments(); | |
919 foreach (const AbstractMetaArgument *argument, arguments) { | |
920 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
921 if (nativeArgCount > 0) | |
922 s << "," << " "; | |
923 | |
924 AbstractMetaType *d_type = argument->type(); | |
925 QString arg_name = argument->indexedName(); | |
926 | |
927 if (d_type->name() == "QModelIndex") | |
928 s << "QModelIndexAccessor" << QString(d_type->actualIndirections(), '*') << " " << arg_name; | |
929 else if (d_type->isContainer()) { | |
930 if (d_export) { | |
931 s << DGenerator::translateType(d_type, d_function->ownerClass(), NoOption) << "* "; | |
932 } else | |
933 s << "DArray* "; | |
934 s << arg_name; | |
935 } else if (d_type->typeEntry()->isStructInD()) | |
936 s << d_type->typeEntry()->qualifiedCppName() << QString(d_type->actualIndirections(), '*') | |
937 << " " << arg_name; | |
938 else if (d_type->isTargetLangString()) { | |
939 if (d_export) | |
940 s << "wchar* "; | |
941 else | |
942 s << "const unsigned short* "; | |
943 s << QString("%1, int %1_size").arg(arg_name); | |
944 } else { | |
945 if(d_type->isVariant()) | |
946 s << "void*"; | |
947 else if (!d_type->hasNativeId()) | |
948 { | |
949 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_type->typeEntry()); | |
950 if(d_type->typeEntry()->isObject() && ctype->isAbstract() && d_export) | |
951 { | |
952 QString d_name = d_type->typeEntry()->qualifiedCppName(); | |
953 s << d_name + "_ConcreteWrapper" + QString(d_type->indirections(),'*'); | |
954 } | |
955 else | |
39 | 956 s << translateType(d_type, EnumAsInts, d_export); |
1 | 957 } |
958 else | |
959 s << "void*"; | |
960 s << " " << argument->indexedName(); | |
961 } | |
962 nativeArgCount++; | |
963 } | |
964 } | |
965 s << ")"; | |
966 } | |
967 | |
968 void CppImplGenerator::writeJavaLangObjectOverrideFunctions(QTextStream &s, const AbstractMetaClass *cls) | |
969 { | |
970 if (cls->hasHashFunction()) { | |
971 AbstractMetaFunctionList hashcode_functions = cls->queryFunctionsByName("hashCode"); | |
972 bool found = false; | |
973 foreach (const AbstractMetaFunction *function, hashcode_functions) { | |
974 if (function->actualMinimumArgumentCount() == 0) { | |
975 found = true; | |
976 break; | |
977 } | |
978 } | |
979 | |
980 if (!found) { | |
981 s << endl | |
982 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_hashCode", "jint") | |
983 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
984 << INDENT << "{" << endl; | |
985 { | |
986 Indentation indent(INDENT); | |
987 s << INDENT << "Q_UNUSED(__jni_env);" << endl | |
988 << INDENT << cls->qualifiedCppName() << " *__qt_this = (" | |
989 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
990 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
991 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
992 << INDENT << "return qHash(*__qt_this);" << endl; | |
993 } | |
994 s << INDENT << "}" << endl; | |
995 } | |
996 } | |
997 | |
998 // Qt has a standard toString() conversion in QVariant? | |
999 QVariant::Type type = QVariant::nameToType(cls->qualifiedCppName().toLatin1()); | |
1000 if (QVariant(type).canConvert(QVariant::String) && !cls->hasToStringCapability()) { | |
1001 AbstractMetaFunctionList tostring_functions = cls->queryFunctionsByName("toString"); | |
1002 bool found = false; | |
1003 foreach (const AbstractMetaFunction *function, tostring_functions) { | |
1004 if (function->actualMinimumArgumentCount() == 0) { | |
1005 found = true; | |
1006 break; | |
1007 } | |
1008 } | |
1009 | |
1010 if (!found) { | |
1011 s << endl | |
1012 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_toString", "jstring") | |
1013 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1014 << INDENT << "{" << endl; | |
1015 { | |
1016 Indentation indent(INDENT); | |
1017 s << INDENT << cls->qualifiedCppName() << " *__qt_this = (" | |
1018 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1019 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1020 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1021 << INDENT << "return qtjambi_from_qstring(__jni_env, QVariant(*__qt_this).toString());" << endl; | |
1022 } | |
1023 s << INDENT << "}" << endl; | |
1024 } | |
1025 } | |
1026 } | |
1027 | |
1028 void CppImplGenerator::writeExtraFunctions(QTextStream &s, const AbstractMetaClass *java_class) | |
1029 { | |
1030 const ComplexTypeEntry *class_type = java_class->typeEntry(); | |
1031 Q_ASSERT(class_type); | |
1032 | |
1033 CodeSnipList code_snips = class_type->codeSnips(); | |
1034 foreach (const CodeSnip &snip, code_snips) { | |
1035 if (snip.language == TypeSystem::ShellCode || snip.language == TypeSystem::NativeCode) { | |
1036 snip.formattedCode(s, INDENT) << endl; | |
1037 } | |
1038 } | |
1039 } | |
1040 | |
1041 void CppImplGenerator::writeToStringFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
1042 { | |
1043 FunctionModelItem fun = java_class->hasToStringCapability(); | |
1044 bool core = java_class->package() == QLatin1String("qt.core"); | |
1045 bool qevent = false; | |
1046 | |
1047 const AbstractMetaClass *cls = java_class; | |
1048 while (cls) { | |
1049 if (cls->name() == "QEvent") { | |
1050 qevent = true; | |
1051 fun = cls->hasToStringCapability(); | |
1052 break; | |
1053 } | |
1054 cls = cls->baseClass(); | |
1055 } | |
1056 | |
1057 if (!java_class->hasDefaultToStringFunction() && fun && !(qevent && core)) { | |
1058 | |
1059 int indirections = fun->arguments().at(1)->type().indirections(); | |
1060 QString deref = QLatin1String(indirections == 0 ? "*" : ""); | |
1061 | |
1062 s << endl; | |
1063 s << "#include <QDebug>" << endl; | |
1064 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_toString", "jstring") | |
1065 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1066 << INDENT << "{" << endl; | |
1067 { | |
1068 Indentation indent(INDENT); | |
1069 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = (" | |
1070 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1071 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1072 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1073 | |
1074 << INDENT << "QString res;" << endl | |
1075 << INDENT << "QDebug d(&res);" << endl | |
1076 << INDENT << "d << " << deref << "__qt_this;" << endl; | |
1077 s << INDENT << "return qtjambi_from_qstring(__jni_env, res);" << endl; | |
1078 } | |
1079 s << INDENT << "}" << endl << endl; | |
1080 } | |
1081 } | |
1082 | |
1083 void CppImplGenerator::writeCloneFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
1084 { | |
1085 s << endl | |
1086 << jni_function_signature(java_class->package(), java_class->name(), "__qt_clone", "jobject") << endl | |
1087 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1088 << INDENT << "{" << endl; | |
1089 { | |
1090 Indentation indent(INDENT); | |
1091 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = (" | |
1092 | |
1093 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1094 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1095 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1096 << INDENT << java_class->qualifiedCppName() << " *res = __qt_this;" << endl | |
1097 << INDENT << "return qtjambi_from_object(__jni_env, res, \"" << java_class->name() << "\", \"" << java_class->package().replace(".", "/") << "/\", true);" << endl; | |
1098 } | |
1099 s << INDENT << "}" << endl << endl; | |
1100 } | |
1101 | |
1102 void CppImplGenerator::writeShellSignatures(QTextStream &s, const AbstractMetaClass *java_class) | |
1103 { | |
1104 bool has_constructors = java_class->hasConstructors(); | |
1105 | |
1106 // Write the function names... | |
1107 if (has_constructors && java_class->hasVirtualFunctions()) { | |
1108 AbstractMetaFunctionList virtual_functions = java_class->virtualFunctions(); | |
1109 { | |
1110 Indentation indent(INDENT); | |
1111 | |
1112 int pos = -1; | |
1113 foreach (AbstractMetaFunction *function, virtual_functions) { | |
1114 ++pos; | |
1115 | |
1116 if (pos == 0) | |
1117 s << "static const char *qtjambi_method_names[] = {"; | |
1118 else | |
1119 s << ","; | |
1120 s << endl | |
1121 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ " | |
1122 << "\"" << function->name() << "\""; | |
1123 } | |
1124 if (pos >= 0) | |
1125 s << endl << "};" << endl << endl; | |
1126 else | |
1127 s << "static const char **qtjambi_method_names = 0;" << endl; | |
1128 } | |
1129 | |
1130 // Write the function signatures | |
1131 { | |
1132 Indentation indent(INDENT); | |
1133 | |
1134 int pos = -1; | |
1135 foreach (AbstractMetaFunction *function, virtual_functions) { | |
1136 ++pos; | |
1137 | |
1138 if (pos == 0) | |
1139 s << "static const char *qtjambi_method_signatures[] = {"; | |
1140 else | |
1141 s << ","; | |
1142 s << endl | |
1143 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ " | |
1144 << "\"" | |
1145 << jni_signature(function, SlashesAndStuff) | |
1146 << "\""; | |
1147 } | |
1148 if (pos >= 0) | |
1149 s << endl << "};" << endl; | |
1150 else | |
1151 s << "static const char **qtjambi_method_signatures = 0;" << endl; | |
1152 s << "static const int qtjambi_method_count = " << QString::number(pos + 1) << ";" << endl | |
1153 << endl; | |
1154 } | |
1155 } | |
1156 | |
1157 if (has_constructors && java_class->hasInconsistentFunctions()) { | |
1158 AbstractMetaFunctionList inconsistents = java_class->cppInconsistentFunctions(); | |
1159 // Write the inconsistent function names... | |
1160 { | |
1161 Indentation indent(INDENT); | |
1162 s << "static const char *qtjambi_inconsistent_names[] = {"; | |
1163 for (int i=0; i<inconsistents.size(); ++i) { | |
1164 if (i != 0) | |
1165 s << ","; | |
1166 s << endl << INDENT << "\"" << inconsistents.at(i)->name() << "\""; | |
1167 } | |
1168 s << endl << "};" << endl << endl; | |
1169 } | |
1170 | |
1171 // Write the function signatures | |
1172 { | |
1173 Indentation indent(INDENT); | |
1174 s << "static const char *qtjambi_inconsistent_signatures[] = {"; | |
1175 for (int i=0; i<inconsistents.size(); ++i) { | |
1176 const AbstractMetaFunction *function = inconsistents.at(i); | |
1177 | |
1178 if (i != 0) | |
1179 s << ","; | |
1180 s << endl << INDENT << "\"" | |
1181 << jni_signature(function, SlashesAndStuff) | |
1182 << "\""; | |
1183 } | |
1184 s << endl << "};" << endl << endl; | |
1185 } | |
1186 s << "static const int qtjambi_inconsistent_count = " << inconsistents.size() << ";" << endl | |
1187 << endl; | |
1188 } | |
1189 | |
1190 | |
1191 AbstractMetaFunctionList signal_functions = java_class->cppSignalFunctions(); | |
1192 if (signal_functions.size()) { | |
1193 Indentation indent(INDENT); | |
1194 s << "static const char *qtjambi_signal_names[] = {"; | |
1195 for (int i=0; i<signal_functions.size(); ++i) { | |
1196 if (i != 0) | |
1197 s << ","; | |
1198 | |
1199 const AbstractMetaFunction *f = signal_functions.at(i); | |
1200 | |
1201 QString signalName = f->name(); | |
1202 | |
1203 s << endl << INDENT << "\"" << signalName << "\""; | |
1204 } | |
1205 s << endl << "};" << endl << endl; | |
1206 | |
1207 s << "static const int qtjambi_signal_argumentcounts[] = {"; | |
1208 for (int i=0; i<signal_functions.size(); ++i) { | |
1209 if (i != 0) | |
1210 s << ","; | |
1211 s << endl << INDENT << signal_functions.at(i)->arguments().count(); | |
1212 } | |
1213 s << endl << "};" << endl << endl; | |
1214 s << "static const int qtjambi_signal_count = " << signal_functions.size() << ";" << endl | |
1215 << endl; | |
1216 } | |
1217 } | |
1218 | |
1219 void CppImplGenerator::writeQObjectFunctions(QTextStream &s, const AbstractMetaClass *java_class) | |
1220 { | |
1221 // QObject::metaObject() | |
1222 /* s << "const QMetaObject *" << shellClassName(java_class) << "::metaObject() const" << endl | |
1223 << "{" << endl | |
1224 << " if (m_meta_object == 0) {" << endl | |
1225 << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl | |
1226 << " jobject __obj = m_link != 0 ? m_link->javaObject(__jni_env) : 0;" << endl | |
1227 << " if (__obj == 0) return " << java_class->qualifiedCppName() << "::metaObject();" << endl | |
1228 << " else m_meta_object = qtjambi_metaobject_for_class(__jni_env, __jni_env->GetObjectClass(__obj), " << java_class->qualifiedCppName() << "::metaObject());" << endl; | |
1229 | |
1230 | |
1231 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions(); | |
1232 for (int pos=0; pos<virtualFunctions.size(); ++pos) { | |
1233 const AbstractMetaFunction *virtualFunction = virtualFunctions.at(pos); | |
1234 if (virtualFunction->isVirtualSlot()) { | |
1235 QStringList introspectionCompatibleSignatures = virtualFunction->introspectionCompatibleSignatures(); | |
1236 foreach (QString introspectionCompatibleSignature, introspectionCompatibleSignatures) { | |
1237 s << " {" << endl | |
1238 << " int idx = " | |
1239 << java_class->qualifiedCppName() << "::metaObject()->indexOfMethod(\"" | |
1240 << introspectionCompatibleSignature << "\");" << endl; | |
1241 | |
1242 s << " if (idx >= 0) m_map.insert(idx, " << pos << ");" << endl | |
1243 << " }" << endl; | |
1244 } | |
1245 } | |
1246 | |
1247 } | |
1248 | |
1249 s << " }" << endl | |
1250 << " return m_meta_object;" << endl | |
1251 << "}" << endl << endl; | |
1252 | |
1253 // QObject::qt_metacast() | |
1254 s << "void *" << shellClassName(java_class) << "::qt_metacast(const char *_clname)" << endl | |
1255 << "{" << endl | |
1256 << " if (!_clname) return 0;" << endl | |
1257 << " if (!strcmp(_clname, \"" << shellClassName(java_class) << "\"))" << endl | |
1258 << " return static_cast<void*>(const_cast<" << shellClassName(java_class) << "*>(this));" << endl | |
1259 << " return " << java_class->qualifiedCppName() << "::qt_metacast(_clname);" << endl | |
1260 << "}" << endl << endl; | |
1261 */ | |
1262 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1263 // writeSignalsHandling(s, java_class); |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1264 /* |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1265 // QObject_Link::qt_metacall() |
1 | 1266 s << "int " << shellClassName(java_class) << "::qt_metacall(QMetaObject::Call _c, int _id, void **_a)" << endl |
1267 << "{" << endl; | |
1268 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1269 s << " _id = " << java_class->qualifiedCppName() << "::qt_metacall(_c, _id, _a);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1270 << " if (_id < 0 || _c != QMetaObject::InvokeMetaMethod)" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1271 << " return _id;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1272 // << " Q_ASSERT(_id < 2);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1273 << " emit_callbacks_" << java_class->name() << "[_id](this->d_entity(), _a);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1274 << " return -1;" << endl |
1 | 1275 << "}" << endl << endl; |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1276 */ |
1 | 1277 } |
1278 | |
1279 void CppImplGenerator::writeSignalHandler(QTextStream &s, const AbstractMetaClass *d_class, AbstractMetaFunction *function) | |
1280 { | |
1281 QString extra_args, extra_call_args, conversion_code; | |
1282 QTextStream s2(&conversion_code); | |
1283 | |
1284 Indentation indent(INDENT); | |
1285 | |
1286 AbstractMetaArgumentList arguments = function->arguments(); | |
1287 foreach (AbstractMetaArgument *argument, arguments) { | |
1288 if(argument->type()->isContainer()) { | |
1289 QString arg_name = argument->indexedName(); | |
1290 const AbstractMetaType *arg_type = argument->type(); | |
1291 extra_args += ", DArray " + arg_name; | |
1292 extra_call_args += ", " + arg_name + "_arr"; | |
1293 | |
1294 s2 << INDENT; | |
1295 writeTypeInfo(s2, arg_type, NoOption); | |
1296 s2 << arg_name << " = (*reinterpret_cast< "; | |
1297 writeTypeInfo(s2, arg_type, ExcludeReference); | |
1298 s2 << "(*)>(args[" << argument->argumentIndex() + 1 << "]));" << endl | |
1299 << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl | |
1300 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name); | |
1301 | |
1302 writeQtToJava(s2, | |
1303 arg_type, | |
1304 arg_name, | |
1305 "__d_" + arg_name, | |
1306 function, | |
1307 argument->argumentIndex() + 1, | |
1308 Option(VirtualDispatch)); | |
1309 } | |
1310 } | |
1311 QString sig_name = signalExternName(d_class, function); | |
1312 s << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle_in_d(void* d_entity, void** args" << extra_args <<");" << endl | |
1313 << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle(void* d_entity, void** args)" << endl | |
1314 << "{" << endl | |
1315 << conversion_code << endl | |
1316 << INDENT << sig_name << "_handle_in_d(d_entity, args" << extra_call_args << ");" << endl | |
1317 << "}" << endl; | |
1318 | |
1319 } | |
1320 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1321 void CppImplGenerator::writeQObjectLink(QTextStream &s, const AbstractMetaClass *java_class) |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1322 { |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1323 QString linkName = java_class->name() + "_Link"; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1324 QString className = java_class->name(); |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1325 s << "class " << linkName << " : public QObject, public QObjectUserData" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1326 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1327 << "public:" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1328 << " Q_OBJECT_CHECK" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1329 << " virtual int qt_metacall(QMetaObject::Call, int, void **);" << endl << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1330 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1331 << " " << linkName << "(QObject *parent, void *d_ptr) : QObject() { _d_ptr = d_ptr; }" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1332 << " void *d_entity() const { return _d_ptr; }" << endl << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1333 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1334 << "private:" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1335 << " void *_d_ptr;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1336 << "};" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1337 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1338 // QObject_Link::qt_metacall() |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1339 s << "int " << linkName << "::qt_metacall(QMetaObject::Call _c, int _id, void **_a)" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1340 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1341 << " _id = QObject::qt_metacall(_c, _id, _a);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1342 << " if (_id < 0 || _c != QMetaObject::InvokeMetaMethod)" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1343 << " return _id;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1344 // << " Q_ASSERT(_id < 2);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1345 << " emit_callbacks_" << java_class->name() << "[_id](this->d_entity(), _a);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1346 << " return -1;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1347 << "}" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1348 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1349 s << QString("inline %1_Link *get_%1_link(%1 *obj)").arg(className) << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1350 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1351 << " return static_cast<" << linkName << "*>(obj->userData(USER_DATA_ID));" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1352 << "}" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1353 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1354 s << QString("extern \"C\" DLL_PUBLIC void* qtd_%1_d_pointer(%1 *obj)").arg(className) << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1355 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1356 << " if (obj->userData(USER_DATA_ID)) {" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1357 << " " << QString("%1_Link *qobj_helper = get_%1_link(obj);").arg(className) << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1358 << " return qobj_helper->d_entity();" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1359 << " } else" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1360 << " return NULL;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1361 << "}" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1362 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1363 s << QString("extern \"C\" DLL_PUBLIC void qtd_%1_create_link(%1 *obj, void* d_obj)").arg(className) << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1364 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1365 << " if(obj->userData(USER_DATA_ID))" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1366 << " return;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1367 << " " << QString("%1 *qobj_link = new %1(obj, d_obj);").arg(linkName) << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1368 << " obj->setUserData(USER_DATA_ID, qobj_link);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1369 << "}" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1370 } |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1371 |
1 | 1372 void CppImplGenerator::writeSignalsHandling(QTextStream &s, const AbstractMetaClass *java_class) |
1373 { | |
1374 s << "extern \"C\" typedef void (*EmitCallback)(void*, void**);" << endl; | |
1375 AbstractMetaFunctionList signal_funcs = signalFunctions(java_class); | |
1376 | |
1377 if (cpp_shared) | |
1378 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "];" << endl; | |
1379 else { | |
1380 // D-side signal callbacks | |
1381 for(int i = 0; i < signal_funcs.size(); i++) { | |
1382 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1383 writeSignalHandler(s, java_class, signal); | |
1384 } | |
1385 | |
1386 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "] = {" << endl; | |
1387 for(int i = 0; i < signal_funcs.size(); i++) { | |
1388 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1389 s << endl; | |
1390 if (i != 0) | |
1391 s << ", "; | |
1392 s << "&" << signalExternName(java_class, signal) << "_handle"; | |
1393 } | |
1394 s << endl << "};" << endl << endl; | |
1395 } | |
1396 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1397 writeQObjectLink(s, java_class); |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1398 |
1 | 1399 // Functions connecting/disconnecting shell's slots |
1400 for(int i = 0; i < signal_funcs.size(); i++) { | |
1401 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1402 QString sigExternName = signalExternName(java_class, signal); | |
1403 | |
1404 s << "extern \"C\" DLL_PUBLIC void " << sigExternName << "_connect" | |
1405 << "(void* native_id)" << endl << "{" << endl | |
1406 << " " << shellClassName(java_class) << " *qobj = (" << shellClassName(java_class) << "*) native_id;" << endl | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1407 << " const QMetaObject &mo = " << java_class->name() << "::staticMetaObject;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1408 << " const QMetaObject &mo2 = " << java_class->name() << "_Link::staticMetaObject;" << endl |
1 | 1409 << " int signalId = mo.indexOfSignal(\"" << signal->minimalSignature() << "\");" << endl |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1410 << " mo.connect(qobj, signalId, get_" << java_class->name() << "_link(qobj), mo2.methodCount() + " << i << ");" << endl |
1 | 1411 << "}" << endl; |
1412 | |
1413 s << "extern \"C\" DLL_PUBLIC void " << sigExternName << "_disconnect" | |
1414 << "(void* native_id)" << endl << "{" << endl | |
1415 << " " << shellClassName(java_class) << " *qobj = (" << shellClassName(java_class) << "*) native_id;" << endl | |
1416 << " const QMetaObject &mo = " << shellClassName(java_class) << "::staticMetaObject;" << endl | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1417 << " const QMetaObject &mo2 = " << java_class->name() << "_Link::staticMetaObject;" << endl |
1 | 1418 << " int signalId = mo.indexOfSignal(\"" << signal->minimalSignature() << "\");" << endl |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1419 << " mo.disconnect(qobj, signalId, get_" << java_class->name() << "_link(qobj), mo2.methodCount() + " << i << ");" << endl |
1 | 1420 << "}" << endl << endl; |
1421 } | |
1422 } | |
1423 | |
1424 | |
1425 void CppImplGenerator::writeShellConstructor(QTextStream &s, const AbstractMetaFunction *java_function) | |
1426 { | |
1427 if (java_function->isModifiedRemoved(TypeSystem::ShellCode)) | |
1428 return; | |
1429 | |
1430 const AbstractMetaClass *cls = java_function->ownerClass(); | |
1431 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1432 | |
1433 writeFunctionSignature(s, java_function, cls); | |
1434 | |
1435 s << endl; | |
1436 s << " : " << cls->qualifiedCppName() << "("; | |
1437 for (int i=0; i<arguments.size(); ++i) { | |
1438 s << arguments.at(i)->indexedName(); | |
1439 if (i != arguments.size() - 1) | |
1440 s << ", "; | |
1441 } | |
1442 s << ")"; | |
1443 if (cls->hasVirtualFunctions()) | |
1444 s << "," << endl << " Qtd_QObjectEntity(d_ptr)"; | |
1445 /* qtd s << " m_meta_object(0)," << endl; | |
1446 s << " m_vtable(0)," << endl | |
1447 << " m_link(0)" << endl; | |
1448 */ | |
1449 s << endl; | |
1450 s << "{" << endl; | |
1451 { | |
1452 Indentation indent(INDENT); | |
1453 writeCodeInjections(s, java_function, cls, CodeSnip::Beginning, TypeSystem::ShellCode); | |
1454 writeCodeInjections(s, java_function, cls, CodeSnip::End, TypeSystem::ShellCode); | |
1455 } | |
1456 s << "}" << endl << endl; | |
1457 } | |
1458 | |
1459 void CppImplGenerator::writeShellDestructor(QTextStream &s, const AbstractMetaClass *java_class) | |
1460 { | |
1461 | |
1462 if (java_class->isQObject()) | |
1463 if (cpp_shared) | |
1464 s << "extern \"C\" typedef void (*qtd_pf_D_" << java_class->name() << "_delete)(void *d_ptr);" << endl | |
1465 << "qtd_pf_D_" << java_class->name() << "_delete qtd_D_" << java_class->name() << "_delete;" << endl << endl; | |
1466 else | |
1467 s << "extern \"C\" void qtd_D_" << java_class->name() << "_delete(void *d_ptr);" << endl << endl; | |
1468 | |
1469 s << shellClassName(java_class) << "::~" | |
1470 << shellClassName(java_class) << "()" << endl | |
1471 << "{" << endl; | |
1472 { | |
1473 Indentation indent(INDENT); | |
1474 if (java_class->isQObject()) | |
1475 s << INDENT << "if (QObject::parent())" << endl | |
1476 << INDENT << " qtd_D_" << java_class->name() << "_delete(this->d_entity());" << endl; | |
1477 | |
1478 /* qtd | |
1479 s << "#ifdef QT_DEBUG" << endl | |
1480 << INDENT << "if (m_vtable)" << endl | |
1481 << INDENT << " m_vtable->deref();" << endl | |
1482 << "#endif" << endl | |
1483 << INDENT << "if (m_link) {" << endl; | |
1484 | |
1485 AbstractMetaClassList interfaces = java_class->interfaces(); | |
1486 if (interfaces.size() + (java_class->baseClass() != 0 ? 1 : 0) > 1) { | |
1487 if (java_class->baseClass() != 0) | |
1488 interfaces += java_class->baseClass(); | |
1489 foreach (AbstractMetaClass *iface, interfaces) { | |
1490 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface; | |
1491 s << INDENT << " m_link->unregisterSubObject((" << impl->qualifiedCppName() << " *) this);" << endl; | |
1492 } | |
1493 } | |
1494 | |
1495 if (!java_class->isQObject()) { | |
1496 s << INDENT << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl | |
1497 << INDENT << " if (__jni_env != 0) m_link->nativeShellObjectDestroyed(__jni_env);" << endl; | |
1498 } | |
1499 | |
1500 #if defined(QTJAMBI_DEBUG_TOOLS) | |
1501 s << INDENT << " qtjambi_increase_shellDestructorCalledCount(QString::fromLatin1(\"" << java_class->name() << "\"));" << endl; | |
1502 #endif | |
1503 | |
1504 s << INDENT << "}" << endl; */ | |
1505 } | |
1506 s << "}" << endl << endl; | |
1507 } | |
1508 | |
1509 void CppImplGenerator::writeCodeInjections(QTextStream &s, const AbstractMetaFunction *java_function, | |
1510 const AbstractMetaClass *implementor, CodeSnip::Position position, | |
1511 TypeSystem::Language language) | |
1512 { | |
1513 | |
1514 FunctionModificationList mods; | |
1515 const AbstractMetaClass *cls = implementor; | |
1516 while (cls != 0) { | |
1517 mods += java_function->modifications(cls); | |
1518 | |
1519 if (cls == cls->baseClass()) | |
1520 break; | |
1521 cls = cls->baseClass(); | |
1522 } | |
1523 | |
1524 foreach (FunctionModification mod, mods) { | |
1525 if (mod.snips.count() <= 0) | |
1526 continue ; | |
1527 | |
1528 foreach (CodeSnip snip, mod.snips) { | |
1529 if (snip.position != position) | |
1530 continue ; | |
1531 | |
1532 if ((snip.language & language) == false) | |
1533 continue ; | |
1534 | |
1535 if (position == CodeSnip::End) | |
1536 s << endl; | |
1537 | |
1538 QString code; | |
1539 QTextStream tmpStream(&code); | |
1540 snip.formattedCode(tmpStream, INDENT); | |
1541 ArgumentMap map = snip.argumentMap; | |
1542 ArgumentMap::iterator it = map.begin(); | |
1543 for (;it!=map.end();++it) { | |
1544 int pos = it.key() - 1; | |
1545 QString meta_name = it.value(); | |
1546 | |
1547 if (pos >= 0 && pos < java_function->arguments().count()) { | |
1548 code = code.replace(meta_name, java_function->arguments().at(pos)->indexedName()); | |
1549 } else { | |
1550 QString debug = QString("argument map specifies invalid argument index %1" | |
1551 "for function '%2'") | |
1552 .arg(pos + 1).arg(java_function->name()); | |
1553 ReportHandler::warning(debug); | |
1554 } | |
1555 | |
1556 } | |
1557 s << code; | |
1558 if (position == CodeSnip::Beginning) | |
1559 s << endl; | |
1560 } | |
1561 } | |
1562 } | |
1563 | |
1564 static QString function_call_for_ownership(TypeSystem::Ownership owner, const QString &var_name) | |
1565 { | |
1566 if (owner == TypeSystem::CppOwnership) { | |
1567 return "setCppOwnership(__jni_env, " + var_name + ")"; | |
1568 } else if (owner == TypeSystem::TargetLangOwnership) { | |
1569 return "setJavaOwnership(__jni_env, " + var_name + ")"; | |
1570 } else if (owner == TypeSystem::DefaultOwnership) { | |
1571 return "setDefaultOwnership(__jni_env, " + var_name + ")"; | |
1572 } else { | |
1573 Q_ASSERT(false); | |
1574 return "bogus()"; | |
1575 } | |
1576 } | |
1577 | |
1578 void CppImplGenerator::writeOwnership(QTextStream &s, | |
1579 const AbstractMetaFunction *java_function, | |
1580 const QString &var_name, | |
1581 int var_index, | |
1582 const AbstractMetaClass *implementor) | |
1583 { | |
1584 TypeSystem::Ownership owner = TypeSystem::InvalidOwnership; | |
1585 const AbstractMetaClass *cls = implementor; | |
1586 while (cls != 0 && owner == TypeSystem::InvalidOwnership) { | |
1587 owner = java_function->ownership(cls, TypeSystem::ShellCode, var_index); | |
1588 cls = cls->baseClass(); | |
1589 } | |
1590 | |
1591 if (owner == TypeSystem::InvalidOwnership) | |
1592 return; | |
1593 | |
1594 if (var_index != -1) { | |
1595 s << INDENT << "if (" << var_name << " != 0) {" << endl; | |
1596 { | |
1597 Indentation indent(INDENT); | |
1598 s << INDENT << "QtJambiLink *__link = QtJambiLink::findLink(__jni_env, " | |
1599 << var_name << ");" << endl | |
1600 << INDENT << "Q_ASSERT(__link != 0);" << endl; | |
1601 | |
1602 s << INDENT << "__link->" << function_call_for_ownership(owner, var_name) << ";" << endl; | |
1603 } | |
1604 s << INDENT << "}" << endl; | |
1605 } else { | |
1606 s << INDENT << "if (m_link) {" << endl; | |
1607 { | |
1608 Indentation indent(INDENT); | |
1609 s << INDENT << "m_link->" << function_call_for_ownership(owner, "m_link->javaObject(__jni_env)") << ";" << endl; | |
1610 } | |
1611 s << INDENT << "}" << endl; | |
1612 } | |
1613 | |
1614 } | |
1615 | |
1616 void CppImplGenerator::writeShellFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
1617 const AbstractMetaClass *implementor, int id) | |
1618 { | |
1619 writeFunctionSignature(s, java_function, implementor, QString(), OriginalName); | |
1620 | |
1621 s << endl | |
1622 << "{" << endl; | |
1623 | |
1624 Indentation indent(INDENT); | |
1625 | |
1626 QString java_function_signature = java_function->signature(); | |
1627 /* s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) entering: " << implementor->name() << "::" | |
1628 << java_function_signature << "\");" << endl; | |
1629 */ | |
1630 writeCodeInjections(s, java_function, implementor, CodeSnip::Beginning, TypeSystem::ShellCode); | |
1631 | |
1632 // s << " printf(\"%s : %s\\n\", \"" << java_function->enclosingClass()->name() << "\"" | |
1633 // << ", \"" << java_function->name() << "\");" << endl; | |
1634 | |
1635 if (!java_function->isFinalInCpp() || java_function->isVirtualSlot()) { | |
1636 s << INDENT << "jmethodID method_id = m_vtable->method(" << id << ");" << endl; | |
1637 s << INDENT << "if (method_id) {" << endl; | |
1638 | |
1639 { | |
1640 Indentation indent(INDENT); | |
1641 s << INDENT << "JNIEnv *__jni_env = qtjambi_current_environment();" << endl; | |
1642 | |
1643 // This nasty case comes up when we're shutting down while receiving virtual | |
1644 // calls.. With these checks we safly abort... | |
1645 s << INDENT << "if (!__jni_env) {" << endl | |
1646 << " "; | |
1647 writeBaseClassFunctionCall(s, java_function, implementor); | |
1648 if (!java_function->type()) { | |
1649 s << INDENT << " return;" << endl; | |
1650 } | |
1651 s << INDENT << "}" << endl; | |
1652 | |
1653 // otherwise, continue with the function call... | |
1654 s << INDENT << "__jni_env->PushLocalFrame(100);" << endl; | |
1655 | |
1656 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1657 AbstractMetaArgumentList argumentsToReset; | |
1658 foreach (AbstractMetaArgument *argument, arguments) { | |
1659 if (!java_function->argumentRemoved(argument->argumentIndex()+1)) { | |
1660 if (!argument->type()->isPrimitive() | |
1661 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
1662 writeQtToJava(s, | |
1663 argument->type(), | |
1664 argument->indexedName(), | |
1665 "__java_" + argument->indexedName(), | |
1666 java_function, | |
1667 argument->argumentIndex() + 1); | |
1668 } | |
1669 | |
1670 if (java_function->resetObjectAfterUse(argument->argumentIndex()+1)) | |
1671 argumentsToReset.append(argument); | |
1672 } | |
1673 } | |
1674 | |
1675 for (int i=0; i<arguments.size(); ++i) | |
1676 writeOwnership(s, java_function, "__java_" + arguments.at(i)->indexedName(), i+1, implementor); | |
1677 | |
1678 | |
1679 AbstractMetaType *function_type = java_function->type(); | |
1680 QString new_return_type = java_function->typeReplaced(0); | |
1681 bool has_function_type = ((function_type != 0 | |
1682 || !new_return_type.isEmpty()) | |
1683 && new_return_type != "void"); | |
1684 | |
1685 s << INDENT; | |
1686 if (has_function_type) { | |
1687 if (new_return_type.isEmpty()) { | |
1688 s << translateType(function_type); | |
1689 } else { | |
1690 s << jniName(new_return_type); | |
1691 } | |
1692 s << " " << "__d_return_value = "; | |
1693 } | |
1694 | |
1695 s << "__jni_env->"; | |
1696 if (new_return_type.isEmpty()) { | |
1697 s << callXxxMethod(java_function->type()); | |
1698 } else if (!has_function_type) { | |
1699 s << "CallVoidMethod"; | |
1700 } else { | |
1701 s << callXxxMethod(new_return_type); | |
1702 } | |
1703 | |
1704 s << "(m_link->javaObject(__jni_env), method_id"; | |
1705 if (arguments.size() > 0) | |
1706 s << ", "; | |
1707 writeFunctionCallArguments(s, java_function, "__java_", Option(NoCasts | SkipRemovedArguments)); | |
1708 s << ");" << endl | |
1709 << INDENT << "qtjambi_exception_check(__jni_env);" << endl; | |
1710 | |
1711 if (has_function_type) { | |
1712 writeJavaToQt(s, function_type, "__qt_return_value", "__d_return_value", | |
1713 java_function, 0, GlobalRefJObject); | |
1714 | |
1715 if (java_function->nullPointersDisabled()) { | |
1716 s << INDENT << "if (__d_return_value == 0) {" << endl; | |
1717 { | |
1718 Indentation indent(INDENT); | |
1719 s << INDENT << "fprintf(stderr, \"QtJambi: Unexpected null pointer returned from override of '" << java_function->name() << "' in class '%s'\\n\"," << endl | |
1720 << INDENT << " qPrintable(qtjambi_object_class_name(__jni_env, m_link->javaObject(__jni_env))));" << endl; | |
1721 s << INDENT << "__qt_return_value = "; | |
1722 QString defaultValue = java_function->nullPointerDefaultValue(); | |
1723 if (!defaultValue.isEmpty()) | |
1724 s << defaultValue << ";"; | |
1725 else | |
1726 writeBaseClassFunctionCall(s, java_function, implementor, NoReturnStatement); | |
1727 s << endl; | |
1728 } | |
1729 | |
1730 s << INDENT << "}" << endl; | |
1731 } | |
1732 } else if (!java_function->conversionRule(TypeSystem::ShellCode, 0).isEmpty()) { | |
1733 writeConversionRule(s, TypeSystem::ShellCode, java_function, 0, "<invalid>", "<invalid>"); | |
1734 } | |
1735 | |
1736 writeOwnership(s, java_function, "this", -1, implementor); | |
1737 writeOwnership(s, java_function, "__d_return_value", 0, implementor); | |
1738 | |
1739 foreach (AbstractMetaArgument *argumentToReset, argumentsToReset) { | |
1740 | |
1741 QString argumentName = "__java_" + argumentToReset->indexedName(); | |
1742 | |
1743 s << INDENT; | |
1744 if (argumentToReset->type()->isContainer()) | |
1745 s << "qtjambi_invalidate_collection("; | |
1746 else | |
1747 s << "qtjambi_invalidate_object("; | |
1748 | |
1749 s << "__jni_env, " << argumentName << ");" << endl; | |
1750 } | |
1751 | |
1752 s << INDENT << "__jni_env->PopLocalFrame(0);" << endl; | |
1753 | |
1754 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> leaving: " << implementor->name() | |
1755 << "::" << java_function_signature << "\");" << endl; | |
1756 | |
1757 if (function_type) | |
1758 s << INDENT << "return __qt_return_value;" << endl; | |
1759 | |
1760 } | |
1761 | |
1762 s << INDENT << "} else {" << endl; | |
1763 | |
1764 { | |
1765 Indentation indent(INDENT); | |
1766 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> super() and leaving: " | |
1767 << implementor->name() << "::" << java_function_signature << "\");" << endl; | |
1768 writeBaseClassFunctionCall(s, java_function, implementor); | |
1769 } | |
1770 | |
1771 | |
1772 s << INDENT << "}" << endl; | |
1773 | |
1774 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1775 | |
1776 // A little trick to close open painters on a widget | |
1777 if (java_function->name() == "paintEvent") { | |
1778 s << INDENT << "JNIEnv *env = qtjambi_current_environment();" << endl | |
1779 << INDENT << "qtjambi_end_paint(env, m_link->javaObject(env));" << endl; | |
1780 } | |
1781 | |
1782 } else { | |
1783 if(java_function->isRemovedFrom(implementor, TypeSystem::TargetLangCode)){ | |
1784 // Avoid compiler warnings for unused parameters | |
1785 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1786 | |
1787 foreach (const AbstractMetaArgument *argument, arguments) { | |
1788 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
1789 } | |
1790 } | |
1791 writeBaseClassFunctionCall(s, java_function, implementor); | |
1792 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1793 } | |
1794 | |
1795 s << "}" << endl << endl; | |
1796 | |
1797 } | |
1798 | |
1799 // ### kill implementor | |
1800 | |
1801 void CppImplGenerator::writePublicFunctionOverride(QTextStream &s, | |
1802 const AbstractMetaFunction *java_function, | |
1803 const AbstractMetaClass *implementor) | |
1804 { | |
1805 Q_ASSERT(java_function->originalAttributes() | |
1806 & (AbstractMetaAttributes::Protected | |
1807 | AbstractMetaAttributes::Final)); | |
1808 | |
1809 // The write a public override version of this function to be used by native functions | |
1810 writeFunctionSignature(s, java_function, implementor, "__public_", | |
1811 Option(EnumAsInts | UnderscoreSpaces | |
1812 | (java_function->isAbstract() ? SkipName : NoOption))); | |
1813 s << endl | |
1814 << "{" << endl; | |
1815 Indentation indent(INDENT); | |
1816 writeBaseClassFunctionCall(s, java_function, implementor); | |
1817 s << "}" << endl << endl; | |
1818 } | |
1819 | |
1820 | |
1821 void CppImplGenerator::writeVirtualFunctionOverride(QTextStream &s, | |
1822 const AbstractMetaFunction *java_function, | |
1823 const AbstractMetaClass *implementor) | |
1824 { | |
1825 Q_ASSERT(!java_function->isFinalInCpp()); | |
1826 | |
1827 Option options = Option(EnumAsInts | UnderscoreSpaces); | |
1828 | |
1829 // The write a public override version of this function to be used by native functions | |
1830 writeFunctionSignature(s, java_function, implementor, "__override_", | |
1831 options, | |
21 | 1832 QString(), // the class prefix |
1833 QStringList() << "bool static_call"); | |
1 | 1834 s << endl |
1835 << "{" << endl; | |
1836 Indentation indent(INDENT); | |
21 | 1837 s << INDENT << "if (static_call) {" << endl; |
1 | 1838 { |
21 | 1839 Indentation indent(INDENT); |
1 | 1840 writeBaseClassFunctionCall(s, java_function, implementor); |
21 | 1841 } |
1 | 1842 s << INDENT << "} else {" << endl; |
1843 { | |
1844 Indentation indent(INDENT); | |
1845 writeBaseClassFunctionCall(s, java_function, implementor, VirtualCall); | |
1846 } | |
1847 | |
21 | 1848 s << INDENT << "}" << endl; |
1 | 1849 s << "}" << endl << endl; |
1850 } | |
1851 | |
1852 | |
1853 void CppImplGenerator::writeBaseClassFunctionCall(QTextStream &s, | |
1854 const AbstractMetaFunction *java_function, | |
1855 const AbstractMetaClass *, | |
1856 Option options) | |
1857 { | |
1858 bool static_call = !(options & VirtualCall); | |
1859 if ((options & NoReturnStatement) == 0) | |
1860 s << INDENT; | |
1861 if (java_function->isAbstract() && static_call) { | |
1862 s << default_return_statement_qt(java_function->type(), options) << ";" << endl; | |
1863 } else { | |
1864 if (java_function->type() && (options & NoReturnStatement) == 0) | |
1865 s << "return "; | |
1866 if (static_call) { | |
1867 const AbstractMetaClass *implementor = java_function->implementingClass(); | |
1868 if (java_function->isInterfaceFunction()) | |
1869 implementor = java_function->interfaceClass()->primaryInterfaceImplementor(); | |
1870 s << implementor->qualifiedCppName() << "::"; | |
1871 } | |
1872 s << java_function->originalName() << "("; | |
1873 writeFunctionCallArguments(s, java_function, QString(), Option(options | ForceEnumCast)); | |
1874 s << ");" << endl; | |
1875 } | |
1876 } | |
1877 | |
1878 | |
1879 void CppImplGenerator::writeFunctionName(QTextStream &s, | |
1880 const AbstractMetaFunction *java_function, | |
1881 const AbstractMetaClass *java_class, | |
1882 uint options) | |
1883 { | |
1884 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass(); | |
1885 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1886 | |
1887 // Function signature | |
1888 QString return_type = jniReturnName(java_function, options); | |
1889 | |
1890 QString function_name; | |
1891 | |
1892 bool callThrough = java_function->needsCallThrough(); | |
1893 /* qtd if (!callThrough) | |
1894 function_name = java_function->name(); | |
1895 else */ | |
1896 function_name = java_function->marshalledName(); | |
1897 | |
1898 QString args = "__"; | |
1899 | |
1900 if (callThrough && !java_function->isStatic() && !java_function->isConstructor()) | |
1901 args += "J"; | |
1902 | |
1903 if (!arguments.isEmpty()) { | |
1904 foreach (const AbstractMetaArgument *argument, arguments) { | |
1905 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
1906 if (!argument->type()->hasNativeId()) { | |
1907 QString modified_type = java_function->typeReplaced(argument->argumentIndex()+1); | |
1908 if (modified_type.isEmpty()) | |
1909 args += jni_signature(argument->type(), Underscores); | |
1910 else | |
1911 args += jni_signature(modified_type, Underscores); | |
1912 } else { | |
1913 args += "J"; | |
1914 } | |
1915 } | |
1916 } | |
1917 } | |
1918 | |
1919 s << jni_function_signature(cls->package(), cls->name(), function_name, | |
1920 return_type, args, options); | |
1921 | |
1922 } | |
1923 | |
1924 void CppImplGenerator::writeFinalFunctionArguments(QTextStream &s, const AbstractMetaFunction *java_function, bool d_export) | |
1925 { | |
1926 bool callThrough = java_function->needsCallThrough(); | |
1927 s << "("; | |
1928 /* qtd | |
1929 << "JNIEnv *__jni_env," << endl; | |
1930 if (!java_function->isConstructor()) { | |
1931 if (java_function->isStatic()) | |
1932 s << " jclass"; | |
1933 else | |
1934 s << " jobject"; | |
1935 } else | |
1936 s << " jobject " << java_object_name; | |
1937 */ | |
1938 uint nativeArgCount = 0; | |
1939 const AbstractMetaClass *cls = java_function->ownerClass(); | |
1940 if (java_function->isConstructor() && | |
1941 ( cls->hasVirtualFunctions() | |
1942 || cls->typeEntry()->isObject() ) ) | |
1943 { | |
1944 s << "void *d_ptr"; | |
1945 nativeArgCount++; | |
1946 } | |
1947 | |
1948 // passing pointer to C++ object | |
1949 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
1950 if (hasNativeId) { | |
1951 if (nativeArgCount > 0) | |
1952 s << "," << endl << " "; | |
1953 /* qtd << "," << endl */ s << "void* __this_nativeId"; | |
1954 nativeArgCount++; | |
1955 } | |
1956 | |
1957 AbstractMetaType *f_type = java_function->type(); | |
1958 | |
1959 // return values as strings, arrays or QModelIndex'es we return as arguments | |
1960 bool return_arg = false; | |
1961 if (f_type) { | |
1962 if (f_type->isTargetLangString() || | |
1963 f_type->isContainer() || | |
81 | 1964 f_type->name() == "QModelIndex" || |
1965 f_type->typeEntry()->isStructInD()) | |
1 | 1966 return_arg = true; |
1967 | |
1968 if (return_arg && nativeArgCount > 0) | |
1969 s << "," << endl << " "; | |
1970 | |
1971 if (f_type->isTargetLangString() || f_type->isContainer()) | |
1972 s << "void*"; | |
1973 else if (f_type->name() == "QModelIndex") | |
1974 s << "QModelIndexAccessor*"; | |
81 | 1975 else if (f_type->typeEntry()->isStructInD()) |
1976 s << f_type->typeEntry()->qualifiedCppName() << " *"; | |
1 | 1977 |
1978 if(return_arg) { | |
1979 s << " __d_return_value"; | |
1980 nativeArgCount++; | |
1981 } | |
1982 } | |
1983 | |
1984 // the function arguments | |
1985 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1986 foreach (const AbstractMetaArgument *argument, arguments) { | |
1987 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
1988 AbstractMetaType *d_type = argument->type(); | |
1989 const TypeEntry *te = d_type->typeEntry(); | |
1990 | |
1991 QString arg_name = argument->indexedName(); | |
1992 | |
1993 if (nativeArgCount > 0) | |
1994 s << "," << endl << " "; | |
81 | 1995 |
1996 // if QString argument we have to pass DArray | |
35 | 1997 if ((te && te->qualifiedCppName() == "QString") || d_type->isTargetLangString()) { |
1998 if (d_export) | |
1999 s << "string " << arg_name; | |
2000 else | |
2001 s << "DArray " << arg_name; | |
2002 } else if (d_type->isContainer()) { | |
1 | 2003 const ContainerTypeEntry *cte = |
2004 static_cast<const ContainerTypeEntry *>(te); | |
2005 if(isLinearContainer(cte)) | |
2006 s << QString("void *%1, size_t %1_size").arg(arg_name); | |
2007 } else { | |
2008 if (!d_type->hasNativeId()) { | |
2009 if(d_type->isVariant()) { | |
2010 if (d_export) s << "void*"; | |
2011 else s << "QVariant*"; | |
2012 } else | |
2013 s << translateType(argument->type(), EnumAsInts, d_export); | |
2014 } | |
2015 else if (d_type->name() == "QModelIndex") | |
2016 s << "QModelIndexAccessor"; | |
2017 else if (te->isStructInD()) | |
2018 s << te->qualifiedCppName(); | |
2019 else | |
2020 s << "void*"; | |
2021 | |
2022 s << " " << arg_name; | |
2023 } | |
2024 nativeArgCount++; | |
2025 } | |
2026 } | |
2027 s << ")"; | |
2028 } | |
2029 | |
2030 | |
2031 /*! | |
2032 Generates type conversion from Java -> Qt for all the arguments | |
2033 that are to be to be passed to the function | |
2034 */ | |
2035 void CppImplGenerator::writeFinalFunctionSetup(QTextStream &s, const AbstractMetaFunction *java_function, | |
2036 const QString &qt_object_name, | |
2037 const AbstractMetaClass *cls) | |
2038 { | |
2039 | |
2040 // Translate each of the function arguments into qt types | |
2041 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2042 foreach (const AbstractMetaArgument *argument, arguments) { | |
2043 if (!argument->type()->isPrimitive() | |
2044 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex() + 1).isEmpty()) { | |
2045 writeJavaToQt(s, | |
2046 argument->type(), | |
2047 "__qt_" + argument->indexedName(), | |
2048 argument->indexedName(), | |
2049 java_function, | |
2050 argument->argumentIndex() + 1, | |
2051 Option(UseNativeIds | EnumAsInts)); | |
2052 } | |
2053 } | |
2054 | |
2055 // Extract the qt equivalent to the this pointer and name it "qt_object_name" | |
2056 if (!java_function->isStatic() && !java_function->isConstructor()) { | |
2057 // qtd2 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls); | |
2058 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls); | |
2059 s << INDENT | |
2060 << className << " *" << qt_object_name | |
2061 << " = (" << className << " *) __this_nativeId;" | |
2062 << endl; | |
2063 // << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
2064 // qtd << INDENT << "Q_ASSERT(" << qt_object_name << ");" << endl; | |
2065 } | |
2066 } | |
2067 | |
2068 void CppImplGenerator::writeFinalFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
2069 const AbstractMetaClass *java_class) | |
2070 { | |
2071 Q_ASSERT(java_class); | |
2072 | |
2073 if (java_function->isModifiedRemoved(TypeSystem::NativeCode)) | |
2074 return; | |
2075 | |
2076 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass(); | |
2077 | |
2078 QString java_function_signature = cls->name() + "::" + java_function->signature(); | |
2079 | |
2080 s << "// " << java_function_signature << endl; | |
2081 | |
2082 const AbstractMetaType *function_type = java_function->type(); | |
2083 QString new_return_type = java_function->typeReplaced(0); | |
2084 bool has_function_type = new_return_type != "void" | |
2085 && (!new_return_type.isEmpty() || function_type != 0) | |
2086 && java_function->argumentReplaced(0).isEmpty(); | |
2087 | |
2088 const QString qt_object_name = java_function->isStatic() ? shellClassName(cls) : "__qt_this"; | |
2089 const QString java_object_name = java_function->isStatic() ? "__jni_class" : "__jni_object"; | |
2090 | |
2091 // we are not wrapping some functions depending on arguments because API is not yet full | |
2092 if (notWrappedYet(java_function)) | |
2093 return; | |
2094 | |
2095 // function signature... | |
2096 bool callThrough = java_function->needsCallThrough(); | |
2097 uint options = m_native_jump_table ? ReturnType | ExternC : StandardJNISignature; | |
2098 writeFunctionName(s, java_function, cls, options); | |
2099 s << endl; | |
2100 writeFinalFunctionArguments(s, java_function); | |
2101 s << endl << "{" << endl; | |
2102 Indentation indent(INDENT); | |
2103 | |
2104 // qtd2 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) entering: " << java_function_signature << "\");" << endl; | |
2105 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
2106 /* if (hasNativeId) | |
2107 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl; | |
2108 // Avoid compiler warnings when the variables are unused | |
2109 { | |
2110 s << INDENT << "Q_UNUSED(__jni_env)" << endl; | |
2111 | |
2112 if (java_function->isConstructor()) | |
2113 s << INDENT << "Q_UNUSED(" << java_object_name << ")" << endl; | |
2114 | |
2115 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
2116 if (hasNativeId) | |
2117 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl; | |
2118 } | |
2119 */ | |
2120 | |
2121 if (cls->isFinal() && (!java_function->isAbstract() || !java_function->isFinalInTargetLang()) && !java_function->wasPublic()) { | |
2122 QString debug = QString("protected function '%1' in final class '%2'") | |
2123 .arg(java_function->signature()).arg(java_class->name()); | |
2124 ReportHandler::warning(debug); | |
2125 // Avoid compiler warnings for unused parameters | |
2126 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2127 | |
2128 foreach (const AbstractMetaArgument *argument, arguments) { | |
2129 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
2130 } | |
2131 s << INDENT << default_return_statement_qt(java_function->type()) << ""; | |
2132 } else { | |
2133 writeFinalFunctionSetup(s, java_function, qt_object_name, cls); | |
2134 | |
2135 writeCodeInjections(s, java_function, java_function->implementingClass(), CodeSnip::Beginning, TypeSystem::NativeCode); | |
2136 | |
2137 if (java_function->isConstructor()) { | |
2138 writeFinalConstructor(s, java_function, qt_object_name, java_object_name); | |
2139 } else { | |
2140 | |
2141 QString function_prefix = ""; | |
2142 QStringList extra_param; | |
2143 Option option = NoOption; | |
2144 | |
2145 bool hasShell = cls->generateShellClass(); | |
2146 | |
2147 if (java_function->isFinalOverload()) { | |
2148 // no prefix | |
2149 } else if (java_function->isFinalInCpp() && !java_function->wasPublic() && hasShell) { | |
2150 function_prefix = "__public_"; | |
2151 } else if (!java_function->isFinalInCpp() && !java_function->isStatic() && hasShell) { | |
2152 function_prefix = "__override_"; | |
21 | 2153 extra_param.append("__do_static_call"); |
1 | 2154 s << INDENT |
21 | 2155 << "bool __do_static_call = __this_nativeId ? " |
2156 << "__" << java_class->name() << "_entity(__this_nativeId) : false;" << endl; | |
2157 } else { | |
1 | 2158 option = OriginalName; |
2159 } | |
2160 | |
2161 // Call the Qt function on the java object | |
2162 s << " "; | |
2163 if (has_function_type) { | |
2164 const QString qt_return_value = "__qt_return_value"; | |
2165 const QString java_return_value = "__d_return_value"; | |
2166 if (function_type) { | |
2167 writeTypeInfo(s, function_type, EnumAsInts); | |
2168 s << " " << qt_return_value | |
2169 << " = "; | |
2170 } | |
2171 | |
2172 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, | |
2173 extra_param); | |
2174 s << endl; | |
2175 | |
104 | 2176 writeRefArguments(s, java_function); |
2177 | |
1 | 2178 writeQtToJava(s, function_type, qt_return_value, java_return_value, |
2179 java_function, 0, EnumAsInts); | |
2180 | |
2181 // qtd s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) -> leaving: " | |
2182 // << java_function_signature << "\");"; | |
2183 | |
2184 if (function_type && function_type->name() != "QModelIndex") { | |
2185 if(function_type->typeEntry()->isStructInD()) | |
81 | 2186 ; //s << endl << INDENT << "return " << qt_return_value << ";"; |
1 | 2187 else if (!function_type->isTargetLangString() && !function_type->isContainer()) |
2188 s << endl << INDENT << "return " << java_return_value << ";"; | |
2189 } | |
2190 | |
2191 } else { | |
2192 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, | |
2193 extra_param); | |
104 | 2194 writeRefArguments(s, java_function); |
1 | 2195 } |
2196 } | |
2197 } | |
2198 if(!java_function->argumentReplaced(0).isEmpty()) { | |
2199 s << INDENT << "return 0;" << endl; | |
2200 } | |
2201 | |
2202 s << endl << "}"; | |
2203 s << endl << endl; | |
2204 } | |
2205 | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2206 void CppImplGenerator::writeRefArguments(QTextStream &s, const AbstractMetaFunction *java_function) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2207 { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2208 AbstractMetaArgumentList arguments = java_function->arguments(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2209 foreach (const AbstractMetaArgument *argument, arguments) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2210 AbstractMetaType *d_type = argument->type(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2211 const TypeEntry *te = d_type->typeEntry(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2212 if ((te && d_type->isNativePointer() && te->name() == "QString")) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2213 s << QString(" _d_toUtf8(__qt_%1.utf16(), __qt_%1.size(), &%1);").arg(argument->indexedName()) << endl; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2214 } |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2215 } |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2216 |
1 | 2217 void CppImplGenerator::writeAssignment(QTextStream &s, const QString &destName, const QString &srcName, |
2218 const AbstractMetaType *java_type) | |
2219 { | |
2220 if (java_type->isArray()) { | |
2221 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2222 writeAssignment(s, destName + "[" + QString::number(i) + "]", | |
2223 srcName + "[" + QString::number(i) + "]", java_type->arrayElementType()); | |
2224 } | |
2225 } else { | |
2226 s << INDENT << destName << " = " << srcName << ";" << endl; | |
2227 } | |
2228 } | |
2229 | |
2230 void CppImplGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *java_field) | |
2231 { | |
2232 Q_ASSERT(java_field); | |
2233 Q_ASSERT(java_field->isPublic() || java_field->isProtected()); | |
2234 | |
2235 const AbstractMetaFunction *setter = java_field->setter(); | |
2236 const AbstractMetaFunction *getter = java_field->getter(); | |
2237 | |
2238 const AbstractMetaClass *cls = java_field->enclosingClass(); | |
2239 FieldModification mod = cls->typeEntry()->fieldModification(java_field->name()); | |
2240 | |
2241 if(notWrappedYet(getter)) | |
2242 return; | |
2243 | |
2244 if (mod.isReadable()) { | |
2245 // Getter | |
2246 if (getter->wasProtected()) { | |
2247 writeFunctionSignature(s, getter, getter->ownerClass()); | |
2248 s << endl | |
2249 << "{" << endl; | |
2250 { | |
2251 Indentation indent(INDENT); | |
2252 s << INDENT << "return " << java_field->name() << ";" << endl; | |
2253 } | |
2254 s << "}" << endl << endl; | |
2255 } | |
2256 | |
2257 | |
2258 writeFunctionName(s, getter, getter->ownerClass()); | |
2259 s << endl; | |
2260 writeFinalFunctionArguments(s, getter); | |
2261 s << "{" << endl; | |
2262 { | |
2263 Indentation indent(INDENT); | |
2264 | |
2265 | |
2266 writeFinalFunctionSetup(s, getter, "__qt_object", getter->ownerClass()); | |
2267 | |
2268 const QString java_return_value = "__d_return_value"; | |
2269 QString qt_return_value; | |
2270 if (setter->isStatic()) | |
2271 qt_return_value = shellClassName(setter->ownerClass()) + "::"; | |
2272 else | |
2273 qt_return_value = "__qt_object->"; | |
2274 | |
2275 | |
2276 // To avoid "taking address of tmp" | |
2277 s << INDENT; | |
2278 writeTypeInfo(s, getter->type(), Option(ArrayAsPointer)); | |
2279 QString tmp_name = "__tmp_" + getter->name(); | |
2280 s << tmp_name << " = "; | |
2281 | |
2282 if (getter->wasPublic()) | |
2283 qt_return_value += java_field->name(); | |
2284 else | |
2285 qt_return_value += getter->name() + "_getter()"; | |
2286 s << qt_return_value << ";" << endl; | |
2287 | |
2288 writeQtToJava(s, getter->type(), tmp_name, java_return_value, 0, -1, EnumAsInts); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2289 if (getter->type()->isTargetLangString() || getter->type()->name() == "QModelIndex") |
1 | 2290 ; |
2291 else if(getter->type()->typeEntry()->isStructInD()) | |
81 | 2292 ; //s << INDENT << "return " << tmp_name << ";" << endl; |
1 | 2293 else |
2294 s << INDENT << "return " << java_return_value << ";" << endl; | |
2295 } | |
2296 s << "}" << endl << endl; | |
2297 } | |
2298 | |
2299 if(notWrappedYet(setter)) | |
2300 return; | |
2301 | |
2302 // Setter | |
2303 if (mod.isWritable() && !java_field->type()->isConstant()) { | |
2304 // Write public override for protected fields | |
2305 if (setter->wasProtected()) { | |
2306 writeFunctionSignature(s, setter, setter->ownerClass()); | |
2307 s << endl | |
2308 << "{" << endl; | |
2309 { | |
2310 Indentation indent(INDENT); | |
2311 | |
2312 Q_ASSERT(setter->arguments().count() > 0); | |
2313 const AbstractMetaArgument *argument = setter->arguments().at(0); | |
2314 | |
2315 QString thisRef = java_field->isStatic() | |
2316 ? setter->ownerClass()->qualifiedCppName() + QString("::") | |
2317 : QString("this->"); | |
2318 writeAssignment(s, thisRef + java_field->name(), argument->indexedName(), argument->type()); | |
2319 } | |
2320 s << "}" << endl << endl; | |
2321 } | |
2322 | |
2323 writeFunctionName(s, setter, setter->ownerClass()); | |
2324 s << endl; | |
2325 writeFinalFunctionArguments(s, setter); | |
2326 s << "{" << endl; | |
2327 | |
2328 { | |
2329 Indentation indent(INDENT); | |
2330 | |
2331 writeFinalFunctionSetup(s, setter, "__qt_object", setter->ownerClass()); | |
2332 | |
2333 Q_ASSERT(setter->arguments().count() == 1); | |
2334 const AbstractMetaArgument *argument = setter->arguments().at(0); | |
2335 | |
2336 QString dest; | |
2337 if (setter->isStatic()) | |
2338 dest = shellClassName(setter->ownerClass()) + "::"; | |
2339 else | |
2340 dest = "__qt_object->"; | |
2341 | |
2342 QString src; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2343 if (!argument->type()->isPrimitive() && !argument->type()->typeEntry()->isStructInD()) { |
1 | 2344 src = "__qt_" + argument->indexedName(); |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2345 } else if (argument->type()->name() == "QModelIndex") { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2346 src = "qtd_to_QModelIndex(" + argument->indexedName() + ")"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2347 } else |
1 | 2348 src = argument->indexedName(); |
2349 | |
2350 if (setter->wasPublic()) | |
2351 writeAssignment(s, dest + java_field->name(), src, argument->type()); | |
2352 else | |
2353 s << INDENT << dest << setter->name() << "_setter(" << src << ");" << endl; | |
2354 } | |
2355 s << "}" << endl << endl; | |
2356 } | |
2357 } | |
2358 | |
2359 void CppImplGenerator::writeFinalDestructor(QTextStream &s, const AbstractMetaClass *cls) | |
2360 { | |
2361 if (cls->hasConstructors()) { | |
2362 s << INDENT << "extern \"C\" DLL_PUBLIC void qtd_" << cls->name() << "_destructor(void *ptr)" << endl | |
2363 << INDENT << "{" << endl; | |
2364 { | |
2365 Indentation indent(INDENT); | |
2366 /* qtd | |
2367 if (!cls->isQObject() && !cls->generateShellClass()) { | |
2368 s << INDENT << "QtJambiLink *link = QtJambiLink::findLinkForUserObject(ptr);" << endl | |
2369 << INDENT << "if (link) link->resetObject(qtjambi_current_environment());" << endl; | |
2370 } | |
2371 | |
2372 // Code injectsions... | |
2373 foreach (const CodeSnip &snip, cls->typeEntry()->codeSnips()) { | |
2374 if (snip.language == TypeSystem::DestructorFunction) { | |
2375 s << snip.code(); | |
2376 } | |
2377 } | |
2378 */ | |
2379 s << INDENT << "delete (" << shellClassName(cls) << " *)ptr;" << endl; | |
2380 | |
2381 #if defined(QTJAMBI_DEBUG_TOOLS) | |
2382 s << INDENT << "qtjambi_increase_destructorFunctionCalledCount(QString::fromLatin1(\"" << cls->name() << "\"));" << endl; | |
2383 #endif | |
2384 } | |
2385 | |
2386 s << INDENT << "}" << endl << endl; | |
2387 } | |
2388 } | |
2389 | |
2390 void CppImplGenerator::writeFinalConstructor(QTextStream &s, | |
2391 const AbstractMetaFunction *java_function, | |
2392 const QString &qt_object_name, | |
2393 const QString &java_object_name) | |
2394 { | |
2395 const AbstractMetaClass *cls = java_function->ownerClass(); | |
2396 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2397 QString className = cls->typeEntry()->qualifiedCppName(); | |
2398 | |
2399 bool hasShellClass = cls->generateShellClass(); | |
2400 | |
2401 s << INDENT << shellClassName(cls) << " *" << qt_object_name | |
2402 << " = new " << shellClassName(cls) | |
2403 << "("; | |
2404 writeFunctionCallArguments(s, java_function, "__qt_"); | |
2405 s << ");" << endl; | |
2406 s << INDENT << "return (void *) " << qt_object_name << ";" << endl; // qtd | |
2407 | |
2408 /* qtd s << INDENT << "QtJambiLink *__qt_java_link = "; | |
2409 if (cls->isQObject()) { | |
2410 s << "qtjambi_construct_qobject(__jni_env, " << java_object_name << ", " | |
2411 << qt_object_name << ")"; | |
2412 } else { | |
2413 s << "qtjambi_construct_object(__jni_env, " << java_object_name << ", " << qt_object_name; | |
2414 if (cls->typeEntry()->isValue()) | |
2415 s << ", \"" << className << "\")"; | |
2416 else // non-QObject, object type | |
2417 s << ", QMetaType::Void, QLatin1String(\"" << cls->fullName().replace(".", "/") << "\"), true)"; | |
2418 } | |
2419 s << ";" << endl | |
2420 << INDENT << "if (!__qt_java_link) {" << endl; | |
2421 { | |
2422 Indentation indent(INDENT); | |
2423 s << INDENT << "qWarning(\"object construction failed for type: " | |
2424 << className << "\");" << endl | |
2425 << INDENT << "return;" << endl; | |
2426 } | |
2427 s << INDENT << "}" << endl; | |
2428 | |
2429 | |
2430 if (cls->isQObject()) { | |
2431 // Make sure all qobjects created by Java are owned by java only if | |
2432 // parent object has not been set. | |
2433 // All other objects will default to split ownership. | |
2434 s << INDENT << "if(!__qt_this->QObject::parent()){" << endl; | |
2435 s << INDENT << " __qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2436 s << INDENT << "}" << endl; | |
2437 } else { | |
2438 // All non-qobjects created by Java are owned by java | |
2439 s << INDENT << "__qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2440 } | |
2441 | |
2442 if (hasCustomDestructor(cls)) { | |
2443 s << INDENT << "__qt_java_link->setDestructorFunction(qtjambi_destructor);" << endl; | |
2444 } | |
2445 | |
2446 if (cls->typeEntry()->typeFlags() & ComplexTypeEntry::DeleteInMainThread) | |
2447 s << INDENT << "__qt_java_link->setDeleteInMainThread(true);" << endl; | |
2448 | |
2449 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions() && !cls->typeEntry()->isObject()) | |
2450 return; | |
2451 | |
2452 if (hasShellClass) { | |
2453 // Set up the link object | |
2454 s << INDENT << qt_object_name << "->m_link = __qt_java_link;" << endl; | |
2455 | |
2456 // Make sure the user data in the QObject has bindings to the qobject's meta object | |
2457 // (this has to be done after the link is set, so that the fake meta object | |
2458 // can access the java object, for which it gets a reference in the link) | |
2459 if (cls->isQObject()) | |
2460 s << INDENT << qt_object_name << "->m_link->setMetaObject(" << qt_object_name << "->metaObject());" << endl; | |
2461 | |
2462 s << INDENT << qt_object_name << "->m_link->setCreatedByJava(true);" << endl; | |
2463 | |
2464 | |
2465 | |
2466 AbstractMetaClassList interfaces = cls->interfaces(); | |
2467 if (interfaces.size() + (cls->baseClass() != 0 ? 1 : 0) > 1) { | |
2468 if (cls->baseClass() != 0) | |
2469 interfaces += cls->baseClass(); | |
2470 foreach (AbstractMetaClass *iface, interfaces) { | |
2471 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface; | |
2472 s << INDENT << qt_object_name << "->m_link->registerSubObject((" << impl->qualifiedCppName() << " *) " << qt_object_name << ");" << endl; | |
2473 } | |
2474 } | |
2475 } | |
2476 | |
2477 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions()) | |
2478 return; | |
2479 | |
2480 // Set up the vtable | |
2481 s << INDENT; | |
2482 QString space(24, ' '); | |
2483 if (hasShellClass) | |
2484 s << qt_object_name << "->m_vtable = "; | |
2485 s << "qtjambi_setup_vtable(__jni_env, " << endl << space << "__jni_object, " << endl; | |
2486 | |
2487 if (cls->hasInconsistentFunctions()) { | |
2488 s << space << "qtjambi_inconsistent_count, " << endl | |
2489 << space << "qtjambi_inconsistent_names, " << endl | |
2490 << space << "qtjambi_inconsistent_signatures, " << endl; | |
2491 } else { | |
2492 s << space << "0, 0, 0, // no inconsistent functions" << endl; | |
2493 } | |
2494 | |
2495 if (cls->hasVirtualFunctions()) { | |
2496 s << space << "qtjambi_method_count, " << endl | |
2497 << space << "qtjambi_method_names, " << endl | |
2498 << space << "qtjambi_method_signatures" << endl; | |
2499 } else { | |
2500 s << space << "0, 0, 0 // no virtual functions" << endl; | |
2501 } | |
2502 | |
2503 s << space << ");" << endl; */ | |
2504 } | |
2505 | |
2506 void CppImplGenerator::writeSignalInitialization(QTextStream &s, const AbstractMetaClass *java_class) | |
2507 { | |
2508 if (!java_class->isQObject() | |
2509 || java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromTargetLang).size() == 0) { | |
2510 return ; | |
2511 } | |
2512 | |
2513 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_signalInitialization", "jboolean") | |
2514 << endl << "(JNIEnv *__jni_env, jobject java_object, jlong ptr, jstring java_signal_name)" << endl | |
2515 << "{" << endl | |
2516 << " QtJambiLink *link = (QtJambiLink *) ptr;" << endl | |
2517 << " if (link == 0)" << endl | |
2518 << " return true;" << endl << endl | |
2519 << " QObject *qt_this = link->qobject();" << endl | |
2520 << " Q_ASSERT(qt_this);" << endl << endl | |
2521 << " QtJambi_SignalWrapper_" << java_class->name() << " *qt_wrapper = " | |
2522 << " (QtJambi_SignalWrapper_" << java_class->name() << " *) link->signalWrapper();" << endl | |
2523 << " if (qt_wrapper == 0) {" << endl | |
2524 << " qt_wrapper = new QtJambi_SignalWrapper_" << java_class->name() << ";" << endl | |
2525 << " link->setSignalWrapper(qt_wrapper);" << endl | |
2526 << " qt_wrapper->link = link;" << endl << endl | |
2527 << " qtjambi_resolve_signals(__jni_env," << endl | |
2528 << " java_object," << endl | |
2529 << " qt_wrapper->m_signals," << endl | |
2530 << " qtjambi_signal_count," << endl | |
2531 << " (char **) qtjambi_signal_names," << endl | |
2532 << " (int *) qtjambi_signal_argumentcounts);" << endl | |
2533 << " }" << endl | |
2534 << " QString signal_name = qtjambi_to_qstring(__jni_env, java_signal_name);" << endl | |
2535 << " return qtjambi_connect_cpp_to_java(__jni_env," << endl | |
2536 << " signal_name," << endl | |
2537 << " qt_this," << endl | |
2538 << " qt_wrapper," << endl | |
2539 << " QLatin1String(\"" << java_class->fullName() << "\")," << endl | |
2540 << " QLatin1String(\"" << signalWrapperPrefix() << "\"));" << endl | |
2541 << "}"; | |
2542 } | |
2543 | |
2544 QString CppImplGenerator::fromObject(const TypeEntry *entry, | |
2545 const QString &var_name) | |
2546 { | |
2547 QString returned; | |
2548 QString package = entry->javaPackage(); | |
2549 const ComplexTypeEntry *centry = entry->isComplex() | |
2550 ? static_cast<const ComplexTypeEntry *>(entry) | |
2551 : 0; | |
2552 | |
2553 if (centry == 0 || centry->polymorphicIdValue().isEmpty()) { | |
2554 /* qtd returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2555 + entry->lookupName() | |
2556 + "\", \"" + QString(package).replace(".", "/") + "/\", true);"; | |
2557 */ | |
2558 if(entry->isObject()) | |
2559 returned = var_name + ";"; | |
2560 else | |
2561 returned = "new " + entry->lookupName() + "(" + var_name +");"; | |
2562 } else { | |
2563 AbstractMetaClass *cls = classes().findClass(centry->qualifiedCppName()); | |
2564 if (!cls) { | |
2565 qFatal("CppImplGenerator::fromObject(): class '%s' could not be resolved...", | |
2566 qPrintable(centry->qualifiedCppName())); | |
2567 } | |
2568 | |
2569 while (cls != 0 && !cls->typeEntry()->isPolymorphicBase()) | |
2570 cls = cls->baseClass(); | |
2571 | |
2572 QString full_name; | |
2573 if (cls != 0) { | |
2574 full_name = cls->fullName(); | |
2575 } else { | |
2576 ReportHandler::warning(QString("class '%1' has polymorphic id but does not inherit a polymorphic class") | |
2577 .arg(centry->qualifiedCppName())); | |
2578 } | |
2579 /* qtd | |
2580 returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2581 + centry->lookupName() | |
2582 + "\", \"" + QString(package).replace(".", "/") + "/\"," | |
2583 + "\"" + jni_signature(full_name, Underscores) + "\", true); // fucking complex"; | |
2584 */ | |
2585 if(entry->isObject()) | |
2586 returned = var_name + "; // complex entry"; | |
2587 else | |
2588 returned = "new " + centry->lookupName() + "(" + var_name +"); // complex entry"; | |
2589 } | |
2590 | |
2591 return returned; | |
2592 } | |
2593 | |
2594 void CppImplGenerator::writeOriginalMetaObjectFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2595 { | |
2596 Q_ASSERT(java_class->isQObject()); | |
2597 | |
2598 s << jni_function_signature(java_class->package(), | |
2599 java_class->name(), | |
2600 "originalMetaObject", | |
2601 "jlong"); | |
2602 | |
2603 s << endl | |
2604 << "(JNIEnv *," << endl | |
2605 << " jclass)" << endl | |
2606 << "{" << endl; | |
2607 { | |
2608 Indentation indent(INDENT); | |
2609 s << INDENT << "return reinterpret_cast<jlong>(&" << java_class->qualifiedCppName() << "::staticMetaObject);" << endl; | |
2610 } | |
2611 s << "}" << endl << endl; | |
2612 } | |
2613 | |
2614 void CppImplGenerator::writeFromNativeFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2615 { | |
2616 s << jni_function_signature(java_class->package(), | |
2617 java_class->name(), | |
2618 "fromNativePointer", | |
2619 "jobject"); | |
2620 s << endl | |
2621 << "(JNIEnv *__jni_env," << endl | |
2622 << " jclass," << endl | |
2623 << " jobject nativePointer)" << endl | |
2624 << "{" << endl; | |
2625 { | |
2626 Indentation indent(INDENT); | |
2627 s << INDENT << "void *ptr = qtjambi_to_cpointer(__jni_env, nativePointer, 1);" << endl | |
2628 << INDENT << "return " << fromObject(java_class->typeEntry(), "ptr") << endl | |
2629 << "}" << endl; | |
2630 } | |
2631 } | |
2632 | |
2633 void CppImplGenerator::writeFromArrayFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2634 { | |
2635 s << jni_function_signature(java_class->package(), | |
2636 java_class->name(), | |
2637 "nativePointerArray", | |
2638 "jobject"); | |
2639 s << endl | |
2640 << "(JNIEnv *__jni_env," << endl | |
2641 << " jclass," << endl | |
2642 << " jobjectArray array)" << endl | |
2643 << "{" << endl; | |
2644 { | |
2645 Indentation indent(INDENT); | |
2646 s << INDENT << "return qtjambi_array_to_nativepointer(__jni_env, " << endl | |
2647 << INDENT << " array, " << endl | |
2648 << INDENT << " sizeof(" | |
2649 << java_class->qualifiedCppName() << "));" << endl; | |
2650 } | |
2651 s << "}" << endl; | |
2652 } | |
2653 | |
2654 | |
2655 void CppImplGenerator::writeInterfaceCastFunction(QTextStream &s, | |
2656 const AbstractMetaClass *java_class, | |
2657 const AbstractMetaClass *interface) | |
2658 { | |
2659 Q_ASSERT(interface->isInterface()); | |
2660 const InterfaceTypeEntry *ie = static_cast<const InterfaceTypeEntry *>(interface->typeEntry()); | |
2661 QString interface_name = ie->origin()->targetLangName(); | |
2662 | |
2663 s << endl | |
2664 << jni_function_signature(java_class->package(), | |
2665 java_class->name(), | |
2666 QString("__qt_cast_to_%1").arg(interface_name), | |
2667 "jlong", | |
2668 "__J"); | |
2669 | |
2670 s << endl | |
2671 << "(JNIEnv *," << endl | |
2672 << " jobject," << endl | |
2673 << " jlong ptr)" << endl | |
2674 << "{" << endl | |
2675 << " return (jlong) (" << interface->primaryInterfaceImplementor()->qualifiedCppName() << " *) " | |
2676 << "(" << java_class->qualifiedCppName() << " *) ptr;" << endl | |
2677 << "}" << endl; | |
2678 } | |
2679 | |
2680 bool CppImplGenerator::writeConversionRule(QTextStream &s, | |
2681 TypeSystem::Language target_language, | |
2682 const AbstractMetaFunction *java_function, | |
2683 int argument_index, | |
2684 const QString &qt_name, | |
2685 const QString &java_name) | |
2686 { | |
2687 if (argument_index < 0 || java_function == 0) | |
2688 return false; | |
2689 | |
2690 QString conversion_rule = java_function->conversionRule(target_language, argument_index); | |
2691 | |
2692 if (!conversion_rule.isEmpty()) { | |
2693 QString qt_name_var; | |
2694 QString java_name_var; | |
2695 | |
2696 if ((argument_index == 0 && target_language == TypeSystem::NativeCode) | |
2697 || (argument_index != 0 && target_language == TypeSystem::ShellCode)) { | |
2698 qt_name_var = "%in"; | |
2699 java_name_var = "%out"; | |
2700 } else { | |
2701 qt_name_var = "%out"; | |
2702 java_name_var = "%in"; | |
2703 } | |
2704 | |
2705 conversion_rule = conversion_rule.replace(qt_name_var, qt_name) | |
2706 .replace(java_name_var, java_name); | |
2707 | |
2708 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2709 for (int i=0; i<arguments.size(); ++i) { | |
2710 conversion_rule = conversion_rule.replace("%" + QString::number(i+1), | |
2711 arguments.at(i)->indexedName()); | |
2712 } | |
2713 | |
2714 QStringList lines = conversion_rule.split("\n"); | |
2715 foreach (QString line, lines) { | |
2716 s << INDENT << line.trimmed() << endl; | |
2717 } | |
2718 | |
2719 return true; | |
2720 } else { | |
2721 return false; | |
2722 } | |
2723 } | |
2724 | |
2725 | |
2726 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2727 const AbstractMetaClass *java_class, | |
2728 const AbstractMetaType *function_return_type, | |
2729 const QString &qt_name, | |
2730 const QString &java_name, | |
2731 const AbstractMetaFunction *java_function, | |
2732 int argument_index) | |
2733 { | |
2734 // Conversion to C++: Shell code for return values, native code for arguments | |
2735 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2736 if (writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2737 return; | |
2738 | |
2739 s << INDENT << shellClassName(java_class) << " *" << qt_name << " = (" | |
2740 << shellClassName(java_class) << " *) "; | |
2741 if (java_class->isQObject()) | |
2742 s << "qtjambi_to_qobject"; | |
2743 else | |
2744 s << "qtjambi_to_object"; | |
2745 s << "(__jni_env, " << java_name << ");" << endl; | |
2746 if (java_class->isQObject()) { | |
2747 // ### throw exceptions when objects are null... | |
2748 s << INDENT << "if (!" << qt_name << ") " | |
2749 << default_return_statement_java(function_return_type) << ";" << endl << endl; | |
2750 } | |
2751 } | |
2752 | |
2753 | |
2754 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2755 const AbstractMetaType *java_type, | |
2756 const QString &qt_name, | |
2757 const QString &java_name, | |
2758 const AbstractMetaFunction *java_function, | |
2759 int argument_index, | |
2760 Option options) | |
2761 { | |
2762 // Conversion to C++: Shell code for return values, native code for arguments | |
2763 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2764 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2765 return; | |
2766 | |
2767 if (java_type == 0) { | |
2768 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
2769 "language '%4'") | |
2770 .arg(argument_index) | |
2771 .arg(java_function->implementingClass()->name()) | |
2772 .arg(java_function->name()) | |
2773 .arg(int(lang)); | |
2774 ReportHandler::warning(warn); | |
2775 return; | |
2776 } | |
2777 if (java_type->name() == "QModelIndex") { | |
2778 s << INDENT << "QModelIndex " << qt_name << " = qtd_to_QModelIndex(" | |
2779 << java_name << ");" << endl; | |
2780 } else if (java_type->typeEntry()->isStructInD()) { | |
2781 // empty | |
2782 } else if (java_type->typeEntry() && java_type->typeEntry()->qualifiedCppName() == "QString") { | |
2783 s << INDENT << "QString " << qt_name | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2784 << " = " << QString("QString::fromUtf8((const char *)%1.ptr, %1.length);").arg(java_name) << endl; |
1 | 2785 } else if (java_type->isJObjectWrapper()) { |
2786 s << INDENT << "JObjectWrapper " << qt_name | |
2787 << " = qtjambi_to_jobjectwrapper(__jni_env, " << java_name << ");" << endl; | |
2788 } else if (java_type->isVariant()) { | |
2789 s << INDENT << "QVariant " << qt_name | |
2790 << " = " << java_name << " == NULL ? QVariant() : QVariant(*" << java_name << ");" << endl; | |
2791 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { | |
2792 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2793 | |
2794 // ### Don't assert on wrong array lengths | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2795 // s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; |
1 | 2796 s << INDENT; |
2797 writeTypeInfo(s, elementType); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2798 s << " *" << qt_name << " = ("; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2799 writeTypeInfo(s, elementType); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2800 s << "*) " << java_name << ";" << endl; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2801 /* |
1 | 2802 s << INDENT << "__jni_env->" << getXxxArrayRegion(elementType) << "( (" << translateType(java_type, options) |
2803 << ")" << java_name << ", 0, " << java_type->arrayElementCount() << ", " | |
2804 << "(" << translateType(elementType, options) << " *" << ")" | |
2805 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2806 */ |
1 | 2807 } else if (java_type->isArray()) { |
2808 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2809 | |
2810 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; | |
2811 writeTypeInfo(s, elementType); | |
2812 s << "[" << java_type->arrayElementCount() << "]" << qt_name << ";" << endl; | |
2813 | |
2814 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2815 writeJavaToQt(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
2816 "__jni_env->GetObjectArrayElement(" + java_name + ", " + QString::number(i) + ")", 0, -1, options); | |
2817 } | |
2818 | |
2819 } else if (java_type->isTargetLangString()) { | |
2820 s << INDENT << "QString " << qt_name | |
2821 << " = " << "QString::fromUtf8(" << java_name << ", " << java_name << "_size);" << endl; | |
2822 // qtd << " = qtjambi_to_qstring(__jni_env, (jstring) " << java_name << ");" << endl; | |
2823 | |
2824 } else if (java_type->isTargetLangChar()) { | |
2825 s << INDENT << "QChar " << qt_name | |
2826 << " = (ushort)" << java_name << ";" << endl; | |
2827 | |
2828 } else if (java_type->isEnum() || java_type->isFlags()) { | |
2829 | |
2830 bool written = false; | |
2831 if (java_type->isEnum()) { | |
2832 AbstractMetaEnum *java_enum = | |
2833 m_classes.findEnum(static_cast<const EnumTypeEntry *>(java_type->typeEntry())); | |
2834 if (java_enum && !java_enum->isPublic()) { | |
2835 | |
2836 s << INDENT << "int " << qt_name << " = "; | |
2837 written = true; | |
2838 } | |
2839 } | |
2840 | |
2841 if (!written) { | |
2842 QString qualified_name = java_type->typeEntry()->qualifiedCppName(); | |
2843 s << INDENT << qualified_name << " " << qt_name | |
2844 << " = (" << qualified_name << ") "; | |
2845 } | |
2846 | |
2847 if ((options & EnumAsInts) == 0 && (java_type->isTargetLangEnum() || java_type->isTargetLangFlags())) { | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2848 s << java_name << ";" << endl; |
1 | 2849 |
2850 } else if (options & BoxedPrimitive) { | |
2851 const PrimitiveTypeEntry *pentry = TypeDatabase::instance()->findTargetLangPrimitiveType("int"); | |
2852 Q_ASSERT(pentry); | |
2853 | |
2854 s << java_name << ";" << endl; | |
2855 | |
2856 } else { | |
2857 s << java_name << ';' << endl; | |
2858 } | |
2859 | |
2860 } else if (java_type->isContainer()) { | |
2861 writeJavaToQtContainer(s, java_type, qt_name, java_name, 0, -1); | |
2862 | |
2863 } else if (java_type->isThread()) { | |
2864 s << INDENT << "QThread *" << qt_name << " = qtjambi_to_thread(__jni_env, " << java_name | |
2865 << ");" << endl; | |
2866 | |
2867 } else if (java_type->typeEntry()->isCustom()) { | |
2868 const CustomTypeEntry *custom_type = | |
2869 static_cast<const CustomTypeEntry *>(java_type->typeEntry()); | |
2870 s << INDENT; | |
2871 custom_type->generateCppJavaToQt(s, java_type, "__jni_env", qt_name, java_name); | |
2872 s << ";" << endl; | |
2873 | |
2874 } else { | |
2875 | |
2876 const TypeEntry *type = java_type->typeEntry(); | |
2877 QString class_name = type->name(); | |
2878 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName()); | |
2879 | |
2880 // Declaration and the c-cast | |
2881 s << INDENT; | |
2882 writeTypeInfo(s, java_type); | |
2883 s << ' ' << qt_name << " = ("; | |
2884 writeTypeInfo(s, java_type); | |
2885 s << ") "; | |
2886 | |
2887 if (java_type->isPrimitive()) { | |
2888 if (options & BoxedPrimitive) { | |
2889 const PrimitiveTypeEntry *pentry = static_cast<const PrimitiveTypeEntry *>(type); | |
2890 //std::cout << "---error_here " << type->targetLangName().toStdString() << " \n"; | |
2891 //std::cout << "----func_here " << java_function->marshalledName().toStdString() << " \n"; | |
2892 | |
2893 if (!pentry->preferredConversion()) | |
2894 pentry = TypeDatabase::instance()->findTargetLangPrimitiveType(pentry->targetLangName()); | |
2895 Q_ASSERT(pentry); | |
2896 | |
2897 s << java_name << ";" << endl; | |
2898 | |
2899 } else if ((options & GlobalRefJObject) && type->jniName() == QLatin1String("jobject")) { | |
2900 s << "__jni_env->NewGlobalRef(" << java_name << ");" << endl; | |
2901 } else { | |
2902 s << java_name << ';' << endl; | |
2903 } | |
2904 | |
2905 #if 0 | |
2906 } else if (java_type->isEnum()) { | |
2907 s << "qtjambi_to_enum(__jni_env, " << java_name << ");" << endl; | |
2908 #endif | |
2909 | |
2910 } else if ((java_type->isQObject() || java_type->isObject()) | |
2911 && static_cast<const ObjectTypeEntry *>(type)->designatedInterface()) { | |
2912 /* qtd const InterfaceTypeEntry *ie = | |
2913 static_cast<const ObjectTypeEntry *>(type)->designatedInterface(); | |
2914 s << "qtjambi_to_interface(__jni_env, "; | |
2915 | |
2916 // This cast is only valid if we're dealing with a native id | |
2917 if ((options & UseNativeIds) == UseNativeIds) | |
2918 s << "(QtJambiLink *)"; | |
2919 */ | |
2920 s << java_name << ";" << endl; | |
2921 /* qtd | |
2922 s << "\"" << ie->targetLangName() << "\", \"" | |
2923 << ie->javaPackage().replace(".", "/") << "/\", " | |
2924 << "\"__qt_cast_to_" << type->targetLangName() << "\");" << endl; | |
2925 */ | |
2926 } else if (java_type->isObject() || java_type->isQObject() || java_type->isNativePointer()) { | |
2927 if (java_type->isReference()) { | |
2928 s << "* (" << qualified_class_name << " " | |
2929 << QString(java_type->actualIndirections(), '*') << ") "; | |
2930 } | |
2931 | |
2932 if (java_type->isNativePointer()) { | |
2933 /* qtd s << "qtjambi_to_cpointer(" | |
2934 << "__jni_env, " | |
2935 << java_name << ", " | |
2936 << java_type->actualIndirections() << ");" << endl; */ | |
2937 s << java_name << ";" << endl; // qtd | |
2938 }/* qtd else if (java_type->isQObject()) { | |
2939 if ((options & UseNativeIds) == 0) | |
2940 s << "qtjambi_to_qobject(__jni_env, "; | |
2941 else | |
2942 s << "qtjambi_from_jlong("; | |
2943 s << java_name; | |
2944 s << ");" << endl; | |
2945 }*/ else { | |
2946 /* qtd if ((options & UseNativeIds) == 0) | |
2947 s << "qtjambi_to_object(__jni_env, "; | |
2948 else | |
2949 s << "qtjambi_from_jlong("; | |
2950 */ s << java_name; | |
2951 s << ";" << endl; // + | |
2952 // qtd s << ");" << endl; | |
2953 } | |
2954 | |
2955 } else { | |
2956 // Return values... | |
2957 if (argument_index == 0) { | |
2958 s << "(" << java_name << " != 0 ? *(" << qualified_class_name << " *)"; | |
2959 /* qtd if ((options & UseNativeIds) == 0) | |
2960 s << "qtjambi_to_object(__jni_env, "; | |
2961 else | |
2962 s << "qtjambi_from_jlong("; | |
2963 */ s << java_name; | |
2964 s << " : " << qualified_class_name << "());" << endl; | |
2965 } else { | |
2966 s << "*" | |
2967 << "(" << qualified_class_name << " *)"; | |
2968 bool null_check = false; | |
2969 /* qtd if ((options & UseNativeIds) == 0) { | |
2970 s << "qtjambi_to_object(__jni_env, "; | |
2971 } else if (hasDefaultConstructor(java_type)) { | |
2972 null_check = true; | |
2973 s << "(" << java_name << " != 0 ? qtjambi_from_jlong("; | |
2974 } else { | |
2975 s << "qtjambi_from_jlong("; | |
2976 } | |
2977 */ s << java_name; | |
2978 // qtd s << ")"; | |
2979 | |
2980 if (null_check) | |
2981 s << " : default_" << QString(qualified_class_name).replace("::", "_") << "())"; | |
2982 s << ";" << endl; | |
2983 } | |
2984 | |
2985 } | |
2986 } | |
2987 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl; | |
2988 } | |
2989 | |
2990 void CppImplGenerator::writeQtToJava(QTextStream &s, | |
2991 const AbstractMetaType *java_type, | |
2992 const QString &qt_name, | |
2993 const QString &java_name, | |
2994 const AbstractMetaFunction *java_function, | |
2995 int argument_index, | |
2996 Option option) | |
2997 { | |
2998 | |
2999 // Conversion to Java: Native code for return values, shell code for arguments | |
3000 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode; | |
3001 /* qtd if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3002 return; | |
3003 */ | |
3004 if (java_type == 0) { | |
3005 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3006 "language '%4'") | |
3007 .arg(argument_index) | |
3008 .arg(java_function->implementingClass()->name()) | |
3009 .arg(java_function->name()) | |
3010 .arg(int(lang)); | |
3011 ReportHandler::warning(warn); | |
3012 return; | |
3013 } | |
3014 | |
3015 if (java_type->name() == "QModelIndex") { | |
3016 QString prefix = "*"; | |
3017 if (option & BoxedPrimitive) | |
3018 s << INDENT << "QModelIndexAccessor tmp_index = qtd_from_QModelIndex(" << qt_name << ");" << endl | |
3019 << INDENT << "QModelIndexAccessor *" << java_name << " = &tmp_index;" << endl; | |
3020 else | |
3021 s << INDENT << "*" << java_name << " = qtd_from_QModelIndex(" << qt_name << ");" << endl; | |
3022 | |
81 | 3023 } else if (java_type->typeEntry()->isStructInD()) { |
3024 if (option & BoxedPrimitive) { | |
3025 s << INDENT << java_type->typeEntry()->name() << " *" << java_name << " = (" | |
3026 << java_type->typeEntry()->name() << " *) &" << qt_name << ";" << endl; | |
3027 } else { | |
3028 s << INDENT << "*" << java_name << " = " << qt_name << ";" << endl; | |
3029 } | |
1 | 3030 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { |
3031 AbstractMetaType *elementType = java_type->arrayElementType(); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3032 /* qtd |
1 | 3033 s << INDENT << translateType(java_type, option) << " " << java_name << " = __jni_env->" << newXxxArray(elementType) |
3034 << "(" << java_type->arrayElementCount() << ");" << endl; | |
3035 | |
3036 s << INDENT << "__jni_env->" << setXxxArrayRegion(elementType) << "(" | |
3037 << "(" << translateType(java_type, option) << ")" << java_name | |
3038 << ", 0, " << java_type->arrayElementCount() << ", " | |
3039 << "(" << translateType(elementType, option) << " *" << ")" | |
3040 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3041 */ |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3042 s << INDENT << translateType(java_type, option) << " " << java_name << " = " << qt_name << ";" <<endl; |
1 | 3043 } else if (java_type->isArray()) { |
3044 AbstractMetaType *elementType = java_type->arrayElementType(); | |
3045 | |
3046 s << INDENT << "jobject " << java_name << " = __jni_env->NewObjectArray(" | |
3047 << java_type->arrayElementCount() << ");" << endl; | |
3048 | |
3049 s << "jobject __qt_element = 0;"; | |
3050 | |
3051 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
3052 writeQtToJava(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
3053 "__qt_element", 0, -1, option); | |
3054 s << "__jni_env->SetObjectArrayElement((jobjectArray) " << java_name << ", " | |
3055 << i << ", __qt_element);" << endl; | |
3056 } | |
3057 | |
3058 } else if (java_type->isPrimitive()) { | |
3059 const PrimitiveTypeEntry *type = | |
3060 static_cast<const PrimitiveTypeEntry *>(java_type->typeEntry()); | |
3061 | |
3062 Q_ASSERT(type); | |
3063 QString ret_val; | |
3064 if (java_function) | |
3065 ret_val = jniReturnName(java_function); | |
3066 else | |
3067 ret_val = fixCppTypeName(java_type->typeEntry()->qualifiedCppName()); | |
3068 s << INDENT << ret_val << " " << java_name << " = " << qt_name << ";" << endl; | |
3069 } else if (java_type->isJObjectWrapper()) { | |
3070 s << INDENT << "jobject " << java_name << " = qtjambi_from_jobjectwrapper(__jni_env, " | |
3071 << qt_name << ");" << endl; | |
3072 } else if (java_type->isVariant()) { | |
3073 s << INDENT << "QVariant *" << java_name | |
3074 << " = new QVariant(" << qt_name << ");" << endl; | |
3075 } else if (java_type->isTargetLangString()) { | |
35 | 3076 if(java_type->typeEntry()->qualifiedCppName() == "QStringRef") { |
3077 s << INDENT << "const QString *str_ref = " << qt_name << ".string();" << endl | |
3078 << INDENT << "if(str_ref)" << endl | |
3079 << INDENT << " _d_toUtf8(str_ref->utf16(), str_ref->size(), " << java_name << ");" << endl | |
3080 << INDENT << "else {" << endl | |
3081 << INDENT << " QString empty_str;" << endl | |
3082 << INDENT << " _d_toUtf8(empty_str.utf16(), empty_str.size(), " << java_name << ");" << endl | |
3083 << INDENT << "}" << endl; | |
3084 } else { | |
3085 s << INDENT << QString("_d_toUtf8(%1.utf16(), %1.size(), %2);").arg(qt_name, java_name) << endl; | |
3086 } | |
1 | 3087 } else if (java_type->isTargetLangChar()) { |
3088 s << INDENT << "jchar " << java_name << " = " << qt_name << ".unicode();" << endl; | |
3089 | |
3090 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags() | |
3091 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) { | |
3092 // } else if (java_type->isEnum() || java_type->isFlags()) { | |
3093 | |
3094 // if (option & EnumAsInts) { | |
3095 // qDebug() << java_type->name() << "should be int..."; | |
3096 // } | |
3097 | |
3098 /* if (option & BoxedPrimitive) { | |
3099 s << INDENT << "jobject " << java_name << " = qtjambi_from_int(__jni_env, " | |
3100 << qt_name << ");" << endl; | |
3101 } else */{ | |
3102 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl; | |
3103 } | |
3104 | |
3105 } else if (java_type->isTargetLangEnum()) { | |
3106 Q_ASSERT((option & EnumAsInts) == 0); | |
3107 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
3108 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl; | |
3109 | |
3110 } else if (java_type->isTargetLangFlags()) { | |
3111 Q_ASSERT((option & EnumAsInts) == 0); | |
3112 const FlagsTypeEntry *ft = static_cast<const FlagsTypeEntry *>(java_type->typeEntry()); | |
3113 s << INDENT << "jobject " << java_name << " = qtjambi_from_flags(__jni_env, " | |
3114 << qt_name << ", \"" << ft->javaPackage().replace('.', '/') << '/' | |
3115 << ft->originator()->javaQualifier() << '$' << ft->targetLangName() << "\");" << endl; | |
3116 | |
3117 } else if (java_type->isContainer()) { | |
3118 writeQtToJavaContainer(s, java_type, qt_name, java_name, 0, -1); | |
3119 | |
3120 } else if (java_type->isThread()) { | |
3121 s << INDENT << "jobject " << java_name << " = qtjambi_from_thread(__jni_env, " << qt_name | |
3122 << ");" << endl; | |
3123 | |
3124 } else if (!java_type->isNativePointer() && java_type->typeEntry()->isCustom()) { | |
3125 s << INDENT; | |
3126 static_cast<const CustomTypeEntry *>(java_type->typeEntry()) | |
3127 ->generateCppQtToJava(s, java_type, "__jni_env", qt_name, java_name); | |
3128 s << ";" << endl; | |
3129 | |
3130 } else { | |
3131 QString return_type; | |
3132 if (java_function) | |
3133 return_type = jniReturnName(java_function); | |
3134 else { | |
3135 return_type = jniReturnType(java_type); | |
3136 return_type = fixCppTypeName(return_type); | |
3137 // return_type = fixCppTypeName(java_type->typeEntry()->qualifiedCppName()); | |
3138 } | |
3139 /* if( (java_type->isValue() && !java_type->typeEntry()->isStructInD()) | |
3140 || java_type->isObject() ) | |
3141 s << INDENT << return_type << " *" << java_name << " = (" << return_type << "*) "; | |
3142 else*/ | |
3143 s << INDENT << return_type << " " << java_name << " = (" << return_type << ") "; | |
3144 | |
3145 if (java_type->isQObject()) { | |
3146 /* qtd s << "qtjambi_from_qobject(__jni_env, " << "(QObject *) "; | |
3147 | |
3148 if (java_type->isReference() && java_type->indirections() == 0) | |
3149 s << "&"; | |
3150 | |
3151 s << qt_name | |
3152 << ", \"" << java_type->typeEntry()->lookupName() << "\"" | |
3153 << ", \"" << java_type->package().replace(".", "/") << "/\"" | |
3154 << ");" << endl; | |
3155 */ | |
3156 s << qt_name << ";" << endl; | |
3157 | |
3158 #if 0 | |
3159 } else if (java_type->isEnum()) { | |
3160 | |
3161 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
3162 s << "qtjambi_from_enum(__jni_env, " << qt_name << ", \"" | |
3163 << et->javaQualifier() << "$" << et->targetLangName() << "\");" << endl; | |
3164 #endif | |
3165 } else if (java_type->isNativePointer()) { | |
3166 /* qtd s << "qtjambi_from_cpointer(__jni_env, "; | |
3167 if (java_type->isReference()) | |
3168 s << "&"; | |
3169 s << qt_name << ", " << nativePointerType(java_type) << ", " | |
3170 << java_type->actualIndirections() << ");" << endl; | |
3171 */ | |
3172 if (java_type->isReference()) | |
3173 s << "&"; | |
3174 s << qt_name << ";" << "// qtjambi_from_cpointer" << endl; | |
3175 } else if (java_type->isValue()) { | |
3176 // qtd s << fromObject(java_type->typeEntry(), "&" + qt_name) << endl; | |
3177 s << "new " << java_type->typeEntry()->qualifiedCppName() << "(" << qt_name << ");" << endl; | |
3178 } else { | |
3179 // qtd s << fromObject(java_type->typeEntry(), | |
3180 // qtd (java_type->isReference() ? "&" : "") + qt_name) << endl; | |
3181 s << qt_name << ";" << endl; | |
3182 } | |
3183 } | |
3184 | |
3185 } | |
3186 | |
3187 QString CppImplGenerator::getTypeName(const TypeEntry *entry, Option option) | |
3188 { | |
3189 if(entry->isEnum() && (option & EnumAsInts)) | |
3190 return "int"; | |
3191 | |
3192 return entry->lookupName(); | |
3193 } | |
3194 | |
3195 void CppImplGenerator::writeQtToJavaContainer(QTextStream &s, | |
3196 const AbstractMetaType *java_type, | |
3197 const QString &qt_name, | |
3198 const QString &java_name, | |
3199 const AbstractMetaFunction *java_function, | |
3200 int argument_index) | |
3201 { | |
3202 // Language for conversion to Java: Native code for return values and Shell code for arguments | |
3203 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode; | |
3204 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3205 return; | |
3206 | |
3207 if (java_type == 0) { | |
3208 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3209 "language '%4'") | |
3210 .arg(argument_index) | |
3211 .arg(java_function->implementingClass()->name()) | |
3212 .arg(java_function->name()) | |
3213 .arg(int(lang)); | |
3214 ReportHandler::warning(warn); | |
3215 return; | |
3216 } | |
3217 | |
3218 Q_ASSERT(java_type->isContainer()); | |
3219 const ContainerTypeEntry *type = | |
3220 static_cast<const ContainerTypeEntry *>(java_type->typeEntry()); | |
3221 | |
3222 if (type->type() == ContainerTypeEntry::ListContainer | |
3223 || type->type() == ContainerTypeEntry::VectorContainer | |
3224 || type->type() == ContainerTypeEntry::StringListContainer | |
3225 || type->type() == ContainerTypeEntry::LinkedListContainer | |
3226 || type->type() == ContainerTypeEntry::StackContainer | |
3227 || type->type() == ContainerTypeEntry::SetContainer | |
3228 || type->type() == ContainerTypeEntry::QueueContainer) { | |
3229 | |
3230 Q_ASSERT(java_type->instantiations().size() == 1); | |
3231 AbstractMetaType *targ = java_type->instantiations().first(); | |
3232 | |
3233 QString cls_name = getTypeName(targ->typeEntry(), EnumAsInts); | |
3234 cls_name.remove("_ConcreteWrapper"); | |
3235 | |
3236 s << endl | |
39 | 3237 << INDENT << "{" << endl // qtd2 hack, additional scope for avoiding duplicating of "i" |
1 | 3238 << INDENT; |
3239 | |
3240 switch (type->type()) { | |
3241 case ContainerTypeEntry::LinkedListContainer: | |
3242 case ContainerTypeEntry::QueueContainer: | |
3243 s << "qtjambi_linkedlist_new(__jni_env)"; | |
3244 break; | |
3245 case ContainerTypeEntry::StackContainer: | |
3246 s << "qtjambi_stack_new(__jni_env)"; | |
3247 break; | |
3248 case ContainerTypeEntry::SetContainer: | |
3249 s << "qtjambi_hashset_new(__jni_env)"; | |
3250 break; | |
3251 default: | |
3252 s << "qtd_allocate_" << cls_name | |
3253 << "_array(" << java_name << ", " << qt_name << ".size())"; | |
3254 break; | |
3255 } | |
3256 | |
3257 s << ";" << endl | |
3258 << INDENT; | |
3259 | |
3260 | |
3261 writeTypeInfo(s, java_type, ForceValueType); | |
3262 s << "::const_iterator " << qt_name << "_end_it = " << qt_name << ".constEnd();" << endl | |
3263 << INDENT << "int i = 0;" << endl | |
3264 << INDENT; | |
3265 s << "for ("; | |
3266 writeTypeInfo(s, java_type, ForceValueType); | |
3267 s << "::const_iterator " << qt_name << "_it = " << qt_name << ".constBegin(); " | |
3268 << qt_name << "_it != " << qt_name << "_end_it; ++" << qt_name << "_it) {" << endl; | |
3269 { | |
3270 Indentation indent(INDENT); | |
3271 s << INDENT; | |
3272 writeTypeInfo(s, targ); | |
3273 s << " __qt_tmp = *" << qt_name << "_it;" << endl; | |
3274 | |
3275 if(targ->isTargetLangString()) | |
3276 s << INDENT << "void *__java_tmp = qtd_string_from_array(" << java_name << ", i);" << endl; | |
3277 | |
3278 writeQtToJava(s, targ, "__qt_tmp", "__java_tmp", 0, -1, BoxedPrimitive); | |
3279 | |
3280 s << INDENT << "qtd_assign_" << cls_name << "_array_element(" << java_name << ", i, __java_tmp);" << endl; | |
3281 s << INDENT << "++i;" << endl; | |
3282 } | |
39 | 3283 s << INDENT << "}" << endl |
3284 << INDENT << "}" << endl; | |
1 | 3285 |
3286 } else if (type->type() == ContainerTypeEntry::PairContainer) { | |
3287 QList<AbstractMetaType *> args = java_type->instantiations(); | |
3288 Q_ASSERT(args.size() == 2); | |
3289 | |
3290 s << INDENT << "jobject " << java_name << ";" << endl | |
3291 << INDENT << "{" << endl; | |
3292 { | |
3293 Indentation indent(INDENT); | |
3294 writeQtToJava(s, args.at(0), qt_name + ".first", "__java_tmp_first", 0, -1, BoxedPrimitive); | |
3295 writeQtToJava(s, args.at(1), qt_name + ".second", "__java_tmp_second", 0, -1, BoxedPrimitive); | |
3296 s << INDENT << java_name << " = qtjambi_pair_new(__jni_env, " | |
3297 << "__java_tmp_first, __java_tmp_second);" << endl; | |
3298 } | |
3299 | |
3300 s << INDENT << "}" << endl; | |
3301 | |
3302 } else if (type->type() == ContainerTypeEntry::MultiMapContainer) { | |
3303 | |
3304 Q_ASSERT(java_type->instantiations().size() == 2); | |
3305 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3306 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3307 | |
3308 s << endl | |
3309 << INDENT << "jobject " << java_name << " = qtjambi_treemap_new(__jni_env, " << qt_name << ".keys().size());" << endl | |
3310 << INDENT << "QList<"; | |
3311 writeTypeInfo(s, targ_key); | |
3312 s << "> __qt_keys = " << qt_name << ".keys();" << endl | |
3313 << INDENT << "for (int i=0; i<__qt_keys.size(); ++i) {" << endl; | |
3314 { | |
3315 Indentation indent(INDENT); | |
3316 | |
3317 s << INDENT; | |
3318 writeTypeInfo(s, targ_key); | |
3319 s << " __qt_tmp_key = __qt_keys.at(i);" << endl; | |
3320 writeQtToJava(s, targ_key, "__qt_tmp_key", "__java_tmp_key", 0, -1, BoxedPrimitive); | |
3321 | |
3322 s << INDENT << "QList<"; | |
3323 writeTypeInfo(s, targ_val); | |
3324 s << "> __qt_values = " << qt_name << ".values(__qt_tmp_key);" << endl | |
3325 << INDENT << "jobject __java_value_list = qtjambi_arraylist_new(__jni_env, __qt_values.size());" << endl | |
3326 << INDENT << "for (int j=0; j<__qt_values.size(); ++j) {" << endl; | |
3327 { | |
3328 Indentation indent(INDENT); | |
3329 | |
3330 s << INDENT; | |
3331 writeTypeInfo(s, targ_val); | |
3332 s << " __qt_tmp_val = __qt_values.at(j);" << endl; | |
3333 writeQtToJava(s, targ_val, "__qt_tmp_val", "__java_tmp_val", 0, -1, BoxedPrimitive); | |
3334 | |
3335 s << INDENT << "qtjambi_collection_add(__jni_env, __java_value_list, __java_tmp_val);" << endl; | |
3336 } | |
3337 s << INDENT << "}" << endl | |
3338 << INDENT << "qtjambi_map_put(__jni_env, " << java_name << ", __java_tmp_key, __java_value_list);" << endl; | |
3339 } | |
3340 s << INDENT << "}" << endl; | |
3341 | |
3342 } else if (type->type() == ContainerTypeEntry::MapContainer | |
3343 || type->type() == ContainerTypeEntry::HashContainer) { | |
3344 QString constructor = type->type() == ContainerTypeEntry::MapContainer | |
3345 ? "qtjambi_treemap_new" | |
3346 : "qtjambi_hashmap_new"; | |
3347 | |
3348 Q_ASSERT(java_type->instantiations().size() == 2); | |
3349 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3350 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3351 | |
3352 s << endl | |
3353 << INDENT << "jobject " << java_name << " = " << constructor << "(__jni_env, " << qt_name | |
3354 << ".size());" << endl | |
3355 << INDENT; | |
3356 writeTypeInfo(s, java_type, Option(ExcludeReference | ExcludeConst)); | |
3357 s << "::const_iterator it;" << endl | |
3358 << INDENT << "for (it=" << qt_name << ".constBegin(); it!=" << qt_name << ".constEnd(); ++it) {" << endl; | |
3359 { | |
3360 Indentation indent(INDENT); | |
3361 s << INDENT; | |
3362 writeTypeInfo(s, targ_key); | |
3363 s << " __qt_tmp_key = it.key();" << endl | |
3364 << INDENT; | |
3365 writeTypeInfo(s, targ_val); | |
3366 s << " __qt_tmp_val = it.value();" << endl; | |
3367 writeQtToJava(s, targ_key, "__qt_tmp_key", "__java_tmp_key", 0, -1, BoxedPrimitive); | |
3368 writeQtToJava(s, targ_val, "__qt_tmp_val", "__java_tmp_val", 0, -1, BoxedPrimitive); | |
3369 s << INDENT << "qtjambi_map_put(__jni_env, " << java_name | |
3370 << ", __java_tmp_key, __java_tmp_val);" << endl; | |
3371 } | |
3372 s << INDENT << "}" << endl; | |
3373 | |
3374 } else { | |
3375 ReportHandler::warning(QString("unable to generate container type %1, type=%2") | |
3376 .arg(java_type->name()).arg(type->type())); | |
3377 } | |
3378 | |
3379 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl; | |
3380 } | |
3381 | |
3382 | |
3383 void CppImplGenerator::writeJavaToQtContainer(QTextStream &s, | |
3384 const AbstractMetaType *java_type, | |
3385 const QString &qt_name, | |
3386 const QString &java_name, | |
3387 const AbstractMetaFunction *java_function, | |
3388 int argument_index) | |
3389 { | |
3390 // Conversion to C++: Shell code for return value, native code for arguments | |
3391 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
3392 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3393 return; | |
3394 | |
3395 if (java_type == 0) { | |
3396 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3397 "language '%4'") | |
3398 .arg(argument_index) | |
3399 .arg(java_function->implementingClass()->name()) | |
3400 .arg(java_function->name()) | |
3401 .arg(int(lang)); | |
3402 ReportHandler::warning(warn); | |
3403 return; | |
3404 } | |
3405 | |
3406 | |
3407 Q_ASSERT(java_type->isContainer()); | |
3408 const ContainerTypeEntry *type = | |
3409 static_cast<const ContainerTypeEntry *>(java_type->typeEntry()); | |
3410 | |
3411 if (type->type() == ContainerTypeEntry::ListContainer | |
3412 || type->type() == ContainerTypeEntry::VectorContainer | |
3413 || type->type() == ContainerTypeEntry::StringListContainer | |
3414 || type->type() == ContainerTypeEntry::LinkedListContainer | |
3415 || type->type() == ContainerTypeEntry::StackContainer | |
3416 || type->type() == ContainerTypeEntry::SetContainer | |
3417 || type->type() == ContainerTypeEntry::QueueContainer) { | |
3418 Q_ASSERT(java_type->instantiations().size() == 1); | |
3419 AbstractMetaType *targ = java_type->instantiations().first(); | |
3420 QString elem_type = getTypeName(targ->typeEntry(), EnumAsInts); | |
3421 elem_type.remove("_ConcreteWrapper"); | |
3422 | |
3423 s << INDENT; | |
3424 writeTypeInfo(s, java_type, ForceValueType); | |
3425 s << qt_name << ";" << endl; | |
3426 | |
3427 // qtd s << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3428 { | |
3429 /* qtd Indentation indent(INDENT); | |
3430 s << INDENT << "jobjectArray __qt__array = qtjambi_collection_toArray(__jni_env, " | |
3431 << java_name << ");" << endl | |
3432 << INDENT << "jsize __qt__size = __jni_env->GetArrayLength(__qt__array);" << endl; | |
3433 */ | |
3434 if (type->type() == ContainerTypeEntry::VectorContainer | |
3435 || type->type() == ContainerTypeEntry::StackContainer) | |
3436 s << INDENT << qt_name << ".reserve(" << java_name << "_size);" << endl; | |
3437 | |
3438 s << INDENT << "for (int i=0; i<" << java_name << "_size; ++i) {" << endl; | |
3439 { | |
3440 Indentation indent(INDENT); | |
3441 if(targ->isTargetLangString()) | |
35 | 3442 s << INDENT << "DArray __d_element;" << endl |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3443 << INDENT << "qtd_get_string_from_array(" << java_name << ", i, &__d_element);" << endl; |
1 | 3444 else { |
3445 s << INDENT; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3446 writeTypeInfo(s, targ, Option(VirtualDispatch | ForcePointer | EnumAsInts)); |
1 | 3447 QString cast_string = ""; |
3448 const TypeEntry* centry = targ->typeEntry(); | |
3449 if (centry->isComplex() && (centry->isObject() || centry->isValue() || centry->isInterface())) | |
3450 cast_string = "(void**)"; | |
3451 s << "__d_element;" << endl | |
3452 << INDENT << "qtd_get_" << elem_type << "_from_array(" << java_name << ", i, &__d_element);" << endl; | |
3453 } | |
3454 /* qtd s << INDENT << "jobject __d_element = " | |
3455 << "__jni_env->GetObjectArrayElement(__qt__array, i);" << endl;*/ | |
3456 writeJavaToQt(s, targ, "__qt_element", "__d_element", 0, -1, BoxedPrimitive); | |
3457 QString cont_element = "__qt_element"; | |
3458 if(targ->typeEntry()->isStructInD() && targ->name() != "QModelIndex") | |
3459 cont_element = "__d_element"; | |
3460 s << INDENT << qt_name << " << " << cont_element << ";" << endl; | |
3461 } | |
3462 // qtd s << INDENT << "}" << endl; | |
3463 } | |
3464 s << INDENT << "}" << endl; | |
3465 } else if (type->type() == ContainerTypeEntry::PairContainer) { | |
3466 QList<AbstractMetaType *> targs = java_type->instantiations(); | |
3467 Q_ASSERT(targs.size() == 2); | |
3468 | |
3469 s << INDENT; | |
3470 writeTypeInfo(s, java_type, ForceValueType); | |
3471 s << " " << qt_name << ";" << endl | |
3472 << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3473 { | |
3474 // separate scope required just in case function takes two QPair's. | |
3475 Indentation indent(INDENT); | |
3476 s << INDENT << "jobject __java_first = qtjambi_pair_get(__jni_env, " | |
3477 << java_name << ", 0);" << endl; | |
3478 writeJavaToQt(s, targs.at(0), "__qt_first", "__java_first", 0, -1, BoxedPrimitive); | |
3479 | |
3480 s << INDENT << "jobject __java_second = qtjambi_pair_get(__jni_env, " | |
3481 << java_name << ", 1);" << endl; | |
3482 writeJavaToQt(s, targs.at(1), "__qt_second", "__java_second", 0, -1, BoxedPrimitive); | |
3483 | |
3484 s << INDENT << qt_name << ".first = __qt_first;" << endl | |
3485 << INDENT << qt_name << ".second = __qt_second;" << endl; | |
3486 } | |
3487 s << INDENT << "}" << endl; | |
3488 } else if (type->type() == ContainerTypeEntry::MapContainer | |
3489 || type->type() == ContainerTypeEntry::HashContainer) { | |
3490 Q_ASSERT(java_type->instantiations().size() == 2); | |
3491 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3492 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3493 | |
3494 s << INDENT; | |
3495 writeTypeInfo(s, java_type, ForceValueType); | |
3496 s << qt_name << ";" << endl; | |
3497 s << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3498 { | |
3499 Indentation indent(INDENT); | |
3500 s << INDENT << "int __qt_list_size = qtjambi_map_size(__jni_env, " << java_name | |
3501 << ");" << endl | |
3502 << INDENT | |
3503 << "jobjectArray __java_entry_set = qtjambi_map_entryset_array(__jni_env, " << java_name | |
3504 << ");" << endl; | |
3505 | |
3506 s << INDENT << "for (int i=0; i<__qt_list_size; ++i) {" << endl; | |
3507 { | |
3508 Indentation indent(INDENT); | |
3509 s << INDENT | |
3510 << "QPair<jobject, jobject> __java_entry = " | |
3511 << "qtjambi_entryset_array_get(__jni_env, __java_entry_set, i);" | |
3512 << endl | |
3513 << INDENT << "jobject __java_key = __java_entry.first;" << endl | |
3514 << INDENT << "jobject __java_val = __java_entry.second;" << endl; | |
3515 writeJavaToQt(s, targ_key, "__qt_key", "__java_key", 0, -1, BoxedPrimitive); | |
3516 writeJavaToQt(s, targ_val, "__qt_val", "__java_val", 0, -1, BoxedPrimitive); | |
3517 s << INDENT << qt_name << ".insert(__qt_key, __qt_val);" << endl; | |
3518 } | |
3519 s << INDENT << "}" << endl; | |
3520 } | |
3521 s << INDENT << "}" << endl; | |
3522 | |
3523 } else { | |
3524 ReportHandler::warning(QString("unable to generate container type %1, %2") | |
3525 .arg(java_type->name()).arg(type->type())); | |
3526 } | |
3527 | |
3528 } | |
3529 | |
3530 | |
3531 void CppImplGenerator::writeFunctionCall(QTextStream &s, const QString &object_name, | |
3532 const AbstractMetaFunction *java_function, | |
3533 const QString &prefix, | |
3534 Option option, | |
3535 const QStringList &extra_arguments) | |
3536 { | |
3537 QString function_name = option & OriginalName ? java_function->originalName() : java_function->name(); | |
3538 | |
3539 AbstractMetaClassList interfaces = java_function->implementingClass()->interfaces(); | |
3540 | |
3541 QString classPrefix; | |
3542 if (prefix.isEmpty() | |
3543 && !java_function->implementingClass()->interfaces().isEmpty() | |
3544 && !java_function->implementingClass()->inheritsFrom(java_function->declaringClass())) { | |
3545 classPrefix = java_function->declaringClass()->qualifiedCppName() + "::"; | |
3546 } | |
3547 | |
3548 if (java_function->isInGlobalScope()) { | |
3549 | |
3550 // Global scope stream operators need the arguments to be reordered (this ref at end) | |
3551 // so we special case them in order to simplify this code | |
3552 bool stream_operator = java_function->originalName() == "operator<<" | |
3553 || java_function->originalName() == "operator>>"; | |
3554 | |
3555 if (java_function->type() == 0) | |
3556 s << "if (" << object_name << " != 0) "; | |
3557 else | |
3558 s << "(" << object_name << " != 0) ? "; | |
3559 s << "::" << prefix << function_name << "("; | |
3560 if (!stream_operator) | |
3561 s << "*" << object_name << ", "; | |
3562 writeFunctionCallArguments(s, java_function, "__qt_"); | |
3563 if (stream_operator) | |
3564 s << ", *" << object_name; | |
3565 s << ")"; | |
3566 if (java_function->type() != 0) | |
3567 s << " : " << default_return_statement_qt(java_function->type(), Generator::Option(option | Generator::NoReturnStatement)); | |
3568 s << ";"; | |
3569 } else { | |
3570 s << object_name << (java_function->isStatic() ? QLatin1String("::") : QLatin1String("->") + classPrefix) | |
3571 << prefix << function_name << "("; | |
3572 writeFunctionCallArguments(s, java_function, "__qt_"); | |
3573 | |
3574 // The extra arguments... | |
3575 for (int i=0; i<extra_arguments.size(); ++i) { | |
3576 if (i > 0 || java_function->arguments().size() != 0) | |
3577 s << ", "; | |
3578 s << extra_arguments.at(i); | |
3579 } | |
3580 | |
3581 s << ");"; | |
3582 } | |
3583 | |
3584 s << endl; | |
3585 | |
3586 } | |
3587 | |
3588 | |
3589 void CppImplGenerator::writeFunctionCallArguments(QTextStream &s, | |
3590 const AbstractMetaFunction *java_function, | |
3591 const QString &prefix, | |
3592 Option options) | |
3593 { | |
3594 AbstractMetaArgumentList arguments = java_function->arguments(); | |
3595 | |
3596 int written_arguments = 0; | |
3597 const AbstractMetaClass *cls = java_function->ownerClass(); | |
3598 if (java_function->isConstructor() && cls->hasVirtualFunctions()) { | |
3599 s << "d_ptr"; | |
3600 written_arguments++; | |
3601 } | |
3602 for (int i=0; i<arguments.size(); ++i) { | |
3603 const AbstractMetaArgument *argument = arguments.at(i); | |
3604 AbstractMetaType *a_type = argument->type(); | |
3605 | |
3606 if ((options & SkipRemovedArguments) == SkipRemovedArguments | |
3607 && java_function->argumentRemoved(i+1)) { | |
3608 continue; | |
3609 } | |
3610 | |
3611 if (written_arguments++ > 0) { | |
3612 s << ", "; | |
3613 } | |
3614 | |
3615 bool enum_as_int = (options & EnumAsInts) && (argument->type()->typeEntry()->isEnum() | |
3616 || argument->type()->typeEntry()->isFlags()); | |
3617 if (a_type->isEnum()) { | |
3618 AbstractMetaEnum *java_enum = | |
3619 m_classes.findEnum(static_cast<const EnumTypeEntry *>(argument->type()->typeEntry())); | |
3620 if (java_enum == 0) { | |
3621 ReportHandler::warning(QString("enum not found: '%1'") | |
3622 .arg(argument->type()->typeEntry()->qualifiedCppName())); | |
3623 } else { | |
3624 enum_as_int |= !java_enum->isPublic(); | |
3625 } | |
3626 } | |
3627 | |
3628 if ( (options & VirtualDispatch) | |
3629 && a_type->isContainer()) { | |
3630 s << "__d_" << argument->indexedName(); | |
3631 continue; | |
3632 } | |
3633 | |
3634 if ((!(options & NoCasts) && !enum_as_int) || ((options & ForceEnumCast) && a_type->isEnum())) { | |
3635 | |
3636 // If the type in the signature is specified without template instantiation, but the | |
3637 // class is actually a template class, then we have troubles. | |
3638 AbstractMetaClass *cls = classes().findClass(argument->type()->typeEntry()->qualifiedCppName()); | |
3639 | |
3640 if ( (options & VirtualDispatch) && !a_type->typeEntry()->isStructInD() | |
3641 && ( a_type->isValue() | |
3642 || (a_type->isReference() && (a_type->typeEntry()->isValue() || a_type->typeEntry()->isObject() || a_type->typeEntry()->isPrimitive()) && !a_type->isTargetLangString()) | |
3643 ) ) | |
3644 s << "&"; | |
3645 | |
3646 if( (options & VirtualDispatch) && a_type->typeEntry()->isStructInD() && a_type->isReference()) | |
3647 s << "&"; | |
3648 | |
3649 if ( (options & VirtualDispatch) | |
3650 && ( a_type->isTargetLangString() || a_type->name() == "QModelIndex" ) ) | |
3651 ; | |
3652 else if (cls == 0 || cls->templateArguments().size() == a_type->instantiations().size()) { | |
3653 s << "("; | |
3654 writeTypeInfo(s, a_type, options); | |
3655 s << ")"; | |
3656 } | |
3657 } | |
3658 | |
3659 if (a_type->isNativePointer() && a_type->typeEntry()->name() == "QString" && !a_type->isReference()) | |
3660 s << "&"; | |
3661 | |
3662 if ( ( !a_type->isPrimitive() | |
3663 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty() ) | |
3664 && (!a_type->typeEntry()->isStructInD() || a_type->name() == "QModelIndex") ) { | |
3665 s << prefix; | |
3666 } | |
3667 | |
3668 if (options & VirtualDispatch) { | |
3669 if( argument->type()->isTargetLangString()) | |
3670 s << argument->indexedName() << ".utf16(), " << argument->indexedName() << ".size()"; | |
3671 else if (argument->type()->name() == "QModelIndex") | |
3672 s << "qtd_from_QModelIndex(" << argument->indexedName() << ")"; | |
3673 else | |
3674 s << argument->indexedName(); | |
3675 } else | |
3676 s << argument->indexedName(); | |
3677 } | |
3678 } | |
3679 | |
3680 | |
3681 QString CppImplGenerator::translateType(const AbstractMetaType *java_type, Option option, bool d_export) | |
3682 { | |
3683 if (!java_type) | |
3684 return "void"; | |
3685 | |
3686 const TypeEntry *type = java_type->typeEntry(); | |
3687 QString class_name = type->name(); | |
3688 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName()); | |
3689 QString d_name = qualified_class_name; | |
3690 if (d_export) | |
3691 d_name = type->lookupName(); | |
3692 | |
3693 if (java_type->isPrimitive() | |
3694 || java_type->isTargetLangString() | |
3695 || java_type->isVariant() | |
3696 || java_type->isJObjectWrapper() | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3697 || java_type->isTargetLangChar()) { |
1 | 3698 return d_name; |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3699 } else if (java_type->isArray()) { |
39 | 3700 if (d_export) |
3701 return java_type->arrayElementType()->name() + "*"; | |
3702 else | |
3703 return java_type->arrayElementType()->typeEntry()->qualifiedCppName() + "*"; | |
1 | 3704 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags() |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3705 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3706 return "int"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3707 } else if (java_type->isReference()) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3708 if (java_type->typeEntry()->isValue()) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3709 return "void*"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3710 else |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3711 return d_name + " "+ QString(java_type->actualIndirections(), '*'); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3712 } else if (java_type->isNativePointer()) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3713 if (java_type->typeEntry()->isValue()) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3714 return "void*"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3715 else if (java_type->typeEntry()->isEnum() && d_export) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3716 return "int" + QString(java_type->indirections(), '*'); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3717 else |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3718 return d_name + QString(java_type->indirections(), '*'); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3719 } else { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3720 return d_name + QString(java_type->indirections(), '*'); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3721 } |
81 | 3722 } |
1 | 3723 |
3724 void CppImplGenerator::writeExtraIncludes(QTextStream &s, const AbstractMetaClass *java_class) | |
3725 { | |
3726 IncludeList includes = java_class->typeEntry()->extraIncludes(); | |
3727 qSort(includes.begin(), includes.end()); | |
3728 | |
3729 int used = 0; | |
3730 foreach (const Include &i, includes) { | |
3731 if (i.type != Include::TargetLangImport) { | |
3732 s << i.toString() << endl; | |
3733 ++used; | |
3734 } | |
3735 } | |
3736 | |
3737 | |
3738 if (used) | |
3739 s << endl; | |
3740 | |
3741 } | |
3742 | |
3743 | |
3744 void CppImplGenerator::writeDefaultConstructedValues_helper(QSet<QString> &values, | |
3745 const AbstractMetaFunction *func) | |
3746 { | |
3747 foreach (AbstractMetaArgument *arg, func->arguments()) { | |
3748 AbstractMetaType *type = arg->type(); | |
3749 if (func->typeReplaced(arg->argumentIndex()+1).isEmpty() && type->isValue() && hasDefaultConstructor(type)) | |
3750 values << type->typeEntry()->qualifiedCppName(); | |
3751 } | |
3752 } | |
3753 | |
3754 | |
3755 void CppImplGenerator::writeDefaultConstructedValues(QTextStream &s, const AbstractMetaClass *java_class) { | |
3756 | |
3757 QSet<QString> values; | |
3758 | |
3759 // Class functions, more or less copied from the logic in write(Class) above... | |
3760 AbstractMetaFunctionList class_funcs; | |
3761 | |
3762 // Add normal final functions | |
3763 foreach (AbstractMetaFunction *function, java_class->functionsInTargetLang()) { | |
3764 if (!function->isEmptyFunction()) | |
3765 class_funcs << function; | |
3766 } | |
3767 | |
3768 // Add abstract functions, I think... | |
3769 foreach (AbstractMetaFunction *function, java_class->queryFunctions(AbstractMetaClass::NormalFunctions | |
3770 | AbstractMetaClass::AbstractFunctions | |
3771 | AbstractMetaClass::NotRemovedFromTargetLang)) { | |
3772 if (function->implementingClass() != java_class) | |
3773 class_funcs << function; | |
3774 } | |
3775 | |
3776 // Signals (their c++ wrapper calls actually...) | |
3777 class_funcs += java_class->queryFunctions(AbstractMetaClass::Signals); | |
3778 | |
3779 // | |
3780 foreach (AbstractMetaFunction *f, class_funcs) { | |
3781 writeDefaultConstructedValues_helper(values, f); | |
3782 } | |
3783 | |
3784 foreach (AbstractMetaField *field, java_class->fields()) { | |
3785 writeDefaultConstructedValues_helper(values, field->setter()); | |
3786 } | |
3787 | |
3788 if (!values.isEmpty()) { | |
3789 s << endl << endl | |
3790 << "// Default constructed values used throughout final functions..." << endl; | |
3791 for (QSet<QString>::const_iterator it = values.constBegin(); it != values.constEnd(); ++it) { | |
3792 s << "Q_GLOBAL_STATIC(" << *it << ", default_" << QString(*it).replace("::", "_") | |
3793 << ");" << endl; | |
3794 } | |
3795 s << endl << endl; | |
3796 } | |
3797 } |