mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-05 16:55:23 +08:00
Move helpers in separate file and add unit tests
Gather helpers in `promisehelpers.h` and add a global include file to make easier integration with the lib (`#include <QtPromise>`).
This commit is contained in:
@ -104,6 +104,5 @@ private:
|
||||
} // namespace QtPromise
|
||||
|
||||
#include "qpromise.inl"
|
||||
#include "qpromise_qfuture.inl"
|
||||
|
||||
#endif // ifndef _QTPROMISE_QPROMISE_H
|
||||
|
@ -311,37 +311,4 @@ inline void QPromise<void>::notify(const typename QtPromisePrivate::PromiseData<
|
||||
});
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
template <typename T>
|
||||
typename QtPromisePrivate::PromiseDeduce<T>::Type qPromise(const T& value)
|
||||
{
|
||||
using namespace QtPromisePrivate;
|
||||
using Promise = typename PromiseDeduce<T>::Type;
|
||||
return Promise([=](
|
||||
const QPromiseResolve<typename Promise::Type>& resolve,
|
||||
const QPromiseReject<typename Promise::Type>& reject) {
|
||||
PromiseFulfill<T>::call(value, resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
QPromise<void> qPromise()
|
||||
{
|
||||
return QPromise<void>([](
|
||||
const QPromiseResolve<void>& resolve) {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QPromise<QVector<T> > qPromiseAll(const QVector<QPromise<T> >& promises)
|
||||
{
|
||||
return QPromise<T>::all(promises);
|
||||
}
|
||||
|
||||
QPromise<void> qPromiseAll(const QVector<QPromise<void> >& promises)
|
||||
{
|
||||
return QPromise<void>::all(promises);
|
||||
}
|
||||
|
||||
} // namespace QtPromise
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include "qpromiseglobal.h"
|
||||
|
||||
// Qt
|
||||
#include <QCoreApplication>
|
||||
#include <QTimer>
|
||||
#include <QSharedData>
|
||||
#include <QVector>
|
||||
|
@ -1,3 +1,6 @@
|
||||
#ifndef _QTPROMISE_QPROMISEFUTURE_P_H
|
||||
#define _QTPROMISE_QPROMISEFUTURE_P_H
|
||||
|
||||
#include <QFutureWatcher>
|
||||
#include <QFuture>
|
||||
|
||||
@ -62,3 +65,5 @@ struct PromiseFulfill<QFuture<void> >
|
||||
};
|
||||
|
||||
} // namespace QtPromise
|
||||
|
||||
#endif // _QTPROMISE_QPROMISEFUTURE_P_H
|
42
src/qtpromise/qpromisehelpers.h
Normal file
42
src/qtpromise/qpromisehelpers.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef _QTPROMISE_QPROMISEHELPERS_H
|
||||
#define _QTPROMISE_QPROMISEHELPERS_H
|
||||
|
||||
// QtPromise
|
||||
#include "qpromise_p.h"
|
||||
|
||||
namespace QtPromise {
|
||||
|
||||
template <typename T>
|
||||
typename QtPromisePrivate::PromiseDeduce<T>::Type qPromise(const T& value)
|
||||
{
|
||||
using namespace QtPromisePrivate;
|
||||
using Promise = typename PromiseDeduce<T>::Type;
|
||||
return Promise([=](
|
||||
const QPromiseResolve<typename Promise::Type>& resolve,
|
||||
const QPromiseReject<typename Promise::Type>& reject) {
|
||||
PromiseFulfill<T>::call(value, resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
QPromise<void> qPromise()
|
||||
{
|
||||
return QPromise<void>([](
|
||||
const QPromiseResolve<void>& resolve) {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QPromise<QVector<T> > qPromiseAll(const QVector<QPromise<T> >& promises)
|
||||
{
|
||||
return QPromise<T>::all(promises);
|
||||
}
|
||||
|
||||
QPromise<void> qPromiseAll(const QVector<QPromise<void> >& promises)
|
||||
{
|
||||
return QPromise<void>::all(promises);
|
||||
}
|
||||
|
||||
} // namespace QtPromise
|
||||
|
||||
#endif // _QTPROMISE_QPROMISEHELPERS_H
|
@ -2,6 +2,7 @@ HEADERS += \
|
||||
$$PWD/qpromise.h \
|
||||
$$PWD/qpromise.inl \
|
||||
$$PWD/qpromise_p.h \
|
||||
$$PWD/qpromise_qfuture.inl \
|
||||
$$PWD/qpromiseerror.h \
|
||||
$$PWD/qpromiseglobal.h
|
||||
$$PWD/qpromisefuture.h \
|
||||
$$PWD/qpromiseglobal.h \
|
||||
$$PWD/qpromisehelpers.h
|
||||
|
Reference in New Issue
Block a user