diff options
author | axis <qt-info@nokia.com> | 2009-04-24 11:34:15 (GMT) |
---|---|---|
committer | axis <qt-info@nokia.com> | 2009-04-24 11:34:15 (GMT) |
commit | 8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76 (patch) | |
tree | a17e1a767a89542ab59907462206d7dcf2e504b2 /tools/qtconcurrent | |
download | Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.zip Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.gz Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.bz2 |
Long live Qt for S60!
Diffstat (limited to 'tools/qtconcurrent')
-rw-r--r-- | tools/qtconcurrent/codegenerator/codegenerator.pri | 5 | ||||
-rw-r--r-- | tools/qtconcurrent/codegenerator/example/example.pro | 9 | ||||
-rw-r--r-- | tools/qtconcurrent/codegenerator/example/main.cpp | 83 | ||||
-rw-r--r-- | tools/qtconcurrent/codegenerator/src/codegenerator.cpp | 140 | ||||
-rw-r--r-- | tools/qtconcurrent/codegenerator/src/codegenerator.h | 204 | ||||
-rw-r--r-- | tools/qtconcurrent/generaterun/main.cpp | 422 | ||||
-rw-r--r-- | tools/qtconcurrent/generaterun/run.pro | 9 |
7 files changed, 872 insertions, 0 deletions
diff --git a/tools/qtconcurrent/codegenerator/codegenerator.pri b/tools/qtconcurrent/codegenerator/codegenerator.pri new file mode 100644 index 0000000..0aeabab --- /dev/null +++ b/tools/qtconcurrent/codegenerator/codegenerator.pri @@ -0,0 +1,5 @@ +SOURCES += $$PWD/src/codegenerator.cpp +HEADERS += $$PWD/src/codegenerator.h +INCLUDEPATH += $$PWD/src +DEPENDPATH += $$PWD/src + diff --git a/tools/qtconcurrent/codegenerator/example/example.pro b/tools/qtconcurrent/codegenerator/example/example.pro new file mode 100644 index 0000000..df266fe --- /dev/null +++ b/tools/qtconcurrent/codegenerator/example/example.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +include (../codegenerator.pri) + +# Input +SOURCES += main.cpp diff --git a/tools/qtconcurrent/codegenerator/example/main.cpp b/tools/qtconcurrent/codegenerator/example/main.cpp new file mode 100644 index 0000000..0cd4c49 --- /dev/null +++ b/tools/qtconcurrent/codegenerator/example/main.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QDebug> +#include "codegenerator.h" +using namespace CodeGenerator; + +int main() +{ + // The code generator works on items. Each item has a generate() function: + Item item(""); + qDebug() << item.generate(); // produces "". + + // There are several Item subclasses. Text items contains a text string which they + // reproduce when generate is called: + Text text(" Hi there"); + qDebug() << text.generate(); // produces " Hi there". + + // Items can be concatenated: + Item sentence = text + Text(" Bye there") ; + qDebug() << sentence.generate(); // produces "Hi there Bye there". + // (Internally, this creates a tree of items, and generate is called recursively + // for items that have children.) + + // Repeater items repeat their content when generate is called: + Repeater repeater = text; + repeater.setRepeatCount(3); + qDebug() << repeater.generate(); // produces "Hi there Hi there Hi there". + + // Counters evaluate to the current repeat index. + Repeater repeater2 = text + Counter(); + repeater2.setRepeatCount(3); + qDebug() << repeater2.generate(); // produces "Hi there0 Hi there1 Hi there2". + + // Groups provide sub-groups which are repeated according to the current repeat index. + // Counters inside Groups evaluate to the local repeat index for the Group. + Group arguments("Arg" + Counter() + " arg" + Counter()); + Repeater function("void foo(" + arguments + ");\n"); + function.setRepeatCount(3); + qDebug() << function.generate(); + + // Produces: + // void foo(Arg1 arg1); + // void foo(Arg1 arg1, Arg2 arg2); + // void foo(Arg1 arg1, Arg2 arg2, Arg3 arg3); +} diff --git a/tools/qtconcurrent/codegenerator/src/codegenerator.cpp b/tools/qtconcurrent/codegenerator/src/codegenerator.cpp new file mode 100644 index 0000000..62b66cc --- /dev/null +++ b/tools/qtconcurrent/codegenerator/src/codegenerator.cpp @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "codegenerator.h" +#include <qdebug.h> +namespace CodeGenerator +{ + +//Convenience constructor so you can say Item("foo") +Item::Item(const char * const text) : generator(Text(QByteArray(text)).generator) {} + +int BaseGenerator::currentCount(GeneratorStack * const stack) const +{ + const int stackSize = stack->count(); + for (int i = stackSize - 1; i >= 0; --i) { + BaseGenerator const * const generator = stack->at(i); + switch (generator->type) { + case RepeaterType: { + RepeaterGenerator const * const repeater = static_cast<RepeaterGenerator const * const>(generator); + return repeater->currentRepeat; + } break; + case GroupType: { + GroupGenerator const * const group = static_cast<GroupGenerator const * const>(generator); + return group->currentRepeat; + } break; + default: + break; + } + } + return -1; +} + +int BaseGenerator::repeatCount(GeneratorStack * const stack) const +{ + const int stackSize = stack->count(); + for (int i = stackSize - 1; i >= 0; --i) { + BaseGenerator const * const generator = stack->at(i); + switch (generator->type) { + case RepeaterType: { + RepeaterGenerator const * const repeater = static_cast<RepeaterGenerator const * const>(generator); + return repeater->currentRepeat; + } break; +/* case GroupType: { + GroupGenerator const * const group = static_cast<GroupGenerator const * const>(generator); + return group->currentRepeat; + } break; +*/ + default: + break; + } + } + return -1; +} + +QByteArray RepeaterGenerator::generate(GeneratorStack * const stack) +{ + GeneratorStacker stacker(stack, this); + QByteArray generated; + for (int i = repeatOffset; i < repeatCount + repeatOffset; ++i) { + currentRepeat = i; + generated += childGenerator->generate(stack); + } + return generated; +}; + +QByteArray GroupGenerator::generate(GeneratorStack * const stack) +{ + const int repeatCount = currentCount(stack); + GeneratorStacker stacker(stack, this); + QByteArray generated; + + if (repeatCount > 0) + generated += prefix->generate(stack); + + for (int i = 1; i <= repeatCount; ++i) { + currentRepeat = i; + generated += childGenerator->generate(stack); + if (i != repeatCount) + generated += separator->generate(stack); + } + + if (repeatCount > 0) + generated += postfix->generate(stack); + + return generated; +}; + +const Compound operator+(const Item &a, const Item &b) +{ + return Compound(a, b); +} + +const Compound operator+(const Item &a, const char * const text) +{ + return Compound(a, Text(text)); +} + +const Compound operator+(const char * const text, const Item &b) +{ + return Compound(Text(text), b); +} + +}
\ No newline at end of file diff --git a/tools/qtconcurrent/codegenerator/src/codegenerator.h b/tools/qtconcurrent/codegenerator/src/codegenerator.h new file mode 100644 index 0000000..260ba8c --- /dev/null +++ b/tools/qtconcurrent/codegenerator/src/codegenerator.h @@ -0,0 +1,204 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef CODEGENERATOR_H +#define CODEGENERATOR_H + +#include <QByteArray> +#include <QString> +#include <QList> +#include <QStack> + +namespace CodeGenerator +{ + enum GeneratorType {NoopType, CompoundType, TextType, RepeaterType, CounterType, GroupType}; + class BaseGenerator; + typedef QStack<BaseGenerator *> GeneratorStack; + + template <typename ValueType> + class Stacker { + public: + Stacker(QStack<ValueType> *stack, ValueType value) : stack(stack) { stack->push(value); } + ~Stacker() { stack->pop();} + private: + QStack<ValueType> *stack; + }; + typedef Stacker<BaseGenerator *> GeneratorStacker; + + class BaseGenerator + { + public: + BaseGenerator(GeneratorType type = NoopType) : type(type) {} + virtual ~BaseGenerator() {}; + virtual QByteArray generate(GeneratorStack *stack) { Q_UNUSED(stack); return QByteArray(); }; + int currentCount(GeneratorStack *stack) const; + int repeatCount(GeneratorStack *stack) const; + GeneratorType type; + }; + + class Item + { + public: + Item(BaseGenerator * const base) : generator(base) {} + Item(const char * const text); + QByteArray generate() const + { GeneratorStack stack; return generator->generate(&stack); } + // ### TODO: Fix memory leak! + // QExplicitlySharedDataPointer<BaseGenerator> generator; + BaseGenerator * const generator; + }; + + class CompoundGenerator : public BaseGenerator + { + public: + CompoundGenerator(BaseGenerator * const a, BaseGenerator * const b) + : BaseGenerator(CompoundType), a(a), b(b) {} + virtual QByteArray generate(GeneratorStack *stack) + { return a->generate(stack) + b->generate(stack); }; + protected: + BaseGenerator * const a; + BaseGenerator * const b; + }; + + class Compound : public Item + { + public: + Compound(const Item &a, const Item &b) : Item(new CompoundGenerator(a.generator, b.generator)) {} + }; + + class TextGenerator : public BaseGenerator + { + public: + TextGenerator(const QByteArray &text) : BaseGenerator(TextType), text(text) {} + virtual QByteArray generate(GeneratorStack *) { return text; }; + protected: + QByteArray text; + }; + + class Text : public Item { + public: + Text(const QByteArray &text) : Item(new TextGenerator(text)) {} + Text(const char * const text) : Item(new TextGenerator(QByteArray(text))) {} + }; + + class RepeaterGenerator : public BaseGenerator + { + public: + RepeaterGenerator(BaseGenerator * const childGenerator) + : BaseGenerator(RepeaterType), repeatCount(1), repeatOffset(0), childGenerator(childGenerator) {} + virtual QByteArray generate(GeneratorStack *stack); + + int repeatCount; + int repeatOffset; + int currentRepeat; + BaseGenerator * const childGenerator; + }; + + class Repeater : public Item { + public: + Repeater(const Item &item) : Item(new RepeaterGenerator(item.generator)) {} + void setRepeatCount(int count) + { static_cast<RepeaterGenerator * const>(generator)->repeatCount = count; } + void setRepeatOffset(int offset) + { static_cast<RepeaterGenerator * const>(generator)->repeatOffset = offset; } + }; + + class CounterGenerator : public BaseGenerator + { + public: + CounterGenerator() : BaseGenerator(CounterType), offset(0), increment(1), reverse(false) {} + QByteArray generate(GeneratorStack *stack) + { + if (reverse) + return QByteArray::number(repeatCount(stack) - (currentCount(stack) * increment) + offset + 1); + else + return QByteArray::number((currentCount(stack) * increment) + offset); + } + int offset; + int increment; + bool reverse; + }; + + class Counter : public Item { + public: + Counter() : Item(new CounterGenerator()) {} + Counter(int offset) : Item(new CounterGenerator()) { setOffset(offset); } + void setOffset(int offset) + { static_cast<CounterGenerator *>(generator)->offset = offset; } + void setIncrement(int increment) + { static_cast<CounterGenerator *>(generator)->increment = increment; } + void setReverse(bool reverse) + { static_cast<CounterGenerator *>(generator)->reverse = reverse; } + + }; + + class GroupGenerator : public BaseGenerator + { + public: + GroupGenerator(BaseGenerator * const childGenerator) + : BaseGenerator(GroupType), currentRepeat(0), childGenerator(childGenerator), + separator(new BaseGenerator()), prefix(new BaseGenerator()), postfix(new BaseGenerator()) { } + virtual QByteArray generate(GeneratorStack *stack); + int currentRepeat; + BaseGenerator * const childGenerator; + BaseGenerator *separator; + BaseGenerator *prefix; + BaseGenerator *postfix; + }; + + class Group : public Item + { + public: + Group(const Item &item) : Item(new GroupGenerator(item.generator)) { setSeparator(", "); } + void setSeparator(const Item &separator) + { static_cast<GroupGenerator *>(generator)->separator = separator.generator; } + void setPrefix(const Item &prefix) + { static_cast<GroupGenerator *>(generator)->prefix = prefix.generator; } + void setPostfix(const Item &postfix) + { static_cast<GroupGenerator *>(generator)->postfix = postfix.generator; } + }; + + const Compound operator+(const Item &a, const Item &b); + const Compound operator+(const Item &a, const char * const text); + const Compound operator+(const char * const text, const Item &b); + +} //namespace CodeGenerator + +#endif diff --git a/tools/qtconcurrent/generaterun/main.cpp b/tools/qtconcurrent/generaterun/main.cpp new file mode 100644 index 0000000..5dd18fb --- /dev/null +++ b/tools/qtconcurrent/generaterun/main.cpp @@ -0,0 +1,422 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QApplication> +#include <QDebug> +#include <QFile> + +#include "codegenerator.h" +using namespace CodeGenerator; + +const Item argument = "arg" + Counter(); +const Item argumentRef = "&arg" + Counter(); +const Item argumentType = "Arg" + Counter(); +const Item constArgumentType = "const Arg" + Counter(); +const Item parameterType = "Param" + Counter(); + +Group argumentTypes(argumentType); // expands to ",Arg1, Arg2, ..." +Group argumentTypesNoPrefix(argumentType); // expands to "Arg1, Arg2, ..." +Group arguments(argument); // expands to ",arg1, arg2, ..." +Group argumentsNoPrefix(argument); // expands to "arg1, arg2, ..." +Group parameterTypes(parameterType); // expands to ",Param1, Param2, ..." +Group parameterTypesNoPrefix(parameterType); // expands to "Param1, Param2, ..." +Group typenameTypes("typename " + parameterType + ", typename " + argumentType); // expands to " ,typename Param1, typename Arg1, ..." +Group types(parameterType + ", " + argumentType); // expands to ", Param1, Arg1, ..." +Group functionParameters(constArgumentType + " " + argumentRef); +Group typenameArgumentTypes("typename " + argumentType); + +Group initializers(argument + "(" + argument + ")"); +Group classData(argumentType +" " + argument + ";"); +Group arglist(argument); +Group typeList(argumentTypes); + +void init() +{ + argumentTypes.setPrefix(", "); + arguments.setPrefix(", "); + parameterTypes.setPrefix(", "); + typenameTypes.setPrefix(", "); + types.setPrefix(", "); + functionParameters.setPrefix(", "); + typenameArgumentTypes.setPrefix(", "); + + initializers.setPrefix(", "); + classData.setSeparator(" "); + classData.setPrefix(" "); + arglist.setPrefix(", "); + typeList.setPrefix(", "); +} + + +Item Line(Item item) +{ + return item + "\n"; +} + +Item generateRunFunctions(int repeats) +{ + Item functionPointerType = "T (*)(" + parameterTypesNoPrefix + ")"; + + Item functionPointerParameter = "T (*functionPointer)(" + parameterTypesNoPrefix + ")"; + + + + // plain functions + Repeater functions = Line ("template <typename T" + typenameTypes + ">") + + Line ("QFuture<T> run(" + functionPointerParameter + functionParameters + ")") + + Line("{") + + Line(" return (new QT_TYPENAME SelectStoredFunctorCall" + Counter() + "<T, " + + functionPointerType + argumentTypes + ">::type(functionPointer" + arguments + "))->start();") + + Line("}"); + functions.setRepeatCount(repeats); + + // function objects by value + Repeater functionObjects = Line ("template <typename FunctionObject" + typenameArgumentTypes + ">") + + Line ("QFuture<typename FunctionObject::result_type> run(FunctionObject functionObject" + functionParameters + ")") + + Line("{") + + Line(" return (new QT_TYPENAME SelectStoredFunctorCall" + Counter() + + "<QT_TYPENAME FunctionObject::result_type, FunctionObject" + + argumentTypes + ">::type(functionObject" + arguments + "))->start();") + + Line("}"); + functionObjects.setRepeatCount(repeats); + + // function objects by pointer + Repeater functionObjectsPointer = Line ("template <typename FunctionObject" + typenameArgumentTypes + ">") + + Line ("QFuture<typename FunctionObject::result_type> run(FunctionObject *functionObject" + functionParameters + ")") + + Line("{") + + Line(" return (new QT_TYPENAME SelectStoredFunctorPointerCall" + Counter() + + "<QT_TYPENAME FunctionObject::result_type, FunctionObject" + + argumentTypes + ">::type(functionObject" + arguments + "))->start();") + + Line("}"); + functionObjectsPointer.setRepeatCount(repeats); + + // member functions by value + Repeater memberFunction = Line ("template <typename T, typename Class" + typenameTypes + ">") + + Line ("QFuture<T> run(const Class &object, T (Class::*fn)(" + parameterTypesNoPrefix + ")" + functionParameters + ")") + + Line("{") + + Line(" return (new QT_TYPENAME SelectStoredMemberFunctionCall" + Counter() + + "<T, Class" + + types + ">::type(fn, object" + arguments + "))->start();") + + Line("}"); + memberFunction.setRepeatCount(repeats); + + // const member functions by value + Repeater constMemberFunction = Line ("template <typename T, typename Class" + typenameTypes + ">") + + Line ("QFuture<T> run(const Class &object, T (Class::*fn)(" + parameterTypesNoPrefix + ") const" + functionParameters + ")") + + Line("{") + + Line(" return (new QT_TYPENAME SelectStoredConstMemberFunctionCall" + Counter() + + "<T, Class" + + types + ">::type(fn, object" + arguments + "))->start();") + + Line("}"); + constMemberFunction.setRepeatCount(repeats); + + // member functions by class pointer + Repeater memberFunctionPointer = Line ("template <typename T, typename Class" + typenameTypes + ">") + + Line ("QFuture<T> run(Class *object, T (Class::*fn)(" + parameterTypesNoPrefix + ")" + functionParameters + ")") + + Line("{") + + Line(" return (new QT_TYPENAME SelectStoredMemberFunctionPointerCall" + Counter() + + "<T, Class" + + types + ">::type(fn, object" + arguments + "))->start();") + + Line("}"); + memberFunctionPointer.setRepeatCount(repeats); + + // const member functions by class pointer + Repeater constMemberFunctionPointer = Line ("template <typename T, typename Class" + typenameTypes + ">") + + Line ("QFuture<T> run(const Class *object, T (Class::*fn)(" + parameterTypesNoPrefix + ") const" + functionParameters + ")") + + Line("{") + + Line(" return (new QT_TYPENAME SelectStoredConstMemberFunctionPointerCall" + Counter() + + "<T, Class" + + types + ">::type(fn, object" + arguments + "))->start();") + + Line("}"); + constMemberFunctionPointer.setRepeatCount(repeats); + + + Item interfaceFunctionPointerType = "void (*)(QFutureInterface<T> &" + argumentTypes + ")"; + Item interfaceFunctionPointerParameter = "void (*functionPointer)(QFutureInterface<T> &" + argumentTypes + ")"; +/* + // QFutureInterface functions + Repeater interfaceFunctions = Line ("template <typename T" + typenameTypes + ">") + + Line ("QFuture<T> run(" + interfaceFunctionPointerParameter + functionParameters + ")") + + Line("{") + + Line(" return (new StoredInterfaceFunctionCall" + Counter() + "<T, " + + interfaceFunctionPointerType + typenameArgumentTypes + ">(functionPointer" + arguments + "))->start();") + + Line("}"); + functions.setRepeatCount(repeats); + interfaceFunctions.setRepeatCount(repeats); + + // member functions by class pointer + Repeater interfaceMemberFunction = Line ("template <typename Class, typename T" + typenameTypes + ">") + + Line ("QFuture<T> run(void (Class::*fn)(QFutureInterface<T> &), Class *object" + functionParameters + ")") + + Line("{") + + Line(" return (new StoredInterfaceMemberFunctionCall" + Counter() + + "<T, void (Class::*)(QFutureInterface<T> &), Class" + + typenameArgumentTypes + ">(fn, object" + arguments + "))->start();") + + Line("}"); + memberFunctionPointer.setRepeatCount(repeats); +*/ + return functions + Line("") + functionObjects + Line("") + functionObjectsPointer + Line("") + + memberFunction + Line("") + constMemberFunction + Line("") + + memberFunctionPointer + Line("") + constMemberFunctionPointer + Line("") + /* + interfaceFunctions + Line("") + interfaceMemberFunction + Line("")*/ + ; +} + + +Item functions(Item className, Item functorType, Item callLine) +{ + return + Line("template <typename T, typename FunctionPointer" + typenameArgumentTypes + ">") + + Line("struct " + className + Counter() +": public RunFunctionTask<T>") + + Line("{") + + Line(" inline " + className + Counter() + "(" + functorType + " function" + functionParameters +")") + + Line(" : function(function)" + initializers + " {}") + + Line(" void runFunctor() {" + callLine + argumentsNoPrefix + "); }") + + Line(" " + functorType + " function;") + + Line( classData) + + Line("};") + + Line(""); +} + +Item functionSelector(Item classNameBase) +{ + return + Line("template <typename T, typename FunctionPointer" + typenameArgumentTypes + ">") + + Line("struct Select" + classNameBase + Counter()) + + Line("{") + + Line(" typedef typename SelectSpecialization<T>::template") + + Line(" Type<" + classNameBase + Counter() + " <T, FunctionPointer" + argumentTypes + ">,") + + Line(" Void" + classNameBase + Counter() + "<T, FunctionPointer" + argumentTypes + "> >::type type;") + + Line("};"); +} + +Item memberFunctions(Item className, Item constFunction, Item objectArgument, Item objectMember, Item callLine) +{ + return + Line("template <typename T, typename Class" + typenameTypes + ">") + + Line("class " + className + Counter() + " : public RunFunctionTask<T>") + + Line("{") + + Line("public:")+ + Line(" " + className + Counter() + "(T (Class::*fn)(" + parameterTypesNoPrefix + ") " + constFunction + ", " + objectArgument + functionParameters + ")") + + Line(" : fn(fn), object(object)" + initializers + "{ }" ) + + Line("")+ + Line(" void runFunctor()")+ + Line(" {")+ + Line(" " + callLine + argumentsNoPrefix + ");")+ + Line(" }")+ + Line("private:")+ + Line(" T (Class::*fn)(" + parameterTypesNoPrefix + ")" + constFunction + ";")+ + Line(" " + objectMember + ";") + + Line( classData) + + Line("};"); +} + +Item memberFunctionSelector(Item classNameBase) +{ + return + Line("template <typename T, typename Class" + typenameTypes + ">") + + Line("struct Select" + classNameBase + Counter()) + + Line("{") + + Line(" typedef typename SelectSpecialization<T>::template") + + Line(" Type<" + classNameBase + Counter() + " <T, Class" + types + ">,") + + Line(" Void" + classNameBase + Counter() + "<T, Class" + types + "> >::type type;") + + Line("};"); +} + +Item generateSFCs(int repeats) +{ + + Item functionPointerTypedef = "typedef void (*FunctionPointer)(" + argumentTypesNoPrefix + ");"; + + Repeater dataStructures = + + // Function objects by value + functions(Item("StoredFunctorCall"), Item("FunctionPointer"), Item(" this->result = function(")) + + functions(Item("VoidStoredFunctorCall"), Item("FunctionPointer"), Item(" function(")) + + functionSelector(Item("StoredFunctorCall")) + + + // Function objects by pointer + functions(Item("StoredFunctorPointerCall"), Item("FunctionPointer *"), Item(" this->result =(*function)(")) + + functions(Item("VoidStoredFunctorPointerCall"), Item("FunctionPointer *"), Item("(*function)(")) + + functionSelector(Item("StoredFunctorPointerCall")) + + + // Member functions by value + memberFunctions(Item("StoredMemberFunctionCall"), Item(""), Item("const Class &object"), Item("Class object"), Item("this->result = (object.*fn)(")) + + memberFunctions(Item("VoidStoredMemberFunctionCall"), Item(""), Item("const Class &object"), Item("Class object"), Item("(object.*fn)(")) + + memberFunctionSelector(Item("StoredMemberFunctionCall")) + + + // Const Member functions by value + memberFunctions(Item("StoredConstMemberFunctionCall"), Item("const"), Item("const Class &object"), Item("const Class object"), Item("this->result = (object.*fn)(")) + + memberFunctions(Item("VoidStoredConstMemberFunctionCall"), Item("const"), Item("const Class &object"), Item("const Class object"), Item("(object.*fn)(")) + + memberFunctionSelector(Item("StoredConstMemberFunctionCall")) + + + // Member functions by pointer + memberFunctions(Item("StoredMemberFunctionPointerCall"), Item(""), Item("Class *object"), Item("Class *object"), Item("this->result = (object->*fn)(")) + + memberFunctions(Item("VoidStoredMemberFunctionPointerCall"), Item(""), Item("Class *object"), Item("Class *object"), Item("(object->*fn)(")) + + memberFunctionSelector(Item("StoredMemberFunctionPointerCall")) + + + // const member functions by pointer + memberFunctions(Item("StoredConstMemberFunctionPointerCall"), Item("const"), Item("Class const *object"), Item("Class const *object"), Item("this->result = (object->*fn)(")) + + memberFunctions(Item("VoidStoredConstMemberFunctionPointerCall"), Item("const"), Item("Class const *object"), Item("Class const *object"), Item("(object->*fn)(")) + + memberFunctionSelector(Item("StoredConstMemberFunctionPointerCall")); + + dataStructures.setRepeatCount(repeats); + return dataStructures; +} + +void writeFile(QString fileName, QByteArray contents) +{ + QFile runFile(fileName); + if (runFile.open(QIODevice::WriteOnly) == false) { + qDebug() << "Write to" << fileName << "failed"; + return; + } + + runFile.write(contents); + runFile.close(); + qDebug() << "Write to" << fileName << "Ok"; +} + +Item dollarQuote(Item item) +{ + return Item("$") + item + Item("$"); +} + +int main() +{ + const int repeats = 6; + init(); + Item run = ( + Line("/****************************************************************************") + + Line("**") + + Line("** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).") + + Line("** Contact: Qt Software Information (qt-info@nokia.com)") + + Line("**") + + Line("** This file is part of the " + dollarQuote("MODULE") + " of the Qt Toolkit.") + + Line("**") + + Line("** " + dollarQuote("TROLLTECH_DUAL_LICENSE")) + + Line("**") + + Line("****************************************************************************/") + + Line("") + + Line("// Generated code, do not edit! Use generator at tools/qtconcurrent/generaterun/") + + Line("#ifndef QTCONCURRENT_RUN_H") + + Line("#define QTCONCURRENT_RUN_H") + + Line("") + + Line("#ifndef QT_NO_CONCURRENT") + + Line("") + + Line("#include <QtCore/qtconcurrentrunbase.h>") + + Line("#include <QtCore/qtconcurrentstoredfunctioncall.h>") + + Line("") + + Line("QT_BEGIN_HEADER") + + Line("QT_BEGIN_NAMESPACE") + + Line("") + + Line("QT_MODULE(Core)") + + Line("") + + Line("#ifdef qdoc") + + Line("") + + Line("namespace QtConcurrent {") + + Line("") + + Line(" template <typename T>") + + Line(" QFuture<T> run(Function function, ...);") + + Line("") + + Line("} // namespace QtConcurrent") + + Line("") + + Line("#else") + + Line("") + + Line("namespace QtConcurrent {") + + Line("") + + generateRunFunctions(repeats) + + Line("} //namespace QtConcurrent") + + Line("") + + Line("#endif // qdoc") + + Line("") + + Line("QT_END_NAMESPACE") + + Line("QT_END_HEADER") + + Line("") + + Line("#endif") + ); + + writeFile("../../../src/corelib/concurrent/qtconcurrentrun.h", run.generate()); + + Item storedFunctionCall = ( + Line("/****************************************************************************") + + Line("**") + + Line("** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).") + + Line("** Contact: Qt Software Information (qt-info@nokia.com)") + + Line("**") + + Line("** This file is part of the " + dollarQuote("MODULE") + " of the Qt Toolkit.") + + Line("**") + + Line("** " + dollarQuote("TROLLTECH_DUAL_LICENSE")) + + Line("**") + + Line("****************************************************************************/") + + Line("") + + Line("// Generated code, do not edit! Use generator at tools/qtconcurrent/generaterun/") + + Line("#ifndef QTCONCURRENT_STOREDFUNCTIONCALL_H") + + Line("#define QTCONCURRENT_STOREDFUNCTIONCALL_H") + + Line("") + + Line("#include <QtCore/qglobal.h>") + + Line("") + + Line("#ifndef QT_NO_CONCURRENT") + + Line("#include <QtCore/qtconcurrentrunbase.h>") + + Line("") + + Line("QT_BEGIN_HEADER") + + Line("QT_BEGIN_NAMESPACE") + + Line("") + + Line("QT_MODULE(Core)") + + Line("") + + Line("#ifndef qdoc") + + Line("") + + Line("namespace QtConcurrent {") + + generateSFCs(repeats) + + Line("} //namespace QtConcurrent") + + Line("") + + Line("#endif // qdoc") + + Line("") + + Line("QT_END_NAMESPACE") + + Line("QT_END_HEADER") + + Line("") + + Line("#endif // QT_NO_CONCURRENT") + + Line("") + + Line("#endif") + ); + + writeFile("../../../src/corelib/concurrent/qtconcurrentstoredfunctioncall.h", storedFunctionCall.generate()); +} + + diff --git a/tools/qtconcurrent/generaterun/run.pro b/tools/qtconcurrent/generaterun/run.pro new file mode 100644 index 0000000..76bc661 --- /dev/null +++ b/tools/qtconcurrent/generaterun/run.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +include(../codegenerator/codegenerator.pri) + +# Input +SOURCES += main.cpp |