gpt4 book ai didi

Java/C++ SWIG - 使用数组参数调用函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:09:58 26 4
gpt4 key购买 nike

所以我的代码看起来像这样:

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 声明是无大小的。

我试过在 swig 界面中使用 carrays.i。我使用了 arrays_fuctions 指令,但它没有更改方法签名。

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

有什么更好的主意吗?

最佳答案

您可以使用现有的“arrays_java.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)

这正是您所追求的! (如果您好奇的话,请看一看幕后情况 - 这都是类型映射的标准用法)。

关于Java/C++ SWIG - 使用数组参数调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com