diff --git a/include/QtPromise b/include/QtPromise new file mode 100644 index 0000000..65250eb --- /dev/null +++ b/include/QtPromise @@ -0,0 +1,8 @@ +#ifndef _QTPROMISE_MODULE_H +#define _QTPROMISE_MODULE_H + +#include "../src/qtpromise/qpromise.h" +#include "../src/qtpromise/qpromisefuture.h" +#include "../src/qtpromise/qpromisehelpers.h" + +#endif // ifndef _QTPROMISE_MODULE_H diff --git a/qtpromise.pri b/qtpromise.pri index 6247160..f630570 100644 --- a/qtpromise.pri +++ b/qtpromise.pri @@ -1,2 +1,2 @@ -INCLUDEPATH += $$PWD/src -DEPENDPATH += $$PWD/src +INCLUDEPATH += $$PWD/include $$PWD/src +DEPENDPATH += $$PWD/include $$PWD/src diff --git a/qtpromise.pro b/qtpromise.pro index f594992..2475104 100644 --- a/qtpromise.pro +++ b/qtpromise.pro @@ -1,10 +1,12 @@ TEMPLATE = subdirs SUBDIRS = \ - src \ tests -tests.depends = src +_qt_creator_ { + SUBDIRS += src +} OTHER_FILES = \ package/features/*.prf \ + include/* \ qtpromise.pri diff --git a/src/qtpromise/qpromise.h b/src/qtpromise/qpromise.h index ccdd055..80c65fa 100644 --- a/src/qtpromise/qpromise.h +++ b/src/qtpromise/qpromise.h @@ -104,6 +104,5 @@ private: } // namespace QtPromise #include "qpromise.inl" -#include "qpromise_qfuture.inl" #endif // ifndef _QTPROMISE_QPROMISE_H diff --git a/src/qtpromise/qpromise.inl b/src/qtpromise/qpromise.inl index 42b5264..c598c13 100644 --- a/src/qtpromise/qpromise.inl +++ b/src/qtpromise/qpromise.inl @@ -311,37 +311,4 @@ inline void QPromise::notify(const typename QtPromisePrivate::PromiseData< }); } -// Helpers - -template -typename QtPromisePrivate::PromiseDeduce::Type qPromise(const T& value) -{ - using namespace QtPromisePrivate; - using Promise = typename PromiseDeduce::Type; - return Promise([=]( - const QPromiseResolve& resolve, - const QPromiseReject& reject) { - PromiseFulfill::call(value, resolve, reject); - }); -} - -QPromise qPromise() -{ - return QPromise([]( - const QPromiseResolve& resolve) { - resolve(); - }); -} - -template -QPromise > qPromiseAll(const QVector >& promises) -{ - return QPromise::all(promises); -} - -QPromise qPromiseAll(const QVector >& promises) -{ - return QPromise::all(promises); -} - } // namespace QtPromise diff --git a/src/qtpromise/qpromise_p.h b/src/qtpromise/qpromise_p.h index eddc2aa..28c71f4 100644 --- a/src/qtpromise/qpromise_p.h +++ b/src/qtpromise/qpromise_p.h @@ -6,7 +6,6 @@ #include "qpromiseglobal.h" // Qt -#include #include #include #include diff --git a/src/qtpromise/qpromise_qfuture.inl b/src/qtpromise/qpromisefuture.h similarity index 93% rename from src/qtpromise/qpromise_qfuture.inl rename to src/qtpromise/qpromisefuture.h index eec8726..0ca4d94 100644 --- a/src/qtpromise/qpromise_qfuture.inl +++ b/src/qtpromise/qpromisefuture.h @@ -1,3 +1,6 @@ +#ifndef _QTPROMISE_QPROMISEFUTURE_P_H +#define _QTPROMISE_QPROMISEFUTURE_P_H + #include #include @@ -62,3 +65,5 @@ struct PromiseFulfill > }; } // namespace QtPromise + +#endif // _QTPROMISE_QPROMISEFUTURE_P_H diff --git a/src/qtpromise/qpromisehelpers.h b/src/qtpromise/qpromisehelpers.h new file mode 100644 index 0000000..f901733 --- /dev/null +++ b/src/qtpromise/qpromisehelpers.h @@ -0,0 +1,42 @@ +#ifndef _QTPROMISE_QPROMISEHELPERS_H +#define _QTPROMISE_QPROMISEHELPERS_H + +// QtPromise +#include "qpromise_p.h" + +namespace QtPromise { + +template +typename QtPromisePrivate::PromiseDeduce::Type qPromise(const T& value) +{ + using namespace QtPromisePrivate; + using Promise = typename PromiseDeduce::Type; + return Promise([=]( + const QPromiseResolve& resolve, + const QPromiseReject& reject) { + PromiseFulfill::call(value, resolve, reject); + }); +} + +QPromise qPromise() +{ + return QPromise([]( + const QPromiseResolve& resolve) { + resolve(); + }); +} + +template +QPromise > qPromiseAll(const QVector >& promises) +{ + return QPromise::all(promises); +} + +QPromise qPromiseAll(const QVector >& promises) +{ + return QPromise::all(promises); +} + +} // namespace QtPromise + +#endif // _QTPROMISE_QPROMISEHELPERS_H diff --git a/src/qtpromise/qtpromise.pri b/src/qtpromise/qtpromise.pri index a941102..8a554e5 100644 --- a/src/qtpromise/qtpromise.pri +++ b/src/qtpromise/qtpromise.pri @@ -2,6 +2,7 @@ HEADERS += \ $$PWD/qpromise.h \ $$PWD/qpromise.inl \ $$PWD/qpromise_p.h \ - $$PWD/qpromise_qfuture.inl \ $$PWD/qpromiseerror.h \ - $$PWD/qpromiseglobal.h + $$PWD/qpromisefuture.h \ + $$PWD/qpromiseglobal.h \ + $$PWD/qpromisehelpers.h diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index a95fbee..ac52c3a 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,4 +1,5 @@ TEMPLATE = subdirs SUBDIRS += \ + helpers \ qpromise \ requirements diff --git a/tests/auto/helpers/helpers.pro b/tests/auto/helpers/helpers.pro new file mode 100644 index 0000000..0fc1653 --- /dev/null +++ b/tests/auto/helpers/helpers.pro @@ -0,0 +1,4 @@ +TARGET = tst_helpers +SOURCES += $$PWD/tst_helpers.cpp + +include(../tests.pri) diff --git a/tests/auto/helpers/tst_helpers.cpp b/tests/auto/helpers/tst_helpers.cpp new file mode 100644 index 0000000..0d073a9 --- /dev/null +++ b/tests/auto/helpers/tst_helpers.cpp @@ -0,0 +1,244 @@ +// QtPromise +#include + +// Qt +#include + +using namespace QtPromise; + +class tst_helpers: public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void resolve(); + void resolve_void(); + void resolve_promise(); + void resolve_promise_void(); + + void allFulfilled(); + void allFulfilled_void(); + void allRejected(); + void allRejected_void(); + void allEmpty(); + void allEmpty_void(); + +}; // class tst_helpers + +QTEST_MAIN(tst_helpers) +#include "tst_helpers.moc" + +void tst_helpers::resolve() +{ + int value = -1; + auto p = QtPromise::qPromise(42); + + Q_STATIC_ASSERT((std::is_same >::value)); + + QCOMPARE(p.isFulfilled(), true); + + p.then([&](int res) { + value = res; + }).wait(); + + QCOMPARE(value, 42); +} + +void tst_helpers::resolve_void() +{ + int value = -1; + auto p = QtPromise::qPromise(); + + Q_STATIC_ASSERT((std::is_same >::value)); + + QCOMPARE(p.isFulfilled(), true); + + p.then([&]() { + value = 42; + }).wait(); + + QCOMPARE(value, 42); +} + +void tst_helpers::resolve_promise() +{ + QString value; + auto p = QtPromise::qPromise( + QPromise([](const QPromiseResolve& resolve) { + QtPromisePrivate::qtpromise_defer([=](){ + resolve("foo"); + }); + })); + + Q_STATIC_ASSERT((std::is_same >::value)); + + QCOMPARE(p.isPending(), true); + + p.then([&](const QString& res) { + value = res; + }).wait(); + + QCOMPARE(p.isFulfilled(), true); + QCOMPARE(value, QString("foo")); +} + +void tst_helpers::resolve_promise_void() +{ + QList values; + auto p = QtPromise::qPromise( + QPromise([&](const QPromiseResolve& resolve) { + QtPromisePrivate::qtpromise_defer([=, &values](){ + values << 42; + resolve(); + }); + })); + + Q_STATIC_ASSERT((std::is_same >::value)); + + QCOMPARE(p.isPending(), true); + + p.then([&]() { + values << 43; + }).wait(); + + QCOMPARE(p.isFulfilled(), true); + QCOMPARE(values, QList({42, 43})); +} + +void tst_helpers::allFulfilled() +{ + auto p0 = QtPromise::qPromise(42); + auto p1 = QtPromise::qPromise(44); + auto p2 = QPromise([](const QPromiseResolve& resolve) { + QtPromisePrivate::qtpromise_defer([=](){ + resolve(43); + }); + }); + + auto p = qPromiseAll(QVector >{p0, p2, p1}); + + Q_STATIC_ASSERT((std::is_same > >::value)); + + QCOMPARE(p.isPending(), true); + QCOMPARE(p0.isFulfilled(), true); + QCOMPARE(p1.isFulfilled(), true); + QCOMPARE(p2.isPending(), true); + + QVector values; + p.then([&](const QVector& res) { + values = res; + }).wait(); + + QCOMPARE(p.isFulfilled(), true); + QCOMPARE(p2.isFulfilled(), true); + QCOMPARE(values, QVector({42, 43, 44})); +} + +void tst_helpers::allFulfilled_void() +{ + auto p0 = QtPromise::qPromise(); + auto p1 = QtPromise::qPromise(); + auto p2 = QPromise([](const QPromiseResolve& resolve) { + QtPromisePrivate::qtpromise_defer([=](){ + resolve(); + }); + }); + + auto p = qPromiseAll(QVector >{p0, p2, p1}); + + Q_STATIC_ASSERT((std::is_same >::value)); + + QCOMPARE(p.isPending(), true); + QCOMPARE(p0.isFulfilled(), true); + QCOMPARE(p1.isFulfilled(), true); + QCOMPARE(p2.isPending(), true); + + p.wait(); + + QCOMPARE(p.isFulfilled(), true); + QCOMPARE(p2.isFulfilled(), true); +} + +void tst_helpers::allRejected() +{ + auto p0 = QtPromise::qPromise(42); + auto p1 = QtPromise::qPromise(44); + auto p2 = QPromise([](const QPromiseResolve&, const QPromiseReject& reject) { + QtPromisePrivate::qtpromise_defer([=](){ + reject(QString("foo")); + }); + }); + + auto p = qPromiseAll(QVector >{p0, p2, p1}); + + Q_STATIC_ASSERT((std::is_same > >::value)); + + QCOMPARE(p.isPending(), true); + QCOMPARE(p0.isFulfilled(), true); + QCOMPARE(p1.isFulfilled(), true); + QCOMPARE(p2.isPending(), true); + + QString error; + p.fail([&](const QString& err) { + error = err; + return QVector(); + }).wait(); + + QCOMPARE(p.isRejected(), true); + QCOMPARE(p2.isRejected(), true); + QCOMPARE(error, QString("foo")); +} + +void tst_helpers::allRejected_void() +{ + auto p0 = QtPromise::qPromise(); + auto p1 = QtPromise::qPromise(); + auto p2 = QPromise([](const QPromiseResolve&, const QPromiseReject& reject) { + QtPromisePrivate::qtpromise_defer([=](){ + reject(QString("foo")); + }); + }); + + auto p = qPromiseAll(QVector >{p0, p2, p1}); + + Q_STATIC_ASSERT((std::is_same >::value)); + + QCOMPARE(p.isPending(), true); + QCOMPARE(p0.isFulfilled(), true); + QCOMPARE(p1.isFulfilled(), true); + QCOMPARE(p2.isPending(), true); + + QString error; + p.fail([&](const QString& err) { + error = err; + }).wait(); + + QCOMPARE(p.isRejected(), true); + QCOMPARE(p2.isRejected(), true); + QCOMPARE(error, QString("foo")); +} + +void tst_helpers::allEmpty() +{ + auto p = qPromiseAll(QVector >()); + + Q_STATIC_ASSERT((std::is_same > >::value)); + + QCOMPARE(p.isFulfilled(), true); + + QVector values; + p.then([&](const QVector& res) { + values = res; + }).wait(); + + QCOMPARE(values, QVector()); +} + +void tst_helpers::allEmpty_void() +{ + auto p = qPromiseAll(QVector >()); + + Q_STATIC_ASSERT((std::is_same >::value)); + + QCOMPARE(p.isFulfilled(), true); +} diff --git a/tests/auto/qpromise/tst_qpromise.cpp b/tests/auto/qpromise/tst_qpromise.cpp index 231a986..a69629c 100644 --- a/tests/auto/qpromise/tst_qpromise.cpp +++ b/tests/auto/qpromise/tst_qpromise.cpp @@ -1,5 +1,5 @@ // QtPromise -#include +#include // Qt #include diff --git a/tests/auto/requirements/tst_requirements.cpp b/tests/auto/requirements/tst_requirements.cpp index f6d0110..fc329cf 100644 --- a/tests/auto/requirements/tst_requirements.cpp +++ b/tests/auto/requirements/tst_requirements.cpp @@ -1,5 +1,5 @@ // QtPromise -#include +#include // Qt #include