source: trunk/tools/qtconcurrent/generaterun/main.cpp@ 166

Last change on this file since 166 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 21.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41#include <QApplication>
42#include <QDebug>
43#include <QFile>
44
45#include "codegenerator.h"
46using namespace CodeGenerator;
47
48const Item argument = "arg" + Counter();
49const Item argumentRef = "&arg" + Counter();
50const Item argumentType = "Arg" + Counter();
51const Item constArgumentType = "const Arg" + Counter();
52const Item parameterType = "Param" + Counter();
53
54Group argumentTypes(argumentType); // expands to ",Arg1, Arg2, ..."
55Group argumentTypesNoPrefix(argumentType); // expands to "Arg1, Arg2, ..."
56Group arguments(argument); // expands to ",arg1, arg2, ..."
57Group argumentsNoPrefix(argument); // expands to "arg1, arg2, ..."
58Group parameterTypes(parameterType); // expands to ",Param1, Param2, ..."
59Group parameterTypesNoPrefix(parameterType); // expands to "Param1, Param2, ..."
60Group typenameTypes("typename " + parameterType + ", typename " + argumentType); // expands to " ,typename Param1, typename Arg1, ..."
61Group types(parameterType + ", " + argumentType); // expands to ", Param1, Arg1, ..."
62Group functionParameters(constArgumentType + " " + argumentRef);
63Group typenameArgumentTypes("typename " + argumentType);
64
65Group initializers(argument + "(" + argument + ")");
66Group classData(argumentType +" " + argument + ";");
67Group arglist(argument);
68Group typeList(argumentTypes);
69
70void init()
71{
72 argumentTypes.setPrefix(", ");
73 arguments.setPrefix(", ");
74 parameterTypes.setPrefix(", ");
75 typenameTypes.setPrefix(", ");
76 types.setPrefix(", ");
77 functionParameters.setPrefix(", ");
78 typenameArgumentTypes.setPrefix(", ");
79
80 initializers.setPrefix(", ");
81 classData.setSeparator(" ");
82 classData.setPrefix(" ");
83 arglist.setPrefix(", ");
84 typeList.setPrefix(", ");
85}
86
87
88Item Line(Item item)
89{
90 return item + "\n";
91}
92
93Item generateRunFunctions(int repeats)
94{
95 Item functionPointerType = "T (*)(" + parameterTypesNoPrefix + ")";
96
97 Item functionPointerParameter = "T (*functionPointer)(" + parameterTypesNoPrefix + ")";
98
99
100
101 // plain functions
102 Repeater functions = Line ("template <typename T" + typenameTypes + ">") +
103 Line ("QFuture<T> run(" + functionPointerParameter + functionParameters + ")") +
104 Line("{") +
105 Line(" return (new QT_TYPENAME SelectStoredFunctorCall" + Counter() + "<T, " +
106 functionPointerType + argumentTypes + ">::type(functionPointer" + arguments + "))->start();") +
107 Line("}");
108 functions.setRepeatCount(repeats);
109
110 // function objects by value
111 Repeater functionObjects = Line ("template <typename FunctionObject" + typenameArgumentTypes + ">") +
112 Line ("QFuture<typename FunctionObject::result_type> run(FunctionObject functionObject" + functionParameters + ")") +
113 Line("{") +
114 Line(" return (new QT_TYPENAME SelectStoredFunctorCall" + Counter() +
115 "<QT_TYPENAME FunctionObject::result_type, FunctionObject" +
116 argumentTypes + ">::type(functionObject" + arguments + "))->start();") +
117 Line("}");
118 functionObjects.setRepeatCount(repeats);
119
120 // function objects by pointer
121 Repeater functionObjectsPointer = Line ("template <typename FunctionObject" + typenameArgumentTypes + ">") +
122 Line ("QFuture<typename FunctionObject::result_type> run(FunctionObject *functionObject" + functionParameters + ")") +
123 Line("{") +
124 Line(" return (new QT_TYPENAME SelectStoredFunctorPointerCall" + Counter() +
125 "<QT_TYPENAME FunctionObject::result_type, FunctionObject" +
126 argumentTypes + ">::type(functionObject" + arguments + "))->start();") +
127 Line("}");
128 functionObjectsPointer.setRepeatCount(repeats);
129
130 // member functions by value
131 Repeater memberFunction = Line ("template <typename T, typename Class" + typenameTypes + ">") +
132 Line ("QFuture<T> run(const Class &object, T (Class::*fn)(" + parameterTypesNoPrefix + ")" + functionParameters + ")") +
133 Line("{") +
134 Line(" return (new QT_TYPENAME SelectStoredMemberFunctionCall" + Counter() +
135 "<T, Class" +
136 types + ">::type(fn, object" + arguments + "))->start();") +
137 Line("}");
138 memberFunction.setRepeatCount(repeats);
139
140 // const member functions by value
141 Repeater constMemberFunction = Line ("template <typename T, typename Class" + typenameTypes + ">") +
142 Line ("QFuture<T> run(const Class &object, T (Class::*fn)(" + parameterTypesNoPrefix + ") const" + functionParameters + ")") +
143 Line("{") +
144 Line(" return (new QT_TYPENAME SelectStoredConstMemberFunctionCall" + Counter() +
145 "<T, Class" +
146 types + ">::type(fn, object" + arguments + "))->start();") +
147 Line("}");
148 constMemberFunction.setRepeatCount(repeats);
149
150 // member functions by class pointer
151 Repeater memberFunctionPointer = Line ("template <typename T, typename Class" + typenameTypes + ">") +
152 Line ("QFuture<T> run(Class *object, T (Class::*fn)(" + parameterTypesNoPrefix + ")" + functionParameters + ")") +
153 Line("{") +
154 Line(" return (new QT_TYPENAME SelectStoredMemberFunctionPointerCall" + Counter() +
155 "<T, Class" +
156 types + ">::type(fn, object" + arguments + "))->start();") +
157 Line("}");
158 memberFunctionPointer.setRepeatCount(repeats);
159
160 // const member functions by class pointer
161 Repeater constMemberFunctionPointer = Line ("template <typename T, typename Class" + typenameTypes + ">") +
162 Line ("QFuture<T> run(const Class *object, T (Class::*fn)(" + parameterTypesNoPrefix + ") const" + functionParameters + ")") +
163 Line("{") +
164 Line(" return (new QT_TYPENAME SelectStoredConstMemberFunctionPointerCall" + Counter() +
165 "<T, Class" +
166 types + ">::type(fn, object" + arguments + "))->start();") +
167 Line("}");
168 constMemberFunctionPointer.setRepeatCount(repeats);
169
170
171 Item interfaceFunctionPointerType = "void (*)(QFutureInterface<T> &" + argumentTypes + ")";
172 Item interfaceFunctionPointerParameter = "void (*functionPointer)(QFutureInterface<T> &" + argumentTypes + ")";
173/*
174 // QFutureInterface functions
175 Repeater interfaceFunctions = Line ("template <typename T" + typenameTypes + ">") +
176 Line ("QFuture<T> run(" + interfaceFunctionPointerParameter + functionParameters + ")") +
177 Line("{") +
178 Line(" return (new StoredInterfaceFunctionCall" + Counter() + "<T, " +
179 interfaceFunctionPointerType + typenameArgumentTypes + ">(functionPointer" + arguments + "))->start();") +
180 Line("}");
181 functions.setRepeatCount(repeats);
182 interfaceFunctions.setRepeatCount(repeats);
183
184 // member functions by class pointer
185 Repeater interfaceMemberFunction = Line ("template <typename Class, typename T" + typenameTypes + ">") +
186 Line ("QFuture<T> run(void (Class::*fn)(QFutureInterface<T> &), Class *object" + functionParameters + ")") +
187 Line("{") +
188 Line(" return (new StoredInterfaceMemberFunctionCall" + Counter() +
189 "<T, void (Class::*)(QFutureInterface<T> &), Class" +
190 typenameArgumentTypes + ">(fn, object" + arguments + "))->start();") +
191 Line("}");
192 memberFunctionPointer.setRepeatCount(repeats);
193*/
194 return functions + Line("") + functionObjects + Line("") + functionObjectsPointer + Line("")
195 + memberFunction + Line("") + constMemberFunction + Line("")
196 + memberFunctionPointer + Line("") + constMemberFunctionPointer + Line("")
197 /* + interfaceFunctions + Line("") + interfaceMemberFunction + Line("")*/
198 ;
199}
200
201
202Item functions(Item className, Item functorType, Item callLine)
203{
204 return
205 Line("template <typename T, typename FunctionPointer" + typenameArgumentTypes + ">") +
206 Line("struct " + className + Counter() +": public RunFunctionTask<T>") +
207 Line("{") +
208 Line(" inline " + className + Counter() + "(" + functorType + " function" + functionParameters +")") +
209 Line(" : function(function)" + initializers + " {}") +
210 Line(" void runFunctor() {" + callLine + argumentsNoPrefix + "); }") +
211 Line(" " + functorType + " function;") +
212 Line( classData) +
213 Line("};") +
214 Line("");
215}
216
217Item functionSelector(Item classNameBase)
218{
219 return
220 Line("template <typename T, typename FunctionPointer" + typenameArgumentTypes + ">") +
221 Line("struct Select" + classNameBase + Counter()) +
222 Line("{") +
223 Line(" typedef typename SelectSpecialization<T>::template") +
224 Line(" Type<" + classNameBase + Counter() + " <T, FunctionPointer" + argumentTypes + ">,") +
225 Line(" Void" + classNameBase + Counter() + "<T, FunctionPointer" + argumentTypes + "> >::type type;") +
226 Line("};");
227}
228
229Item memberFunctions(Item className, Item constFunction, Item objectArgument, Item objectMember, Item callLine)
230{
231 return
232 Line("template <typename T, typename Class" + typenameTypes + ">") +
233 Line("class " + className + Counter() + " : public RunFunctionTask<T>") +
234 Line("{") +
235 Line("public:")+
236 Line(" " + className + Counter() + "(T (Class::*fn)(" + parameterTypesNoPrefix + ") " + constFunction + ", " + objectArgument + functionParameters + ")") +
237 Line(" : fn(fn), object(object)" + initializers + "{ }" ) +
238 Line("")+
239 Line(" void runFunctor()")+
240 Line(" {")+
241 Line(" " + callLine + argumentsNoPrefix + ");")+
242 Line(" }")+
243 Line("private:")+
244 Line(" T (Class::*fn)(" + parameterTypesNoPrefix + ")" + constFunction + ";")+
245 Line(" " + objectMember + ";") +
246 Line( classData) +
247 Line("};");
248}
249
250Item memberFunctionSelector(Item classNameBase)
251{
252 return
253 Line("template <typename T, typename Class" + typenameTypes + ">") +
254 Line("struct Select" + classNameBase + Counter()) +
255 Line("{") +
256 Line(" typedef typename SelectSpecialization<T>::template") +
257 Line(" Type<" + classNameBase + Counter() + " <T, Class" + types + ">,") +
258 Line(" Void" + classNameBase + Counter() + "<T, Class" + types + "> >::type type;") +
259 Line("};");
260}
261
262Item generateSFCs(int repeats)
263{
264
265 Item functionPointerTypedef = "typedef void (*FunctionPointer)(" + argumentTypesNoPrefix + ");";
266
267 Repeater dataStructures =
268
269 // Function objects by value
270 functions(Item("StoredFunctorCall"), Item("FunctionPointer"), Item(" this->result = function(")) +
271 functions(Item("VoidStoredFunctorCall"), Item("FunctionPointer"), Item(" function(")) +
272 functionSelector(Item("StoredFunctorCall")) +
273
274 // Function objects by pointer
275 functions(Item("StoredFunctorPointerCall"), Item("FunctionPointer *"), Item(" this->result =(*function)(")) +
276 functions(Item("VoidStoredFunctorPointerCall"), Item("FunctionPointer *"), Item("(*function)(")) +
277 functionSelector(Item("StoredFunctorPointerCall")) +
278
279 // Member functions by value
280 memberFunctions(Item("StoredMemberFunctionCall"), Item(""), Item("const Class &object"), Item("Class object"), Item("this->result = (object.*fn)(")) +
281 memberFunctions(Item("VoidStoredMemberFunctionCall"), Item(""), Item("const Class &object"), Item("Class object"), Item("(object.*fn)(")) +
282 memberFunctionSelector(Item("StoredMemberFunctionCall")) +
283
284 // Const Member functions by value
285 memberFunctions(Item("StoredConstMemberFunctionCall"), Item("const"), Item("const Class &object"), Item("const Class object"), Item("this->result = (object.*fn)(")) +
286 memberFunctions(Item("VoidStoredConstMemberFunctionCall"), Item("const"), Item("const Class &object"), Item("const Class object"), Item("(object.*fn)(")) +
287 memberFunctionSelector(Item("StoredConstMemberFunctionCall")) +
288
289 // Member functions by pointer
290 memberFunctions(Item("StoredMemberFunctionPointerCall"), Item(""), Item("Class *object"), Item("Class *object"), Item("this->result = (object->*fn)(")) +
291 memberFunctions(Item("VoidStoredMemberFunctionPointerCall"), Item(""), Item("Class *object"), Item("Class *object"), Item("(object->*fn)(")) +
292 memberFunctionSelector(Item("StoredMemberFunctionPointerCall")) +
293
294 // const member functions by pointer
295 memberFunctions(Item("StoredConstMemberFunctionPointerCall"), Item("const"), Item("Class const *object"), Item("Class const *object"), Item("this->result = (object->*fn)(")) +
296 memberFunctions(Item("VoidStoredConstMemberFunctionPointerCall"), Item("const"), Item("Class const *object"), Item("Class const *object"), Item("(object->*fn)(")) +
297 memberFunctionSelector(Item("StoredConstMemberFunctionPointerCall"));
298
299 dataStructures.setRepeatCount(repeats);
300 return dataStructures;
301}
302
303void writeFile(QString fileName, QByteArray contents)
304{
305 QFile runFile(fileName);
306 if (runFile.open(QIODevice::WriteOnly) == false) {
307 qDebug() << "Write to" << fileName << "failed";
308 return;
309 }
310
311 runFile.write(contents);
312 runFile.close();
313 qDebug() << "Write to" << fileName << "Ok";
314}
315
316Item dollarQuote(Item item)
317{
318 return Item("$") + item + Item("$");
319}
320
321int main()
322{
323 const int repeats = 6;
324 init();
325 Item run = (
326 Line("/****************************************************************************") +
327 Line("**") +
328 Line("** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).") +
329 Line("** Contact: Qt Software Information ([email protected])") +
330 Line("**") +
331 Line("** This file is part of the " + dollarQuote("MODULE") + " of the Qt Toolkit.") +
332 Line("**") +
333 Line("** " + dollarQuote("TROLLTECH_DUAL_LICENSE")) +
334 Line("**") +
335 Line("****************************************************************************/") +
336 Line("") +
337 Line("// Generated code, do not edit! Use generator at tools/qtconcurrent/generaterun/") +
338 Line("#ifndef QTCONCURRENT_RUN_H") +
339 Line("#define QTCONCURRENT_RUN_H") +
340 Line("") +
341 Line("#ifndef QT_NO_CONCURRENT") +
342 Line("") +
343 Line("#include <QtCore/qtconcurrentrunbase.h>") +
344 Line("#include <QtCore/qtconcurrentstoredfunctioncall.h>") +
345 Line("") +
346 Line("QT_BEGIN_HEADER") +
347 Line("QT_BEGIN_NAMESPACE") +
348 Line("") +
349 Line("QT_MODULE(Core)") +
350 Line("") +
351 Line("#ifdef qdoc") +
352 Line("") +
353 Line("namespace QtConcurrent {") +
354 Line("") +
355 Line(" template <typename T>") +
356 Line(" QFuture<T> run(Function function, ...);") +
357 Line("") +
358 Line("} // namespace QtConcurrent") +
359 Line("") +
360 Line("#else") +
361 Line("") +
362 Line("namespace QtConcurrent {") +
363 Line("") +
364 generateRunFunctions(repeats) +
365 Line("} //namespace QtConcurrent") +
366 Line("") +
367 Line("#endif // qdoc") +
368 Line("") +
369 Line("QT_END_NAMESPACE") +
370 Line("QT_END_HEADER") +
371 Line("") +
372 Line("#endif")
373 );
374
375 writeFile("../../../src/corelib/concurrent/qtconcurrentrun.h", run.generate());
376
377 Item storedFunctionCall = (
378 Line("/****************************************************************************") +
379 Line("**") +
380 Line("** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).") +
381 Line("** Contact: Qt Software Information ([email protected])") +
382 Line("**") +
383 Line("** This file is part of the " + dollarQuote("MODULE") + " of the Qt Toolkit.") +
384 Line("**") +
385 Line("** " + dollarQuote("TROLLTECH_DUAL_LICENSE")) +
386 Line("**") +
387 Line("****************************************************************************/") +
388 Line("") +
389 Line("// Generated code, do not edit! Use generator at tools/qtconcurrent/generaterun/") +
390 Line("#ifndef QTCONCURRENT_STOREDFUNCTIONCALL_H") +
391 Line("#define QTCONCURRENT_STOREDFUNCTIONCALL_H") +
392 Line("") +
393 Line("#include <QtCore/qglobal.h>") +
394 Line("") +
395 Line("#ifndef QT_NO_CONCURRENT") +
396 Line("#include <QtCore/qtconcurrentrunbase.h>") +
397 Line("") +
398 Line("QT_BEGIN_HEADER") +
399 Line("QT_BEGIN_NAMESPACE") +
400 Line("") +
401 Line("QT_MODULE(Core)") +
402 Line("") +
403 Line("#ifndef qdoc") +
404 Line("") +
405 Line("namespace QtConcurrent {") +
406 generateSFCs(repeats) +
407 Line("} //namespace QtConcurrent") +
408 Line("") +
409 Line("#endif // qdoc") +
410 Line("") +
411 Line("QT_END_NAMESPACE") +
412 Line("QT_END_HEADER") +
413 Line("") +
414 Line("#endif // QT_NO_CONCURRENT") +
415 Line("") +
416 Line("#endif")
417 );
418
419 writeFile("../../../src/corelib/concurrent/qtconcurrentstoredfunctioncall.h", storedFunctionCall.generate());
420}
421
422
Note: See TracBrowser for help on using the repository browser.