mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2024-11-21 18:24:29 +08:00
Use clang-format for code style consistency
Based on the WebKit preset and following 'most' of the Qt guidelines, except a few rules that work better for promise continuation lambdas. Requires clang-format 11.
This commit is contained in:
parent
b99e468c84
commit
d43657fbd5
66
.clang-format
Normal file
66
.clang-format
Normal file
@ -0,0 +1,66 @@
|
||||
---
|
||||
Language: Cpp
|
||||
Standard: Cpp11
|
||||
BasedOnStyle: WebKit
|
||||
AlignAfterOpenBracket: true
|
||||
AlignTrailingComments: true
|
||||
AllowAllArgumentsOnNextLine: false
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
AllowShortLambdasOnASingleLine: Empty
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: true
|
||||
AfterControlStatement: false
|
||||
AfterEnum: false
|
||||
AfterFunction: true
|
||||
AfterNamespace: false
|
||||
AfterStruct: true
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: false
|
||||
SplitEmptyRecord: false
|
||||
SplitEmptyNamespace: false
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
BreakBeforeBraces: Custom
|
||||
BreakInheritanceList: BeforeComma
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
ColumnLimit: 100
|
||||
CommentPragmas: "^!|^:"
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- BOOST_FOREACH
|
||||
- foreach
|
||||
- forever
|
||||
- Q_FOREACH
|
||||
- Q_FOREVER
|
||||
- QBENCHMARK
|
||||
- QBENCHMARK_ONCE
|
||||
IncludeBlocks: Regroup
|
||||
IncludeCategories:
|
||||
- Regex: '^<Q.*>'
|
||||
Priority: 2
|
||||
SortPriority: 0
|
||||
- Regex: '^<.*\.h>'
|
||||
Priority: 3
|
||||
SortPriority: 0
|
||||
- Regex: '^<.*>'
|
||||
Priority: 4
|
||||
SortPriority: 0
|
||||
- Regex: '.*'
|
||||
Priority: 1
|
||||
SortPriority: 0
|
||||
IndentPPDirectives: AfterHash
|
||||
NamespaceIndentation: None
|
||||
PenaltyReturnTypeOnItsOwnLine: 10
|
||||
SpaceAfterTemplateKeyword: false
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
...
|
@ -25,10 +25,12 @@ class QPromiseBase
|
||||
public:
|
||||
using Type = T;
|
||||
|
||||
template <typename F, typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count == 1, int>::type = 0>
|
||||
template<typename F,
|
||||
typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count == 1, int>::type = 0>
|
||||
inline QPromiseBase(F resolver);
|
||||
|
||||
template <typename F, typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count == 2, int>::type = 0>
|
||||
template<typename F,
|
||||
typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count == 2, int>::type = 0>
|
||||
inline QPromiseBase(F resolver);
|
||||
|
||||
QPromiseBase(const QPromiseBase<T>& other) : m_d{other.m_d} { }
|
||||
@ -37,9 +39,16 @@ public:
|
||||
|
||||
virtual ~QPromiseBase() { }
|
||||
|
||||
QPromiseBase<T>& operator=(const QPromiseBase<T>& other) { m_d = other.m_d; return *this;}
|
||||
QPromiseBase<T>& operator=(const QPromiseBase<T>& other)
|
||||
{
|
||||
m_d = other.m_d;
|
||||
return *this;
|
||||
}
|
||||
QPromiseBase<T>& operator=(QPromiseBase<T>&& other) Q_DECL_NOEXCEPT
|
||||
{ QPromiseBase<T>(std::move(other)).swap(*this); return *this; }
|
||||
{
|
||||
QPromiseBase<T>(std::move(other)).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const QPromiseBase<T>& other) const { return (m_d == other.m_d); }
|
||||
bool operator!=(const QPromiseBase<T>& other) const { return (m_d != other.m_d); }
|
||||
@ -99,34 +108,30 @@ class QPromise : public QPromiseBase<T>
|
||||
{
|
||||
public:
|
||||
template<typename F>
|
||||
QPromise(F&& resolver): QPromiseBase<T>(std::forward<F>(resolver)) { }
|
||||
QPromise(F&& resolver) : QPromiseBase<T>(std::forward<F>(resolver))
|
||||
{ }
|
||||
|
||||
template<typename Functor>
|
||||
inline QPromise<T>
|
||||
each(Functor fn);
|
||||
inline QPromise<T> each(Functor fn);
|
||||
|
||||
template<typename Functor>
|
||||
inline QPromise<T>
|
||||
filter(Functor fn);
|
||||
inline QPromise<T> filter(Functor fn);
|
||||
|
||||
template<typename Functor>
|
||||
inline typename QtPromisePrivate::PromiseMapper<T, Functor>::PromiseType
|
||||
map(Functor fn);
|
||||
inline typename QtPromisePrivate::PromiseMapper<T, Functor>::PromiseType map(Functor fn);
|
||||
|
||||
template<typename Functor, typename Input>
|
||||
inline typename QtPromisePrivate::PromiseDeduce<Input>::Type
|
||||
reduce(Functor fn, Input initial);
|
||||
inline typename QtPromisePrivate::PromiseDeduce<Input>::Type reduce(Functor fn, Input initial);
|
||||
|
||||
template<typename Functor, typename U = T>
|
||||
inline typename QtPromisePrivate::PromiseDeduce<typename U::value_type>::Type
|
||||
reduce(Functor fn);
|
||||
|
||||
public: // STATIC
|
||||
|
||||
// DEPRECATED (remove at version 1)
|
||||
template<template<typename, typename...> class Sequence = QVector, typename... Args>
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::all instead") static inline QPromise<QVector<T>>
|
||||
all(const Sequence<QPromise<T>, Args...>& promises);
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::all instead")
|
||||
static inline 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);
|
||||
@ -140,14 +145,14 @@ class QPromise<void> : public QPromiseBase<void>
|
||||
{
|
||||
public:
|
||||
template<typename F>
|
||||
QPromise(F&& resolver): QPromiseBase<void>(std::forward<F>(resolver)) { }
|
||||
QPromise(F&& resolver) : QPromiseBase<void>(std::forward<F>(resolver))
|
||||
{ }
|
||||
|
||||
public: // STATIC
|
||||
|
||||
// DEPRECATED (remove at version 1)
|
||||
template<template<typename, typename...> class Sequence = QVector, typename... Args>
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::all instead") static inline QPromise<void>
|
||||
all(const Sequence<QPromise<void>, Args...>& promises);
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::all instead")
|
||||
static inline QPromise<void> all(const Sequence<QPromise<void>, Args...>& promises);
|
||||
|
||||
inline static QPromise<void> resolve();
|
||||
|
||||
|
@ -16,8 +16,7 @@ namespace QtPromise {
|
||||
|
||||
template<typename T>
|
||||
template<typename F, typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count == 1, int>::type>
|
||||
inline QPromiseBase<T>::QPromiseBase(F callback)
|
||||
: m_d{new QtPromisePrivate::PromiseData<T>{}}
|
||||
inline QPromiseBase<T>::QPromiseBase(F callback) : m_d{new QtPromisePrivate::PromiseData<T>{}}
|
||||
{
|
||||
QtPromisePrivate::PromiseResolver<T> resolver{*this};
|
||||
|
||||
@ -30,8 +29,7 @@ inline QPromiseBase<T>::QPromiseBase(F callback)
|
||||
|
||||
template<typename T>
|
||||
template<typename F, typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count == 2, int>::type>
|
||||
inline QPromiseBase<T>::QPromiseBase(F callback)
|
||||
: m_d{new QtPromisePrivate::PromiseData<T>{}}
|
||||
inline QPromiseBase<T>::QPromiseBase(F callback) : m_d{new QtPromisePrivate::PromiseData<T>{}}
|
||||
{
|
||||
QtPromisePrivate::PromiseResolver<T> resolver{*this};
|
||||
|
||||
@ -50,8 +48,7 @@ QPromiseBase<T>::then(const TFulfilled& fulfilled, const TRejected& rejected) co
|
||||
using namespace QtPromisePrivate;
|
||||
using PromiseType = typename PromiseHandler<T, TFulfilled>::Promise;
|
||||
|
||||
PromiseType next([&](
|
||||
const QPromiseResolve<typename PromiseType::Type>& resolve,
|
||||
PromiseType next([&](const QPromiseResolve<typename PromiseType::Type>& resolve,
|
||||
const QPromiseReject<typename PromiseType::Type>& reject) {
|
||||
m_d->addHandler(PromiseHandler<T, TFulfilled>::create(fulfilled, resolve, reject));
|
||||
m_d->addCatcher(PromiseCatcher<T, TRejected>::create(rejected, resolve, reject));
|
||||
@ -115,10 +112,7 @@ template <typename E>
|
||||
inline QPromise<T> QPromiseBase<T>::timeout(int msec, E&& error) const
|
||||
{
|
||||
QPromise<T> p = *this;
|
||||
return QPromise<T>{[&](
|
||||
const QPromiseResolve<T>& resolve,
|
||||
const QPromiseReject<T>& reject) {
|
||||
|
||||
return QPromise<T>{[&](const QPromiseResolve<T>& resolve, const QPromiseReject<T>& reject) {
|
||||
QTimer::singleShot(msec, [=]() {
|
||||
// we don't need to verify the current promise state, reject()
|
||||
// takes care of checking if the promise is already resolved,
|
||||
@ -183,9 +177,7 @@ inline QPromise<T> QPromise<T>::each(Functor fn)
|
||||
|
||||
std::vector<QPromise<void>> promises;
|
||||
for (const auto& v : values) {
|
||||
promises.push_back(
|
||||
QtPromise::attempt(fn, v, i)
|
||||
.then([]() {
|
||||
promises.push_back(QtPromise::attempt(fn, v, i).then([]() {
|
||||
// Cast to void in case fn returns a non promise value.
|
||||
// TODO remove when implicit cast is implemented.
|
||||
}));
|
||||
|
@ -120,23 +120,19 @@ struct PromiseDeduce
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct PromiseDeduce<T&>
|
||||
: public PromiseDeduce<T>
|
||||
struct PromiseDeduce<T&> : public PromiseDeduce<T>
|
||||
{ };
|
||||
|
||||
template<typename T>
|
||||
struct PromiseDeduce<const T>
|
||||
: public PromiseDeduce<T>
|
||||
struct PromiseDeduce<const T> : public PromiseDeduce<T>
|
||||
{ };
|
||||
|
||||
template<typename T>
|
||||
struct PromiseDeduce<const volatile T>
|
||||
: public PromiseDeduce<T>
|
||||
struct PromiseDeduce<const volatile T> : public PromiseDeduce<T>
|
||||
{ };
|
||||
|
||||
template<typename T>
|
||||
struct PromiseDeduce<QtPromise::QPromise<T>>
|
||||
: public PromiseDeduce<T>
|
||||
struct PromiseDeduce<QtPromise::QPromise<T>> : public PromiseDeduce<T>
|
||||
{ };
|
||||
|
||||
template<typename Functor, typename... Args>
|
||||
@ -160,19 +156,19 @@ template <typename T>
|
||||
struct PromiseFulfill<QtPromise::QPromise<T>>
|
||||
{
|
||||
template<typename TResolve, typename TReject>
|
||||
static void call(
|
||||
const QtPromise::QPromise<T>& promise,
|
||||
const TResolve& resolve,
|
||||
const TReject& reject)
|
||||
static void
|
||||
call(const QtPromise::QPromise<T>& promise, const TResolve& resolve, const TReject& reject)
|
||||
{
|
||||
if (promise.isFulfilled()) {
|
||||
resolve(promise.m_d->value());
|
||||
} else if (promise.isRejected()) {
|
||||
reject(promise.m_d->error());
|
||||
} else {
|
||||
promise.then([=]() {
|
||||
promise.then(
|
||||
[=]() {
|
||||
resolve(promise.m_d->value());
|
||||
}, [=]() { // catch all
|
||||
},
|
||||
[=]() { // catch all
|
||||
reject(promise.m_d->error());
|
||||
});
|
||||
}
|
||||
@ -183,19 +179,18 @@ template <>
|
||||
struct PromiseFulfill<QtPromise::QPromise<void>>
|
||||
{
|
||||
template<typename TPromise, typename TResolve, typename TReject>
|
||||
static void call(
|
||||
const TPromise& promise,
|
||||
const TResolve& resolve,
|
||||
const TReject& reject)
|
||||
static void call(const TPromise& promise, const TResolve& resolve, const TReject& reject)
|
||||
{
|
||||
if (promise.isFulfilled()) {
|
||||
resolve();
|
||||
} else if (promise.isRejected()) {
|
||||
reject(promise.m_d->error());
|
||||
} else {
|
||||
promise.then([=]() {
|
||||
promise.then(
|
||||
[=]() {
|
||||
resolve();
|
||||
}, [=]() { // catch all
|
||||
},
|
||||
[=]() { // catch all
|
||||
reject(promise.m_d->error());
|
||||
});
|
||||
}
|
||||
@ -209,8 +204,7 @@ struct PromiseDispatch
|
||||
static void call(const Resolve& resolve, const Reject& reject, Functor fn, Args&&... args)
|
||||
{
|
||||
try {
|
||||
PromiseFulfill<Unqualified<Result>>::call(
|
||||
fn(std::forward<Args>(args)...),
|
||||
PromiseFulfill<Unqualified<Result>>::call(fn(std::forward<Args>(args)...),
|
||||
resolve,
|
||||
reject);
|
||||
} catch (...) {
|
||||
@ -241,10 +235,8 @@ struct PromiseHandler
|
||||
using Promise = typename PromiseDeduce<ResType>::Type;
|
||||
|
||||
template<typename TResolve, typename TReject>
|
||||
static std::function<void(const T&)> create(
|
||||
const THandler& handler,
|
||||
const TResolve& resolve,
|
||||
const TReject& reject)
|
||||
static std::function<void(const T&)>
|
||||
create(const THandler& handler, const TResolve& resolve, const TReject& reject)
|
||||
{
|
||||
return [=](const T& value) {
|
||||
PromiseDispatch<ResType>::call(resolve, reject, handler, value);
|
||||
@ -259,10 +251,8 @@ struct PromiseHandler<T, THandler, void>
|
||||
using Promise = typename PromiseDeduce<ResType>::Type;
|
||||
|
||||
template<typename TResolve, typename TReject>
|
||||
static std::function<void(const T&)> create(
|
||||
const THandler& handler,
|
||||
const TResolve& resolve,
|
||||
const TReject& reject)
|
||||
static std::function<void(const T&)>
|
||||
create(const THandler& handler, const TResolve& resolve, const TReject& reject)
|
||||
{
|
||||
return [=](const T&) {
|
||||
PromiseDispatch<ResType>::call(resolve, reject, handler);
|
||||
@ -277,10 +267,8 @@ struct PromiseHandler<void, THandler, void>
|
||||
using Promise = typename PromiseDeduce<ResType>::Type;
|
||||
|
||||
template<typename TResolve, typename TReject>
|
||||
static std::function<void()> create(
|
||||
const THandler& handler,
|
||||
const TResolve& resolve,
|
||||
const TReject& reject)
|
||||
static std::function<void()>
|
||||
create(const THandler& handler, const TResolve& resolve, const TReject& reject)
|
||||
{
|
||||
return [=]() {
|
||||
PromiseDispatch<ResType>::call(resolve, reject, handler);
|
||||
@ -294,10 +282,8 @@ struct PromiseHandler<T, std::nullptr_t, void>
|
||||
using Promise = QtPromise::QPromise<T>;
|
||||
|
||||
template<typename TResolve, typename TReject>
|
||||
static std::function<void(const T&)> create(
|
||||
std::nullptr_t,
|
||||
const TResolve& resolve,
|
||||
const TReject& reject)
|
||||
static std::function<void(const T&)>
|
||||
create(std::nullptr_t, const TResolve& resolve, const TReject& reject)
|
||||
{
|
||||
return [=](const T& value) {
|
||||
// 2.2.7.3. If onFulfilled is not a function and promise1 is fulfilled,
|
||||
@ -313,10 +299,7 @@ struct PromiseHandler<void, std::nullptr_t, void>
|
||||
using Promise = QtPromise::QPromise<void>;
|
||||
|
||||
template<typename TResolve, typename TReject>
|
||||
static std::function<void()> create(
|
||||
std::nullptr_t,
|
||||
const TResolve& resolve,
|
||||
const TReject&)
|
||||
static std::function<void()> create(std::nullptr_t, const TResolve& resolve, const TReject&)
|
||||
{
|
||||
return [=]() {
|
||||
// 2.2.7.3. If onFulfilled is not a function and promise1 is fulfilled,
|
||||
@ -332,10 +315,8 @@ struct PromiseCatcher
|
||||
using ResType = typename std::result_of<THandler(TArg)>::type;
|
||||
|
||||
template<typename TResolve, typename TReject>
|
||||
static std::function<void(const PromiseError&)> create(
|
||||
const THandler& handler,
|
||||
const TResolve& resolve,
|
||||
const TReject& reject)
|
||||
static std::function<void(const PromiseError&)>
|
||||
create(const THandler& handler, const TResolve& resolve, const TReject& reject)
|
||||
{
|
||||
return [=](const PromiseError& error) {
|
||||
try {
|
||||
@ -355,10 +336,8 @@ struct PromiseCatcher<T, THandler, void>
|
||||
using ResType = typename std::result_of<THandler()>::type;
|
||||
|
||||
template<typename TResolve, typename TReject>
|
||||
static std::function<void(const PromiseError&)> create(
|
||||
const THandler& handler,
|
||||
const TResolve& resolve,
|
||||
const TReject& reject)
|
||||
static std::function<void(const PromiseError&)>
|
||||
create(const THandler& handler, const TResolve& resolve, const TReject& reject)
|
||||
{
|
||||
return [=](const PromiseError& error) {
|
||||
try {
|
||||
@ -374,10 +353,8 @@ template <typename T>
|
||||
struct PromiseCatcher<T, std::nullptr_t, void>
|
||||
{
|
||||
template<typename TResolve, typename TReject>
|
||||
static std::function<void(const PromiseError&)> create(
|
||||
std::nullptr_t,
|
||||
const TResolve&,
|
||||
const TReject& reject)
|
||||
static std::function<void(const PromiseError&)>
|
||||
create(std::nullptr_t, const TResolve&, const TReject& reject)
|
||||
{
|
||||
return [=](const PromiseError& error) {
|
||||
// 2.2.7.4. If onRejected is not a function and promise1 is rejected,
|
||||
@ -399,7 +376,8 @@ struct PromiseMapper<Sequence<T, Args...>, F>
|
||||
using PromiseType = QtPromise::QPromise<ResultType>;
|
||||
};
|
||||
|
||||
template <typename T> class PromiseData;
|
||||
template<typename T>
|
||||
class PromiseData;
|
||||
|
||||
template<typename T, typename F>
|
||||
class PromiseDataBase : public QSharedData
|
||||
@ -410,15 +388,8 @@ public:
|
||||
|
||||
virtual ~PromiseDataBase() { }
|
||||
|
||||
bool isFulfilled() const
|
||||
{
|
||||
return !isPending() && m_error.isNull();
|
||||
}
|
||||
|
||||
bool isRejected() const
|
||||
{
|
||||
return !isPending() && !m_error.isNull();
|
||||
}
|
||||
bool isFulfilled() const { return !isPending() && m_error.isNull(); }
|
||||
bool isRejected() const { return !isPending() && !m_error.isNull(); }
|
||||
|
||||
bool isPending() const
|
||||
{
|
||||
@ -481,9 +452,11 @@ public:
|
||||
|
||||
for (const auto& catcher : catchers) {
|
||||
const auto& fn = catcher.second;
|
||||
qtpromise_defer([=]() {
|
||||
qtpromise_defer(
|
||||
[=]() {
|
||||
fn(error);
|
||||
}, catcher.first);
|
||||
},
|
||||
catcher.first);
|
||||
}
|
||||
}
|
||||
|
||||
@ -534,9 +507,11 @@ public:
|
||||
|
||||
for (const auto& handler : handlers) {
|
||||
const auto& fn = handler.second;
|
||||
qtpromise_defer([=]() {
|
||||
qtpromise_defer(
|
||||
[=]() {
|
||||
fn(value.data());
|
||||
}, handler.first);
|
||||
},
|
||||
handler.first);
|
||||
}
|
||||
}
|
||||
|
||||
@ -550,10 +525,7 @@ class PromiseData<void> : public PromiseDataBase<void, void()>
|
||||
using Handler = PromiseDataBase<void, void()>::Handler;
|
||||
|
||||
public:
|
||||
void resolve()
|
||||
{
|
||||
setSettled();
|
||||
}
|
||||
void resolve() { setSettled(); }
|
||||
|
||||
protected:
|
||||
void notify(const QVector<Handler>& handlers) Q_DECL_OVERRIDE
|
||||
|
@ -31,14 +31,16 @@ private:
|
||||
{
|
||||
QVector<QMetaObject::Connection> connections;
|
||||
|
||||
~Data() {
|
||||
~Data()
|
||||
{
|
||||
if (!connections.empty()) {
|
||||
qWarning("QPromiseConnections: destroyed with unhandled connections.");
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
void disconnect()
|
||||
{
|
||||
for (const auto& connection : connections) {
|
||||
QObject::disconnect(connection);
|
||||
}
|
||||
|
@ -16,15 +16,13 @@
|
||||
namespace QtPromisePrivate {
|
||||
|
||||
template<typename T>
|
||||
struct PromiseDeduce<QFuture<T>>
|
||||
: public PromiseDeduce<T>
|
||||
struct PromiseDeduce<QFuture<T>> : public PromiseDeduce<T>
|
||||
{ };
|
||||
|
||||
template<typename T>
|
||||
struct PromiseFulfill<QFuture<T>>
|
||||
{
|
||||
static void call(
|
||||
const QFuture<T>& future,
|
||||
static void call(const QFuture<T>& future,
|
||||
const QtPromise::QPromiseResolve<T>& resolve,
|
||||
const QtPromise::QPromiseReject<T>& reject)
|
||||
{
|
||||
@ -58,8 +56,7 @@ struct PromiseFulfill<QFuture<T>>
|
||||
template<>
|
||||
struct PromiseFulfill<QFuture<void>>
|
||||
{
|
||||
static void call(
|
||||
const QFuture<void>& future,
|
||||
static void call(const QFuture<void>& future,
|
||||
const QtPromise::QPromiseResolve<void>& resolve,
|
||||
const QtPromise::QPromiseReject<void>& reject)
|
||||
{
|
||||
|
@ -13,8 +13,8 @@
|
||||
#include <array>
|
||||
#include <functional>
|
||||
|
||||
namespace QtPromisePrivate
|
||||
{
|
||||
namespace QtPromisePrivate {
|
||||
|
||||
// https://rmf.io/cxx11/even-more-traits#unqualified_types
|
||||
template<typename T>
|
||||
using Unqualified = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
|
||||
@ -26,8 +26,12 @@ using Unqualified = typename std::remove_cv<typename std::remove_reference<T>::t
|
||||
template<typename T>
|
||||
struct HasCallOperator
|
||||
{
|
||||
template <typename U> static char check(decltype(&U::operator(), char(0)));
|
||||
template <typename U> static char (&check(...))[2];
|
||||
template<typename U>
|
||||
static char check(decltype(&U::operator(), char(0)));
|
||||
|
||||
template<typename U>
|
||||
static char (&check(...))[2];
|
||||
|
||||
static const bool value = (sizeof(check<T>(0)) == 1);
|
||||
};
|
||||
|
||||
|
@ -14,8 +14,7 @@
|
||||
namespace QtPromise {
|
||||
|
||||
template<typename T>
|
||||
static inline typename QtPromisePrivate::PromiseDeduce<T>::Type
|
||||
resolve(T&& value)
|
||||
static inline typename QtPromisePrivate::PromiseDeduce<T>::Type resolve(T&& value)
|
||||
{
|
||||
using namespace QtPromisePrivate;
|
||||
using PromiseType = typename PromiseDeduce<T>::Type;
|
||||
@ -24,22 +23,19 @@ resolve(T&& value)
|
||||
using RejectType = QPromiseReject<ValueType>;
|
||||
|
||||
return PromiseType{[&](ResolveType&& resolve, RejectType&& reject) {
|
||||
PromiseFulfill<Unqualified<T>>::call(
|
||||
std::forward<T>(value),
|
||||
PromiseFulfill<Unqualified<T>>::call(std::forward<T>(value),
|
||||
std::forward<ResolveType>(resolve),
|
||||
std::forward<RejectType>(reject));
|
||||
}};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static inline QPromise<T>
|
||||
resolve(QPromise<T> value)
|
||||
static inline QPromise<T> resolve(QPromise<T> value)
|
||||
{
|
||||
return std::move(value);
|
||||
}
|
||||
|
||||
static inline QPromise<void>
|
||||
resolve()
|
||||
static inline QPromise<void> resolve()
|
||||
{
|
||||
return QPromise<void>{[](const QPromiseResolve<void>& resolve) {
|
||||
resolve();
|
||||
@ -47,29 +43,28 @@ resolve()
|
||||
}
|
||||
|
||||
template<typename T, template<typename, typename...> class Sequence = QVector, typename... Args>
|
||||
static inline QPromise<QVector<T>>
|
||||
all(const Sequence<QPromise<T>, Args...>& promises)
|
||||
static inline QPromise<QVector<T>> all(const Sequence<QPromise<T>, Args...>& promises)
|
||||
{
|
||||
const int count = static_cast<int>(promises.size());
|
||||
if (count == 0) {
|
||||
return QtPromise::resolve(QVector<T>{});
|
||||
}
|
||||
|
||||
return QPromise<QVector<T>>{[=](
|
||||
const QPromiseResolve<QVector<T>>& resolve,
|
||||
const QPromiseReject<QVector<T>>& reject) {
|
||||
|
||||
return QPromise<QVector<T>>{
|
||||
[=](const QPromiseResolve<QVector<T>>& resolve, const QPromiseReject<QVector<T>>& reject) {
|
||||
auto remaining = QSharedPointer<int>::create(count);
|
||||
auto results = QSharedPointer<QVector<T>>::create(count);
|
||||
|
||||
int i = 0;
|
||||
for (const auto& promise : promises) {
|
||||
promise.then([=](const T& res) mutable {
|
||||
promise.then(
|
||||
[=](const T& res) mutable {
|
||||
(*results)[i] = res;
|
||||
if (--(*remaining) == 0) {
|
||||
resolve(*results);
|
||||
}
|
||||
}, [=]() mutable {
|
||||
},
|
||||
[=]() mutable {
|
||||
if (*remaining != -1) {
|
||||
*remaining = -1;
|
||||
reject(std::current_exception());
|
||||
@ -82,26 +77,25 @@ all(const Sequence<QPromise<T>, Args...>& promises)
|
||||
}
|
||||
|
||||
template<template<typename, typename...> class Sequence = QVector, typename... Args>
|
||||
static inline QPromise<void>
|
||||
all(const Sequence<QPromise<void>, Args...>& promises)
|
||||
static inline QPromise<void> all(const Sequence<QPromise<void>, Args...>& promises)
|
||||
{
|
||||
const int count = static_cast<int>(promises.size());
|
||||
if (count == 0) {
|
||||
return QtPromise::resolve();
|
||||
}
|
||||
|
||||
return QPromise<void>{[=](
|
||||
const QPromiseResolve<void>& resolve,
|
||||
const QPromiseReject<void>& reject) {
|
||||
|
||||
return QPromise<void>{
|
||||
[=](const QPromiseResolve<void>& resolve, const QPromiseReject<void>& reject) {
|
||||
auto remaining = QSharedPointer<int>::create(count);
|
||||
|
||||
for (const auto& promise : promises) {
|
||||
promise.then([=]() {
|
||||
promise.then(
|
||||
[=]() {
|
||||
if (--(*remaining) == 0) {
|
||||
resolve();
|
||||
}
|
||||
}, [=]() {
|
||||
},
|
||||
[=]() {
|
||||
if (*remaining != -1) {
|
||||
*remaining = -1;
|
||||
reject(std::current_exception());
|
||||
@ -127,10 +121,8 @@ attempt(Functor&& fn, Args&&... args)
|
||||
using ResolveType = QPromiseResolve<ValueType>;
|
||||
using RejectType = QPromiseReject<ValueType>;
|
||||
|
||||
return PromiseType{
|
||||
[&](ResolveType&& resolve, RejectType&& reject) {
|
||||
PromiseDispatch<typename FunctorType::ResultType>::call(
|
||||
std::forward<ResolveType>(resolve),
|
||||
return PromiseType{[&](ResolveType&& resolve, RejectType&& reject) {
|
||||
PromiseDispatch<typename FunctorType::ResultType>::call(std::forward<ResolveType>(resolve),
|
||||
std::forward<RejectType>(reject),
|
||||
std::forward<Functor>(fn),
|
||||
std::forward<Args>(args)...);
|
||||
@ -144,8 +136,7 @@ connect(const Sender* sender, Signal signal)
|
||||
using namespace QtPromisePrivate;
|
||||
using T = typename PromiseFromSignal<Signal>::Type;
|
||||
|
||||
return QPromise<T>{
|
||||
[&](const QPromiseResolve<T>& resolve, const QPromiseReject<T>& reject) {
|
||||
return QPromise<T>{[&](const QPromiseResolve<T>& resolve, const QPromiseReject<T>& reject) {
|
||||
QPromiseConnections connections;
|
||||
connectSignalToResolver(connections, resolve, sender, signal);
|
||||
connectDestroyedToReject(connections, reject, sender);
|
||||
@ -159,8 +150,7 @@ connect(const FSender* fsender, FSignal fsignal, const RSender* rsender, RSignal
|
||||
using namespace QtPromisePrivate;
|
||||
using T = typename PromiseFromSignal<FSignal>::Type;
|
||||
|
||||
return QPromise<T>{
|
||||
[&](const QPromiseResolve<T>& resolve, const QPromiseReject<T>& reject) {
|
||||
return QPromise<T>{[&](const QPromiseResolve<T>& resolve, const QPromiseReject<T>& reject) {
|
||||
QPromiseConnections connections;
|
||||
connectSignalToResolver(connections, resolve, fsender, fsignal);
|
||||
connectSignalToResolver(connections, reject, rsender, rsignal);
|
||||
@ -176,8 +166,7 @@ connect(const Sender* sender, FSignal fsignal, RSignal rsignal)
|
||||
}
|
||||
|
||||
template<typename Sequence, typename Functor>
|
||||
static inline QPromise<Sequence>
|
||||
each(const Sequence& values, Functor&& fn)
|
||||
static inline QPromise<Sequence> each(const Sequence& values, Functor&& fn)
|
||||
{
|
||||
return QPromise<Sequence>::resolve(values).each(std::forward<Functor>(fn));
|
||||
}
|
||||
@ -195,9 +184,8 @@ map(const Sequence& values, Functor fn)
|
||||
|
||||
std::vector<QPromise<ResType>> promises;
|
||||
for (const auto& v : values) {
|
||||
promises.push_back(QPromise<ResType>{[&](
|
||||
const QPromiseResolve<ResType>& resolve,
|
||||
const QPromiseReject<ResType>& reject) {
|
||||
promises.push_back(QPromise<ResType>{
|
||||
[&](const QPromiseResolve<ResType>& resolve, const QPromiseReject<ResType>& reject) {
|
||||
PromiseFulfill<RetType>::call(fn(v, i), resolve, reject);
|
||||
}});
|
||||
|
||||
@ -208,11 +196,9 @@ map(const Sequence& values, Functor fn)
|
||||
}
|
||||
|
||||
template<typename Sequence, typename Functor>
|
||||
static inline QPromise<Sequence>
|
||||
filter(const Sequence& values, Functor fn)
|
||||
static inline QPromise<Sequence> filter(const Sequence& values, Functor fn)
|
||||
{
|
||||
return QtPromise::map(values, fn)
|
||||
.then([=](const QVector<bool>& filters) {
|
||||
return QtPromise::map(values, fn).then([=](const QVector<bool>& filters) {
|
||||
Sequence filtered;
|
||||
|
||||
auto filter = filters.begin();
|
||||
@ -228,7 +214,11 @@ filter(const Sequence& values, Functor fn)
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T, template <typename...> class Sequence = QVector, typename Reducer, typename Input, typename ...Args>
|
||||
template<typename T,
|
||||
template<typename...> class Sequence = QVector,
|
||||
typename Reducer,
|
||||
typename Input,
|
||||
typename... Args>
|
||||
static inline typename QtPromisePrivate::PromiseDeduce<Input>::Type
|
||||
reduce(const Sequence<T, Args...>& values, Reducer fn, Input initial)
|
||||
{
|
||||
@ -253,7 +243,10 @@ reduce(const Sequence<T, Args...>& values, Reducer fn, Input initial)
|
||||
return promise;
|
||||
}
|
||||
|
||||
template <typename T, template <typename...> class Sequence = QVector, typename Reducer, typename ...Args>
|
||||
template<typename T,
|
||||
template<typename...> class Sequence = QVector,
|
||||
typename Reducer,
|
||||
typename... Args>
|
||||
static inline typename QtPromisePrivate::PromiseDeduce<T>::Type
|
||||
reduce(const Sequence<T, Args...>& values, Reducer fn)
|
||||
{
|
||||
@ -285,8 +278,7 @@ reduce(const Sequence<T, Args...>& values, Reducer fn)
|
||||
|
||||
template<typename... Args>
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::resolve instead")
|
||||
static inline auto
|
||||
qPromise(Args&&... args)
|
||||
static inline auto qPromise(Args&&... args)
|
||||
-> decltype(QtPromise::resolve(std::forward<Args>(args)...))
|
||||
{
|
||||
return QtPromise::resolve(std::forward<Args>(args)...);
|
||||
@ -294,8 +286,7 @@ qPromise(Args&&... args)
|
||||
|
||||
template<typename... Args>
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::all instead")
|
||||
static inline auto
|
||||
qPromiseAll(Args&&... args)
|
||||
static inline auto qPromiseAll(Args&&... args)
|
||||
-> decltype(QtPromise::all(std::forward<Args>(args)...))
|
||||
{
|
||||
return QtPromise::all(std::forward<Args>(args)...);
|
||||
|
@ -22,8 +22,7 @@ using PromiseFromSignal = typename QtPromise::QPromise<Unqualified<typename Args
|
||||
// Connect signal() to QPromiseResolve
|
||||
template<typename Sender, typename Signal>
|
||||
typename std::enable_if<(ArgsOf<Signal>::count == 0)>::type
|
||||
connectSignalToResolver(
|
||||
const QtPromise::QPromiseConnections& connections,
|
||||
connectSignalToResolver(const QtPromise::QPromiseConnections& connections,
|
||||
const QtPromise::QPromiseResolve<void>& resolve,
|
||||
const Sender* sender,
|
||||
Signal signal)
|
||||
@ -37,8 +36,7 @@ connectSignalToResolver(
|
||||
// Connect signal() to QPromiseReject
|
||||
template<typename T, typename Sender, typename Signal>
|
||||
typename std::enable_if<(ArgsOf<Signal>::count == 0)>::type
|
||||
connectSignalToResolver(
|
||||
const QtPromise::QPromiseConnections& connections,
|
||||
connectSignalToResolver(const QtPromise::QPromiseConnections& connections,
|
||||
const QtPromise::QPromiseReject<T>& reject,
|
||||
const Sender* sender,
|
||||
Signal signal)
|
||||
@ -52,8 +50,7 @@ connectSignalToResolver(
|
||||
// Connect signal(args...) to QPromiseResolve
|
||||
template<typename T, typename Sender, typename Signal>
|
||||
typename std::enable_if<(ArgsOf<Signal>::count >= 1)>::type
|
||||
connectSignalToResolver(
|
||||
const QtPromise::QPromiseConnections& connections,
|
||||
connectSignalToResolver(const QtPromise::QPromiseConnections& connections,
|
||||
const QtPromise::QPromiseResolve<T>& resolve,
|
||||
const Sender* sender,
|
||||
Signal signal)
|
||||
@ -67,8 +64,7 @@ connectSignalToResolver(
|
||||
// Connect signal(args...) to QPromiseReject
|
||||
template<typename T, typename Sender, typename Signal>
|
||||
typename std::enable_if<(ArgsOf<Signal>::count >= 1)>::type
|
||||
connectSignalToResolver(
|
||||
const QtPromise::QPromiseConnections& connections,
|
||||
connectSignalToResolver(const QtPromise::QPromiseConnections& connections,
|
||||
const QtPromise::QPromiseReject<T>& reject,
|
||||
const Sender* sender,
|
||||
Signal signal)
|
||||
@ -82,8 +78,7 @@ connectSignalToResolver(
|
||||
|
||||
// Connect QObject::destroyed signal to QPromiseReject
|
||||
template<typename T, typename Sender>
|
||||
void connectDestroyedToReject(
|
||||
const QtPromise::QPromiseConnections& connections,
|
||||
void connectDestroyedToReject(const QtPromise::QPromiseConnections& connections,
|
||||
const QtPromise::QPromiseReject<T>& reject,
|
||||
const Sender* sender)
|
||||
{
|
||||
|
@ -14,7 +14,8 @@
|
||||
|
||||
namespace QtPromise {
|
||||
|
||||
template <typename T> class QPromise;
|
||||
template<typename T>
|
||||
class QPromise;
|
||||
|
||||
} // namespace QtPromise
|
||||
|
||||
@ -24,8 +25,7 @@ template <typename T>
|
||||
class PromiseResolver
|
||||
{
|
||||
public:
|
||||
PromiseResolver(QtPromise::QPromise<T> promise)
|
||||
: m_d{new Data{}}
|
||||
PromiseResolver(QtPromise::QPromise<T> promise) : m_d{new Data{}}
|
||||
{
|
||||
m_d->promise = new QtPromise::QPromise<T>{std::move(promise)};
|
||||
}
|
||||
@ -93,7 +93,7 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
} // QtPromisePrivate
|
||||
} // namespace QtPromisePrivate
|
||||
|
||||
namespace QtPromise {
|
||||
|
||||
@ -101,8 +101,7 @@ template <class T>
|
||||
class QPromiseResolve
|
||||
{
|
||||
public:
|
||||
QPromiseResolve(QtPromisePrivate::PromiseResolver<T> resolver)
|
||||
: m_resolver{std::move(resolver)}
|
||||
QPromiseResolve(QtPromisePrivate::PromiseResolver<T> resolver) : m_resolver{std::move(resolver)}
|
||||
{ }
|
||||
|
||||
template<typename V>
|
||||
@ -111,10 +110,7 @@ public:
|
||||
m_resolver.resolve(std::forward<V>(value));
|
||||
}
|
||||
|
||||
void operator()() const
|
||||
{
|
||||
m_resolver.resolve();
|
||||
}
|
||||
void operator()() const { m_resolver.resolve(); }
|
||||
|
||||
private:
|
||||
mutable QtPromisePrivate::PromiseResolver<T> m_resolver;
|
||||
@ -124,8 +120,7 @@ template <class T>
|
||||
class QPromiseReject
|
||||
{
|
||||
public:
|
||||
QPromiseReject(QtPromisePrivate::PromiseResolver<T> resolver)
|
||||
: m_resolver{std::move(resolver)}
|
||||
QPromiseReject(QtPromisePrivate::PromiseResolver<T> resolver) : m_resolver{std::move(resolver)}
|
||||
{ }
|
||||
|
||||
template<typename E>
|
||||
@ -134,10 +129,7 @@ public:
|
||||
m_resolver.reject(std::forward<E>(error));
|
||||
}
|
||||
|
||||
void operator()() const
|
||||
{
|
||||
m_resolver.reject();
|
||||
}
|
||||
void operator()() const { m_resolver.reject(); }
|
||||
|
||||
private:
|
||||
mutable QtPromisePrivate::PromiseResolver<T> m_resolver;
|
||||
|
@ -86,9 +86,11 @@ void tst_benchmark::valueThen()
|
||||
{ // should not copy value on continutation if fulfilled
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<Data>::resolve(Data{42}).then([&](const Data& res) {
|
||||
QPromise<Data>::resolve(Data{42})
|
||||
.then([&](const Data& res) {
|
||||
value = res.value();
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 1);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -100,11 +102,15 @@ void tst_benchmark::valueThen()
|
||||
int value = -1;
|
||||
QString error;
|
||||
Data::logs().reset();
|
||||
QPromise<Data>::reject(QString{"foo"}).then([&](const Data& res) {
|
||||
QPromise<Data>::reject(QString{"foo"})
|
||||
.then(
|
||||
[&](const Data& res) {
|
||||
value = res.value();
|
||||
}, [&](const QString& err) {
|
||||
},
|
||||
[&](const QString& err) {
|
||||
error = err;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 0);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -116,11 +122,14 @@ void tst_benchmark::valueThen()
|
||||
{ // should move the returned value when fulfilled
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<int>::resolve(42).then([&](int res) {
|
||||
QPromise<int>::resolve(42)
|
||||
.then([&](int res) {
|
||||
return Data{res + 2};
|
||||
}).then([&](const Data& res) {
|
||||
})
|
||||
.then([&](const Data& res) {
|
||||
value = res.value();
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 1);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -130,10 +139,12 @@ void tst_benchmark::valueThen()
|
||||
}
|
||||
{ // should not create any data if handler throws
|
||||
Data::logs().reset();
|
||||
QPromise<int>::resolve(42).then([&](int res) {
|
||||
QPromise<int>::resolve(42)
|
||||
.then([&](int res) {
|
||||
throw QString{"foo"};
|
||||
return Data{res + 2};
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 0);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -147,11 +158,14 @@ void tst_benchmark::valueDelayed()
|
||||
{ // should not copy the value on continutation if fulfilled
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<int>::resolve(42).then([&](int res) {
|
||||
QPromise<int>::resolve(42)
|
||||
.then([&](int res) {
|
||||
return QPromise<Data>::resolve(Data{res + 1});
|
||||
}).then([&](const Data& res) {
|
||||
})
|
||||
.then([&](const Data& res) {
|
||||
value = res.value();
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 1);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -161,9 +175,11 @@ void tst_benchmark::valueDelayed()
|
||||
}
|
||||
{ // should not create value on continutation if rejected
|
||||
Data::logs().reset();
|
||||
QPromise<int>::resolve(42).then([&]() {
|
||||
QPromise<int>::resolve(42)
|
||||
.then([&]() {
|
||||
return QPromise<Data>::reject(QString{"foo"});
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 0);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -177,9 +193,11 @@ void tst_benchmark::valueFinally()
|
||||
{ // should not copy the value on continutation if fulfilled
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<Data>::resolve(Data{42}).finally([&]() {
|
||||
QPromise<Data>::resolve(Data{42})
|
||||
.finally([&]() {
|
||||
value = 42;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 1);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -190,9 +208,11 @@ void tst_benchmark::valueFinally()
|
||||
{ // should not create value on continutation if rejected
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<Data>::reject(QString{"foo"}).finally([&]() {
|
||||
QPromise<Data>::reject(QString{"foo"})
|
||||
.finally([&]() {
|
||||
value = 42;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 0);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -207,9 +227,11 @@ void tst_benchmark::valueTap()
|
||||
{ // should not copy the value on continutation if fulfilled
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<Data>::resolve(Data{42}).tap([&](const Data& res) {
|
||||
QPromise<Data>::resolve(Data{42})
|
||||
.tap([&](const Data& res) {
|
||||
value = res.value();
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 1);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -220,9 +242,11 @@ void tst_benchmark::valueTap()
|
||||
{ // should not create value on continutation if rejected
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<Data>::reject(QString{"foo"}).tap([&](const Data& res) {
|
||||
QPromise<Data>::reject(QString{"foo"})
|
||||
.tap([&](const Data& res) {
|
||||
value = res.value();
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 0);
|
||||
QCOMPARE(Data::logs().copy, 0);
|
||||
@ -264,12 +288,15 @@ void tst_benchmark::errorThen()
|
||||
{ // should not copy error on continutation if rejected
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<void>::reject(Data{42}).fail([&](const Data& res) {
|
||||
QPromise<void>::reject(Data{42})
|
||||
.fail([&](const Data& res) {
|
||||
value = res.value();
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 1);
|
||||
QCOMPARE(Data::logs().copy, 1 + 2 * EXCEPT_CALL_COPY_CTOR); // (initial) copy value in std::exception_ptr
|
||||
QCOMPARE(Data::logs().copy,
|
||||
1 + 2 * EXCEPT_CALL_COPY_CTOR); // (initial) copy value in std::exception_ptr
|
||||
QCOMPARE(Data::logs().move, 0);
|
||||
QCOMPARE(Data::logs().refs, 0);
|
||||
QCOMPARE(value, 42);
|
||||
@ -277,14 +304,18 @@ void tst_benchmark::errorThen()
|
||||
{ // should not copy error on continutation if rethrown
|
||||
int value = -1;
|
||||
Data::logs().reset();
|
||||
QPromise<void>::reject(Data{42}).fail([](const Data&) {
|
||||
QPromise<void>::reject(Data{42})
|
||||
.fail([](const Data&) {
|
||||
throw;
|
||||
}).fail([&](const Data& res) {
|
||||
})
|
||||
.fail([&](const Data& res) {
|
||||
value = res.value();
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(Data::logs().ctor, 1);
|
||||
QCOMPARE(Data::logs().copy, 1 + 4 * EXCEPT_CALL_COPY_CTOR); // (initial) copy value in std::exception_ptr
|
||||
QCOMPARE(Data::logs().copy,
|
||||
1 + 4 * EXCEPT_CALL_COPY_CTOR); // (initial) copy value in std::exception_ptr
|
||||
QCOMPARE(Data::logs().move, 0);
|
||||
QCOMPARE(Data::logs().refs, 0);
|
||||
QCOMPARE(value, 42);
|
||||
|
@ -228,7 +228,9 @@ void tst_deprecations_helpers_qpromise::voidPromise()
|
||||
|
||||
void tst_deprecations_helpers_qpromise::typedFuture()
|
||||
{
|
||||
auto fn = [](){ return 42; };
|
||||
auto fn = []() {
|
||||
return 42;
|
||||
};
|
||||
QFuture<int> v0 = QtConcurrent::run(fn);
|
||||
const QFuture<int> v1 = v0;
|
||||
|
||||
|
@ -40,11 +40,7 @@ struct SequenceTester
|
||||
|
||||
static void exec()
|
||||
{
|
||||
Sequence promises{
|
||||
QtPromise::resolve(42),
|
||||
QtPromise::resolve(43),
|
||||
QtPromise::resolve(44)
|
||||
};
|
||||
Sequence promises{QtPromise::resolve(42), QtPromise::resolve(43), QtPromise::resolve(44)};
|
||||
|
||||
promises.push_back(QtPromise::resolve(45));
|
||||
promises.insert(++promises.begin(), QtPromise::resolve(46));
|
||||
@ -63,11 +59,9 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
Sequence<QPromise<void>, Args...> promises{
|
||||
Sequence<QPromise<void>, Args...> promises{QtPromise::resolve(),
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve()
|
||||
};
|
||||
QtPromise::resolve()};
|
||||
|
||||
promises.push_back(QtPromise::resolve());
|
||||
promises.insert(++promises.begin(), QtPromise::resolve());
|
||||
|
@ -40,11 +40,7 @@ struct SequenceTester
|
||||
|
||||
static void exec()
|
||||
{
|
||||
Sequence promises{
|
||||
QtPromise::resolve(42),
|
||||
QtPromise::resolve(43),
|
||||
QtPromise::resolve(44)
|
||||
};
|
||||
Sequence promises{QtPromise::resolve(42), QtPromise::resolve(43), QtPromise::resolve(44)};
|
||||
|
||||
promises.push_back(QtPromise::resolve(45));
|
||||
promises.insert(++promises.begin(), QtPromise::resolve(46));
|
||||
@ -64,11 +60,9 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
Sequence<QPromise<void>, Args...> promises{
|
||||
Sequence<QPromise<void>, Args...> promises{QtPromise::resolve(),
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve()
|
||||
};
|
||||
QtPromise::resolve()};
|
||||
|
||||
promises.push_back(QtPromise::resolve());
|
||||
promises.insert(++promises.begin(), QtPromise::resolve());
|
||||
|
@ -33,7 +33,9 @@ namespace {
|
||||
template<class E>
|
||||
void verify()
|
||||
{
|
||||
auto p = QtPromise::resolve(QtConcurrent::run([]() { throw E(); }));
|
||||
auto p = QtPromise::resolve(QtConcurrent::run([]() {
|
||||
throw E();
|
||||
}));
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForRejected<E>(p), true);
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
|
@ -37,9 +37,7 @@ private Q_SLOTS:
|
||||
class MyException : public QException
|
||||
{
|
||||
public:
|
||||
MyException(const QString& error)
|
||||
: m_error{error}
|
||||
{ }
|
||||
MyException(const QString& error) : m_error{error} { }
|
||||
|
||||
const QString& error() const { return m_error; }
|
||||
|
||||
@ -141,10 +139,12 @@ void tst_future::unhandled()
|
||||
p.fail([&](const QString& err) {
|
||||
error += err;
|
||||
return -1;
|
||||
}).fail([&](const QUnhandledException&) {
|
||||
})
|
||||
.fail([&](const QUnhandledException&) {
|
||||
error += "bar";
|
||||
return -1;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QCOMPARE(error, QString{"bar"});
|
||||
@ -162,9 +162,11 @@ void tst_future::unhandled_void()
|
||||
|
||||
p.fail([&](const QString& err) {
|
||||
error += err;
|
||||
}).fail([&](const QUnhandledException&) {
|
||||
})
|
||||
.fail([&](const QUnhandledException&) {
|
||||
error += "bar";
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QCOMPARE(error, QString{"bar"});
|
||||
@ -231,9 +233,11 @@ void tst_future::then()
|
||||
QCOMPARE(input.isFulfilled(), true);
|
||||
QCOMPARE(output.isPending(), true);
|
||||
|
||||
output.then([&](const QString& res) {
|
||||
output
|
||||
.then([&](const QString& res) {
|
||||
result = res;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(output.isFulfilled(), true);
|
||||
QCOMPARE(result, QString{"foo42"});
|
||||
@ -252,9 +256,11 @@ void tst_future::then_void()
|
||||
QCOMPARE(input.isFulfilled(), true);
|
||||
QCOMPARE(output.isPending(), true);
|
||||
|
||||
output.then([&]() {
|
||||
output
|
||||
.then([&]() {
|
||||
result += "bar";
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(input.isFulfilled(), true);
|
||||
QCOMPARE(result, QString{"foobar"});
|
||||
@ -265,17 +271,21 @@ void tst_future::fail()
|
||||
QString result;
|
||||
auto input = QPromise<QString>::reject(MyException{"bar"});
|
||||
auto output = input.fail([](const MyException& e) {
|
||||
return QtConcurrent::run([](const QString& error) {
|
||||
return QtConcurrent::run(
|
||||
[](const QString& error) {
|
||||
return QString{"foo%1"}.arg(error);
|
||||
}, e.error());
|
||||
},
|
||||
e.error());
|
||||
});
|
||||
|
||||
QCOMPARE(input.isRejected(), true);
|
||||
QCOMPARE(output.isPending(), true);
|
||||
|
||||
output.then([&](const QString& res) {
|
||||
output
|
||||
.then([&](const QString& res) {
|
||||
result = res;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(output.isFulfilled(), true);
|
||||
QCOMPARE(result, QString{"foobar"});
|
||||
@ -286,17 +296,21 @@ void tst_future::fail_void()
|
||||
QString result;
|
||||
auto input = QPromise<void>::reject(MyException{"bar"});
|
||||
auto output = input.fail([&](const MyException& e) {
|
||||
return QtConcurrent::run([&](const QString& error) {
|
||||
return QtConcurrent::run(
|
||||
[&](const QString& error) {
|
||||
result = error;
|
||||
}, e.error());
|
||||
},
|
||||
e.error());
|
||||
});
|
||||
|
||||
QCOMPARE(input.isRejected(), true);
|
||||
QCOMPARE(output.isPending(), true);
|
||||
|
||||
output.then([&]() {
|
||||
output
|
||||
.then([&]() {
|
||||
result = result.prepend("foo");
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(output.isFulfilled(), true);
|
||||
QCOMPARE(result, QString{"foobar"});
|
||||
@ -317,9 +331,11 @@ void tst_future::finally()
|
||||
QCOMPARE(output.isPending(), true);
|
||||
|
||||
int value = -1;
|
||||
output.then([&](int res) {
|
||||
output
|
||||
.then([&](int res) {
|
||||
value = res;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(output.isFulfilled(), true);
|
||||
QCOMPARE(value, 42);
|
||||
@ -340,10 +356,12 @@ void tst_future::finallyRejected()
|
||||
QCOMPARE(output.isPending(), true);
|
||||
|
||||
QString error;
|
||||
output.fail([&](const MyException& e) {
|
||||
output
|
||||
.fail([&](const MyException& e) {
|
||||
error = e.error();
|
||||
return -1;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(output.isRejected(), true);
|
||||
QCOMPARE(error, QString{"foo"});
|
||||
|
@ -40,11 +40,7 @@ struct SequenceTester
|
||||
|
||||
static void exec()
|
||||
{
|
||||
Sequence promises{
|
||||
QtPromise::resolve(42),
|
||||
QtPromise::resolve(43),
|
||||
QtPromise::resolve(44)
|
||||
};
|
||||
Sequence promises{QtPromise::resolve(42), QtPromise::resolve(43), QtPromise::resolve(44)};
|
||||
|
||||
promises.push_back(QtPromise::resolve(45));
|
||||
promises.insert(++promises.begin(), QtPromise::resolve(46));
|
||||
@ -63,11 +59,9 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
Sequence<QPromise<void>, Args...> promises{
|
||||
Sequence<QPromise<void>, Args...> promises{QtPromise::resolve(),
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve()
|
||||
};
|
||||
QtPromise::resolve()};
|
||||
|
||||
promises.push_back(QtPromise::resolve());
|
||||
promises.insert(++promises.begin(), QtPromise::resolve());
|
||||
|
@ -91,9 +91,12 @@ void tst_helpers_attempt::functorThrows()
|
||||
|
||||
void tst_helpers_attempt::callWithParams()
|
||||
{
|
||||
auto p = QtPromise::attempt([&](int i, const QString& s) {
|
||||
auto p = QtPromise::attempt(
|
||||
[&](int i, const QString& s) {
|
||||
return QString{"%1:%2"}.arg(i).arg(s);
|
||||
}, 42, "foo");
|
||||
},
|
||||
42,
|
||||
"foo");
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value));
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
|
@ -108,9 +108,8 @@ void tst_helpers_each::delayedFulfilled()
|
||||
void tst_helpers_each::delayedRejected()
|
||||
{
|
||||
auto p = QtPromise::each(QVector<int>{42, 43, 44}, [](int v, ...) {
|
||||
return QPromise<int>{[&](
|
||||
const QPromiseResolve<int>& resolve,
|
||||
const QPromiseReject<int>& reject) {
|
||||
return QPromise<int>{
|
||||
[&](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
if (v == 43) {
|
||||
reject(QString{"foo"});
|
||||
|
@ -87,9 +87,8 @@ void tst_helpers_filter::delayedFulfilled()
|
||||
void tst_helpers_filter::delayedRejected()
|
||||
{
|
||||
auto p = QtPromise::filter(QVector<int>{42, 43, 44}, [](int v, ...) {
|
||||
return QPromise<bool>{[&](
|
||||
const QPromiseResolve<bool>& resolve,
|
||||
const QPromiseReject<bool>& reject) {
|
||||
return QPromise<bool>{
|
||||
[&](const QPromiseResolve<bool>& resolve, const QPromiseReject<bool>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
if (v == 44) {
|
||||
reject(QString{"foo"});
|
||||
|
@ -96,9 +96,8 @@ void tst_helpers_map::delayedFulfilled()
|
||||
void tst_helpers_map::delayedRejected()
|
||||
{
|
||||
auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) {
|
||||
return QPromise<int>{[&](
|
||||
const QPromiseResolve<int>& resolve,
|
||||
const QPromiseReject<int>& reject) {
|
||||
return QPromise<int>{
|
||||
[&](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
if (v == 43) {
|
||||
reject(QString{"foo"});
|
||||
|
@ -39,11 +39,9 @@ struct SequenceTester
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
Sequence inputs{
|
||||
QtPromise::resolve(4).delay(400),
|
||||
Sequence inputs{QtPromise::resolve(4).delay(400),
|
||||
QtPromise::resolve(6).delay(300),
|
||||
QtPromise::resolve(8).delay(200)
|
||||
};
|
||||
QtPromise::resolve(8).delay(200)};
|
||||
QVector<int> v0;
|
||||
QVector<int> v1;
|
||||
|
||||
@ -51,10 +49,13 @@ struct SequenceTester
|
||||
v0 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
});
|
||||
auto p1 = QtPromise::reduce(inputs, [&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::reduce(
|
||||
inputs,
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
}, QtPromise::resolve(2).delay(100));
|
||||
},
|
||||
QtPromise::resolve(2).delay(100));
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -74,10 +75,13 @@ void tst_helpers_reduce::emptySequence()
|
||||
{
|
||||
bool called = false;
|
||||
|
||||
auto p = QtPromise::reduce(QVector<int>{}, [&](...) {
|
||||
auto p = QtPromise::reduce(
|
||||
QVector<int>{},
|
||||
[&](...) {
|
||||
called = true;
|
||||
return 43;
|
||||
}, 42);
|
||||
},
|
||||
42);
|
||||
|
||||
// NOTE(SB): reduce() on an empty sequence without an initial value is an error!
|
||||
|
||||
@ -97,10 +101,13 @@ void tst_helpers_reduce::regularValues()
|
||||
v0 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
});
|
||||
auto p1 = QtPromise::reduce(inputs, [&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::reduce(
|
||||
inputs,
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -115,11 +122,9 @@ void tst_helpers_reduce::regularValues()
|
||||
|
||||
void tst_helpers_reduce::promiseValues()
|
||||
{
|
||||
QVector<QPromise<int>> inputs{
|
||||
QtPromise::resolve(4).delay(400),
|
||||
QVector<QPromise<int>> inputs{QtPromise::resolve(4).delay(400),
|
||||
QtPromise::resolve(6).delay(300),
|
||||
QtPromise::resolve(8).delay(200)
|
||||
};
|
||||
QtPromise::resolve(8).delay(200)};
|
||||
QVector<int> v0;
|
||||
QVector<int> v1;
|
||||
|
||||
@ -127,10 +132,13 @@ void tst_helpers_reduce::promiseValues()
|
||||
v0 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
});
|
||||
auto p1 = QtPromise::reduce(inputs, [&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::reduce(
|
||||
inputs,
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -147,9 +155,12 @@ void tst_helpers_reduce::convertResultType()
|
||||
{
|
||||
QVector<int> inputs{4, 6, 8};
|
||||
|
||||
auto p = QtPromise::reduce(inputs, [&](const QString& acc, int cur, int idx) {
|
||||
auto p = QtPromise::reduce(
|
||||
inputs,
|
||||
[&](const QString& acc, int cur, int idx) {
|
||||
return QString{"%1:%2:%3"}.arg(acc).arg(cur).arg(idx);
|
||||
}, QString{"foo"});
|
||||
},
|
||||
QString{"foo"});
|
||||
|
||||
// NOTE(SB): when no initial value is given, the result type is the sequence type.
|
||||
|
||||
@ -163,10 +174,13 @@ void tst_helpers_reduce::delayedInitialValue()
|
||||
{
|
||||
QVector<int> values;
|
||||
|
||||
auto p = QtPromise::reduce(QVector<int>{4, 6, 8}, [&](int acc, int cur, int idx) {
|
||||
auto p = QtPromise::reduce(
|
||||
QVector<int>{4, 6, 8},
|
||||
[&](int acc, int cur, int idx) {
|
||||
values << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
}, QtPromise::resolve(2).delay(100));
|
||||
},
|
||||
QtPromise::resolve(2).delay(100));
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value));
|
||||
|
||||
@ -185,10 +199,13 @@ void tst_helpers_reduce::delayedFulfilled()
|
||||
v0 << acc << cur << idx;
|
||||
return QtPromise::resolve(acc + cur + idx).delay(100);
|
||||
});
|
||||
auto p1 = QtPromise::reduce(inputs, [&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::reduce(
|
||||
inputs,
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
return QtPromise::resolve(acc + cur + idx).delay(100);
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -214,13 +231,16 @@ void tst_helpers_reduce::delayedRejected()
|
||||
}
|
||||
return QtPromise::resolve(acc + cur + idx);
|
||||
});
|
||||
auto p1 = QtPromise::reduce(inputs, [&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::reduce(
|
||||
inputs,
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
if (cur == 6) {
|
||||
return QPromise<int>::reject(QString{"bar"});
|
||||
}
|
||||
return QtPromise::resolve(acc + cur + idx);
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -246,13 +266,16 @@ void tst_helpers_reduce::functorThrows()
|
||||
}
|
||||
return acc + cur + idx;
|
||||
});
|
||||
auto p1 = QtPromise::reduce(inputs, [&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::reduce(
|
||||
inputs,
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
if (cur == 6) {
|
||||
throw QString{"bar"};
|
||||
}
|
||||
return acc + cur + idx;
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
|
@ -8,8 +8,8 @@
|
||||
#include "../shared/data.h"
|
||||
#include "../shared/utils.h"
|
||||
|
||||
#include <QtPromise>
|
||||
#include <QtConcurrent>
|
||||
#include <QtPromise>
|
||||
#include <QtTest>
|
||||
|
||||
#include <memory>
|
||||
@ -228,7 +228,9 @@ void tst_helpers_resolve::voidPromise()
|
||||
|
||||
void tst_helpers_resolve::typedFuture()
|
||||
{
|
||||
auto fn = [](){ return 42; };
|
||||
auto fn = []() {
|
||||
return 42;
|
||||
};
|
||||
QFuture<int> v0 = QtConcurrent::run(fn);
|
||||
const QFuture<int> v1 = v0;
|
||||
|
||||
|
@ -45,25 +45,70 @@ namespace {
|
||||
|
||||
const float kRes = 0.42f;
|
||||
|
||||
float fnNoArg() { return kRes; }
|
||||
float fnOneArg(float v) { return v; }
|
||||
float fnManyArgs(const float& v0, int, char*) { return v0; }
|
||||
float fnNoArg()
|
||||
{
|
||||
return kRes;
|
||||
}
|
||||
float fnOneArg(float v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
float fnManyArgs(const float& v0, int, char*)
|
||||
{
|
||||
return v0;
|
||||
}
|
||||
|
||||
struct OpNoArg { float operator()() { return kRes; } };
|
||||
struct OpOneArg { float operator()(float v) { return v; } };
|
||||
struct OpManyArgs { float operator()(const float& v, int, char*) { return v; } };
|
||||
struct OpNoArg
|
||||
{
|
||||
float operator()() { return kRes; }
|
||||
};
|
||||
struct OpOneArg
|
||||
{
|
||||
float operator()(float v) { return v; }
|
||||
};
|
||||
struct OpManyArgs
|
||||
{
|
||||
float operator()(const float& v, int, char*) { return v; }
|
||||
};
|
||||
|
||||
struct OpCNoArg { float operator()() const { return kRes; } };
|
||||
struct OpCOneArg { float operator()(float v) const { return v; } };
|
||||
struct OpCManyArgs { float operator()(const float& v, int, char*) const { return v; } };
|
||||
struct OpCNoArg
|
||||
{
|
||||
float operator()() const { return kRes; }
|
||||
};
|
||||
struct OpCOneArg
|
||||
{
|
||||
float operator()(float v) const { return v; }
|
||||
};
|
||||
struct OpCManyArgs
|
||||
{
|
||||
float operator()(const float& v, int, char*) const { return v; }
|
||||
};
|
||||
|
||||
struct OpVNoArg { float operator()() volatile { return kRes; } };
|
||||
struct OpVOneArg { float operator()(float v) volatile { return v; } };
|
||||
struct OpVManyArgs { float operator()(const float& v, int, char*) volatile { return v; } };
|
||||
struct OpVNoArg
|
||||
{
|
||||
float operator()() volatile { return kRes; }
|
||||
};
|
||||
struct OpVOneArg
|
||||
{
|
||||
float operator()(float v) volatile { return v; }
|
||||
};
|
||||
struct OpVManyArgs
|
||||
{
|
||||
float operator()(const float& v, int, char*) volatile { return v; }
|
||||
};
|
||||
|
||||
struct OpCVNoArg { float operator()() const volatile { return kRes; } };
|
||||
struct OpCVOneArg { float operator()(float v) const volatile { return v; } };
|
||||
struct OpCVManyArgs { float operator()(const float& v, int, char*) const volatile { return v; } };
|
||||
struct OpCVNoArg
|
||||
{
|
||||
float operator()() const volatile { return kRes; }
|
||||
};
|
||||
struct OpCVOneArg
|
||||
{
|
||||
float operator()(float v) const volatile { return v; }
|
||||
};
|
||||
struct OpCVManyArgs
|
||||
{
|
||||
float operator()(const float& v, int, char*) const volatile { return v; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
|
@ -270,11 +270,11 @@ void tst_qpromise_construct::connectAndResolve()
|
||||
std::weak_ptr<int> wptr;
|
||||
|
||||
{
|
||||
auto p = QPromise<std::shared_ptr<int>>{[&](
|
||||
const QPromiseResolve<std::shared_ptr<int>>& resolve,
|
||||
auto p =
|
||||
QPromise<std::shared_ptr<int>>{[&](const QPromiseResolve<std::shared_ptr<int>>& resolve,
|
||||
const QPromiseReject<std::shared_ptr<int>>& reject) {
|
||||
|
||||
connect(object.data(), &QObject::objectNameChanged,
|
||||
connect(object.data(),
|
||||
&QObject::objectNameChanged,
|
||||
[=, &wptr](const QString& name) {
|
||||
auto sptr = std::make_shared<int>(42);
|
||||
|
||||
@ -307,12 +307,9 @@ void tst_qpromise_construct::connectAndReject()
|
||||
std::weak_ptr<int> wptr;
|
||||
|
||||
{
|
||||
auto p = QPromise<int>{[&](
|
||||
const QPromiseResolve<int>& resolve,
|
||||
auto p = QPromise<int>{[&](const QPromiseResolve<int>& resolve,
|
||||
const QPromiseReject<int>& reject) {
|
||||
|
||||
connect(object.data(), &QObject::objectNameChanged,
|
||||
[=, &wptr](const QString& name) {
|
||||
connect(object.data(), &QObject::objectNameChanged, [=, &wptr](const QString& name) {
|
||||
auto sptr = std::make_shared<int>(42);
|
||||
|
||||
wptr = sptr;
|
||||
|
@ -38,30 +38,28 @@ struct SequenceTester
|
||||
static void exec()
|
||||
{
|
||||
QVector<int> values;
|
||||
auto p = QtPromise::resolve(Sequence{42, 43, 44}).each([&](int v, int i) {
|
||||
auto p = QtPromise::resolve(Sequence{42, 43, 44})
|
||||
.each([&](int v, int i) {
|
||||
values << i << v;
|
||||
}).each([&](int v, ...) {
|
||||
})
|
||||
.each([&](int v, ...) {
|
||||
values << v;
|
||||
return QString{"foo"};
|
||||
}).each([&](int v, ...) {
|
||||
})
|
||||
.each([&](int v, ...) {
|
||||
values << v + 1;
|
||||
return QtPromise::resolve(QString{"foo"}).then([&]() {
|
||||
values << -1;
|
||||
});
|
||||
}).each([&](int v, ...) {
|
||||
})
|
||||
.each([&](int v, ...) {
|
||||
values << v + 2;
|
||||
});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Sequence>>::value));
|
||||
QCOMPARE(waitForValue(p, Sequence{}), (Sequence{42, 43, 44}));
|
||||
|
||||
QVector<int> expected{
|
||||
0, 42, 1, 43, 2, 44,
|
||||
42, 43, 44,
|
||||
43, 44, 45,
|
||||
-1, -1, -1,
|
||||
44, 45, 46
|
||||
};
|
||||
QVector<int> expected{0, 42, 1, 43, 2, 44, 42, 43, 44, 43, 44, 45, -1, -1, -1, 44, 45, 46};
|
||||
QCOMPARE(values, expected);
|
||||
}
|
||||
};
|
||||
@ -124,9 +122,8 @@ void tst_qpromise_each::delayedFulfilled()
|
||||
void tst_qpromise_each::delayedRejected()
|
||||
{
|
||||
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).each([](int v, ...) {
|
||||
return QPromise<int>{[&](
|
||||
const QPromiseResolve<int>& resolve,
|
||||
const QPromiseReject<int>& reject) {
|
||||
return QPromise<int>{
|
||||
[&](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
if (v == 44) {
|
||||
reject(QString{"foo"});
|
||||
|
@ -36,11 +36,21 @@ const QString kErr{"0.42"};
|
||||
const float kRes = 0.42f;
|
||||
const float kFail = -1.f;
|
||||
|
||||
float fnNoArg() { return kErr.toFloat(); }
|
||||
float fnArgByVal(QString e) { return e.toFloat(); }
|
||||
float fnArgByRef(const QString& e) { return e.toFloat(); }
|
||||
float fnNoArg()
|
||||
{
|
||||
return kErr.toFloat();
|
||||
}
|
||||
float fnArgByVal(QString e)
|
||||
{
|
||||
return e.toFloat();
|
||||
}
|
||||
float fnArgByRef(const QString& e)
|
||||
{
|
||||
return e.toFloat();
|
||||
}
|
||||
|
||||
class Klass {
|
||||
class Klass
|
||||
{
|
||||
public: // STATICS
|
||||
static float kFnNoArg() { return kErr.toFloat(); }
|
||||
static float kFnArgByVal(QString e) { return e.toFloat(); }
|
||||
@ -68,13 +78,16 @@ void tst_qpromise_fail::sameType()
|
||||
p.fail([&](const std::domain_error& e) {
|
||||
error += QString{e.what()} + "0";
|
||||
return -1;
|
||||
}).fail([&](const std::out_of_range& e) {
|
||||
})
|
||||
.fail([&](const std::out_of_range& e) {
|
||||
error += QString{e.what()} + "1";
|
||||
return -1;
|
||||
}).fail([&](const std::exception& e) {
|
||||
})
|
||||
.fail([&](const std::exception& e) {
|
||||
error += QString{e.what()} + "2";
|
||||
return -1;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(error, QString{"foo1"});
|
||||
}
|
||||
@ -88,13 +101,16 @@ void tst_qpromise_fail::baseClass()
|
||||
p.fail([&](const std::runtime_error& e) {
|
||||
error += QString{e.what()} + "0";
|
||||
return -1;
|
||||
}).fail([&](const std::logic_error& e) {
|
||||
})
|
||||
.fail([&](const std::logic_error& e) {
|
||||
error += QString{e.what()} + "1";
|
||||
return -1;
|
||||
}).fail([&](const std::exception& e) {
|
||||
})
|
||||
.fail([&](const std::exception& e) {
|
||||
error += QString{e.what()} + "2";
|
||||
return -1;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(error, QString{"foo1"});
|
||||
}
|
||||
@ -107,13 +123,16 @@ void tst_qpromise_fail::catchAll()
|
||||
p.fail([&](const std::runtime_error& e) {
|
||||
error += QString{e.what()} + "0";
|
||||
return -1;
|
||||
}).fail([&]() {
|
||||
})
|
||||
.fail([&]() {
|
||||
error += "bar";
|
||||
return -1;
|
||||
}).fail([&](const std::exception& e) {
|
||||
})
|
||||
.fail([&](const std::exception& e) {
|
||||
error += QString{e.what()} + "2";
|
||||
return -1;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(error, QString{"bar"});
|
||||
}
|
||||
@ -172,7 +191,8 @@ void tst_qpromise_fail::stdFunctionHandlers()
|
||||
{ // rvalue.
|
||||
auto p0 = QPromise<float>::reject(kErr).fail(std::function<float()>{fnNoArg});
|
||||
auto p1 = QPromise<float>::reject(kErr).fail(std::function<float(QString)>{fnArgByVal});
|
||||
auto p2 = QPromise<float>::reject(kErr).fail(std::function<float(const QString&)>{fnArgByRef});
|
||||
auto p2 =
|
||||
QPromise<float>::reject(kErr).fail(std::function<float(const QString&)>{fnArgByRef});
|
||||
|
||||
QCOMPARE(waitForValue(p0, kFail), kRes);
|
||||
QCOMPARE(waitForValue(p1, kFail), kRes);
|
||||
@ -190,7 +210,8 @@ void tst_qpromise_fail::stdBindHandlers()
|
||||
|
||||
const std::function<float()> bindNoArg = std::bind(&Klass::fnNoArg, &obj);
|
||||
const std::function<float(QString)> bindArgByVal = std::bind(&Klass::fnArgByVal, &obj, _1);
|
||||
const std::function<float(const QString&)> bindArgByRef = std::bind(&Klass::fnArgByRef, &obj, _1);
|
||||
const std::function<float(const QString&)> bindArgByRef =
|
||||
std::bind(&Klass::fnArgByRef, &obj, _1);
|
||||
|
||||
auto p0 = QPromise<float>::reject(kErr).fail(bindNoArg);
|
||||
auto p1 = QPromise<float>::reject(kErr).fail(bindArgByVal);
|
||||
@ -204,9 +225,15 @@ void tst_qpromise_fail::stdBindHandlers()
|
||||
void tst_qpromise_fail::lambdaHandlers()
|
||||
{
|
||||
{ // lvalue.
|
||||
auto lambdaNoArg = []() { return kRes; };
|
||||
auto lambdaArgByVal = [](QString v) { return v.toFloat(); };
|
||||
auto lambdaArgByRef = [](const QString& v) { return v.toFloat(); };
|
||||
auto lambdaNoArg = []() {
|
||||
return kRes;
|
||||
};
|
||||
auto lambdaArgByVal = [](QString v) {
|
||||
return v.toFloat();
|
||||
};
|
||||
auto lambdaArgByRef = [](const QString& v) {
|
||||
return v.toFloat();
|
||||
};
|
||||
|
||||
auto p0 = QPromise<float>::reject(kErr).fail(lambdaNoArg);
|
||||
auto p1 = QPromise<float>::reject(kErr).fail(lambdaArgByVal);
|
||||
@ -217,9 +244,15 @@ void tst_qpromise_fail::lambdaHandlers()
|
||||
QCOMPARE(waitForValue(p2, kFail), kRes);
|
||||
}
|
||||
{ // const lvalue.
|
||||
const auto lambdaNoArg = []() { return kRes; };
|
||||
const auto lambdaArgByVal = [](QString v) { return v.toFloat(); };
|
||||
const auto lambdaArgByRef = [](const QString& v) { return v.toFloat(); };
|
||||
const auto lambdaNoArg = []() {
|
||||
return kRes;
|
||||
};
|
||||
const auto lambdaArgByVal = [](QString v) {
|
||||
return v.toFloat();
|
||||
};
|
||||
const auto lambdaArgByRef = [](const QString& v) {
|
||||
return v.toFloat();
|
||||
};
|
||||
|
||||
auto p0 = QPromise<float>::reject(kErr).fail(lambdaNoArg);
|
||||
auto p1 = QPromise<float>::reject(kErr).fail(lambdaArgByVal);
|
||||
@ -230,9 +263,15 @@ void tst_qpromise_fail::lambdaHandlers()
|
||||
QCOMPARE(waitForValue(p2, kFail), kRes);
|
||||
}
|
||||
{ // rvalue.
|
||||
auto p0 = QPromise<float>::reject(kErr).fail([]() { return kRes; });
|
||||
auto p1 = QPromise<float>::reject(kErr).fail([](QString v) { return v.toFloat(); });
|
||||
auto p2 = QPromise<float>::reject(kErr).fail([](const QString& v) { return v.toFloat(); });
|
||||
auto p0 = QPromise<float>::reject(kErr).fail([]() {
|
||||
return kRes;
|
||||
});
|
||||
auto p1 = QPromise<float>::reject(kErr).fail([](QString v) {
|
||||
return v.toFloat();
|
||||
});
|
||||
auto p2 = QPromise<float>::reject(kErr).fail([](const QString& v) {
|
||||
return v.toFloat();
|
||||
});
|
||||
|
||||
QCOMPARE(waitForValue(p0, kFail), kRes);
|
||||
QCOMPARE(waitForValue(p1, kFail), kRes);
|
||||
|
@ -38,13 +38,14 @@ struct SequenceTester
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
auto p = QtPromise::resolve(Sequence{
|
||||
42, 43, 44, 45, 46, 47, 48, 49, 50, 51
|
||||
}).filter([](int v, ...) {
|
||||
auto p = QtPromise::resolve(Sequence{42, 43, 44, 45, 46, 47, 48, 49, 50, 51})
|
||||
.filter([](int v, ...) {
|
||||
return v > 42 && v < 51;
|
||||
}).filter([](int, int i) {
|
||||
})
|
||||
.filter([](int, int i) {
|
||||
return QPromise<bool>::resolve(i % 2 == 0);
|
||||
}).filter([](int v, ...) {
|
||||
})
|
||||
.filter([](int v, ...) {
|
||||
return v != 45;
|
||||
});
|
||||
|
||||
@ -92,9 +93,8 @@ void tst_qpromise_filter::delayedFulfilled()
|
||||
void tst_qpromise_filter::delayedRejected()
|
||||
{
|
||||
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) {
|
||||
return QPromise<bool>{[&](
|
||||
const QPromiseResolve<bool>& resolve,
|
||||
const QPromiseReject<bool>& reject) {
|
||||
return QPromise<bool>{
|
||||
[&](const QPromiseResolve<bool>& resolve, const QPromiseReject<bool>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
if (v == 43) {
|
||||
reject(QString{"foo"});
|
||||
|
@ -38,18 +38,23 @@ struct SequenceTester
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
auto p = QtPromise::resolve(Sequence{42, 43, 44}).map([](int v, ...) {
|
||||
auto p = QtPromise::resolve(Sequence{42, 43, 44})
|
||||
.map([](int v, ...) {
|
||||
return QString::number(v + 1);
|
||||
}).map([](const QString& v, int i) {
|
||||
})
|
||||
.map([](const QString& v, int i) {
|
||||
return QtPromise::resolve(QString{"%1:%2"}.arg(i).arg(v));
|
||||
}).map([](const QString& v, ...) {
|
||||
})
|
||||
.map([](const QString& v, ...) {
|
||||
return QtPromise::resolve((v + "!").toUtf8());
|
||||
}).map([](const QByteArray& v, ...) {
|
||||
})
|
||||
.map([](const QByteArray& v, ...) {
|
||||
return QString::fromUtf8(v);
|
||||
});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<QString>>>::value));
|
||||
QCOMPARE(waitForValue(p, QVector<QString>{}), (QVector<QString>{"0:43!", "1:44!", "2:45!"}));
|
||||
QCOMPARE(waitForValue(p, QVector<QString>{}),
|
||||
(QVector<QString>{"0:43!", "1:44!", "2:45!"}));
|
||||
}
|
||||
};
|
||||
|
||||
@ -102,9 +107,8 @@ void tst_qpromise_map::delayedFulfilled()
|
||||
void tst_qpromise_map::delayedRejected()
|
||||
{
|
||||
auto p = QtPromise::resolve(QVector<int>{42, 43, 44}).map([](int v, ...) {
|
||||
return QPromise<int>{[&](
|
||||
const QPromiseResolve<int>& resolve,
|
||||
const QPromiseReject<int>& reject) {
|
||||
return QPromise<int>{
|
||||
[&](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
if (v == 43) {
|
||||
reject(QString{"foo"});
|
||||
|
@ -39,11 +39,9 @@ struct SequenceTester
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
Sequence inputs{
|
||||
QtPromise::resolve(4).delay(400),
|
||||
Sequence inputs{QtPromise::resolve(4).delay(400),
|
||||
QtPromise::resolve(6).delay(300),
|
||||
QtPromise::resolve(8).delay(200)
|
||||
};
|
||||
QtPromise::resolve(8).delay(200)};
|
||||
QVector<int> v0;
|
||||
QVector<int> v1;
|
||||
|
||||
@ -51,10 +49,12 @@ struct SequenceTester
|
||||
v0 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
});
|
||||
auto p1 = QtPromise::resolve(inputs).reduce([&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::resolve(inputs).reduce(
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
}, QtPromise::resolve(2).delay(100));
|
||||
},
|
||||
QtPromise::resolve(2).delay(100));
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -74,10 +74,13 @@ void tst_qpromise_reduce::emptySequence()
|
||||
{
|
||||
bool called = false;
|
||||
|
||||
auto p = QtPromise::resolve(QVector<int>{}).reduce([&](...) {
|
||||
auto p = QtPromise::resolve(QVector<int>{})
|
||||
.reduce(
|
||||
[&](...) {
|
||||
called = true;
|
||||
return 43;
|
||||
}, 42);
|
||||
},
|
||||
42);
|
||||
|
||||
// NOTE(SB): reduce() on an empty sequence without an initial value is an error!
|
||||
|
||||
@ -97,10 +100,12 @@ void tst_qpromise_reduce::regularValues()
|
||||
v0 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
});
|
||||
auto p1 = QtPromise::resolve(inputs).reduce([&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::resolve(inputs).reduce(
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -115,11 +120,9 @@ void tst_qpromise_reduce::regularValues()
|
||||
|
||||
void tst_qpromise_reduce::promiseValues()
|
||||
{
|
||||
QVector<QPromise<int>> inputs{
|
||||
QtPromise::resolve(4).delay(400),
|
||||
QVector<QPromise<int>> inputs{QtPromise::resolve(4).delay(400),
|
||||
QtPromise::resolve(6).delay(300),
|
||||
QtPromise::resolve(8).delay(200)
|
||||
};
|
||||
QtPromise::resolve(8).delay(200)};
|
||||
QVector<int> v0;
|
||||
QVector<int> v1;
|
||||
|
||||
@ -127,10 +130,12 @@ void tst_qpromise_reduce::promiseValues()
|
||||
v0 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
});
|
||||
auto p1 = QtPromise::resolve(inputs).reduce([&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::resolve(inputs).reduce(
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -147,9 +152,11 @@ void tst_qpromise_reduce::convertResultType()
|
||||
{
|
||||
QVector<int> inputs{4, 6, 8};
|
||||
|
||||
auto p = QtPromise::resolve(inputs).reduce([&](const QString& acc, int cur, int idx) {
|
||||
auto p = QtPromise::resolve(inputs).reduce(
|
||||
[&](const QString& acc, int cur, int idx) {
|
||||
return QString{"%1:%2:%3"}.arg(acc).arg(cur).arg(idx);
|
||||
}, QString{"foo"});
|
||||
},
|
||||
QString{"foo"});
|
||||
|
||||
// NOTE(SB): when no initial value is given, the result type is the sequence type.
|
||||
|
||||
@ -163,10 +170,13 @@ void tst_qpromise_reduce::delayedInitialValue()
|
||||
{
|
||||
QVector<int> values;
|
||||
|
||||
auto p = QtPromise::resolve(QVector<int>{4, 6, 8}).reduce([&](int acc, int cur, int idx) {
|
||||
auto p = QtPromise::resolve(QVector<int>{4, 6, 8})
|
||||
.reduce(
|
||||
[&](int acc, int cur, int idx) {
|
||||
values << acc << cur << idx;
|
||||
return acc + cur + idx;
|
||||
}, QtPromise::resolve(2).delay(100));
|
||||
},
|
||||
QtPromise::resolve(2).delay(100));
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value));
|
||||
|
||||
@ -185,10 +195,12 @@ void tst_qpromise_reduce::delayedFulfilled()
|
||||
v0 << acc << cur << idx;
|
||||
return QtPromise::resolve(acc + cur + idx).delay(100);
|
||||
});
|
||||
auto p1 = QtPromise::resolve(inputs).reduce([&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::resolve(inputs).reduce(
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
return QtPromise::resolve(acc + cur + idx).delay(100);
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -214,13 +226,15 @@ void tst_qpromise_reduce::delayedRejected()
|
||||
}
|
||||
return QtPromise::resolve(acc + cur + idx);
|
||||
});
|
||||
auto p1 = QtPromise::resolve(inputs).reduce([&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::resolve(inputs).reduce(
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
if (cur == 6) {
|
||||
return QPromise<int>::reject(QString{"bar"});
|
||||
}
|
||||
return QtPromise::resolve(acc + cur + idx);
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
@ -246,13 +260,15 @@ void tst_qpromise_reduce::functorThrows()
|
||||
}
|
||||
return acc + cur + idx;
|
||||
});
|
||||
auto p1 = QtPromise::resolve(inputs).reduce([&](int acc, int cur, int idx) {
|
||||
auto p1 = QtPromise::resolve(inputs).reduce(
|
||||
[&](int acc, int cur, int idx) {
|
||||
v1 << acc << cur << idx;
|
||||
if (cur == 6) {
|
||||
throw QString{"bar"};
|
||||
}
|
||||
return acc + cur + idx;
|
||||
}, 2);
|
||||
},
|
||||
2);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
|
||||
|
@ -58,13 +58,11 @@ void tst_qpromise_tapfail::rejected()
|
||||
{
|
||||
QStringList errors;
|
||||
|
||||
auto p0 = QPromise<int>::reject(QString{"foo"})
|
||||
.tapFail([&](const QString& err) {
|
||||
auto p0 = QPromise<int>::reject(QString{"foo"}).tapFail([&](const QString& err) {
|
||||
errors << "1:" + err;
|
||||
});
|
||||
|
||||
auto p1 = p0
|
||||
.fail([&](const QString& err) {
|
||||
auto p1 = p0.fail([&](const QString& err) {
|
||||
errors << "2:" + err;
|
||||
return 43;
|
||||
});
|
||||
@ -80,13 +78,11 @@ void tst_qpromise_tapfail::rejected_void()
|
||||
{
|
||||
QStringList errors;
|
||||
|
||||
auto p0 = QPromise<void>::reject(QString{"foo"})
|
||||
.tapFail([&](const QString& err) {
|
||||
auto p0 = QPromise<void>::reject(QString{"foo"}).tapFail([&](const QString& err) {
|
||||
errors << "1:" + err;
|
||||
});
|
||||
|
||||
auto p1 = p0
|
||||
.fail([&](const QString& err) {
|
||||
auto p1 = p0.fail([&](const QString& err) {
|
||||
errors << "2:" + err;
|
||||
});
|
||||
|
||||
@ -99,8 +95,7 @@ void tst_qpromise_tapfail::rejected_void()
|
||||
|
||||
void tst_qpromise_tapfail::throws()
|
||||
{
|
||||
auto p = QPromise<int>::reject(QString{"foo"})
|
||||
.tapFail([&]() {
|
||||
auto p = QPromise<int>::reject(QString{"foo"}).tapFail([&]() {
|
||||
throw QString{"bar"};
|
||||
});
|
||||
|
||||
@ -110,8 +105,7 @@ void tst_qpromise_tapfail::throws()
|
||||
|
||||
void tst_qpromise_tapfail::throws_void()
|
||||
{
|
||||
auto p = QPromise<void>::reject(QString{"foo"})
|
||||
.tapFail([&]() {
|
||||
auto p = QPromise<void>::reject(QString{"foo"}).tapFail([&]() {
|
||||
throw QString{"bar"};
|
||||
});
|
||||
|
||||
@ -122,8 +116,7 @@ void tst_qpromise_tapfail::throws_void()
|
||||
void tst_qpromise_tapfail::delayedResolved()
|
||||
{
|
||||
QVector<int> values;
|
||||
auto p = QPromise<int>::reject(QString{"foo"})
|
||||
.tapFail([&]() {
|
||||
auto p = QPromise<int>::reject(QString{"foo"}).tapFail([&]() {
|
||||
QPromise<void> p{[&](const QPromiseResolve<void>& resolve) {
|
||||
QtPromisePrivate::qtpromise_defer([=, &values]() {
|
||||
values << 3;
|
||||
@ -142,11 +135,8 @@ void tst_qpromise_tapfail::delayedResolved()
|
||||
void tst_qpromise_tapfail::delayedRejected()
|
||||
{
|
||||
QVector<int> values;
|
||||
auto p = QPromise<int>::reject(QString{"foo"})
|
||||
.tapFail([&]() {
|
||||
QPromise<void> p{[&](
|
||||
const QPromiseResolve<void>&,
|
||||
const QPromiseReject<void>& reject){
|
||||
auto p = QPromise<int>::reject(QString{"foo"}).tapFail([&]() {
|
||||
QPromise<void> p{[&](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=, &values]() {
|
||||
values << 3;
|
||||
reject(QString{"bar"});
|
||||
|
@ -39,11 +39,21 @@ namespace {
|
||||
const float kRes = 0.42f;
|
||||
const float kFail = -1.f;
|
||||
|
||||
float fnNoArg() { return kRes; }
|
||||
float fnArgByVal(float v) { return v; }
|
||||
float fnArgByRef(const float& v) { return v; }
|
||||
float fnNoArg()
|
||||
{
|
||||
return kRes;
|
||||
}
|
||||
float fnArgByVal(float v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
float fnArgByRef(const float& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
class Klass {
|
||||
class Klass
|
||||
{
|
||||
public: // STATICS
|
||||
static float kFnNoArg() { return kRes; }
|
||||
static float kFnArgByVal(float v) { return v; }
|
||||
@ -72,11 +82,14 @@ void tst_qpromise_then::resolveSync()
|
||||
return QString::number(res + 1);
|
||||
});
|
||||
|
||||
output.then([&](const QString& res) {
|
||||
output
|
||||
.then([&](const QString& res) {
|
||||
values << res;
|
||||
}).then([&]() {
|
||||
})
|
||||
.then([&]() {
|
||||
values << 44;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(values, (QVariantList{42, QString{"43"}, 44}));
|
||||
QCOMPARE(input.isFulfilled(), true);
|
||||
@ -107,11 +120,14 @@ void tst_qpromise_then::rejectSync()
|
||||
});
|
||||
|
||||
QString error;
|
||||
output.then([&](int res) {
|
||||
output
|
||||
.then([&](int res) {
|
||||
error += "bar" + QString::number(res);
|
||||
}).fail([&](const QString& err) {
|
||||
})
|
||||
.fail([&](const QString& err) {
|
||||
error += err;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(error, QString{"foo42"});
|
||||
QCOMPARE(input.isFulfilled(), true);
|
||||
@ -121,7 +137,8 @@ void tst_qpromise_then::rejectSync()
|
||||
void tst_qpromise_then::rejectAsync()
|
||||
{
|
||||
auto p = QPromise<int>::resolve(42).then([](int res) {
|
||||
return QPromise<void>{[=](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) {
|
||||
return QPromise<void>{
|
||||
[=](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
reject(QString{"foo%1"}.arg(res));
|
||||
});
|
||||
@ -248,9 +265,15 @@ void tst_qpromise_then::stdBindHandlers()
|
||||
void tst_qpromise_then::lambdaHandlers()
|
||||
{
|
||||
{ // lvalue.
|
||||
auto lambdaNoArg = []() { return kRes; };
|
||||
auto lambdaArgByVal = [](float v) { return v; };
|
||||
auto lambdaArgByRef = [](const float& v) { return v; };
|
||||
auto lambdaNoArg = []() {
|
||||
return kRes;
|
||||
};
|
||||
auto lambdaArgByVal = [](float v) {
|
||||
return v;
|
||||
};
|
||||
auto lambdaArgByRef = [](const float& v) {
|
||||
return v;
|
||||
};
|
||||
|
||||
auto p0 = QtPromise::resolve().then(lambdaNoArg);
|
||||
auto p1 = QtPromise::resolve(kRes).then(lambdaArgByVal);
|
||||
@ -261,9 +284,15 @@ void tst_qpromise_then::lambdaHandlers()
|
||||
QCOMPARE(waitForValue(p2, kFail), kRes);
|
||||
}
|
||||
{ // const lvalue.
|
||||
const auto lambdaNoArg = []() { return kRes; };
|
||||
const auto lambdaArgByVal = [](float v) { return v; };
|
||||
const auto lambdaArgByRef = [](const float& v) { return v; };
|
||||
const auto lambdaNoArg = []() {
|
||||
return kRes;
|
||||
};
|
||||
const auto lambdaArgByVal = [](float v) {
|
||||
return v;
|
||||
};
|
||||
const auto lambdaArgByRef = [](const float& v) {
|
||||
return v;
|
||||
};
|
||||
|
||||
auto p0 = QtPromise::resolve().then(lambdaNoArg);
|
||||
auto p1 = QtPromise::resolve(kRes).then(lambdaArgByVal);
|
||||
@ -274,9 +303,15 @@ void tst_qpromise_then::lambdaHandlers()
|
||||
QCOMPARE(waitForValue(p2, kFail), kRes);
|
||||
}
|
||||
{ // rvalue.
|
||||
auto p0 = QtPromise::resolve().then([]() { return kRes; });
|
||||
auto p1 = QtPromise::resolve(kRes).then([](float v) { return v; });
|
||||
auto p2 = QtPromise::resolve(kRes).then([](const float& v) { return v; });
|
||||
auto p0 = QtPromise::resolve().then([]() {
|
||||
return kRes;
|
||||
});
|
||||
auto p1 = QtPromise::resolve(kRes).then([](float v) {
|
||||
return v;
|
||||
});
|
||||
auto p2 = QtPromise::resolve(kRes).then([](const float& v) {
|
||||
return v;
|
||||
});
|
||||
|
||||
QCOMPARE(waitForValue(p0, kFail), kRes);
|
||||
QCOMPARE(waitForValue(p1, kFail), kRes);
|
||||
|
@ -42,7 +42,8 @@ void tst_qpromise_timeout::fulfilled()
|
||||
QTimer::singleShot(1000, [=]() {
|
||||
resolve(42);
|
||||
});
|
||||
}}.timeout(2000).finally([&]() {
|
||||
}}.timeout(2000)
|
||||
.finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
@ -62,11 +63,11 @@ void tst_qpromise_timeout::rejected()
|
||||
QTimer::singleShot(1000, [=]() {
|
||||
reject(QString{"foo"});
|
||||
});
|
||||
}}.timeout(2000).finally([&]() {
|
||||
}}.timeout(2000)
|
||||
.finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
|
||||
QCOMPARE(waitForError(p, QString{}), QString{"foo"});
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QVERIFY(elapsed < 2000);
|
||||
@ -84,7 +85,8 @@ void tst_qpromise_timeout::timeout()
|
||||
QTimer::singleShot(4000, [=]() {
|
||||
resolve(42);
|
||||
});
|
||||
}}.timeout(2000).finally([&]() {
|
||||
}}.timeout(2000)
|
||||
.finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
@ -115,7 +117,8 @@ void tst_qpromise_timeout::fulfilledStdChrono()
|
||||
QTimer::singleShot(1000, [=]() {
|
||||
resolve(42);
|
||||
});
|
||||
}}.timeout(std::chrono::seconds{2}).finally([&]() {
|
||||
}}.timeout(std::chrono::seconds{2})
|
||||
.finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
@ -135,11 +138,11 @@ void tst_qpromise_timeout::rejectedStdChrono()
|
||||
QTimer::singleShot(1000, [=]() {
|
||||
reject(QString{"foo"});
|
||||
});
|
||||
}}.timeout(std::chrono::seconds{2}).finally([&]() {
|
||||
}}.timeout(std::chrono::seconds{2})
|
||||
.finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
|
||||
QCOMPARE(waitForError(p, QString{}), QString{"foo"});
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QVERIFY(elapsed < 2000);
|
||||
@ -157,7 +160,8 @@ void tst_qpromise_timeout::timeoutStdChrono()
|
||||
QTimer::singleShot(4000, [=]() {
|
||||
resolve(42);
|
||||
});
|
||||
}}.timeout(std::chrono::seconds{2}).finally([&]() {
|
||||
}}.timeout(std::chrono::seconds{2})
|
||||
.finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
|
@ -44,7 +44,9 @@ void tst_requirements::statePending()
|
||||
// 2.1.1.1. may transition to either the fulfilled state
|
||||
{
|
||||
QPromise<int> p{[&](const QPromiseResolve<int>& resolve) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() { resolve(42); });
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
resolve(42);
|
||||
});
|
||||
}};
|
||||
|
||||
QVERIFY(p.isPending());
|
||||
@ -61,7 +63,9 @@ void tst_requirements::statePending()
|
||||
// 2.1.1.1. ... or the rejected state
|
||||
{
|
||||
QPromise<int> p{[&](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() { reject(QString{"foo"}); });
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
reject(QString{"foo"});
|
||||
});
|
||||
}};
|
||||
|
||||
QVERIFY(p.isPending());
|
||||
@ -82,9 +86,7 @@ void tst_requirements::stateFulfilled()
|
||||
int value = -1;
|
||||
|
||||
// 2.1.2. When fulfilled, a promise:
|
||||
QPromise<int> p{[](
|
||||
const QPromiseResolve<int>& resolve,
|
||||
const QPromiseReject<int>& reject) {
|
||||
QPromise<int> p{[](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
// 2.1.2.2. must have a value, which must not change.
|
||||
resolve(42);
|
||||
@ -97,11 +99,14 @@ void tst_requirements::stateFulfilled()
|
||||
|
||||
QVERIFY(p.isPending());
|
||||
|
||||
p.then([&](int res) {
|
||||
p.then(
|
||||
[&](int res) {
|
||||
value = res;
|
||||
}, [&](const QString& err) {
|
||||
},
|
||||
[&](const QString& err) {
|
||||
error = err;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QVERIFY(p.isFulfilled());
|
||||
QVERIFY(!p.isRejected());
|
||||
@ -115,9 +120,7 @@ void tst_requirements::stateRejected()
|
||||
int value = -1;
|
||||
|
||||
// 2.1.3 When rejected, a promise:
|
||||
QPromise<int> p{[](
|
||||
const QPromiseResolve<int>& resolve,
|
||||
const QPromiseReject<int>& reject) {
|
||||
QPromise<int> p{[](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=]() {
|
||||
// 2.1.3.2. must have a reason, which must not change.
|
||||
reject(QString{"foo"});
|
||||
@ -130,11 +133,14 @@ void tst_requirements::stateRejected()
|
||||
|
||||
QVERIFY(p.isPending());
|
||||
|
||||
p.then([&](int res) {
|
||||
p.then(
|
||||
[&](int res) {
|
||||
value = res;
|
||||
}, [&](const QString& err) {
|
||||
},
|
||||
[&](const QString& err) {
|
||||
error = err;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QVERIFY(!p.isFulfilled());
|
||||
QVERIFY(p.isRejected());
|
||||
@ -148,10 +154,15 @@ void tst_requirements::thenArguments()
|
||||
{
|
||||
QString error;
|
||||
int value = -1;
|
||||
QPromise<int>::resolve(42).then(
|
||||
[&](int res) { value = res; },
|
||||
[&](const QString& err) { error = err; }
|
||||
).wait();
|
||||
QPromise<int>::resolve(42)
|
||||
.then(
|
||||
[&](int res) {
|
||||
value = res;
|
||||
},
|
||||
[&](const QString& err) {
|
||||
error = err;
|
||||
})
|
||||
.wait();
|
||||
|
||||
QVERIFY(error.isEmpty());
|
||||
QCOMPARE(value, 42);
|
||||
@ -159,10 +170,15 @@ void tst_requirements::thenArguments()
|
||||
{
|
||||
QString error;
|
||||
int value = -1;
|
||||
QPromise<int>::reject(QString{"foo"}).then(
|
||||
[&](int res) { value = res; },
|
||||
[&](const QString& err){ error = err; }
|
||||
).wait();
|
||||
QPromise<int>::reject(QString{"foo"})
|
||||
.then(
|
||||
[&](int res) {
|
||||
value = res;
|
||||
},
|
||||
[&](const QString& err) {
|
||||
error = err;
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(error, QString{"foo"});
|
||||
QCOMPARE(value, -1);
|
||||
@ -171,10 +187,13 @@ void tst_requirements::thenArguments()
|
||||
// 2.2.1. onFulfilled is an optional arguments:
|
||||
{
|
||||
QString error;
|
||||
QPromise<int>::reject(QString{"foo"}).then(
|
||||
nullptr,
|
||||
[&](const QString& err){ error = err; return 42; }
|
||||
).wait();
|
||||
QPromise<int>::reject(QString{"foo"})
|
||||
.then(nullptr,
|
||||
[&](const QString& err) {
|
||||
error = err;
|
||||
return 42;
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(error, QString{"foo"});
|
||||
}
|
||||
@ -182,9 +201,11 @@ void tst_requirements::thenArguments()
|
||||
// 2.2.1. onRejected is an optional arguments:
|
||||
{
|
||||
int value = -1;
|
||||
QPromise<int>::resolve(42).then(
|
||||
[&value](int res) { value = res; }
|
||||
).wait();
|
||||
QPromise<int>::resolve(42)
|
||||
.then([&value](int res) {
|
||||
value = res;
|
||||
})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(value, 42);
|
||||
}
|
||||
@ -206,7 +227,9 @@ void tst_requirements::thenOnFulfilled()
|
||||
});
|
||||
}};
|
||||
|
||||
auto p1 = p0.then([&](int res) { values << res; });
|
||||
auto p1 = p0.then([&](int res) {
|
||||
values << res;
|
||||
});
|
||||
|
||||
// 2.2.2.2. it must not be called before promise is fulfilled.
|
||||
QVERIFY(p0.isPending());
|
||||
@ -234,7 +257,9 @@ void tst_requirements::thenOnRejected()
|
||||
});
|
||||
}};
|
||||
|
||||
auto p1 = p0.then(nullptr, [&](const QString& err) { errors << err; });
|
||||
auto p1 = p0.then(nullptr, [&](const QString& err) {
|
||||
errors << err;
|
||||
});
|
||||
|
||||
// 2.2.3.2. it must not be called before promise is rejected.
|
||||
QVERIFY(p0.isPending());
|
||||
@ -260,7 +285,9 @@ void tst_requirements::thenAsynchronous()
|
||||
auto p0 = QPromise<int>::resolve(42);
|
||||
QVERIFY(p0.isFulfilled());
|
||||
|
||||
auto p1 = p0.then([&](int res){ value = res; });
|
||||
auto p1 = p0.then([&](int res) {
|
||||
value = res;
|
||||
});
|
||||
QVERIFY(p1.isPending());
|
||||
QCOMPARE(value, -1);
|
||||
|
||||
@ -283,11 +310,16 @@ void tst_requirements::thenMultipleCalls()
|
||||
});
|
||||
}};
|
||||
|
||||
QtPromise::all(QVector<QPromise<void>>{
|
||||
p.then([&](int r) { values << r + 1; }),
|
||||
p.then([&](int r) { values << r + 2; }),
|
||||
p.then([&](int r) { values << r + 3; })
|
||||
}).wait();
|
||||
QtPromise::all(QVector<QPromise<void>>{p.then([&](int r) {
|
||||
values << r + 1;
|
||||
}),
|
||||
p.then([&](int r) {
|
||||
values << r + 2;
|
||||
}),
|
||||
p.then([&](int r) {
|
||||
values << r + 3;
|
||||
})})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(values, (QVector<int>{43, 44, 45}));
|
||||
}
|
||||
@ -302,11 +334,22 @@ void tst_requirements::thenMultipleCalls()
|
||||
});
|
||||
}};
|
||||
|
||||
QtPromise::all(QVector<QPromise<int>>{
|
||||
p.then(nullptr, [&](int r) { values << r + 1; return r + 1; }),
|
||||
p.then(nullptr, [&](int r) { values << r + 2; return r + 2; }),
|
||||
p.then(nullptr, [&](int r) { values << r + 3; return r + 3; })
|
||||
}).wait();
|
||||
QtPromise::all(QVector<QPromise<int>>{p.then(nullptr,
|
||||
[&](int r) {
|
||||
values << r + 1;
|
||||
return r + 1;
|
||||
}),
|
||||
p.then(nullptr,
|
||||
[&](int r) {
|
||||
values << r + 2;
|
||||
return r + 2;
|
||||
}),
|
||||
p.then(nullptr,
|
||||
[&](int r) {
|
||||
values << r + 3;
|
||||
return r + 3;
|
||||
})})
|
||||
.wait();
|
||||
|
||||
QCOMPARE(values, (QVector<int>{9, 10, 11}));
|
||||
}
|
||||
@ -316,7 +359,9 @@ void tst_requirements::thenHandlers()
|
||||
{
|
||||
// 2.2.7. then must return a promise: p2 = p1.then(onFulfilled, onRejected);
|
||||
{
|
||||
auto handler = [](){ return 42; };
|
||||
auto handler = []() {
|
||||
return 42;
|
||||
};
|
||||
auto p1 = QPromise<int>::resolve(42);
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1.then(handler, nullptr)), QPromise<int>>::value));
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p1.then(nullptr, handler)), QPromise<int>>::value));
|
||||
@ -331,8 +376,12 @@ void tst_requirements::thenHandlers()
|
||||
{
|
||||
QString reason;
|
||||
auto p1 = QPromise<int>::resolve(42);
|
||||
auto p2 = p1.then([](){ throw QString{"foo"}; });
|
||||
p2.then(nullptr, [&](const QString& e) { reason = e; }).wait();
|
||||
auto p2 = p1.then([]() {
|
||||
throw QString{"foo"};
|
||||
});
|
||||
p2.then(nullptr, [&](const QString& e) {
|
||||
reason = e;
|
||||
}).wait();
|
||||
|
||||
QVERIFY(p1.isFulfilled());
|
||||
QVERIFY(p2.isRejected());
|
||||
@ -341,8 +390,14 @@ void tst_requirements::thenHandlers()
|
||||
{
|
||||
QString reason;
|
||||
auto p1 = QPromise<int>::reject(QString{"foo"});
|
||||
auto p2 = p1.then(nullptr, [](){ throw QString{"bar"}; return 42; });
|
||||
p2.then(nullptr, [&](const QString& e) { reason = e; return 0; }).wait();
|
||||
auto p2 = p1.then(nullptr, []() {
|
||||
throw QString{"bar"};
|
||||
return 42;
|
||||
});
|
||||
p2.then(nullptr, [&](const QString& e) {
|
||||
reason = e;
|
||||
return 0;
|
||||
}).wait();
|
||||
|
||||
QVERIFY(p1.isRejected());
|
||||
QVERIFY(p2.isRejected());
|
||||
@ -354,9 +409,13 @@ void tst_requirements::thenHandlers()
|
||||
{
|
||||
QString value;
|
||||
auto p1 = QPromise<QString>::resolve("42");
|
||||
auto p2 = p1.then(nullptr, [](){ return QString{}; });
|
||||
auto p2 = p1.then(nullptr, []() {
|
||||
return QString{};
|
||||
});
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<QString>>::value));
|
||||
p2.then([&](const QString& e) { value = e; }).wait();
|
||||
p2.then([&](const QString& e) {
|
||||
value = e;
|
||||
}).wait();
|
||||
|
||||
QVERIFY(p1.isFulfilled());
|
||||
QVERIFY(p2.isFulfilled());
|
||||
|
@ -10,13 +10,15 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
struct Logs {
|
||||
struct Logs
|
||||
{
|
||||
int ctor = 0;
|
||||
int copy = 0;
|
||||
int move = 0;
|
||||
int refs = 0;
|
||||
|
||||
void reset() {
|
||||
void reset()
|
||||
{
|
||||
ctor = 0;
|
||||
copy = 0;
|
||||
move = 0;
|
||||
@ -26,16 +28,40 @@ struct Logs {
|
||||
|
||||
struct Logger
|
||||
{
|
||||
Logger() { logs().ctor++; logs().refs++; }
|
||||
Logger(const Logger&) { logs().copy++; logs().refs++; }
|
||||
Logger(Logger&&) { logs().move++; logs().refs++; }
|
||||
Logger()
|
||||
{
|
||||
logs().ctor++;
|
||||
logs().refs++;
|
||||
}
|
||||
Logger(const Logger&)
|
||||
{
|
||||
logs().copy++;
|
||||
logs().refs++;
|
||||
}
|
||||
Logger(Logger&&)
|
||||
{
|
||||
logs().move++;
|
||||
logs().refs++;
|
||||
}
|
||||
~Logger() { logs().refs--; }
|
||||
|
||||
Logger& operator=(const Logger&) { logs().copy++; return *this; }
|
||||
Logger& operator=(Logger&&) { logs().move++; return *this; }
|
||||
Logger& operator=(const Logger&)
|
||||
{
|
||||
logs().copy++;
|
||||
return *this;
|
||||
}
|
||||
Logger& operator=(Logger&&)
|
||||
{
|
||||
logs().move++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
public: // STATICS
|
||||
static Logs& logs() { static Logs logs; return logs; }
|
||||
static Logs& logs()
|
||||
{
|
||||
static Logs logs;
|
||||
return logs;
|
||||
}
|
||||
};
|
||||
|
||||
struct Data : public Logger
|
||||
@ -48,15 +74,9 @@ struct Data : public Logger
|
||||
// constructor and operator also need to be explicitly defined (error C2280).
|
||||
// https://stackoverflow.com/a/26581337
|
||||
|
||||
Data(const Data& other)
|
||||
: Logger{other}
|
||||
, m_value{other.m_value}
|
||||
{ }
|
||||
Data(const Data& other) : Logger{other}, m_value{other.m_value} { }
|
||||
|
||||
Data(Data&& other) : Logger{std::forward<Data>(other)}
|
||||
{
|
||||
std::swap(m_value, other.m_value);
|
||||
}
|
||||
Data(Data&& other) : Logger{std::forward<Data>(other)} { std::swap(m_value, other.m_value); }
|
||||
|
||||
Data& operator=(const Data& other)
|
||||
{
|
||||
|
@ -14,19 +14,24 @@ template <typename T>
|
||||
static inline T waitForValue(const QtPromise::QPromise<T>& promise, const T& initial)
|
||||
{
|
||||
T value(initial);
|
||||
promise.then([&](const T& res) {
|
||||
promise
|
||||
.then([&](const T& res) {
|
||||
value = res;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
return value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static inline T waitForValue(const QtPromise::QPromise<void>& promise, const T& initial, const T& expected)
|
||||
static inline T
|
||||
waitForValue(const QtPromise::QPromise<void>& promise, const T& initial, const T& expected)
|
||||
{
|
||||
T value(initial);
|
||||
promise.then([&]() {
|
||||
promise
|
||||
.then([&]() {
|
||||
value = expected;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -34,10 +39,12 @@ template <typename T, typename E>
|
||||
static inline E waitForError(const QtPromise::QPromise<T>& promise, const E& initial)
|
||||
{
|
||||
E error(initial);
|
||||
promise.fail([&](const E& err) {
|
||||
promise
|
||||
.fail([&](const E& err) {
|
||||
error = err;
|
||||
return T();
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -45,9 +52,11 @@ template <typename E>
|
||||
static inline E waitForError(const QtPromise::QPromise<void>& promise, const E& initial)
|
||||
{
|
||||
E error(initial);
|
||||
promise.fail([&](const E& err) {
|
||||
promise
|
||||
.fail([&](const E& err) {
|
||||
error = err;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -55,9 +64,11 @@ template <typename E, typename T>
|
||||
static inline bool waitForRejected(const T& promise)
|
||||
{
|
||||
bool result = false;
|
||||
promise.tapFail([&](const E&) {
|
||||
promise
|
||||
.tapFail([&](const E&) {
|
||||
result = true;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -40,10 +40,12 @@ void tst_thread::resolve()
|
||||
source = QThread::currentThread();
|
||||
resolve(42);
|
||||
});
|
||||
}}.then([&](int res) {
|
||||
}}
|
||||
.then([&](int res) {
|
||||
target = QThread::currentThread();
|
||||
value = res;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QVERIFY(source != nullptr);
|
||||
QVERIFY(source != target);
|
||||
@ -62,10 +64,12 @@ void tst_thread::resolve_void()
|
||||
source = QThread::currentThread();
|
||||
resolve();
|
||||
});
|
||||
}}.then([&]() {
|
||||
}}
|
||||
.then([&]() {
|
||||
target = QThread::currentThread();
|
||||
value = 43;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QVERIFY(source != nullptr);
|
||||
QVERIFY(source != target);
|
||||
@ -84,11 +88,13 @@ void tst_thread::reject()
|
||||
source = QThread::currentThread();
|
||||
reject(QString{"foo"});
|
||||
});
|
||||
}}.fail([&](const QString& err) {
|
||||
}}
|
||||
.fail([&](const QString& err) {
|
||||
target = QThread::currentThread();
|
||||
error = err;
|
||||
return -1;
|
||||
}).wait();
|
||||
})
|
||||
.wait();
|
||||
|
||||
QVERIFY(source != nullptr);
|
||||
QVERIFY(source != target);
|
||||
@ -106,12 +112,15 @@ void tst_thread::then()
|
||||
|
||||
int value = -1;
|
||||
QThread* target = nullptr;
|
||||
QtPromise::resolve(QtConcurrent::run([&](const QPromise<int>& p) {
|
||||
QtPromise::resolve(QtConcurrent::run(
|
||||
[&](const QPromise<int>& p) {
|
||||
p.then([&](int res) {
|
||||
target = QThread::currentThread();
|
||||
value = res;
|
||||
}).wait();
|
||||
}, p)).wait();
|
||||
},
|
||||
p))
|
||||
.wait();
|
||||
|
||||
QVERIFY(target != nullptr);
|
||||
QVERIFY(source != target);
|
||||
@ -129,12 +138,15 @@ void tst_thread::then_void()
|
||||
|
||||
int value = -1;
|
||||
QThread* target = nullptr;
|
||||
QtPromise::resolve(QtConcurrent::run([&](const QPromise<void>& p) {
|
||||
QtPromise::resolve(QtConcurrent::run(
|
||||
[&](const QPromise<void>& p) {
|
||||
p.then([&]() {
|
||||
target = QThread::currentThread();
|
||||
value = 43;
|
||||
}).wait();
|
||||
}, p)).wait();
|
||||
},
|
||||
p))
|
||||
.wait();
|
||||
|
||||
QVERIFY(target != nullptr);
|
||||
QVERIFY(source != target);
|
||||
@ -152,13 +164,16 @@ void tst_thread::fail()
|
||||
|
||||
QString error;
|
||||
QThread* target = nullptr;
|
||||
QtPromise::resolve(QtConcurrent::run([&](const QPromise<int>& p) {
|
||||
QtPromise::resolve(QtConcurrent::run(
|
||||
[&](const QPromise<int>& p) {
|
||||
p.fail([&](const QString& err) {
|
||||
target = QThread::currentThread();
|
||||
error = err;
|
||||
return -1;
|
||||
}).wait();
|
||||
}, p)).wait();
|
||||
},
|
||||
p))
|
||||
.wait();
|
||||
|
||||
QVERIFY(target != nullptr);
|
||||
QVERIFY(source != target);
|
||||
@ -176,12 +191,15 @@ void tst_thread::finally()
|
||||
|
||||
int value = -1;
|
||||
QThread* target = nullptr;
|
||||
QtPromise::resolve(QtConcurrent::run([&](const QPromise<int>& p) {
|
||||
QtPromise::resolve(QtConcurrent::run(
|
||||
[&](const QPromise<int>& p) {
|
||||
p.finally([&]() {
|
||||
target = QThread::currentThread();
|
||||
value = 43;
|
||||
}).wait();
|
||||
}, p)).wait();
|
||||
},
|
||||
p))
|
||||
.wait();
|
||||
|
||||
QVERIFY(target != nullptr);
|
||||
QVERIFY(source != target);
|
||||
|
Loading…
Reference in New Issue
Block a user