From 3598028ded73982e747c6d4c03a414cc7e0ce043 Mon Sep 17 00:00:00 2001 From: Syping Date: Mon, 4 Nov 2019 14:00:10 +0100 Subject: [PATCH] add LuaEngineRegistry --- src/luaenginecore/luaengine/LuaEngine.cpp | 9 ++-- .../luaengine/LuaEngineRegistry.cpp | 35 ++++++++++++++++ .../luaengine/LuaEngineRegistry.h | 41 +++++++++++++++++++ src/luaenginecore/luaenginecore.pro | 6 ++- 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 src/luaenginecore/luaengine/LuaEngineRegistry.cpp create mode 100644 src/luaenginecore/luaengine/LuaEngineRegistry.h diff --git a/src/luaenginecore/luaengine/LuaEngine.cpp b/src/luaenginecore/luaengine/LuaEngine.cpp index 83db65b..0a68f4e 100644 --- a/src/luaenginecore/luaengine/LuaEngine.cpp +++ b/src/luaenginecore/luaengine/LuaEngine.cpp @@ -17,6 +17,7 @@ #define LUA_LIB #include "LuaEngine.h" +#include "LuaEngineRegistry.h" #include #include @@ -25,7 +26,8 @@ LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent) L = luaL_newstate(); if (loadBaseLibraries) luaL_openlibs(L); - pushPointer("__LuaEngine", (void*)this); + engineRegistry->registerEngine((void*)L, (void*)this); + pushVariant("DeleteInstant", 0); pushVariant("DeleteLater", 1); pushFunction("delete", luaObjectDelete_p); @@ -38,6 +40,7 @@ LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent) LuaEngine::~LuaEngine() { + engineRegistry->unregisterEngine(L); lua_close(L); } @@ -445,7 +448,7 @@ int LuaEngine::luaTriggerConnect_p(lua_State *L_p) QString signalString = getVariant(L_p, 2).toString(); int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); if (signalIndex != -1) { - LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); + LuaEngine *engine = (LuaEngine*)engineRegistry->getEngine(L_p); int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); if (slotIndex != -1) { QMetaMethod signal = object->metaObject()->method(signalIndex); @@ -470,7 +473,7 @@ int LuaEngine::luaTriggerDisconnect_p(lua_State *L_p) QString signalString = getVariant(L_p, 2).toString(); int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); if (signalIndex != -1) { - LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); + LuaEngine *engine = (LuaEngine*)engineRegistry->getEngine(L_p); int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); if (slotIndex != -1) { QMetaMethod signal = object->metaObject()->method(signalIndex); diff --git a/src/luaenginecore/luaengine/LuaEngineRegistry.cpp b/src/luaenginecore/luaengine/LuaEngineRegistry.cpp new file mode 100644 index 0000000..6db97d5 --- /dev/null +++ b/src/luaenginecore/luaengine/LuaEngineRegistry.cpp @@ -0,0 +1,35 @@ +/***************************************************************************** +* luaEngine Lua Engine for Qt +* Copyright (C) 2019 Syping +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*****************************************************************************/ + +#include "LuaEngineRegistry.h" + +LuaEngineRegistry LuaEngineRegistry::luaEngineRegistryInstance; + +void LuaEngineRegistry::registerEngine(void *state, void *engine) +{ + registryMap.insert(state, engine); +} + +void LuaEngineRegistry::unregisterEngine(void *state) +{ + registryMap.remove(state); +} + +void* LuaEngineRegistry::getEngine(void *state) +{ + return registryMap.value(state, nullptr); +} diff --git a/src/luaenginecore/luaengine/LuaEngineRegistry.h b/src/luaenginecore/luaengine/LuaEngineRegistry.h new file mode 100644 index 0000000..2f6c5ab --- /dev/null +++ b/src/luaenginecore/luaengine/LuaEngineRegistry.h @@ -0,0 +1,41 @@ +/***************************************************************************** +* luaEngine Lua Engine for Qt +* Copyright (C) 2019 Syping +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*****************************************************************************/ + +#ifndef LUAENGINEREGISTRY_H +#define LUAENGINEREGISTRY_H + +#include "LuaEngine_global.h" +#include +#include + +class LUAENGINESHARED_EXPORT LuaEngineRegistry : public QObject +{ + Q_OBJECT +public: + static LuaEngineRegistry* getInstance() { return &luaEngineRegistryInstance; } + void registerEngine(void *state, void *engine); + void unregisterEngine(void *state); + void* getEngine(void *state); + +private: + static LuaEngineRegistry luaEngineRegistryInstance; + QMap registryMap; +}; + +#define engineRegistry LuaEngineRegistry::getInstance() + +#endif // LUAENGINEREGISTRY_H diff --git a/src/luaenginecore/luaenginecore.pro b/src/luaenginecore/luaenginecore.pro index 1b065ca..e8fddfe 100644 --- a/src/luaenginecore/luaenginecore.pro +++ b/src/luaenginecore/luaenginecore.pro @@ -36,12 +36,14 @@ unix { } SOURCES += \ - luaengine/LuaEngine.cpp + luaengine/LuaEngine.cpp \ + luaengine/LuaEngineRegistry.cpp HEADERS += \ luaengine/LuaEngine.h \ luaengine/LuaEngine_global.h \ - luaengine/LuaEngine_macro.h + luaengine/LuaEngine_macro.h \ + luaengine/LuaEngineRegistry.h SOURCES += \ lua/lapi.c \