这是我想使用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类型图的更简单方法?
发布评论