tolua++:向 C++ 添加 lua 函数已导出到 Lua 的类
我正在使用 tolua++ 将一些 C++ 类导出到 Lua。
我的理解是每个类都是在 lua 端“实现”的,作为一个 lua 表。我想知道是否有可能因此向C++对象表添加新的(Lua)方法,以便我可以达到向C++类添加新方法的效果。
假设我正在导出 Foobar 类。
Foobar 的方法将在 Lua 中按如下方式访问:
Foobar:method1()
Foobar:method2()
...
等等
我希望能够向 Foobar 表“对象”添加新方法(newmethod1 和 newmethod2)
我有以下问题:
- 是否可以“扩展”以我上面描述的方式导出的 C++ 对象的功能?
- 如何向 Foobar 表添加新函数? (我对Lua比较陌生)
I am using tolua++ to export some C++ classes to Lua.
My understanding that each class is 'implemented' on the lua side, as a lua table. I am wondering if it is possible therefore, to add new (Lua) methods to the C++ object table so that I can achieve the effect of adding new methods to the C++ class.
Assume that I am exporting the class Foobar.
Methods of Foobar will be accessisble in Lua as follows:
Foobar:method1()
Foobar:method2()
...
etc.
I would like to be able to add new methods (newmethod1, and newmethod2) to the Foobar table "object"
I have the following questions:
- Is it possible to 'extend' the functionality of an exported C++ object in the manner I described above?
- How would one go about add new functions to the Foobar table? (I am relatively new to Lua)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,可以向现有的导出类添加新方法。
Foobar 只是一个普通的表,因此您可以像任何其他表一样在其上附加方法。 [1]
现在您可以执行以下操作:
并且将显示
1 2
。[1] tolua++ 的工作方式有些奇怪。它为每个类创建一个主表,即您所看到的 Foobar,它包含该类的静态成员函数(例如
Foobar:new()
)和类的实例成员函数(例如Foobar:method1()
可能是什么)。 lua中的:
运算符是语法糖;a:b()
转换为ab(a)
。这意味着当您调用Foobar:new()
时,它会转置为Foobar:new(Foobar)
,因此self
参数是 Foobar桌子。当您创建 Foobar 类型的对象,然后对其调用 method1() 时,self
将成为该对象。因此,向 Foobar 表添加新方法method3
将允许您执行Foobar:method3()
和obj = Foobar:新的(); obj:method3()
,尽管前者会报错。因此,这个答案仅适用于tolua++。编辑:以解决评论
让我在这里使用实际代码。因此,假设我们有一个 Foobar 的类声明,如下所示:
当 tolua++ 为该类生成 lua 绑定代码时,它将为您提供一个包含以下方法的表
Foobar:new()< /code> 创建 Foobar 的实例。
instance:delete()
来销毁 Foobar 的实例。instance:method1()
调用以在实例上运行 method1。Foobar:method2()
来运行 method2。然而,tolua++ 实际上并不使用两个不同的表(一张用于应附加到类本身的方法,一张用于该类实例的方法)。相反,它将两者合并在一起,因此可以运行
Foobar:method1()
和instance:new()
...即使方法不应该是这样用过的。因此,静态方法的使用方式和实例方法的使用方式没有区别。从语法上来说,也是一样的。 (我个人认为这是 tolua++ 的问题。)如果您正在寻找示例,那么您应该如何从 lua 中调用这些函数:
Yes, It is possible to add new methods to an existing exported class.
Foobar is just a plain table, so you can attach methods onto it as you would any other table. [1]
and now you can do:
and
1 2
will be displayed.[1] tolua++ works somewhat oddly. It creates one main table for each class, which is what you see as Foobar, that holds both the static member functions of the class (e.g.
Foobar:new()
) and the instance member functions of the class (e.g. whatFoobar:method1()
likely is). The:
operator in lua is syntactic sugar;a:b()
is converted toa.b(a)
. This means when you callFoobar:new()
, it is transposed toFoobar:new(Foobar)
, thus theself
argument is the Foobar table. When you create an object of type Foobar, and then call method1() on it,self
will be that object. Thus, adding a new methodmethod3
to the Foobar table will allow you to doFoobar:method3()
andobj = Foobar:new(); obj:method3()
, although the former will give an error. Thus, this answer only works for tolua++.Edit: to address comment
Let me use actual code here. So let's assume we have a class declaration of Foobar that looks like:
When tolua++ generates the lua binding code for this class it's going to give you a table with the following methods
Foobar:new()
to create an instance of Foobar.instance:delete()
to destroy an instance of Foobar.instance:method1()
to run method1 on instance.Foobar:method2()
to run method2.However, tolua++ doesn't actually use two different tables (one for the methods that should be attached to the class itself and one for the methods of the instance of that class). Instead, it merges the two together, so it's possible to run
Foobar:method1()
andinstance:new()
... even though that's not how the methods should be used. Thus, there is no difference between how static methods are used and how instance methods are used. Syntactically, it's also the same. (I personally feel that this is a problem with tolua++.)If you're looking for an example, here is how you should be calling these functions from within lua: