add async result
This commit is contained in:
49
doric/async/async_result.h
Normal file
49
doric/async/async_result.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#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;
|
||||
|
||||
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());
|
||||
}
|
||||
};
|
||||
|
||||
#endif // ASYNC_RESULT_H
|
18
doric/async/callback.h
Normal file
18
doric/async/callback.h
Normal 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
|
Reference in New Issue
Block a user