Mercurial > projects > qtd
annotate generator/cppimplgenerator.cpp @ 190:a1b48a630f73
D2 support, simple stuff works now
author | eldar |
---|---|
date | Sun, 12 Jul 2009 20:51:37 +0000 |
parents | 7dd099050621 |
children | 1349940724eb |
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 | |
148
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
132
diff
changeset
|
750 << "pf" << f_name << " " << f_name << ";"; |
1 | 751 } |
752 | |
753 s << endl; | |
754 } | |
755 | |
756 void CppImplGenerator::writeShellVirtualFunction(QTextStream &s, const AbstractMetaFunction *function, | |
757 const AbstractMetaClass *implementor, int id) | |
758 { | |
759 // ---------------------------- | |
760 if(notWrappedYet(function)) | |
761 return; | |
762 | |
763 AbstractMetaType *f_type = function->type(); | |
764 QString new_return_type = function->typeReplaced(0); | |
765 bool has_function_type = ((f_type != 0 | |
766 || !new_return_type.isEmpty()) | |
767 && new_return_type != "void"); | |
768 | |
769 writeVirtualDispatchFunction(s, function); | |
770 | |
771 writeFunctionSignature(s, function, implementor, QString(), OriginalName); | |
772 | |
773 s << endl | |
774 << "{" << endl; | |
775 | |
776 Option options = NoOption; | |
777 | |
778 Indentation indent(INDENT); | |
779 //bool static_call = !(options & VirtualCall); | |
780 //s << "std::cout << \"entering " << function->marshalledName() << " \\n\"; " << endl; | |
781 | |
782 if (f_type) { | |
783 if (f_type->isTargetLangString()) | |
104 | 784 s << INDENT << "DArray ret_str;" << endl; |
81 | 785 |
1 | 786 if (f_type->name() == "QModelIndex") |
787 s << INDENT << "QModelIndexAccessor __d_return_value;" << endl; | |
81 | 788 else if (f_type->typeEntry()->isStructInD()) |
789 s << INDENT << f_type->typeEntry()->qualifiedCppName() << " __d_return_value;" << endl; | |
790 | |
1 | 791 if (f_type->isContainer()) |
792 s << INDENT << "void* __d_return_value;" << endl | |
793 << INDENT << "size_t __d_return_value_size;" << endl; | |
794 } | |
795 | |
796 AbstractMetaArgumentList arguments = function->arguments(); | |
797 foreach (AbstractMetaArgument *argument, arguments) { | |
798 if (!function->argumentRemoved(argument->argumentIndex()+1)) { | |
799 if (!argument->type()->isPrimitive() | |
800 || !function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
801 if(argument->type()->isContainer()) { | |
802 QString arg_name = argument->indexedName(); | |
803 s << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl | |
804 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name); | |
805 | |
806 writeQtToJava(s, | |
807 argument->type(), | |
808 arg_name, | |
809 "__d_" + arg_name, | |
810 function, | |
811 argument->argumentIndex() + 1, | |
812 Option(VirtualDispatch)); | |
813 } | |
814 } | |
815 } | |
816 } | |
817 | |
818 if ((options & NoReturnStatement) == 0) | |
819 s << INDENT; | |
820 /* qtd if (function->isAbstract() && static_call) { | |
821 s << default_return_statement_qt(function->type(), options) << ";" << endl; | |
822 } else */ | |
823 { | |
824 if (f_type) { | |
825 if (f_type->isTargetLangString() || f_type->isContainer()) | |
826 ; | |
827 else if ((f_type->isValue() && !f_type->typeEntry()->isStructInD()) || | |
828 f_type->isVariant() ) | |
829 s << f_type->name() << " *__qt_return_value = (" << f_type->name() << "*) "; | |
830 else if (f_type->isObject() || f_type->isQObject()) | |
831 s << "return (" << f_type->name() <<"*) "; | |
81 | 832 else if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 833 ; |
834 else if ((options & NoReturnStatement) == 0) | |
835 s << "return "; | |
836 | |
837 if (f_type->isEnum() || f_type->isFlags()) | |
838 s << "(" << f_type->typeEntry()->qualifiedCppName() <<") "; | |
839 } | |
840 | |
841 s << function->marshalledName() << "_dispatch(" | |
842 << "this->d_entity()"; | |
843 | |
844 if (f_type) { | |
845 if (f_type->isTargetLangString()) | |
104 | 846 s << ", &ret_str"; |
81 | 847 if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 848 s << ", &__d_return_value"; |
849 if (f_type->isContainer()) | |
850 s << ", &__d_return_value, &__d_return_value_size"; | |
851 } | |
852 | |
853 if (function->arguments().size() > 0) | |
854 s << ", "; | |
855 writeFunctionCallArguments(s, function, QString(), Option(options | ForceEnumCast | SkipRemovedArguments | ExcludeConst | VirtualDispatch)); | |
856 | |
857 s << ");" << endl; | |
858 | |
859 //s << "std::cout << \"leaving " << function->marshalledName() << " \\n\"; " << endl; | |
860 | |
861 if (f_type) { | |
862 if (f_type->name() == "QModelIndex") { | |
863 s << INDENT << "QModelIndex __qt_return_value = qtd_to_QModelIndex( __d_return_value );" << endl; | |
864 #ifdef Q_OS_WIN32 | |
865 s << "__qtd_dummy();" << endl; // hack!!! | |
866 #endif | |
867 s << INDENT << "return __qt_return_value;" << endl; | |
81 | 868 } else if (f_type->typeEntry()->isStructInD()) |
869 s << INDENT << "return __d_return_value;" << endl; | |
1 | 870 |
871 if (f_type->isContainer()) { | |
872 writeJavaToQt(s, f_type, "__qt_return_value", "__d_return_value", | |
873 function, 0, GlobalRefJObject); | |
874 s << INDENT << "return __qt_return_value;" << endl; | |
875 } | |
876 | |
877 if (f_type->isTargetLangString()) | |
104 | 878 s << INDENT << "return " << "QString::fromUtf8((const char*) ret_str.ptr, ret_str.length);" << endl; |
1 | 879 |
880 if ( ( f_type->isValue() && !f_type->typeEntry()->isStructInD() ) || f_type->isVariant() ) | |
881 s << INDENT << "return " << f_type->name() << "(*__qt_return_value);" << endl; //" __qt_return_value = "; | |
882 } | |
883 } | |
884 | |
885 s << "}" << endl << endl; | |
886 // ---------------------------- | |
887 } | |
888 | |
889 void CppImplGenerator::writeVirtualDispatchArguments(QTextStream &s, const AbstractMetaFunction *d_function, bool d_export) | |
890 { | |
891 uint nativeArgCount = 0; | |
892 AbstractMetaType *ret_type = d_function->type(); | |
893 | |
894 s << "(void *d_entity"; | |
895 | |
896 if (ret_type) { | |
104 | 897 if (ret_type->isTargetLangString()) { |
898 if (d_export) | |
899 s << ", string* ret_str"; | |
900 else | |
901 s << ", DArray* ret_str"; | |
902 } | |
1 | 903 if (ret_type->name() == "QModelIndex") |
904 s << ", QModelIndexAccessor *__d_return_value"; | |
81 | 905 else if(ret_type->typeEntry()->isStructInD()) |
906 s << ", " << ret_type->typeEntry()->qualifiedCppName() << " *__d_return_value"; | |
907 | |
1 | 908 if (ret_type->isContainer()) |
909 s << ", void** __d_arr_ptr, size_t* __d_arr_size"; | |
910 } | |
911 | |
912 if (d_function->arguments().size() > 0) | |
913 s << ", "; | |
914 | |
915 | |
916 // the function arguments | |
917 AbstractMetaArgumentList arguments = d_function->arguments(); | |
918 foreach (const AbstractMetaArgument *argument, arguments) { | |
919 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
920 if (nativeArgCount > 0) | |
921 s << "," << " "; | |
922 | |
923 AbstractMetaType *d_type = argument->type(); | |
924 QString arg_name = argument->indexedName(); | |
925 | |
926 if (d_type->name() == "QModelIndex") | |
927 s << "QModelIndexAccessor" << QString(d_type->actualIndirections(), '*') << " " << arg_name; | |
928 else if (d_type->isContainer()) { | |
929 if (d_export) { | |
930 s << DGenerator::translateType(d_type, d_function->ownerClass(), NoOption) << "* "; | |
931 } else | |
932 s << "DArray* "; | |
933 s << arg_name; | |
934 } else if (d_type->typeEntry()->isStructInD()) | |
935 s << d_type->typeEntry()->qualifiedCppName() << QString(d_type->actualIndirections(), '*') | |
936 << " " << arg_name; | |
937 else if (d_type->isTargetLangString()) { | |
938 if (d_export) | |
939 s << "wchar* "; | |
940 else | |
941 s << "const unsigned short* "; | |
942 s << QString("%1, int %1_size").arg(arg_name); | |
943 } else { | |
944 if(d_type->isVariant()) | |
945 s << "void*"; | |
946 else if (!d_type->hasNativeId()) | |
947 { | |
948 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_type->typeEntry()); | |
949 if(d_type->typeEntry()->isObject() && ctype->isAbstract() && d_export) | |
950 { | |
951 QString d_name = d_type->typeEntry()->qualifiedCppName(); | |
952 s << d_name + "_ConcreteWrapper" + QString(d_type->indirections(),'*'); | |
953 } | |
954 else | |
39 | 955 s << translateType(d_type, EnumAsInts, d_export); |
1 | 956 } |
957 else | |
958 s << "void*"; | |
959 s << " " << argument->indexedName(); | |
960 } | |
961 nativeArgCount++; | |
962 } | |
963 } | |
964 s << ")"; | |
965 } | |
966 | |
967 void CppImplGenerator::writeJavaLangObjectOverrideFunctions(QTextStream &s, const AbstractMetaClass *cls) | |
968 { | |
969 if (cls->hasHashFunction()) { | |
970 AbstractMetaFunctionList hashcode_functions = cls->queryFunctionsByName("hashCode"); | |
971 bool found = false; | |
972 foreach (const AbstractMetaFunction *function, hashcode_functions) { | |
973 if (function->actualMinimumArgumentCount() == 0) { | |
974 found = true; | |
975 break; | |
976 } | |
977 } | |
978 | |
979 if (!found) { | |
980 s << endl | |
981 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_hashCode", "jint") | |
982 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
983 << INDENT << "{" << endl; | |
984 { | |
985 Indentation indent(INDENT); | |
986 s << INDENT << "Q_UNUSED(__jni_env);" << endl | |
987 << INDENT << cls->qualifiedCppName() << " *__qt_this = (" | |
988 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
989 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
990 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
991 << INDENT << "return qHash(*__qt_this);" << endl; | |
992 } | |
993 s << INDENT << "}" << endl; | |
994 } | |
995 } | |
996 | |
997 // Qt has a standard toString() conversion in QVariant? | |
998 QVariant::Type type = QVariant::nameToType(cls->qualifiedCppName().toLatin1()); | |
999 if (QVariant(type).canConvert(QVariant::String) && !cls->hasToStringCapability()) { | |
1000 AbstractMetaFunctionList tostring_functions = cls->queryFunctionsByName("toString"); | |
1001 bool found = false; | |
1002 foreach (const AbstractMetaFunction *function, tostring_functions) { | |
1003 if (function->actualMinimumArgumentCount() == 0) { | |
1004 found = true; | |
1005 break; | |
1006 } | |
1007 } | |
1008 | |
1009 if (!found) { | |
1010 s << endl | |
1011 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_toString", "jstring") | |
1012 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1013 << INDENT << "{" << endl; | |
1014 { | |
1015 Indentation indent(INDENT); | |
1016 s << INDENT << cls->qualifiedCppName() << " *__qt_this = (" | |
1017 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1018 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1019 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1020 << INDENT << "return qtjambi_from_qstring(__jni_env, QVariant(*__qt_this).toString());" << endl; | |
1021 } | |
1022 s << INDENT << "}" << endl; | |
1023 } | |
1024 } | |
1025 } | |
1026 | |
1027 void CppImplGenerator::writeExtraFunctions(QTextStream &s, const AbstractMetaClass *java_class) | |
1028 { | |
1029 const ComplexTypeEntry *class_type = java_class->typeEntry(); | |
1030 Q_ASSERT(class_type); | |
1031 | |
1032 CodeSnipList code_snips = class_type->codeSnips(); | |
1033 foreach (const CodeSnip &snip, code_snips) { | |
1034 if (snip.language == TypeSystem::ShellCode || snip.language == TypeSystem::NativeCode) { | |
1035 snip.formattedCode(s, INDENT) << endl; | |
1036 } | |
1037 } | |
1038 } | |
1039 | |
1040 void CppImplGenerator::writeToStringFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
1041 { | |
1042 FunctionModelItem fun = java_class->hasToStringCapability(); | |
1043 bool core = java_class->package() == QLatin1String("qt.core"); | |
1044 bool qevent = false; | |
1045 | |
1046 const AbstractMetaClass *cls = java_class; | |
1047 while (cls) { | |
1048 if (cls->name() == "QEvent") { | |
1049 qevent = true; | |
1050 fun = cls->hasToStringCapability(); | |
1051 break; | |
1052 } | |
1053 cls = cls->baseClass(); | |
1054 } | |
1055 | |
1056 if (!java_class->hasDefaultToStringFunction() && fun && !(qevent && core)) { | |
1057 | |
1058 int indirections = fun->arguments().at(1)->type().indirections(); | |
1059 QString deref = QLatin1String(indirections == 0 ? "*" : ""); | |
1060 | |
1061 s << endl; | |
1062 s << "#include <QDebug>" << endl; | |
1063 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_toString", "jstring") | |
1064 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1065 << INDENT << "{" << endl; | |
1066 { | |
1067 Indentation indent(INDENT); | |
1068 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = (" | |
1069 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1070 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1071 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1072 | |
1073 << INDENT << "QString res;" << endl | |
1074 << INDENT << "QDebug d(&res);" << endl | |
1075 << INDENT << "d << " << deref << "__qt_this;" << endl; | |
1076 s << INDENT << "return qtjambi_from_qstring(__jni_env, res);" << endl; | |
1077 } | |
1078 s << INDENT << "}" << endl << endl; | |
1079 } | |
1080 } | |
1081 | |
1082 void CppImplGenerator::writeCloneFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
1083 { | |
1084 s << endl | |
1085 << jni_function_signature(java_class->package(), java_class->name(), "__qt_clone", "jobject") << endl | |
1086 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1087 << INDENT << "{" << endl; | |
1088 { | |
1089 Indentation indent(INDENT); | |
1090 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = (" | |
1091 | |
1092 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1093 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1094 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1095 << INDENT << java_class->qualifiedCppName() << " *res = __qt_this;" << endl | |
1096 << INDENT << "return qtjambi_from_object(__jni_env, res, \"" << java_class->name() << "\", \"" << java_class->package().replace(".", "/") << "/\", true);" << endl; | |
1097 } | |
1098 s << INDENT << "}" << endl << endl; | |
1099 } | |
1100 | |
1101 void CppImplGenerator::writeShellSignatures(QTextStream &s, const AbstractMetaClass *java_class) | |
1102 { | |
1103 bool has_constructors = java_class->hasConstructors(); | |
1104 | |
1105 // Write the function names... | |
1106 if (has_constructors && java_class->hasVirtualFunctions()) { | |
1107 AbstractMetaFunctionList virtual_functions = java_class->virtualFunctions(); | |
1108 { | |
1109 Indentation indent(INDENT); | |
1110 | |
1111 int pos = -1; | |
1112 foreach (AbstractMetaFunction *function, virtual_functions) { | |
1113 ++pos; | |
1114 | |
1115 if (pos == 0) | |
1116 s << "static const char *qtjambi_method_names[] = {"; | |
1117 else | |
1118 s << ","; | |
1119 s << endl | |
1120 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ " | |
1121 << "\"" << function->name() << "\""; | |
1122 } | |
1123 if (pos >= 0) | |
1124 s << endl << "};" << endl << endl; | |
1125 else | |
1126 s << "static const char **qtjambi_method_names = 0;" << endl; | |
1127 } | |
1128 | |
1129 // Write the function signatures | |
1130 { | |
1131 Indentation indent(INDENT); | |
1132 | |
1133 int pos = -1; | |
1134 foreach (AbstractMetaFunction *function, virtual_functions) { | |
1135 ++pos; | |
1136 | |
1137 if (pos == 0) | |
1138 s << "static const char *qtjambi_method_signatures[] = {"; | |
1139 else | |
1140 s << ","; | |
1141 s << endl | |
1142 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ " | |
1143 << "\"" | |
1144 << jni_signature(function, SlashesAndStuff) | |
1145 << "\""; | |
1146 } | |
1147 if (pos >= 0) | |
1148 s << endl << "};" << endl; | |
1149 else | |
1150 s << "static const char **qtjambi_method_signatures = 0;" << endl; | |
1151 s << "static const int qtjambi_method_count = " << QString::number(pos + 1) << ";" << endl | |
1152 << endl; | |
1153 } | |
1154 } | |
1155 | |
1156 if (has_constructors && java_class->hasInconsistentFunctions()) { | |
1157 AbstractMetaFunctionList inconsistents = java_class->cppInconsistentFunctions(); | |
1158 // Write the inconsistent function names... | |
1159 { | |
1160 Indentation indent(INDENT); | |
1161 s << "static const char *qtjambi_inconsistent_names[] = {"; | |
1162 for (int i=0; i<inconsistents.size(); ++i) { | |
1163 if (i != 0) | |
1164 s << ","; | |
1165 s << endl << INDENT << "\"" << inconsistents.at(i)->name() << "\""; | |
1166 } | |
1167 s << endl << "};" << endl << endl; | |
1168 } | |
1169 | |
1170 // Write the function signatures | |
1171 { | |
1172 Indentation indent(INDENT); | |
1173 s << "static const char *qtjambi_inconsistent_signatures[] = {"; | |
1174 for (int i=0; i<inconsistents.size(); ++i) { | |
1175 const AbstractMetaFunction *function = inconsistents.at(i); | |
1176 | |
1177 if (i != 0) | |
1178 s << ","; | |
1179 s << endl << INDENT << "\"" | |
1180 << jni_signature(function, SlashesAndStuff) | |
1181 << "\""; | |
1182 } | |
1183 s << endl << "};" << endl << endl; | |
1184 } | |
1185 s << "static const int qtjambi_inconsistent_count = " << inconsistents.size() << ";" << endl | |
1186 << endl; | |
1187 } | |
1188 | |
1189 | |
1190 AbstractMetaFunctionList signal_functions = java_class->cppSignalFunctions(); | |
1191 if (signal_functions.size()) { | |
1192 Indentation indent(INDENT); | |
1193 s << "static const char *qtjambi_signal_names[] = {"; | |
1194 for (int i=0; i<signal_functions.size(); ++i) { | |
1195 if (i != 0) | |
1196 s << ","; | |
1197 | |
1198 const AbstractMetaFunction *f = signal_functions.at(i); | |
1199 | |
1200 QString signalName = f->name(); | |
1201 | |
1202 s << endl << INDENT << "\"" << signalName << "\""; | |
1203 } | |
1204 s << endl << "};" << endl << endl; | |
1205 | |
1206 s << "static const int qtjambi_signal_argumentcounts[] = {"; | |
1207 for (int i=0; i<signal_functions.size(); ++i) { | |
1208 if (i != 0) | |
1209 s << ","; | |
1210 s << endl << INDENT << signal_functions.at(i)->arguments().count(); | |
1211 } | |
1212 s << endl << "};" << endl << endl; | |
1213 s << "static const int qtjambi_signal_count = " << signal_functions.size() << ";" << endl | |
1214 << endl; | |
1215 } | |
1216 } | |
1217 | |
1218 void CppImplGenerator::writeQObjectFunctions(QTextStream &s, const AbstractMetaClass *java_class) | |
1219 { | |
1220 // QObject::metaObject() | |
1221 /* s << "const QMetaObject *" << shellClassName(java_class) << "::metaObject() const" << endl | |
1222 << "{" << endl | |
1223 << " if (m_meta_object == 0) {" << endl | |
1224 << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl | |
1225 << " jobject __obj = m_link != 0 ? m_link->javaObject(__jni_env) : 0;" << endl | |
1226 << " if (__obj == 0) return " << java_class->qualifiedCppName() << "::metaObject();" << endl | |
1227 << " else m_meta_object = qtjambi_metaobject_for_class(__jni_env, __jni_env->GetObjectClass(__obj), " << java_class->qualifiedCppName() << "::metaObject());" << endl; | |
1228 | |
1229 | |
1230 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions(); | |
1231 for (int pos=0; pos<virtualFunctions.size(); ++pos) { | |
1232 const AbstractMetaFunction *virtualFunction = virtualFunctions.at(pos); | |
1233 if (virtualFunction->isVirtualSlot()) { | |
1234 QStringList introspectionCompatibleSignatures = virtualFunction->introspectionCompatibleSignatures(); | |
1235 foreach (QString introspectionCompatibleSignature, introspectionCompatibleSignatures) { | |
1236 s << " {" << endl | |
1237 << " int idx = " | |
1238 << java_class->qualifiedCppName() << "::metaObject()->indexOfMethod(\"" | |
1239 << introspectionCompatibleSignature << "\");" << endl; | |
1240 | |
1241 s << " if (idx >= 0) m_map.insert(idx, " << pos << ");" << endl | |
1242 << " }" << endl; | |
1243 } | |
1244 } | |
1245 | |
1246 } | |
1247 | |
1248 s << " }" << endl | |
1249 << " return m_meta_object;" << endl | |
1250 << "}" << endl << endl; | |
1251 | |
1252 // QObject::qt_metacast() | |
1253 s << "void *" << shellClassName(java_class) << "::qt_metacast(const char *_clname)" << endl | |
1254 << "{" << endl | |
1255 << " if (!_clname) return 0;" << endl | |
1256 << " if (!strcmp(_clname, \"" << shellClassName(java_class) << "\"))" << endl | |
1257 << " return static_cast<void*>(const_cast<" << shellClassName(java_class) << "*>(this));" << endl | |
1258 << " return " << java_class->qualifiedCppName() << "::qt_metacast(_clname);" << endl | |
1259 << "}" << endl << endl; | |
1260 */ | |
1261 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1262 // writeSignalsHandling(s, java_class); |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1263 /* |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1264 // QObject_Link::qt_metacall() |
1 | 1265 s << "int " << shellClassName(java_class) << "::qt_metacall(QMetaObject::Call _c, int _id, void **_a)" << endl |
1266 << "{" << endl; | |
1267 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1268 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
|
1269 << " 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
|
1270 << " return _id;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1271 // << " Q_ASSERT(_id < 2);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1272 << " 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
|
1273 << " return -1;" << endl |
1 | 1274 << "}" << endl << endl; |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1275 */ |
1 | 1276 } |
1277 | |
1278 void CppImplGenerator::writeSignalHandler(QTextStream &s, const AbstractMetaClass *d_class, AbstractMetaFunction *function) | |
1279 { | |
1280 QString extra_args, extra_call_args, conversion_code; | |
1281 QTextStream s2(&conversion_code); | |
1282 | |
1283 Indentation indent(INDENT); | |
1284 | |
1285 AbstractMetaArgumentList arguments = function->arguments(); | |
1286 foreach (AbstractMetaArgument *argument, arguments) { | |
1287 if(argument->type()->isContainer()) { | |
1288 QString arg_name = argument->indexedName(); | |
1289 const AbstractMetaType *arg_type = argument->type(); | |
1290 extra_args += ", DArray " + arg_name; | |
1291 extra_call_args += ", " + arg_name + "_arr"; | |
1292 | |
1293 s2 << INDENT; | |
1294 writeTypeInfo(s2, arg_type, NoOption); | |
1295 s2 << arg_name << " = (*reinterpret_cast< "; | |
1296 writeTypeInfo(s2, arg_type, ExcludeReference); | |
1297 s2 << "(*)>(args[" << argument->argumentIndex() + 1 << "]));" << endl | |
1298 << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl | |
1299 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name); | |
1300 | |
1301 writeQtToJava(s2, | |
1302 arg_type, | |
1303 arg_name, | |
1304 "__d_" + arg_name, | |
1305 function, | |
1306 argument->argumentIndex() + 1, | |
1307 Option(VirtualDispatch)); | |
1308 } | |
1309 } | |
1310 QString sig_name = signalExternName(d_class, function); | |
1311 s << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle_in_d(void* d_entity, void** args" << extra_args <<");" << endl | |
1312 << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle(void* d_entity, void** args)" << endl | |
1313 << "{" << endl | |
1314 << conversion_code << endl | |
1315 << INDENT << sig_name << "_handle_in_d(d_entity, args" << extra_call_args << ");" << endl | |
1316 << "}" << endl; | |
1317 | |
1318 } | |
1319 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1320 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
|
1321 { |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1322 QString linkName = java_class->name() + "_Link"; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1323 QString className = java_class->name(); |
183 | 1324 |
1325 if (cpp_shared) | |
1326 s << "extern \"C\" typedef void (*qtd_pf_D_" << java_class->name() << "_delete)(void *d_ptr);" << endl | |
1327 << "qtd_pf_D_" << java_class->name() << "_delete qtd_D_" << java_class->name() << "_delete;" << endl << endl; | |
1328 else | |
1329 s << "extern \"C\" void qtd_D_" << java_class->name() << "_delete(void *d_ptr);" << endl << endl; | |
1330 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1331 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
|
1332 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1333 << "public:" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1334 << " Q_OBJECT_CHECK" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1335 << " 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
|
1336 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1337 << " " << linkName << "(QObject *parent, void *d_ptr) : QObject() { _d_ptr = d_ptr; }" << endl |
183 | 1338 << " ~" << linkName << "() { qtd_D_" << className << "_delete(_d_ptr); }" << endl |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1339 << " 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
|
1340 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1341 << "private:" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1342 << " void *_d_ptr;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1343 << "};" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1344 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1345 // QObject_Link::qt_metacall() |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1346 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
|
1347 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1348 << " _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
|
1349 << " 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
|
1350 << " return _id;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1351 // << " Q_ASSERT(_id < 2);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1352 << " 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
|
1353 << " return -1;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1354 << "}" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1355 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1356 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
|
1357 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1358 << " 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
|
1359 << "}" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1360 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1361 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
|
1362 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1363 << " 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
|
1364 << " " << 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
|
1365 << " return qobj_helper->d_entity();" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1366 << " } else" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1367 << " return NULL;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1368 << "}" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1369 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1370 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
|
1371 << "{" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1372 << " 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
|
1373 << " return;" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1374 << " " << 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
|
1375 << " 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
|
1376 << "}" << endl << endl; |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1377 } |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1378 |
1 | 1379 void CppImplGenerator::writeSignalsHandling(QTextStream &s, const AbstractMetaClass *java_class) |
1380 { | |
1381 s << "extern \"C\" typedef void (*EmitCallback)(void*, void**);" << endl; | |
1382 AbstractMetaFunctionList signal_funcs = signalFunctions(java_class); | |
1383 | |
1384 if (cpp_shared) | |
132 | 1385 if(signal_funcs.size() > 0) |
1386 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "];" << endl; | |
1387 else | |
1388 s << "EmitCallback emit_callbacks_" << java_class->name() << "[1];" << endl; // Hack for msvc. | |
1 | 1389 else { |
1390 // D-side signal callbacks | |
1391 for(int i = 0; i < signal_funcs.size(); i++) { | |
1392 AbstractMetaFunction *signal = signal_funcs.at(i); | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
1393 s << "extern \"C\" DLL_PUBLIC void " << signalExternName(java_class, signal) << "_handle(void* d_entity, void** args);" << endl; |
1 | 1394 } |
1395 | |
132 | 1396 if(signal_funcs.size() > 0) |
1397 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "] = {" << endl; | |
1398 else | |
1399 s << "EmitCallback emit_callbacks_" << java_class->name() << "[1] = {" << endl; // Hack for msvc. | |
1 | 1400 for(int i = 0; i < signal_funcs.size(); i++) { |
1401 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1402 s << endl; | |
1403 if (i != 0) | |
1404 s << ", "; | |
1405 s << "&" << signalExternName(java_class, signal) << "_handle"; | |
1406 } | |
1407 s << endl << "};" << endl << endl; | |
1408 } | |
1409 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1410 writeQObjectLink(s, java_class); |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
1411 |
1 | 1412 // Functions connecting/disconnecting shell's slots |
1413 for(int i = 0; i < signal_funcs.size(); i++) { | |
1414 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1415 QString sigExternName = signalExternName(java_class, signal); | |
1416 | |
1417 s << "extern \"C\" DLL_PUBLIC void " << sigExternName << "_connect" | |
1418 << "(void* native_id)" << endl << "{" << endl | |
1419 << " " << 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
|
1420 << " 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
|
1421 << " const QMetaObject &mo2 = " << java_class->name() << "_Link::staticMetaObject;" << endl |
1 | 1422 << " 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
|
1423 << " mo.connect(qobj, signalId, get_" << java_class->name() << "_link(qobj), mo2.methodCount() + " << i << ");" << endl |
1 | 1424 << "}" << endl; |
1425 | |
1426 s << "extern \"C\" DLL_PUBLIC void " << sigExternName << "_disconnect" | |
1427 << "(void* native_id)" << endl << "{" << endl | |
1428 << " " << shellClassName(java_class) << " *qobj = (" << shellClassName(java_class) << "*) native_id;" << endl | |
1429 << " 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
|
1430 << " const QMetaObject &mo2 = " << java_class->name() << "_Link::staticMetaObject;" << endl |
1 | 1431 << " 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
|
1432 << " mo.disconnect(qobj, signalId, get_" << java_class->name() << "_link(qobj), mo2.methodCount() + " << i << ");" << endl |
1 | 1433 << "}" << endl << endl; |
1434 } | |
1435 } | |
1436 | |
1437 | |
1438 void CppImplGenerator::writeShellConstructor(QTextStream &s, const AbstractMetaFunction *java_function) | |
1439 { | |
1440 if (java_function->isModifiedRemoved(TypeSystem::ShellCode)) | |
1441 return; | |
1442 | |
1443 const AbstractMetaClass *cls = java_function->ownerClass(); | |
1444 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1445 | |
1446 writeFunctionSignature(s, java_function, cls); | |
1447 | |
1448 s << endl; | |
1449 s << " : " << cls->qualifiedCppName() << "("; | |
1450 for (int i=0; i<arguments.size(); ++i) { | |
1451 s << arguments.at(i)->indexedName(); | |
1452 if (i != arguments.size() - 1) | |
1453 s << ", "; | |
1454 } | |
1455 s << ")"; | |
1456 if (cls->hasVirtualFunctions()) | |
1457 s << "," << endl << " Qtd_QObjectEntity(d_ptr)"; | |
1458 /* qtd s << " m_meta_object(0)," << endl; | |
1459 s << " m_vtable(0)," << endl | |
1460 << " m_link(0)" << endl; | |
1461 */ | |
1462 s << endl; | |
1463 s << "{" << endl; | |
1464 { | |
1465 Indentation indent(INDENT); | |
1466 writeCodeInjections(s, java_function, cls, CodeSnip::Beginning, TypeSystem::ShellCode); | |
1467 writeCodeInjections(s, java_function, cls, CodeSnip::End, TypeSystem::ShellCode); | |
1468 } | |
1469 s << "}" << endl << endl; | |
1470 } | |
1471 | |
1472 void CppImplGenerator::writeShellDestructor(QTextStream &s, const AbstractMetaClass *java_class) | |
1473 { | |
1474 s << shellClassName(java_class) << "::~" | |
1475 << shellClassName(java_class) << "()" << endl | |
1476 << "{" << endl; | |
1477 { | |
183 | 1478 /* qtd |
1 | 1479 Indentation indent(INDENT); |
183 | 1480 |
1 | 1481 s << "#ifdef QT_DEBUG" << endl |
1482 << INDENT << "if (m_vtable)" << endl | |
1483 << INDENT << " m_vtable->deref();" << endl | |
1484 << "#endif" << endl | |
1485 << INDENT << "if (m_link) {" << endl; | |
1486 | |
1487 AbstractMetaClassList interfaces = java_class->interfaces(); | |
1488 if (interfaces.size() + (java_class->baseClass() != 0 ? 1 : 0) > 1) { | |
1489 if (java_class->baseClass() != 0) | |
1490 interfaces += java_class->baseClass(); | |
1491 foreach (AbstractMetaClass *iface, interfaces) { | |
1492 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface; | |
1493 s << INDENT << " m_link->unregisterSubObject((" << impl->qualifiedCppName() << " *) this);" << endl; | |
1494 } | |
1495 } | |
1496 | |
1497 if (!java_class->isQObject()) { | |
1498 s << INDENT << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl | |
1499 << INDENT << " if (__jni_env != 0) m_link->nativeShellObjectDestroyed(__jni_env);" << endl; | |
1500 } | |
1501 | |
1502 #if defined(QTJAMBI_DEBUG_TOOLS) | |
1503 s << INDENT << " qtjambi_increase_shellDestructorCalledCount(QString::fromLatin1(\"" << java_class->name() << "\"));" << endl; | |
1504 #endif | |
1505 | |
1506 s << INDENT << "}" << endl; */ | |
1507 } | |
1508 s << "}" << endl << endl; | |
1509 } | |
1510 | |
1511 void CppImplGenerator::writeCodeInjections(QTextStream &s, const AbstractMetaFunction *java_function, | |
1512 const AbstractMetaClass *implementor, CodeSnip::Position position, | |
1513 TypeSystem::Language language) | |
1514 { | |
1515 | |
1516 FunctionModificationList mods; | |
1517 const AbstractMetaClass *cls = implementor; | |
1518 while (cls != 0) { | |
1519 mods += java_function->modifications(cls); | |
1520 | |
1521 if (cls == cls->baseClass()) | |
1522 break; | |
1523 cls = cls->baseClass(); | |
1524 } | |
1525 | |
1526 foreach (FunctionModification mod, mods) { | |
1527 if (mod.snips.count() <= 0) | |
1528 continue ; | |
1529 | |
1530 foreach (CodeSnip snip, mod.snips) { | |
1531 if (snip.position != position) | |
1532 continue ; | |
1533 | |
1534 if ((snip.language & language) == false) | |
1535 continue ; | |
1536 | |
1537 if (position == CodeSnip::End) | |
1538 s << endl; | |
1539 | |
1540 QString code; | |
1541 QTextStream tmpStream(&code); | |
1542 snip.formattedCode(tmpStream, INDENT); | |
1543 ArgumentMap map = snip.argumentMap; | |
1544 ArgumentMap::iterator it = map.begin(); | |
1545 for (;it!=map.end();++it) { | |
1546 int pos = it.key() - 1; | |
1547 QString meta_name = it.value(); | |
1548 | |
1549 if (pos >= 0 && pos < java_function->arguments().count()) { | |
1550 code = code.replace(meta_name, java_function->arguments().at(pos)->indexedName()); | |
1551 } else { | |
1552 QString debug = QString("argument map specifies invalid argument index %1" | |
1553 "for function '%2'") | |
1554 .arg(pos + 1).arg(java_function->name()); | |
1555 ReportHandler::warning(debug); | |
1556 } | |
1557 | |
1558 } | |
1559 s << code; | |
1560 if (position == CodeSnip::Beginning) | |
1561 s << endl; | |
1562 } | |
1563 } | |
1564 } | |
1565 | |
1566 static QString function_call_for_ownership(TypeSystem::Ownership owner, const QString &var_name) | |
1567 { | |
1568 if (owner == TypeSystem::CppOwnership) { | |
1569 return "setCppOwnership(__jni_env, " + var_name + ")"; | |
1570 } else if (owner == TypeSystem::TargetLangOwnership) { | |
1571 return "setJavaOwnership(__jni_env, " + var_name + ")"; | |
1572 } else if (owner == TypeSystem::DefaultOwnership) { | |
1573 return "setDefaultOwnership(__jni_env, " + var_name + ")"; | |
1574 } else { | |
1575 Q_ASSERT(false); | |
1576 return "bogus()"; | |
1577 } | |
1578 } | |
1579 | |
1580 void CppImplGenerator::writeOwnership(QTextStream &s, | |
1581 const AbstractMetaFunction *java_function, | |
1582 const QString &var_name, | |
1583 int var_index, | |
1584 const AbstractMetaClass *implementor) | |
1585 { | |
1586 TypeSystem::Ownership owner = TypeSystem::InvalidOwnership; | |
1587 const AbstractMetaClass *cls = implementor; | |
1588 while (cls != 0 && owner == TypeSystem::InvalidOwnership) { | |
1589 owner = java_function->ownership(cls, TypeSystem::ShellCode, var_index); | |
1590 cls = cls->baseClass(); | |
1591 } | |
1592 | |
1593 if (owner == TypeSystem::InvalidOwnership) | |
1594 return; | |
1595 | |
1596 if (var_index != -1) { | |
1597 s << INDENT << "if (" << var_name << " != 0) {" << endl; | |
1598 { | |
1599 Indentation indent(INDENT); | |
1600 s << INDENT << "QtJambiLink *__link = QtJambiLink::findLink(__jni_env, " | |
1601 << var_name << ");" << endl | |
1602 << INDENT << "Q_ASSERT(__link != 0);" << endl; | |
1603 | |
1604 s << INDENT << "__link->" << function_call_for_ownership(owner, var_name) << ";" << endl; | |
1605 } | |
1606 s << INDENT << "}" << endl; | |
1607 } else { | |
1608 s << INDENT << "if (m_link) {" << endl; | |
1609 { | |
1610 Indentation indent(INDENT); | |
1611 s << INDENT << "m_link->" << function_call_for_ownership(owner, "m_link->javaObject(__jni_env)") << ";" << endl; | |
1612 } | |
1613 s << INDENT << "}" << endl; | |
1614 } | |
1615 | |
1616 } | |
1617 | |
1618 void CppImplGenerator::writeShellFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
1619 const AbstractMetaClass *implementor, int id) | |
1620 { | |
1621 writeFunctionSignature(s, java_function, implementor, QString(), OriginalName); | |
1622 | |
1623 s << endl | |
1624 << "{" << endl; | |
1625 | |
1626 Indentation indent(INDENT); | |
1627 | |
1628 QString java_function_signature = java_function->signature(); | |
1629 /* s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) entering: " << implementor->name() << "::" | |
1630 << java_function_signature << "\");" << endl; | |
1631 */ | |
1632 writeCodeInjections(s, java_function, implementor, CodeSnip::Beginning, TypeSystem::ShellCode); | |
1633 | |
1634 // s << " printf(\"%s : %s\\n\", \"" << java_function->enclosingClass()->name() << "\"" | |
1635 // << ", \"" << java_function->name() << "\");" << endl; | |
1636 | |
1637 if (!java_function->isFinalInCpp() || java_function->isVirtualSlot()) { | |
1638 s << INDENT << "jmethodID method_id = m_vtable->method(" << id << ");" << endl; | |
1639 s << INDENT << "if (method_id) {" << endl; | |
1640 | |
1641 { | |
1642 Indentation indent(INDENT); | |
1643 s << INDENT << "JNIEnv *__jni_env = qtjambi_current_environment();" << endl; | |
1644 | |
1645 // This nasty case comes up when we're shutting down while receiving virtual | |
1646 // calls.. With these checks we safly abort... | |
1647 s << INDENT << "if (!__jni_env) {" << endl | |
1648 << " "; | |
1649 writeBaseClassFunctionCall(s, java_function, implementor); | |
1650 if (!java_function->type()) { | |
1651 s << INDENT << " return;" << endl; | |
1652 } | |
1653 s << INDENT << "}" << endl; | |
1654 | |
1655 // otherwise, continue with the function call... | |
1656 s << INDENT << "__jni_env->PushLocalFrame(100);" << endl; | |
1657 | |
1658 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1659 AbstractMetaArgumentList argumentsToReset; | |
1660 foreach (AbstractMetaArgument *argument, arguments) { | |
1661 if (!java_function->argumentRemoved(argument->argumentIndex()+1)) { | |
1662 if (!argument->type()->isPrimitive() | |
1663 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
1664 writeQtToJava(s, | |
1665 argument->type(), | |
1666 argument->indexedName(), | |
1667 "__java_" + argument->indexedName(), | |
1668 java_function, | |
1669 argument->argumentIndex() + 1); | |
1670 } | |
1671 | |
1672 if (java_function->resetObjectAfterUse(argument->argumentIndex()+1)) | |
1673 argumentsToReset.append(argument); | |
1674 } | |
1675 } | |
1676 | |
1677 for (int i=0; i<arguments.size(); ++i) | |
1678 writeOwnership(s, java_function, "__java_" + arguments.at(i)->indexedName(), i+1, implementor); | |
1679 | |
1680 | |
1681 AbstractMetaType *function_type = java_function->type(); | |
1682 QString new_return_type = java_function->typeReplaced(0); | |
1683 bool has_function_type = ((function_type != 0 | |
1684 || !new_return_type.isEmpty()) | |
1685 && new_return_type != "void"); | |
1686 | |
1687 s << INDENT; | |
1688 if (has_function_type) { | |
1689 if (new_return_type.isEmpty()) { | |
1690 s << translateType(function_type); | |
1691 } else { | |
1692 s << jniName(new_return_type); | |
1693 } | |
1694 s << " " << "__d_return_value = "; | |
1695 } | |
1696 | |
1697 s << "__jni_env->"; | |
1698 if (new_return_type.isEmpty()) { | |
1699 s << callXxxMethod(java_function->type()); | |
1700 } else if (!has_function_type) { | |
1701 s << "CallVoidMethod"; | |
1702 } else { | |
1703 s << callXxxMethod(new_return_type); | |
1704 } | |
1705 | |
1706 s << "(m_link->javaObject(__jni_env), method_id"; | |
1707 if (arguments.size() > 0) | |
1708 s << ", "; | |
1709 writeFunctionCallArguments(s, java_function, "__java_", Option(NoCasts | SkipRemovedArguments)); | |
1710 s << ");" << endl | |
1711 << INDENT << "qtjambi_exception_check(__jni_env);" << endl; | |
1712 | |
1713 if (has_function_type) { | |
1714 writeJavaToQt(s, function_type, "__qt_return_value", "__d_return_value", | |
1715 java_function, 0, GlobalRefJObject); | |
1716 | |
1717 if (java_function->nullPointersDisabled()) { | |
1718 s << INDENT << "if (__d_return_value == 0) {" << endl; | |
1719 { | |
1720 Indentation indent(INDENT); | |
1721 s << INDENT << "fprintf(stderr, \"QtJambi: Unexpected null pointer returned from override of '" << java_function->name() << "' in class '%s'\\n\"," << endl | |
1722 << INDENT << " qPrintable(qtjambi_object_class_name(__jni_env, m_link->javaObject(__jni_env))));" << endl; | |
1723 s << INDENT << "__qt_return_value = "; | |
1724 QString defaultValue = java_function->nullPointerDefaultValue(); | |
1725 if (!defaultValue.isEmpty()) | |
1726 s << defaultValue << ";"; | |
1727 else | |
1728 writeBaseClassFunctionCall(s, java_function, implementor, NoReturnStatement); | |
1729 s << endl; | |
1730 } | |
1731 | |
1732 s << INDENT << "}" << endl; | |
1733 } | |
1734 } else if (!java_function->conversionRule(TypeSystem::ShellCode, 0).isEmpty()) { | |
1735 writeConversionRule(s, TypeSystem::ShellCode, java_function, 0, "<invalid>", "<invalid>"); | |
1736 } | |
1737 | |
1738 writeOwnership(s, java_function, "this", -1, implementor); | |
1739 writeOwnership(s, java_function, "__d_return_value", 0, implementor); | |
1740 | |
1741 foreach (AbstractMetaArgument *argumentToReset, argumentsToReset) { | |
1742 | |
1743 QString argumentName = "__java_" + argumentToReset->indexedName(); | |
1744 | |
1745 s << INDENT; | |
1746 if (argumentToReset->type()->isContainer()) | |
1747 s << "qtjambi_invalidate_collection("; | |
1748 else | |
1749 s << "qtjambi_invalidate_object("; | |
1750 | |
1751 s << "__jni_env, " << argumentName << ");" << endl; | |
1752 } | |
1753 | |
1754 s << INDENT << "__jni_env->PopLocalFrame(0);" << endl; | |
1755 | |
1756 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> leaving: " << implementor->name() | |
1757 << "::" << java_function_signature << "\");" << endl; | |
1758 | |
1759 if (function_type) | |
1760 s << INDENT << "return __qt_return_value;" << endl; | |
1761 | |
1762 } | |
1763 | |
1764 s << INDENT << "} else {" << endl; | |
1765 | |
1766 { | |
1767 Indentation indent(INDENT); | |
1768 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> super() and leaving: " | |
1769 << implementor->name() << "::" << java_function_signature << "\");" << endl; | |
1770 writeBaseClassFunctionCall(s, java_function, implementor); | |
1771 } | |
1772 | |
1773 | |
1774 s << INDENT << "}" << endl; | |
1775 | |
1776 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1777 | |
1778 // A little trick to close open painters on a widget | |
1779 if (java_function->name() == "paintEvent") { | |
1780 s << INDENT << "JNIEnv *env = qtjambi_current_environment();" << endl | |
1781 << INDENT << "qtjambi_end_paint(env, m_link->javaObject(env));" << endl; | |
1782 } | |
1783 | |
1784 } else { | |
1785 if(java_function->isRemovedFrom(implementor, TypeSystem::TargetLangCode)){ | |
1786 // Avoid compiler warnings for unused parameters | |
1787 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1788 | |
1789 foreach (const AbstractMetaArgument *argument, arguments) { | |
1790 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
1791 } | |
1792 } | |
1793 writeBaseClassFunctionCall(s, java_function, implementor); | |
1794 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1795 } | |
1796 | |
1797 s << "}" << endl << endl; | |
1798 | |
1799 } | |
1800 | |
1801 // ### kill implementor | |
1802 | |
1803 void CppImplGenerator::writePublicFunctionOverride(QTextStream &s, | |
1804 const AbstractMetaFunction *java_function, | |
1805 const AbstractMetaClass *implementor) | |
1806 { | |
1807 Q_ASSERT(java_function->originalAttributes() | |
1808 & (AbstractMetaAttributes::Protected | |
1809 | AbstractMetaAttributes::Final)); | |
1810 | |
1811 // The write a public override version of this function to be used by native functions | |
1812 writeFunctionSignature(s, java_function, implementor, "__public_", | |
1813 Option(EnumAsInts | UnderscoreSpaces | |
1814 | (java_function->isAbstract() ? SkipName : NoOption))); | |
1815 s << endl | |
1816 << "{" << endl; | |
1817 Indentation indent(INDENT); | |
1818 writeBaseClassFunctionCall(s, java_function, implementor); | |
1819 s << "}" << endl << endl; | |
1820 } | |
1821 | |
1822 | |
1823 void CppImplGenerator::writeVirtualFunctionOverride(QTextStream &s, | |
1824 const AbstractMetaFunction *java_function, | |
1825 const AbstractMetaClass *implementor) | |
1826 { | |
1827 Q_ASSERT(!java_function->isFinalInCpp()); | |
1828 | |
1829 Option options = Option(EnumAsInts | UnderscoreSpaces); | |
1830 | |
1831 // The write a public override version of this function to be used by native functions | |
1832 writeFunctionSignature(s, java_function, implementor, "__override_", | |
1833 options, | |
21 | 1834 QString(), // the class prefix |
1835 QStringList() << "bool static_call"); | |
1 | 1836 s << endl |
1837 << "{" << endl; | |
1838 Indentation indent(INDENT); | |
21 | 1839 s << INDENT << "if (static_call) {" << endl; |
1 | 1840 { |
21 | 1841 Indentation indent(INDENT); |
1 | 1842 writeBaseClassFunctionCall(s, java_function, implementor); |
21 | 1843 } |
1 | 1844 s << INDENT << "} else {" << endl; |
1845 { | |
1846 Indentation indent(INDENT); | |
1847 writeBaseClassFunctionCall(s, java_function, implementor, VirtualCall); | |
1848 } | |
1849 | |
21 | 1850 s << INDENT << "}" << endl; |
1 | 1851 s << "}" << endl << endl; |
1852 } | |
1853 | |
1854 | |
1855 void CppImplGenerator::writeBaseClassFunctionCall(QTextStream &s, | |
1856 const AbstractMetaFunction *java_function, | |
1857 const AbstractMetaClass *, | |
1858 Option options) | |
1859 { | |
1860 bool static_call = !(options & VirtualCall); | |
1861 if ((options & NoReturnStatement) == 0) | |
1862 s << INDENT; | |
1863 if (java_function->isAbstract() && static_call) { | |
1864 s << default_return_statement_qt(java_function->type(), options) << ";" << endl; | |
1865 } else { | |
1866 if (java_function->type() && (options & NoReturnStatement) == 0) | |
1867 s << "return "; | |
1868 if (static_call) { | |
1869 const AbstractMetaClass *implementor = java_function->implementingClass(); | |
1870 if (java_function->isInterfaceFunction()) | |
1871 implementor = java_function->interfaceClass()->primaryInterfaceImplementor(); | |
1872 s << implementor->qualifiedCppName() << "::"; | |
1873 } | |
1874 s << java_function->originalName() << "("; | |
1875 writeFunctionCallArguments(s, java_function, QString(), Option(options | ForceEnumCast)); | |
1876 s << ");" << endl; | |
1877 } | |
1878 } | |
1879 | |
1880 | |
1881 void CppImplGenerator::writeFunctionName(QTextStream &s, | |
1882 const AbstractMetaFunction *java_function, | |
1883 const AbstractMetaClass *java_class, | |
1884 uint options) | |
1885 { | |
1886 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass(); | |
1887 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1888 | |
1889 // Function signature | |
1890 QString return_type = jniReturnName(java_function, options); | |
1891 | |
1892 QString function_name; | |
1893 | |
1894 bool callThrough = java_function->needsCallThrough(); | |
1895 /* qtd if (!callThrough) | |
1896 function_name = java_function->name(); | |
1897 else */ | |
1898 function_name = java_function->marshalledName(); | |
1899 | |
1900 QString args = "__"; | |
1901 | |
1902 if (callThrough && !java_function->isStatic() && !java_function->isConstructor()) | |
1903 args += "J"; | |
1904 | |
1905 if (!arguments.isEmpty()) { | |
1906 foreach (const AbstractMetaArgument *argument, arguments) { | |
1907 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
1908 if (!argument->type()->hasNativeId()) { | |
1909 QString modified_type = java_function->typeReplaced(argument->argumentIndex()+1); | |
1910 if (modified_type.isEmpty()) | |
1911 args += jni_signature(argument->type(), Underscores); | |
1912 else | |
1913 args += jni_signature(modified_type, Underscores); | |
1914 } else { | |
1915 args += "J"; | |
1916 } | |
1917 } | |
1918 } | |
1919 } | |
1920 | |
1921 s << jni_function_signature(cls->package(), cls->name(), function_name, | |
1922 return_type, args, options); | |
1923 | |
1924 } | |
1925 | |
1926 void CppImplGenerator::writeFinalFunctionArguments(QTextStream &s, const AbstractMetaFunction *java_function, bool d_export) | |
1927 { | |
1928 bool callThrough = java_function->needsCallThrough(); | |
1929 s << "("; | |
1930 /* qtd | |
1931 << "JNIEnv *__jni_env," << endl; | |
1932 if (!java_function->isConstructor()) { | |
1933 if (java_function->isStatic()) | |
1934 s << " jclass"; | |
1935 else | |
1936 s << " jobject"; | |
1937 } else | |
1938 s << " jobject " << java_object_name; | |
1939 */ | |
1940 uint nativeArgCount = 0; | |
1941 const AbstractMetaClass *cls = java_function->ownerClass(); | |
1942 if (java_function->isConstructor() && | |
1943 ( cls->hasVirtualFunctions() | |
1944 || cls->typeEntry()->isObject() ) ) | |
1945 { | |
1946 s << "void *d_ptr"; | |
1947 nativeArgCount++; | |
1948 } | |
1949 | |
1950 // passing pointer to C++ object | |
1951 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
1952 if (hasNativeId) { | |
1953 if (nativeArgCount > 0) | |
1954 s << "," << endl << " "; | |
1955 /* qtd << "," << endl */ s << "void* __this_nativeId"; | |
1956 nativeArgCount++; | |
1957 } | |
1958 | |
1959 AbstractMetaType *f_type = java_function->type(); | |
1960 | |
1961 // return values as strings, arrays or QModelIndex'es we return as arguments | |
1962 bool return_arg = false; | |
1963 if (f_type) { | |
1964 if (f_type->isTargetLangString() || | |
1965 f_type->isContainer() || | |
81 | 1966 f_type->name() == "QModelIndex" || |
1967 f_type->typeEntry()->isStructInD()) | |
1 | 1968 return_arg = true; |
1969 | |
1970 if (return_arg && nativeArgCount > 0) | |
1971 s << "," << endl << " "; | |
1972 | |
1973 if (f_type->isTargetLangString() || f_type->isContainer()) | |
1974 s << "void*"; | |
1975 else if (f_type->name() == "QModelIndex") | |
1976 s << "QModelIndexAccessor*"; | |
81 | 1977 else if (f_type->typeEntry()->isStructInD()) |
1978 s << f_type->typeEntry()->qualifiedCppName() << " *"; | |
1 | 1979 |
1980 if(return_arg) { | |
1981 s << " __d_return_value"; | |
1982 nativeArgCount++; | |
1983 } | |
1984 } | |
1985 | |
1986 // the function arguments | |
1987 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1988 foreach (const AbstractMetaArgument *argument, arguments) { | |
1989 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
1990 AbstractMetaType *d_type = argument->type(); | |
1991 const TypeEntry *te = d_type->typeEntry(); | |
1992 | |
1993 QString arg_name = argument->indexedName(); | |
1994 | |
1995 if (nativeArgCount > 0) | |
1996 s << "," << endl << " "; | |
81 | 1997 |
1998 // if QString argument we have to pass DArray | |
35 | 1999 if ((te && te->qualifiedCppName() == "QString") || d_type->isTargetLangString()) { |
2000 if (d_export) | |
2001 s << "string " << arg_name; | |
2002 else | |
2003 s << "DArray " << arg_name; | |
2004 } else if (d_type->isContainer()) { | |
1 | 2005 const ContainerTypeEntry *cte = |
2006 static_cast<const ContainerTypeEntry *>(te); | |
2007 if(isLinearContainer(cte)) | |
2008 s << QString("void *%1, size_t %1_size").arg(arg_name); | |
2009 } else { | |
2010 if (!d_type->hasNativeId()) { | |
2011 if(d_type->isVariant()) { | |
2012 if (d_export) s << "void*"; | |
2013 else s << "QVariant*"; | |
2014 } else | |
2015 s << translateType(argument->type(), EnumAsInts, d_export); | |
2016 } | |
188 | 2017 else if (d_type->name() == "QModelIndex") { |
2018 if(d_export && dVersion == 2 && d_type->isConstant()) | |
2019 s << "const(QModelIndexAccessor)"; | |
2020 else | |
2021 s << "QModelIndexAccessor"; | |
2022 } else if (te->isStructInD()) | |
1 | 2023 s << te->qualifiedCppName(); |
2024 else | |
2025 s << "void*"; | |
2026 | |
2027 s << " " << arg_name; | |
2028 } | |
2029 nativeArgCount++; | |
2030 } | |
2031 } | |
2032 s << ")"; | |
2033 } | |
2034 | |
2035 | |
2036 /*! | |
2037 Generates type conversion from Java -> Qt for all the arguments | |
2038 that are to be to be passed to the function | |
2039 */ | |
2040 void CppImplGenerator::writeFinalFunctionSetup(QTextStream &s, const AbstractMetaFunction *java_function, | |
2041 const QString &qt_object_name, | |
2042 const AbstractMetaClass *cls) | |
2043 { | |
2044 | |
2045 // Translate each of the function arguments into qt types | |
2046 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2047 foreach (const AbstractMetaArgument *argument, arguments) { | |
2048 if (!argument->type()->isPrimitive() | |
2049 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex() + 1).isEmpty()) { | |
2050 writeJavaToQt(s, | |
2051 argument->type(), | |
2052 "__qt_" + argument->indexedName(), | |
2053 argument->indexedName(), | |
2054 java_function, | |
2055 argument->argumentIndex() + 1, | |
2056 Option(UseNativeIds | EnumAsInts)); | |
2057 } | |
2058 } | |
2059 | |
2060 // Extract the qt equivalent to the this pointer and name it "qt_object_name" | |
2061 if (!java_function->isStatic() && !java_function->isConstructor()) { | |
2062 // qtd2 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls); | |
2063 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls); | |
2064 s << INDENT | |
2065 << className << " *" << qt_object_name | |
2066 << " = (" << className << " *) __this_nativeId;" | |
2067 << endl; | |
2068 // << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
2069 // qtd << INDENT << "Q_ASSERT(" << qt_object_name << ");" << endl; | |
2070 } | |
2071 } | |
2072 | |
2073 void CppImplGenerator::writeFinalFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
2074 const AbstractMetaClass *java_class) | |
2075 { | |
2076 Q_ASSERT(java_class); | |
2077 | |
2078 if (java_function->isModifiedRemoved(TypeSystem::NativeCode)) | |
2079 return; | |
2080 | |
2081 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass(); | |
2082 | |
2083 QString java_function_signature = cls->name() + "::" + java_function->signature(); | |
2084 | |
2085 s << "// " << java_function_signature << endl; | |
2086 | |
2087 const AbstractMetaType *function_type = java_function->type(); | |
2088 QString new_return_type = java_function->typeReplaced(0); | |
2089 bool has_function_type = new_return_type != "void" | |
2090 && (!new_return_type.isEmpty() || function_type != 0) | |
2091 && java_function->argumentReplaced(0).isEmpty(); | |
2092 | |
2093 const QString qt_object_name = java_function->isStatic() ? shellClassName(cls) : "__qt_this"; | |
2094 const QString java_object_name = java_function->isStatic() ? "__jni_class" : "__jni_object"; | |
2095 | |
2096 // we are not wrapping some functions depending on arguments because API is not yet full | |
2097 if (notWrappedYet(java_function)) | |
2098 return; | |
2099 | |
2100 // function signature... | |
2101 bool callThrough = java_function->needsCallThrough(); | |
2102 uint options = m_native_jump_table ? ReturnType | ExternC : StandardJNISignature; | |
2103 writeFunctionName(s, java_function, cls, options); | |
2104 s << endl; | |
2105 writeFinalFunctionArguments(s, java_function); | |
2106 s << endl << "{" << endl; | |
2107 Indentation indent(INDENT); | |
2108 | |
2109 // qtd2 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) entering: " << java_function_signature << "\");" << endl; | |
2110 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
2111 /* if (hasNativeId) | |
2112 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl; | |
2113 // Avoid compiler warnings when the variables are unused | |
2114 { | |
2115 s << INDENT << "Q_UNUSED(__jni_env)" << endl; | |
2116 | |
2117 if (java_function->isConstructor()) | |
2118 s << INDENT << "Q_UNUSED(" << java_object_name << ")" << endl; | |
2119 | |
2120 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
2121 if (hasNativeId) | |
2122 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl; | |
2123 } | |
2124 */ | |
2125 | |
2126 if (cls->isFinal() && (!java_function->isAbstract() || !java_function->isFinalInTargetLang()) && !java_function->wasPublic()) { | |
2127 QString debug = QString("protected function '%1' in final class '%2'") | |
2128 .arg(java_function->signature()).arg(java_class->name()); | |
2129 ReportHandler::warning(debug); | |
2130 // Avoid compiler warnings for unused parameters | |
2131 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2132 | |
2133 foreach (const AbstractMetaArgument *argument, arguments) { | |
2134 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
2135 } | |
2136 s << INDENT << default_return_statement_qt(java_function->type()) << ""; | |
2137 } else { | |
2138 writeFinalFunctionSetup(s, java_function, qt_object_name, cls); | |
2139 | |
2140 writeCodeInjections(s, java_function, java_function->implementingClass(), CodeSnip::Beginning, TypeSystem::NativeCode); | |
2141 | |
2142 if (java_function->isConstructor()) { | |
2143 writeFinalConstructor(s, java_function, qt_object_name, java_object_name); | |
2144 } else { | |
2145 | |
2146 QString function_prefix = ""; | |
2147 QStringList extra_param; | |
2148 Option option = NoOption; | |
2149 | |
2150 bool hasShell = cls->generateShellClass(); | |
2151 | |
2152 if (java_function->isFinalOverload()) { | |
2153 // no prefix | |
2154 } else if (java_function->isFinalInCpp() && !java_function->wasPublic() && hasShell) { | |
2155 function_prefix = "__public_"; | |
2156 } else if (!java_function->isFinalInCpp() && !java_function->isStatic() && hasShell) { | |
2157 function_prefix = "__override_"; | |
21 | 2158 extra_param.append("__do_static_call"); |
1 | 2159 s << INDENT |
21 | 2160 << "bool __do_static_call = __this_nativeId ? " |
2161 << "__" << java_class->name() << "_entity(__this_nativeId) : false;" << endl; | |
2162 } else { | |
1 | 2163 option = OriginalName; |
2164 } | |
2165 | |
2166 // Call the Qt function on the java object | |
2167 s << " "; | |
2168 if (has_function_type) { | |
2169 const QString qt_return_value = "__qt_return_value"; | |
2170 const QString java_return_value = "__d_return_value"; | |
2171 if (function_type) { | |
2172 writeTypeInfo(s, function_type, EnumAsInts); | |
2173 s << " " << qt_return_value | |
2174 << " = "; | |
2175 } | |
2176 | |
2177 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, | |
2178 extra_param); | |
2179 s << endl; | |
2180 | |
104 | 2181 writeRefArguments(s, java_function); |
2182 | |
1 | 2183 writeQtToJava(s, function_type, qt_return_value, java_return_value, |
2184 java_function, 0, EnumAsInts); | |
2185 | |
2186 // qtd s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) -> leaving: " | |
2187 // << java_function_signature << "\");"; | |
2188 | |
2189 if (function_type && function_type->name() != "QModelIndex") { | |
2190 if(function_type->typeEntry()->isStructInD()) | |
81 | 2191 ; //s << endl << INDENT << "return " << qt_return_value << ";"; |
1 | 2192 else if (!function_type->isTargetLangString() && !function_type->isContainer()) |
2193 s << endl << INDENT << "return " << java_return_value << ";"; | |
2194 } | |
2195 | |
2196 } else { | |
2197 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, | |
2198 extra_param); | |
104 | 2199 writeRefArguments(s, java_function); |
1 | 2200 } |
2201 } | |
2202 } | |
2203 if(!java_function->argumentReplaced(0).isEmpty()) { | |
2204 s << INDENT << "return 0;" << endl; | |
2205 } | |
2206 | |
2207 s << endl << "}"; | |
2208 s << endl << endl; | |
2209 } | |
2210 | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2211 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
|
2212 { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2213 AbstractMetaArgumentList arguments = java_function->arguments(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2214 foreach (const AbstractMetaArgument *argument, arguments) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2215 AbstractMetaType *d_type = argument->type(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2216 const TypeEntry *te = d_type->typeEntry(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2217 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
|
2218 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
|
2219 } |
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 |
1 | 2222 void CppImplGenerator::writeAssignment(QTextStream &s, const QString &destName, const QString &srcName, |
2223 const AbstractMetaType *java_type) | |
2224 { | |
2225 if (java_type->isArray()) { | |
2226 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2227 writeAssignment(s, destName + "[" + QString::number(i) + "]", | |
2228 srcName + "[" + QString::number(i) + "]", java_type->arrayElementType()); | |
2229 } | |
2230 } else { | |
2231 s << INDENT << destName << " = " << srcName << ";" << endl; | |
2232 } | |
2233 } | |
2234 | |
2235 void CppImplGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *java_field) | |
2236 { | |
2237 Q_ASSERT(java_field); | |
2238 Q_ASSERT(java_field->isPublic() || java_field->isProtected()); | |
2239 | |
2240 const AbstractMetaFunction *setter = java_field->setter(); | |
2241 const AbstractMetaFunction *getter = java_field->getter(); | |
2242 | |
2243 const AbstractMetaClass *cls = java_field->enclosingClass(); | |
2244 FieldModification mod = cls->typeEntry()->fieldModification(java_field->name()); | |
2245 | |
2246 if(notWrappedYet(getter)) | |
2247 return; | |
2248 | |
2249 if (mod.isReadable()) { | |
2250 // Getter | |
2251 if (getter->wasProtected()) { | |
2252 writeFunctionSignature(s, getter, getter->ownerClass()); | |
2253 s << endl | |
2254 << "{" << endl; | |
2255 { | |
2256 Indentation indent(INDENT); | |
2257 s << INDENT << "return " << java_field->name() << ";" << endl; | |
2258 } | |
2259 s << "}" << endl << endl; | |
2260 } | |
2261 | |
2262 | |
2263 writeFunctionName(s, getter, getter->ownerClass()); | |
2264 s << endl; | |
2265 writeFinalFunctionArguments(s, getter); | |
2266 s << "{" << endl; | |
2267 { | |
2268 Indentation indent(INDENT); | |
2269 | |
2270 | |
2271 writeFinalFunctionSetup(s, getter, "__qt_object", getter->ownerClass()); | |
2272 | |
2273 const QString java_return_value = "__d_return_value"; | |
2274 QString qt_return_value; | |
2275 if (setter->isStatic()) | |
2276 qt_return_value = shellClassName(setter->ownerClass()) + "::"; | |
2277 else | |
2278 qt_return_value = "__qt_object->"; | |
2279 | |
2280 | |
2281 // To avoid "taking address of tmp" | |
2282 s << INDENT; | |
2283 writeTypeInfo(s, getter->type(), Option(ArrayAsPointer)); | |
2284 QString tmp_name = "__tmp_" + getter->name(); | |
2285 s << tmp_name << " = "; | |
2286 | |
2287 if (getter->wasPublic()) | |
2288 qt_return_value += java_field->name(); | |
2289 else | |
2290 qt_return_value += getter->name() + "_getter()"; | |
2291 s << qt_return_value << ";" << endl; | |
2292 | |
2293 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
|
2294 if (getter->type()->isTargetLangString() || getter->type()->name() == "QModelIndex") |
1 | 2295 ; |
2296 else if(getter->type()->typeEntry()->isStructInD()) | |
81 | 2297 ; //s << INDENT << "return " << tmp_name << ";" << endl; |
1 | 2298 else |
2299 s << INDENT << "return " << java_return_value << ";" << endl; | |
2300 } | |
2301 s << "}" << endl << endl; | |
2302 } | |
2303 | |
2304 if(notWrappedYet(setter)) | |
2305 return; | |
2306 | |
2307 // Setter | |
2308 if (mod.isWritable() && !java_field->type()->isConstant()) { | |
2309 // Write public override for protected fields | |
2310 if (setter->wasProtected()) { | |
2311 writeFunctionSignature(s, setter, setter->ownerClass()); | |
2312 s << endl | |
2313 << "{" << endl; | |
2314 { | |
2315 Indentation indent(INDENT); | |
2316 | |
2317 Q_ASSERT(setter->arguments().count() > 0); | |
2318 const AbstractMetaArgument *argument = setter->arguments().at(0); | |
2319 | |
2320 QString thisRef = java_field->isStatic() | |
2321 ? setter->ownerClass()->qualifiedCppName() + QString("::") | |
2322 : QString("this->"); | |
2323 writeAssignment(s, thisRef + java_field->name(), argument->indexedName(), argument->type()); | |
2324 } | |
2325 s << "}" << endl << endl; | |
2326 } | |
2327 | |
2328 writeFunctionName(s, setter, setter->ownerClass()); | |
2329 s << endl; | |
2330 writeFinalFunctionArguments(s, setter); | |
2331 s << "{" << endl; | |
2332 | |
2333 { | |
2334 Indentation indent(INDENT); | |
2335 | |
2336 writeFinalFunctionSetup(s, setter, "__qt_object", setter->ownerClass()); | |
2337 | |
2338 Q_ASSERT(setter->arguments().count() == 1); | |
2339 const AbstractMetaArgument *argument = setter->arguments().at(0); | |
2340 | |
2341 QString dest; | |
2342 if (setter->isStatic()) | |
2343 dest = shellClassName(setter->ownerClass()) + "::"; | |
2344 else | |
2345 dest = "__qt_object->"; | |
2346 | |
2347 QString src; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2348 if (!argument->type()->isPrimitive() && !argument->type()->typeEntry()->isStructInD()) { |
1 | 2349 src = "__qt_" + argument->indexedName(); |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2350 } else if (argument->type()->name() == "QModelIndex") { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2351 src = "qtd_to_QModelIndex(" + argument->indexedName() + ")"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2352 } else |
1 | 2353 src = argument->indexedName(); |
2354 | |
2355 if (setter->wasPublic()) | |
2356 writeAssignment(s, dest + java_field->name(), src, argument->type()); | |
2357 else | |
2358 s << INDENT << dest << setter->name() << "_setter(" << src << ");" << endl; | |
2359 } | |
2360 s << "}" << endl << endl; | |
2361 } | |
2362 } | |
2363 | |
2364 void CppImplGenerator::writeFinalDestructor(QTextStream &s, const AbstractMetaClass *cls) | |
2365 { | |
2366 if (cls->hasConstructors()) { | |
2367 s << INDENT << "extern \"C\" DLL_PUBLIC void qtd_" << cls->name() << "_destructor(void *ptr)" << endl | |
2368 << INDENT << "{" << endl; | |
2369 { | |
2370 Indentation indent(INDENT); | |
2371 /* qtd | |
2372 if (!cls->isQObject() && !cls->generateShellClass()) { | |
2373 s << INDENT << "QtJambiLink *link = QtJambiLink::findLinkForUserObject(ptr);" << endl | |
2374 << INDENT << "if (link) link->resetObject(qtjambi_current_environment());" << endl; | |
2375 } | |
2376 | |
2377 // Code injectsions... | |
2378 foreach (const CodeSnip &snip, cls->typeEntry()->codeSnips()) { | |
2379 if (snip.language == TypeSystem::DestructorFunction) { | |
2380 s << snip.code(); | |
2381 } | |
2382 } | |
2383 */ | |
2384 s << INDENT << "delete (" << shellClassName(cls) << " *)ptr;" << endl; | |
2385 | |
2386 #if defined(QTJAMBI_DEBUG_TOOLS) | |
2387 s << INDENT << "qtjambi_increase_destructorFunctionCalledCount(QString::fromLatin1(\"" << cls->name() << "\"));" << endl; | |
2388 #endif | |
2389 } | |
2390 | |
2391 s << INDENT << "}" << endl << endl; | |
2392 } | |
2393 } | |
2394 | |
2395 void CppImplGenerator::writeFinalConstructor(QTextStream &s, | |
2396 const AbstractMetaFunction *java_function, | |
2397 const QString &qt_object_name, | |
2398 const QString &java_object_name) | |
2399 { | |
2400 const AbstractMetaClass *cls = java_function->ownerClass(); | |
2401 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2402 QString className = cls->typeEntry()->qualifiedCppName(); | |
2403 | |
2404 bool hasShellClass = cls->generateShellClass(); | |
2405 | |
2406 s << INDENT << shellClassName(cls) << " *" << qt_object_name | |
2407 << " = new " << shellClassName(cls) | |
2408 << "("; | |
2409 writeFunctionCallArguments(s, java_function, "__qt_"); | |
2410 s << ");" << endl; | |
2411 s << INDENT << "return (void *) " << qt_object_name << ";" << endl; // qtd | |
2412 | |
2413 /* qtd s << INDENT << "QtJambiLink *__qt_java_link = "; | |
2414 if (cls->isQObject()) { | |
2415 s << "qtjambi_construct_qobject(__jni_env, " << java_object_name << ", " | |
2416 << qt_object_name << ")"; | |
2417 } else { | |
2418 s << "qtjambi_construct_object(__jni_env, " << java_object_name << ", " << qt_object_name; | |
2419 if (cls->typeEntry()->isValue()) | |
2420 s << ", \"" << className << "\")"; | |
2421 else // non-QObject, object type | |
2422 s << ", QMetaType::Void, QLatin1String(\"" << cls->fullName().replace(".", "/") << "\"), true)"; | |
2423 } | |
2424 s << ";" << endl | |
2425 << INDENT << "if (!__qt_java_link) {" << endl; | |
2426 { | |
2427 Indentation indent(INDENT); | |
2428 s << INDENT << "qWarning(\"object construction failed for type: " | |
2429 << className << "\");" << endl | |
2430 << INDENT << "return;" << endl; | |
2431 } | |
2432 s << INDENT << "}" << endl; | |
2433 | |
2434 | |
2435 if (cls->isQObject()) { | |
2436 // Make sure all qobjects created by Java are owned by java only if | |
2437 // parent object has not been set. | |
2438 // All other objects will default to split ownership. | |
2439 s << INDENT << "if(!__qt_this->QObject::parent()){" << endl; | |
2440 s << INDENT << " __qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2441 s << INDENT << "}" << endl; | |
2442 } else { | |
2443 // All non-qobjects created by Java are owned by java | |
2444 s << INDENT << "__qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2445 } | |
2446 | |
2447 if (hasCustomDestructor(cls)) { | |
2448 s << INDENT << "__qt_java_link->setDestructorFunction(qtjambi_destructor);" << endl; | |
2449 } | |
2450 | |
2451 if (cls->typeEntry()->typeFlags() & ComplexTypeEntry::DeleteInMainThread) | |
2452 s << INDENT << "__qt_java_link->setDeleteInMainThread(true);" << endl; | |
2453 | |
2454 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions() && !cls->typeEntry()->isObject()) | |
2455 return; | |
2456 | |
2457 if (hasShellClass) { | |
2458 // Set up the link object | |
2459 s << INDENT << qt_object_name << "->m_link = __qt_java_link;" << endl; | |
2460 | |
2461 // Make sure the user data in the QObject has bindings to the qobject's meta object | |
2462 // (this has to be done after the link is set, so that the fake meta object | |
2463 // can access the java object, for which it gets a reference in the link) | |
2464 if (cls->isQObject()) | |
2465 s << INDENT << qt_object_name << "->m_link->setMetaObject(" << qt_object_name << "->metaObject());" << endl; | |
2466 | |
2467 s << INDENT << qt_object_name << "->m_link->setCreatedByJava(true);" << endl; | |
2468 | |
2469 | |
2470 | |
2471 AbstractMetaClassList interfaces = cls->interfaces(); | |
2472 if (interfaces.size() + (cls->baseClass() != 0 ? 1 : 0) > 1) { | |
2473 if (cls->baseClass() != 0) | |
2474 interfaces += cls->baseClass(); | |
2475 foreach (AbstractMetaClass *iface, interfaces) { | |
2476 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface; | |
2477 s << INDENT << qt_object_name << "->m_link->registerSubObject((" << impl->qualifiedCppName() << " *) " << qt_object_name << ");" << endl; | |
2478 } | |
2479 } | |
2480 } | |
2481 | |
2482 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions()) | |
2483 return; | |
2484 | |
2485 // Set up the vtable | |
2486 s << INDENT; | |
2487 QString space(24, ' '); | |
2488 if (hasShellClass) | |
2489 s << qt_object_name << "->m_vtable = "; | |
2490 s << "qtjambi_setup_vtable(__jni_env, " << endl << space << "__jni_object, " << endl; | |
2491 | |
2492 if (cls->hasInconsistentFunctions()) { | |
2493 s << space << "qtjambi_inconsistent_count, " << endl | |
2494 << space << "qtjambi_inconsistent_names, " << endl | |
2495 << space << "qtjambi_inconsistent_signatures, " << endl; | |
2496 } else { | |
2497 s << space << "0, 0, 0, // no inconsistent functions" << endl; | |
2498 } | |
2499 | |
2500 if (cls->hasVirtualFunctions()) { | |
2501 s << space << "qtjambi_method_count, " << endl | |
2502 << space << "qtjambi_method_names, " << endl | |
2503 << space << "qtjambi_method_signatures" << endl; | |
2504 } else { | |
2505 s << space << "0, 0, 0 // no virtual functions" << endl; | |
2506 } | |
2507 | |
2508 s << space << ");" << endl; */ | |
2509 } | |
2510 | |
2511 void CppImplGenerator::writeSignalInitialization(QTextStream &s, const AbstractMetaClass *java_class) | |
2512 { | |
2513 if (!java_class->isQObject() | |
2514 || java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromTargetLang).size() == 0) { | |
2515 return ; | |
2516 } | |
2517 | |
2518 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_signalInitialization", "jboolean") | |
2519 << endl << "(JNIEnv *__jni_env, jobject java_object, jlong ptr, jstring java_signal_name)" << endl | |
2520 << "{" << endl | |
2521 << " QtJambiLink *link = (QtJambiLink *) ptr;" << endl | |
2522 << " if (link == 0)" << endl | |
2523 << " return true;" << endl << endl | |
2524 << " QObject *qt_this = link->qobject();" << endl | |
2525 << " Q_ASSERT(qt_this);" << endl << endl | |
2526 << " QtJambi_SignalWrapper_" << java_class->name() << " *qt_wrapper = " | |
2527 << " (QtJambi_SignalWrapper_" << java_class->name() << " *) link->signalWrapper();" << endl | |
2528 << " if (qt_wrapper == 0) {" << endl | |
2529 << " qt_wrapper = new QtJambi_SignalWrapper_" << java_class->name() << ";" << endl | |
2530 << " link->setSignalWrapper(qt_wrapper);" << endl | |
2531 << " qt_wrapper->link = link;" << endl << endl | |
2532 << " qtjambi_resolve_signals(__jni_env," << endl | |
2533 << " java_object," << endl | |
2534 << " qt_wrapper->m_signals," << endl | |
2535 << " qtjambi_signal_count," << endl | |
2536 << " (char **) qtjambi_signal_names," << endl | |
2537 << " (int *) qtjambi_signal_argumentcounts);" << endl | |
2538 << " }" << endl | |
2539 << " QString signal_name = qtjambi_to_qstring(__jni_env, java_signal_name);" << endl | |
2540 << " return qtjambi_connect_cpp_to_java(__jni_env," << endl | |
2541 << " signal_name," << endl | |
2542 << " qt_this," << endl | |
2543 << " qt_wrapper," << endl | |
2544 << " QLatin1String(\"" << java_class->fullName() << "\")," << endl | |
2545 << " QLatin1String(\"" << signalWrapperPrefix() << "\"));" << endl | |
2546 << "}"; | |
2547 } | |
2548 | |
2549 QString CppImplGenerator::fromObject(const TypeEntry *entry, | |
2550 const QString &var_name) | |
2551 { | |
2552 QString returned; | |
2553 QString package = entry->javaPackage(); | |
2554 const ComplexTypeEntry *centry = entry->isComplex() | |
2555 ? static_cast<const ComplexTypeEntry *>(entry) | |
2556 : 0; | |
2557 | |
2558 if (centry == 0 || centry->polymorphicIdValue().isEmpty()) { | |
2559 /* qtd returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2560 + entry->lookupName() | |
2561 + "\", \"" + QString(package).replace(".", "/") + "/\", true);"; | |
2562 */ | |
2563 if(entry->isObject()) | |
2564 returned = var_name + ";"; | |
2565 else | |
2566 returned = "new " + entry->lookupName() + "(" + var_name +");"; | |
2567 } else { | |
2568 AbstractMetaClass *cls = classes().findClass(centry->qualifiedCppName()); | |
2569 if (!cls) { | |
2570 qFatal("CppImplGenerator::fromObject(): class '%s' could not be resolved...", | |
2571 qPrintable(centry->qualifiedCppName())); | |
2572 } | |
2573 | |
2574 while (cls != 0 && !cls->typeEntry()->isPolymorphicBase()) | |
2575 cls = cls->baseClass(); | |
2576 | |
2577 QString full_name; | |
2578 if (cls != 0) { | |
2579 full_name = cls->fullName(); | |
2580 } else { | |
2581 ReportHandler::warning(QString("class '%1' has polymorphic id but does not inherit a polymorphic class") | |
2582 .arg(centry->qualifiedCppName())); | |
2583 } | |
2584 /* qtd | |
2585 returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2586 + centry->lookupName() | |
2587 + "\", \"" + QString(package).replace(".", "/") + "/\"," | |
2588 + "\"" + jni_signature(full_name, Underscores) + "\", true); // fucking complex"; | |
2589 */ | |
2590 if(entry->isObject()) | |
2591 returned = var_name + "; // complex entry"; | |
2592 else | |
2593 returned = "new " + centry->lookupName() + "(" + var_name +"); // complex entry"; | |
2594 } | |
2595 | |
2596 return returned; | |
2597 } | |
2598 | |
2599 void CppImplGenerator::writeOriginalMetaObjectFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2600 { | |
2601 Q_ASSERT(java_class->isQObject()); | |
2602 | |
2603 s << jni_function_signature(java_class->package(), | |
2604 java_class->name(), | |
2605 "originalMetaObject", | |
2606 "jlong"); | |
2607 | |
2608 s << endl | |
2609 << "(JNIEnv *," << endl | |
2610 << " jclass)" << endl | |
2611 << "{" << endl; | |
2612 { | |
2613 Indentation indent(INDENT); | |
2614 s << INDENT << "return reinterpret_cast<jlong>(&" << java_class->qualifiedCppName() << "::staticMetaObject);" << endl; | |
2615 } | |
2616 s << "}" << endl << endl; | |
2617 } | |
2618 | |
2619 void CppImplGenerator::writeFromNativeFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2620 { | |
2621 s << jni_function_signature(java_class->package(), | |
2622 java_class->name(), | |
2623 "fromNativePointer", | |
2624 "jobject"); | |
2625 s << endl | |
2626 << "(JNIEnv *__jni_env," << endl | |
2627 << " jclass," << endl | |
2628 << " jobject nativePointer)" << endl | |
2629 << "{" << endl; | |
2630 { | |
2631 Indentation indent(INDENT); | |
2632 s << INDENT << "void *ptr = qtjambi_to_cpointer(__jni_env, nativePointer, 1);" << endl | |
2633 << INDENT << "return " << fromObject(java_class->typeEntry(), "ptr") << endl | |
2634 << "}" << endl; | |
2635 } | |
2636 } | |
2637 | |
2638 void CppImplGenerator::writeFromArrayFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2639 { | |
2640 s << jni_function_signature(java_class->package(), | |
2641 java_class->name(), | |
2642 "nativePointerArray", | |
2643 "jobject"); | |
2644 s << endl | |
2645 << "(JNIEnv *__jni_env," << endl | |
2646 << " jclass," << endl | |
2647 << " jobjectArray array)" << endl | |
2648 << "{" << endl; | |
2649 { | |
2650 Indentation indent(INDENT); | |
2651 s << INDENT << "return qtjambi_array_to_nativepointer(__jni_env, " << endl | |
2652 << INDENT << " array, " << endl | |
2653 << INDENT << " sizeof(" | |
2654 << java_class->qualifiedCppName() << "));" << endl; | |
2655 } | |
2656 s << "}" << endl; | |
2657 } | |
2658 | |
2659 | |
2660 void CppImplGenerator::writeInterfaceCastFunction(QTextStream &s, | |
2661 const AbstractMetaClass *java_class, | |
2662 const AbstractMetaClass *interface) | |
2663 { | |
2664 Q_ASSERT(interface->isInterface()); | |
2665 const InterfaceTypeEntry *ie = static_cast<const InterfaceTypeEntry *>(interface->typeEntry()); | |
2666 QString interface_name = ie->origin()->targetLangName(); | |
2667 | |
2668 s << endl | |
2669 << jni_function_signature(java_class->package(), | |
2670 java_class->name(), | |
2671 QString("__qt_cast_to_%1").arg(interface_name), | |
2672 "jlong", | |
2673 "__J"); | |
2674 | |
2675 s << endl | |
2676 << "(JNIEnv *," << endl | |
2677 << " jobject," << endl | |
2678 << " jlong ptr)" << endl | |
2679 << "{" << endl | |
2680 << " return (jlong) (" << interface->primaryInterfaceImplementor()->qualifiedCppName() << " *) " | |
2681 << "(" << java_class->qualifiedCppName() << " *) ptr;" << endl | |
2682 << "}" << endl; | |
2683 } | |
2684 | |
2685 bool CppImplGenerator::writeConversionRule(QTextStream &s, | |
2686 TypeSystem::Language target_language, | |
2687 const AbstractMetaFunction *java_function, | |
2688 int argument_index, | |
2689 const QString &qt_name, | |
2690 const QString &java_name) | |
2691 { | |
2692 if (argument_index < 0 || java_function == 0) | |
2693 return false; | |
2694 | |
2695 QString conversion_rule = java_function->conversionRule(target_language, argument_index); | |
2696 | |
2697 if (!conversion_rule.isEmpty()) { | |
2698 QString qt_name_var; | |
2699 QString java_name_var; | |
2700 | |
2701 if ((argument_index == 0 && target_language == TypeSystem::NativeCode) | |
2702 || (argument_index != 0 && target_language == TypeSystem::ShellCode)) { | |
2703 qt_name_var = "%in"; | |
2704 java_name_var = "%out"; | |
2705 } else { | |
2706 qt_name_var = "%out"; | |
2707 java_name_var = "%in"; | |
2708 } | |
2709 | |
2710 conversion_rule = conversion_rule.replace(qt_name_var, qt_name) | |
2711 .replace(java_name_var, java_name); | |
2712 | |
2713 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2714 for (int i=0; i<arguments.size(); ++i) { | |
2715 conversion_rule = conversion_rule.replace("%" + QString::number(i+1), | |
2716 arguments.at(i)->indexedName()); | |
2717 } | |
2718 | |
2719 QStringList lines = conversion_rule.split("\n"); | |
2720 foreach (QString line, lines) { | |
2721 s << INDENT << line.trimmed() << endl; | |
2722 } | |
2723 | |
2724 return true; | |
2725 } else { | |
2726 return false; | |
2727 } | |
2728 } | |
2729 | |
2730 | |
2731 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2732 const AbstractMetaClass *java_class, | |
2733 const AbstractMetaType *function_return_type, | |
2734 const QString &qt_name, | |
2735 const QString &java_name, | |
2736 const AbstractMetaFunction *java_function, | |
2737 int argument_index) | |
2738 { | |
2739 // Conversion to C++: Shell code for return values, native code for arguments | |
2740 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2741 if (writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2742 return; | |
2743 | |
2744 s << INDENT << shellClassName(java_class) << " *" << qt_name << " = (" | |
2745 << shellClassName(java_class) << " *) "; | |
2746 if (java_class->isQObject()) | |
2747 s << "qtjambi_to_qobject"; | |
2748 else | |
2749 s << "qtjambi_to_object"; | |
2750 s << "(__jni_env, " << java_name << ");" << endl; | |
2751 if (java_class->isQObject()) { | |
2752 // ### throw exceptions when objects are null... | |
2753 s << INDENT << "if (!" << qt_name << ") " | |
2754 << default_return_statement_java(function_return_type) << ";" << endl << endl; | |
2755 } | |
2756 } | |
2757 | |
2758 | |
2759 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2760 const AbstractMetaType *java_type, | |
2761 const QString &qt_name, | |
2762 const QString &java_name, | |
2763 const AbstractMetaFunction *java_function, | |
2764 int argument_index, | |
2765 Option options) | |
2766 { | |
2767 // Conversion to C++: Shell code for return values, native code for arguments | |
2768 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2769 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2770 return; | |
2771 | |
2772 if (java_type == 0) { | |
2773 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
2774 "language '%4'") | |
2775 .arg(argument_index) | |
2776 .arg(java_function->implementingClass()->name()) | |
2777 .arg(java_function->name()) | |
2778 .arg(int(lang)); | |
2779 ReportHandler::warning(warn); | |
2780 return; | |
2781 } | |
2782 if (java_type->name() == "QModelIndex") { | |
2783 s << INDENT << "QModelIndex " << qt_name << " = qtd_to_QModelIndex(" | |
2784 << java_name << ");" << endl; | |
2785 } else if (java_type->typeEntry()->isStructInD()) { | |
2786 // empty | |
2787 } else if (java_type->typeEntry() && java_type->typeEntry()->qualifiedCppName() == "QString") { | |
2788 s << INDENT << "QString " << qt_name | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2789 << " = " << QString("QString::fromUtf8((const char *)%1.ptr, %1.length);").arg(java_name) << endl; |
1 | 2790 } else if (java_type->isJObjectWrapper()) { |
2791 s << INDENT << "JObjectWrapper " << qt_name | |
2792 << " = qtjambi_to_jobjectwrapper(__jni_env, " << java_name << ");" << endl; | |
2793 } else if (java_type->isVariant()) { | |
2794 s << INDENT << "QVariant " << qt_name | |
2795 << " = " << java_name << " == NULL ? QVariant() : QVariant(*" << java_name << ");" << endl; | |
2796 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { | |
2797 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2798 | |
2799 // ### 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
|
2800 // s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; |
1 | 2801 s << INDENT; |
2802 writeTypeInfo(s, elementType); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2803 s << " *" << qt_name << " = ("; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2804 writeTypeInfo(s, elementType); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2805 s << "*) " << java_name << ";" << endl; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2806 /* |
1 | 2807 s << INDENT << "__jni_env->" << getXxxArrayRegion(elementType) << "( (" << translateType(java_type, options) |
2808 << ")" << java_name << ", 0, " << java_type->arrayElementCount() << ", " | |
2809 << "(" << translateType(elementType, options) << " *" << ")" | |
2810 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2811 */ |
1 | 2812 } else if (java_type->isArray()) { |
2813 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2814 | |
2815 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; | |
2816 writeTypeInfo(s, elementType); | |
2817 s << "[" << java_type->arrayElementCount() << "]" << qt_name << ";" << endl; | |
2818 | |
2819 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2820 writeJavaToQt(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
2821 "__jni_env->GetObjectArrayElement(" + java_name + ", " + QString::number(i) + ")", 0, -1, options); | |
2822 } | |
2823 | |
2824 } else if (java_type->isTargetLangString()) { | |
2825 s << INDENT << "QString " << qt_name | |
2826 << " = " << "QString::fromUtf8(" << java_name << ", " << java_name << "_size);" << endl; | |
2827 // qtd << " = qtjambi_to_qstring(__jni_env, (jstring) " << java_name << ");" << endl; | |
2828 | |
2829 } else if (java_type->isTargetLangChar()) { | |
2830 s << INDENT << "QChar " << qt_name | |
2831 << " = (ushort)" << java_name << ";" << endl; | |
2832 | |
2833 } else if (java_type->isEnum() || java_type->isFlags()) { | |
2834 | |
2835 bool written = false; | |
2836 if (java_type->isEnum()) { | |
2837 AbstractMetaEnum *java_enum = | |
2838 m_classes.findEnum(static_cast<const EnumTypeEntry *>(java_type->typeEntry())); | |
2839 if (java_enum && !java_enum->isPublic()) { | |
2840 | |
2841 s << INDENT << "int " << qt_name << " = "; | |
2842 written = true; | |
2843 } | |
2844 } | |
2845 | |
2846 if (!written) { | |
2847 QString qualified_name = java_type->typeEntry()->qualifiedCppName(); | |
2848 s << INDENT << qualified_name << " " << qt_name | |
2849 << " = (" << qualified_name << ") "; | |
2850 } | |
2851 | |
2852 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
|
2853 s << java_name << ";" << endl; |
1 | 2854 |
2855 } else if (options & BoxedPrimitive) { | |
2856 const PrimitiveTypeEntry *pentry = TypeDatabase::instance()->findTargetLangPrimitiveType("int"); | |
2857 Q_ASSERT(pentry); | |
2858 | |
2859 s << java_name << ";" << endl; | |
2860 | |
2861 } else { | |
2862 s << java_name << ';' << endl; | |
2863 } | |
2864 | |
2865 } else if (java_type->isContainer()) { | |
2866 writeJavaToQtContainer(s, java_type, qt_name, java_name, 0, -1); | |
2867 | |
2868 } else if (java_type->isThread()) { | |
2869 s << INDENT << "QThread *" << qt_name << " = qtjambi_to_thread(__jni_env, " << java_name | |
2870 << ");" << endl; | |
2871 | |
2872 } else if (java_type->typeEntry()->isCustom()) { | |
2873 const CustomTypeEntry *custom_type = | |
2874 static_cast<const CustomTypeEntry *>(java_type->typeEntry()); | |
2875 s << INDENT; | |
2876 custom_type->generateCppJavaToQt(s, java_type, "__jni_env", qt_name, java_name); | |
2877 s << ";" << endl; | |
2878 | |
2879 } else { | |
2880 | |
2881 const TypeEntry *type = java_type->typeEntry(); | |
2882 QString class_name = type->name(); | |
2883 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName()); | |
2884 | |
2885 // Declaration and the c-cast | |
2886 s << INDENT; | |
2887 writeTypeInfo(s, java_type); | |
2888 s << ' ' << qt_name << " = ("; | |
2889 writeTypeInfo(s, java_type); | |
2890 s << ") "; | |
2891 | |
2892 if (java_type->isPrimitive()) { | |
2893 if (options & BoxedPrimitive) { | |
2894 const PrimitiveTypeEntry *pentry = static_cast<const PrimitiveTypeEntry *>(type); | |
2895 //std::cout << "---error_here " << type->targetLangName().toStdString() << " \n"; | |
2896 //std::cout << "----func_here " << java_function->marshalledName().toStdString() << " \n"; | |
2897 | |
2898 if (!pentry->preferredConversion()) | |
2899 pentry = TypeDatabase::instance()->findTargetLangPrimitiveType(pentry->targetLangName()); | |
2900 Q_ASSERT(pentry); | |
2901 | |
2902 s << java_name << ";" << endl; | |
2903 | |
2904 } else if ((options & GlobalRefJObject) && type->jniName() == QLatin1String("jobject")) { | |
2905 s << "__jni_env->NewGlobalRef(" << java_name << ");" << endl; | |
2906 } else { | |
2907 s << java_name << ';' << endl; | |
2908 } | |
2909 | |
2910 #if 0 | |
2911 } else if (java_type->isEnum()) { | |
2912 s << "qtjambi_to_enum(__jni_env, " << java_name << ");" << endl; | |
2913 #endif | |
2914 | |
2915 } else if ((java_type->isQObject() || java_type->isObject()) | |
2916 && static_cast<const ObjectTypeEntry *>(type)->designatedInterface()) { | |
2917 /* qtd const InterfaceTypeEntry *ie = | |
2918 static_cast<const ObjectTypeEntry *>(type)->designatedInterface(); | |
2919 s << "qtjambi_to_interface(__jni_env, "; | |
2920 | |
2921 // This cast is only valid if we're dealing with a native id | |
2922 if ((options & UseNativeIds) == UseNativeIds) | |
2923 s << "(QtJambiLink *)"; | |
2924 */ | |
2925 s << java_name << ";" << endl; | |
2926 /* qtd | |
2927 s << "\"" << ie->targetLangName() << "\", \"" | |
2928 << ie->javaPackage().replace(".", "/") << "/\", " | |
2929 << "\"__qt_cast_to_" << type->targetLangName() << "\");" << endl; | |
2930 */ | |
2931 } else if (java_type->isObject() || java_type->isQObject() || java_type->isNativePointer()) { | |
2932 if (java_type->isReference()) { | |
2933 s << "* (" << qualified_class_name << " " | |
2934 << QString(java_type->actualIndirections(), '*') << ") "; | |
2935 } | |
2936 | |
2937 if (java_type->isNativePointer()) { | |
2938 /* qtd s << "qtjambi_to_cpointer(" | |
2939 << "__jni_env, " | |
2940 << java_name << ", " | |
2941 << java_type->actualIndirections() << ");" << endl; */ | |
2942 s << java_name << ";" << endl; // qtd | |
2943 }/* qtd else if (java_type->isQObject()) { | |
2944 if ((options & UseNativeIds) == 0) | |
2945 s << "qtjambi_to_qobject(__jni_env, "; | |
2946 else | |
2947 s << "qtjambi_from_jlong("; | |
2948 s << java_name; | |
2949 s << ");" << endl; | |
2950 }*/ else { | |
2951 /* qtd if ((options & UseNativeIds) == 0) | |
2952 s << "qtjambi_to_object(__jni_env, "; | |
2953 else | |
2954 s << "qtjambi_from_jlong("; | |
2955 */ s << java_name; | |
2956 s << ";" << endl; // + | |
2957 // qtd s << ");" << endl; | |
2958 } | |
2959 | |
2960 } else { | |
2961 // Return values... | |
2962 if (argument_index == 0) { | |
2963 s << "(" << java_name << " != 0 ? *(" << qualified_class_name << " *)"; | |
2964 /* qtd if ((options & UseNativeIds) == 0) | |
2965 s << "qtjambi_to_object(__jni_env, "; | |
2966 else | |
2967 s << "qtjambi_from_jlong("; | |
2968 */ s << java_name; | |
2969 s << " : " << qualified_class_name << "());" << endl; | |
2970 } else { | |
2971 s << "*" | |
2972 << "(" << qualified_class_name << " *)"; | |
2973 bool null_check = false; | |
2974 /* qtd if ((options & UseNativeIds) == 0) { | |
2975 s << "qtjambi_to_object(__jni_env, "; | |
2976 } else if (hasDefaultConstructor(java_type)) { | |
2977 null_check = true; | |
2978 s << "(" << java_name << " != 0 ? qtjambi_from_jlong("; | |
2979 } else { | |
2980 s << "qtjambi_from_jlong("; | |
2981 } | |
2982 */ s << java_name; | |
2983 // qtd s << ")"; | |
2984 | |
2985 if (null_check) | |
2986 s << " : default_" << QString(qualified_class_name).replace("::", "_") << "())"; | |
2987 s << ";" << endl; | |
2988 } | |
2989 | |
2990 } | |
2991 } | |
2992 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl; | |
2993 } | |
2994 | |
2995 void CppImplGenerator::writeQtToJava(QTextStream &s, | |
2996 const AbstractMetaType *java_type, | |
2997 const QString &qt_name, | |
2998 const QString &java_name, | |
2999 const AbstractMetaFunction *java_function, | |
3000 int argument_index, | |
3001 Option option) | |
3002 { | |
3003 | |
3004 // Conversion to Java: Native code for return values, shell code for arguments | |
3005 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode; | |
3006 /* qtd if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3007 return; | |
3008 */ | |
3009 if (java_type == 0) { | |
3010 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3011 "language '%4'") | |
3012 .arg(argument_index) | |
3013 .arg(java_function->implementingClass()->name()) | |
3014 .arg(java_function->name()) | |
3015 .arg(int(lang)); | |
3016 ReportHandler::warning(warn); | |
3017 return; | |
3018 } | |
3019 | |
3020 if (java_type->name() == "QModelIndex") { | |
3021 QString prefix = "*"; | |
3022 if (option & BoxedPrimitive) | |
3023 s << INDENT << "QModelIndexAccessor tmp_index = qtd_from_QModelIndex(" << qt_name << ");" << endl | |
3024 << INDENT << "QModelIndexAccessor *" << java_name << " = &tmp_index;" << endl; | |
3025 else | |
3026 s << INDENT << "*" << java_name << " = qtd_from_QModelIndex(" << qt_name << ");" << endl; | |
3027 | |
81 | 3028 } else if (java_type->typeEntry()->isStructInD()) { |
3029 if (option & BoxedPrimitive) { | |
3030 s << INDENT << java_type->typeEntry()->name() << " *" << java_name << " = (" | |
3031 << java_type->typeEntry()->name() << " *) &" << qt_name << ";" << endl; | |
3032 } else { | |
3033 s << INDENT << "*" << java_name << " = " << qt_name << ";" << endl; | |
3034 } | |
1 | 3035 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { |
3036 AbstractMetaType *elementType = java_type->arrayElementType(); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3037 /* qtd |
1 | 3038 s << INDENT << translateType(java_type, option) << " " << java_name << " = __jni_env->" << newXxxArray(elementType) |
3039 << "(" << java_type->arrayElementCount() << ");" << endl; | |
3040 | |
3041 s << INDENT << "__jni_env->" << setXxxArrayRegion(elementType) << "(" | |
3042 << "(" << translateType(java_type, option) << ")" << java_name | |
3043 << ", 0, " << java_type->arrayElementCount() << ", " | |
3044 << "(" << translateType(elementType, option) << " *" << ")" | |
3045 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3046 */ |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3047 s << INDENT << translateType(java_type, option) << " " << java_name << " = " << qt_name << ";" <<endl; |
1 | 3048 } else if (java_type->isArray()) { |
3049 AbstractMetaType *elementType = java_type->arrayElementType(); | |
3050 | |
3051 s << INDENT << "jobject " << java_name << " = __jni_env->NewObjectArray(" | |
3052 << java_type->arrayElementCount() << ");" << endl; | |
3053 | |
3054 s << "jobject __qt_element = 0;"; | |
3055 | |
3056 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
3057 writeQtToJava(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
3058 "__qt_element", 0, -1, option); | |
3059 s << "__jni_env->SetObjectArrayElement((jobjectArray) " << java_name << ", " | |
3060 << i << ", __qt_element);" << endl; | |
3061 } | |
3062 | |
3063 } else if (java_type->isPrimitive()) { | |
3064 const PrimitiveTypeEntry *type = | |
3065 static_cast<const PrimitiveTypeEntry *>(java_type->typeEntry()); | |
3066 | |
3067 Q_ASSERT(type); | |
3068 QString ret_val; | |
3069 if (java_function) | |
3070 ret_val = jniReturnName(java_function); | |
3071 else | |
3072 ret_val = fixCppTypeName(java_type->typeEntry()->qualifiedCppName()); | |
3073 s << INDENT << ret_val << " " << java_name << " = " << qt_name << ";" << endl; | |
3074 } else if (java_type->isJObjectWrapper()) { | |
3075 s << INDENT << "jobject " << java_name << " = qtjambi_from_jobjectwrapper(__jni_env, " | |
3076 << qt_name << ");" << endl; | |
3077 } else if (java_type->isVariant()) { | |
3078 s << INDENT << "QVariant *" << java_name | |
3079 << " = new QVariant(" << qt_name << ");" << endl; | |
3080 } else if (java_type->isTargetLangString()) { | |
35 | 3081 if(java_type->typeEntry()->qualifiedCppName() == "QStringRef") { |
3082 s << INDENT << "const QString *str_ref = " << qt_name << ".string();" << endl | |
3083 << INDENT << "if(str_ref)" << endl | |
3084 << INDENT << " _d_toUtf8(str_ref->utf16(), str_ref->size(), " << java_name << ");" << endl | |
3085 << INDENT << "else {" << endl | |
3086 << INDENT << " QString empty_str;" << endl | |
3087 << INDENT << " _d_toUtf8(empty_str.utf16(), empty_str.size(), " << java_name << ");" << endl | |
3088 << INDENT << "}" << endl; | |
3089 } else { | |
3090 s << INDENT << QString("_d_toUtf8(%1.utf16(), %1.size(), %2);").arg(qt_name, java_name) << endl; | |
3091 } | |
1 | 3092 } else if (java_type->isTargetLangChar()) { |
3093 s << INDENT << "jchar " << java_name << " = " << qt_name << ".unicode();" << endl; | |
3094 | |
3095 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags() | |
3096 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) { | |
3097 // } else if (java_type->isEnum() || java_type->isFlags()) { | |
3098 | |
3099 // if (option & EnumAsInts) { | |
3100 // qDebug() << java_type->name() << "should be int..."; | |
3101 // } | |
3102 | |
3103 /* if (option & BoxedPrimitive) { | |
3104 s << INDENT << "jobject " << java_name << " = qtjambi_from_int(__jni_env, " | |
3105 << qt_name << ");" << endl; | |
3106 } else */{ | |
3107 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl; | |
3108 } | |
3109 | |
3110 } else if (java_type->isTargetLangEnum()) { | |
3111 Q_ASSERT((option & EnumAsInts) == 0); | |
3112 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
3113 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl; | |
3114 | |
3115 } else if (java_type->isTargetLangFlags()) { | |
3116 Q_ASSERT((option & EnumAsInts) == 0); | |
3117 const FlagsTypeEntry *ft = static_cast<const FlagsTypeEntry *>(java_type->typeEntry()); | |
3118 s << INDENT << "jobject " << java_name << " = qtjambi_from_flags(__jni_env, " | |
3119 << qt_name << ", \"" << ft->javaPackage().replace('.', '/') << '/' | |
3120 << ft->originator()->javaQualifier() << '$' << ft->targetLangName() << "\");" << endl; | |
3121 | |
3122 } else if (java_type->isContainer()) { | |
3123 writeQtToJavaContainer(s, java_type, qt_name, java_name, 0, -1); | |
3124 | |
3125 } else if (java_type->isThread()) { | |
3126 s << INDENT << "jobject " << java_name << " = qtjambi_from_thread(__jni_env, " << qt_name | |
3127 << ");" << endl; | |
3128 | |
3129 } else if (!java_type->isNativePointer() && java_type->typeEntry()->isCustom()) { | |
3130 s << INDENT; | |
3131 static_cast<const CustomTypeEntry *>(java_type->typeEntry()) | |
3132 ->generateCppQtToJava(s, java_type, "__jni_env", qt_name, java_name); | |
3133 s << ";" << endl; | |
3134 | |
3135 } else { | |
3136 QString return_type; | |
3137 if (java_function) | |
3138 return_type = jniReturnName(java_function); | |
3139 else { | |
3140 return_type = jniReturnType(java_type); | |
3141 return_type = fixCppTypeName(return_type); | |
3142 // return_type = fixCppTypeName(java_type->typeEntry()->qualifiedCppName()); | |
3143 } | |
3144 /* if( (java_type->isValue() && !java_type->typeEntry()->isStructInD()) | |
3145 || java_type->isObject() ) | |
3146 s << INDENT << return_type << " *" << java_name << " = (" << return_type << "*) "; | |
3147 else*/ | |
3148 s << INDENT << return_type << " " << java_name << " = (" << return_type << ") "; | |
3149 | |
3150 if (java_type->isQObject()) { | |
3151 /* qtd s << "qtjambi_from_qobject(__jni_env, " << "(QObject *) "; | |
3152 | |
3153 if (java_type->isReference() && java_type->indirections() == 0) | |
3154 s << "&"; | |
3155 | |
3156 s << qt_name | |
3157 << ", \"" << java_type->typeEntry()->lookupName() << "\"" | |
3158 << ", \"" << java_type->package().replace(".", "/") << "/\"" | |
3159 << ");" << endl; | |
3160 */ | |
3161 s << qt_name << ";" << endl; | |
3162 | |
3163 #if 0 | |
3164 } else if (java_type->isEnum()) { | |
3165 | |
3166 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
3167 s << "qtjambi_from_enum(__jni_env, " << qt_name << ", \"" | |
3168 << et->javaQualifier() << "$" << et->targetLangName() << "\");" << endl; | |
3169 #endif | |
3170 } else if (java_type->isNativePointer()) { | |
3171 /* qtd s << "qtjambi_from_cpointer(__jni_env, "; | |
3172 if (java_type->isReference()) | |
3173 s << "&"; | |
3174 s << qt_name << ", " << nativePointerType(java_type) << ", " | |
3175 << java_type->actualIndirections() << ");" << endl; | |
3176 */ | |
3177 if (java_type->isReference()) | |
3178 s << "&"; | |
3179 s << qt_name << ";" << "// qtjambi_from_cpointer" << endl; | |
3180 } else if (java_type->isValue()) { | |
3181 // qtd s << fromObject(java_type->typeEntry(), "&" + qt_name) << endl; | |
3182 s << "new " << java_type->typeEntry()->qualifiedCppName() << "(" << qt_name << ");" << endl; | |
3183 } else { | |
3184 // qtd s << fromObject(java_type->typeEntry(), | |
3185 // qtd (java_type->isReference() ? "&" : "") + qt_name) << endl; | |
3186 s << qt_name << ";" << endl; | |
3187 } | |
3188 } | |
3189 | |
3190 } | |
3191 | |
3192 QString CppImplGenerator::getTypeName(const TypeEntry *entry, Option option) | |
3193 { | |
3194 if(entry->isEnum() && (option & EnumAsInts)) | |
3195 return "int"; | |
3196 | |
3197 return entry->lookupName(); | |
3198 } | |
3199 | |
3200 void CppImplGenerator::writeQtToJavaContainer(QTextStream &s, | |
3201 const AbstractMetaType *java_type, | |
3202 const QString &qt_name, | |
3203 const QString &java_name, | |
3204 const AbstractMetaFunction *java_function, | |
3205 int argument_index) | |
3206 { | |
3207 // Language for conversion to Java: Native code for return values and Shell code for arguments | |
3208 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode; | |
3209 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3210 return; | |
3211 | |
3212 if (java_type == 0) { | |
3213 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3214 "language '%4'") | |
3215 .arg(argument_index) | |
3216 .arg(java_function->implementingClass()->name()) | |
3217 .arg(java_function->name()) | |
3218 .arg(int(lang)); | |
3219 ReportHandler::warning(warn); | |
3220 return; | |
3221 } | |
3222 | |
3223 Q_ASSERT(java_type->isContainer()); | |
3224 const ContainerTypeEntry *type = | |
3225 static_cast<const ContainerTypeEntry *>(java_type->typeEntry()); | |
3226 | |
3227 if (type->type() == ContainerTypeEntry::ListContainer | |
3228 || type->type() == ContainerTypeEntry::VectorContainer | |
3229 || type->type() == ContainerTypeEntry::StringListContainer | |
3230 || type->type() == ContainerTypeEntry::LinkedListContainer | |
3231 || type->type() == ContainerTypeEntry::StackContainer | |
3232 || type->type() == ContainerTypeEntry::SetContainer | |
3233 || type->type() == ContainerTypeEntry::QueueContainer) { | |
3234 | |
3235 Q_ASSERT(java_type->instantiations().size() == 1); | |
3236 AbstractMetaType *targ = java_type->instantiations().first(); | |
3237 | |
3238 QString cls_name = getTypeName(targ->typeEntry(), EnumAsInts); | |
3239 cls_name.remove("_ConcreteWrapper"); | |
3240 | |
3241 s << endl | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3242 // << INDENT << "{" << endl // qtd2 hack, additional scope for avoiding duplicating of "i" |
1 | 3243 << INDENT; |
3244 | |
3245 switch (type->type()) { | |
3246 case ContainerTypeEntry::LinkedListContainer: | |
3247 case ContainerTypeEntry::QueueContainer: | |
3248 s << "qtjambi_linkedlist_new(__jni_env)"; | |
3249 break; | |
3250 case ContainerTypeEntry::StackContainer: | |
3251 s << "qtjambi_stack_new(__jni_env)"; | |
3252 break; | |
3253 case ContainerTypeEntry::SetContainer: | |
3254 s << "qtjambi_hashset_new(__jni_env)"; | |
3255 break; | |
3256 default: | |
3257 s << "qtd_allocate_" << cls_name | |
3258 << "_array(" << java_name << ", " << qt_name << ".size())"; | |
3259 break; | |
3260 } | |
3261 | |
3262 s << ";" << endl | |
3263 << INDENT; | |
3264 | |
3265 | |
3266 writeTypeInfo(s, java_type, ForceValueType); | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3267 QString index = "i_" + qt_name; |
1 | 3268 s << "::const_iterator " << qt_name << "_end_it = " << qt_name << ".constEnd();" << endl |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3269 << INDENT << QString("int %0 = 0;").arg(index) << endl |
1 | 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()) | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3282 s << INDENT << "void *__java_tmp = qtd_string_from_array(" << java_name << ", " << index << ");" << endl; |
1 | 3283 |
3284 writeQtToJava(s, targ, "__qt_tmp", "__java_tmp", 0, -1, BoxedPrimitive); | |
3285 | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3286 s << INDENT << "qtd_assign_" << cls_name << "_array_element(" << java_name << ", " << index << ", __java_tmp);" << endl; |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3287 s << INDENT << "++" << index << ";" << endl; |
1 | 3288 } |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3289 s << INDENT << "}" << endl; |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
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()) { |
190 | 3719 if (java_type->typeEntry()->isStructInD() && dVersion == 2 && java_type->isConstant() && d_export) |
3720 return "const(" + d_name + QString(java_type->indirections()-1, '*') + ")*"; | |
3721 else if (java_type->typeEntry()->isValue()) | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3722 return "void*"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3723 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
|
3724 return "int" + QString(java_type->indirections(), '*'); |
188 | 3725 else { |
3726 if (java_type->isConstant() && dVersion == 2 && d_export) | |
190 | 3727 return "const (" + d_name + QString(java_type->indirections(), '*') + ")"; |
188 | 3728 else |
3729 return d_name + QString(java_type->indirections(), '*'); | |
3730 } | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3731 } else { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3732 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
|
3733 } |
81 | 3734 } |
1 | 3735 |
3736 void CppImplGenerator::writeExtraIncludes(QTextStream &s, const AbstractMetaClass *java_class) | |
3737 { | |
3738 IncludeList includes = java_class->typeEntry()->extraIncludes(); | |
3739 qSort(includes.begin(), includes.end()); | |
3740 | |
3741 int used = 0; | |
3742 foreach (const Include &i, includes) { | |
3743 if (i.type != Include::TargetLangImport) { | |
3744 s << i.toString() << endl; | |
3745 ++used; | |
3746 } | |
3747 } | |
3748 | |
3749 | |
3750 if (used) | |
3751 s << endl; | |
3752 | |
3753 } | |
3754 | |
3755 | |
3756 void CppImplGenerator::writeDefaultConstructedValues_helper(QSet<QString> &values, | |
3757 const AbstractMetaFunction *func) | |
3758 { | |
3759 foreach (AbstractMetaArgument *arg, func->arguments()) { | |
3760 AbstractMetaType *type = arg->type(); | |
3761 if (func->typeReplaced(arg->argumentIndex()+1).isEmpty() && type->isValue() && hasDefaultConstructor(type)) | |
3762 values << type->typeEntry()->qualifiedCppName(); | |
3763 } | |
3764 } | |
3765 | |
3766 | |
3767 void CppImplGenerator::writeDefaultConstructedValues(QTextStream &s, const AbstractMetaClass *java_class) { | |
3768 | |
3769 QSet<QString> values; | |
3770 | |
3771 // Class functions, more or less copied from the logic in write(Class) above... | |
3772 AbstractMetaFunctionList class_funcs; | |
3773 | |
3774 // Add normal final functions | |
3775 foreach (AbstractMetaFunction *function, java_class->functionsInTargetLang()) { | |
3776 if (!function->isEmptyFunction()) | |
3777 class_funcs << function; | |
3778 } | |
3779 | |
3780 // Add abstract functions, I think... | |
3781 foreach (AbstractMetaFunction *function, java_class->queryFunctions(AbstractMetaClass::NormalFunctions | |
3782 | AbstractMetaClass::AbstractFunctions | |
3783 | AbstractMetaClass::NotRemovedFromTargetLang)) { | |
3784 if (function->implementingClass() != java_class) | |
3785 class_funcs << function; | |
3786 } | |
3787 | |
3788 // Signals (their c++ wrapper calls actually...) | |
3789 class_funcs += java_class->queryFunctions(AbstractMetaClass::Signals); | |
3790 | |
3791 // | |
3792 foreach (AbstractMetaFunction *f, class_funcs) { | |
3793 writeDefaultConstructedValues_helper(values, f); | |
3794 } | |
3795 | |
3796 foreach (AbstractMetaField *field, java_class->fields()) { | |
3797 writeDefaultConstructedValues_helper(values, field->setter()); | |
3798 } | |
3799 | |
3800 if (!values.isEmpty()) { | |
3801 s << endl << endl | |
3802 << "// Default constructed values used throughout final functions..." << endl; | |
3803 for (QSet<QString>::const_iterator it = values.constBegin(); it != values.constEnd(); ++it) { | |
3804 s << "Q_GLOBAL_STATIC(" << *it << ", default_" << QString(*it).replace("::", "_") | |
3805 << ");" << endl; | |
3806 } | |
3807 s << endl << endl; | |
3808 } | |
3809 } |