summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qfutureinterface.cpp
diff options
context:
space:
mode:
authorTimur Pocheptsov <timur.pocheptsov@qt.io>2020-02-26 10:40:02 +0100
committerTimur Pocheptsov <timur.pocheptsov@qt.io>2020-03-31 15:28:23 +0200
commit44ceb56455c82df3e6b1c9a2fa373cac14a039f8 (patch)
tree74f61f7adae376af0f2a9ee256a911e2bfc1ee9b /src/corelib/thread/qfutureinterface.cpp
parent986cfe312e4c01259f9a81c00dadebb10bc27ac9 (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/qfutureinterface.cpp')
-rw-r--r--src/corelib/thread/qfutureinterface.cpp19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/corelib/thread/qfutureinterface.cpp b/src/corelib/thread/qfutureinterface.cpp
index 074e28d8df..9d00bc3271 100644
--- a/src/corelib/thread/qfutureinterface.cpp
+++ b/src/corelib/thread/qfutureinterface.cpp
@@ -114,6 +114,7 @@ void QFutureInterfaceBase::cancel()
d->waitCondition.wakeAll();
d->pausedWaitCondition.wakeAll();
d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Canceled));
+ d->isValid = false;
}
void QFutureInterfaceBase::setPaused(bool paused)
@@ -191,6 +192,12 @@ bool QFutureInterfaceBase::isResultReadyAt(int index) const
return d->internal_isResultReadyAt(index);
}
+bool QFutureInterfaceBase::isValid() const
+{
+ const QMutexLocker lock(&d->m_mutex);
+ return d->isValid;
+}
+
bool QFutureInterfaceBase::isRunningOrPending() const
{
return queryState(static_cast<State>(Running | Pending));
@@ -263,9 +270,9 @@ void QFutureInterfaceBase::reportStarted()
QMutexLocker locker(&d->m_mutex);
if (d->state.loadRelaxed() & (Started|Canceled|Finished))
return;
-
d->setState(State(Started | Running));
d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Started));
+ d->isValid = true;
}
void QFutureInterfaceBase::reportCanceled()
@@ -473,6 +480,16 @@ bool QFutureInterfaceBase::derefT() const
return d->refCount.derefT();
}
+void QFutureInterfaceBase::reset()
+{
+ d->m_progressValue = 0;
+ d->m_progressMinimum = 0;
+ d->m_progressMaximum = 0;
+ d->setState(QFutureInterfaceBase::NoState);
+ d->progressTime.invalidate();
+ d->isValid = false;
+}
+
QFutureInterfaceBasePrivate::QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState)
: refCount(1), m_progressValue(0), m_progressMinimum(0), m_progressMaximum(0),
state(initialState),