[英]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");
}
$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
。
但我想知道是否有比這更簡單的解決方案,因為上述兩個類型映射是相同的,但僅使用不同的名稱。
假設如果我以后有一個使用3個t_string
參數的C ++函數,那么是否應該使用其他名稱添加一個typemap? 還是會有更簡單的解決方案?
你這樣做是錯的。 您正在為單個類型使用多參數類型映射。 從t_string
刪除變量名稱(以及可選的括號)。 在文檔的類型映射一章中有關模式匹配的部分中對此進行了詳細說明。
%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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.