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
#include "LuaEngine.h"
#include "LuaEngineRegistry.h"
#include <QTextStream>
#include <QMetaMethod>
@ -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);

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 += \
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 \