mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 02:35:38 +08:00
clang-format src
This commit is contained in:
@@ -31,10 +31,13 @@ namespace BlackMisc
|
||||
{
|
||||
public:
|
||||
CPromiseData() { this->reportStarted(); }
|
||||
~CPromiseData() { if (this->isRunning()) { this->cancel(); } }
|
||||
~CPromiseData()
|
||||
{
|
||||
if (this->isRunning()) { this->cancel(); }
|
||||
}
|
||||
|
||||
CPromiseData(const CPromiseData &) = delete;
|
||||
CPromiseData &operator =(const CPromiseData &) = delete;
|
||||
CPromiseData &operator=(const CPromiseData &) = delete;
|
||||
};
|
||||
|
||||
//! \private
|
||||
@@ -43,8 +46,7 @@ namespace BlackMisc
|
||||
{
|
||||
QSharedPointer<QFutureWatcher<T>> watcher(new QFutureWatcher<T>, &QObject::deleteLater);
|
||||
if (!context) { context = watcher.data(); }
|
||||
QObject::connect(watcher.data(), &QFutureWatcher<T>::finished, context, [watcher, func = std::forward<F>(func)]() mutable
|
||||
{
|
||||
QObject::connect(watcher.data(), &QFutureWatcher<T>::finished, context, [watcher, func = std::forward<F>(func)]() mutable {
|
||||
if (!watcher->isCanceled()) { func(watcher->future()); }
|
||||
watcher.reset();
|
||||
});
|
||||
@@ -68,7 +70,7 @@ namespace BlackMisc
|
||||
template <typename F>
|
||||
void doAfter(QFuture<void> future, QObject *context, F &&func)
|
||||
{
|
||||
Private::doAfter(future, context, [func = std::forward<F>(func)](auto&&) { func(); });
|
||||
Private::doAfter(future, context, [func = std::forward<F>(func)](auto &&) { func(); });
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -82,23 +84,37 @@ namespace BlackMisc
|
||||
QFuture<T> future() { return m_data->future(); }
|
||||
|
||||
//! Mark the result as cancelled.
|
||||
void abandon() { m_data->cancel(); m_data->reportFinished(); }
|
||||
void abandon()
|
||||
{
|
||||
m_data->cancel();
|
||||
m_data->reportFinished();
|
||||
}
|
||||
|
||||
//! Set the result value that will be made available through the future.
|
||||
void setResult(const T &value) { m_data->reportFinished(&value); }
|
||||
|
||||
//! Set the result value from the given future. Will block if future is not ready.
|
||||
template <typename U>
|
||||
void setResult(QFuture<U> future) { future.waitForFinished(); future.isCanceled() ? abandon() : setResult(future.result()); }
|
||||
void setResult(QFuture<U> future)
|
||||
{
|
||||
future.waitForFinished();
|
||||
future.isCanceled() ? abandon() : setResult(future.result());
|
||||
}
|
||||
|
||||
//! When the given future is ready, use its result to set the result of this promise.
|
||||
template <typename U>
|
||||
void chainResult(QFuture<U> future) { doAfter(future, nullptr, [*this](auto &&f) mutable { setResult(f); }); }
|
||||
void chainResult(QFuture<U> future)
|
||||
{
|
||||
doAfter(future, nullptr, [*this](auto &&f) mutable { setResult(f); });
|
||||
}
|
||||
|
||||
//! Invoke a functor and use its return value to set the result.
|
||||
//! \details Useful for uniform syntax in generic code where T could be void.
|
||||
template <typename F>
|
||||
void setResultFrom(F &&func) { setResult(std::forward<F>(func)()); }
|
||||
void setResultFrom(F &&func)
|
||||
{
|
||||
setResult(std::forward<F>(func)());
|
||||
}
|
||||
|
||||
private:
|
||||
QSharedPointer<Private::CPromiseData<T>> m_data { new Private::CPromiseData<T> };
|
||||
@@ -115,23 +131,38 @@ namespace BlackMisc
|
||||
QFuture<void> future() { return m_data->future(); }
|
||||
|
||||
//! Mark the task as cancelled.
|
||||
void abandon() { m_data->cancel(); m_data->reportFinished(); }
|
||||
void abandon()
|
||||
{
|
||||
m_data->cancel();
|
||||
m_data->reportFinished();
|
||||
}
|
||||
|
||||
//! Mark the task as complete.
|
||||
void setResult() { m_data->reportFinished(); }
|
||||
|
||||
//! Wait for the given future, then mark the task as complete.
|
||||
template <typename U>
|
||||
void setResult(QFuture<U> future) { future.waitForFinished(); future.isCanceled() ? abandon() : setResult(); }
|
||||
void setResult(QFuture<U> future)
|
||||
{
|
||||
future.waitForFinished();
|
||||
future.isCanceled() ? abandon() : setResult();
|
||||
}
|
||||
|
||||
//! When the given future is ready, mark this promise as complete.
|
||||
template <typename U>
|
||||
void chainResult(QFuture<U> future) { doAfter(future, nullptr, [this](auto &&f) { setResult(f); }); }
|
||||
void chainResult(QFuture<U> future)
|
||||
{
|
||||
doAfter(future, nullptr, [this](auto &&f) { setResult(f); });
|
||||
}
|
||||
|
||||
//! Invoke a functor and mark the task as complete.
|
||||
//! \details Useful for uniform syntax in generic code where T could be void.
|
||||
template <typename F>
|
||||
void setResultFrom(F &&func) { std::forward<F>(func)(); setResult(); }
|
||||
void setResultFrom(F &&func)
|
||||
{
|
||||
std::forward<F>(func)();
|
||||
setResult();
|
||||
}
|
||||
|
||||
private:
|
||||
QSharedPointer<Private::CPromiseData<void>> m_data { new Private::CPromiseData<void> };
|
||||
|
||||
Reference in New Issue
Block a user