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)
{
// 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>,
}
QRect sol_lua_get(sol::types<QRect>, lua_State *L, int index, sol::stack::record &tracking)
{
sol::state_view lua(L);
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 "
"size) or 4 elements");
if (table.size() == 0)
const sol::state_view lua(L);
const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
switch (table.size()) {
case 0:
return QRect(
table.get<int>("x"),
table.get<int>("y"),
table.get<int>("width"),
table.get<int>("height"));
if (table.size() == 2)
case 2:
return QRect(table.get<QPoint>(1), table.get<QSize>(2));
return QRect(table.get<int>(1), table.get<int>(2), table.get<int>(3), table.get<int>(4));
case 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)
{
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>,
QSize sol_lua_get(sol::types<QSize>, lua_State *L, int index, sol::stack::record &tracking)
{
sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(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<sol::table>(L, index, tracking);
switch (table.size()) {
case 0:
return QSize(table.get<int>("width"), table.get<int>("height"));
return QSize(table.get<int>(1), table.get<int>(2));
case 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)
{
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>,
}
QPoint sol_lua_get(sol::types<QPoint>, lua_State *L, int index, sol::stack::record &tracking)
{
sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(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<sol::table>(L, index, tracking);
switch (table.size()) {
case 0:
return QPoint(table.get<int>("x"), table.get<int>("y"));
return QPoint(table.get<int>(1), table.get<int>(2));
case 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)
{
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>,
}
QRectF sol_lua_get(sol::types<QRectF>, lua_State *L, int index, sol::stack::record &tracking)
{
sol::state_view lua(L);
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 "
"size) or 4 elements");
if (table.size() == 0) {
const sol::state_view lua(L);
const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
switch (table.size()) {
case 0:
return QRectF(
table.get<qreal>("x"),
table.get<qreal>("y"),
table.get<qreal>("width"),
table.get<qreal>("height"));
}
if (table.size() == 2)
case 2:
return QRectF(table.get<QPointF>(1), table.get<QSizeF>(2));
return QRectF(table.get<qreal>(1), table.get<qreal>(2), table.get<qreal>(3), table.get<qreal>(4));
case 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)
{
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>,
}
QSizeF sol_lua_get(sol::types<QSizeF>, lua_State *L, int index, sol::stack::record &tracking)
{
sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(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<sol::table>(L, index, tracking);
switch (table.size()) {
case 0:
return QSizeF(table.get<qreal>("width"), table.get<qreal>("height"));
return QSizeF(table.get<qreal>(1), table.get<qreal>(2));
case 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)
{
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>,
}
QPointF sol_lua_get(sol::types<QPointF>, lua_State *L, int index, sol::stack::record &tracking)
{
sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(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<sol::table>(L, index, tracking);
switch (table.size()) {
case 0:
return QPointF(table.get<qreal>("x"), table.get<qreal>("y"));
return QPointF(table.get<qreal>(1), table.get<qreal>(2));
case 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)
{
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>,
}
QColor sol_lua_get(sol::types<QColor>, lua_State *L, int index, sol::stack::record &tracking)
{
sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(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<sol::table>(L, index, tracking);
switch (table.size()) {
case 0:
return QColor(
table.get<int>("red"),
table.get<int>("green"),
table.get<int>("blue"),
table.get<int>("alpha"));
case 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");
}
return QColor(table.get<int>(1), table.get<int>(2), table.get<int>(3), table.get<int>(4));
}
int sol_lua_push(sol::types<QColor>, 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>,
{
QStringList result;
sol::state_view lua(L);
sol::table table = sol::stack::get<sol::table>(L, index, tracking);
for (size_t i = 1; i < table.size() + 1; i++) {
result.append(table.get<QString>(i));
}
const sol::table table = sol::stack::get<sol::table>(L, index, tracking);
for (const auto &kv : table)
result.append(kv.second.as<QString>());
return result;
}
int sol_lua_push(sol::types<QStringList>, lua_State *L, const QStringList &value)