improve error proofness

This commit is contained in:
Syping 2018-08-23 17:02:12 +02:00
parent 53e8d9abe5
commit 115d384f88

View file

@ -293,7 +293,7 @@ int LuaEngine::luaDeletePointer_p(lua_State *L_p)
{ {
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
void *pointer = getPointer(L_p, 1); void *pointer = getPointer(L_p, 1);
if (pointer != NULL && ((QObject*)pointer)->inherits("QObject")) { if (pointer != NULL) {
switch (getVariant(L_p, 2).toInt()) switch (getVariant(L_p, 2).toInt())
{ {
case 1: case 1:
@ -310,19 +310,22 @@ int LuaEngine::luaDeletePointer_p(lua_State *L_p)
int LuaEngine::luaTriggerConnect_p(lua_State *L_p) int LuaEngine::luaTriggerConnect_p(lua_State *L_p)
{ {
if (getArgumentCount(L_p) >= 3) { if (getArgumentCount(L_p) >= 3) {
QObject *object = (QObject*)getPointer(L_p, 1); void *pointer = getPointer(L_p, 1);
QString signalString = getVariant(L_p, 2).toString(); if (pointer != NULL) {
int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); QObject *object = (QObject*)pointer;
if (signalIndex != -1) { QString signalString = getVariant(L_p, 2).toString();
LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data());
int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); if (signalIndex != -1) {
if (slotIndex != -1) { LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine");
QMetaMethod signal = object->metaObject()->method(signalIndex); int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()");
QMetaMethod slot = engine->metaObject()->method(slotIndex); if (slotIndex != -1) {
QString funcStorage; QMetaMethod signal = object->metaObject()->method(signalIndex);
QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); QMetaMethod slot = engine->metaObject()->method(slotIndex);
pushVariant(L_p, funcStorage.toUtf8().data(), getVariant(L_p, 3).toString()); QString funcStorage;
QObject::connect(object, signal, engine, slot); QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name();
pushVariant(L_p, funcStorage.toUtf8().data(), getVariant(L_p, 3).toString());
QObject::connect(object, signal, engine, slot);
}
} }
} }
} }
@ -332,19 +335,22 @@ int LuaEngine::luaTriggerConnect_p(lua_State *L_p)
int LuaEngine::luaTriggerDisconnect_p(lua_State *L_p) int LuaEngine::luaTriggerDisconnect_p(lua_State *L_p)
{ {
if (getArgumentCount(L_p) >= 2) { if (getArgumentCount(L_p) >= 2) {
QObject *object = (QObject*)getPointer(L_p, 1); void *pointer = getPointer(L_p, 1);
QString signalString = getVariant(L_p, 2).toString(); if (pointer != NULL) {
int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); QObject *object = (QObject*)pointer;
if (signalIndex != -1) { QString signalString = getVariant(L_p, 2).toString();
LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data());
int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); if (signalIndex != -1) {
if (slotIndex != -1) { LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine");
QMetaMethod signal = object->metaObject()->method(signalIndex); int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()");
QMetaMethod slot = engine->metaObject()->method(slotIndex); if (slotIndex != -1) {
QString funcStorage; QMetaMethod signal = object->metaObject()->method(signalIndex);
QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); QMetaMethod slot = engine->metaObject()->method(slotIndex);
pushVariant(L_p, funcStorage.toUtf8().data(), QVariant()); QString funcStorage;
QObject::disconnect(object, signal, engine, slot); QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name();
pushVariant(L_p, funcStorage.toUtf8().data(), QVariant());
QObject::disconnect(object, signal, engine, slot);
}
} }
} }
} }