This commit is contained in:
王劲鹏
2021-01-28 17:06:40 +08:00
committed by osborn
parent 0be7a9679a
commit 93b7cc2457
53 changed files with 1020 additions and 774 deletions

View File

@@ -0,0 +1,15 @@
#ifndef ASYNC_CALL_H
#define ASYNC_CALL_H
#include <QThreadPool>
class AsyncCall {
public:
static void ensureRunInThreadPool(QThreadPool &threadPool, QRunnable *runnable)
{
}
};
#endif // ASYNC_CALL_H

View File

@@ -0,0 +1,56 @@
#include "async_result.h"
AsyncResult::AsyncResult()
{
}
AsyncResult::AsyncResult(QJSValue result)
{
this->result = result;
}
void AsyncResult::setResult(QJSValue result)
{
this->result = result;
if (this->callback != NULL) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
void AsyncResult::setError(QJSValue exception)
{
this->result = exception;
if (this->callback != NULL) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
bool AsyncResult::hasResult()
{
return !result.equals(EMPTY);
}
QJSValue AsyncResult::getResult()
{
return this->result;
}
void AsyncResult::setCallback(Callback *callback)
{
this->callback = callback;
if (this->result.isError()) {
this->callback->onError(result);
this->callback->onFinish();
} else if (!result.equals(EMPTY)) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
SettableFuture* AsyncResult::synchronous()
{
return NULL;
}

View File

@@ -1,60 +1,35 @@
#ifndef ASYNC_RESULT_H
#define ASYNC_RESULT_H
#ifndef ASYNCRESULT_H
#define ASYNCRESULT_H
#include <QDebug>
#include <QVariant>
#include <QJSValue>
#include "callback.h"
#include "settable_future.h"
template <class R>
class AsyncResult {
static QJSValue EMPTY(QJSValue::NullValue);
class AsyncResult
{
private:
QVariant result;
Callback<R> *callback = nullptr;
QJSValue result = EMPTY;
Callback *callback;
public:
AsyncResult() {}
AsyncResult();
AsyncResult(R result) {
this->result.setValue(result);
}
AsyncResult(QJSValue result);
void setResult(R result) {
this->result.setValue(result);
if (callback != nullptr) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
void setResult(QJSValue result);
void setError(QException exception) {
this->result->setValue(exception);
if (callback != nullptr) {
this->callback->onError(exception);
this->callback->onFinish();
}
}
void setError(QJSValue exception);
bool hasResult() {
qDebug() << result.typeName();
return !QString(result.typeName()).isEmpty();
}
bool hasResult();
R *getResult() {
return static_cast<R*>(result.data());
}
QJSValue getResult();
void setCallback(Callback<R> *callback) {
this->callback = callback;
if (QException *exception = static_cast<QException*>(result.data())) {
this->callback->onError(*exception);
this->callback->onFinish();
} else if (hasResult()) {
this->callback->onResult(*getResult());
this->callback->onFinish();
}
}
void setCallback(Callback *callback);
SettableFuture *synchronous();
};
#endif // ASYNC_RESULT_H
#endif // ASYNCRESULT_H

View File

@@ -1,16 +1,13 @@
#ifndef CALLBACK_H
#define CALLBACK_H
#include <QException>
#include <QJSValue>
template <class R>
class Callback {
public:
virtual void onResult(QJSValue result) = 0;
virtual void onResult(R result) = 0;
virtual void onError(QException exception) = 0;
virtual void onError(QJSValue error) = 0;
virtual void onFinish() = 0;
};

View File

@@ -0,0 +1,20 @@
#include <QDebug>
#include "settable_future.h"
void SettableFuture::set(QJSValue result)
{
if (mReadyLatch == NULL)
{
qDebug() << "Result has already been set!";
return;
}
mResult = result;
delete mReadyLatch;
}
QJSValue SettableFuture::get()
{
mReadyLatch->lock();
return mResult;
}

View File

@@ -0,0 +1,18 @@
#ifndef SETTABLE_FUTURE_H
#define SETTABLE_FUTURE_H
#include <QJSValue>
#include "utils/count_down_latch.h"
class SettableFuture {
private:
QJSValue mResult;
CountDownLatch *mReadyLatch = new CountDownLatch();
public:
void set(QJSValue result);
QJSValue get();
};
#endif // SETTABLE_FUTURE_H