From 3fdedb62400f8bd6b7b52ec6987041aa8150682a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 13 Dec 2019 17:45:27 +0800 Subject: [PATCH] add context holder & registry & bridge --- .gitignore | 3 ++- doric/context_holder.h | 17 +++++++++++++++++ doric/context_manager.h | 8 ++++++-- doric/doric.pro | 6 +++++- doric/driver/driver.h | 6 +++--- doric/driver/native_driver.h | 4 ++-- doric/engine/js_engine.h | 4 ++-- doric/native/native_bridge.cpp | 11 +++++++++++ doric/native/native_bridge.h | 8 +------- doric/plugin/shader_plugin.h | 12 ++++++++++++ doric/registry.h | 24 ++++++++++++++++++++++++ 11 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 doric/context_holder.h create mode 100644 doric/native/native_bridge.cpp create mode 100644 doric/plugin/shader_plugin.h create mode 100644 doric/registry.h diff --git a/.gitignore b/.gitignore index 40301363..9a73525d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/build-doric-*/ \ No newline at end of file +/build-doric-*/ +.DS_Store \ No newline at end of file diff --git a/doric/context_holder.h b/doric/context_holder.h new file mode 100644 index 00000000..97213a41 --- /dev/null +++ b/doric/context_holder.h @@ -0,0 +1,17 @@ +#ifndef CONTEXT_HOLDER_H +#define CONTEXT_HOLDER_H + +#include "context.h" + +class ContextHolder { + +public: + Context *_context; + +public: + ContextHolder(Context *context) { + this->_context = context; + } +}; + +#endif // CONTEXT_HOLDER_H diff --git a/doric/context_manager.h b/doric/context_manager.h index 5597545d..47a3f943 100644 --- a/doric/context_manager.h +++ b/doric/context_manager.h @@ -29,13 +29,17 @@ public: return &locla_s; } - Context* createContext(QString* script, QString* source) { + Context *createContext(QString *script, QString *source) { int contextId = counter->fetchAndAddOrdered(1); - Context* context = new Context(contextId, source); + Context *context = new Context(contextId, source); contextMap->insert(contextId, context); context->driver->createContext(contextId, script); return context; } + + Context *getContext(int contextId) { + return contextMap->take(contextId); + } }; #endif // CONTEXT_MANAGER_H diff --git a/doric/doric.pro b/doric/doric.pro index 64b9c924..379ad3b3 100644 --- a/doric/doric.pro +++ b/doric/doric.pro @@ -16,7 +16,8 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ constant.cpp \ driver/native_driver.cpp \ - main.cpp + main.cpp \ + native/native_bridge.cpp RESOURCES += qml.qrc @@ -34,6 +35,7 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin HEADERS += \ constant.h \ context.h \ + context_holder.h \ context_manager.h \ driver/driver.h \ driver/native_driver.h \ @@ -42,5 +44,7 @@ HEADERS += \ native/native_empty.h \ native/native_log.h \ native/native_timer.h \ + plugin/shader_plugin.h \ + registry.h \ template/singleton.h \ utility/utility.h diff --git a/doric/driver/driver.h b/doric/driver/driver.h index aafb11b5..7150138a 100644 --- a/doric/driver/driver.h +++ b/doric/driver/driver.h @@ -6,11 +6,11 @@ class Driver { public: - virtual void createContext(int contextId, QString* script) = 0; + virtual void createContext(int contextId, QString *script) = 0; virtual void destroyContext(int contextId) = 0; - virtual void invokeContextEntityMethod(int contextId, QString* method, ...) = 0; - virtual void invokeDoricMethod(QString* method, ...) = 0; + virtual void invokeContextEntityMethod(int contextId, QString *method, ...) = 0; + virtual void invokeDoricMethod(QString *method, ...) = 0; virtual ~Driver() = default; }; diff --git a/doric/driver/native_driver.h b/doric/driver/native_driver.h index 488d05db..2fc17667 100644 --- a/doric/driver/native_driver.h +++ b/doric/driver/native_driver.h @@ -28,8 +28,8 @@ public: void createContext(int contextId, QString *script) override; void destroyContext(int contextId) override; - void invokeContextEntityMethod(int contextId, QString* method, ...) override; - void invokeDoricMethod(QString* method, ...) override; + void invokeContextEntityMethod(int contextId, QString *method, ...) override; + void invokeDoricMethod(QString *method, ...) override; }; #endif // NATIVE_DRIVER_H diff --git a/doric/engine/js_engine.h b/doric/engine/js_engine.h index caba32b7..4e352382 100644 --- a/doric/engine/js_engine.h +++ b/doric/engine/js_engine.h @@ -2,8 +2,8 @@ #define JS_ENGINE_H #include -#include #include +#include #include #include "constant.h" @@ -24,7 +24,7 @@ public: initDoricRuntime(); } - void prepareContext(int contextId, QString* script) { + void prepareContext(int contextId, QString *script) { QString contextIdString = QString::number(contextId); QString source = QString(Constant::TEMPLATE_CONTEXT_CREATE) .replace("%s1", *script) diff --git a/doric/native/native_bridge.cpp b/doric/native/native_bridge.cpp new file mode 100644 index 00000000..beba2dad --- /dev/null +++ b/doric/native/native_bridge.cpp @@ -0,0 +1,11 @@ +#include "context_manager.h" +#include "native_bridge.h" + +Q_INVOKABLE void NativeBridge::function(int contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue) { + qDebug() << "contextId: " + QString::number(contextId) + ", " + + "module: " + module + ", " + + "methodName: " + methodName + ", " + + "callbackId: " + callbackId + ", " + + "arguments: " + jsValue.toString(); + Context* context = ContextManager::getInstance()->getContext(contextId); +} diff --git a/doric/native/native_bridge.h b/doric/native/native_bridge.h index a3cce1be..7783e38c 100644 --- a/doric/native/native_bridge.h +++ b/doric/native/native_bridge.h @@ -11,13 +11,7 @@ class NativeBridge : public QObject { public: NativeBridge(QObject *parent = nullptr) : QObject(parent) {} - Q_INVOKABLE void function(int contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue) { - qDebug() << "contextId: " + QString::number(contextId) + ", " + - "module: " + module + ", " + - "methodName: " + methodName + ", " + - "callbackId: " + callbackId + ", " + - "arguments: " + jsValue.toString(); - } + Q_INVOKABLE void function(int contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue); }; #endif // NATIVE_BRIDGE_H diff --git a/doric/plugin/shader_plugin.h b/doric/plugin/shader_plugin.h new file mode 100644 index 00000000..da70df05 --- /dev/null +++ b/doric/plugin/shader_plugin.h @@ -0,0 +1,12 @@ +#ifndef SHADER_PLUGIN_H +#define SHADER_PLUGIN_H + +#include "context_holder.h" + +class ShaderPlugin : public ContextHolder { + +public: + ShaderPlugin(Context* context) : ContextHolder(context) {} +}; + +#endif // SHADER_PLUGIN_H diff --git a/doric/registry.h b/doric/registry.h new file mode 100644 index 00000000..9b7cd2ad --- /dev/null +++ b/doric/registry.h @@ -0,0 +1,24 @@ +#ifndef REGISTRY_H +#define REGISTRY_H + +#include +#include + +#include "plugin/shader_plugin.h" + +class Registry { + +private: + QMap pluginInfoMap; + +public: + Registry() { + registerNativePlugin(typeid(ShaderPlugin).name()); + } + + void registerNativePlugin(QString name) { + qDebug() << name; + } +}; + +#endif // REGISTRY_H