mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2024-11-22 02:34:30 +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>
|
||||
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);
|
||||
|
||||
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>
|
||||
inline QPromise<T> QPromise<T>::resolve(T&& value)
|
||||
{
|
||||
|
@ -20,6 +20,7 @@ class tst_benchmark : public QObject
|
||||
|
||||
private Q_SLOTS:
|
||||
void valueResolve();
|
||||
void valueResolveStatic();
|
||||
void valueReject();
|
||||
void valueThen();
|
||||
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()
|
||||
{
|
||||
{ // should not create any data if rejected
|
||||
|
@ -6,6 +6,7 @@ SUBDIRS += \
|
||||
fail \
|
||||
finally \
|
||||
operators \
|
||||
resolve \
|
||||
tap \
|
||||
then \
|
||||
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