diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2020-02-26 10:40:02 +0100 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2020-03-31 15:28:23 +0200 |
commit | 44ceb56455c82df3e6b1c9a2fa373cac14a039f8 (patch) | |
tree | 74f61f7adae376af0f2a9ee256a911e2bfc1ee9b /src/concurrent/qtconcurrentrunbase.h | |
parent | 986cfe312e4c01259f9a81c00dadebb10bc27ac9 (diff) |
QFuture - add ability to move results from QFuture
QFuture's original design pre-dates C++11 and its
introduction of move semantics. QFuture is documented
as requiring copy-constructible classes and uses copy
operations for results (which in Qt's universe in general
is relatively cheap, due to the use of COW/data sharing).
QFuture::result(), QFuture::results(), QFuture::resultAt()
return copies. Now that the year is 2020, it makes some
sense to add support for move semantics and, in particular,
move-only types, like std::unique_ptr (that cannot be
obtained from QFuture using result etc.). Taking a result
or results from a QFuture renders it invalid. This patch
adds QFuture<T>::takeResults(), takeResult() and isValid().
'Taking' functions are 'enabled_if' for non-void types only
to improve the compiler's diagnostic (which would otherwise
spit some semi-articulate diagnostic).
As a bonus a bug was found in the pre-existing code (after
initially copy and pasted into the new function) - the one
where we incorrectly report ready results in (rather obscure)
filter mode.
Fixes: QTBUG-81941
Fixes: QTBUG-83182
Change-Id: I8ccdfc50aa310a3a79eef2cdc55f5ea210f889c3
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/concurrent/qtconcurrentrunbase.h')
-rw-r--r-- | src/concurrent/qtconcurrentrunbase.h | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/concurrent/qtconcurrentrunbase.h b/src/concurrent/qtconcurrentrunbase.h index e84c0cdb67..5e9fa0aa00 100644 --- a/src/concurrent/qtconcurrentrunbase.h +++ b/src/concurrent/qtconcurrentrunbase.h @@ -48,6 +48,9 @@ #include <QtCore/qrunnable.h> #include <QtCore/qthreadpool.h> +#include <type_traits> +#include <utility> + QT_BEGIN_NAMESPACE @@ -123,7 +126,11 @@ public: } #endif - this->reportResult(result); + if constexpr (std::is_move_constructible_v<T>) + this->reportAndMoveResult(std::move(result)); + else if constexpr (std::is_copy_constructible_v<T>) + this->reportResult(result); + this->reportFinished(); } T result; |