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 <christian.kandeler@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-08-23 08:23:33 +02:00
parent 715339bca6
commit b8a95b50d4

View File

@@ -28,12 +28,8 @@ QString sol_lua_get(sol::types<QString>, lua_State *L, int index, sol::stack::re
int sol_lua_push(sol::types<QString>, lua_State *L, const QString &qStr) int sol_lua_push(sol::types<QString>, lua_State *L, const QString &qStr)
{ {
// create table
sol::state_view lua(L); sol::state_view lua(L);
// use base sol method to push the string return sol::stack::push(L, qStr.toLocal8Bit().data());
int amount = sol::stack::push(L, qStr.toLocal8Bit().data());
// return # of things pushed onto stack
return amount;
} }
// QRect // QRect
@@ -47,28 +43,30 @@ bool sol_lua_check(sol::types<QRect>,
} }
QRect sol_lua_get(sol::types<QRect>, lua_State *L, int index, sol::stack::record &tracking) QRect sol_lua_get(sol::types<QRect>, lua_State *L, int index, sol::stack::record &tracking)
{ {
sol::state_view lua(L); const sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking); const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
if (table.size() != 0 && table.size() != 2 && table.size() != 4) switch (table.size()) {
throw sol::error("Expected table to have 'x', 'y', 'width' and 'height' or 2 (pos and " case 0:
"size) or 4 elements");
if (table.size() == 0)
return QRect( return QRect(
table.get<int>("x"), table.get<int>("x"),
table.get<int>("y"), table.get<int>("y"),
table.get<int>("width"), table.get<int>("width"),
table.get<int>("height")); table.get<int>("height"));
if (table.size() == 2) case 2:
return QRect(table.get<QPoint>(1), table.get<QSize>(2)); return QRect(table.get<QPoint>(1), table.get<QSize>(2));
case 4:
return QRect(table.get<int>(1), table.get<int>(2), table.get<int>(3), table.get<int>(4)); return QRect(table.get<int>(1), table.get<int>(2), table.get<int>(3), table.get<int>(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<QRect>, lua_State *L, const QRect &value) int sol_lua_push(sol::types<QRect>, lua_State *L, const QRect &value)
{ {
sol::state_view lua(L); sol::state_view lua(L);
sol::table table = lua.create_table(); const sol::table table = lua.create_table_with(
table.set("x", value.x(), "y", value.y(), "width", value.width(), "height", value.height()); "x", value.x(), "y", value.y(), "width", value.width(), "height", value.height());
return sol::stack::push(L, table); return sol::stack::push(L, table);
} }
@@ -84,20 +82,21 @@ bool sol_lua_check(sol::types<QSize>,
QSize sol_lua_get(sol::types<QSize>, lua_State *L, int index, sol::stack::record &tracking) QSize sol_lua_get(sol::types<QSize>, lua_State *L, int index, sol::stack::record &tracking)
{ {
sol::state_view lua(L); const sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking); const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
if (table.size() != 0 && table.size() != 2) switch (table.size()) {
throw sol::error("Expected table to have 'width' and 'height' or 2 elements"); case 0:
if (table.size() == 0)
return QSize(table.get<int>("width"), table.get<int>("height")); return QSize(table.get<int>("width"), table.get<int>("height"));
case 2:
return QSize(table.get<int>(1), table.get<int>(2)); return QSize(table.get<int>(1), table.get<int>(2));
default:
throw sol::error("Expected table to have 'width' and 'height' or 2 elements");
}
} }
int sol_lua_push(sol::types<QSize>, lua_State *L, const QSize &value) int sol_lua_push(sol::types<QSize>, lua_State *L, const QSize &value)
{ {
sol::state_view lua(L); sol::state_view lua(L);
sol::table table = lua.create_table(); const sol::table table = lua.create_table_with("width", value.width(), "height", value.height());
table.set("width", value.width(), "height", value.height());
return sol::stack::push(L, table); return sol::stack::push(L, table);
} }
@@ -112,19 +111,21 @@ bool sol_lua_check(sol::types<QPoint>,
} }
QPoint sol_lua_get(sol::types<QPoint>, lua_State *L, int index, sol::stack::record &tracking) QPoint sol_lua_get(sol::types<QPoint>, lua_State *L, int index, sol::stack::record &tracking)
{ {
sol::state_view lua(L); const sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking); const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
if (table.size() != 0 && table.size() != 2) switch (table.size()) {
throw sol::error("Expected table to have 'x' and 'y' or 2 elements"); case 0:
if (table.size() == 0)
return QPoint(table.get<int>("x"), table.get<int>("y")); return QPoint(table.get<int>("x"), table.get<int>("y"));
case 2:
return QPoint(table.get<int>(1), table.get<int>(2)); return QPoint(table.get<int>(1), table.get<int>(2));
default:
throw sol::error("Expected table to have 'x' and 'y' or 2 elements");
}
} }
int sol_lua_push(sol::types<QPoint>, lua_State *L, const QPoint &value) int sol_lua_push(sol::types<QPoint>, lua_State *L, const QPoint &value)
{ {
sol::state_view lua(L); sol::state_view lua(L);
sol::table table = lua.create_table(); const sol::table table = lua.create_table_with("x", value.x(), "y", value.y());
table.set("x", value.x(), "y", value.y());
return sol::stack::push(L, table); return sol::stack::push(L, table);
} }
@@ -139,28 +140,31 @@ bool sol_lua_check(sol::types<QRectF>,
} }
QRectF sol_lua_get(sol::types<QRectF>, lua_State *L, int index, sol::stack::record &tracking) QRectF sol_lua_get(sol::types<QRectF>, lua_State *L, int index, sol::stack::record &tracking)
{ {
sol::state_view lua(L); const sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking); const sol::table table = sol::stack::get<sol::table>(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 " switch (table.size()) {
"size) or 4 elements"); case 0:
if (table.size() == 0) {
return QRectF( return QRectF(
table.get<qreal>("x"), table.get<qreal>("x"),
table.get<qreal>("y"), table.get<qreal>("y"),
table.get<qreal>("width"), table.get<qreal>("width"),
table.get<qreal>("height")); table.get<qreal>("height"));
} case 2:
if (table.size() == 2)
return QRectF(table.get<QPointF>(1), table.get<QSizeF>(2)); return QRectF(table.get<QPointF>(1), table.get<QSizeF>(2));
case 4:
return QRectF(table.get<qreal>(1), table.get<qreal>(2), table.get<qreal>(3), table.get<qreal>(4)); return QRectF(
table.get<qreal>(1), table.get<qreal>(2), table.get<qreal>(3), table.get<qreal>(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<QRectF>, lua_State *L, const QRectF &value) int sol_lua_push(sol::types<QRectF>, lua_State *L, const QRectF &value)
{ {
sol::state_view lua(L); sol::state_view lua(L);
sol::table table = lua.create_table(); const sol::table table = lua.create_table_with(
table.set("x", value.x(), "y", value.y(), "width", value.width(), "height", value.height()); "x", value.x(), "y", value.y(), "width", value.width(), "height", value.height());
return sol::stack::push(L, table); return sol::stack::push(L, table);
} }
@@ -175,19 +179,21 @@ bool sol_lua_check(sol::types<QSizeF>,
} }
QSizeF sol_lua_get(sol::types<QSizeF>, lua_State *L, int index, sol::stack::record &tracking) QSizeF sol_lua_get(sol::types<QSizeF>, lua_State *L, int index, sol::stack::record &tracking)
{ {
sol::state_view lua(L); const sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking); const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
if (table.size() != 0 && table.size() != 2) switch (table.size()) {
throw sol::error("Expected table to have 'width' and 'height' or 2 elements"); case 0:
if (table.size() == 0)
return QSizeF(table.get<qreal>("width"), table.get<qreal>("height")); return QSizeF(table.get<qreal>("width"), table.get<qreal>("height"));
case 2:
return QSizeF(table.get<qreal>(1), table.get<qreal>(2)); return QSizeF(table.get<qreal>(1), table.get<qreal>(2));
default:
throw sol::error("Expected table to have 'width' and 'height' or 2 elements");
}
} }
int sol_lua_push(sol::types<QSizeF>, lua_State *L, const QSizeF &value) int sol_lua_push(sol::types<QSizeF>, lua_State *L, const QSizeF &value)
{ {
sol::state_view lua(L); sol::state_view lua(L);
sol::table table = lua.create_table(); const sol::table table = lua.create_table_with("width", value.width(), "height", value.height());
table.set("width", value.width(), "height", value.height());
return sol::stack::push(L, table); return sol::stack::push(L, table);
} }
@@ -202,19 +208,21 @@ bool sol_lua_check(sol::types<QPointF>,
} }
QPointF sol_lua_get(sol::types<QPointF>, lua_State *L, int index, sol::stack::record &tracking) QPointF sol_lua_get(sol::types<QPointF>, lua_State *L, int index, sol::stack::record &tracking)
{ {
sol::state_view lua(L); const sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking); const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
if (table.size() != 0 && table.size() != 2) switch (table.size()) {
throw sol::error("Expected table to have 'x' and 'y' or 2 elements"); case 0:
if (table.size() == 0)
return QPointF(table.get<qreal>("x"), table.get<qreal>("y")); return QPointF(table.get<qreal>("x"), table.get<qreal>("y"));
case 2:
return QPointF(table.get<qreal>(1), table.get<qreal>(2)); return QPointF(table.get<qreal>(1), table.get<qreal>(2));
default:
throw sol::error("Expected table to have 'x' and 'y' or 2 elements");
}
} }
int sol_lua_push(sol::types<QPointF>, lua_State *L, const QPointF &value) int sol_lua_push(sol::types<QPointF>, lua_State *L, const QPointF &value)
{ {
sol::state_view lua(L); sol::state_view lua(L);
sol::table table = lua.create_table(); const sol::table table = lua.create_table_with("x", value.x(), "y", value.y());
table.set("x", value.x(), "y", value.y());
return sol::stack::push(L, table); return sol::stack::push(L, table);
} }
@@ -229,31 +237,26 @@ bool sol_lua_check(sol::types<QColor>,
} }
QColor sol_lua_get(sol::types<QColor>, lua_State *L, int index, sol::stack::record &tracking) QColor sol_lua_get(sol::types<QColor>, lua_State *L, int index, sol::stack::record &tracking)
{ {
sol::state_view lua(L); const sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking); const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
if (table.size() != 0 && table.size() != 4) switch (table.size()) {
throw sol::error("Expected table to have 0 or 4 elements"); case 0:
if (table.size() == 0) {
return QColor( return QColor(
table.get<int>("red"), table.get<int>("red"),
table.get<int>("green"), table.get<int>("green"),
table.get<int>("blue"), table.get<int>("blue"),
table.get<int>("alpha")); table.get<int>("alpha"));
} case 4:
return QColor(table.get<int>(1), table.get<int>(2), table.get<int>(3), table.get<int>(4)); return QColor(table.get<int>(1), table.get<int>(2), table.get<int>(3), table.get<int>(4));
default:
throw sol::error("Expected table to have 0 or 4 elements");
}
} }
int sol_lua_push(sol::types<QColor>, lua_State *L, const QColor &value) int sol_lua_push(sol::types<QColor>, lua_State *L, const QColor &value)
{ {
sol::state_view lua(L); sol::state_view lua(L);
sol::table table = lua.create_table(); const sol::table table = lua.create_table_with(
table.set("red", "red", value.red(), "green", value.green(), "blue", value.blue(), "alpha", value.alpha());
value.red(),
"green",
value.green(),
"blue",
value.blue(),
"alpha",
value.alpha());
return sol::stack::push(L, table); return sol::stack::push(L, table);
} }
@@ -273,10 +276,10 @@ QStringList sol_lua_get(sol::types<QStringList>,
{ {
QStringList result; QStringList result;
sol::state_view lua(L); sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking); const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
for (size_t i = 1; i < table.size() + 1; i++) { for (const auto &kv : table)
result.append(table.get<QString>(i)); result.append(kv.second.as<QString>());
}
return result; return result;
} }
int sol_lua_push(sol::types<QStringList>, lua_State *L, const QStringList &value) int sol_lua_push(sol::types<QStringList>, lua_State *L, const QStringList &value)