我的 Swig 接口(interface)文件中有以下内容,用于连接小型 GUI 库的 .c/.h:
%{
void Widget_name_set( Widget *widget, char *name )
{
if( !name ) return;
WidgetSetName( widget, name );
}
char *Widget_name_get( Widget *widget )
{
return WidgetGetName( widget );
}
%}
struct Widget
{
%extend
{
char name[ 32 ];
Widget( void )
{
return WidgetNew();
}
~Widget()
{
if( $self ) WidgetDelete( $self );
}
void SetName( char *name )
{
Widget_name_set( $self, name );
}
char *GetName()
{
return Widget_name_get( $self );
}
}
};
然后我使用接口(interface)文件生成 Lua 包装器。如果我在 Lua 中调用以下内容,结果几乎与预期一致:
w = Widget();
w:SetName("test");
一切都好。但如果我这样做:
w = Widget();
w.SetName( nil, "test" );
由于参数为零,它显然崩溃了。有没有一种方法(使用 Swig 接口(interface)或在 Lua 中)抑制所有带点的调用,只保留带列的调用?这样对于用户来说会更容易,并且会避免我添加 1000 万次指针检查等...
最佳答案
Is there a way (either using the Swig interface or in Lua) to suppress all the call with a dot and only keep the one with column?
没有。就 Lua 而言,这只是语义糖。使用 .
表示仅访问该成员。使用 :
意味着当您调用该函数时,第一个参数是使用 :
访问的表。
你可能甚至无法从检查 Lua 反汇编中看出区别(尽管不可否认,这只是我的猜测);到那时,它可能已经转换成最终形式了。
关于c - Swig Lua 成员(member)生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10307258/