Support of lambdas in QtConcurrent::run

Reviewed-by: Joao
(cherry picked from commit 917f2ff617209bcc283eb3590b422bcf239c0537)
Change-Id: I837f18f043b18410c1d93b9f1156acf729dad510
Reviewed-on: http://codereview.qt.nokia.com/142
Reviewed-by: Olivier Goffart <olivier.goffart@nokia.com>
This commit is contained in:
Olivier Goffart 2011-04-18 14:07:32 +02:00 committed by Qt Continuous Integration System
parent 83291e6a07
commit d3e5fc0220
6 changed files with 194 additions and 81 deletions

View File

@ -57,6 +57,20 @@ QT_MODULE(Core)
# define QT_TYPENAME typename
#endif
namespace QtPrivate {
template<class T>
class HasResultType {
typedef char Yes;
typedef void *No;
template<typename U> static Yes test(int, const typename U::result_type * = 0);
template<typename U> static No test(double);
public:
enum { Value = (sizeof(test<T>(0)) == sizeof(Yes)) };
};
}
QT_END_NAMESPACE
QT_END_HEADER

View File

@ -195,17 +195,10 @@ QtConcurrent::ConstMemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func
return QtConcurrent::ConstMemberFunctionWrapper<T, C>(func);
}
template<typename T>
void *lazyResultType_helper(int, typename T::result_type * = 0);
template<typename T>
char lazyResultType_helper(double);
template <typename Functor, bool foo = sizeof(lazyResultType_helper<Functor>(0)) != sizeof(void*)>
template <typename Functor, bool foo = HasResultType<Functor>::Value>
struct LazyResultType { typedef typename Functor::result_type Type; };
template <typename Functor>
struct LazyResultType<Functor, true> { typedef void Type; };
struct LazyResultType<Functor, false> { typedef void Type; };
template <class T>
struct ReduceResultType;

View File

