将向量传递给<struct> Lua 表

Passing a Vector<struct> to Lua table

本文关键字:struct gt Lua 向量 lt      更新时间:2023-10-16

我想通过发送一个C++的预先格式化的Lua表来改进下面的代码:

int GetCategory(lua_State* L)
{
uint32 Type = CHECKVAL<int>(L, 1);
lua_newtable(L); 
int tbl = lua_gettop(L);
uint32 counter = 1;
// Struct CT { string CategoryBrandName, CategoryName }; > Vector<CT>
auto list = sManagerMgr->GetAll(); 

// Hack modify this to send a metatable/UserData/Table whatever is called
for (auto& elem : list)
{
switch (Type)
{
case 1:
lua_pushstring(L, elem->CategoryBrandName);
break;
case 2:
lua_pushstring(L, elem->CategoryName);
break;
}
lua_rawseti(L, tbl, counter);
counter++;
}
lua_settop(L, tbl);
return 1;
}

基本上 lua_newtable将一张桌子推到Lua堆栈上, lua_gettop将采用顶部索引,即表所在的索引。 然后lua_pushstring(L,元素(; lua_rawseti(升,吨,计数器(;将 ELEMENT 放在我们使用 gettop 获得的索引 tbl 处的表中。元素的索引是计数器的值。

但是这里的问题是,我被迫调用两次 GetCategory 来填充它,如下所示在我的.lua文件中。

table.insert(Group, { GetCategory(1), GetCategory(2) });

当前用途 :

print(i, Group(1)[i], Group(2)[i]);

所以。。我宁愿调用它一次并直接得到这样的东西:

local Group = 
{ 
[1] = { "elem->CategoryBrandName[1]", "elem->CategoryName[1]" },
[2] = { "elem->CategoryBrandName[2]", "elem->CategoryName[2]" }
--etc
};

我试过将 elem 填充到 2D 数组中[1][2],然后推送数组失败

我已经对表,元表,多维数组等进行了大量研究,但我找不到适合我的需求或工作的东西。

有人有解决方案吗?

为什么不让你的函数返回两个值?然后你可以写

local Group = { GetCategories }

我不是 C API 的专家,但我认为只需调用lua_newtable(L)就可以相当容易地完成此操作,所以像这样:

int GetCategories(lua_State* L) {
lua_settop(L, 0);
// Discard arguments so we don't have to save the top of the stack
// and can just use numbers instead (see following lines)
lua_newtable(L); // Index 1 on stack
lua_newtable(L); // Index 2 on stack
// Do your magic
lua_settop(L, 2); // Get rid of your temp variables
return 2; // number of values we return in Lua
}

优化提示:您可以使用lua_createtable并告诉它每个表将有多少个元素,以便Lua可以为它预先分配一些内存。

编辑:我刚刚注意到这一点,但是在您的代码中:

for (auto& elem : list) {
switch (Type) {
case 1:
lua_pushstring(L, elem->CategoryBrandName);
break;
case 2:
lua_pushstring(L, elem->CategoryName);
break;
}
lua_rawseti(L, tbl, counter);
counter++;
}

您只需不断将值推送到堆栈即可。对于长向量,这可能会溢出堆栈(宜早不宜迟(,从而导致麻烦。更好的方法是 1( 推送到堆栈 2( 插入表 3( 将它们弹出:

// Modified for my suggested implementation that returns
// two tables. They can easily be turned around here.
for (auto& elem : list) {
lua_pushstring(L, elem->CategoryBrandName);
lua_rawseti(L, 1, counter++);
lua_pop(L, 1);
lua_pushstring(L, elem->CategoryName);
lua_rawseti(L, 2, counter++);
lua_pop(L, 1);
}

了解堆栈上的内容和不内容始终是一个好主意。节省一些内存不仅可以提高性能,还可以避免由于(Lua(堆栈溢出而导致的潜在问题。

最后一个细节:在Lua中不需要;,除非你在一行中有两个语句print('more readable'); print('like this'),否则使用它们被认为是不好的风格。

如果有人正在寻找类似的东西,这是我使用 lua_createtable 进行管理的方式。它按预期工作,可能需要一些改进的想法。

int GetCategory(lua_State* L)
{   
int counter = 1;
int MaxListSize = 2;
auto Categories = sManagerMgr->GetAll();
lua_createtable(L, Categories.size(), 0);
for (auto& elem : Categories)
{
vector<string> list;
list.reserve(MaxListSize);
list.emplace_back(elem->CategoryBrandName);
list.emplace_back(elem->CategoryName);
Macro::Push(L, counter); // custom
lua_createtable(L, 0, MaxListSize);
for (int i = 1; i <= MaxListSize; i++)
{
Macro::Push(L, list.at(i - 1)); // custom
lua_rawseti(L, -2, i);
}
lua_settable(L, -3);
list.clear();
counter++;
}
return 1;
}

将产生类似于

local Group = 
{ 
[1] = { "elem->CategoryBrandName[1]", "elem->CategoryName[2]" },
[2] = { "elem->CategoryBrandName[1]", "elem->CategoryName[2]" }
--etc
};