Move resolver related classes in a separate file.

This commit is contained in:
Simon Brunel 2018-05-21 09:25:14 +02:00
parent 54d88f16a3
commit 4cfe2e54f4
5 changed files with 129 additions and 104 deletions

View File

@ -5,6 +5,7 @@
#include "qpromise_p.h"
#include "qpromiseerror.h"
#include "qpromiseglobal.h"
#include "qpromiseresolver.h"
// Qt
#include <QExplicitlySharedDataPointer>

View File

@ -1,3 +1,5 @@
#include "qpromise.h"
// Qt
#include <QCoreApplication>
#include <QSharedPointer>
@ -5,47 +7,6 @@
namespace QtPromise {
template <class T>
class QPromiseResolve
{
public:
QPromiseResolve(QtPromisePrivate::PromiseResolver<T> resolver)
: m_resolver(std::move(resolver))
{ }
template <typename V>
void operator()(V&& value) const
{
m_resolver.resolve(std::forward<V>(value));
}
void operator()() const
{
m_resolver.resolve();
}
private:
mutable QtPromisePrivate::PromiseResolver<T> m_resolver;
};
template <class T>
class QPromiseReject
{
public:
QPromiseReject(QtPromisePrivate::PromiseResolver<T> resolver)
: m_resolver(std::move(resolver))
{ }
template <typename E>
void operator()(E&& error) const
{
m_resolver.reject(std::forward<E>(error));
}
private:
mutable QtPromisePrivate::PromiseResolver<T> m_resolver;
};
template <typename T>
template <typename F, typename std::enable_if<QtPromisePrivate::ArgsOf<F>::count == 1, int>::type>
inline QPromiseBase<T>::QPromiseBase(F callback)

View File

@ -562,68 +562,6 @@ protected:
}
};
template <typename T>
class PromiseResolver
{
public:
PromiseResolver(QtPromise::QPromise<T> promise)
: m_d(new Data())
{
m_d->promise = new QtPromise::QPromise<T>(std::move(promise));
}
template <typename E>
void reject(E&& error)
{
auto promise = m_d->promise;
if (promise) {
Q_ASSERT(promise->isPending());
promise->m_d->reject(std::forward<E>(error));
promise->m_d->dispatch();
release();
}
}
template <typename V>
void resolve(V&& value)
{
auto promise = m_d->promise;
if (promise) {
Q_ASSERT(promise->isPending());
promise->m_d->resolve(std::forward<V>(value));
promise->m_d->dispatch();
release();
}
}
void resolve()
{
auto promise = m_d->promise;
if (promise) {
Q_ASSERT(promise->isPending());
promise->m_d->resolve();
promise->m_d->dispatch();
release();
}
}
private:
struct Data : public QSharedData
{
QtPromise::QPromise<T>* promise = nullptr;
};
QExplicitlySharedDataPointer<Data> m_d;
void release()
{
Q_ASSERT(m_d->promise);
Q_ASSERT(!m_d->promise->isPending());
delete m_d->promise;
m_d->promise = nullptr;
}
};
} // namespace QtPromise
#endif // ifndef QTPROMISE_QPROMISE_H

View File

@ -0,0 +1,124 @@
#ifndef QTPROMISE_QPROMISERESOLVER_H
#define QTPROMISE_QPROMISERESOLVER_H
// Qt
#include <QExplicitlySharedDataPointer>
namespace QtPromise {
template <typename T> class QPromise;
} // namespace QtPromise
namespace QtPromisePrivate {
template <typename T>
class PromiseResolver
{
public:
PromiseResolver(QtPromise::QPromise<T> promise)
: m_d(new Data())
{
m_d->promise = new QtPromise::QPromise<T>(std::move(promise));
}
template <typename E>
void reject(E&& error)
{
auto promise = m_d->promise;
if (promise) {
Q_ASSERT(promise->isPending());
promise->m_d->reject(std::forward<E>(error));
promise->m_d->dispatch();
release();
}
}
template <typename V>
void resolve(V&& value)
{
auto promise = m_d->promise;
if (promise) {
Q_ASSERT(promise->isPending());
promise->m_d->resolve(std::forward<V>(value));
promise->m_d->dispatch();
release();
}
}
void resolve()
{
auto promise = m_d->promise;
if (promise) {
Q_ASSERT(promise->isPending());
promise->m_d->resolve();
promise->m_d->dispatch();
release();
}
}
private:
struct Data : public QSharedData
{
QtPromise::QPromise<T>* promise = nullptr;
};
QExplicitlySharedDataPointer<Data> m_d;
void release()
{
Q_ASSERT(m_d->promise);
Q_ASSERT(!m_d->promise->isPending());
delete m_d->promise;
m_d->promise = nullptr;
}
};
} // QtPromisePrivate
namespace QtPromise {
template <class T>
class QPromiseResolve
{
public:
QPromiseResolve(QtPromisePrivate::PromiseResolver<T> resolver)
: m_resolver(std::move(resolver))
{ }
template <typename V>
void operator()(V&& value) const
{
m_resolver.resolve(std::forward<V>(value));
}
void operator()() const
{
m_resolver.resolve();
}
private:
mutable QtPromisePrivate::PromiseResolver<T> m_resolver;
};
template <class T>
class QPromiseReject
{
public:
QPromiseReject(QtPromisePrivate::PromiseResolver<T> resolver)
: m_resolver(std::move(resolver))
{ }
template <typename E>
void operator()(E&& error) const
{
m_resolver.reject(std::forward<E>(error));
}
private:
mutable QtPromisePrivate::PromiseResolver<T> m_resolver;
};
} // namespace QtPromise
#endif // QTPROMISE_QPROMISERESOLVER_H

View File

@ -5,4 +5,5 @@ HEADERS += \
$$PWD/qpromiseerror.h \
$$PWD/qpromisefuture.h \
$$PWD/qpromiseglobal.h \
$$PWD/qpromisehelpers.h
$$PWD/qpromisehelpers.h \
$$PWD/qpromiseresolver.h