@ -71,63 +71,114 @@ namespace QtConcurrent {
template <typename T>
QFuture<T> run(T (*functionPointer)())
{
return (new QT_TYPENAME SelectStoredFunctorCall0<T, T (*)()>::type(functionPointer))->start();
return (new StoredFunctorCall0<T, T (*)()>(functionPointer))->start();
}
template <typename T, typename Param1, typename Arg1>
QFuture<T> run(T (*functionPointer)(Param1), const Arg1 &arg1)
{
return (new QT_TYPENAME SelectStoredFunctorCall1<T, T (*)(Param1), Arg1>::type(functionPointer, arg1))->start();
return (new StoredFunctorCall1<T, T (*)(Param1), Arg1>(functionPointer, arg1))->start();
}
template <typename T, typename Param1, typename Arg1, typename Param2, typename Arg2>
QFuture<T> run(T (*functionPointer)(Param1, Param2), const Arg1 &arg1, const Arg2 &arg2)
{
return (new QT_TYPENAME SelectStoredFunctorCall2<T, T (*)(Param1, Param2), Arg1, Arg2>::type(functionPointer, arg1, arg2))->start();
return (new StoredFunctorCall2<T, T (*)(Param1, Param2), Arg1, Arg2>(functionPointer, arg1, arg2))->start();
}
template <typename T, typename Param1, typename Arg1, typename Param2, typename Arg2, typename Param3, typename Arg3>
QFuture<T> run(T (*functionPointer)(Param1, Param2, Param3), const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3)
{
return (new QT_TYPENAME SelectStoredFunctorCall3<T, T (*)(Param1, Param2, Param3), Arg1, Arg2, Arg3>::type(functionPointer, arg1, arg2, arg3))->start();
return (new StoredFunctorCall3<T, T (*)(Param1, Param2, Param3), Arg1, Arg2, Arg3>(functionPointer, arg1, arg2, arg3))->start();
}
template <typename T, typename Param1, typename Arg1, typename Param2, typename Arg2, typename Param3, typename Arg3, typename Param4, typename Arg4>
QFuture<T> run(T (*functionPointer)(Param1, Param2, Param3, Param4), const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4)
{
return (new QT_TYPENAME SelectStoredFunctorCall4<T, T (*)(Param1, Param2, Param3, Param4), Arg1, Arg2, Arg3, Arg4>::type(functionPointer, arg1, arg2, arg3, arg4))->start();
return (new StoredFunctorCall4<T, T (*)(Param1, Param2, Param3, Param4), Arg1, Arg2, Arg3, Arg4>(functionPointer, arg1, arg2, arg3, arg4))->start();
}
template <typename T, typename Param1, typename Arg1, typename Param2, typename Arg2, typename Param3, typename Arg3, typename Param4, typename Arg4, typename Param5, typename Arg5>
QFuture<T> run(T (*functionPointer)(Param1, Param2, Param3, Param4, Param5), const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5)
{
return (new QT_TYPENAME SelectStoredFunctorCall5<T, T (*)(Param1, Param2, Param3, Param4, Param5), Arg1, Arg2, Arg3, Arg4, Arg5>::type(functionPointer, arg1, arg2, arg3, arg4, arg5))->start();
return (new StoredFunctorCall5<T, T (*)(Param1, Param2, Param3, Param4, Param5), Arg1, Arg2, Arg3, Arg4, Arg5>(functionPointer, arg1, arg2, arg3, arg4, arg5))->start();
}
#ifdef Q_COMPILER_DECLTYPE
template <typename Functor>
auto run(Functor functor) -> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor())> >::Type
{
typedef decltype(functor()) result_type;
return (new StoredFunctorCall0<result_type, Functor>(functor))->start();
}
template <typename Functor, typename Arg1>
auto run(Functor functor, const Arg1 &arg1)
-> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor(arg1))> >::Type
{
typedef decltype(functor(arg1)) result_type;
return (new StoredFunctorCall1<result_type, Functor, Arg1>(functor, arg1))->start();
}
template <typename Functor, typename Arg1, typename Arg2>
auto run(Functor functor, const Arg1 &arg1, const Arg2 &arg2)
-> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor(arg1, arg2))> >::Type
{
typedef decltype(functor(arg1, arg2)) result_type;
return (new StoredFunctorCall2<result_type, Functor, Arg1, Arg2>(functor, arg1, arg2))->start();
}
template <typename Functor, typename Arg1, typename Arg2, typename Arg3>
auto run(Functor functor, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3)
-> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor(arg1, arg2, arg3))> >::Type
{
typedef decltype(functor(arg1, arg2, arg3)) result_type;
return (new StoredFunctorCall3<result_type, Functor, Arg1, Arg2, Arg3>(functor, arg1, arg2, arg3))->start();
}
template <typename Functor, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
auto run(Functor functor, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4)
-> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor(arg1, arg2, arg3, arg4))> >::Type
{
typedef decltype(functor(arg1, arg2, arg3, arg4)) result_type;
return (new StoredFunctorCall4<result_type, Functor, Arg1, Arg2, Arg3, Arg4>(functor, arg1, arg2, arg3, arg4))->start();
}
template <typename Functor, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
auto run(Functor functor, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5)
-> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor(arg1, arg2, arg3, arg4, arg5))> >::Type
{
typedef decltype(functor(arg1, arg2, arg3, arg4, arg5)) result_type;
return (new StoredFunctorCall5<result_type, Functor, Arg1, Arg2, Arg3, Arg4, Arg5>(functor, arg1, arg2, arg3, arg4, arg5))->start();
}
#endif
template <typename FunctionObject>
QFuture<typename FunctionObject::result_type> run(FunctionObject functionObject)
{
return (new QT_TYPENAME SelectStoredFunctorCall0<QT_TYPENAME FunctionObject::result_type, FunctionObject>::type(functionObject))->start();
return (new StoredFunctorCall0<QT_TYPENAME FunctionObject::result_type, FunctionObject>(functionObject))->start();
}
template <typename FunctionObject, typename Arg1>
QFuture<typename FunctionObject::result_type> run(FunctionObject functionObject, const Arg1 &arg1)
{
return (new QT_TYPENAME SelectStoredFunctorCall1<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1>::type(functionObject, arg1))->start();
return (new StoredFunctorCall1<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1>(functionObject, arg1))->start();
}
template <typename FunctionObject, typename Arg1, typename Arg2>
QFuture<typename FunctionObject::result_type> run(FunctionObject functionObject, const Arg1 &arg1, const Arg2 &arg2)
{
return (new QT_TYPENAME SelectStoredFunctorCall2<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1, Arg2>::type(functionObject, arg1, arg2))->start();
return (new StoredFunctorCall2<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1, Arg2>(functionObject, arg1, arg2))->start();
}
template <typename FunctionObject, typename Arg1, typename Arg2, typename Arg3>
QFuture<typename FunctionObject::result_type> run(FunctionObject functionObject, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3)
{
return (new QT_TYPENAME SelectStoredFunctorCall3<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1, Arg2, Arg3>::type(functionObject, arg1, arg2, arg3))->start();
return (new StoredFunctorCall3<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1, Arg2, Arg3>(functionObject, arg1, arg2, arg3))->start();
}
template <typename FunctionObject, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
QFuture<typename FunctionObject::result_type> run(FunctionObject functionObject, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4)
{
return (new QT_TYPENAME SelectStoredFunctorCall4<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1, Arg2, Arg3, Arg4>::type(functionObject, arg1, arg2, arg3, arg4))->start();
return (new StoredFunctorCall4<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1, Arg2, Arg3, Arg4>(functionObject, arg1, arg2, arg3, arg4))->start();
}
template <typename FunctionObject, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
QFuture<typename FunctionObject::result_type> run(FunctionObject functionObject, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5)
{
return (new QT_TYPENAME SelectStoredFunctorCall5<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1, Arg2, Arg3, Arg4, Arg5>::type(functionObject, arg1, arg2, arg3, arg4, arg5))->start();
return (new StoredFunctorCall5<QT_TYPENAME FunctionObject::result_type, FunctionObject, Arg1, Arg2, Arg3, Arg4, Arg5>(functionObject, arg1, arg2, arg3, arg4, arg5))->start();
}
template <typename FunctionObject>

