使用 SWIG 简单地创建多参数函数的 typemap 的方法是什么?

以下为我想使用 SWIG 封装的 C++ 函数。

static void my_func(t_string name, t_string value)
{
    do_something(name, value);
}

这里是 SWIG 的 typemaps。

%typemap(in) (t_string name)
{
    if (!lua_isstring(L, $input)) {
      SWIG_exception(SWIG_RuntimeError, "argument mismatch: string expected");
    }
    $1 = lua_tostring(L, $input);
}

%typemap(in) (t_string value)
{
    if (!lua_isstring(L, $input)) {
      SWIG_exception(SWIG_RuntimeError, "argument mismatch: string expected");
    }
    $1 = lua_tostring(L, $input);
}

我能够成功地在 Lua 中使用 my_func

但我想知道是否有任何简单的解决方案,因为上面两个 typemaps 是相同的,只是使用不同的名称。

假设我以后有一个 C++ 函数需要使用 3 个 t_string 参数,那么我应该添加一个使用不同名称的 typemap 吗? 还是有更简单的解决方案?

点赞
用户1944004
用户1944004

你做错了。你正在使用多参数 typemap 来处理单个类型。从 t_string 中删除变量名(和括号,如果需要)。关于模式匹配更详细的说明在文档的 typemaps 章节中。

%module typemaptest

%typemap(in) t_string
{
    if (!lua_isstring(L, $input)) {
      SWIG_exception(SWIG_RuntimeError, "argument mismatch: string expected");
    }
    $1 = lua_tostring(L, $input);
}

void my_func(t_string name, t_string value);
2018-06-16 22:54:47