Java/ c++ SWIG -调用函数与数组参数

Java/C++ SWIG - Calling function with array parameter

本文关键字:函数 数组 参数 调用 c++ SWIG Java      更新时间:2023-10-16

我的代码是这样的:

bool doSomething( unsigned int x, const myStruct1 typeOne[2], myStruct2 typeTwo[2] );

使用swig得到java代码:

public static boolean doSomething(long x, myStruct1 typeOne, myStruct2 type2){}

我想要的是:

public static boolean doSomething(long x, myStruct1[] typeOne, myStruct2[] type2){}

我得到的问题是,SWIG不能知道我的数组在Java中只有2个元素,因为Java声明是无大小的。

我试过使用回车。I在swig界面中。我使用了arrays_functions指令,但它没有改变方法签名。

我的下一个想法是在SWIG文件中编写一个内联函数,它为每个结构体接受两个参数,然后充当实际函数的代理。

有更好的主意吗?

您可以使用现有的"arrays_java. xml "。i" SWIG库文件。

文件中有一个宏叫做JAVA_ARRAYSOFCLASSES,可以用作:

%module test
%include <arrays_java.i>
JAVA_ARRAYSOFCLASSES(myStruct1);
JAVA_ARRAYSOFCLASSES(myStruct2);
struct myStruct1 {};
struct myStruct2 {};
bool doSomething(unsigned int x, const myStruct1 typeOne[2], myStruct2 typeTwo[2]);

生成以下Java函数:

public static boolean doSomething(long x, myStruct1[] typeOne, myStruct2[] typeTwo)

这正是你想要的!(如果你好奇的话,可以看看它的底层——这都是typemaps的标准用法)。