Mercurial > projects > qtd
annotate generator/cppimplgenerator.cpp @ 132:a860544c5ee8
CMake: initial version of CMakeD script.
CMake: MSVC fixes (not work yet).
CMake: add experimental option BUILD_SHARED_LIBRARIES.
author | SokoL_SD |
---|---|
date | Sun, 07 Jun 2009 14:31:36 +0000 |
parents | 23e208cef962 |
children | ae34188ddd84 |
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) | |
132 | 1378 if(signal_funcs.size() > 0) |
1379 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "];" << endl; | |
1380 else | |
1381 s << "EmitCallback emit_callbacks_" << java_class->name() << "[1];" << endl; // Hack for msvc. | |
1 | 1382 else { |
1383 // D-side signal callbacks | |
1384 for(int i = 0; i < signal_funcs.size(); i++) { | |
1385 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1386 writeSignalHandler(s, java_class, signal); | |
1387 } | |
1388 | |
132 | 1389 if(signal_funcs.size() > 0) |
1390 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "] = {" << endl; | |
1391 else | |
1392 s << "EmitCallback emit_callbacks_" << java_class->name() << "[1] = {" << endl; // Hack for msvc. | |
1 | 1393 for(int i = 0; i < signal_funcs.size(); i++) { |
1394 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1395 s << endl; | |
1396 if (i != 0) | |
1397 s << ", "; | |
1398 s << "&" << signalExternName(java_class, signal) << "_handle"; | |
1399 } | |
1400 s << endl << "};" << endl << endl; | |
1401 } | |
1402 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1403 writeQObjectLink(s, java_class); |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1404 |
1 | 1405 // Functions connecting/disconnecting shell's slots |
1406 for(int i = 0; i < signal_funcs.size(); i++) { | |
1407 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1408 QString sigExternName = signalExternName(java_class, signal); | |
1409 | |
1410 s << "extern \"C\" DLL_PUBLIC void " << sigExternName << "_connect" | |
1411 << "(void* native_id)" << endl << "{" << endl | |
1412 << " " << 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
|
1413 << " 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
|
1414 << " const QMetaObject &mo2 = " << java_class->name() << "_Link::staticMetaObject;" << endl |
1 | 1415 << " 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
|
1416 << " mo.connect(qobj, signalId, get_" << java_class->name() << "_link(qobj), mo2.methodCount() + " << i << ");" << endl |
1 | 1417 << "}" << endl; |
1418 | |
1419 s << "extern \"C\" DLL_PUBLIC void " << sigExternName << "_disconnect" | |
1420 << "(void* native_id)" << endl << "{" << endl | |
1421 << " " << shellClassName(java_class) << " *qobj = (" << shellClassName(java_class) << "*) native_id;" << endl | |
1422 << " 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
|
1423 << " const QMetaObject &mo2 = " << java_class->name() << "_Link::staticMetaObject;" << endl |
1 | 1424 << " 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
|
1425 << " mo.disconnect(qobj, signalId, get_" << java_class->name() << "_link(qobj), mo2.methodCount() + " << i << ");" << endl |
1 | 1426 << "}" << endl << endl; |
1427 } | |
1428 } | |
1429 | |
1430 | |
1431 void CppImplGenerator::writeShellConstructor(QTextStream &s, const AbstractMetaFunction *java_function) | |
1432 { | |
1433 if (java_function->isModifiedRemoved(TypeSystem::ShellCode)) | |
1434 return; | |
1435 | |
1436 const AbstractMetaClass *cls = java_function->ownerClass(); | |
1437 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1438 | |
1439 writeFunctionSignature(s, java_function, cls); | |
1440 | |
1441 s << endl; | |
1442 s << " : " << cls->qualifiedCppName() << "("; | |
1443 for (int i=0; i<arguments.size(); ++i) { | |
1444 s << arguments.at(i)->indexedName(); | |
1445 if (i != arguments.size() - 1) | |
1446 s << ", "; | |
1447 } | |
1448 s << ")"; | |
1449 if (cls->hasVirtualFunctions()) | |
1450 s << "," << endl << " Qtd_QObjectEntity(d_ptr)"; | |
1451 /* qtd s << " m_meta_object(0)," << endl; | |
1452 s << " m_vtable(0)," << endl | |
1453 << " m_link(0)" << endl; | |
1454 */ | |
1455 s << endl; | |
1456 s << "{" << endl; | |
1457 { | |
1458 Indentation indent(INDENT); | |
1459 writeCodeInjections(s, java_function, cls, CodeSnip::Beginning, TypeSystem::ShellCode); | |
1460 writeCodeInjections(s, java_function, cls, CodeSnip::End, TypeSystem::ShellCode); | |
1461 } | |
1462 s << "}" << endl << endl; | |
1463 } | |
1464 | |
1465 void CppImplGenerator::writeShellDestructor(QTextStream &s, const AbstractMetaClass *java_class) | |
1466 { | |
1467 | |
1468 if (java_class->isQObject()) | |
1469 if (cpp_shared) | |
1470 s << "extern \"C\" typedef void (*qtd_pf_D_" << java_class->name() << "_delete)(void *d_ptr);" << endl | |
1471 << "qtd_pf_D_" << java_class->name() << "_delete qtd_D_" << java_class->name() << "_delete;" << endl << endl; | |
1472 else | |
1473 s << "extern \"C\" void qtd_D_" << java_class->name() << "_delete(void *d_ptr);" << endl << endl; | |
1474 | |
1475 s << shellClassName(java_class) << "::~" | |
1476 << shellClassName(java_class) << "()" << endl | |
1477 << "{" << endl; | |
1478 { | |
1479 Indentation indent(INDENT); | |
1480 if (java_class->isQObject()) | |
1481 s << INDENT << "if (QObject::parent())" << endl | |
1482 << INDENT << " qtd_D_" << java_class->name() << "_delete(this->d_entity());" << endl; | |
1483 | |
1484 /* qtd | |
1485 s << "#ifdef QT_DEBUG" << endl | |
1486 << INDENT << "if (m_vtable)" << endl | |
1487 << INDENT << " m_vtable->deref();" << endl | |
1488 << "#endif" << endl | |
1489 << INDENT << "if (m_link) {" << endl; | |
1490 | |
1491 AbstractMetaClassList interfaces = java_class->interfaces(); | |
1492 if (interfaces.size() + (java_class->baseClass() != 0 ? 1 : 0) > 1) { | |
1493 if (java_class->baseClass() != 0) | |
1494 interfaces += java_class->baseClass(); | |
1495 foreach (AbstractMetaClass *iface, interfaces) { | |
1496 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface; | |
1497 s << INDENT << " m_link->unregisterSubObject((" << impl->qualifiedCppName() << " *) this);" << endl; | |
1498 } | |
1499 } | |
1500 | |
1501 if (!java_class->isQObject()) { | |
1502 s << INDENT << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl | |
1503 << INDENT << " if (__jni_env != 0) m_link->nativeShellObjectDestroyed(__jni_env);" << endl; | |
1504 } | |
1505 | |
1506 #if defined(QTJAMBI_DEBUG_TOOLS) | |
1507 s << INDENT << " qtjambi_increase_shellDestructorCalledCount(QString::fromLatin1(\"" << java_class->name() << "\"));" << endl; | |
1508 #endif | |
1509 | |
1510 s << INDENT << "}" << endl; */ | |
1511 } | |
1512 s << "}" << endl << endl; | |
1513 } | |
1514 | |
1515 void CppImplGenerator::writeCodeInjections(QTextStream &s, const AbstractMetaFunction *java_function, | |
1516 const AbstractMetaClass *implementor, CodeSnip::Position position, | |
1517 TypeSystem::Language language) | |
1518 { | |
1519 | |
1520 FunctionModificationList mods; | |
1521 const AbstractMetaClass *cls = implementor; | |
1522 while (cls != 0) { | |
1523 mods += java_function->modifications(cls); | |
1524 | |
1525 if (cls == cls->baseClass()) | |
1526 break; | |
1527 cls = cls->baseClass(); | |
1528 } | |
1529 | |
1530 foreach (FunctionModification mod, mods) { | |
1531 if (mod.snips.count() <= 0) | |
1532 continue ; | |
1533 | |
1534 foreach (CodeSnip snip, mod.snips) { | |
1535 if (snip.position != position) | |
1536 continue ; | |
1537 | |
1538 if ((snip.language & language) == false) | |
1539 continue ; | |
1540 | |
1541 if (position == CodeSnip::End) | |
1542 s << endl; | |
1543 | |
1544 QString code; | |
1545 QTextStream tmpStream(&code); | |
1546 snip.formattedCode(tmpStream, INDENT); | |
1547 ArgumentMap map = snip.argumentMap; | |
1548 ArgumentMap::iterator it = map.begin(); | |
1549 for (;it!=map.end();++it) { | |
1550 int pos = it.key() - 1; | |
1551 QString meta_name = it.value(); | |
1552 | |
1553 if (pos >= 0 && pos < java_function->arguments().count()) { | |
1554 code = code.replace(meta_name, java_function->arguments().at(pos)->indexedName()); | |
1555 } else { | |
1556 QString debug = QString("argument map specifies invalid argument index %1" | |
1557 "for function '%2'") | |
1558 .arg(pos + 1).arg(java_function->name()); | |
1559 ReportHandler::warning(debug); | |
1560 } | |
1561 | |
1562 } | |
1563 s << code; | |
1564 if (position == CodeSnip::Beginning) | |
1565 s << endl; | |
1566 } | |
1567 } | |
1568 } | |
1569 | |
1570 static QString function_call_for_ownership(TypeSystem::Ownership owner, const QString &var_name) | |
1571 { | |
1572 if (owner == TypeSystem::CppOwnership) { | |
1573 return "setCppOwnership(__jni_env, " + var_name + ")"; | |
1574 } else if (owner == TypeSystem::TargetLangOwnership) { | |
1575 return "setJavaOwnership(__jni_env, " + var_name + ")"; | |
1576 } else if (owner == TypeSystem::DefaultOwnership) { | |
1577 return "setDefaultOwnership(__jni_env, " + var_name + ")"; | |
1578 } else { | |
1579 Q_ASSERT(false); | |
1580 return "bogus()"; | |
1581 } | |
1582 } | |
1583 | |
1584 void CppImplGenerator::writeOwnership(QTextStream &s, | |
1585 const AbstractMetaFunction *java_function, | |
1586 const QString &var_name, | |
1587 int var_index, | |
1588 const AbstractMetaClass *implementor) | |
1589 { | |
1590 TypeSystem::Ownership owner = TypeSystem::InvalidOwnership; | |
1591 const AbstractMetaClass *cls = implementor; | |
1592 while (cls != 0 && owner == TypeSystem::InvalidOwnership) { | |
1593 owner = java_function->ownership(cls, TypeSystem::ShellCode, var_index); | |
1594 cls = cls->baseClass(); | |
1595 } | |
1596 | |
1597 if (owner == TypeSystem::InvalidOwnership) | |
1598 return; | |
1599 | |
1600 if (var_index != -1) { | |
1601 s << INDENT << "if (" << var_name << " != 0) {" << endl; | |
1602 { | |
1603 Indentation indent(INDENT); | |
1604 s << INDENT << "QtJambiLink *__link = QtJambiLink::findLink(__jni_env, " | |
1605 << var_name << ");" << endl | |
1606 << INDENT << "Q_ASSERT(__link != 0);" << endl; | |
1607 | |
1608 s << INDENT << "__link->" << function_call_for_ownership(owner, var_name) << ";" << endl; | |
1609 } | |
1610 s << INDENT << "}" << endl; | |
1611 } else { | |
1612 s << INDENT << "if (m_link) {" << endl; | |
1613 { | |
1614 Indentation indent(INDENT); | |
1615 s << INDENT << "m_link->" << function_call_for_ownership(owner, "m_link->javaObject(__jni_env)") << ";" << endl; | |
1616 } | |
1617 s << INDENT << "}" << endl; | |
1618 } | |
1619 | |
1620 } | |
1621 | |
1622 void CppImplGenerator::writeShellFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
1623 const AbstractMetaClass *implementor, int id) | |
1624 { | |
1625 writeFunctionSignature(s, java_function, implementor, QString(), OriginalName); | |
1626 | |
1627 s << endl | |
1628 << "{" << endl; | |
1629 | |
1630 Indentation indent(INDENT); | |
1631 | |
1632 QString java_function_signature = java_function->signature(); | |
1633 /* s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) entering: " << implementor->name() << "::" | |
1634 << java_function_signature << "\");" << endl; | |
1635 */ | |
1636 writeCodeInjections(s, java_function, implementor, CodeSnip::Beginning, TypeSystem::ShellCode); | |
1637 | |
1638 // s << " printf(\"%s : %s\\n\", \"" << java_function->enclosingClass()->name() << "\"" | |
1639 // << ", \"" << java_function->name() << "\");" << endl; | |
1640 | |
1641 if (!java_function->isFinalInCpp() || java_function->isVirtualSlot()) { | |
1642 s << INDENT << "jmethodID method_id = m_vtable->method(" << id << ");" << endl; | |
1643 s << INDENT << "if (method_id) {" << endl; | |
1644 | |
1645 { | |
1646 Indentation indent(INDENT); | |
1647 s << INDENT << "JNIEnv *__jni_env = qtjambi_current_environment();" << endl; | |
1648 | |
1649 // This nasty case comes up when we're shutting down while receiving virtual | |
1650 // calls.. With these checks we safly abort... | |
1651 s << INDENT << "if (!__jni_env) {" << endl | |
1652 << " "; | |
1653 writeBaseClassFunctionCall(s, java_function, implementor); | |
1654 if (!java_function->type()) { | |
1655 s << INDENT << " return;" << endl; | |
1656 } | |
1657 s << INDENT << "}" << endl; | |
1658 | |
1659 // otherwise, continue with the function call... | |
1660 s << INDENT << "__jni_env->PushLocalFrame(100);" << endl; | |
1661 | |
1662 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1663 AbstractMetaArgumentList argumentsToReset; | |
1664 foreach (AbstractMetaArgument *argument, arguments) { | |
1665 if (!java_function->argumentRemoved(argument->argumentIndex()+1)) { | |
1666 if (!argument->type()->isPrimitive() | |
1667 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
1668 writeQtToJava(s, | |
1669 argument->type(), | |
1670 argument->indexedName(), | |
1671 "__java_" + argument->indexedName(), | |
1672 java_function, | |
1673 argument->argumentIndex() + 1); | |
1674 } | |
1675 | |
1676 if (java_function->resetObjectAfterUse(argument->argumentIndex()+1)) | |
1677 argumentsToReset.append(argument); | |
1678 } | |
1679 } | |
1680 | |
1681 for (int i=0; i<arguments.size(); ++i) | |
1682 writeOwnership(s, java_function, "__java_" + arguments.at(i)->indexedName(), i+1, implementor); | |
1683 | |
1684 | |
1685 AbstractMetaType *function_type = java_function->type(); | |
1686 QString new_return_type = java_function->typeReplaced(0); | |
1687 bool has_function_type = ((function_type != 0 | |
1688 || !new_return_type.isEmpty()) | |
1689 && new_return_type != "void"); | |
1690 | |
1691 s << INDENT; | |
1692 if (has_function_type) { | |
1693 if (new_return_type.isEmpty()) { | |
1694 s << translateType(function_type); | |
1695 } else { | |
1696 s << jniName(new_return_type); | |
1697 } | |
1698 s << " " << "__d_return_value = "; | |
1699 } | |
1700 | |
1701 s << "__jni_env->"; | |
1702 if (new_return_type.isEmpty()) { | |
1703 s << callXxxMethod(java_function->type()); | |
1704 } else if (!has_function_type) { | |
1705 s << "CallVoidMethod"; | |
1706 } else { | |
1707 s << callXxxMethod(new_return_type); | |
1708 } | |
1709 | |
1710 s << "(m_link->javaObject(__jni_env), method_id"; | |
1711 if (arguments.size() > 0) | |
1712 s << ", "; | |
1713 writeFunctionCallArguments(s, java_function, "__java_", Option(NoCasts | SkipRemovedArguments)); | |
1714 s << ");" << endl | |
1715 << INDENT << "qtjambi_exception_check(__jni_env);" << endl; | |
1716 | |
1717 if (has_function_type) { | |
1718 writeJavaToQt(s, function_type, "__qt_return_value", "__d_return_value", | |
1719 java_function, 0, GlobalRefJObject); | |
1720 | |
1721 if (java_function->nullPointersDisabled()) { | |
1722 s << INDENT << "if (__d_return_value == 0) {" << endl; | |
1723 { | |
1724 Indentation indent(INDENT); | |
1725 s << INDENT << "fprintf(stderr, \"QtJambi: Unexpected null pointer returned from override of '" << java_function->name() << "' in class '%s'\\n\"," << endl | |
1726 << INDENT << " qPrintable(qtjambi_object_class_name(__jni_env, m_link->javaObject(__jni_env))));" << endl; | |
1727 s << INDENT << "__qt_return_value = "; | |
1728 QString defaultValue = java_function->nullPointerDefaultValue(); | |
1729 if (!defaultValue.isEmpty()) | |
1730 s << defaultValue << ";"; | |
1731 else | |
1732 writeBaseClassFunctionCall(s, java_function, implementor, NoReturnStatement); | |
1733 s << endl; | |
1734 } | |
1735 | |
1736 s << INDENT << "}" << endl; | |
1737 } | |
1738 } else if (!java_function->conversionRule(TypeSystem::ShellCode, 0).isEmpty()) { | |
1739 writeConversionRule(s, TypeSystem::ShellCode, java_function, 0, "<invalid>", "<invalid>"); | |
1740 } | |
1741 | |
1742 writeOwnership(s, java_function, "this", -1, implementor); | |
1743 writeOwnership(s, java_function, "__d_return_value", 0, implementor); | |
1744 | |
1745 foreach (AbstractMetaArgument *argumentToReset, argumentsToReset) { | |
1746 | |
1747 QString argumentName = "__java_" + argumentToReset->indexedName(); | |
1748 | |
1749 s << INDENT; | |
1750 if (argumentToReset->type()->isContainer()) | |
1751 s << "qtjambi_invalidate_collection("; | |
1752 else | |
1753 s << "qtjambi_invalidate_object("; | |
1754 | |
1755 s << "__jni_env, " << argumentName << ");" << endl; | |
1756 } | |
1757 | |
1758 s << INDENT << "__jni_env->PopLocalFrame(0);" << endl; | |
1759 | |
1760 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> leaving: " << implementor->name() | |
1761 << "::" << java_function_signature << "\");" << endl; | |
1762 | |
1763 if (function_type) | |
1764 s << INDENT << "return __qt_return_value;" << endl; | |
1765 | |
1766 } | |
1767 | |
1768 s << INDENT << "} else {" << endl; | |
1769 | |
1770 { | |
1771 Indentation indent(INDENT); | |
1772 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> super() and leaving: " | |
1773 << implementor->name() << "::" << java_function_signature << "\");" << endl; | |
1774 writeBaseClassFunctionCall(s, java_function, implementor); | |
1775 } | |
1776 | |
1777 | |
1778 s << INDENT << "}" << endl; | |
1779 | |
1780 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1781 | |
1782 // A little trick to close open painters on a widget | |
1783 if (java_function->name() == "paintEvent") { | |
1784 s << INDENT << "JNIEnv *env = qtjambi_current_environment();" << endl | |
1785 << INDENT << "qtjambi_end_paint(env, m_link->javaObject(env));" << endl; | |
1786 } | |
1787 | |
1788 } else { | |
1789 if(java_function->isRemovedFrom(implementor, TypeSystem::TargetLangCode)){ | |
1790 // Avoid compiler warnings for unused parameters | |
1791 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1792 | |
1793 foreach (const AbstractMetaArgument *argument, arguments) { | |
1794 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
1795 } | |
1796 } | |
1797 writeBaseClassFunctionCall(s, java_function, implementor); | |
1798 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1799 } | |
1800 | |
1801 s << "}" << endl << endl; | |
1802 | |
1803 } | |
1804 | |
1805 // ### kill implementor | |
1806 | |
1807 void CppImplGenerator::writePublicFunctionOverride(QTextStream &s, | |
1808 const AbstractMetaFunction *java_function, | |
1809 const AbstractMetaClass *implementor) | |
1810 { | |
1811 Q_ASSERT(java_function->originalAttributes() | |
1812 & (AbstractMetaAttributes::Protected | |
1813 | AbstractMetaAttributes::Final)); | |
1814 | |
1815 // The write a public override version of this function to be used by native functions | |
1816 writeFunctionSignature(s, java_function, implementor, "__public_", | |
1817 Option(EnumAsInts | UnderscoreSpaces | |
1818 | (java_function->isAbstract() ? SkipName : NoOption))); | |
1819 s << endl | |
1820 << "{" << endl; | |
1821 Indentation indent(INDENT); | |
1822 writeBaseClassFunctionCall(s, java_function, implementor); | |
1823 s << "}" << endl << endl; | |
1824 } | |
1825 | |
1826 | |
1827 void CppImplGenerator::writeVirtualFunctionOverride(QTextStream &s, | |
1828 const AbstractMetaFunction *java_function, | |
1829 const AbstractMetaClass *implementor) | |
1830 { | |
1831 Q_ASSERT(!java_function->isFinalInCpp()); | |
1832 | |
1833 Option options = Option(EnumAsInts | UnderscoreSpaces); | |
1834 | |
1835 // The write a public override version of this function to be used by native functions | |
1836 writeFunctionSignature(s, java_function, implementor, "__override_", | |
1837 options, | |
21 | 1838 QString(), // the class prefix |
1839 QStringList() << "bool static_call"); | |
1 | 1840 s << endl |
1841 << "{" << endl; | |
1842 Indentation indent(INDENT); | |
21 | 1843 s << INDENT << "if (static_call) {" << endl; |
1 | 1844 { |
21 | 1845 Indentation indent(INDENT); |
1 | 1846 writeBaseClassFunctionCall(s, java_function, implementor); |
21 | 1847 } |
1 | 1848 s << INDENT << "} else {" << endl; |
1849 { | |
1850 Indentation indent(INDENT); | |
1851 writeBaseClassFunctionCall(s, java_function, implementor, VirtualCall); | |
1852 } | |
1853 | |
21 | 1854 s << INDENT << "}" << endl; |
1 | 1855 s << "}" << endl << endl; |
1856 } | |
1857 | |
1858 | |
1859 void CppImplGenerator::writeBaseClassFunctionCall(QTextStream &s, | |
1860 const AbstractMetaFunction *java_function, | |
1861 const AbstractMetaClass *, | |
1862 Option options) | |
1863 { | |
1864 bool static_call = !(options & VirtualCall); | |
1865 if ((options & NoReturnStatement) == 0) | |
1866 s << INDENT; | |
1867 if (java_function->isAbstract() && static_call) { | |
1868 s << default_return_statement_qt(java_function->type(), options) << ";" << endl; | |
1869 } else { | |
1870 if (java_function->type() && (options & NoReturnStatement) == 0) | |
1871 s << "return "; | |
1872 if (static_call) { | |
1873 const AbstractMetaClass *implementor = java_function->implementingClass(); | |
1874 if (java_function->isInterfaceFunction()) | |
1875 implementor = java_function->interfaceClass()->primaryInterfaceImplementor(); | |
1876 s << implementor->qualifiedCppName() << "::"; | |
1877 } | |
1878 s << java_function->originalName() << "("; | |
1879 writeFunctionCallArguments(s, java_function, QString(), Option(options | ForceEnumCast)); | |
1880 s << ");" << endl; | |
1881 } | |
1882 } | |
1883 | |
1884 | |
1885 void CppImplGenerator::writeFunctionName(QTextStream &s, | |
1886 const AbstractMetaFunction *java_function, | |
1887 const AbstractMetaClass *java_class, | |
1888 uint options) | |
1889 { | |
1890 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass(); | |
1891 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1892 | |
1893 // Function signature | |
1894 QString return_type = jniReturnName(java_function, options); | |
1895 | |
1896 QString function_name; | |
1897 | |
1898 bool callThrough = java_function->needsCallThrough(); | |
1899 /* qtd if (!callThrough) | |
1900 function_name = java_function->name(); | |
1901 else */ | |
1902 function_name = java_function->marshalledName(); | |
1903 | |
1904 QString args = "__"; | |
1905 | |
1906 if (callThrough && !java_function->isStatic() && !java_function->isConstructor()) | |
1907 args += "J"; | |
1908 | |
1909 if (!arguments.isEmpty()) { | |
1910 foreach (const AbstractMetaArgument *argument, arguments) { | |
1911 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
1912 if (!argument->type()->hasNativeId()) { | |
1913 QString modified_type = java_function->typeReplaced(argument->argumentIndex()+1); | |
1914 if (modified_type.isEmpty()) | |
1915 args += jni_signature(argument->type(), Underscores); | |
1916 else | |
1917 args += jni_signature(modified_type, Underscores); | |
1918 } else { | |
1919 args += "J"; | |
1920 } | |
1921 } | |
1922 } | |
1923 } | |
1924 | |
1925 s << jni_function_signature(cls->package(), cls->name(), function_name, | |
1926 return_type, args, options); | |
1927 | |
1928 } | |
1929 | |
1930 void CppImplGenerator::writeFinalFunctionArguments(QTextStream &s, const AbstractMetaFunction *java_function, bool d_export) | |
1931 { | |
1932 bool callThrough = java_function->needsCallThrough(); | |
1933 s << "("; | |
1934 /* qtd | |
1935 << "JNIEnv *__jni_env," << endl; | |
1936 if (!java_function->isConstructor()) { | |
1937 if (java_function->isStatic()) | |
1938 s << " jclass"; | |
1939 else | |
1940 s << " jobject"; | |
1941 } else | |
1942 s << " jobject " << java_object_name; | |
1943 */ | |
1944 uint nativeArgCount = 0; | |
1945 const AbstractMetaClass *cls = java_function->ownerClass(); | |
1946 if (java_function->isConstructor() && | |
1947 ( cls->hasVirtualFunctions() | |
1948 || cls->typeEntry()->isObject() ) ) | |
1949 { | |
1950 s << "void *d_ptr"; | |
1951 nativeArgCount++; | |
1952 } | |
1953 | |
1954 // passing pointer to C++ object | |
1955 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
1956 if (hasNativeId) { | |
1957 if (nativeArgCount > 0) | |
1958 s << "," << endl << " "; | |
1959 /* qtd << "," << endl */ s << "void* __this_nativeId"; | |
1960 nativeArgCount++; | |
1961 } | |
1962 | |
1963 AbstractMetaType *f_type = java_function->type(); | |
1964 | |
1965 // return values as strings, arrays or QModelIndex'es we return as arguments | |
1966 bool return_arg = false; | |
1967 if (f_type) { | |
1968 if (f_type->isTargetLangString() || | |
1969 f_type->isContainer() || | |
81 | 1970 f_type->name() == "QModelIndex" || |
1971 f_type->typeEntry()->isStructInD()) | |
1 | 1972 return_arg = true; |
1973 | |
1974 if (return_arg && nativeArgCount > 0) | |
1975 s << "," << endl << " "; | |
1976 | |
1977 if (f_type->isTargetLangString() || f_type->isContainer()) | |
1978 s << "void*"; | |
1979 else if (f_type->name() == "QModelIndex") | |
1980 s << "QModelIndexAccessor*"; | |
81 | 1981 else if (f_type->typeEntry()->isStructInD()) |
1982 s << f_type->typeEntry()->qualifiedCppName() << " *"; | |
1 | 1983 |
1984 if(return_arg) { | |
1985 s << " __d_return_value"; | |
1986 nativeArgCount++; | |
1987 } | |
1988 } | |
1989 | |
1990 // the function arguments | |
1991 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1992 foreach (const AbstractMetaArgument *argument, arguments) { | |
1993 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
1994 AbstractMetaType *d_type = argument->type(); | |
1995 const TypeEntry *te = d_type->typeEntry(); | |
1996 | |
1997 QString arg_name = argument->indexedName(); | |
1998 | |
1999 if (nativeArgCount > 0) | |
2000 s << "," << endl << " "; | |
81 | 2001 |
2002 // if QString argument we have to pass DArray | |
35 | 2003 if ((te && te->qualifiedCppName() == "QString") || d_type->isTargetLangString()) { |
2004 if (d_export) | |
2005 s << "string " << arg_name; | |
2006 else | |
2007 s << "DArray " << arg_name; | |
2008 } else if (d_type->isContainer()) { | |
1 | 2009 const ContainerTypeEntry *cte = |
2010 static_cast<const ContainerTypeEntry *>(te); | |
2011 if(isLinearContainer(cte)) | |
2012 s << QString("void *%1, size_t %1_size").arg(arg_name); | |
2013 } else { | |
2014 if (!d_type->hasNativeId()) { | |
2015 if(d_type->isVariant()) { | |
2016 if (d_export) s << "void*"; | |
2017 else s << "QVariant*"; | |
2018 } else | |
2019 s << translateType(argument->type(), EnumAsInts, d_export); | |
2020 } | |
2021 else if (d_type->name() == "QModelIndex") | |
2022 s << "QModelIndexAccessor"; | |
2023 else if (te->isStructInD()) | |
2024 s << te->qualifiedCppName(); | |
2025 else | |
2026 s << "void*"; | |
2027 | |
2028 s << " " << arg_name; | |
2029 } | |
2030 nativeArgCount++; | |
2031 } | |
2032 } | |
2033 s << ")"; | |
2034 } | |
2035 | |
2036 | |
2037 /*! | |
2038 Generates type conversion from Java -> Qt for all the arguments | |
2039 that are to be to be passed to the function | |
2040 */ | |
2041 void CppImplGenerator::writeFinalFunctionSetup(QTextStream &s, const AbstractMetaFunction *java_function, | |
2042 const QString &qt_object_name, | |
2043 const AbstractMetaClass *cls) | |
2044 { | |
2045 | |
2046 // Translate each of the function arguments into qt types | |
2047 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2048 foreach (const AbstractMetaArgument *argument, arguments) { | |
2049 if (!argument->type()->isPrimitive() | |
2050 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex() + 1).isEmpty()) { | |
2051 writeJavaToQt(s, | |
2052 argument->type(), | |
2053 "__qt_" + argument->indexedName(), | |
2054 argument->indexedName(), | |
2055 java_function, | |
2056 argument->argumentIndex() + 1, | |
2057 Option(UseNativeIds | EnumAsInts)); | |
2058 } | |
2059 } | |
2060 | |
2061 // Extract the qt equivalent to the this pointer and name it "qt_object_name" | |
2062 if (!java_function->isStatic() && !java_function->isConstructor()) { | |
2063 // qtd2 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls); | |
2064 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls); | |
2065 s << INDENT | |
2066 << className << " *" << qt_object_name | |
2067 << " = (" << className << " *) __this_nativeId;" | |
2068 << endl; | |
2069 // << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
2070 // qtd << INDENT << "Q_ASSERT(" << qt_object_name << ");" << endl; | |
2071 } | |
2072 } | |
2073 | |
2074 void CppImplGenerator::writeFinalFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
2075 const AbstractMetaClass *java_class) | |
2076 { | |
2077 Q_ASSERT(java_class); | |
2078 | |
2079 if (java_function->isModifiedRemoved(TypeSystem::NativeCode)) | |
2080 return; | |
2081 | |
2082 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass(); | |
2083 | |
2084 QString java_function_signature = cls->name() + "::" + java_function->signature(); | |
2085 | |
2086 s << "// " << java_function_signature << endl; | |
2087 | |
2088 const AbstractMetaType *function_type = java_function->type(); | |
2089 QString new_return_type = java_function->typeReplaced(0); | |
2090 bool has_function_type = new_return_type != "void" | |
2091 && (!new_return_type.isEmpty() || function_type != 0) | |
2092 && java_function->argumentReplaced(0).isEmpty(); | |
2093 | |
2094 const QString qt_object_name = java_function->isStatic() ? shellClassName(cls) : "__qt_this"; | |
2095 const QString java_object_name = java_function->isStatic() ? "__jni_class" : "__jni_object"; | |
2096 | |
2097 // we are not wrapping some functions depending on arguments because API is not yet full | |
2098 if (notWrappedYet(java_function)) | |
2099 return; | |
2100 | |
2101 // function signature... | |
2102 bool callThrough = java_function->needsCallThrough(); | |
2103 uint options = m_native_jump_table ? ReturnType | ExternC : StandardJNISignature; | |
2104 writeFunctionName(s, java_function, cls, options); | |
2105 s << endl; | |
2106 writeFinalFunctionArguments(s, java_function); | |
2107 s << endl << "{" << endl; | |
2108 Indentation indent(INDENT); | |
2109 | |
2110 // qtd2 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) entering: " << java_function_signature << "\");" << endl; | |
2111 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
2112 /* if (hasNativeId) | |
2113 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl; | |
2114 // Avoid compiler warnings when the variables are unused | |
2115 { | |
2116 s << INDENT << "Q_UNUSED(__jni_env)" << endl; | |
2117 | |
2118 if (java_function->isConstructor()) | |
2119 s << INDENT << "Q_UNUSED(" << java_object_name << ")" << endl; | |
2120 | |
2121 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
2122 if (hasNativeId) | |
2123 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl; | |
2124 } | |
2125 */ | |
2126 | |
2127 if (cls->isFinal() && (!java_function->isAbstract() || !java_function->isFinalInTargetLang()) && !java_function->wasPublic()) { | |
2128 QString debug = QString("protected function '%1' in final class '%2'") | |
2129 .arg(java_function->signature()).arg(java_class->name()); | |
2130 ReportHandler::warning(debug); | |
2131 // Avoid compiler warnings for unused parameters | |
2132 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2133 | |
2134 foreach (const AbstractMetaArgument *argument, arguments) { | |
2135 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
2136 } | |
2137 s << INDENT << default_return_statement_qt(java_function->type()) << ""; | |
2138 } else { | |
2139 writeFinalFunctionSetup(s, java_function, qt_object_name, cls); | |
2140 | |
2141 writeCodeInjections(s, java_function, java_function->implementingClass(), CodeSnip::Beginning, TypeSystem::NativeCode); | |
2142 | |
2143 if (java_function->isConstructor()) { | |
2144 writeFinalConstructor(s, java_function, qt_object_name, java_object_name); | |
2145 } else { | |
2146 | |
2147 QString function_prefix = ""; | |
2148 QStringList extra_param; | |
2149 Option option = NoOption; | |
2150 | |
2151 bool hasShell = cls->generateShellClass(); | |
2152 | |
2153 if (java_function->isFinalOverload()) { | |
2154 // no prefix | |
2155 } else if (java_function->isFinalInCpp() && !java_function->wasPublic() && hasShell) { | |
2156 function_prefix = "__public_"; | |
2157 } else if (!java_function->isFinalInCpp() && !java_function->isStatic() && hasShell) { | |
2158 function_prefix = "__override_"; | |
21 | 2159 extra_param.append("__do_static_call"); |
1 | 2160 s << INDENT |
21 | 2161 << "bool __do_static_call = __this_nativeId ? " |
2162 << "__" << java_class->name() << "_entity(__this_nativeId) : false;" << endl; | |
2163 } else { | |
1 | 2164 option = OriginalName; |
2165 } | |
2166 | |
2167 // Call the Qt function on the java object | |
2168 s << " "; | |
2169 if (has_function_type) { | |
2170 const QString qt_return_value = "__qt_return_value"; | |
2171 const QString java_return_value = "__d_return_value"; | |
2172 if (function_type) { | |
2173 writeTypeInfo(s, function_type, EnumAsInts); | |
2174 s << " " << qt_return_value | |
2175 << " = "; | |
2176 } | |
2177 | |
2178 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, | |
2179 extra_param); | |
2180 s << endl; | |
2181 | |
104 | 2182 writeRefArguments(s, java_function); |
2183 | |
1 | 2184 writeQtToJava(s, function_type, qt_return_value, java_return_value, |
2185 java_function, 0, EnumAsInts); | |
2186 | |
2187 // qtd s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) -> leaving: " | |
2188 // << java_function_signature << "\");"; | |
2189 | |
2190 if (function_type && function_type->name() != "QModelIndex") { | |
2191 if(function_type->typeEntry()->isStructInD()) | |
81 | 2192 ; //s << endl << INDENT << "return " << qt_return_value << ";"; |
1 | 2193 else if (!function_type->isTargetLangString() && !function_type->isContainer()) |
2194 s << endl << INDENT << "return " << java_return_value << ";"; | |
2195 } | |
2196 | |
2197 } else { | |
2198 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, | |
2199 extra_param); | |
104 | 2200 writeRefArguments(s, java_function); |
1 | 2201 } |
2202 } | |
2203 } | |
2204 if(!java_function->argumentReplaced(0).isEmpty()) { | |
2205 s << INDENT << "return 0;" << endl; | |
2206 } | |
2207 | |
2208 s << endl << "}"; | |
2209 s << endl << endl; | |
2210 } | |
2211 | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2212 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
|
2213 { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2214 AbstractMetaArgumentList arguments = java_function->arguments(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2215 foreach (const AbstractMetaArgument *argument, arguments) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2216 AbstractMetaType *d_type = argument->type(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2217 const TypeEntry *te = d_type->typeEntry(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2218 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
|
2219 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
|
2220 } |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2221 } |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2222 |
1 | 2223 void CppImplGenerator::writeAssignment(QTextStream &s, const QString &destName, const QString &srcName, |
2224 const AbstractMetaType *java_type) | |
2225 { | |
2226 if (java_type->isArray()) { | |
2227 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2228 writeAssignment(s, destName + "[" + QString::number(i) + "]", | |
2229 srcName + "[" + QString::number(i) + "]", java_type->arrayElementType()); | |
2230 } | |
2231 } else { | |
2232 s << INDENT << destName << " = " << srcName << ";" << endl; | |
2233 } | |
2234 } | |
2235 | |
2236 void CppImplGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *java_field) | |
2237 { | |
2238 Q_ASSERT(java_field); | |
2239 Q_ASSERT(java_field->isPublic() || java_field->isProtected()); | |
2240 | |
2241 const AbstractMetaFunction *setter = java_field->setter(); | |
2242 const AbstractMetaFunction *getter = java_field->getter(); | |
2243 | |
2244 const AbstractMetaClass *cls = java_field->enclosingClass(); | |
2245 FieldModification mod = cls->typeEntry()->fieldModification(java_field->name()); | |
2246 | |
2247 if(notWrappedYet(getter)) | |
2248 return; | |
2249 | |
2250 if (mod.isReadable()) { | |
2251 // Getter | |
2252 if (getter->wasProtected()) { | |
2253 writeFunctionSignature(s, getter, getter->ownerClass()); | |
2254 s << endl | |
2255 << "{" << endl; | |
2256 { | |
2257 Indentation indent(INDENT); | |
2258 s << INDENT << "return " << java_field->name() << ";" << endl; | |
2259 } | |
2260 s << "}" << endl << endl; | |
2261 } | |
2262 | |
2263 | |
2264 writeFunctionName(s, getter, getter->ownerClass()); | |
2265 s << endl; | |
2266 writeFinalFunctionArguments(s, getter); | |
2267 s << "{" << endl; | |
2268 { | |
2269 Indentation indent(INDENT); | |
2270 | |
2271 | |
2272 writeFinalFunctionSetup(s, getter, "__qt_object", getter->ownerClass()); | |
2273 | |
2274 const QString java_return_value = "__d_return_value"; | |
2275 QString qt_return_value; | |
2276 if (setter->isStatic()) | |
2277 qt_return_value = shellClassName(setter->ownerClass()) + "::"; | |
2278 else | |
2279 qt_return_value = "__qt_object->"; | |
2280 | |
2281 | |
2282 // To avoid "taking address of tmp" | |
2283 s << INDENT; | |
2284 writeTypeInfo(s, getter->type(), Option(ArrayAsPointer)); | |
2285 QString tmp_name = "__tmp_" + getter->name(); | |
2286 s << tmp_name << " = "; | |
2287 | |
2288 if (getter->wasPublic()) | |
2289 qt_return_value += java_field->name(); | |
2290 else | |
2291 qt_return_value += getter->name() + "_getter()"; | |
2292 s << qt_return_value << ";" << endl; | |
2293 | |
2294 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
|
2295 if (getter->type()->isTargetLangString() || getter->type()->name() == "QModelIndex") |
1 | 2296 ; |
2297 else if(getter->type()->typeEntry()->isStructInD()) | |
81 | 2298 ; //s << INDENT << "return " << tmp_name << ";" << endl; |
1 | 2299 else |
2300 s << INDENT << "return " << java_return_value << ";" << endl; | |
2301 } | |
2302 s << "}" << endl << endl; | |
2303 } | |
2304 | |
2305 if(notWrappedYet(setter)) | |
2306 return; | |
2307 | |
2308 // Setter | |
2309 if (mod.isWritable() && !java_field->type()->isConstant()) { | |
2310 // Write public override for protected fields | |
2311 if (setter->wasProtected()) { | |
2312 writeFunctionSignature(s, setter, setter->ownerClass()); | |
2313 s << endl | |
2314 << "{" << endl; | |
2315 { | |
2316 Indentation indent(INDENT); | |
2317 | |
2318 Q_ASSERT(setter->arguments().count() > 0); | |
2319 const AbstractMetaArgument *argument = setter->arguments().at(0); | |
2320 | |
2321 QString thisRef = java_field->isStatic() | |
2322 ? setter->ownerClass()->qualifiedCppName() + QString("::") | |
2323 : QString("this->"); | |
2324 writeAssignment(s, thisRef + java_field->name(), argument->indexedName(), argument->type()); | |
2325 } | |
2326 s << "}" << endl << endl; | |
2327 } | |
2328 | |
2329 writeFunctionName(s, setter, setter->ownerClass()); | |
2330 s << endl; | |
2331 writeFinalFunctionArguments(s, setter); | |
2332 s << "{" << endl; | |
2333 | |
2334 { | |
2335 Indentation indent(INDENT); | |
2336 | |
2337 writeFinalFunctionSetup(s, setter, "__qt_object", setter->ownerClass()); | |
2338 | |
2339 Q_ASSERT(setter->arguments().count() == 1); | |
2340 const AbstractMetaArgument *argument = setter->arguments().at(0); | |
2341 | |
2342 QString dest; | |
2343 if (setter->isStatic()) | |
2344 dest = shellClassName(setter->ownerClass()) + "::"; | |
2345 else | |
2346 dest = "__qt_object->"; | |
2347 | |
2348 QString src; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2349 if (!argument->type()->isPrimitive() && !argument->type()->typeEntry()->isStructInD()) { |
1 | 2350 src = "__qt_" + argument->indexedName(); |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2351 } else if (argument->type()->name() == "QModelIndex") { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2352 src = "qtd_to_QModelIndex(" + argument->indexedName() + ")"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2353 } else |
1 | 2354 src = argument->indexedName(); |
2355 | |
2356 if (setter->wasPublic()) | |
2357 writeAssignment(s, dest + java_field->name(), src, argument->type()); | |
2358 else | |
2359 s << INDENT << dest << setter->name() << "_setter(" << src << ");" << endl; | |
2360 } | |
2361 s << "}" << endl << endl; | |
2362 } | |
2363 } | |
2364 | |
2365 void CppImplGenerator::writeFinalDestructor(QTextStream &s, const AbstractMetaClass *cls) | |
2366 { | |
2367 if (cls->hasConstructors()) { | |
2368 s << INDENT << "extern \"C\" DLL_PUBLIC void qtd_" << cls->name() << "_destructor(void *ptr)" << endl | |
2369 << INDENT << "{" << endl; | |
2370 { | |
2371 Indentation indent(INDENT); | |
2372 /* qtd | |
2373 if (!cls->isQObject() && !cls->generateShellClass()) { | |
2374 s << INDENT << "QtJambiLink *link = QtJambiLink::findLinkForUserObject(ptr);" << endl | |
2375 << INDENT << "if (link) link->resetObject(qtjambi_current_environment());" << endl; | |
2376 } | |
2377 | |
2378 // Code injectsions... | |
2379 foreach (const CodeSnip &snip, cls->typeEntry()->codeSnips()) { | |
2380 if (snip.language == TypeSystem::DestructorFunction) { | |
2381 s << snip.code(); | |
2382 } | |
2383 } | |
2384 */ | |
2385 s << INDENT << "delete (" << shellClassName(cls) << " *)ptr;" << endl; | |
2386 | |
2387 #if defined(QTJAMBI_DEBUG_TOOLS) | |
2388 s << INDENT << "qtjambi_increase_destructorFunctionCalledCount(QString::fromLatin1(\"" << cls->name() << "\"));" << endl; | |
2389 #endif | |
2390 } | |
2391 | |
2392 s << INDENT << "}" << endl << endl; | |
2393 } | |
2394 } | |
2395 | |
2396 void CppImplGenerator::writeFinalConstructor(QTextStream &s, | |
2397 const AbstractMetaFunction *java_function, | |
2398 const QString &qt_object_name, | |
2399 const QString &java_object_name) | |
2400 { | |
2401 const AbstractMetaClass *cls = java_function->ownerClass(); | |
2402 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2403 QString className = cls->typeEntry()->qualifiedCppName(); | |
2404 | |
2405 bool hasShellClass = cls->generateShellClass(); | |
2406 | |
2407 s << INDENT << shellClassName(cls) << " *" << qt_object_name | |
2408 << " = new " << shellClassName(cls) | |
2409 << "("; | |
2410 writeFunctionCallArguments(s, java_function, "__qt_"); | |
2411 s << ");" << endl; | |
2412 s << INDENT << "return (void *) " << qt_object_name << ";" << endl; // qtd | |
2413 | |
2414 /* qtd s << INDENT << "QtJambiLink *__qt_java_link = "; | |
2415 if (cls->isQObject()) { | |
2416 s << "qtjambi_construct_qobject(__jni_env, " << java_object_name << ", " | |
2417 << qt_object_name << ")"; | |
2418 } else { | |
2419 s << "qtjambi_construct_object(__jni_env, " << java_object_name << ", " << qt_object_name; | |
2420 if (cls->typeEntry()->isValue()) | |
2421 s << ", \"" << className << "\")"; | |
2422 else // non-QObject, object type | |
2423 s << ", QMetaType::Void, QLatin1String(\"" << cls->fullName().replace(".", "/") << "\"), true)"; | |
2424 } | |
2425 s << ";" << endl | |
2426 << INDENT << "if (!__qt_java_link) {" << endl; | |
2427 { | |
2428 Indentation indent(INDENT); | |
2429 s << INDENT << "qWarning(\"object construction failed for type: " | |
2430 << className << "\");" << endl | |
2431 << INDENT << "return;" << endl; | |
2432 } | |
2433 s << INDENT << "}" << endl; | |
2434 | |
2435 | |
2436 if (cls->isQObject()) { | |
2437 // Make sure all qobjects created by Java are owned by java only if | |
2438 // parent object has not been set. | |
2439 // All other objects will default to split ownership. | |
2440 s << INDENT << "if(!__qt_this->QObject::parent()){" << endl; | |
2441 s << INDENT << " __qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2442 s << INDENT << "}" << endl; | |
2443 } else { | |
2444 // All non-qobjects created by Java are owned by java | |
2445 s << INDENT << "__qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2446 } | |
2447 | |
2448 if (hasCustomDestructor(cls)) { | |
2449 s << INDENT << "__qt_java_link->setDestructorFunction(qtjambi_destructor);" << endl; | |
2450 } | |
2451 | |
2452 if (cls->typeEntry()->typeFlags() & ComplexTypeEntry::DeleteInMainThread) | |
2453 s << INDENT << "__qt_java_link->setDeleteInMainThread(true);" << endl; | |
2454 | |
2455 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions() && !cls->typeEntry()->isObject()) | |
2456 return; | |
2457 | |
2458 if (hasShellClass) { | |
2459 // Set up the link object | |
2460 s << INDENT << qt_object_name << "->m_link = __qt_java_link;" << endl; | |
2461 | |
2462 // Make sure the user data in the QObject has bindings to the qobject's meta object | |
2463 // (this has to be done after the link is set, so that the fake meta object | |
2464 // can access the java object, for which it gets a reference in the link) | |
2465 if (cls->isQObject()) | |
2466 s << INDENT << qt_object_name << "->m_link->setMetaObject(" << qt_object_name << "->metaObject());" << endl; | |
2467 | |
2468 s << INDENT << qt_object_name << "->m_link->setCreatedByJava(true);" << endl; | |
2469 | |
2470 | |
2471 | |
2472 AbstractMetaClassList interfaces = cls->interfaces(); | |
2473 if (interfaces.size() + (cls->baseClass() != 0 ? 1 : 0) > 1) { | |
2474 if (cls->baseClass() != 0) | |
2475 interfaces += cls->baseClass(); | |
2476 foreach (AbstractMetaClass *iface, interfaces) { | |
2477 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface; | |
2478 s << INDENT << qt_object_name << "->m_link->registerSubObject((" << impl->qualifiedCppName() << " *) " << qt_object_name << ");" << endl; | |
2479 } | |
2480 } | |
2481 } | |
2482 | |
2483 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions()) | |
2484 return; | |
2485 | |
2486 // Set up the vtable | |
2487 s << INDENT; | |
2488 QString space(24, ' '); | |
2489 if (hasShellClass) | |
2490 s << qt_object_name << "->m_vtable = "; | |
2491 s << "qtjambi_setup_vtable(__jni_env, " << endl << space << "__jni_object, " << endl; | |
2492 | |
2493 if (cls->hasInconsistentFunctions()) { | |
2494 s << space << "qtjambi_inconsistent_count, " << endl | |
2495 << space << "qtjambi_inconsistent_names, " << endl | |
2496 << space << "qtjambi_inconsistent_signatures, " << endl; | |
2497 } else { | |
2498 s << space << "0, 0, 0, // no inconsistent functions" << endl; | |
2499 } | |
2500 | |
2501 if (cls->hasVirtualFunctions()) { | |
2502 s << space << "qtjambi_method_count, " << endl | |
2503 << space << "qtjambi_method_names, " << endl | |
2504 << space << "qtjambi_method_signatures" << endl; | |
2505 } else { | |
2506 s << space << "0, 0, 0 // no virtual functions" << endl; | |
2507 } | |
2508 | |
2509 s << space << ");" << endl; */ | |
2510 } | |
2511 | |
2512 void CppImplGenerator::writeSignalInitialization(QTextStream &s, const AbstractMetaClass *java_class) | |
2513 { | |
2514 if (!java_class->isQObject() | |
2515 || java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromTargetLang).size() == 0) { | |
2516 return ; | |
2517 } | |
2518 | |
2519 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_signalInitialization", "jboolean") | |
2520 << endl << "(JNIEnv *__jni_env, jobject java_object, jlong ptr, jstring java_signal_name)" << endl | |
2521 << "{" << endl | |
2522 << " QtJambiLink *link = (QtJambiLink *) ptr;" << endl | |
2523 << " if (link == 0)" << endl | |
2524 << " return true;" << endl << endl | |
2525 << " QObject *qt_this = link->qobject();" << endl | |
2526 << " Q_ASSERT(qt_this);" << endl << endl | |
2527 << " QtJambi_SignalWrapper_" << java_class->name() << " *qt_wrapper = " | |
2528 << " (QtJambi_SignalWrapper_" << java_class->name() << " *) link->signalWrapper();" << endl | |
2529 << " if (qt_wrapper == 0) {" << endl | |
2530 << " qt_wrapper = new QtJambi_SignalWrapper_" << java_class->name() << ";" << endl | |
2531 << " link->setSignalWrapper(qt_wrapper);" << endl | |
2532 << " qt_wrapper->link = link;" << endl << endl | |
2533 << " qtjambi_resolve_signals(__jni_env," << endl | |
2534 << " java_object," << endl | |
2535 << " qt_wrapper->m_signals," << endl | |
2536 << " qtjambi_signal_count," << endl | |
2537 << " (char **) qtjambi_signal_names," << endl | |
2538 << " (int *) qtjambi_signal_argumentcounts);" << endl | |
2539 << " }" << endl | |
2540 << " QString signal_name = qtjambi_to_qstring(__jni_env, java_signal_name);" << endl | |
2541 << " return qtjambi_connect_cpp_to_java(__jni_env," << endl | |
2542 << " signal_name," << endl | |
2543 << " qt_this," << endl | |
2544 << " qt_wrapper," << endl | |
2545 << " QLatin1String(\"" << java_class->fullName() << "\")," << endl | |
2546 << " QLatin1String(\"" << signalWrapperPrefix() << "\"));" << endl | |
2547 << "}"; | |
2548 } | |
2549 | |
2550 QString CppImplGenerator::fromObject(const TypeEntry *entry, | |
2551 const QString &var_name) | |
2552 { | |
2553 QString returned; | |
2554 QString package = entry->javaPackage(); | |
2555 const ComplexTypeEntry *centry = entry->isComplex() | |
2556 ? static_cast<const ComplexTypeEntry *>(entry) | |
2557 : 0; | |
2558 | |
2559 if (centry == 0 || centry->polymorphicIdValue().isEmpty()) { | |
2560 /* qtd returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2561 + entry->lookupName() | |
2562 + "\", \"" + QString(package).replace(".", "/") + "/\", true);"; | |
2563 */ | |
2564 if(entry->isObject()) | |
2565 returned = var_name + ";"; | |
2566 else | |
2567 returned = "new " + entry->lookupName() + "(" + var_name +");"; | |
2568 } else { | |
2569 AbstractMetaClass *cls = classes().findClass(centry->qualifiedCppName()); | |
2570 if (!cls) { | |
2571 qFatal("CppImplGenerator::fromObject(): class '%s' could not be resolved...", | |
2572 qPrintable(centry->qualifiedCppName())); | |
2573 } | |
2574 | |
2575 while (cls != 0 && !cls->typeEntry()->isPolymorphicBase()) | |
2576 cls = cls->baseClass(); | |
2577 | |
2578 QString full_name; | |
2579 if (cls != 0) { | |
2580 full_name = cls->fullName(); | |
2581 } else { | |
2582 ReportHandler::warning(QString("class '%1' has polymorphic id but does not inherit a polymorphic class") | |
2583 .arg(centry->qualifiedCppName())); | |
2584 } | |
2585 /* qtd | |
2586 returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2587 + centry->lookupName() | |
2588 + "\", \"" + QString(package).replace(".", "/") + "/\"," | |
2589 + "\"" + jni_signature(full_name, Underscores) + "\", true); // fucking complex"; | |
2590 */ | |
2591 if(entry->isObject()) | |
2592 returned = var_name + "; // complex entry"; | |
2593 else | |
2594 returned = "new " + centry->lookupName() + "(" + var_name +"); // complex entry"; | |
2595 } | |
2596 | |
2597 return returned; | |
2598 } | |
2599 | |
2600 void CppImplGenerator::writeOriginalMetaObjectFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2601 { | |
2602 Q_ASSERT(java_class->isQObject()); | |
2603 | |
2604 s << jni_function_signature(java_class->package(), | |
2605 java_class->name(), | |
2606 "originalMetaObject", | |
2607 "jlong"); | |
2608 | |
2609 s << endl | |
2610 << "(JNIEnv *," << endl | |
2611 << " jclass)" << endl | |
2612 << "{" << endl; | |
2613 { | |
2614 Indentation indent(INDENT); | |
2615 s << INDENT << "return reinterpret_cast<jlong>(&" << java_class->qualifiedCppName() << "::staticMetaObject);" << endl; | |
2616 } | |
2617 s << "}" << endl << endl; | |
2618 } | |
2619 | |
2620 void CppImplGenerator::writeFromNativeFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2621 { | |
2622 s << jni_function_signature(java_class->package(), | |
2623 java_class->name(), | |
2624 "fromNativePointer", | |
2625 "jobject"); | |
2626 s << endl | |
2627 << "(JNIEnv *__jni_env," << endl | |
2628 << " jclass," << endl | |
2629 << " jobject nativePointer)" << endl | |
2630 << "{" << endl; | |
2631 { | |
2632 Indentation indent(INDENT); | |
2633 s << INDENT << "void *ptr = qtjambi_to_cpointer(__jni_env, nativePointer, 1);" << endl | |
2634 << INDENT << "return " << fromObject(java_class->typeEntry(), "ptr") << endl | |
2635 << "}" << endl; | |
2636 } | |
2637 } | |
2638 | |
2639 void CppImplGenerator::writeFromArrayFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2640 { | |
2641 s << jni_function_signature(java_class->package(), | |
2642 java_class->name(), | |
2643 "nativePointerArray", | |
2644 "jobject"); | |
2645 s << endl | |
2646 << "(JNIEnv *__jni_env," << endl | |
2647 << " jclass," << endl | |
2648 << " jobjectArray array)" << endl | |
2649 << "{" << endl; | |
2650 { | |
2651 Indentation indent(INDENT); | |
2652 s << INDENT << "return qtjambi_array_to_nativepointer(__jni_env, " << endl | |
2653 << INDENT << " array, " << endl | |
2654 << INDENT << " sizeof(" | |
2655 << java_class->qualifiedCppName() << "));" << endl; | |
2656 } | |
2657 s << "}" << endl; | |
2658 } | |
2659 | |
2660 | |
2661 void CppImplGenerator::writeInterfaceCastFunction(QTextStream &s, | |
2662 const AbstractMetaClass *java_class, | |
2663 const AbstractMetaClass *interface) | |
2664 { | |
2665 Q_ASSERT(interface->isInterface()); | |
2666 const InterfaceTypeEntry *ie = static_cast<const InterfaceTypeEntry *>(interface->typeEntry()); | |
2667 QString interface_name = ie->origin()->targetLangName(); | |
2668 | |
2669 s << endl | |
2670 << jni_function_signature(java_class->package(), | |
2671 java_class->name(), | |
2672 QString("__qt_cast_to_%1").arg(interface_name), | |
2673 "jlong", | |
2674 "__J"); | |
2675 | |
2676 s << endl | |
2677 << "(JNIEnv *," << endl | |
2678 << " jobject," << endl | |
2679 << " jlong ptr)" << endl | |
2680 << "{" << endl | |
2681 << " return (jlong) (" << interface->primaryInterfaceImplementor()->qualifiedCppName() << " *) " | |
2682 << "(" << java_class->qualifiedCppName() << " *) ptr;" << endl | |
2683 << "}" << endl; | |
2684 } | |
2685 | |
2686 bool CppImplGenerator::writeConversionRule(QTextStream &s, | |
2687 TypeSystem::Language target_language, | |
2688 const AbstractMetaFunction *java_function, | |
2689 int argument_index, | |
2690 const QString &qt_name, | |
2691 const QString &java_name) | |
2692 { | |
2693 if (argument_index < 0 || java_function == 0) | |
2694 return false; | |
2695 | |
2696 QString conversion_rule = java_function->conversionRule(target_language, argument_index); | |
2697 | |
2698 if (!conversion_rule.isEmpty()) { | |
2699 QString qt_name_var; | |
2700 QString java_name_var; | |
2701 | |
2702 if ((argument_index == 0 && target_language == TypeSystem::NativeCode) | |
2703 || (argument_index != 0 && target_language == TypeSystem::ShellCode)) { | |
2704 qt_name_var = "%in"; | |
2705 java_name_var = "%out"; | |
2706 } else { | |
2707 qt_name_var = "%out"; | |
2708 java_name_var = "%in"; | |
2709 } | |
2710 | |
2711 conversion_rule = conversion_rule.replace(qt_name_var, qt_name) | |
2712 .replace(java_name_var, java_name); | |
2713 | |
2714 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2715 for (int i=0; i<arguments.size(); ++i) { | |
2716 conversion_rule = conversion_rule.replace("%" + QString::number(i+1), | |
2717 arguments.at(i)->indexedName()); | |
2718 } | |
2719 | |
2720 QStringList lines = conversion_rule.split("\n"); | |
2721 foreach (QString line, lines) { | |
2722 s << INDENT << line.trimmed() << endl; | |
2723 } | |
2724 | |
2725 return true; | |
2726 } else { | |
2727 return false; | |
2728 } | |
2729 } | |
2730 | |
2731 | |
2732 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2733 const AbstractMetaClass *java_class, | |
2734 const AbstractMetaType *function_return_type, | |
2735 const QString &qt_name, | |
2736 const QString &java_name, | |
2737 const AbstractMetaFunction *java_function, | |
2738 int argument_index) | |
2739 { | |
2740 // Conversion to C++: Shell code for return values, native code for arguments | |
2741 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2742 if (writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2743 return; | |
2744 | |
2745 s << INDENT << shellClassName(java_class) << " *" << qt_name << " = (" | |
2746 << shellClassName(java_class) << " *) "; | |
2747 if (java_class->isQObject()) | |
2748 s << "qtjambi_to_qobject"; | |
2749 else | |
2750 s << "qtjambi_to_object"; | |
2751 s << "(__jni_env, " << java_name << ");" << endl; | |
2752 if (java_class->isQObject()) { | |
2753 // ### throw exceptions when objects are null... | |
2754 s << INDENT << "if (!" << qt_name << ") " | |
2755 << default_return_statement_java(function_return_type) << ";" << endl << endl; | |
2756 } | |
2757 } | |
2758 | |
2759 | |
2760 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2761 const AbstractMetaType *java_type, | |
2762 const QString &qt_name, | |
2763 const QString &java_name, | |
2764 const AbstractMetaFunction *java_function, | |
2765 int argument_index, | |
2766 Option options) | |
2767 { | |
2768 // Conversion to C++: Shell code for return values, native code for arguments | |
2769 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2770 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2771 return; | |
2772 | |
2773 if (java_type == 0) { | |
2774 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
2775 "language '%4'") | |
2776 .arg(argument_index) | |
2777 .arg(java_function->implementingClass()->name()) | |
2778 .arg(java_function->name()) | |
2779 .arg(int(lang)); | |
2780 ReportHandler::warning(warn); | |
2781 return; | |
2782 } | |
2783 if (java_type->name() == "QModelIndex") { | |
2784 s << INDENT << "QModelIndex " << qt_name << " = qtd_to_QModelIndex(" | |
2785 << java_name << ");" << endl; | |
2786 } else if (java_type->typeEntry()->isStructInD()) { | |
2787 // empty | |
2788 } else if (java_type->typeEntry() && java_type->typeEntry()->qualifiedCppName() == "QString") { | |
2789 s << INDENT << "QString " << qt_name | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2790 << " = " << QString("QString::fromUtf8((const char *)%1.ptr, %1.length);").arg(java_name) << endl; |
1 | 2791 } else if (java_type->isJObjectWrapper()) { |
2792 s << INDENT << "JObjectWrapper " << qt_name | |
2793 << " = qtjambi_to_jobjectwrapper(__jni_env, " << java_name << ");" << endl; | |
2794 } else if (java_type->isVariant()) { | |
2795 s << INDENT << "QVariant " << qt_name | |
2796 << " = " << java_name << " == NULL ? QVariant() : QVariant(*" << java_name << ");" << endl; | |
2797 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { | |
2798 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2799 | |
2800 // ### 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
|
2801 // s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; |
1 | 2802 s << INDENT; |
2803 writeTypeInfo(s, elementType); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2804 s << " *" << qt_name << " = ("; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2805 writeTypeInfo(s, elementType); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2806 s << "*) " << java_name << ";" << endl; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2807 /* |
1 | 2808 s << INDENT << "__jni_env->" << getXxxArrayRegion(elementType) << "( (" << translateType(java_type, options) |
2809 << ")" << java_name << ", 0, " << java_type->arrayElementCount() << ", " | |
2810 << "(" << translateType(elementType, options) << " *" << ")" | |
2811 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2812 */ |
1 | 2813 } else if (java_type->isArray()) { |
2814 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2815 | |
2816 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; | |
2817 writeTypeInfo(s, elementType); | |
2818 s << "[" << java_type->arrayElementCount() << "]" << qt_name << ";" << endl; | |
2819 | |
2820 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2821 writeJavaToQt(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
2822 "__jni_env->GetObjectArrayElement(" + java_name + ", " + QString::number(i) + ")", 0, -1, options); | |
2823 } | |
2824 | |
2825 } else if (java_type->isTargetLangString()) { | |
2826 s << INDENT << "QString " << qt_name | |
2827 << " = " << "QString::fromUtf8(" << java_name << ", " << java_name << "_size);" << endl; | |
2828 // qtd << " = qtjambi_to_qstring(__jni_env, (jstring) " << java_name << ");" << endl; | |
2829 | |
2830 } else if (java_type->isTargetLangChar()) { | |
2831 s << INDENT << "QChar " << qt_name | |
2832 << " = (ushort)" << java_name << ";" << endl; | |
2833 | |
2834 } else if (java_type->isEnum() || java_type->isFlags()) { | |
2835 | |
2836 bool written = false; | |
2837 if (java_type->isEnum()) { | |
2838 AbstractMetaEnum *java_enum = | |
2839 m_classes.findEnum(static_cast<const EnumTypeEntry *>(java_type->typeEntry())); | |
2840 if (java_enum && !java_enum->isPublic()) { | |
2841 | |
2842 s << INDENT << "int " << qt_name << " = "; | |
2843 written = true; | |
2844 } | |
2845 } | |
2846 | |
2847 if (!written) { | |
2848 QString qualified_name = java_type->typeEntry()->qualifiedCppName(); | |
2849 s << INDENT << qualified_name << " " << qt_name | |
2850 << " = (" << qualified_name << ") "; | |
2851 } | |
2852 | |
2853 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
|
2854 s << java_name << ";" << endl; |
1 | 2855 |
2856 } else if (options & BoxedPrimitive) { | |
2857 const PrimitiveTypeEntry *pentry = TypeDatabase::instance()->findTargetLangPrimitiveType("int"); | |
2858 Q_ASSERT(pentry); | |
2859 | |
2860 s << java_name << ";" << endl; | |
2861 | |
2862 } else { | |
2863 s << java_name << ';' << endl; | |
2864 } | |
2865 | |
2866 } else if (java_type->isContainer()) { | |
2867 writeJavaToQtContainer(s, java_type, qt_name, java_name, 0, -1); | |
2868 | |
2869 } else if (java_type->isThread()) { | |
2870 s << INDENT << "QThread *" << qt_name << " = qtjambi_to_thread(__jni_env, " << java_name | |
2871 << ");" << endl; | |
2872 | |
2873 } else if (java_type->typeEntry()->isCustom()) { | |
2874 const CustomTypeEntry *custom_type = | |
2875 static_cast<const CustomTypeEntry *>(java_type->typeEntry()); | |
2876 s << INDENT; | |
2877 custom_type->generateCppJavaToQt(s, java_type, "__jni_env", qt_name, java_name); | |
2878 s << ";" << endl; | |
2879 | |
2880 } else { | |
2881 | |
2882 const TypeEntry *type = java_type->typeEntry(); | |
2883 QString class_name = type->name(); | |
2884 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName()); | |
2885 | |
2886 // Declaration and the c-cast | |
2887 s << INDENT; | |
2888 writeTypeInfo(s, java_type); | |
2889 s << ' ' << qt_name << " = ("; | |
2890 writeTypeInfo(s, java_type); | |
2891 s << ") "; | |
2892 | |
2893 if (java_type->isPrimitive()) { | |
2894 if (options & BoxedPrimitive) { | |
2895 const PrimitiveTypeEntry *pentry = static_cast<const PrimitiveTypeEntry *>(type); | |
2896 //std::cout << "---error_here " << type->targetLangName().toStdString() << " \n"; | |
2897 //std::cout << "----func_here " << java_function->marshalledName().toStdString() << " \n"; | |
2898 | |
2899 if (!pentry->preferredConversion()) | |
2900 pentry = TypeDatabase::instance()->findTargetLangPrimitiveType(pentry->targetLangName()); | |
2901 Q_ASSERT(pentry); | |
2902 | |
2903 s << java_name << ";" << endl; | |
2904 | |
2905 } else if ((options & GlobalRefJObject) && type->jniName() == QLatin1String("jobject")) { | |
2906 s << "__jni_env->NewGlobalRef(" << java_name << ");" << endl; | |
2907 } else { | |
2908 s << java_name << ';' << endl; | |
2909 } | |
2910 | |
2911 #if 0 | |
2912 } else if (java_type->isEnum()) { | |
2913 s << "qtjambi_to_enum(__jni_env, " << java_name << ");" << endl; | |
2914 #endif | |
2915 | |
2916 } else if ((java_type->isQObject() || java_type->isObject()) | |
2917 && static_cast<const ObjectTypeEntry *>(type)->designatedInterface()) { | |
2918 /* qtd const InterfaceTypeEntry *ie = | |
2919 static_cast<const ObjectTypeEntry *>(type)->designatedInterface(); | |
2920 s << "qtjambi_to_interface(__jni_env, "; | |
2921 | |
2922 // This cast is only valid if we're dealing with a native id | |
2923 if ((options & UseNativeIds) == UseNativeIds) | |
2924 s << "(QtJambiLink *)"; | |
2925 */ | |
2926 s << java_name << ";" << endl; | |
2927 /* qtd | |
2928 s << "\"" << ie->targetLangName() << "\", \"" | |
2929 << ie->javaPackage().replace(".", "/") << "/\", " | |
2930 << "\"__qt_cast_to_" << type->targetLangName() << "\");" << endl; | |
2931 */ | |
2932 } else if (java_type->isObject() || java_type->isQObject() || java_type->isNativePointer()) { | |
2933 if (java_type->isReference()) { | |
2934 s << "* (" << qualified_class_name << " " | |
2935 << QString(java_type->actualIndirections(), '*') << ") "; | |
2936 } | |
2937 | |
2938 if (java_type->isNativePointer()) { | |
2939 /* qtd s << "qtjambi_to_cpointer(" | |
2940 << "__jni_env, " | |
2941 << java_name << ", " | |
2942 << java_type->actualIndirections() << ");" << endl; */ | |
2943 s << java_name << ";" << endl; // qtd | |
2944 }/* qtd else if (java_type->isQObject()) { | |
2945 if ((options & UseNativeIds) == 0) | |
2946 s << "qtjambi_to_qobject(__jni_env, "; | |
2947 else | |
2948 s << "qtjambi_from_jlong("; | |
2949 s << java_name; | |
2950 s << ");" << endl; | |
2951 }*/ else { | |
2952 /* qtd if ((options & UseNativeIds) == 0) | |
2953 s << "qtjambi_to_object(__jni_env, "; | |
2954 else | |
2955 s << "qtjambi_from_jlong("; | |
2956 */ s << java_name; | |
2957 s << ";" << endl; // + | |
2958 // qtd s << ");" << endl; | |
2959 } | |
2960 | |
2961 } else { | |
2962 // Return values... | |
2963 if (argument_index == 0) { | |
2964 s << "(" << java_name << " != 0 ? *(" << qualified_class_name << " *)"; | |
2965 /* qtd if ((options & UseNativeIds) == 0) | |
2966 s << "qtjambi_to_object(__jni_env, "; | |
2967 else | |
2968 s << "qtjambi_from_jlong("; | |
2969 */ s << java_name; | |
2970 s << " : " << qualified_class_name << "());" << endl; | |
2971 } else { | |
2972 s << "*" | |
2973 << "(" << qualified_class_name << " *)"; | |
2974 bool null_check = false; | |
2975 /* qtd if ((options & UseNativeIds) == 0) { | |
2976 s << "qtjambi_to_object(__jni_env, "; | |
2977 } else if (hasDefaultConstructor(java_type)) { | |
2978 null_check = true; | |
2979 s << "(" << java_name << " != 0 ? qtjambi_from_jlong("; | |
2980 } else { | |
2981 s << "qtjambi_from_jlong("; | |
2982 } | |
2983 */ s << java_name; | |
2984 // qtd s << ")"; | |
2985 | |
2986 if (null_check) | |
2987 s << " : default_" << QString(qualified_class_name).replace("::", "_") << "())"; | |
2988 s << ";" << endl; | |
2989 } | |
2990 | |
2991 } | |
2992 } | |
2993 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl; | |
2994 } | |
2995 | |
2996 void CppImplGenerator::writeQtToJava(QTextStream &s, | |
2997 const AbstractMetaType *java_type, | |
2998 const QString &qt_name, | |
2999 const QString &java_name, | |
3000 const AbstractMetaFunction *java_function, | |
3001 int argument_index, | |
3002 Option option) | |
3003 { | |
3004 | |
3005 // Conversion to Java: Native code for return values, shell code for arguments | |
3006 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode; | |
3007 /* qtd if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3008 return; | |
3009 */ | |
3010 if (java_type == 0) { | |
3011 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3012 "language '%4'") | |
3013 .arg(argument_index) | |
3014 .arg(java_function->implementingClass()->name()) | |
3015 .arg(java_function->name()) | |
3016 .arg(int(lang)); | |
3017 ReportHandler::warning(warn); | |
3018 return; | |
3019 } | |
3020 | |
3021 if (java_type->name() == "QModelIndex") { | |
3022 QString prefix = "*"; | |
3023 if (option & BoxedPrimitive) | |
3024 s << INDENT << "QModelIndexAccessor tmp_index = qtd_from_QModelIndex(" << qt_name << ");" << endl | |
3025 << INDENT << "QModelIndexAccessor *" << java_name << " = &tmp_index;" << endl; | |
3026 else | |
3027 s << INDENT << "*" << java_name << " = qtd_from_QModelIndex(" << qt_name << ");" << endl; | |
3028 | |
81 | 3029 } else if (java_type->typeEntry()->isStructInD()) { |
3030 if (option & BoxedPrimitive) { | |
3031 s << INDENT << java_type->typeEntry()->name() << " *" << java_name << " = (" | |
3032 << java_type->typeEntry()->name() << " *) &" << qt_name << ";" << endl; | |
3033 } else { | |
3034 s << INDENT << "*" << java_name << " = " << qt_name << ";" << endl; | |
3035 } | |
1 | 3036 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { |
3037 AbstractMetaType *elementType = java_type->arrayElementType(); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3038 /* qtd |
1 | 3039 s << INDENT << translateType(java_type, option) << " " << java_name << " = __jni_env->" << newXxxArray(elementType) |
3040 << "(" << java_type->arrayElementCount() << ");" << endl; | |
3041 | |
3042 s << INDENT << "__jni_env->" << setXxxArrayRegion(elementType) << "(" | |
3043 << "(" << translateType(java_type, option) << ")" << java_name | |
3044 << ", 0, " << java_type->arrayElementCount() << ", " | |
3045 << "(" << translateType(elementType, option) << " *" << ")" | |
3046 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3047 */ |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3048 s << INDENT << translateType(java_type, option) << " " << java_name << " = " << qt_name << ";" <<endl; |
1 | 3049 } else if (java_type->isArray()) { |
3050 AbstractMetaType *elementType = java_type->arrayElementType(); | |
3051 | |
3052 s << INDENT << "jobject " << java_name << " = __jni_env->NewObjectArray(" | |
3053 << java_type->arrayElementCount() << ");" << endl; | |
3054 | |
3055 s << "jobject __qt_element = 0;"; | |
3056 | |
3057 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
3058 writeQtToJava(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
3059 "__qt_element", 0, -1, option); | |
3060 s << "__jni_env->SetObjectArrayElement((jobjectArray) " << java_name << ", " | |
3061 << i << ", __qt_element);" << endl; | |
3062 } | |
3063 | |
3064 } else if (java_type->isPrimitive()) { | |
3065 const PrimitiveTypeEntry *type = | |
3066 static_cast<const PrimitiveTypeEntry *>(java_type->typeEntry()); | |
3067 | |
3068 Q_ASSERT(type); | |
3069 QString ret_val; | |
3070 if (java_function) | |
3071 ret_val = jniReturnName(java_function); | |
3072 else | |
3073 ret_val = fixCppTypeName(java_type->typeEntry()->qualifiedCppName()); | |
3074 s << INDENT << ret_val << " " << java_name << " = " << qt_name << ";" << endl; | |
3075 } else if (java_type->isJObjectWrapper()) { | |
3076 s << INDENT << "jobject " << java_name << " = qtjambi_from_jobjectwrapper(__jni_env, " | |
3077 << qt_name << ");" << endl; | |
3078 } else if (java_type->isVariant()) { | |
3079 s << INDENT << "QVariant *" << java_name | |
3080 << " = new QVariant(" << qt_name << ");" << endl; | |
3081 } else if (java_type->isTargetLangString()) { | |
35 | 3082 if(java_type->typeEntry()->qualifiedCppName() == "QStringRef") { |
3083 s << INDENT << "const QString *str_ref = " << qt_name << ".string();" << endl | |
3084 << INDENT << "if(str_ref)" << endl | |
3085 << INDENT << " _d_toUtf8(str_ref->utf16(), str_ref->size(), " << java_name << ");" << endl | |
3086 << INDENT << "else {" << endl | |
3087 << INDENT << " QString empty_str;" << endl | |
3088 << INDENT << " _d_toUtf8(empty_str.utf16(), empty_str.size(), " << java_name << ");" << endl | |
3089 << INDENT << "}" << endl; | |
3090 } else { | |
3091 s << INDENT << QString("_d_toUtf8(%1.utf16(), %1.size(), %2);").arg(qt_name, java_name) << endl; | |
3092 } | |
1 | 3093 } else if (java_type->isTargetLangChar()) { |
3094 s << INDENT << "jchar " << java_name << " = " << qt_name << ".unicode();" << endl; | |
3095 | |
3096 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags() | |
3097 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) { | |
3098 // } else if (java_type->isEnum() || java_type->isFlags()) { | |
3099 | |
3100 // if (option & EnumAsInts) { | |
3101 // qDebug() << java_type->name() << "should be int..."; | |
3102 // } | |
3103 | |
3104 /* if (option & BoxedPrimitive) { | |
3105 s << INDENT << "jobject " << java_name << " = qtjambi_from_int(__jni_env, " | |
3106 << qt_name << ");" << endl; | |
3107 } else */{ | |
3108 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl; | |
3109 } | |
3110 | |
3111 } else if (java_type->isTargetLangEnum()) { | |
3112 Q_ASSERT((option & EnumAsInts) == 0); | |
3113 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
3114 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl; | |
3115 | |
3116 } else if (java_type->isTargetLangFlags()) { | |
3117 Q_ASSERT((option & EnumAsInts) == 0); | |
3118 const FlagsTypeEntry *ft = static_cast<const FlagsTypeEntry *>(java_type->typeEntry()); | |
3119 s << INDENT << "jobject " << java_name << " = qtjambi_from_flags(__jni_env, " | |
3120 << qt_name << ", \"" << ft->javaPackage().replace('.', '/') << '/' | |
3121 << ft->originator()->javaQualifier() << '$' << ft->targetLangName() << "\");" << endl; | |
3122 | |
3123 } else if (java_type->isContainer()) { | |
3124 writeQtToJavaContainer(s, java_type, qt_name, java_name, 0, -1); | |
3125 | |
3126 } else if (java_type->isThread()) { | |
3127 s << INDENT << "jobject " << java_name << " = qtjambi_from_thread(__jni_env, " << qt_name | |
3128 << ");" << endl; | |
3129 | |
3130 } else if (!java_type->isNativePointer() && java_type->typeEntry()->isCustom()) { | |
3131 s << INDENT; | |
3132 static_cast<const CustomTypeEntry *>(java_type->typeEntry()) | |
3133 ->generateCppQtToJava(s, java_type, "__jni_env", qt_name, java_name); | |
3134 s << ";" << endl; | |
3135 | |
3136 } else { | |
3137 QString return_type; | |
3138 if (java_function) | |
3139 return_type = jniReturnName(java_function); | |
3140 else { | |
3141 return_type = jniReturnType(java_type); | |
3142 return_type = fixCppTypeName(return_type); | |
3143 // return_type = fixCppTypeName(java_type->typeEntry()->qualifiedCppName()); | |
3144 } | |
3145 /* if( (java_type->isValue() && !java_type->typeEntry()->isStructInD()) | |
3146 || java_type->isObject() ) | |
3147 s << INDENT << return_type << " *" << java_name << " = (" << return_type << "*) "; | |
3148 else*/ | |
3149 s << INDENT << return_type << " " << java_name << " = (" << return_type << ") "; | |
3150 | |
3151 if (java_type->isQObject()) { | |
3152 /* qtd s << "qtjambi_from_qobject(__jni_env, " << "(QObject *) "; | |
3153 | |
3154 if (java_type->isReference() && java_type->indirections() == 0) | |
3155 s << "&"; | |
3156 | |
3157 s << qt_name | |
3158 << ", \"" << java_type->typeEntry()->lookupName() << "\"" | |
3159 << ", \"" << java_type->package().replace(".", "/") << "/\"" | |
3160 << ");" << endl; | |
3161 */ | |
3162 s << qt_name << ";" << endl; | |
3163 | |
3164 #if 0 | |
3165 } else if (java_type->isEnum()) { | |
3166 | |
3167 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
3168 s << "qtjambi_from_enum(__jni_env, " << qt_name << ", \"" | |
3169 << et->javaQualifier() << "$" << et->targetLangName() << "\");" << endl; | |
3170 #endif | |
3171 } else if (java_type->isNativePointer()) { | |
3172 /* qtd s << "qtjambi_from_cpointer(__jni_env, "; | |
3173 if (java_type->isReference()) | |
3174 s << "&"; | |
3175 s << qt_name << ", " << nativePointerType(java_type) << ", " | |
3176 << java_type->actualIndirections() << ");" << endl; | |
3177 */ | |
3178 if (java_type->isReference()) | |
3179 s << "&"; | |
3180 s << qt_name << ";" << "// qtjambi_from_cpointer" << endl; | |
3181 } else if (java_type->isValue()) { | |
3182 // qtd s << fromObject(java_type->typeEntry(), "&" + qt_name) << endl; | |
3183 s << "new " << java_type->typeEntry()->qualifiedCppName() << "(" << qt_name << ");" << endl; | |
3184 } else { | |
3185 // qtd s << fromObject(java_type->typeEntry(), | |
3186 // qtd (java_type->isReference() ? "&" : "") + qt_name) << endl; | |
3187 s << qt_name << ";" << endl; | |
3188 } | |
3189 } | |
3190 | |
3191 } | |
3192 | |
3193 QString CppImplGenerator::getTypeName(const TypeEntry *entry, Option option) | |
3194 { | |
3195 if(entry->isEnum() && (option & EnumAsInts)) | |
3196 return "int"; | |
3197 | |
3198 return entry->lookupName(); | |
3199 } | |
3200 | |
3201 void CppImplGenerator::writeQtToJavaContainer(QTextStream &s, | |
3202 const AbstractMetaType *java_type, | |
3203 const QString &qt_name, | |
3204 const QString &java_name, | |
3205 const AbstractMetaFunction *java_function, | |
3206 int argument_index) | |
3207 { | |
3208 // Language for conversion to Java: Native code for return values and Shell code for arguments | |
3209 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode; | |
3210 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3211 return; | |
3212 | |
3213 if (java_type == 0) { | |
3214 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3215 "language '%4'") | |
3216 .arg(argument_index) | |
3217 .arg(java_function->implementingClass()->name()) | |
3218 .arg(java_function->name()) | |
3219 .arg(int(lang)); | |
3220 ReportHandler::warning(warn); | |
3221 return; | |
3222 } | |
3223 | |
3224 Q_ASSERT(java_type->isContainer()); | |
3225 const ContainerTypeEntry *type = | |
3226 static_cast<const ContainerTypeEntry *>(java_type->typeEntry()); | |
3227 | |
3228 if (type->type() == ContainerTypeEntry::ListContainer | |
3229 || type->type() == ContainerTypeEntry::VectorContainer | |
3230 || type->type() == ContainerTypeEntry::StringListContainer | |
3231 || type->type() == ContainerTypeEntry::LinkedListContainer | |
3232 || type->type() == ContainerTypeEntry::StackContainer | |
3233 || type->type() == ContainerTypeEntry::SetContainer | |
3234 || type->type() == ContainerTypeEntry::QueueContainer) { | |
3235 | |
3236 Q_ASSERT(java_type->instantiations().size() == 1); | |
3237 AbstractMetaType *targ = java_type->instantiations().first(); | |
3238 | |
3239 QString cls_name = getTypeName(targ->typeEntry(), EnumAsInts); | |
3240 cls_name.remove("_ConcreteWrapper"); | |
3241 | |
3242 s << endl | |
39 | 3243 << INDENT << "{" << endl // qtd2 hack, additional scope for avoiding duplicating of "i" |
1 | 3244 << INDENT; |
3245 | |
3246 switch (type->type()) { | |
3247 case ContainerTypeEntry::LinkedListContainer: | |
3248 case ContainerTypeEntry::QueueContainer: | |
3249 s << "qtjambi_linkedlist_new(__jni_env)"; | |
3250 break; | |
3251 case ContainerTypeEntry::StackContainer: | |
3252 s << "qtjambi_stack_new(__jni_env)"; | |
3253 break; | |
3254 case ContainerTypeEntry::SetContainer: | |
3255 s << "qtjambi_hashset_new(__jni_env)"; | |
3256 break; | |
3257 default: | |
3258 s << "qtd_allocate_" << cls_name | |
3259 << "_array(" << java_name << ", " << qt_name << ".size())"; | |
3260 break; | |
3261 } | |
3262 | |
3263 s << ";" << endl | |
3264 << INDENT; | |
3265 | |
3266 | |
3267 writeTypeInfo(s, java_type, ForceValueType); | |
3268 s << "::const_iterator " << qt_name << "_end_it = " << qt_name << ".constEnd();" << endl | |
3269 << INDENT << "int i = 0;" << endl | |
3270 << INDENT; | |
3271 s << "for ("; | |
3272 writeTypeInfo(s, java_type, ForceValueType); | |
3273 s << "::const_iterator " << qt_name << "_it = " << qt_name << ".constBegin(); " | |
3274 << qt_name << "_it != " << qt_name << "_end_it; ++" << qt_name << "_it) {" << endl; | |
3275 { | |
3276 Indentation indent(INDENT); | |
3277 s << INDENT; | |
3278 writeTypeInfo(s, targ); | |
3279 s << " __qt_tmp = *" << qt_name << "_it;" << endl; | |
3280 | |
3281 if(targ->isTargetLangString()) | |
3282 s << INDENT << "void *__java_tmp = qtd_string_from_array(" << java_name << ", i);" << endl; | |
3283 | |
3284 writeQtToJava(s, targ, "__qt_tmp", "__java_tmp", 0, -1, BoxedPrimitive); | |
3285 | |
3286 s << INDENT << "qtd_assign_" << cls_name << "_array_element(" << java_name << ", i, __java_tmp);" << endl; | |
3287 s << INDENT << "++i;" << endl; | |
3288 } | |
39 | 3289 s << INDENT << "}" << endl |
3290 << INDENT << "}" << endl; | |
1 | 3291 |
3292 } else if (type->type() == ContainerTypeEntry::PairContainer) { | |
3293 QList<AbstractMetaType *> args = java_type->instantiations(); | |
3294 Q_ASSERT(args.size() == 2); | |
3295 | |
3296 s << INDENT << "jobject " << java_name << ";" << endl | |
3297 << INDENT << "{" << endl; | |
3298 { | |
3299 Indentation indent(INDENT); | |
3300 writeQtToJava(s, args.at(0), qt_name + ".first", "__java_tmp_first", 0, -1, BoxedPrimitive); | |
3301 writeQtToJava(s, args.at(1), qt_name + ".second", "__java_tmp_second", 0, -1, BoxedPrimitive); | |
3302 s << INDENT << java_name << " = qtjambi_pair_new(__jni_env, " | |
3303 << "__java_tmp_first, __java_tmp_second);" << endl; | |
3304 } | |
3305 | |
3306 s << INDENT << "}" << endl; | |
3307 | |
3308 } else if (type->type() == ContainerTypeEntry::MultiMapContainer) { | |
3309 | |
3310 Q_ASSERT(java_type->instantiations().size() == 2); | |
3311 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3312 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3313 | |
3314 s << endl | |
3315 << INDENT << "jobject " << java_name << " = qtjambi_treemap_new(__jni_env, " << qt_name << ".keys().size());" << endl | |
3316 << INDENT << "QList<"; | |
3317 writeTypeInfo(s, targ_key); | |
3318 s << "> __qt_keys = " << qt_name << ".keys();" << endl | |
3319 << INDENT << "for (int i=0; i<__qt_keys.size(); ++i) {" << endl; | |
3320 { | |
3321 Indentation indent(INDENT); | |
3322 | |
3323 s << INDENT; | |
3324 writeTypeInfo(s, targ_key); | |
3325 s << " __qt_tmp_key = __qt_keys.at(i);" << endl; | |
3326 writeQtToJava(s, targ_key, "__qt_tmp_key", "__java_tmp_key", 0, -1, BoxedPrimitive); | |
3327 | |
3328 s << INDENT << "QList<"; | |
3329 writeTypeInfo(s, targ_val); | |
3330 s << "> __qt_values = " << qt_name << ".values(__qt_tmp_key);" << endl | |
3331 << INDENT << "jobject __java_value_list = qtjambi_arraylist_new(__jni_env, __qt_values.size());" << endl | |
3332 << INDENT << "for (int j=0; j<__qt_values.size(); ++j) {" << endl; | |
3333 { | |
3334 Indentation indent(INDENT); | |
3335 | |
3336 s << INDENT; | |
3337 writeTypeInfo(s, targ_val); | |
3338 s << " __qt_tmp_val = __qt_values.at(j);" << endl; | |
3339 writeQtToJava(s, targ_val, "__qt_tmp_val", "__java_tmp_val", 0, -1, BoxedPrimitive); | |
3340 | |
3341 s << INDENT << "qtjambi_collection_add(__jni_env, __java_value_list, __java_tmp_val);" << endl; | |
3342 } | |
3343 s << INDENT << "}" << endl | |
3344 << INDENT << "qtjambi_map_put(__jni_env, " << java_name << ", __java_tmp_key, __java_value_list);" << endl; | |
3345 } | |
3346 s << INDENT << "}" << endl; | |
3347 | |
3348 } else if (type->type() == ContainerTypeEntry::MapContainer | |
3349 || type->type() == ContainerTypeEntry::HashContainer) { | |
3350 QString constructor = type->type() == ContainerTypeEntry::MapContainer | |
3351 ? "qtjambi_treemap_new" | |
3352 : "qtjambi_hashmap_new"; | |
3353 | |
3354 Q_ASSERT(java_type->instantiations().size() == 2); | |
3355 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3356 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3357 | |
3358 s << endl | |
3359 << INDENT << "jobject " << java_name << " = " << constructor << "(__jni_env, " << qt_name | |
3360 << ".size());" << endl | |
3361 << INDENT; | |
3362 writeTypeInfo(s, java_type, Option(ExcludeReference | ExcludeConst)); | |
3363 s << "::const_iterator it;" << endl | |
3364 << INDENT << "for (it=" << qt_name << ".constBegin(); it!=" << qt_name << ".constEnd(); ++it) {" << endl; | |
3365 { | |
3366 Indentation indent(INDENT); | |
3367 s << INDENT; | |
3368 writeTypeInfo(s, targ_key); | |
3369 s << " __qt_tmp_key = it.key();" << endl | |
3370 << INDENT; | |
3371 writeTypeInfo(s, targ_val); | |
3372 s << " __qt_tmp_val = it.value();" << endl; | |
3373 writeQtToJava(s, targ_key, "__qt_tmp_key", "__java_tmp_key", 0, -1, BoxedPrimitive); | |
3374 writeQtToJava(s, targ_val, "__qt_tmp_val", "__java_tmp_val", 0, -1, BoxedPrimitive); | |
3375 s << INDENT << "qtjambi_map_put(__jni_env, " << java_name | |
3376 << ", __java_tmp_key, __java_tmp_val);" << endl; | |
3377 } | |
3378 s << INDENT << "}" << endl; | |
3379 | |
3380 } else { | |
3381 ReportHandler::warning(QString("unable to generate container type %1, type=%2") | |
3382 .arg(java_type->name()).arg(type->type())); | |
3383 } | |
3384 | |
3385 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl; | |
3386 } | |
3387 | |
3388 | |
3389 void CppImplGenerator::writeJavaToQtContainer(QTextStream &s, | |
3390 const AbstractMetaType *java_type, | |
3391 const QString &qt_name, | |
3392 const QString &java_name, | |
3393 const AbstractMetaFunction *java_function, | |
3394 int argument_index) | |
3395 { | |
3396 // Conversion to C++: Shell code for return value, native code for arguments | |
3397 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
3398 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3399 return; | |
3400 | |
3401 if (java_type == 0) { | |
3402 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3403 "language '%4'") | |
3404 .arg(argument_index) | |
3405 .arg(java_function->implementingClass()->name()) | |
3406 .arg(java_function->name()) | |
3407 .arg(int(lang)); | |
3408 ReportHandler::warning(warn); | |
3409 return; | |
3410 } | |
3411 | |
3412 | |
3413 Q_ASSERT(java_type->isContainer()); | |
3414 const ContainerTypeEntry *type = | |
3415 static_cast<const ContainerTypeEntry *>(java_type->typeEntry()); | |
3416 | |
3417 if (type->type() == ContainerTypeEntry::ListContainer | |
3418 || type->type() == ContainerTypeEntry::VectorContainer | |
3419 || type->type() == ContainerTypeEntry::StringListContainer | |
3420 || type->type() == ContainerTypeEntry::LinkedListContainer | |
3421 || type->type() == ContainerTypeEntry::StackContainer | |
3422 || type->type() == ContainerTypeEntry::SetContainer | |
3423 || type->type() == ContainerTypeEntry::QueueContainer) { | |
3424 Q_ASSERT(java_type->instantiations().size() == 1); | |
3425 AbstractMetaType *targ = java_type->instantiations().first(); | |
3426 QString elem_type = getTypeName(targ->typeEntry(), EnumAsInts); | |
3427 elem_type.remove("_ConcreteWrapper"); | |
3428 | |
3429 s << INDENT; | |
3430 writeTypeInfo(s, java_type, ForceValueType); | |
3431 s << qt_name << ";" << endl; | |
3432 | |
3433 // qtd s << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3434 { | |
3435 /* qtd Indentation indent(INDENT); | |
3436 s << INDENT << "jobjectArray __qt__array = qtjambi_collection_toArray(__jni_env, " | |
3437 << java_name << ");" << endl | |
3438 << INDENT << "jsize __qt__size = __jni_env->GetArrayLength(__qt__array);" << endl; | |
3439 */ | |
3440 if (type->type() == ContainerTypeEntry::VectorContainer | |
3441 || type->type() == ContainerTypeEntry::StackContainer) | |
3442 s << INDENT << qt_name << ".reserve(" << java_name << "_size);" << endl; | |
3443 | |
3444 s << INDENT << "for (int i=0; i<" << java_name << "_size; ++i) {" << endl; | |
3445 { | |
3446 Indentation indent(INDENT); | |
3447 if(targ->isTargetLangString()) | |
35 | 3448 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
|
3449 << INDENT << "qtd_get_string_from_array(" << java_name << ", i, &__d_element);" << endl; |
1 | 3450 else { |
3451 s << INDENT; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3452 writeTypeInfo(s, targ, Option(VirtualDispatch | ForcePointer | EnumAsInts)); |
1 | 3453 QString cast_string = ""; |
3454 const TypeEntry* centry = targ->typeEntry(); | |
3455 if (centry->isComplex() && (centry->isObject() || centry->isValue() || centry->isInterface())) | |
3456 cast_string = "(void**)"; | |
3457 s << "__d_element;" << endl | |
3458 << INDENT << "qtd_get_" << elem_type << "_from_array(" << java_name << ", i, &__d_element);" << endl; | |
3459 } | |
3460 /* qtd s << INDENT << "jobject __d_element = " | |
3461 << "__jni_env->GetObjectArrayElement(__qt__array, i);" << endl;*/ | |
3462 writeJavaToQt(s, targ, "__qt_element", "__d_element", 0, -1, BoxedPrimitive); | |
3463 QString cont_element = "__qt_element"; | |
3464 if(targ->typeEntry()->isStructInD() && targ->name() != "QModelIndex") | |
3465 cont_element = "__d_element"; | |
3466 s << INDENT << qt_name << " << " << cont_element << ";" << endl; | |
3467 } | |
3468 // qtd s << INDENT << "}" << endl; | |
3469 } | |
3470 s << INDENT << "}" << endl; | |
3471 } else if (type->type() == ContainerTypeEntry::PairContainer) { | |
3472 QList<AbstractMetaType *> targs = java_type->instantiations(); | |
3473 Q_ASSERT(targs.size() == 2); | |
3474 | |
3475 s << INDENT; | |
3476 writeTypeInfo(s, java_type, ForceValueType); | |
3477 s << " " << qt_name << ";" << endl | |
3478 << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3479 { | |
3480 // separate scope required just in case function takes two QPair's. | |
3481 Indentation indent(INDENT); | |
3482 s << INDENT << "jobject __java_first = qtjambi_pair_get(__jni_env, " | |
3483 << java_name << ", 0);" << endl; | |
3484 writeJavaToQt(s, targs.at(0), "__qt_first", "__java_first", 0, -1, BoxedPrimitive); | |
3485 | |
3486 s << INDENT << "jobject __java_second = qtjambi_pair_get(__jni_env, " | |
3487 << java_name << ", 1);" << endl; | |
3488 writeJavaToQt(s, targs.at(1), "__qt_second", "__java_second", 0, -1, BoxedPrimitive); | |
3489 | |
3490 s << INDENT << qt_name << ".first = __qt_first;" << endl | |
3491 << INDENT << qt_name << ".second = __qt_second;" << endl; | |
3492 } | |
3493 s << INDENT << "}" << endl; | |
3494 } else if (type->type() == ContainerTypeEntry::MapContainer | |
3495 || type->type() == ContainerTypeEntry::HashContainer) { | |
3496 Q_ASSERT(java_type->instantiations().size() == 2); | |
3497 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3498 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3499 | |
3500 s << INDENT; | |
3501 writeTypeInfo(s, java_type, ForceValueType); | |
3502 s << qt_name << ";" << endl; | |
3503 s << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3504 { | |
3505 Indentation indent(INDENT); | |
3506 s << INDENT << "int __qt_list_size = qtjambi_map_size(__jni_env, " << java_name | |
3507 << ");" << endl | |
3508 << INDENT | |
3509 << "jobjectArray __java_entry_set = qtjambi_map_entryset_array(__jni_env, " << java_name | |
3510 << ");" << endl; | |
3511 | |
3512 s << INDENT << "for (int i=0; i<__qt_list_size; ++i) {" << endl; | |
3513 { | |
3514 Indentation indent(INDENT); | |
3515 s << INDENT | |
3516 << "QPair<jobject, jobject> __java_entry = " | |
3517 << "qtjambi_entryset_array_get(__jni_env, __java_entry_set, i);" | |
3518 << endl | |
3519 << INDENT << "jobject __java_key = __java_entry.first;" << endl | |
3520 << INDENT << "jobject __java_val = __java_entry.second;" << endl; | |
3521 writeJavaToQt(s, targ_key, "__qt_key", "__java_key", 0, -1, BoxedPrimitive); | |
3522 writeJavaToQt(s, targ_val, "__qt_val", "__java_val", 0, -1, BoxedPrimitive); | |
3523 s << INDENT << qt_name << ".insert(__qt_key, __qt_val);" << endl; | |
3524 } | |
3525 s << INDENT << "}" << endl; | |
3526 } | |
3527 s << INDENT << "}" << endl; | |
3528 | |
3529 } else { | |
3530 ReportHandler::warning(QString("unable to generate container type %1, %2") | |
3531 .arg(java_type->name()).arg(type->type())); | |
3532 } | |
3533 | |
3534 } | |
3535 | |
3536 | |
3537 void CppImplGenerator::writeFunctionCall(QTextStream &s, const QString &object_name, | |
3538 const AbstractMetaFunction *java_function, | |
3539 const QString &prefix, | |
3540 Option option, | |
3541 const QStringList &extra_arguments) | |
3542 { | |
3543 QString function_name = option & OriginalName ? java_function->originalName() : java_function->name(); | |
3544 | |
3545 AbstractMetaClassList interfaces = java_function->implementingClass()->interfaces(); | |
3546 | |
3547 QString classPrefix; | |
3548 if (prefix.isEmpty() | |
3549 && !java_function->implementingClass()->interfaces().isEmpty() | |
3550 && !java_function->implementingClass()->inheritsFrom(java_function->declaringClass())) { | |
3551 classPrefix = java_function->declaringClass()->qualifiedCppName() + "::"; | |
3552 } | |
3553 | |
3554 if (java_function->isInGlobalScope()) { | |
3555 | |
3556 // Global scope stream operators need the arguments to be reordered (this ref at end) | |
3557 // so we special case them in order to simplify this code | |
3558 bool stream_operator = java_function->originalName() == "operator<<" | |
3559 || java_function->originalName() == "operator>>"; | |
3560 | |
3561 if (java_function->type() == 0) | |
3562 s << "if (" << object_name << " != 0) "; | |
3563 else | |
3564 s << "(" << object_name << " != 0) ? "; | |
3565 s << "::" << prefix << function_name << "("; | |
3566 if (!stream_operator) | |
3567 s << "*" << object_name << ", "; | |
3568 writeFunctionCallArguments(s, java_function, "__qt_"); | |
3569 if (stream_operator) | |
3570 s << ", *" << object_name; | |
3571 s << ")"; | |
3572 if (java_function->type() != 0) | |
3573 s << " : " << default_return_statement_qt(java_function->type(), Generator::Option(option | Generator::NoReturnStatement)); | |
3574 s << ";"; | |
3575 } else { | |
3576 s << object_name << (java_function->isStatic() ? QLatin1String("::") : QLatin1String("->") + classPrefix) | |
3577 << prefix << function_name << "("; | |
3578 writeFunctionCallArguments(s, java_function, "__qt_"); | |
3579 | |
3580 // The extra arguments... | |
3581 for (int i=0; i<extra_arguments.size(); ++i) { | |
3582 if (i > 0 || java_function->arguments().size() != 0) | |
3583 s << ", "; | |
3584 s << extra_arguments.at(i); | |
3585 } | |
3586 | |
3587 s << ");"; | |
3588 } | |
3589 | |
3590 s << endl; | |
3591 | |
3592 } | |
3593 | |
3594 | |
3595 void CppImplGenerator::writeFunctionCallArguments(QTextStream &s, | |
3596 const AbstractMetaFunction *java_function, | |
3597 const QString &prefix, | |
3598 Option options) | |
3599 { | |
3600 AbstractMetaArgumentList arguments = java_function->arguments(); | |
3601 | |
3602 int written_arguments = 0; | |
3603 const AbstractMetaClass *cls = java_function->ownerClass(); | |
3604 if (java_function->isConstructor() && cls->hasVirtualFunctions()) { | |
3605 s << "d_ptr"; | |
3606 written_arguments++; | |
3607 } | |
3608 for (int i=0; i<arguments.size(); ++i) { | |
3609 const AbstractMetaArgument *argument = arguments.at(i); | |
3610 AbstractMetaType *a_type = argument->type(); | |
3611 | |
3612 if ((options & SkipRemovedArguments) == SkipRemovedArguments | |
3613 && java_function->argumentRemoved(i+1)) { | |
3614 continue; | |
3615 } | |
3616 | |
3617 if (written_arguments++ > 0) { | |
3618 s << ", "; | |
3619 } | |
3620 | |
3621 bool enum_as_int = (options & EnumAsInts) && (argument->type()->typeEntry()->isEnum() | |
3622 || argument->type()->typeEntry()->isFlags()); | |
3623 if (a_type->isEnum()) { | |
3624 AbstractMetaEnum *java_enum = | |
3625 m_classes.findEnum(static_cast<const EnumTypeEntry *>(argument->type()->typeEntry())); | |
3626 if (java_enum == 0) { | |
3627 ReportHandler::warning(QString("enum not found: '%1'") | |
3628 .arg(argument->type()->typeEntry()->qualifiedCppName())); | |
3629 } else { | |
3630 enum_as_int |= !java_enum->isPublic(); | |
3631 } | |
3632 } | |
3633 | |
3634 if ( (options & VirtualDispatch) | |
3635 && a_type->isContainer()) { | |
3636 s << "__d_" << argument->indexedName(); | |
3637 continue; | |
3638 } | |
3639 | |
3640 if ((!(options & NoCasts) && !enum_as_int) || ((options & ForceEnumCast) && a_type->isEnum())) { | |
3641 | |
3642 // If the type in the signature is specified without template instantiation, but the | |
3643 // class is actually a template class, then we have troubles. | |
3644 AbstractMetaClass *cls = classes().findClass(argument->type()->typeEntry()->qualifiedCppName()); | |
3645 | |
3646 if ( (options & VirtualDispatch) && !a_type->typeEntry()->isStructInD() | |
3647 && ( a_type->isValue() | |
3648 || (a_type->isReference() && (a_type->typeEntry()->isValue() || a_type->typeEntry()->isObject() || a_type->typeEntry()->isPrimitive()) && !a_type->isTargetLangString()) | |
3649 ) ) | |
3650 s << "&"; | |
3651 | |
3652 if( (options & VirtualDispatch) && a_type->typeEntry()->isStructInD() && a_type->isReference()) | |
3653 s << "&"; | |
3654 | |
3655 if ( (options & VirtualDispatch) | |
3656 && ( a_type->isTargetLangString() || a_type->name() == "QModelIndex" ) ) | |
3657 ; | |
3658 else if (cls == 0 || cls->templateArguments().size() == a_type->instantiations().size()) { | |
3659 s << "("; | |
3660 writeTypeInfo(s, a_type, options); | |
3661 s << ")"; | |
3662 } | |
3663 } | |
3664 | |
3665 if (a_type->isNativePointer() && a_type->typeEntry()->name() == "QString" && !a_type->isReference()) | |
3666 s << "&"; | |
3667 | |
3668 if ( ( !a_type->isPrimitive() | |
3669 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty() ) | |
3670 && (!a_type->typeEntry()->isStructInD() || a_type->name() == "QModelIndex") ) { | |
3671 s << prefix; | |
3672 } | |
3673 | |
3674 if (options & VirtualDispatch) { | |
3675 if( argument->type()->isTargetLangString()) | |
3676 s << argument->indexedName() << ".utf16(), " << argument->indexedName() << ".size()"; | |
3677 else if (argument->type()->name() == "QModelIndex") | |
3678 s << "qtd_from_QModelIndex(" << argument->indexedName() << ")"; | |
3679 else | |
3680 s << argument->indexedName(); | |
3681 } else | |
3682 s << argument->indexedName(); | |
3683 } | |
3684 } | |
3685 | |
3686 | |
3687 QString CppImplGenerator::translateType(const AbstractMetaType *java_type, Option option, bool d_export) | |
3688 { | |
3689 if (!java_type) | |
3690 return "void"; | |
3691 | |
3692 const TypeEntry *type = java_type->typeEntry(); | |
3693 QString class_name = type->name(); | |
3694 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName()); | |
3695 QString d_name = qualified_class_name; | |
3696 if (d_export) | |
3697 d_name = type->lookupName(); | |
3698 | |
3699 if (java_type->isPrimitive() | |
3700 || java_type->isTargetLangString() | |
3701 || java_type->isVariant() | |
3702 || java_type->isJObjectWrapper() | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3703 || java_type->isTargetLangChar()) { |
1 | 3704 return d_name; |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3705 } else if (java_type->isArray()) { |
39 | 3706 if (d_export) |
3707 return java_type->arrayElementType()->name() + "*"; | |
3708 else | |
3709 return java_type->arrayElementType()->typeEntry()->qualifiedCppName() + "*"; | |
1 | 3710 } 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
|
3711 || ((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
|
3712 return "int"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3713 } else if (java_type->isReference()) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3714 if (java_type->typeEntry()->isValue()) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3715 return "void*"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3716 else |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3717 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
|
3718 } else if (java_type->isNativePointer()) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3719 if (java_type->typeEntry()->isValue()) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3720 return "void*"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3721 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
|
3722 return "int" + QString(java_type->indirections(), '*'); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3723 else |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3724 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
|
3725 } else { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3726 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
|
3727 } |
81 | 3728 } |
1 | 3729 |
3730 void CppImplGenerator::writeExtraIncludes(QTextStream &s, const AbstractMetaClass *java_class) | |
3731 { | |
3732 IncludeList includes = java_class->typeEntry()->extraIncludes(); | |
3733 qSort(includes.begin(), includes.end()); | |
3734 | |
3735 int used = 0; | |
3736 foreach (const Include &i, includes) { | |
3737 if (i.type != Include::TargetLangImport) { | |
3738 s << i.toString() << endl; | |
3739 ++used; | |
3740 } | |
3741 } | |
3742 | |
3743 | |
3744 if (used) | |
3745 s << endl; | |
3746 | |
3747 } | |
3748 | |
3749 | |
3750 void CppImplGenerator::writeDefaultConstructedValues_helper(QSet<QString> &values, | |
3751 const AbstractMetaFunction *func) | |
3752 { | |
3753 foreach (AbstractMetaArgument *arg, func->arguments()) { | |
3754 AbstractMetaType *type = arg->type(); | |
3755 if (func->typeReplaced(arg->argumentIndex()+1).isEmpty() && type->isValue() && hasDefaultConstructor(type)) | |
3756 values << type->typeEntry()->qualifiedCppName(); | |
3757 } | |
3758 } | |
3759 | |
3760 | |
3761 void CppImplGenerator::writeDefaultConstructedValues(QTextStream &s, const AbstractMetaClass *java_class) { | |
3762 | |
3763 QSet<QString> values; | |
3764 | |
3765 // Class functions, more or less copied from the logic in write(Class) above... | |
3766 AbstractMetaFunctionList class_funcs; | |
3767 | |
3768 // Add normal final functions | |
3769 foreach (AbstractMetaFunction *function, java_class->functionsInTargetLang()) { | |
3770 if (!function->isEmptyFunction()) | |
3771 class_funcs << function; | |
3772 } | |
3773 | |
3774 // Add abstract functions, I think... | |
3775 foreach (AbstractMetaFunction *function, java_class->queryFunctions(AbstractMetaClass::NormalFunctions | |
3776 | AbstractMetaClass::AbstractFunctions | |
3777 | AbstractMetaClass::NotRemovedFromTargetLang)) { | |
3778 if (function->implementingClass() != java_class) | |
3779 class_funcs << function; | |
3780 } | |
3781 | |
3782 // Signals (their c++ wrapper calls actually...) | |
3783 class_funcs += java_class->queryFunctions(AbstractMetaClass::Signals); | |
3784 | |
3785 // | |
3786 foreach (AbstractMetaFunction *f, class_funcs) { | |
3787 writeDefaultConstructedValues_helper(values, f); | |
3788 } | |
3789 | |
3790 foreach (AbstractMetaField *field, java_class->fields()) { | |
3791 writeDefaultConstructedValues_helper(values, field->setter()); | |
3792 } | |
3793 | |
3794 if (!values.isEmpty()) { | |
3795 s << endl << endl | |
3796 << "// Default constructed values used throughout final functions..." << endl; | |
3797 for (QSet<QString>::const_iterator it = values.constBegin(); it != values.constEnd(); ++it) { | |
3798 s << "Q_GLOBAL_STATIC(" << *it << ", default_" << QString(*it).replace("::", "_") | |
3799 << ");" << endl; | |
3800 } | |
3801 s << endl << endl; | |
3802 } | |
3803 } |