为带有多个参数的函数创建SWIG类型图的更简单方法? [英] Simpler way to create a SWIG typemap for a function with multiple arguments?

查看:100
本文介绍了为带有多个参数的函数创建SWIG类型图的更简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's the C++ function that I want to wrap using SWIG.

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");
    }
    $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.

This way, I could successfully use my_func in Lua.

但是我想知道是否有比这更简单的解决方案,因为上述两个类型映射是相同的,但仅使用不同的名称.

But I wonder if there's any simpler solution than this since the above 2 typemaps are identical but only use different name.

如果我以后有一个使用3个t_string参数的C ++函数,那么我是否应该再添加一个使用不同名称的typemap? 还是会有更简单的解决方案?

Let's say if I later have a C++ function that takes 3 t_string arguments, then should I add one more typemap using different name? Or would there be any simpler solution?

推荐答案

您做错了.您正在为单个类型使用多参数类型映射.从t_string中删除变量名称(以及可选的括号).在有关类型映射的章节中有关模式匹配的部分中对此进行了详细说明.文档.

You're doing it wrong. You are using a multi-argument typemap for a single type. Remove the variable name (and optionally the parentheses) from t_string. This is explained in details in the section about pattern matching in the chapter for typemaps of the documentation.

%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);

这篇关于为带有多个参数的函数创建SWIG类型图的更简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