add LuaEngineRegistry

This commit is contained in:
Syping 2019-11-04 14:00:10 +01:00
parent eb7ad50299
commit 3598028ded
4 changed files with 86 additions and 5 deletions

View file

@ -17,6 +17,7 @@
#define LUA_LIB #define LUA_LIB
#include "LuaEngine.h" #include "LuaEngine.h"
#include "LuaEngineRegistry.h"
#include <QTextStream> #include <QTextStream>
#include <QMetaMethod> #include <QMetaMethod>
@ -25,7 +26,8 @@ LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent)
L = luaL_newstate(); L = luaL_newstate();
if (loadBaseLibraries) if (loadBaseLibraries)
luaL_openlibs(L); luaL_openlibs(L);
pushPointer("__LuaEngine", (void*)this); engineRegistry->registerEngine((void*)L, (void*)this);
pushVariant("DeleteInstant", 0); pushVariant("DeleteInstant", 0);
pushVariant("DeleteLater", 1); pushVariant("DeleteLater", 1);
pushFunction("delete", luaObjectDelete_p); pushFunction("delete", luaObjectDelete_p);
@ -38,6 +40,7 @@ LuaEngine::LuaEngine(QObject *parent, bool loadBaseLibraries) : QObject(parent)
LuaEngine::~LuaEngine() LuaEngine::~LuaEngine()
{ {
engineRegistry->unregisterEngine(L);
lua_close(L); lua_close(L);
} }
@ -445,7 +448,7 @@ int LuaEngine::luaTriggerConnect_p(lua_State *L_p)
QString signalString = getVariant(L_p, 2).toString(); QString signalString = getVariant(L_p, 2).toString();
int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data());
if (signalIndex != -1) { if (signalIndex != -1) {
LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); LuaEngine *engine = (LuaEngine*)engineRegistry->getEngine(L_p);
int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()");
if (slotIndex != -1) { if (slotIndex != -1) {
QMetaMethod signal = object->metaObject()->method(signalIndex); 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(); QString signalString = getVariant(L_p, 2).toString();
int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data());
if (signalIndex != -1) { if (signalIndex != -1) {
LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); LuaEngine *engine = (LuaEngine*)engineRegistry->getEngine(L_p);
int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()");
if (slotIndex != -1) { if (slotIndex != -1) {
QMetaMethod signal = object->metaObject()->method(signalIndex); QMetaMethod signal = object->metaObject()->method(signalIndex);

View file

@ -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);
}

View file

@ -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 <QObject>
#include <QMap>
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<void*, void*> registryMap;
};
#define engineRegistry LuaEngineRegistry::getInstance()
#endif // LUAENGINEREGISTRY_H

View file

@ -36,12 +36,14 @@ unix {
} }
SOURCES += \ SOURCES += \
luaengine/LuaEngine.cpp luaengine/LuaEngine.cpp \
luaengine/LuaEngineRegistry.cpp
HEADERS += \ HEADERS += \
luaengine/LuaEngine.h \ luaengine/LuaEngine.h \
luaengine/LuaEngine_global.h \ luaengine/LuaEngine_global.h \
luaengine/LuaEngine_macro.h luaengine/LuaEngine_macro.h \
luaengine/LuaEngineRegistry.h
SOURCES += \ SOURCES += \
lua/lapi.c \ lua/lapi.c \