首页 > 解决方案 > Lua SWIG 基础知识

问题描述

我尝试使用以下提供的说明来实现基本的矢量类操作:

http://swig.org/Doc1.3/SWIG.html#SWIG_adding_member_functions

我有以下.i:

%module mymodule
%{
    typedef struct
    {
        float x,y,z;
    } Vector3f;
%}

typedef struct
{
    float x,y,z;    
} Vector3f;

%extend Vector3f {
    Vector3f(float x, float y, float z) {
    Vector3f *v;
    v = (Vector3f *) malloc(sizeof(Vector3f));
    v->x = x;
    v->y = y;
    v->z = z;
    return v;
}
~Vector3f() {
    free($self);
}
void print() {
    printf("Vector [%f, %f, %f]\n", $self->x,$self->y,$self->z);
}
};

现在我的问题是如果我在 Lua 中调用以下代码:

print(mymodule)
local v = Vector(3,4,0)
v.print()

--By the way is there an equivalent in Lua?
--del v 

我得到以下输出:

table: 0x000001F9356B1920
attempt to call global 'Vector' (a nil value)

显然,当我第一次打印表地址时,模块已正确加载,但我无法创建向量......我也尝试调用模块方法,就像mymodule:Vector(1,2,3)仍然产生错误一样。我在这里想念什么?

我想要的只是生成一个新的Vector并让 GC 使用 ~Vector3f() 方法销毁它。我应该修改什么才能使这个机制起作用?

标签: c++luascriptingswig

解决方案


SWIG 将自动__gc从析构函数生成元方法。原则上,您的类甚至不需要自定义析构函数,默认的析构函数就可以了。

此外,SWIG 不需要了解函数的所有实现细节,签名完全足以生成包装器代码。这就是为什么我将Vector3f结构移动到文字 C++ 部分(也可以在头文件中)并且只重复签名的原因。

与其拥有一个print成员函数,为什么不添加Vector3f与 Lua 函数一起使用的print()功能呢?只需编写一个__tostring返回对象的字符串表示的函数。

test.i

%module mymodule
%{
#include <iostream>

struct Vector3f {
    float x,y,z;
    Vector3f(float x, float y, float z) : x(x), y(y), z(z) {
        std::cout << "Constructing vector\n";
    }
    ~Vector3f() {
        std::cout << "Destroying vector\n";
    }
};
%}

%include <std_string.i>

struct Vector3f
{
    Vector3f(float x, float y, float z);
    ~Vector3f();
};

%extend Vector3f {
    std::string __tostring() {
        return std::string{"Vector ["}
            + std::to_string($self->x) + ", "
            + std::to_string($self->y) + ", "
            + std::to_string($self->z) + "]";
    }
};

test.lua

local mymodule = require("mymodule")
local v = mymodule.Vector3f(3,4,0)
print(v)

编译和运行的示例工作流:

$ swig -lua -c++ test.i
$ clang++ -Wall -Wextra -Wpedantic -I/usr/include/lua5.2/ -fPIC -shared test_wrap.cxx -o mymodule.so -llua5.2
$ lua test.lua
Constructing vector
Vector [3.000000, 4.000000, 0.000000]
Destroying vector

推荐阅读