Skip empty parts in KDEDIRS
[qt-netbsd.git] / tools / qtconcurrent / generaterun / main.cpp
blob5dd18fbcf448518048bd40edb6e0fe58b1020483
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** No Commercial Usage
10 ** This file contains pre-release code and may not be distributed.
11 ** You may use this file in accordance with the terms and conditions
12 ** contained in the either Technology Preview License Agreement or the
13 ** Beta Release License Agreement.
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.
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.
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.
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
41 #include <QApplication>
42 #include <QDebug>
43 #include <QFile>
45 #include "codegenerator.h"
46 using namespace CodeGenerator;
48 const Item argument = "arg" + Counter();
49 const Item argumentRef = "&arg" + Counter();
50 const Item argumentType = "Arg" + Counter();
51 const Item constArgumentType = "const Arg" + Counter();
52 const Item parameterType = "Param" + Counter();
54 Group argumentTypes(argumentType); // expands to ",Arg1, Arg2, ..."
55 Group argumentTypesNoPrefix(argumentType); // expands to "Arg1, Arg2, ..."
56 Group arguments(argument); // expands to ",arg1, arg2, ..."
57 Group argumentsNoPrefix(argument); // expands to "arg1, arg2, ..."
58 Group parameterTypes(parameterType); // expands to ",Param1, Param2, ..."
59 Group parameterTypesNoPrefix(parameterType); // expands to "Param1, Param2, ..."
60 Group typenameTypes("typename " + parameterType + ", typename " + argumentType); // expands to " ,typename Param1, typename Arg1, ..."
61 Group types(parameterType + ", " + argumentType); // expands to ", Param1, Arg1, ..."
62 Group functionParameters(constArgumentType + " " + argumentRef);
63 Group typenameArgumentTypes("typename " + argumentType);
65 Group initializers(argument + "(" + argument + ")");
66 Group classData(argumentType +" " + argument + ";");
67 Group arglist(argument);
68 Group typeList(argumentTypes);
70 void init()
72 argumentTypes.setPrefix(", ");
73 arguments.setPrefix(", ");
74 parameterTypes.setPrefix(", ");
75 typenameTypes.setPrefix(", ");
76 types.setPrefix(", ");
77 functionParameters.setPrefix(", ");
78 typenameArgumentTypes.setPrefix(", ");
80 initializers.setPrefix(", ");
81 classData.setSeparator(" ");
82 classData.setPrefix(" ");
83 arglist.setPrefix(", ");
84 typeList.setPrefix(", ");
88 Item Line(Item item)
90 return item + "\n";
93 Item generateRunFunctions(int repeats)
95 Item functionPointerType = "T (*)(" + parameterTypesNoPrefix + ")";
97 Item functionPointerParameter = "T (*functionPointer)(" + parameterTypesNoPrefix + ")";
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);
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);
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);
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);
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);
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);
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);
171 Item interfaceFunctionPointerType = "void (*)(QFutureInterface<T> &" + argumentTypes + ")";
172 Item interfaceFunctionPointerParameter = "void (*functionPointer)(QFutureInterface<T> &" + argumentTypes + ")";
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);
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);
194 return functions + Line("") + functionObjects + Line("") + functionObjectsPointer + Line("")
195 + memberFunction + Line("") + constMemberFunction + Line("")
196 + memberFunctionPointer + Line("") + constMemberFunctionPointer + Line("")
197 /* + interfaceFunctions + Line("") + interfaceMemberFunction + Line("")*/
202 Item functions(Item className, Item functorType, Item callLine)
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("");
217 Item functionSelector(Item classNameBase)
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("};");
229 Item memberFunctions(Item className, Item constFunction, Item objectArgument, Item objectMember, Item callLine)
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("};");
250 Item memberFunctionSelector(Item classNameBase)
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("};");
262 Item generateSFCs(int repeats)
265 Item functionPointerTypedef = "typedef void (*FunctionPointer)(" + argumentTypesNoPrefix + ");";
267 Repeater dataStructures =
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")) +
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")) +
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")) +
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")) +
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")) +
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"));
299 dataStructures.setRepeatCount(repeats);
300 return dataStructures;
303 void writeFile(QString fileName, QByteArray contents)
305 QFile runFile(fileName);
306 if (runFile.open(QIODevice::WriteOnly) == false) {
307 qDebug() << "Write to" << fileName << "failed";
308 return;
311 runFile.write(contents);
312 runFile.close();
313 qDebug() << "Write to" << fileName << "Ok";
316 Item dollarQuote(Item item)
318 return Item("$") + item + Item("$");
321 int main()
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 (qt-info@nokia.com)") +
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")
375 writeFile("../../../src/corelib/concurrent/qtconcurrentrun.h", run.generate());
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 (qt-info@nokia.com)") +
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")
419 writeFile("../../../src/corelib/concurrent/qtconcurrentstoredfunctioncall.h", storedFunctionCall.generate());