Luabind 中的自定义构造函数
我正在使用 Luabind 将 C++ API 绑定到 Lua。我有一些对象无法直接创建,而必须在另一个线程上创建。我目前通过定义一个名为 create
的“静态”成员来处理此问题,该成员在创建对象之前一直生成:
luabind::class_<Foo>("Foo")
.scope
[
luabind::def("create", &someCreateMethod, luabind::yield)
]
这可行,但缺点是使客户端 API 变得复杂。对于这些类,您无法正常创建它们(例如local f = Foo()
),而是需要执行local f = Foo.create()
。
是否可以定义一个 Luabind 构造函数,它实际上并不调用 C++ 构造函数,而是返回构造对象的另一个函数(同时可以产生结果)?我尝试定义 __init
和 __call
的绑定(后者在 scope
下,在类上定义它,而不是在它的实例上),但这两种方法我都没有成功。
I'm using Luabind to bind a C++ API to Lua. I have some objects that cannot be created directly, but rather must be created on another thread. I'm currently handling this by defining a "static" member called create
that yields until the object is created:
luabind::class_<Foo>("Foo")
.scope
[
luabind::def("create", &someCreateMethod, luabind::yield)
]
This works, but has the disadvantage of complicating the client API. For these classes, you cannot create them normally (e.g. local f = Foo()
), but instead need to do local f = Foo.create()
.
Is it possible to define a Luabind constructor that doesn't actually call the C++ constructor, but instead another function that returns the constructed object (and can yield in the meantime)? I've tried defining bindings for __init
and __call
(the latter under a scope
, to define it on the class, not its instances), but I didn't have success with either approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Luabind 中的构造函数必须是实际的 C++ 类构造函数。所以你只需要处理 API 的一些奇怪问题。
如果您感兴趣的只是能够使用
Foo
作为构造函数方法,那么您可以这样做。将您的 C++ 类Foo
注册为FooLua
到 Lua。然后,注册这个someCreateMethod
,不是作为FooLua
的成员,而是作为一个名为Foo
的 Lua 自由函数。因此,对于用户而言,Foo
是Lua类Foo
的构造函数。现在,这将限制您为
Foo
提供其他静态属性(如成员等)的能力。但是您可以通过使用一些直接的 Lua API 编码来实现这一点。您可以创建一个空表Foo
并为其创建一个元表,将__index
和__newindex
调用转发到FooLua
。同样,您可以重写此元表的__call
以将构造转发给Foo.create
。Constructors in Luabind must be actual C++ class constructors. So you'll just have to deal with the slight API weirdness.
If all you're interested in is the ability to use
Foo
as a constructor method, then you can do this. Register your C++ classFoo
asFooLua
to Lua. Then, register thissomeCreateMethod
, not as a member ofFooLua
, but as just a Lua free function calledFoo
. Thus, as far as the user is concerned,Foo
is a constructor for the Lua classFoo
.Now, this will inhibit your ability to give
Foo
other static properties, like members and so forth. But you could accomplish that by using some direct Lua API coding. You can create an empty tableFoo
and create a metatable for it that forwards__index
and__newindex
calls toFooLua
. Similarly, you can override this metatable's__call
to forward the construction toFoo.create
.虽然 luabind 没有提供定义自定义构造函数的直接方法,但实际上可以通过一些技巧实现:
这将允许您在类之后使用任何自由函数作为构造函数定义:
使用 luabind 的 deboostified 版本进行测试(https://github.com/decimad/luabind-deboostified),但它也应该适用于常规版本。
While luabind doesn't provide a straight-forward way of defining custom constructors, it is in fact possible with a bit of a hack:
This will allow you to use any free function as a constructor after the class definition:
Tested with the deboostified version of luabind (https://github.com/decimad/luabind-deboostified), but it should work with the regular version as well.