tools/qtconcurrent/generaterun/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <QApplication>
       
    42 #include <QDebug>
       
    43 #include <QFile>
       
    44 
       
    45 #include "codegenerator.h"
       
    46 using namespace CodeGenerator;
       
    47 
       
    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();
       
    53 
       
    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);
       
    64 
       
    65 Group initializers(argument + "(" + argument + ")");
       
    66 Group classData(argumentType +" "  + argument + ";");
       
    67 Group arglist(argument);
       
    68 Group typeList(argumentTypes);
       
    69 
       
    70 void 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 
       
    88 Item Line(Item item)
       
    89 {
       
    90     return item + "\n";
       
    91 }
       
    92 
       
    93 Item 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 
       
   202 Item 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 
       
   217 Item 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 
       
   229 Item 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 
       
   250 Item 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 
       
   262 Item 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 
       
   303 void 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 
       
   316 Item dollarQuote(Item item)
       
   317 {
       
   318     return Item("$") + item + Item("$");
       
   319 }
       
   320 
       
   321 int 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: Nokia Corporation (qt-info@nokia.com)") +
       
   330                        Line("**") +
       
   331                        Line("** This file is part of the Qt Toolkit.") +
       
   332                        Line("**") +
       
   333                        Line("****************************************************************************/") +
       
   334                        Line("") +
       
   335                        Line("// Generated code, do not edit! Use generator at tools/qtconcurrent/generaterun/") +
       
   336                        Line("#ifndef QTCONCURRENT_RUN_H") +
       
   337                        Line("#define QTCONCURRENT_RUN_H") +
       
   338                        Line("") +
       
   339                        Line("#ifndef QT_NO_CONCURRENT") +
       
   340                        Line("") +
       
   341                        Line("#include <QtCore/qtconcurrentrunbase.h>") +
       
   342                        Line("#include <QtCore/qtconcurrentstoredfunctioncall.h>") +
       
   343                        Line("") +
       
   344                        Line("QT_BEGIN_HEADER") +
       
   345                        Line("QT_BEGIN_NAMESPACE") +
       
   346                        Line("") +
       
   347                        Line("QT_MODULE(Core)") +
       
   348                        Line("") +
       
   349                        Line("#ifdef qdoc") +
       
   350                        Line("") +
       
   351                        Line("namespace QtConcurrent {") +
       
   352                        Line("") +
       
   353                        Line("    template <typename T>") +
       
   354                        Line("    QFuture<T> run(Function function, ...);") +
       
   355                        Line("") +
       
   356                        Line("} // namespace QtConcurrent") +
       
   357                        Line("") +
       
   358                        Line("#else") +
       
   359                        Line("") +
       
   360                        Line("namespace QtConcurrent {") +
       
   361                        Line("") +
       
   362                        generateRunFunctions(repeats) +
       
   363                        Line("} //namespace QtConcurrent") +
       
   364                        Line("") +
       
   365                        Line("#endif // qdoc") +
       
   366                        Line("") +
       
   367                        Line("QT_END_NAMESPACE") +
       
   368                        Line("QT_END_HEADER") +
       
   369                        Line("") +
       
   370                        Line("#endif")
       
   371                       );
       
   372 
       
   373     writeFile("../../../src/corelib/concurrent/qtconcurrentrun.h", run.generate());
       
   374 
       
   375     Item storedFunctionCall = (
       
   376                                      Line("/****************************************************************************") +
       
   377                                      Line("**") +
       
   378                                      Line("** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).") +
       
   379                                      Line("** Contact: Nokia Corporation (qt-info@nokia.com)") +
       
   380                                      Line("**") +
       
   381                                      Line("** This file is part of the Qt Toolkit.") +
       
   382                                      Line("**") +
       
   383                                      Line("****************************************************************************/") +
       
   384                                      Line("") +
       
   385                                      Line("// Generated code, do not edit! Use generator at tools/qtconcurrent/generaterun/") +
       
   386                                      Line("#ifndef QTCONCURRENT_STOREDFUNCTIONCALL_H") +
       
   387                                      Line("#define QTCONCURRENT_STOREDFUNCTIONCALL_H") +
       
   388                                      Line("") +
       
   389                                      Line("#include <QtCore/qglobal.h>") +
       
   390                                      Line("") +
       
   391                                      Line("#ifndef QT_NO_CONCURRENT") +
       
   392                                      Line("#include <QtCore/qtconcurrentrunbase.h>") +
       
   393                                      Line("") +
       
   394                                      Line("QT_BEGIN_HEADER") +
       
   395                                      Line("QT_BEGIN_NAMESPACE") +
       
   396                                      Line("") +
       
   397                                      Line("QT_MODULE(Core)") +
       
   398                                      Line("") +
       
   399                                      Line("#ifndef qdoc") +
       
   400                                      Line("") +
       
   401                                      Line("namespace QtConcurrent {") +
       
   402                                      generateSFCs(repeats) +
       
   403                                      Line("} //namespace QtConcurrent") +
       
   404                                      Line("") +
       
   405                                      Line("#endif // qdoc") +
       
   406                                      Line("") +
       
   407                                      Line("QT_END_NAMESPACE") +
       
   408                                      Line("QT_END_HEADER") +
       
   409                                      Line("") +
       
   410                                      Line("#endif // QT_NO_CONCURRENT") +
       
   411                                      Line("") +
       
   412                                      Line("#endif")
       
   413                                     );
       
   414 
       
   415     writeFile("../../../src/corelib/concurrent/qtconcurrentstoredfunctioncall.h", storedFunctionCall.generate());
       
   416 }
       
   417 
       
   418