开发者

SWIG-Lua question on class returning another class

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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