gpt4 book ai didi

c++ - 存储和返回 Lua 用户数据

转载 作者:搜寻专家 更新时间:2023-10-31 02:20:26 24 4
gpt4 key购买 nike

我在 C++ 中有以下类

class B; 

class A {
B* GetB();
void SetB(B*& b) { _b = b;};
private:
B* _b;
}

以及部分lua绑定(bind)代码:

int A::setB(lua_State* L) {
A* a = checkA(L,1) // Macro for luaL_checkudata
B* b = checkB(L,2) // again similar macro
a->SetB(b);
return 0;
}

int A::getB(lua_State* L) {
A* a = checkA(L,1) // Macro for luaL_checkudata
B* b = a->GetB();
// how do i return the already created userdata for this B* instance?
// right now I am doing
B** bp = (B**)lua_newuserdata(L, sizeof(B*));
*bp = b;

luaL_getmetatable(L, "B");
lua_setmettable(L, -2);

return 1;
}

我想将这些作为用户数据包装在 Lua 中,这样我就可以做类似的事情:

local a = a.new()    -- create new userdata
local b = b.new() -- create new userdata
a:SetB(b) -- associate B with A
local b2 = a:GetB() -- get the B associated with A back, and stored as b2

当我打印 bb2 的地址时,我得到了两个唯一的地址,这是有道理的,因为我调用了 lua_newuserdata。但理想情况下,我希望它返回相同的用户数据,因为它们指向相同的内存块。如何做到这一点?

我希望 Lua 负责内存,因此它会在垃圾回收时被正确删除。所以我不知道是否可以使用轻型用户数据。

最佳答案

您可以使用用户值表在 Lua 中镜像 C++ 对象的成员变量。这样你的方法就变成了:

int A::setB(lua_State* L) {
A* a = checkA(L,1) // Macro for luaL_checkudata
B* b = checkB(L,2) // again similar macro
a->SetB(b);
// if you are paranoid about consistency, you should preallocate the
// the uservalue table slot for "_b", so that the following code
// can't fail during memory allocation
lua_getuservalue(L, 1); // use lua_getfenv in Lua 5.1
lua_pushvalue(L, 2); // duplicate B userdata on stack top
lua_setfield(L, -2, "_b"); // store it in the uservalue table
return 0;
}

int A::getB(lua_State* L) {
A* a = checkA(L,1) // Macro for luaL_checkudata
lua_getuservalue(L, 1);
lua_getfield(L, -1, "_b");
return 1;
}

这有一个额外的好处,即当 B 用户数据被 A 对象引用时,它不会被收集(导致悬空指针)。

通过在每个之后使用 lua_setuservalue()(lua_setfenv() for Lua 5.1)确保所有 A 对象实际上一个用户值表lua_newuserdata() 用于 A 对象。

您还可以添加检查以确保 C++ 端和 Lua 端仍然同步(以防您继续使用 C++ 中的接口(interface)),但是如果您发现 B 对象未反射(reflect)在用户值中表,除了引发错误外,您无能为力,因为您不知道它是如何到达那里的,也不知道它的所有者是谁。

编辑:

如果你想管理 B 指针的容器,你可以通过将所有用户数据存储在一个表中(用户值表本身,或其中的一个字段)并镜像所有容器操作来实现,但这很容易出错.作为替代方案,您可以使用用户值表作为从 C++ 指针值(lightuserdata)到 Lua 对象(完整用户数据)的映射。此映射将使 B 用户数据保持事件状态,您可以简单地通过指针值查找它们。每当添加或删除 B 对象时,您只需更新映射。例如

int A::pushB(lua_State* L) {
A* a = checkA(L,1)
B* b = checkB(L,2)
lua_getuservalue(L, 1);
lua_pushlightuserdata(L, (void*)b);
lua_pushvalue(L, 2);
lua_rawset(L, -3);
a->PushB(b);
return 0;
}

int A::popB(lua_State* L) {
A* a = checkA(L,1)
B* b = a->PopB();
lua_getuservalue(L, 1);
lua_pushlightuserdata(L, (void*)b);
lua_rawget(L, -2); // push full userdata
if (!a->ContainsB(b)) {
// remove reference only if this b isn't in the container anymore
// if done wrong b may be collected too soon (while A still has
// a pointer stored), or too late (when object a gets collected)
lua_pushlightuserdata(L, (void*)b);
lua_pushnil(L);
lua_rawset(L, -4);
}
return 1;
}

关于c++ - 存储和返回 Lua 用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32734674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com