为带有多个参数的函数创建SWIG类型图的更简单方法?

编程入门 行业动态 更新时间:2024-10-27 19:20:52
本文介绍了为带有多个参数的函数创建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类型图的更简单方法?

本文发布于:2023-07-30 16:28:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1251046.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   函数   参数   类型   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!