From 58cc126defe4528ba171c812fd7544027190175e Mon Sep 17 00:00:00 2001 From: Sze Howe Koh Date: Thu, 2 Mar 2017 22:41:29 +0800 Subject: 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) --- .../code/src_concurrent_qtconcurrentfilter.cpp | 14 +++----------- .../snippets/code/src_concurrent_qtconcurrentmap.cpp | 14 +++----------- .../snippets/code/src_concurrent_qtconcurrentrun.cpp | 5 +++-- src/concurrent/qtconcurrentfilter.cpp | 18 +++--------------- src/concurrent/qtconcurrentmap.cpp | 18 +++--------------- src/concurrent/qtconcurrentrun.cpp | 20 ++------------------ 6 files changed, 17 insertions(+), 72 deletions(-) (limited to 'src/concurrent') 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( &QString::contains ), QRegularExpression("...")); +QFuture 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 images = ...; -QFuture thumbnails = QtConcurrent::mapped(images, std::bind(&QImage::scaledToWidth, 100, Qt::SmoothTransformation)); +QFuture 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 future = QtConcurrent::run(std::bind(someFunction, 1, 2.0)); +QFuture 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 */ -- cgit v1.2.3