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/corelib/thread/qresultstore.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/corelib/thread/qresultstore.h')
-rw-r--r-- | src/corelib/thread/qresultstore.h | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/corelib/thread/qresultstore.h b/src/corelib/thread/qresultstore.h index 7a65089396..c150876e74 100644 --- a/src/corelib/thread/qresultstore.h +++ b/src/corelib/thread/qresultstore.h @@ -43,6 +43,8 @@ #include <QtCore/qmap.h> #include <QtCore/qdebug.h> +#include <utility> + QT_REQUIRE_CONFIG(future); QT_BEGIN_NAMESPACE @@ -97,6 +99,19 @@ public: return *pointer<T>(); } + template<typename T> + T &value() + { + return *pointer<T>(); + } + + template <typename T> + T *pointer() + { + const T *p = qAsConst(*this).pointer<T>(); + return const_cast<T *>(p); + } + template <typename T> const T *pointer() const { @@ -144,8 +159,14 @@ public: { if (result == nullptr) return addResult(index, static_cast<void *>(nullptr)); - else - return addResult(index, static_cast<void *>(new T(*result))); + + return addResult(index, static_cast<void *>(new T(*result))); + } + + template <typename T> + int moveResult(int index, T &&result) + { + return addResult(index, static_cast<void *>(new T(std::move_if_noexcept(result)))); } template <typename T> @@ -159,8 +180,8 @@ public: { if (m_filterMode == true && results->count() != totalCount && 0 == results->count()) return addResults(index, nullptr, 0, totalCount); - else - return addResults(index, new QVector<T>(*results), results->count(), totalCount); + + return addResults(index, new QVector<T>(*results), results->count(), totalCount); } int addCanceledResult(int index) |