callback test
This commit is contained in:
parent
3b287974ab
commit
7443e5c1c9
@ -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
|
||||
|
@ -58,5 +58,6 @@ HEADERS += \
|
||||
plugin/shader_plugin.h \
|
||||
registry.h \
|
||||
shader/layer.h \
|
||||
template/custom_callback.h \
|
||||
template/singleton.h \
|
||||
utility/utility.h
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include "context_manager.h"
|
||||
#include "async/async_result.h"
|
||||
#include "template/custom_callback.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
@ -31,10 +32,15 @@ int main(int argc, char *argv[])
|
||||
delete source;
|
||||
}
|
||||
|
||||
QJsonValue *a = new QJsonValue();
|
||||
AsyncResult<QJsonValue> *result = new AsyncResult<QJsonValue>(*a);
|
||||
qDebug() << result->hasResult();
|
||||
qDebug() << result->getResult();
|
||||
{
|
||||
// code for test
|
||||
QJsonValue *a = new QJsonValue();
|
||||
AsyncResult<QJsonValue> *result = new AsyncResult<QJsonValue>(*a);
|
||||
CustomCallback<QJsonValue> *callback = new CustomCallback<QJsonValue>();
|
||||
result->setCallback(callback);
|
||||
qDebug() << result->hasResult();
|
||||
qDebug() << result->getResult();
|
||||
}
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
23
doric/template/custom_callback.h
Normal file
23
doric/template/custom_callback.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef CUSTOM_CALLBACK_H
|
||||
#define CUSTOM_CALLBACK_H
|
||||
|
||||
#include "async/callback.h"
|
||||
|
||||
template <class R>
|
||||
class CustomCallback : public Callback<R> {
|
||||
|
||||
public:
|
||||
void onResult(R result) override {
|
||||
|
||||
}
|
||||
|
||||
void onError(QException exception) override {
|
||||
|
||||
}
|
||||
|
||||
void onFinish() override {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
#endif // CUSTOM_CALLBACK_H
|
Reference in New Issue
Block a user