将向量传递给<struct> Lua 表
Passing a Vector<struct> to Lua table
我想通过发送一个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
};
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- C++Union/Struct位域的实现和可移植性
- EASTL矢量<向量<int>>连续的
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 错误:"Left of getValue must have class/struct/union"
- 在 SVM-Struct 中未定义对 sqrt 的引用(已使用 -lm)
- 数据类型"struct seq<0, 1, 2>{}"含义是什么?
- 附加使用 Struct 作为"multikey"并将 std::vector 用作映射值的映射
- QML/C++/QObject Struct Copy
- 如何在 cpp 中解压缩数字,如果它们是使用 struct.pack(fmt, v1, v2, ..) 打包在 pyth
- Msgpack能否提供更好的性能和与Python的struct.pack()相同的功能?
- 将向量传递给<struct> Lua 表
- MPI_DOUBLE_INT and C++ struct
- C++ 中的"template <typename From, typename Tag> struct Alias;"是什么?
- 如何使用Struct打印交换函数
- 返回类型为struct的函数每次调用时都不返回任何内容
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一