Skip to content

Commit

Permalink
Enable QPromise<T>::resolve() by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrunel committed Feb 14, 2018
1 parent 18739bd commit 4af2740
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/qtpromise/qpromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class QPromise : public QPromiseBase<T>
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:
Expand Down
8 changes: 8 additions & 0 deletions src/qtpromise/qpromise.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
26 changes: 26 additions & 0 deletions tests/auto/qtpromise/benchmark/tst_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class tst_benchmark : public QObject

private Q_SLOTS:
void valueResolve();
void valueResolveStatic();
void valueReject();
void valueThen();
void valueFinally();
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/auto/qtpromise/qpromise/qpromise.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SUBDIRS += \
fail \
finally \
operators \
resolve \
tap \
then \
timeout
4 changes: 4 additions & 0 deletions tests/auto/qtpromise/qpromise/resolve/resolve.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TARGET = tst_qpromise_resolve
SOURCES += $$PWD/tst_resolve.cpp

include(../../qtpromise.pri)
41 changes: 41 additions & 0 deletions tests/auto/qtpromise/qpromise/resolve/tst_resolve.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 4af2740

Please # to comment.