diff options
Diffstat (limited to 'src/concurrent')
-rw-r--r-- | src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp | 14 | ||||
-rw-r--r-- | src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp | 14 | ||||
-rw-r--r-- | src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp | 5 | ||||
-rw-r--r-- | src/concurrent/qtconcurrentfilter.cpp | 18 | ||||
-rw-r--r-- | src/concurrent/qtconcurrentmap.cpp | 18 | ||||
-rw-r--r-- | src/concurrent/qtconcurrentrun.cpp | 20 | ||||
-rw-r--r-- | src/concurrent/qtconcurrentrun.h | 24 |
7 files changed, 29 insertions, 84 deletions
diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp index d0deed4cc8..9b15eeaa99 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp @@ -145,19 +145,11 @@ bool QString::contains(const QRegularExpression ®exp) const; //! [9] -//! [10] -std::bind(&QString::contains, QRegularExpression("^\\S+$")); // matches strings without whitespace -//! [10] - - -//! [11] -bool contains(const QString &string) -//! [11] - - //! [12] QStringList strings = ...; -std::bind(static_cast<bool(QString::*)(const QRegularExpression&)>( &QString::contains ), QRegularExpression("...")); +QFuture<QString> future = QtConcurrent::filtered(list, [](const QString &str) { + return str.contains(QRegularExpression("^\\S+$")); // matches strings without whitespace +}); //! [12] //! [13] diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp index 91e76be0db..183b82bb9a 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp @@ -158,19 +158,11 @@ QImage QImage::scaledToWidth(int width, Qt::TransformationMode) const; //! [10] -//! [11] -std::bind(&QImage::scaledToWidth, 100, Qt::SmoothTransformation) -//! [11] - - -//! [12] -QImage scaledToWith(const QImage &image) -//! [12] - - //! [13] QList<QImage> images = ...; -QFuture<QImage> thumbnails = QtConcurrent::mapped(images, std::bind(&QImage::scaledToWidth, 100, Qt::SmoothTransformation)); +QFuture<QImage> thumbnails = QtConcurrent::mapped(images, [](const QImage &img) { + return img.scaledToWidth(100, Qt::SmoothTransformation); +}); //! [13] //! [14] diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp index 78e4591b0a..5437822842 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp @@ -109,7 +109,8 @@ future.waitForFinished(); //! [6] -void someFunction(int arg1, double arg2); -QFuture<void> future = QtConcurrent::run(std::bind(someFunction, 1, 2.0)); +QFuture<void> future = QtConcurrent::run([=]() { + // Code in this block will run in another thread +}); ... //! [6] diff --git a/src/concurrent/qtconcurrentfilter.cpp b/src/concurrent/qtconcurrentfilter.cpp index a660815daf..3e3ed7cf68 100644 --- a/src/concurrent/qtconcurrentfilter.cpp +++ b/src/concurrent/qtconcurrentfilter.cpp @@ -148,13 +148,11 @@ \snippet code/src_concurrent_qtconcurrentfilter.cpp 13 - \section2 Using Bound Function Arguments + \section2 Wrapping Functions that Take Multiple Arguments If you want to use a filter function takes more than one argument, you can - use std::bind() to transform it onto a function that takes one argument. If - C++11 support is not available, \l{http://www.boost.org/libs/bind/bind.html} - {boost::bind()} or \l{http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf} - {std::tr1::bind()} are suitable replacements. + use a lambda function or \c std::bind() to transform it onto a function that + takes one argument. As an example, we use QString::contains(): @@ -166,16 +164,6 @@ use QString::contains() with QtConcurrent::filtered() we have to provide a value for the \e regexp argument: - \snippet code/src_concurrent_qtconcurrentfilter.cpp 10 - - The return value from std::bind() is a function object (functor) with - the following signature: - - \snippet code/src_concurrent_qtconcurrentfilter.cpp 11 - - This matches what QtConcurrent::filtered() expects, and the complete - example becomes: - \snippet code/src_concurrent_qtconcurrentfilter.cpp 12 */ diff --git a/src/concurrent/qtconcurrentmap.cpp b/src/concurrent/qtconcurrentmap.cpp index e6c347b511..884bf4b4f9 100644 --- a/src/concurrent/qtconcurrentmap.cpp +++ b/src/concurrent/qtconcurrentmap.cpp @@ -198,13 +198,11 @@ \snippet code/src_concurrent_qtconcurrentmap.cpp 14 - \section2 Using Bound Function Arguments + \section2 Wrapping Functions that Take Multiple Arguments If you want to use a map function that takes more than one argument you can - use std::bind() to transform it onto a function that takes one argument. If - C++11 support is not available, \l{http://www.boost.org/libs/bind/bind.html} - {boost::bind()} or \l{http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf} - {std::tr1::bind()} are suitable replacements. + use a lambda function or \c std::bind() to transform it onto a function that + takes one argument. As an example, we'll use QImage::scaledToWidth(): @@ -216,16 +214,6 @@ QImage::scaledToWidth() with QtConcurrent::mapped() we have to provide a value for the \e{width} and the \e{transformation mode}: - \snippet code/src_concurrent_qtconcurrentmap.cpp 11 - - The return value from std::bind() is a function object (functor) with - the following signature: - - \snippet code/src_concurrent_qtconcurrentmap.cpp 12 - - This matches what QtConcurrent::mapped() expects, and the complete example - becomes: - \snippet code/src_concurrent_qtconcurrentmap.cpp 13 */ diff --git a/src/concurrent/qtconcurrentrun.cpp b/src/concurrent/qtconcurrentrun.cpp index d1208e0ffb..1d8f7afe85 100644 --- a/src/concurrent/qtconcurrentrun.cpp +++ b/src/concurrent/qtconcurrentrun.cpp @@ -107,25 +107,9 @@ \snippet code/src_concurrent_qtconcurrentrun.cpp 5 - \section2 Using Bound Function Arguments + \section2 Using Lambda Functions - You can use std::bind() to \e bind a number of arguments to a function when - called. If C++11 support is not available, \l{http://www.boost.org/libs/bind/bind.html} - {boost::bind()} or \l{http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf} - {std::tr1::bind()} are suitable replacements. - - There are number of reasons for binding: - - \list - \li To call a function that takes more than 5 arguments. - \li To simplify calling a function with constant arguments. - \li Changing the order of arguments. - \endlist - - See the documentation for the relevant functions for details on how to use - the bind API. - - Calling a bound function is done like this: + Calling a lambda function is done like this: \snippet code/src_concurrent_qtconcurrentrun.cpp 6 */ diff --git a/src/concurrent/qtconcurrentrun.h b/src/concurrent/qtconcurrentrun.h index 4b45a02b50..8e7c495a0f 100644 --- a/src/concurrent/qtconcurrentrun.h +++ b/src/concurrent/qtconcurrentrun.h @@ -101,7 +101,7 @@ QFuture<T> run(T (*functionPointer)(Param1, Param2, Param3, Param4, Param5), con #if defined(Q_COMPILER_DECLTYPE) && defined(Q_COMPILER_AUTO_FUNCTION) template <typename Functor> -auto run(Functor functor) -> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor())> >::Type +auto run(Functor functor) -> typename std::enable_if<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor())>>::type { typedef decltype(functor()) result_type; return (new StoredFunctorCall0<result_type, Functor>(functor))->start(); @@ -109,7 +109,7 @@ auto run(Functor functor) -> typename QtPrivate::QEnableIf<!QtPrivate::HasResult template <typename Functor, typename Arg1> auto run(Functor functor, const Arg1 &arg1) - -> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor(arg1))> >::Type + -> typename std::enable_if<!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(); @@ -117,7 +117,7 @@ auto run(Functor functor, const Arg1 &arg1) 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 + -> typename std::enable_if<!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(); @@ -125,7 +125,7 @@ auto run(Functor functor, const Arg1 &arg1, const Arg2 &arg2) 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 + -> typename std::enable_if<!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(); @@ -133,7 +133,7 @@ auto run(Functor functor, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3) 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 + -> typename std::enable_if<!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(); @@ -141,7 +141,7 @@ auto run(Functor functor, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, 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 + -> typename std::enable_if<!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(); @@ -372,7 +372,7 @@ QFuture<T> run(QThreadPool *pool, T (*functionPointer)(Param1, Param2, Param3, P #if defined(Q_COMPILER_DECLTYPE) && defined(Q_COMPILER_AUTO_FUNCTION) template <typename Functor> -auto run(QThreadPool *pool, Functor functor) -> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor())> >::Type +auto run(QThreadPool *pool, Functor functor) -> typename std::enable_if<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor())>>::type { typedef decltype(functor()) result_type; return (new StoredFunctorCall0<result_type, Functor>(functor))->start(pool); @@ -380,7 +380,7 @@ auto run(QThreadPool *pool, Functor functor) -> typename QtPrivate::QEnableIf<!Q template <typename Functor, typename Arg1> auto run(QThreadPool *pool, Functor functor, const Arg1 &arg1) - -> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor(arg1))> >::Type + -> typename std::enable_if<!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(pool); @@ -388,7 +388,7 @@ auto run(QThreadPool *pool, Functor functor, const Arg1 &arg1) template <typename Functor, typename Arg1, typename Arg2> auto run(QThreadPool *pool, Functor functor, const Arg1 &arg1, const Arg2 &arg2) - -> typename QtPrivate::QEnableIf<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor(arg1, arg2))> >::Type + -> typename std::enable_if<!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(pool); @@ -396,7 +396,7 @@ auto run(QThreadPool *pool, Functor functor, const Arg1 &arg1, const Arg2 &arg2) template <typename Functor, typename Arg1, typename Arg2, typename Arg3> auto run(QThreadPool *pool, 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 + -> typename std::enable_if<!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(pool); @@ -404,7 +404,7 @@ auto run(QThreadPool *pool, Functor functor, const Arg1 &arg1, const Arg2 &arg2, template <typename Functor, typename Arg1, typename Arg2, typename Arg3, typename Arg4> auto run(QThreadPool *pool, 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 + -> typename std::enable_if<!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(pool); @@ -412,7 +412,7 @@ auto run(QThreadPool *pool, Functor functor, const Arg1 &arg1, const Arg2 &arg2, template <typename Functor, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5> auto run(QThreadPool *pool, 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 + -> typename std::enable_if<!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(pool); |