为具有多个参数的函数创建 SWIG 类型映射的更简单方法?

Simpler way to create a SWIG typemap for a function with multiple arguments?

这是我想使用 SWIG 包装的 C++ 函数。

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

这里是 SWIG 类型映射。

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

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

这样,我就可以在Lua中成功使用my_func了。

但我想知道是否有比这更简单的解决方案,因为上面的 2 个类型映射是相同的,只是使用了不同的名称。

假设我以后有一个接受 3 个 t_string 个参数的 C++ 函数,那么我是否应该使用不同的名称再添加一个类型映射? 或者会有更简单的解决方案吗?

你做错了。您正在为单个类型使用多参数类型映射。从 t_string 中删除变量名称(以及可选的括号)。这在文档的类型映射一章中有关 pattern matching 的部分中有详细解释。

%module typemaptest

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

void my_func(t_string name, t_string value);