1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
6 ** This file is part of the tools applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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
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.
40 ****************************************************************************/
41 #include <QApplication>
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
);
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(", ");
93 Item
generateRunFunctions(int repeats
)
95 Item functionPointerType
= "T (*)(" + parameterTypesNoPrefix
+ ")";
97 Item functionPointerParameter
= "T (*functionPointer)(" + parameterTypesNoPrefix
+ ")";
102 Repeater functions
= Line ("template <typename T" + typenameTypes
+ ">") +
103 Line ("QFuture<T> run(" + functionPointerParameter
+ functionParameters
+ ")") +
105 Line(" return (new QT_TYPENAME SelectStoredFunctorCall" + Counter() + "<T, " +
106 functionPointerType
+ argumentTypes
+ ">::type(functionPointer" + arguments
+ "))->start();") +
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
+ ")") +
114 Line(" return (new QT_TYPENAME SelectStoredFunctorCall" + Counter() +
115 "<QT_TYPENAME FunctionObject::result_type, FunctionObject" +
116 argumentTypes
+ ">::type(functionObject" + arguments
+ "))->start();") +
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
+ ")") +
124 Line(" return (new QT_TYPENAME SelectStoredFunctorPointerCall" + Counter() +
125 "<QT_TYPENAME FunctionObject::result_type, FunctionObject" +
126 argumentTypes
+ ">::type(functionObject" + arguments
+ "))->start();") +
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
+ ")") +
134 Line(" return (new QT_TYPENAME SelectStoredMemberFunctionCall" + Counter() +
136 types
+ ">::type(fn, object" + arguments
+ "))->start();") +
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
+ ")") +
144 Line(" return (new QT_TYPENAME SelectStoredConstMemberFunctionCall" + Counter() +
146 types
+ ">::type(fn, object" + arguments
+ "))->start();") +
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
+ ")") +
154 Line(" return (new QT_TYPENAME SelectStoredMemberFunctionPointerCall" + Counter() +
156 types
+ ">::type(fn, object" + arguments
+ "))->start();") +
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
+ ")") +
164 Line(" return (new QT_TYPENAME SelectStoredConstMemberFunctionPointerCall" + Counter() +
166 types
+ ">::type(fn, object" + arguments
+ "))->start();") +
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 + ")") +
178 Line(" return (new StoredInterfaceFunctionCall" + Counter() + "<T, " +
179 interfaceFunctionPointerType + typenameArgumentTypes + ">(functionPointer" + arguments + "))->start();") +
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 + ")") +
188 Line(" return (new StoredInterfaceMemberFunctionCall" + Counter() +
189 "<T, void (Class::*)(QFutureInterface<T> &), Class" +
190 typenameArgumentTypes + ">(fn, object" + arguments + "))->start();") +
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
)
205 Line("template <typename T, typename FunctionPointer" + typenameArgumentTypes
+ ">") +
206 Line("struct " + className
+ Counter() +": public RunFunctionTask<T>") +
208 Line(" inline " + className
+ Counter() + "(" + functorType
+ " function" + functionParameters
+")") +
209 Line(" : function(function)" + initializers
+ " {}") +
210 Line(" void runFunctor() {" + callLine
+ argumentsNoPrefix
+ "); }") +
211 Line(" " + functorType
+ " function;") +
217 Item
functionSelector(Item classNameBase
)
220 Line("template <typename T, typename FunctionPointer" + typenameArgumentTypes
+ ">") +
221 Line("struct Select" + classNameBase
+ Counter()) +
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;") +
229 Item
memberFunctions(Item className
, Item constFunction
, Item objectArgument
, Item objectMember
, Item callLine
)
232 Line("template <typename T, typename Class" + typenameTypes
+ ">") +
233 Line("class " + className
+ Counter() + " : public RunFunctionTask<T>") +
236 Line(" " + className
+ Counter() + "(T (Class::*fn)(" + parameterTypesNoPrefix
+ ") " + constFunction
+ ", " + objectArgument
+ functionParameters
+ ")") +
237 Line(" : fn(fn), object(object)" + initializers
+ "{ }" ) +
239 Line(" void runFunctor()")+
241 Line(" " + callLine
+ argumentsNoPrefix
+ ");")+
244 Line(" T (Class::*fn)(" + parameterTypesNoPrefix
+ ")" + constFunction
+ ";")+
245 Line(" " + objectMember
+ ";") +
250 Item
memberFunctionSelector(Item classNameBase
)
253 Line("template <typename T, typename Class" + typenameTypes
+ ">") +
254 Line("struct Select" + classNameBase
+ Counter()) +
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;") +
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";
311 runFile
.write(contents
);
313 qDebug() << "Write to" << fileName
<< "Ok";
316 Item
dollarQuote(Item item
)
318 return Item("$") + item
+ Item("$");
323 const int repeats
= 6;
326 Line("/****************************************************************************") +
328 Line("** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).") +
329 Line("** Contact: Qt Software Information (qt-info@nokia.com)") +
331 Line("** This file is part of the " + dollarQuote("MODULE") + " of the Qt Toolkit.") +
333 Line("** " + dollarQuote("TROLLTECH_DUAL_LICENSE")) +
335 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") +
341 Line("#ifndef QT_NO_CONCURRENT") +
343 Line("#include <QtCore/qtconcurrentrunbase.h>") +
344 Line("#include <QtCore/qtconcurrentstoredfunctioncall.h>") +
346 Line("QT_BEGIN_HEADER") +
347 Line("QT_BEGIN_NAMESPACE") +
349 Line("QT_MODULE(Core)") +
351 Line("#ifdef qdoc") +
353 Line("namespace QtConcurrent {") +
355 Line(" template <typename T>") +
356 Line(" QFuture<T> run(Function function, ...);") +
358 Line("} // namespace QtConcurrent") +
362 Line("namespace QtConcurrent {") +
364 generateRunFunctions(repeats
) +
365 Line("} //namespace QtConcurrent") +
367 Line("#endif // qdoc") +
369 Line("QT_END_NAMESPACE") +
370 Line("QT_END_HEADER") +
375 writeFile("../../../src/corelib/concurrent/qtconcurrentrun.h", run
.generate());
377 Item storedFunctionCall
= (
378 Line("/****************************************************************************") +
380 Line("** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).") +
381 Line("** Contact: Qt Software Information (qt-info@nokia.com)") +
383 Line("** This file is part of the " + dollarQuote("MODULE") + " of the Qt Toolkit.") +
385 Line("** " + dollarQuote("TROLLTECH_DUAL_LICENSE")) +
387 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") +
393 Line("#include <QtCore/qglobal.h>") +
395 Line("#ifndef QT_NO_CONCURRENT") +
396 Line("#include <QtCore/qtconcurrentrunbase.h>") +
398 Line("QT_BEGIN_HEADER") +
399 Line("QT_BEGIN_NAMESPACE") +
401 Line("QT_MODULE(Core)") +
403 Line("#ifndef qdoc") +
405 Line("namespace QtConcurrent {") +
406 generateSFCs(repeats
) +
407 Line("} //namespace QtConcurrent") +
409 Line("#endif // qdoc") +
411 Line("QT_END_NAMESPACE") +
412 Line("QT_END_HEADER") +
414 Line("#endif // QT_NO_CONCURRENT") +
419 writeFile("../../../src/corelib/concurrent/qtconcurrentstoredfunctioncall.h", storedFunctionCall
.generate());