View File

@ -66,23 +66,16 @@ struct StoredFunctorCall0: public RunFunctionTask<T>
};
template <typename T, typename FunctionPointer>
struct VoidStoredFunctorCall0: public RunFunctionTask<T>
template <typename FunctionPointer>
struct StoredFunctorCall0<void, FunctionPointer>: public RunFunctionTask<void>
{
inline VoidStoredFunctorCall0(FunctionPointer _function)
inline StoredFunctorCall0(FunctionPointer _function)
: function(_function) {}
void runFunctor() { function(); }
FunctionPointer function;
};
template <typename T, typename FunctionPointer>
struct SelectStoredFunctorCall0
{
typedef typename SelectSpecialization<T>::template
Type<StoredFunctorCall0 <T, FunctionPointer>,
VoidStoredFunctorCall0<T, FunctionPointer> >::type type;
};
template <typename T, typename FunctionPointer>
struct StoredFunctorPointerCall0: public RunFunctionTask<T>
{
@ -276,23 +269,16 @@ struct StoredFunctorCall1: public RunFunctionTask<T>
Arg1 arg1;
};
template <typename T, typename FunctionPointer, typename Arg1>
struct VoidStoredFunctorCall1: public RunFunctionTask<T>
template <typename FunctionPointer, typename Arg1>
struct StoredFunctorCall1<void, FunctionPointer, Arg1>: public RunFunctionTask<void>
{
inline VoidStoredFunctorCall1(FunctionPointer _function, const Arg1 &_arg1)
inline StoredFunctorCall1(FunctionPointer _function, const Arg1 &_arg1)
: function(_function), arg1(_arg1) {}
void runFunctor() { function(arg1); }
FunctionPointer function;
Arg1 arg1;
};
template <typename T, typename FunctionPointer, typename Arg1>
struct SelectStoredFunctorCall1
{
typedef typename SelectSpecialization<T>::template
Type<StoredFunctorCall1 <T, FunctionPointer, Arg1>,
VoidStoredFunctorCall1<T, FunctionPointer, Arg1> >::type type;
};
template <typename T, typename FunctionPointer, typename Arg1>
struct StoredFunctorPointerCall1: public RunFunctionTask<T>
{
@ -486,23 +472,16 @@ struct StoredFunctorCall2: public RunFunctionTask<T>
Arg1 arg1; Arg2 arg2;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2>
struct VoidStoredFunctorCall2: public RunFunctionTask<T>
template <typename FunctionPointer, typename Arg1, typename Arg2>
struct StoredFunctorCall2<void, FunctionPointer, Arg1, Arg2>: public RunFunctionTask<void>
{
inline VoidStoredFunctorCall2(FunctionPointer _function, const Arg1 &_arg1, const Arg2 &_arg2)
inline StoredFunctorCall2(FunctionPointer _function, const Arg1 &_arg1, const Arg2 &_arg2)
: function(_function), arg1(_arg1), arg2(_arg2) {}
void runFunctor() { function(arg1, arg2); }
FunctionPointer function;
Arg1 arg1; Arg2 arg2;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2>
struct SelectStoredFunctorCall2
{
typedef typename SelectSpecialization<T>::template
Type<StoredFunctorCall2 <T, FunctionPointer, Arg1, Arg2>,
VoidStoredFunctorCall2<T, FunctionPointer, Arg1, Arg2> >::type type;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2>
struct StoredFunctorPointerCall2: public RunFunctionTask<T>
{
@ -696,23 +675,16 @@ struct StoredFunctorCall3: public RunFunctionTask<T>
Arg1 arg1; Arg2 arg2; Arg3 arg3;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3>
struct VoidStoredFunctorCall3: public RunFunctionTask<T>
template <typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3>
struct StoredFunctorCall3<void, FunctionPointer, Arg1, Arg2, Arg3>: public RunFunctionTask<void>
{
inline VoidStoredFunctorCall3(FunctionPointer _function, const Arg1 &_arg1, const Arg2 &_arg2, const Arg3 &_arg3)
inline StoredFunctorCall3(FunctionPointer _function, const Arg1 &_arg1, const Arg2 &_arg2, const Arg3 &_arg3)
: function(_function), arg1(_arg1), arg2(_arg2), arg3(_arg3) {}
void runFunctor() { function(arg1, arg2, arg3); }
FunctionPointer function;
Arg1 arg1; Arg2 arg2; Arg3 arg3;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3>
struct SelectStoredFunctorCall3
{
typedef typename SelectSpecialization<T>::template
Type<StoredFunctorCall3 <T, FunctionPointer, Arg1, Arg2, Arg3>,
VoidStoredFunctorCall3<T, FunctionPointer, Arg1, Arg2, Arg3> >::type type;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3>
struct StoredFunctorPointerCall3: public RunFunctionTask<T>
{
@ -906,23 +878,16 @@ struct StoredFunctorCall4: public RunFunctionTask<T>
Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
struct VoidStoredFunctorCall4: public RunFunctionTask<T>
template <typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
struct StoredFunctorCall4<void, FunctionPointer, Arg1, Arg2, Arg3, Arg4>: public RunFunctionTask<void>
{
inline VoidStoredFunctorCall4(FunctionPointer _function, const Arg1 &_arg1, const Arg2 &_arg2, const Arg3 &_arg3, const Arg4 &_arg4)
inline StoredFunctorCall4(FunctionPointer _function, const Arg1 &_arg1, const Arg2 &_arg2, const Arg3 &_arg3, const Arg4 &_arg4)
: function(_function), arg1(_arg1), arg2(_arg2), arg3(_arg3), arg4(_arg4) {}
void runFunctor() { function(arg1, arg2, arg3, arg4); }
FunctionPointer function;
Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
struct SelectStoredFunctorCall4
{
typedef typename SelectSpecialization<T>::template
Type<StoredFunctorCall4 <T, FunctionPointer, Arg1, Arg2, Arg3, Arg4>,
VoidStoredFunctorCall4<T, FunctionPointer, Arg1, Arg2, Arg3, Arg4> >::type type;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
struct StoredFunctorPointerCall4: public RunFunctionTask<T>
{
@ -1116,23 +1081,16 @@ struct StoredFunctorCall5: public RunFunctionTask<T>
Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4; Arg5 arg5;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
struct VoidStoredFunctorCall5: public RunFunctionTask<T>
template <typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
struct StoredFunctorCall5<void, FunctionPointer, Arg1, Arg2, Arg3, Arg4, Arg5>: public RunFunctionTask<void>
{
inline VoidStoredFunctorCall5(FunctionPointer _function, const Arg1 &_arg1, const Arg2 &_arg2, const Arg3 &_arg3, const Arg4 &_arg4, const Arg5 &_arg5)
inline StoredFunctorCall5(FunctionPointer _function, const Arg1 &_arg1, const Arg2 &_arg2, const Arg3 &_arg3, const Arg4 &_arg4, const Arg5 &_arg5)
: function(_function), arg1(_arg1), arg2(_arg2), arg3(_arg3), arg4(_arg4), arg5(_arg5) {}
void runFunctor() { function(arg1, arg2, arg3, arg4, arg5); }
FunctionPointer function;
Arg1 arg1; Arg2 arg2; Arg3 arg3; Arg4 arg4; Arg5 arg5;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
struct SelectStoredFunctorCall5
{
typedef typename SelectSpecialization<T>::template
Type<StoredFunctorCall5 <T, FunctionPointer, Arg1, Arg2, Arg3, Arg4, Arg5>,
VoidStoredFunctorCall5<T, FunctionPointer, Arg1, Arg2, Arg3, Arg4, Arg5> >::type type;
};
template <typename T, typename FunctionPointer, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
struct StoredFunctorPointerCall5: public RunFunctionTask<T>
{
@ -1316,6 +1274,33 @@ struct SelectStoredConstMemberFunctionPointerCall5
Type<StoredConstMemberFunctionPointerCall5 <T, Class, Param1, Arg1, Param2, Arg2, Param3, Arg3, Param4, Arg4, Param5, Arg5>,
VoidStoredConstMemberFunctionPointerCall5<T, Class, Param1, Arg1, Param2, Arg2, Param3, Arg3, Param4, Arg4, Param5, Arg5> >::type type;
};
template <typename T, typename Functor>
class StoredFunctorCall : public RunFunctionTask<T>
{
public:
StoredFunctorCall(const Functor &f) : functor(f) { }
void runFunctor()
{
this->result = functor();
}
private:
Functor functor;
};
template <typename Functor>
class StoredFunctorCall<void, Functor> : public RunFunctionTask<void>
{
public:
StoredFunctorCall(const Functor &f) : functor(f) { }
void runFunctor()
{
functor();
}
private:
Functor functor;
};
} //namespace QtConcurrent
#endif // qdoc

View File

@ -428,6 +428,7 @@ namespace QT_NAMESPACE {}
# define Q_COMPILER_INITIALIZER_LISTS
# define Q_COMPILER_AUTO_TYPE
# define Q_COMPILER_LAMBDA
# define Q_COMPILER_DECLTYPE
//# define Q_COMPILER_VARIADIC_TEMPLATES
//# define Q_COMPILER_CLASS_ENUM
//# define Q_COMPILER_DEFAULT_DELETE_MEMBERS
@ -524,6 +525,7 @@ namespace QT_NAMESPACE {}
# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 403
/* C++0x features supported in GCC 4.3: */
# define Q_COMPILER_RVALUE_REFS
# define Q_COMPILER_DECLTYPE
# endif
# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 404
/* C++0x features supported in GCC 4.4: */
@ -791,6 +793,7 @@ namespace QT_NAMESPACE {}
# if __INTEL_COMPILER >= 1100
# define Q_COMPILER_RVALUE_REFS
# define Q_COMPILER_EXTERN_TEMPLATES
# define Q_COMPILER_DECLTYPE
# elif __INTEL_COMPILER >= 1200
# define Q_COMPILER_VARIADIC_TEMPLATES
# define Q_COMPILER_AUTO_TYPE

View File

@ -67,6 +67,8 @@ private slots:
#if 0
void createFunctor();
#endif
void functor();
void lambda();
};
#if 0
@ -444,6 +446,71 @@ void tst_QtConcurrentRun::createFunctor()
}
#endif
struct Functor {
int operator()() { return 42; }
double operator()(double a, double b) { return a/b; }
int operator()(int a, int b) { return a/b; }
void operator()(int) { }
void operator()(int, int, int) { }
void operator()(int, int, int, int) { }
void operator()(int, int, int, int, int) { }
void operator()(int, int, int, int, int, int) { }
};
void tst_QtConcurrentRun::functor()
{
//this test functor without result_type, decltype need to be supported by the compiler
#ifndef Q_COMPILER_DECLTYPE
QSKIP("Compiler do not suport decltype", SkipAll);
#else
Functor f;
{
QFuture<int> fut = QtConcurrent::run(f);
QCOMPARE(fut.result(), 42);
}
{
QFuture<double> fut = QtConcurrent::run(f, 8.5, 1.8);
QCOMPARE(fut.result(), (8.5/1.8));
}
{
QFuture<int> fut = QtConcurrent::run(f, 19, 3);
QCOMPARE(fut.result(), int(19/3));
}
{
QtConcurrent::run(f, 1).waitForFinished();
QtConcurrent::run(f, 1,2).waitForFinished();
QtConcurrent::run(f, 1,2,3).waitForFinished();
QtConcurrent::run(f, 1,2,3,4).waitForFinished();
QtConcurrent::run(f, 1,2,3,4,5).waitForFinished();
}
#endif
}
void tst_QtConcurrentRun::lambda()
{
#ifndef Q_COMPILER_LAMBDA
QSKIP("Compiler do not suport lambda", SkipAll);
#else
QCOMPARE(QtConcurrent::run([](){ return 45; }).result(), 45);
QCOMPARE(QtConcurrent::run([](int a){ return a+15; }, 12).result(), 12+15);
QCOMPARE(QtConcurrent::run([](int a, double b){ return a + b; }, 12, 15).result(), double(12+15));
QCOMPARE(QtConcurrent::run([](int a , int, int, int, int b){ return a + b; }, 1, 2, 3, 4, 5).result(), 1 + 5);
#ifdef Q_COMPILER_INITIALIZER_LISTS
{
QString str { "Hello World Foo" };
QFuture<QStringList> f1 = QtConcurrent::run([&](){ return str.split(' '); });
auto r = f1.result();
QCOMPARE(r, QStringList({"Hello", "World", "Foo"}));
}
#endif
#endif
}
#include "tst_qtconcurrentrun.moc"
#else