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