Doc: Replace std::bind() with lambdas in Qt Concurrent

Lambda functions provide all the benefits of std::bind() plus more

Change-Id: Iec30b20bb35ac0fae560435b323f80af2f2e5fce
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Sze Howe Koh 2017-03-02 22:41:29 +08:00
parent 9390c44338
commit 58cc126def
6 changed files with 17 additions and 72 deletions

View File

@ -145,19 +145,11 @@ bool QString::contains(const QRegularExpression &regexp) 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]

View File

@ -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]

View File

@ -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]

View File

@ -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
*/

View File

@ -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
*/

View File

@ -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
*/