[英]How to create SWIG typemap for function that takes and returns 2 tables
這是我的 SWIG 類型圖:
%apply (float *INOUT, int) {(float *io1, int n1)};
%apply (float *INOUT, int) {(float *io2, int n2)};
這是我的功能:
void process(float *io1, int n1, float *io2, int n2)
{
for (int i = 0; i < n1; ++i)
{
io1[i] = io1[i] * 0.1;
io2[i] = io2[i] * 0.1;
}
}
我希望process
函數采用 2 個表並返回 2 個表。
在 Lua 中, process
函數似乎返回 2 個表,但它只返回從第一個參數傳遞的相同的 2 個表。
例如在 Lua 中,當我運行以下命令時:
local a, b = {3}, {4}
local c, d = process(a, b)
print(c[1], d[1])
我得到的結果:
0.3 0.3
但我希望:
0.3 0.4
我應該如何更改 SWIG 類型映射以使其按預期工作?
我無法使用以下最小示例重現您的問題。
test.i
%module example
%{
#include "test.h"
%}
%include <typemaps.i>
%apply (float *INOUT, int) {(float *io1, int n1)};
%apply (float *INOUT, int) {(float *io2, int n2)};
%include "test.h"
test.h
#pragma once
void process(float *io1, int n1, float *io2, int n2);
test.c
#include "test.h"
void process(float *io1, int n1, float *io2, int n2) {
for (int i = 0; i < n1; ++i) {
io1[i] = io1[i] * 0.1;
io2[i] = io2[i] * 0.1;
}
}
test.lua
local example = require("example")
local a, b = {3}, {4}
local c, d = example.process(a, b)
print(c[1], d[1])
然后我編譯並運行使用
$ swig -lua test.i
$ cc -fPIC -shared -I /usr/include/lua5.3/ test_wrap.c test.c -o example.so
$ lua5.3 test.lua
0.30000001192093 0.40000000596046
小數點后第 7 位的垃圾值源於將float
提升為默認為double
的lua_Number
。
無視這一點,我看到了預期的0.3 0.4
。 這意味着錯誤必須在您未顯示的某些代碼中。 確保在解析process
原型之前%apply
映射,即在上面的例子中注意%apply
如何在%include "test.h"
之前%include "test.h"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.