c++ - 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() 方法销毁它。我应该修改什么才能使这个机制起作用?
解决方案
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
推荐阅读
- twilio - 当设备在不同的网络上时如何使 webrtc 视频通话正常工作
- docker - 如何在没有 Docker for Desktop 的情况下从 Windows SSH 到在 WSL2 下运行的容器
- google-apps-script - 如何自动调整图像大小以适合 Google 表格中的单元格?
- javascript - 递归中的一元运算符前缀执行未按预期工作
- cassandra - 启用来自其他主机的 cassandra 访问
- mysql - 父母与孩子 MySQL
- xml - powershell核心错误:未加载指定的模块'RPC-Client'
- postgresql - 无法在 RDS Postgres 中创建外部数据包装器
- postgresql - 尝试设置一个用户访问另一个用户对象时出现“必须是角色成员”错误
- c++ - VMMap 报告 ~100 GB 分配