需要帮助理解 luabind 如何实例化类

发布于 2024-11-29 13:00:51 字数 1291 浏览 0 评论 0原文

假设我有一个这样的类:

class A
{
public:
A(){}
~A(){}
};

并通过 Luabind 将其暴露给 Lua,如下所示:

module(luaState) 
    [
        class_<A>("Foo")
        .def(constructor<>())
    ];

最后在这样的脚本中实例化它:

A = Foo();

此时 A 的实际“存在状态”是什么?

它是否在堆中的某个地方,并且 lua 在某个地方保留了对它的引用? (或 luabind::object?)

我有一种感觉,它只能是一个指针,如由 new 或等效对象分配的。

但是,我可以将函数绑定到接受引用的 lua,例如 lua_doSomething(A & a) 并且最终的结果将是一个实际的引用。 当然,我知道这很可能只是 luabind 将 a 作为 *a 传递,但我不知道这是否是如何发生的。

我问这个问题的原因是为了更好地理解和预测脚本中实例化的对象的生命周期。

那,我不确定所有权或生命周期是否会改变,而不是像上面那样将类暴露给lua,而是像这样使用

A * lua_CreateA()
{
return new A();
}

module(luaState) 
    [
        class_<A>("Foo")
    ];


module(luaState)
    [

    def("createA",&lua_CreateA)

    ];

A = createA();

:按照我到目前为止所理解的逻辑,这种情况需要我进行清理因为我是分配一个新对象的人,除非像这样为 luabind 进行的分配与使用绑定构造函数进行分配是一样的。

简而言之,我对对象生命周期和这里的东西真的很困惑...... 我用谷歌搜索了与此相关的关键字,但我只得到了类似的东西 http://www.gamedev.net/topic/525692-luabind-所有权和破坏/

这并不是我真正想知道的。 我想了解有关分配、实例化、生命周期等的幕后处理方式的具体方式。

Let's say I have a class like this:

class A
{
public:
A(){}
~A(){}
};

And expose it to Lua via Luabind like this:

module(luaState) 
    [
        class_<A>("Foo")
        .def(constructor<>())
    ];

And finally instantiate it in a script like this:

A = Foo();

What is the actual 'state of existence' of A at that point?

Is it somewhere in the heap, and lua keeps a reference to it somewhere?
(or a luabind::object?)

I have the feeling that it can only be a pointer, as in, allocated by new or equivalent.

However, I can bind functions to lua that accept references, like lua_doSomething(A & a) and what ends up in there will be an actual reference.
Granted I know this could very well just be luabind passing a as *a, but I have no idea if that's how it happens.

The reason I am asking this is to understand and predict the lifetime of objects instantiated in a script at little better.

That, and me being unsure if ownerships or lifetimes change if, instead of exposing the class to lua like above, I do it like this:

A * lua_CreateA()
{
return new A();
}

module(luaState) 
    [
        class_<A>("Foo")
    ];


module(luaState)
    [

    def("createA",&lua_CreateA)

    ];

And using it like

A = createA();

By the logic I understand so far, this case would require me to do the cleanup since I'm the one allocating a new object, unless assignment like this for luabind would be the same as doing it with the bound constructor.

In short, I'm really really confused about object lifetime and stuff here...
I googled for the keywords related to this, but I'm only getting stuff like
http://www.gamedev.net/topic/525692-luabind-ownership-and-destruction/

Which isn't really what I want to know.
I want to understand the concrete way how things are handled behind the scenes regarding allocation, instantiation, lifetime and all that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘人皮目录ツ 2024-12-06 13:00:51

Luabind 的对象生命周期非常简单。您无需了解 Luabind 内部所做的具体细节即可获得它。

Lua 拥有 Lua 直接创建的对象。因此,如果您在 Lua 中使用构造函数语法分配一个对象,则该对象归 Lua 所有。当该对象不再在Lua中被引用时,Lua GC就会收集它。

如果 Lua 通过任何其他方式获取对对象的引用,那么 Lua 并不拥有该对象,除非您使用 Luabind 的采用策略专门转移所有权。因此,如果您绑定一个返回对象给 Lua 的函数,这样您现在希望 Lua 决定该对象何时存活或死亡,那么您需要使用采用策略。

最后一段仅适用于实际引用(返回指针和引用类型)。如果给 Lua 一个对象的副本(通过非引用或指针返回值),那么 Lua 将拥有该对象的副本

Object lifetime is very simple with Luabind. You don't need to understand the gory details of what Luabind does internally to get it.

Lua owns the objects that are created by Lua directly. So if you allocate an object with constructor syntax in Lua, that object is owned by Lua. When the object is no longer referenced in Lua, the Lua GC will collect it.

If Lua gets a reference to an object by any other means, then Lua does not own that object unless you specifically transfer ownership using Luabind's adopt policy. Therefore, if you are binding a function that returns an object to Lua, such that you now expect Lua to decide when that object is alive or not, then you need to use the adopt policy.

The last paragraph only applies to actual references (returning pointers and reference types). If Lua is given a copy of an object (via a non-reference or pointer return value), then Lua will own that copy of the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文