rename dir

This commit is contained in:
pengfei.zhou
2019-12-21 21:55:53 +08:00
parent 15d3a1cbf7
commit f21732f551
35 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
#ifndef ASYNC_RESULT_H
#define ASYNC_RESULT_H
#include <QDebug>
#include <QVariant>
#include "callback.h"
template <class R>
class AsyncResult {
private:
QVariant result;
Callback<R> *callback = nullptr;
public:
AsyncResult() {}
AsyncResult(R result) {
this->result.setValue(result);
}
void setResult(R result) {
this->result.setValue(result);
if (callback != nullptr) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
void setError(QException exception) {
this->result->setValue(exception);
if (callback != nullptr) {
this->callback->onError(exception);
this->callback->onFinish();
}
}
bool hasResult() {
qDebug() << result.typeName();
return !QString(result.typeName()).isEmpty();
}
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

View File

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