Introduce swap functions for QPromise/QFutureInterface
Made QPromise::swap public, added free standing swap() for QFutureInterface and QPromise. Updated QPromise special member functions. Extended tests Task-number: QTBUG-84977 Change-Id: I5daf6876df306d082441dbcdf5ae4dee3bfc0ead Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
This commit is contained in:
parent
fddb0c895f
commit
b3c1093751
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2020 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the QtCore module of the Qt Toolkit.
|
** This file is part of the QtCore module of the Qt Toolkit.
|
||||||
@ -545,6 +545,11 @@ QFutureInterfaceBase &QFutureInterfaceBase::operator=(const QFutureInterfaceBase
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QFutureInterfaceBase::swap(QFutureInterfaceBase &other) noexcept
|
||||||
|
{
|
||||||
|
qSwap(d, other.d);
|
||||||
|
}
|
||||||
|
|
||||||
bool QFutureInterfaceBase::refT() const
|
bool QFutureInterfaceBase::refT() const
|
||||||
{
|
{
|
||||||
return d->refCount.refT();
|
return d->refCount.refT();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2020 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the QtCore module of the Qt Toolkit.
|
** This file is part of the QtCore module of the Qt Toolkit.
|
||||||
@ -163,6 +163,8 @@ public:
|
|||||||
inline bool operator!=(const QFutureInterfaceBase &other) const { return d != other.d; }
|
inline bool operator!=(const QFutureInterfaceBase &other) const { return d != other.d; }
|
||||||
QFutureInterfaceBase &operator=(const QFutureInterfaceBase &other);
|
QFutureInterfaceBase &operator=(const QFutureInterfaceBase &other);
|
||||||
|
|
||||||
|
void swap(QFutureInterfaceBase &other) noexcept;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool refT() const;
|
bool refT() const;
|
||||||
bool derefT() const;
|
bool derefT() const;
|
||||||
@ -426,6 +428,12 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline void swap(QFutureInterface<T> &a, QFutureInterface<T> &b) noexcept
|
||||||
|
{
|
||||||
|
a.swap(b);
|
||||||
|
}
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
#endif // QFUTUREINTERFACE_H
|
#endif // QFUTUREINTERFACE_H
|
||||||
|
@ -43,6 +43,8 @@
|
|||||||
#include <QtCore/qglobal.h>
|
#include <QtCore/qglobal.h>
|
||||||
#include <QtCore/qfuture.h>
|
#include <QtCore/qfuture.h>
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
QT_REQUIRE_CONFIG(future);
|
QT_REQUIRE_CONFIG(future);
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@ -59,13 +61,12 @@ public:
|
|||||||
Q_DISABLE_COPY(QPromise)
|
Q_DISABLE_COPY(QPromise)
|
||||||
QPromise(QPromise<T> &&other) : d(other.d)
|
QPromise(QPromise<T> &&other) : d(other.d)
|
||||||
{
|
{
|
||||||
// In constructor, there's no need to perform swap(). Assign new
|
|
||||||
// QFutureInterface to other.d instead which is slightly cheaper.
|
|
||||||
other.d = QFutureInterface<T>();
|
other.d = QFutureInterface<T>();
|
||||||
}
|
}
|
||||||
QPromise& operator=(QPromise<T> &&other)
|
QPromise& operator=(QPromise<T> &&other)
|
||||||
{
|
{
|
||||||
swap(other);
|
QPromise<T> tmp(std::move(other));
|
||||||
|
tmp.swap(*this);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
~QPromise()
|
~QPromise()
|
||||||
@ -110,24 +111,25 @@ public:
|
|||||||
d.setProgressValueAndText(progressValue, progressText);
|
d.setProgressValueAndText(progressValue, progressText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void swap(QPromise<T> &other) noexcept
|
||||||
|
{
|
||||||
|
qSwap(this->d, other.d);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(Q_CLANG_QDOC) // documentation-only simplified signatures
|
#if defined(Q_CLANG_QDOC) // documentation-only simplified signatures
|
||||||
void addResult(const T &result, int index = -1) { }
|
void addResult(const T &result, int index = -1) { }
|
||||||
void addResult(T &&result, int index = -1) { }
|
void addResult(T &&result, int index = -1) { }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable QFutureInterface<T> d = QFutureInterface<T>();
|
mutable QFutureInterface<T> d = QFutureInterface<T>();
|
||||||
|
|
||||||
void swap(QPromise<T> &other)
|
|
||||||
{
|
|
||||||
// Note: copy operations are expensive! They trigger several atomic
|
|
||||||
// reference counts
|
|
||||||
auto tmp = this->d;
|
|
||||||
this->d = other.d;
|
|
||||||
other.d = tmp;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline void swap(QPromise<T> &a, QPromise<T> &b) noexcept
|
||||||
|
{
|
||||||
|
a.swap(b);
|
||||||
|
}
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
#endif // QPROMISE_H
|
#endif // QPROMISE_H
|
||||||
|
@ -229,3 +229,8 @@
|
|||||||
\sa QFuture::progressValue(), QFuture::progressText(), QFuture::cancel(),
|
\sa QFuture::progressValue(), QFuture::progressText(), QFuture::cancel(),
|
||||||
reportFinished()
|
reportFinished()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*! \fn template<typename T> void QPromise<T>::swap(QPromise<T> &other) noexcept
|
||||||
|
|
||||||
|
Swaps promise \a other with this promise. This operation is very fast and never fails.
|
||||||
|
*/
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2020 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the test suite of the Qt Toolkit.
|
** This file is part of the test suite of the Qt Toolkit.
|
||||||
@ -689,6 +689,16 @@ void tst_QFuture::futureInterface()
|
|||||||
VoidResult a;
|
VoidResult a;
|
||||||
a.run().waitForFinished();
|
a.run().waitForFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QFutureInterface<int> i1;
|
||||||
|
i1.reportResult(1);
|
||||||
|
QFutureInterface<int> i2;
|
||||||
|
i2.reportResult(2);
|
||||||
|
swap(i1, i2); // ADL must resolve this
|
||||||
|
QCOMPARE(i1.resultReference(0), 2);
|
||||||
|
QCOMPARE(i2.resultReference(0), 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
@ -64,7 +64,7 @@ private slots:
|
|||||||
void cancelWhenDestroyed();
|
void cancelWhenDestroyed();
|
||||||
#endif
|
#endif
|
||||||
void cancelWhenReassigned();
|
void cancelWhenReassigned();
|
||||||
void finishWhenMoved();
|
void finishWhenSwapped();
|
||||||
void cancelWhenMoved();
|
void cancelWhenMoved();
|
||||||
void waitUntilResumed();
|
void waitUntilResumed();
|
||||||
void waitUntilCanceled();
|
void waitUntilCanceled();
|
||||||
@ -470,7 +470,7 @@ void tst_QPromise::cancelWhenReassigned()
|
|||||||
QCOMPARE(f.isCanceled(), true);
|
QCOMPARE(f.isCanceled(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QPromise::finishWhenMoved()
|
void tst_QPromise::finishWhenSwapped()
|
||||||
{
|
{
|
||||||
QPromise<int> promise1;
|
QPromise<int> promise1;
|
||||||
auto f1 = promise1.future();
|
auto f1 = promise1.future();
|
||||||
@ -482,8 +482,11 @@ void tst_QPromise::finishWhenMoved()
|
|||||||
|
|
||||||
ThreadWrapper thr([&promise1, &promise2] () mutable {
|
ThreadWrapper thr([&promise1, &promise2] () mutable {
|
||||||
QThread::msleep(100);
|
QThread::msleep(100);
|
||||||
// There is swap semantics in move, so promise #1 and #2 just swap
|
promise1.addResult(0);
|
||||||
promise1 = std::move(promise2);
|
promise2.addResult(1);
|
||||||
|
swap(promise1, promise2); // ADL must resolve this
|
||||||
|
promise1.addResult(2);
|
||||||
|
promise2.addResult(3);
|
||||||
promise1.reportFinished(); // this finish is for future #2
|
promise1.reportFinished(); // this finish is for future #2
|
||||||
promise2.reportFinished(); // this finish is for future #1
|
promise2.reportFinished(); // this finish is for future #1
|
||||||
});
|
});
|
||||||
@ -497,6 +500,12 @@ void tst_QPromise::finishWhenMoved()
|
|||||||
|
|
||||||
QCOMPARE(f2.isFinished(), true);
|
QCOMPARE(f2.isFinished(), true);
|
||||||
QCOMPARE(f2.isCanceled(), false);
|
QCOMPARE(f2.isCanceled(), false);
|
||||||
|
|
||||||
|
QCOMPARE(f1.resultAt(0), 0);
|
||||||
|
QCOMPARE(f1.resultAt(1), 3);
|
||||||
|
|
||||||
|
QCOMPARE(f2.resultAt(0), 1);
|
||||||
|
QCOMPARE(f2.resultAt(1), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QPromise::cancelWhenMoved()
|
void tst_QPromise::cancelWhenMoved()
|
||||||
@ -512,7 +521,6 @@ void tst_QPromise::cancelWhenMoved()
|
|||||||
// Move promises to local scope to test cancellation behavior
|
// Move promises to local scope to test cancellation behavior
|
||||||
ThreadWrapper thr([p1 = std::move(promise1), p2 = std::move(promise2)] () mutable {
|
ThreadWrapper thr([p1 = std::move(promise1), p2 = std::move(promise2)] () mutable {
|
||||||
QThread::msleep(100);
|
QThread::msleep(100);
|
||||||
// There is swap semantics in move, so promise #1 and #2 just swap
|
|
||||||
p1 = std::move(p2);
|
p1 = std::move(p2);
|
||||||
p1.reportFinished(); // this finish is for future #2
|
p1.reportFinished(); // this finish is for future #2
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user