tolua++:向 C++ 添加 lua 函数已导出到 Lua 的类

发布于 2024-10-11 14:29:41 字数 436 浏览 6 评论 0原文

我正在使用 tolua++ 将一些 C++ 类导出到 Lua。

我的理解是每个类都是在 lua 端“实现”的,作为一个 lua 表。我想知道是否有可能因此向C++对象表添加新的(Lua)方法,以便我可以达到向C++类添加新方法的效果。

假设我正在导出 Foobar 类。

Foobar 的方法将在 Lua 中按如下方式访问:

Foobar:method1()
Foobar:method2()
...

等等

我希望能够向 Foobar 表“对象”添加新方法(newmethod1 和 newmethod2)

我有以下问题:

  1. 是否可以“扩展”以我上面描述的方式导出的 C++ 对象的功能?
  2. 如何向 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:

  1. Is it possible to 'extend' the functionality of an exported C++ object in the manner I described above?
  2. How would one go about add new functions to the Foobar table? (I am relatively new to Lua)

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

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

发布评论

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

评论(1

埋葬我深情 2024-10-18 14:29:41

是的,可以向现有的导出类添加新方法。

Foobar 只是一个普通的表,因此您可以像任何其他表一样在其上附加方法。 [1]

Foobar["extra_method"] = function (self, arg1, arg2) print(arg1, arg2) end

现在您可以执行以下操作:

Foobar:extra_method(1,2)

并且将显示 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 的类声明,如下所示:

class Foobar {
  public:
    void method1();
    static void method2();
};

当 tolua++ 为该类生成 lua 绑定代码时,它将为您提供一个包含以下方法的表

  • new() - 调用为 Foobar:new()< /code> 创建 Foobar 的实例。
  • delete() - 调用 instance:delete() 来销毁 Foobar 的实例。
  • method1() - 作为 instance:method1() 调用以在实例上运行 method1。
  • method2() - 调用 Foobar:method2() 来运行 method2。

然而,tolua++ 实际上并不使用两个不同的表(一张用于应附加到类本身的方法,一张用于该类实例的方法)。相反,它将两者合并在一起,因此可以运行 Foobar:method1()instance:new()...即使方法不应该是这样用过的。因此,静态方法的使用方式和实例方法的使用方式没有区别。从语法上来说,也是一样的。 (我个人认为这是 tolua++ 的问题。)

如果您正在寻找示例,那么您应该如何从 lua 中调用这些函数:

obj = Foobar:new()
print( obj:method1() )
obj:delete()

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]

Foobar["extra_method"] = function (self, arg1, arg2) print(arg1, arg2) end

and now you can do:

Foobar:extra_method(1,2)

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. what Foobar:method1() likely is). The : operator in lua is syntactic sugar; a:b() is converted to a.b(a). This means when you call Foobar:new(), it is transposed to Foobar:new(Foobar), thus the self 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 method method3 to the Foobar table will allow you to do Foobar:method3() and obj = 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:

class Foobar {
  public:
    void method1();
    static void method2();
};

When tolua++ generates the lua binding code for this class it's going to give you a table with the following methods

  • new() - Call as Foobar:new() to create an instance of Foobar.
  • delete() - Call as instance:delete() to destroy an instance of Foobar.
  • method1() - Call as instance:method1() to run method1 on instance.
  • method2() - Call as 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() and instance: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:

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