Mercurial > projects > qtd
annotate generator/cppheadergenerator.cpp @ 310:5bcfe9e7db7f lifetime
Committing local changes
author | maxter <spambox@d-coding.com> |
---|---|
date | Wed, 23 Dec 2009 16:10:46 +0200 |
parents | b5773ccab07d |
children |
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 "cppheadergenerator.h" | |
43 | |
44 #include <QtCore/QDir> | |
45 | |
46 #include <qdebug.h> | |
259 | 47 #include <iostream> |
1 | 48 |
49 QString CppHeaderGenerator::fileNameForClass(const AbstractMetaClass *java_class) const | |
50 { | |
51 return QString("%1_shell.h").arg(java_class->name()); | |
52 } | |
53 | |
54 void CppHeaderGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *java_field) | |
55 { | |
56 Q_ASSERT(java_field->isProtected()); | |
57 | |
58 const AbstractMetaFunction *setter = java_field->setter(); | |
59 const AbstractMetaFunction *getter = java_field->getter(); | |
60 | |
61 // qtd2 | |
62 if(notWrappedYet(getter)) | |
63 return; | |
64 | |
65 if (getter->isModifiedRemoved(TypeSystem::ShellCode)) | |
66 return; | |
67 | |
68 s << " "; | |
69 writeFunctionSignature(s, getter, 0, QString(), Option(ShowStatic)); | |
70 s << ";" << endl; | |
71 | |
72 // qtd2 | |
73 if(notWrappedYet(setter)) | |
74 return; | |
75 | |
76 if (!java_field->type()->isConstant()) { | |
77 if (setter->isModifiedRemoved(TypeSystem::ShellCode)) | |
78 return; | |
79 | |
80 s << " "; | |
81 writeFunctionSignature(s, setter, 0, QString(), Option(ShowStatic)); | |
82 s << ";" << endl; | |
83 } | |
84 } | |
85 | |
86 void CppHeaderGenerator::writeSignalWrapper(QTextStream &s, const AbstractMetaFunction *signal) | |
87 { | |
88 s << " "; | |
89 writeFunctionSignature(s, signal, 0, signalWrapperPrefix(), | |
90 Option(NormalizeAndFixTypeSignature | OriginalName | OriginalTypeDescription | IncludeDefaultExpression)); | |
91 s << ";" << endl; | |
92 } | |
93 | |
94 void CppHeaderGenerator::writeSignalWrappers(QTextStream &s, const AbstractMetaClass *java_class) | |
95 { | |
96 AbstractMetaFunctionList signal_funcs = signalFunctions(java_class); | |
97 if (signal_funcs.size() > 0) { | |
98 s << endl << "public slots:" << endl; | |
99 foreach (const AbstractMetaFunction *signal, signal_funcs) { | |
100 writeSignalWrapper(s, signal); | |
101 } | |
102 } | |
103 } | |
104 | |
105 void CppHeaderGenerator::writeWrapperClass(QTextStream &s, const AbstractMetaClass *java_class) | |
106 { | |
107 AbstractMetaFunctionList signal_functions = | |
108 java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromTargetLang); | |
109 if (signal_functions.size() == 0) | |
110 return ; | |
111 | |
112 s << "class QtJambi_SignalWrapper_" << java_class->name() << ": public QObject" << endl | |
113 << "{" << endl | |
114 << " Q_OBJECT" << endl; | |
115 writeSignalWrappers(s, java_class); | |
116 s << endl << "public:" << endl | |
117 << " QtJambiSignalInfo m_signals[" << signal_functions.size() << "];" << endl | |
118 << " QtJambiLink *link;" << endl | |
119 << "};" << endl << endl; | |
120 } | |
121 | |
122 void CppHeaderGenerator::write(QTextStream &s, const AbstractMetaClass *java_class) | |
123 { | |
124 QString include_block = java_class->name().toUpper() + "_SHELL_H"; | |
125 | |
126 s << "#ifndef " << include_block << endl | |
127 << "#define " << include_block << endl << endl | |
128 // << "#include <qtjambi_core.h>" << endl | |
129 << "#include <QtCore/QHash>" << endl | |
257 | 130 << "#include <qtd_core.h>" << endl; |
1 | 131 |
132 Include inc = java_class->typeEntry()->include(); | |
133 s << "#include "; | |
134 if (inc.type == Include::IncludePath) | |
135 s << "<"; | |
136 else | |
137 s << "\""; | |
138 s << inc.name; | |
139 if (inc.type == Include::IncludePath) | |
140 s << ">"; | |
141 else | |
142 s << "\""; | |
143 s << endl << endl; | |
144 | |
145 IncludeList list = java_class->typeEntry()->extraIncludes(); | |
146 qSort(list.begin(), list.end()); | |
147 foreach (const Include &inc, list) { | |
148 if (inc.type == Include::TargetLangImport) | |
149 continue; | |
150 | |
151 s << "#include "; | |
152 if (inc.type == Include::LocalPath) | |
153 s << "\""; | |
154 else | |
155 s << "<"; | |
156 | |
157 s << inc.name; | |
158 | |
159 if (inc.type == Include::LocalPath) | |
160 s << "\""; | |
161 else | |
162 s << ">"; | |
163 | |
164 s << endl; | |
165 } | |
166 | |
167 /* qtd writeForwardDeclareSection(s, java_class); | |
168 | |
169 writeWrapperClass(s, java_class); | |
170 */ | |
171 s << endl; | |
238 | 172 |
1 | 173 |
174 if (!java_class->generateShellClass()) { | |
175 s << "#endif" << endl << endl; | |
238 | 176 priGenerator->addHeader(java_class->package(), fileNameForClass(java_class)); |
1 | 177 return ; |
178 } | |
179 | |
180 s << "class " << shellClassName(java_class) | |
181 << " : public " << java_class->qualifiedCppName(); | |
254 | 182 if (java_class->isQObject()) |
183 s << ", public QtD_QObjectEntity"; | |
260 | 184 else if (java_class->isPolymorphic()) |
254 | 185 s << ", public QtD_Entity"; |
1 | 186 s << endl << "{" << endl; |
187 | |
188 if (java_class->isQObject()) { | |
189 s << "public:" << endl | |
254 | 190 << " Q_OBJECT_CHECK" << endl |
1 | 191 // << " mutable const QMetaObject *m_meta_object;" << endl; |
192 | |
193 /* if (java_class->hasVirtualSlots()) { | |
194 s << " mutable QHash<int,int> m_map;" << endl; | |
195 } | |
196 */ | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
21
diff
changeset
|
197 // s << " const QMetaObject *metaObject() const;" << endl |
1 | 198 // << " void *qt_metacast(const char *);" << endl |
254 | 199 // << " QT_TR_FUNCTIONS" << end |
200 << " virtual int qt_metacall(QMetaObject::Call, int, void **);" << endl | |
201 << "private:" << endl; | |
1 | 202 } |
203 | |
204 | |
205 s << "public:" << endl; | |
206 // constructor | |
207 foreach (const AbstractMetaFunction *function, java_class->functions()) { | |
208 if (function->isConstructor() && !function->isPrivate()) | |
209 writeFunction(s, function); | |
210 } | |
257 | 211 |
260 | 212 if (java_class->isPolymorphic()) |
213 s << " ~" << shellClassName(java_class) << "();" << endl << endl; | |
257 | 214 |
1 | 215 // All functions in original class that should be reimplemented in shell class |
216 AbstractMetaFunctionList shell_functions = java_class->functionsInShellClass(); | |
217 foreach (const AbstractMetaFunction *function, shell_functions) { | |
218 if(notWrappedYet(function)) | |
219 continue; | |
220 writeFunction(s, function); | |
221 } | |
222 | |
223 // Public call throughs for protected functions | |
224 AbstractMetaFunctionList public_overrides = java_class->publicOverrideFunctions(); | |
225 foreach (const AbstractMetaFunction *function, public_overrides) { | |
226 if(notWrappedYet(function)) | |
227 continue; | |
228 writePublicFunctionOverride(s, function); | |
229 } | |
230 | |
231 // Override all virtual functions to get the decision on static/virtual call | |
232 AbstractMetaFunctionList virtual_functions = java_class->virtualOverrideFunctions(); | |
233 foreach (const AbstractMetaFunction *function, virtual_functions) { | |
234 if(notWrappedYet(function)) | |
235 continue; | |
236 | |
237 writeVirtualFunctionOverride(s, function); | |
238 } | |
239 | |
240 // Field accessors | |
241 foreach (const AbstractMetaField *field, java_class->fields()) { | |
242 if (field->isProtected()) | |
243 writeFieldAccessors(s, field); | |
244 } | |
245 /* qtd | |
246 writeVariablesSection(s, java_class); | |
247 */ | |
248 writeInjectedCode(s, java_class); | |
249 | |
260 | 250 s << "};" << endl << endl; |
251 | |
252 if (!java_class->isQObject() && java_class->isPolymorphic() && java_class->baseClass()) | |
310 | 253 s << "extern \"C\" DLL_PUBLIC void* qtd_" << java_class->polymorphicBase()->name() << "_dId(void *nativeId);" << endl; |
260 | 254 |
255 s << "#endif // " << include_block << endl; | |
1 | 256 |
238 | 257 priGenerator->addHeader(java_class->package(), fileNameForClass(java_class)); |
258 priGenerator->addClass(java_class->package(), java_class->name()); | |
1 | 259 } |
260 | |
261 | |
262 /*! | |
263 Writes out declarations of virtual C++ functions so that they | |
264 can be reimplemented from the java side. | |
265 */ | |
266 void CppHeaderGenerator::writeFunction(QTextStream &s, const AbstractMetaFunction *java_function) | |
267 { | |
268 if (java_function->isModifiedRemoved(TypeSystem::ShellCode)) | |
269 return; | |
270 | |
271 s << " "; | |
272 writeFunctionSignature(s, java_function, 0, QString(), Option(OriginalName | ShowStatic)); | |
273 s << ";" << endl; | |
274 } | |
275 | |
276 void CppHeaderGenerator::writePublicFunctionOverride(QTextStream &s, | |
277 const AbstractMetaFunction *java_function) | |
278 { | |
279 s << " "; | |
280 writeFunctionSignature(s, java_function, 0, "__public_", Option(EnumAsInts | ShowStatic | UnderscoreSpaces)); | |
281 s << ";" << endl; | |
282 } | |
283 | |
284 | |
285 void CppHeaderGenerator::writeVirtualFunctionOverride(QTextStream &s, | |
286 const AbstractMetaFunction *java_function) | |
287 { | |
288 if (java_function->isModifiedRemoved(TypeSystem::NativeCode)) | |
289 return; | |
290 | |
291 s << " "; | |
21 | 292 writeFunctionSignature(s, java_function, 0, "__override_", Option(EnumAsInts | ShowStatic | UnderscoreSpaces), QString(), QStringList() << "bool static_call"); |
1 | 293 s << ";" << endl; |
294 } | |
295 | |
296 | |
297 void CppHeaderGenerator::writeForwardDeclareSection(QTextStream &s, const AbstractMetaClass *) | |
298 { | |
299 s << endl | |
300 << "class QtJambiFunctionTable;" << endl | |
301 << "class QtJambiLink;" << endl; | |
302 } | |
303 | |
304 | |
305 void CppHeaderGenerator::writeVariablesSection(QTextStream &s, const AbstractMetaClass *) | |
306 { | |
307 s << endl | |
308 << " QtJambiFunctionTable *m_vtable;" << endl | |
309 << " QtJambiLink *m_link;" << endl; | |
310 } | |
311 | |
312 void CppHeaderGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaClass *java_class) | |
313 { | |
314 CodeSnipList code_snips = java_class->typeEntry()->codeSnips(); | |
315 foreach (const CodeSnip &cs, code_snips) { | |
316 if (cs.language == TypeSystem::ShellDeclaration) { | |
317 s << cs.code() << endl; | |
318 } | |
319 } | |
320 } |