diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-11-21 15:51:34 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-01-31 19:20:43 +0100 |
commit | c76dd72dc6b78c34edb4dfc5196bb93a51cc488f (patch) | |
tree | 9a5539a13c95a6f61bdb2e9e07bd7df12daff0e1 /src/corelib/thread/qrunnable.cpp | |
parent | 6c4e0f2d2de5f877f698a0faa7bd0d027b7320c9 (diff) |
Add a constructor for QRunnable from anonymous functions
This makes it easier to create one without having to create
a derivative class. The patch also adds a path to avoid using
QRunnable directly in QThreadPool.
Change-Id: I9caa7dabb6f641b547d4771c863aa6ab7f01b704
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src/corelib/thread/qrunnable.cpp')
-rw-r--r-- | src/corelib/thread/qrunnable.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/corelib/thread/qrunnable.cpp b/src/corelib/thread/qrunnable.cpp index bd0a32b53d..58a764d407 100644 --- a/src/corelib/thread/qrunnable.cpp +++ b/src/corelib/thread/qrunnable.cpp @@ -114,4 +114,31 @@ QRunnable::~QRunnable() \sa autoDelete(), QThreadPool */ +class FunctionRunnable : public QRunnable +{ + std::function<void()> m_functor; +public: + FunctionRunnable(std::function<void()> functor) : m_functor(std::move(functor)) + { + } + void run() override + { + m_functor(); + } +}; + +/*! + \since 5.15 + + Creates a QRunnable that calls \a fun in run(). + + Auto-deletion is enabled by default. + + \sa run(), autoDelete() +*/ +QRunnable *QRunnable::create(std::function<void()> fun) +{ + return new FunctionRunnable(std::move(fun)); +} + QT_END_NAMESPACE |