add async result

This commit is contained in:
王劲鹏 2019-12-19 19:54:58 +08:00
parent 001eb4a963
commit 3b287974ab
8 changed files with 100 additions and 54 deletions

View 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
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

View File

@ -42,6 +42,8 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
async/async_result.h \
async/callback.h \
constant.h \
context.h \
context_holder.h \
@ -56,7 +58,5 @@ HEADERS += \
plugin/shader_plugin.h \
registry.h \
shader/layer.h \
shader/super_node.h \
shader/view_node.h \
template/singleton.h \
utility/utility.h

View File

@ -4,6 +4,7 @@
#include <QResource>
#include "context_manager.h"
#include "async/async_result.h"
int main(int argc, char *argv[])
{
@ -30,5 +31,10 @@ 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();
return app.exec();
}

View File

@ -1,7 +1,6 @@
#include "shader_plugin.h"
#include "shader/view_node.h"
Q_INVOKABLE void ShaderPlugin::render(QJSValue jsValue) {
QString viewId = jsValue.property("id").toString();
ViewNode<QObject> *viewNode = new ViewNode<QObject>(nullptr);
qDebug() << viewId;
}

View File

@ -1,10 +1,33 @@
#ifndef LAYER_H
#define LAYER_H
#include "QWidget"
#include <QPainter>
#include <QWidget>
class Layer : public QWidget {
void paintEvent(QPaintEvent *event) override {
QPainter painter(this);
QWidget::paintEvent(event);
}
public:
void setShadow(int sdColor, int sdOpacity, int sdRadius, int offsetX, int offsetY) {
}
void setBorder(int borderWidth, int borderColor) {
}
void setCornerRadius(int corner) {
}
void setCornerRadius(int leftTop, int rightTop, int rightBottom, int leftBottom) {
}
};
#endif // LAYER_H

View File

@ -1,25 +0,0 @@
#ifndef SUPER_NODE_H
#define SUPER_NODE_H
#include <QJSValue>
#include <QMap>
#include <QtPlugin>
#include "view_node.h"
template <typename V>
class SuperNode : public ViewNode<V> {
private:
QMap<QString, QJSValue> subNodes;
protected:
bool reusable = false;
public:
virtual ~SuperNode() = default;
virtual ViewNode<V> *getSubNodeById(QString *id) = 0;
};
#endif // SUPER_NODE_H

View File

@ -1,24 +0,0 @@
#ifndef VIEW_NODE_H
#define VIEW_NODE_H
#include <QDebug>
#include "context_holder.h"
class SuperNode;
template <class T>
class ViewNode : public ContextHolder {
protected:
T view;
private:
SuperNode *superNode;
QString *id;
public:
ViewNode(Context *context) : ContextHolder(context) {}
};
#endif // VIEW_NODE_H