From b8a95b50d42d980d45215487116d0a928e8a1f18 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Fri, 23 Aug 2024 08:23:33 +0200 Subject: [PATCH] Lua: Cleanup qttypes binding Uses switch statements instead to make code easier to read. Cleaned up const'ness Change-Id: I5c36e896743fc51ce9bffc6d9a32e893ef6f012a Reviewed-by: Christian Kandeler --- src/plugins/lua/luaqttypes.cpp | 163 +++++++++++++++++---------------- 1 file changed, 83 insertions(+), 80 deletions(-) diff --git a/src/plugins/lua/luaqttypes.cpp b/src/plugins/lua/luaqttypes.cpp index 7bc1144cf29..62650e667a7 100644 --- a/src/plugins/lua/luaqttypes.cpp +++ b/src/plugins/lua/luaqttypes.cpp @@ -28,12 +28,8 @@ QString sol_lua_get(sol::types, lua_State *L, int index, sol::stack::re int sol_lua_push(sol::types, lua_State *L, const QString &qStr) { - // create table sol::state_view lua(L); - // use base sol method to push the string - int amount = sol::stack::push(L, qStr.toLocal8Bit().data()); - // return # of things pushed onto stack - return amount; + return sol::stack::push(L, qStr.toLocal8Bit().data()); } // QRect @@ -47,28 +43,30 @@ bool sol_lua_check(sol::types, } QRect sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record &tracking) { - sol::state_view lua(L); - sol::table table = sol::stack::get(L, index, tracking); - if (table.size() != 0 && table.size() != 2 && table.size() != 4) - throw sol::error("Expected table to have 'x', 'y', 'width' and 'height' or 2 (pos and " - "size) or 4 elements"); - if (table.size() == 0) + const sol::state_view lua(L); + const sol::table table = sol::stack::get(L, index, tracking); + switch (table.size()) { + case 0: return QRect( table.get("x"), table.get("y"), table.get("width"), table.get("height")); - if (table.size() == 2) + case 2: return QRect(table.get(1), table.get(2)); - - return QRect(table.get(1), table.get(2), table.get(3), table.get(4)); + case 4: + return QRect(table.get(1), table.get(2), table.get(3), table.get(4)); + default: + throw sol::error("Expected table to have 'x', 'y', 'width' and 'height' or 2 (pos and " + "size) or 4 elements"); + } } int sol_lua_push(sol::types, lua_State *L, const QRect &value) { sol::state_view lua(L); - sol::table table = lua.create_table(); - table.set("x", value.x(), "y", value.y(), "width", value.width(), "height", value.height()); + const sol::table table = lua.create_table_with( + "x", value.x(), "y", value.y(), "width", value.width(), "height", value.height()); return sol::stack::push(L, table); } @@ -84,20 +82,21 @@ bool sol_lua_check(sol::types, QSize sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record &tracking) { - sol::state_view lua(L); - sol::table table = sol::stack::get(L, index, tracking); - if (table.size() != 0 && table.size() != 2) - throw sol::error("Expected table to have 'width' and 'height' or 2 elements"); - if (table.size() == 0) + const sol::state_view lua(L); + const sol::table table = sol::stack::get(L, index, tracking); + switch (table.size()) { + case 0: return QSize(table.get("width"), table.get("height")); - - return QSize(table.get(1), table.get(2)); + case 2: + return QSize(table.get(1), table.get(2)); + default: + throw sol::error("Expected table to have 'width' and 'height' or 2 elements"); + } } int sol_lua_push(sol::types, lua_State *L, const QSize &value) { sol::state_view lua(L); - sol::table table = lua.create_table(); - table.set("width", value.width(), "height", value.height()); + const sol::table table = lua.create_table_with("width", value.width(), "height", value.height()); return sol::stack::push(L, table); } @@ -112,19 +111,21 @@ bool sol_lua_check(sol::types, } QPoint sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record &tracking) { - sol::state_view lua(L); - sol::table table = sol::stack::get(L, index, tracking); - if (table.size() != 0 && table.size() != 2) - throw sol::error("Expected table to have 'x' and 'y' or 2 elements"); - if (table.size() == 0) + const sol::state_view lua(L); + const sol::table table = sol::stack::get(L, index, tracking); + switch (table.size()) { + case 0: return QPoint(table.get("x"), table.get("y")); - return QPoint(table.get(1), table.get(2)); + case 2: + return QPoint(table.get(1), table.get(2)); + default: + throw sol::error("Expected table to have 'x' and 'y' or 2 elements"); + } } int sol_lua_push(sol::types, lua_State *L, const QPoint &value) { sol::state_view lua(L); - sol::table table = lua.create_table(); - table.set("x", value.x(), "y", value.y()); + const sol::table table = lua.create_table_with("x", value.x(), "y", value.y()); return sol::stack::push(L, table); } @@ -139,28 +140,31 @@ bool sol_lua_check(sol::types, } QRectF sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record &tracking) { - sol::state_view lua(L); - sol::table table = sol::stack::get(L, index, tracking); - if (table.size() != 0 && table.size() != 2 && table.size() != 4) - throw sol::error("Expected table to have 'x', 'y', 'width' and 'height' or 2 (pos and " - "size) or 4 elements"); - if (table.size() == 0) { + const sol::state_view lua(L); + const sol::table table = sol::stack::get(L, index, tracking); + + switch (table.size()) { + case 0: return QRectF( table.get("x"), table.get("y"), table.get("width"), table.get("height")); - } - if (table.size() == 2) + case 2: return QRectF(table.get(1), table.get(2)); - - return QRectF(table.get(1), table.get(2), table.get(3), table.get(4)); + case 4: + return QRectF( + table.get(1), table.get(2), table.get(3), table.get(4)); + default: + throw sol::error("Expected table to have 'x', 'y', 'width' and 'height' or 2 (pos and " + "size) or 4 elements"); + } } int sol_lua_push(sol::types, lua_State *L, const QRectF &value) { sol::state_view lua(L); - sol::table table = lua.create_table(); - table.set("x", value.x(), "y", value.y(), "width", value.width(), "height", value.height()); + const sol::table table = lua.create_table_with( + "x", value.x(), "y", value.y(), "width", value.width(), "height", value.height()); return sol::stack::push(L, table); } @@ -175,19 +179,21 @@ bool sol_lua_check(sol::types, } QSizeF sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record &tracking) { - sol::state_view lua(L); - sol::table table = sol::stack::get(L, index, tracking); - if (table.size() != 0 && table.size() != 2) - throw sol::error("Expected table to have 'width' and 'height' or 2 elements"); - if (table.size() == 0) + const sol::state_view lua(L); + const sol::table table = sol::stack::get(L, index, tracking); + switch (table.size()) { + case 0: return QSizeF(table.get("width"), table.get("height")); - return QSizeF(table.get(1), table.get(2)); + case 2: + return QSizeF(table.get(1), table.get(2)); + default: + throw sol::error("Expected table to have 'width' and 'height' or 2 elements"); + } } int sol_lua_push(sol::types, lua_State *L, const QSizeF &value) { sol::state_view lua(L); - sol::table table = lua.create_table(); - table.set("width", value.width(), "height", value.height()); + const sol::table table = lua.create_table_with("width", value.width(), "height", value.height()); return sol::stack::push(L, table); } @@ -202,19 +208,21 @@ bool sol_lua_check(sol::types, } QPointF sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record &tracking) { - sol::state_view lua(L); - sol::table table = sol::stack::get(L, index, tracking); - if (table.size() != 0 && table.size() != 2) - throw sol::error("Expected table to have 'x' and 'y' or 2 elements"); - if (table.size() == 0) + const sol::state_view lua(L); + const sol::table table = sol::stack::get(L, index, tracking); + switch (table.size()) { + case 0: return QPointF(table.get("x"), table.get("y")); - return QPointF(table.get(1), table.get(2)); + case 2: + return QPointF(table.get(1), table.get(2)); + default: + throw sol::error("Expected table to have 'x' and 'y' or 2 elements"); + } } int sol_lua_push(sol::types, lua_State *L, const QPointF &value) { sol::state_view lua(L); - sol::table table = lua.create_table(); - table.set("x", value.x(), "y", value.y()); + const sol::table table = lua.create_table_with("x", value.x(), "y", value.y()); return sol::stack::push(L, table); } @@ -229,31 +237,26 @@ bool sol_lua_check(sol::types, } QColor sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record &tracking) { - sol::state_view lua(L); - sol::table table = sol::stack::get(L, index, tracking); - if (table.size() != 0 && table.size() != 4) - throw sol::error("Expected table to have 0 or 4 elements"); - if (table.size() == 0) { + const sol::state_view lua(L); + const sol::table table = sol::stack::get(L, index, tracking); + switch (table.size()) { + case 0: return QColor( table.get("red"), table.get("green"), table.get("blue"), table.get("alpha")); + case 4: + return QColor(table.get(1), table.get(2), table.get(3), table.get(4)); + default: + throw sol::error("Expected table to have 0 or 4 elements"); } - return QColor(table.get(1), table.get(2), table.get(3), table.get(4)); } int sol_lua_push(sol::types, lua_State *L, const QColor &value) { sol::state_view lua(L); - sol::table table = lua.create_table(); - table.set("red", - value.red(), - "green", - value.green(), - "blue", - value.blue(), - "alpha", - value.alpha()); + const sol::table table = lua.create_table_with( + "red", value.red(), "green", value.green(), "blue", value.blue(), "alpha", value.alpha()); return sol::stack::push(L, table); } @@ -273,10 +276,10 @@ QStringList sol_lua_get(sol::types, { QStringList result; sol::state_view lua(L); - sol::table table = sol::stack::get(L, index, tracking); - for (size_t i = 1; i < table.size() + 1; i++) { - result.append(table.get(i)); - } + const sol::table table = sol::stack::get(L, index, tracking); + for (const auto &kv : table) + result.append(kv.second.as()); + return result; } int sol_lua_push(sol::types, lua_State *L, const QStringList &value)