diff --git a/doric/async/async_result.h b/doric/async/async_result.h index ffe14871..2b12b77b 100644 --- a/doric/async/async_result.h +++ b/doric/async/async_result.h @@ -11,7 +11,7 @@ class AsyncResult { private: QVariant result; - Callback *callback; + Callback *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(result.data()); } + + void setCallback(Callback *callback) { + this->callback = callback; + if (QException *exception = static_cast(result.data())) { + this->callback->onError(*exception); + this->callback->onFinish(); + } else if (hasResult()) { + this->callback->onResult(*getResult()); + this->callback->onFinish(); + } + } }; #endif // ASYNC_RESULT_H diff --git a/doric/doric.pro b/doric/doric.pro index 8f61c351..1d33eaf7 100644 --- a/doric/doric.pro +++ b/doric/doric.pro @@ -58,5 +58,6 @@ HEADERS += \ plugin/shader_plugin.h \ registry.h \ shader/layer.h \ + template/custom_callback.h \ template/singleton.h \ utility/utility.h diff --git a/doric/main.cpp b/doric/main.cpp index 994626b7..e633bd07 100644 --- a/doric/main.cpp +++ b/doric/main.cpp @@ -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 *result = new AsyncResult(*a); - qDebug() << result->hasResult(); - qDebug() << result->getResult(); + { + // code for test + QJsonValue *a = new QJsonValue(); + AsyncResult *result = new AsyncResult(*a); + CustomCallback *callback = new CustomCallback(); + result->setCallback(callback); + qDebug() << result->hasResult(); + qDebug() << result->getResult(); + } return app.exec(); } diff --git a/doric/template/custom_callback.h b/doric/template/custom_callback.h new file mode 100644 index 00000000..d0c20667 --- /dev/null +++ b/doric/template/custom_callback.h @@ -0,0 +1,23 @@ +#ifndef CUSTOM_CALLBACK_H +#define CUSTOM_CALLBACK_H + +#include "async/callback.h" + +template +class CustomCallback : public Callback { + +public: + void onResult(R result) override { + + } + + void onError(QException exception) override { + + } + + void onFinish() override { + + } +}; + +#endif // CUSTOM_CALLBACK_H