mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2024-11-22 10:40:08 +08:00
Enable QPromise<T>::resolve() by reference
This commit is contained in:
parent
18739bd8e0
commit
4af2740d80
@ -88,6 +88,7 @@ public: // STATIC
|
|||||||
template <template <typename, typename...> class Sequence = QVector, typename ...Args>
|
template <template <typename, typename...> class Sequence = QVector, typename ...Args>
|
||||||
inline static QPromise<QVector<T> > all(const Sequence<QPromise<T>, Args...>& promises);
|
inline static QPromise<QVector<T> > all(const Sequence<QPromise<T>, Args...>& promises);
|
||||||
|
|
||||||
|
inline static QPromise<T> resolve(const T& value);
|
||||||
inline static QPromise<T> resolve(T&& value);
|
inline static QPromise<T> resolve(T&& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -244,6 +244,14 @@ inline QPromise<QVector<T> > QPromise<T>::all(const Sequence<QPromise<T>, Args..
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline QPromise<T> QPromise<T>::resolve(const T& value)
|
||||||
|
{
|
||||||
|
return QPromise<T>([&](const QPromiseResolve<T>& resolve) {
|
||||||
|
resolve(value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline QPromise<T> QPromise<T>::resolve(T&& value)
|
inline QPromise<T> QPromise<T>::resolve(T&& value)
|
||||||
{
|
{
|
||||||
|
@ -20,6 +20,7 @@ class tst_benchmark : public QObject
|
|||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void valueResolve();
|
void valueResolve();
|
||||||
|
void valueResolveStatic();
|
||||||
void valueReject();
|
void valueReject();
|
||||||
void valueThen();
|
void valueThen();
|
||||||
void valueFinally();
|
void valueFinally();
|
||||||
@ -126,6 +127,31 @@ void tst_benchmark::valueResolve()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_benchmark::valueResolveStatic()
|
||||||
|
{
|
||||||
|
{ // should move the value when resolved by rvalue
|
||||||
|
Data::logs().reset();
|
||||||
|
QPromise<Data>::resolve(Data(42)).wait();
|
||||||
|
|
||||||
|
QCOMPARE(Data::logs().ctor, 1);
|
||||||
|
QCOMPARE(Data::logs().copy, 0);
|
||||||
|
QCOMPARE(Data::logs().move, 1); // move value to the promise data
|
||||||
|
QCOMPARE(Data::logs().refs, 0);
|
||||||
|
}
|
||||||
|
{ // should create one copy of the value when resolved by lvalue
|
||||||
|
{
|
||||||
|
Data::logs().reset();
|
||||||
|
Data value(42);
|
||||||
|
QPromise<Data>::resolve(value).wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
QCOMPARE(Data::logs().ctor, 1);
|
||||||
|
QCOMPARE(Data::logs().copy, 1); // copy value to the promise data
|
||||||
|
QCOMPARE(Data::logs().move, 0);
|
||||||
|
QCOMPARE(Data::logs().refs, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void tst_benchmark::valueReject()
|
void tst_benchmark::valueReject()
|
||||||
{
|
{
|
||||||
{ // should not create any data if rejected
|
{ // should not create any data if rejected
|
||||||
|
@ -6,6 +6,7 @@ SUBDIRS += \
|
|||||||
fail \
|
fail \
|
||||||
finally \
|
finally \
|
||||||
operators \
|
operators \
|
||||||
|
resolve \
|
||||||
tap \
|
tap \
|
||||||
then \
|
then \
|
||||||
timeout
|
timeout
|
||||||
|
4
tests/auto/qtpromise/qpromise/resolve/resolve.pro
Normal file
4
tests/auto/qtpromise/qpromise/resolve/resolve.pro
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
TARGET = tst_qpromise_resolve
|
||||||
|
SOURCES += $$PWD/tst_resolve.cpp
|
||||||
|
|
||||||
|
include(../../qtpromise.pri)
|
41
tests/auto/qtpromise/qpromise/resolve/tst_resolve.cpp
Normal file
41
tests/auto/qtpromise/qpromise/resolve/tst_resolve.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// Tests
|
||||||
|
#include "../../shared/utils.h"
|
||||||
|
|
||||||
|
// QtPromise
|
||||||
|
#include <QtPromise>
|
||||||
|
|
||||||
|
// Qt
|
||||||
|
#include <QtTest>
|
||||||
|
|
||||||
|
using namespace QtPromise;
|
||||||
|
|
||||||
|
class tst_qpromise_resolve : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void value();
|
||||||
|
void empty_void();
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_MAIN(tst_qpromise_resolve)
|
||||||
|
#include "tst_resolve.moc"
|
||||||
|
|
||||||
|
void tst_qpromise_resolve::value()
|
||||||
|
{
|
||||||
|
const int value = 42;
|
||||||
|
auto p0 = QPromise<int>::resolve(value);
|
||||||
|
auto p1 = QPromise<int>::resolve(43);
|
||||||
|
|
||||||
|
QCOMPARE(p0.isFulfilled(), true);
|
||||||
|
QCOMPARE(p1.isFulfilled(), true);
|
||||||
|
QCOMPARE(waitForValue(p0, -1), 42);
|
||||||
|
QCOMPARE(waitForValue(p1, -1), 43);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_qpromise_resolve::empty_void()
|
||||||
|
{
|
||||||
|
auto p = QPromise<void>::resolve();
|
||||||
|
|
||||||
|
QCOMPARE(p.isFulfilled(), true);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user