SWIG-Lua 关于类返回另一个类的问题

发布于 2024-09-02 18:25:33 字数 711 浏览 4 评论 0 原文

我正在具体阐述我之前的一个问题。

我有两个 C++ 类,我使用 SWIG 来包装它们。一个类中的方法可以返回指向另一个类的指针。我怎样才能让 Lua 将其视为不仅仅是一个用户数据?

更具体地说:

我用

class fruit
{
     int numberofseeds;
  //some other stuff about fruit constructors etc...
   public:
     getseedcount()
     {
        return numberofseeds;
     }
}

class tree
{
    fruit * apple; 
    public:
      //constructors and whatnot
    fruit * getfruit()
    {
         return apple;
    }

}

SWIG 包装了这两个类,这样我就可以在 Lua 中访问它们,

这样我就可以在 Lua 中获取对象x=pomona.tree(grannysmith)

我现在的问题是:如何安排,以便当我输入 y=x:getfruit() 时,我会得到一个 pomona:fruit 类型对象?我可以在哪里写一些行y:getseedcount()? 目前我得到的只是不可食用的用户数据。

I am concreting a question I had earlier.

I have two classes in C++ and I use SWIG to wrap them. A method in one class can return a pointer to the other class. How can I get Lua to see it as more than just a userdata?

More concretely:

I have

class fruit
{
     int numberofseeds;
  //some other stuff about fruit constructors etc...
   public:
     getseedcount()
     {
        return numberofseeds;
     }
}

class tree
{
    fruit * apple; 
    public:
      //constructors and whatnot
    fruit * getfruit()
    {
         return apple;
    }

}

I wrap these two class with SWIG so I can access them in Lua

So I can get in Lua the object x=pomona.tree(grannysmith).

My question now is: How can I arrange for things so that when I type y=x:getfruit() I will get a pomona:fruit type object? Where I can write something line y:getseedcount()?
At the moment all I get is userdata which not edible.

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

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

发布评论

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

评论(1

与君绝 2024-09-09 18:25:33

如果您的 SWIG .i 文件设置正确,您可以使用“:”运算符:

local y = x:getfruit()
local z = y:getseedcount()

请参阅 SWIG Lua 文档

如果这不起作用,您需要告诉 SWIG 如何使用 .i 文件中的类型映射将 Fruit* 输出参数转换为 Lua 表示形式。像这样的东西:

%typemap(out) fruit*
{
    swig_module_info* module = SWIG_GetModule(L);
    swig_type_info* typeInfo = SWIG_TypeQueryModule(module, module, "fruit *");

    SWIG_NewPointerObj(L, $1, typeInfo, 1);
}

If your SWIG .i file is set up correctly, you can use the ":" operator:

local y = x:getfruit()
local z = y:getseedcount()

See the "Classes" section (23.2.7) of the SWIG Lua documentation.

If that doesn't work you need to tell SWIG how to convert a fruit* out parameter to a Lua representation using a typemap in your .i file. Something like:

%typemap(out) fruit*
{
    swig_module_info* module = SWIG_GetModule(L);
    swig_type_info* typeInfo = SWIG_TypeQueryModule(module, module, "fruit *");

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