From ed2a1326fc6735c7aab301b083c70f7f426d3d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Sat, 14 Dec 2019 10:04:17 +0800 Subject: [PATCH] h & cpp split --- doric/context.cpp | 39 ++++++++++++++++ doric/context.h | 37 ++------------- doric/doric.pro | 8 +++- doric/driver/native_driver.cpp | 4 ++ doric/driver/native_driver.h | 4 +- doric/engine/js_engine.cpp | 80 +++++++++++++++++++++++++++++++++ doric/engine/js_engine.h | 82 +++------------------------------- doric/main.cpp | 1 + doric/native/native_bridge.h | 1 - doric/native/native_empty.cpp | 7 +++ doric/native/native_empty.h | 5 +-- doric/native/native_log.cpp | 13 ++++++ doric/native/native_log.h | 11 +---- doric/native/native_timer.cpp | 30 +++++++++++++ doric/native/native_timer.h | 28 +----------- doric/registry.cpp | 11 +++++ doric/registry.h | 8 +--- 17 files changed, 210 insertions(+), 159 deletions(-) create mode 100644 doric/context.cpp create mode 100644 doric/engine/js_engine.cpp create mode 100644 doric/native/native_empty.cpp create mode 100644 doric/native/native_log.cpp create mode 100644 doric/native/native_timer.cpp create mode 100644 doric/registry.cpp diff --git a/doric/context.cpp b/doric/context.cpp new file mode 100644 index 00000000..c12b3788 --- /dev/null +++ b/doric/context.cpp @@ -0,0 +1,39 @@ +#include +#include + +#include "constant.h" +#include "context.h" +#include "driver/native_driver.h" + +Context::Context(int contextId, QString *source) { + this->driver = NativeDriver::getInstance(); + + this->contextId = contextId; + this->source = source; +} + +void Context::show() { + QString *method = new QString(Constant::DORIC_ENTITY_SHOW); + QVector *arguments = new QVector(); + + driver->invokeContextEntityMethod(contextId, method, nullptr); + + delete arguments; + delete method; +} + +void Context::init(double width, double height) { + QJsonObject *jsonObject = new QJsonObject(); + jsonObject->insert("width", width); + jsonObject->insert("height", height); + + QString *method = new QString(Constant::DORIC_ENTITY_INIT); + QVariant *variant = new QVariant(); + variant->setValue(*jsonObject); + + driver->invokeContextEntityMethod(contextId, method, variant, nullptr); + + delete variant; + delete method; + delete jsonObject; +} diff --git a/doric/context.h b/doric/context.h index 941d10de..727b248a 100644 --- a/doric/context.h +++ b/doric/context.h @@ -2,12 +2,8 @@ #define CONTEXT_H #include -#include -#include -#include "constant.h" #include "driver/driver.h" -#include "driver/native_driver.h" class Context { @@ -17,38 +13,13 @@ private: QString *source; public: - Driver *driver = NativeDriver::getInstance(); + Driver *driver; - Context(int contextId, QString *source) { - this->contextId = contextId; - this->source = source; - } + Context(int contextId, QString *source); - void show() { - QString *method = new QString(Constant::DORIC_ENTITY_SHOW); - QVector *arguments = new QVector(); + void show(); - driver->invokeContextEntityMethod(contextId, method, nullptr); - - delete arguments; - delete method; - } - - void init(double width, double height) { - QJsonObject *jsonObject = new QJsonObject(); - jsonObject->insert("width", width); - jsonObject->insert("height", height); - - QString *method = new QString(Constant::DORIC_ENTITY_INIT); - QVariant *variant = new QVariant(); - variant->setValue(*jsonObject); - - driver->invokeContextEntityMethod(contextId, method, variant, nullptr); - - delete variant; - delete method; - delete jsonObject; - } + void init(double width, double height); }; #endif // CONTEXT_H diff --git a/doric/doric.pro b/doric/doric.pro index 379ad3b3..7840e88f 100644 --- a/doric/doric.pro +++ b/doric/doric.pro @@ -15,9 +15,15 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ constant.cpp \ + context.cpp \ driver/native_driver.cpp \ + engine/js_engine.cpp \ main.cpp \ - native/native_bridge.cpp + native/native_bridge.cpp \ + native/native_empty.cpp \ + native/native_log.cpp \ + native/native_timer.cpp \ + registry.cpp RESOURCES += qml.qrc diff --git a/doric/driver/native_driver.cpp b/doric/driver/native_driver.cpp index 6216f24a..5a524228 100644 --- a/doric/driver/native_driver.cpp +++ b/doric/driver/native_driver.cpp @@ -3,6 +3,10 @@ #include "native_driver.h" #include "utility/utility.h" +NativeDriver::NativeDriver() { + qDebug() << "NativeDriver constructor"; +} + NativeDriver::~NativeDriver() { qDebug() << "NativeDriver destructor"; } diff --git a/doric/driver/native_driver.h b/doric/driver/native_driver.h index 2fc17667..9535f690 100644 --- a/doric/driver/native_driver.h +++ b/doric/driver/native_driver.h @@ -11,9 +11,7 @@ class NativeDriver : public Driver { private: static NativeDriver *local_instance; - NativeDriver() { - qDebug() << "NativeDriver constructor"; - } + NativeDriver(); ~NativeDriver() override; diff --git a/doric/engine/js_engine.cpp b/doric/engine/js_engine.cpp new file mode 100644 index 00000000..97dd6d1e --- /dev/null +++ b/doric/engine/js_engine.cpp @@ -0,0 +1,80 @@ +#include +#include + +#include "constant.h" +#include "js_engine.h" + +JSEngine::JSEngine() { + initJSEngine(); + injectGlobal(); + initDoricRuntime(); +} + +void JSEngine::prepareContext(int contextId, QString *script) { + QString contextIdString = QString::number(contextId); + QString source = QString(Constant::TEMPLATE_CONTEXT_CREATE) + .replace("%s1", *script) + .replace("%s2", contextIdString) + .replace("%s3", contextIdString) + .replace("%s4", contextIdString); + QJSValue result = engine->evaluate(source, "context://" + contextIdString); + qDebug() << "context://" + contextIdString + " result: " + result.toString(); +} + +void JSEngine::destroyContext(int contextId) { + QString contextIdString = QString::number(contextId); + QString source = QString(Constant::TEMPLATE_CONTEXT_DESTROY) + .replace("%s", contextIdString); + QJSValue result = engine->evaluate(source, "_context://" + contextIdString); + qDebug() << "context://" + contextIdString + " result: " + result.toString(); +} + +void JSEngine::initJSEngine() { + engine->installExtensions(QJSEngine::AllExtensions); +} + +void JSEngine::injectGlobal() { + QJSValue log = engine->newQObject(nativeLog); + engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function")); + + QJSValue timer = engine->newQObject(nativeTimer); + engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, timer.property("setTimer")); + engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, timer.property("clearTimer")); + + QJSValue empty = engine->newQObject(nativeEmpty); + engine->globalObject().setProperty(Constant::INJECT_EMPTY, empty.property("function")); + + QJSValue bridge = engine->newQObject(nativeBridge); + engine->globalObject().setProperty(Constant::INJECT_BRIDGE, bridge.property("function")); +} + +void JSEngine::initDoricRuntime() { + { + QResource resource(":/doric/doric-sandbox.js"); + QFile *file = new QFile(resource.fileName()); + file->open(QFile::ReadOnly | QFile::Text); + QTextStream in(file); + QString script = in.readAll(); + file->close(); + delete file; + + QJSValue result = engine->evaluate(script, "doric-sandbox.js"); + qDebug() << "doric-sandbox.js result: " + result.toString(); + } + + { + QResource resource(":/doric/doric-lib.js"); + QFile *file = new QFile(resource.fileName()); + file->open(QFile::ReadOnly | QFile::Text); + QTextStream in(file); + QString script = in.readAll(); + file->close(); + delete file; + + QString lib = QString(Constant::TEMPLATE_MODULE) + .replace("%s1", "doric") + .replace("%s2", script); + QJSValue result = engine->evaluate(lib, "doric-lib.js"); + qDebug() << "doric-lib.js result: " + result.toString(); + } +} diff --git a/doric/engine/js_engine.h b/doric/engine/js_engine.h index 5a011b88..66fa65c1 100644 --- a/doric/engine/js_engine.h +++ b/doric/engine/js_engine.h @@ -1,47 +1,23 @@ #ifndef JS_ENGINE_H #define JS_ENGINE_H -#include #include -#include -#include -#include "constant.h" #include "native/native_bridge.h" #include "native/native_empty.h" #include "native/native_log.h" #include "native/native_timer.h" -class JSEngine : public QObject { - Q_OBJECT +class JSEngine { public: QJSEngine *engine = new QJSEngine(); - JSEngine(QObject *parent = nullptr) : QObject(parent) { - initJSEngine(); - injectGlobal(); - initDoricRuntime(); - } + JSEngine(); - void prepareContext(int contextId, QString *script) { - QString contextIdString = QString::number(contextId); - QString source = QString(Constant::TEMPLATE_CONTEXT_CREATE) - .replace("%s1", *script) - .replace("%s2", contextIdString) - .replace("%s3", contextIdString) - .replace("%s4", contextIdString); - QJSValue result = engine->evaluate(source, "context://" + contextIdString); - qDebug() << "context://" + contextIdString + " result: " + result.toString(); - } + void prepareContext(int contextId, QString *script); - void destroyContext(int contextId) { - QString contextIdString = QString::number(contextId); - QString source = QString(Constant::TEMPLATE_CONTEXT_DESTROY) - .replace("%s", contextIdString); - QJSValue result = engine->evaluate(source, "_context://" + contextIdString); - qDebug() << "context://" + contextIdString + " result: " + result.toString(); - } + void destroyContext(int contextId); private: NativeLog *nativeLog = new NativeLog(); @@ -49,55 +25,11 @@ private: NativeEmpty *nativeEmpty = new NativeEmpty(); NativeBridge *nativeBridge = new NativeBridge(); - void initJSEngine() { - engine->installExtensions(QJSEngine::AllExtensions); - } + void initJSEngine(); - void injectGlobal() { - QJSValue log = engine->newQObject(nativeLog); - engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function")); + void injectGlobal(); - QJSValue timer = engine->newQObject(nativeTimer); - engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, timer.property("setTimer")); - engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, timer.property("clearTimer")); - - QJSValue empty = engine->newQObject(nativeEmpty); - engine->globalObject().setProperty(Constant::INJECT_EMPTY, empty.property("function")); - - QJSValue bridge = engine->newQObject(nativeBridge); - engine->globalObject().setProperty(Constant::INJECT_BRIDGE, bridge.property("function")); - } - - void initDoricRuntime() { - { - QResource resource(":/doric/doric-sandbox.js"); - QFile *file = new QFile(resource.fileName()); - file->open(QFile::ReadOnly | QFile::Text); - QTextStream in(file); - QString script = in.readAll(); - file->close(); - delete file; - - QJSValue result = engine->evaluate(script, "doric-sandbox.js"); - qDebug() << "doric-sandbox.js result: " + result.toString(); - } - - { - QResource resource(":/doric/doric-lib.js"); - QFile *file = new QFile(resource.fileName()); - file->open(QFile::ReadOnly | QFile::Text); - QTextStream in(file); - QString script = in.readAll(); - file->close(); - delete file; - - QString lib = QString(Constant::TEMPLATE_MODULE) - .replace("%s1", "doric") - .replace("%s2", script); - QJSValue result = engine->evaluate(lib, "doric-lib.js"); - qDebug() << "doric-lib.js result: " + result.toString(); - } - } + void initDoricRuntime(); }; #endif // JS_ENGINE_H diff --git a/doric/main.cpp b/doric/main.cpp index 377c3e08..fed8dade 100644 --- a/doric/main.cpp +++ b/doric/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "context_manager.h" diff --git a/doric/native/native_bridge.h b/doric/native/native_bridge.h index 7783e38c..a274e1f7 100644 --- a/doric/native/native_bridge.h +++ b/doric/native/native_bridge.h @@ -1,7 +1,6 @@ #ifndef NATIVE_BRIDGE_H #define NATIVE_BRIDGE_H -#include #include #include diff --git a/doric/native/native_empty.cpp b/doric/native/native_empty.cpp new file mode 100644 index 00000000..fc60bc1a --- /dev/null +++ b/doric/native/native_empty.cpp @@ -0,0 +1,7 @@ +#include + +#include "native_empty.h" + +void NativeEmpty::function() { + qDebug() << "nativeEmpty"; +} diff --git a/doric/native/native_empty.h b/doric/native/native_empty.h index 14340137..64135411 100644 --- a/doric/native/native_empty.h +++ b/doric/native/native_empty.h @@ -2,7 +2,6 @@ #define NATIVE_EMPTY_H #include -#include class NativeEmpty : public QObject { Q_OBJECT @@ -10,9 +9,7 @@ class NativeEmpty : public QObject { public: NativeEmpty(QObject *parent = nullptr) : QObject(parent) {} - Q_INVOKABLE void function() { - qDebug() << "nativeEmpty"; - } + Q_INVOKABLE void function(); }; #endif // NATIVE_EMPTY_H diff --git a/doric/native/native_log.cpp b/doric/native/native_log.cpp new file mode 100644 index 00000000..70e7bb72 --- /dev/null +++ b/doric/native/native_log.cpp @@ -0,0 +1,13 @@ +#include + +#include "native_log.h" + +void NativeLog::function(QString level, QString content) { + if (level == 'w') { + qWarning() << content; + } else if (level == 'd') { + qDebug() << content; + } else if (level == 'e') { + qCritical() << content; + } +} diff --git a/doric/native/native_log.h b/doric/native/native_log.h index f2caf7f1..1649496b 100644 --- a/doric/native/native_log.h +++ b/doric/native/native_log.h @@ -2,7 +2,6 @@ #define NATIVELOG_H #include -#include class NativeLog : public QObject { Q_OBJECT @@ -10,15 +9,7 @@ class NativeLog : public QObject { public: NativeLog(QObject *parent = nullptr) : QObject(parent) {} - Q_INVOKABLE void function(QString level, QString content) { - if (level == 'w') { - qWarning() << content; - } else if (level == 'd') { - qDebug() << content; - } else if (level == 'e') { - qCritical() << content; - } - } + Q_INVOKABLE void function(QString level, QString content); }; #endif // NATIVELOG_H diff --git a/doric/native/native_timer.cpp b/doric/native/native_timer.cpp new file mode 100644 index 00000000..2e4ec14d --- /dev/null +++ b/doric/native/native_timer.cpp @@ -0,0 +1,30 @@ +#include + +#include "native_timer.h" + +void NativeTimer::setTimer(long timerId, int time, bool repeat) { + QTimer *timer = new QTimer(this); + timer->setSingleShot(!repeat); + connect(timer, &QTimer::timeout, this, [=] () { + if (deletedTimerIds->contains(timerId)) { + deletedTimerIds->remove(timerId); + delete timer; + } else { + engine->evaluate( + Constant::GLOBAL_DORIC + "." + + Constant::DORIC_TIMER_CALLBACK + "(" + + QString::number(timerId) + ")" + ); + + if (!repeat) { + deletedTimerIds->remove(timerId); + delete timer; + } + } + }); + timer->start(time); +} + +void NativeTimer::clearTimer(long timerId) { + deletedTimerIds->insert(timerId); +} diff --git a/doric/native/native_timer.h b/doric/native/native_timer.h index 89575af5..6cb4eff9 100644 --- a/doric/native/native_timer.h +++ b/doric/native/native_timer.h @@ -4,7 +4,6 @@ #include #include #include -#include #include "constant.h" @@ -20,32 +19,9 @@ public: this->engine = engine; } - Q_INVOKABLE void setTimer(long timerId, int time, bool repeat) { - QTimer *timer = new QTimer(this); - timer->setSingleShot(!repeat); - connect(timer, &QTimer::timeout, this, [=] () { - if (deletedTimerIds->contains(timerId)) { - deletedTimerIds->remove(timerId); - delete timer; - } else { - engine->evaluate( - Constant::GLOBAL_DORIC + "." + - Constant::DORIC_TIMER_CALLBACK + "(" + - QString::number(timerId) + ")" - ); + Q_INVOKABLE void setTimer(long timerId, int time, bool repeat); - if (!repeat) { - deletedTimerIds->remove(timerId); - delete timer; - } - } - }); - timer->start(time); - } - - Q_INVOKABLE void clearTimer(long timerId) { - deletedTimerIds->insert(timerId); - } + Q_INVOKABLE void clearTimer(long timerId); }; #endif // NATIVE_TIMER_H diff --git a/doric/registry.cpp b/doric/registry.cpp new file mode 100644 index 00000000..b4e6e79a --- /dev/null +++ b/doric/registry.cpp @@ -0,0 +1,11 @@ +#include + +#include "registry.h" + +Registry::Registry() { + registerNativePlugin(typeid(ShaderPlugin).name()); +} + +void Registry::registerNativePlugin(QString name) { + qDebug() << name; +} diff --git a/doric/registry.h b/doric/registry.h index 9b7cd2ad..30ddc9a6 100644 --- a/doric/registry.h +++ b/doric/registry.h @@ -12,13 +12,9 @@ private: QMap pluginInfoMap; public: - Registry() { - registerNativePlugin(typeid(ShaderPlugin).name()); - } + Registry(); - void registerNativePlugin(QString name) { - qDebug() << name; - } + void registerNativePlugin(QString name); }; #endif // REGISTRY_H