callback test

This commit is contained in:
王劲鹏
2019-12-19 20:24:44 +08:00
parent 3b287974ab
commit 7443e5c1c9
4 changed files with 47 additions and 6 deletions

View File

@@ -11,7 +11,7 @@ class AsyncResult {
private:
QVariant result;
Callback<R> *callback;
Callback<R> *callback = nullptr;
public:
AsyncResult() {}
@@ -28,7 +28,7 @@ public:
}
}
void setError(QException *exception) {
void setError(QException exception) {
this->result->setValue(exception);
if (callback != nullptr) {
this->callback->onError(exception);
@@ -44,6 +44,17 @@ public:
R *getResult() {
return static_cast<R*>(result.data());
}
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();
}
}
};
#endif // ASYNC_RESULT_H