简体   繁体   中英

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

So I have code that looks like this:

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

using swig I get java code:

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

what I want is:

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

I get that the problem is that SWIG can't know that my array in Java is only going to be 2 elements, as java declarations are sizeless.

I've tried using carrays.i in the swig interface. I used the arrays_fuctions instruction but it didn't change the method signature.

My next idea is going to code an inline function, in the SWIG file, that takes two parameters for each struct and then acts as a proxy down to the real function.

Any better ideas?

You can do this with the existing "arrays_java.i" SWIG library file.

There's a macro inside that file called JAVA_ARRAYSOFCLASSES that can be used as:

%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]);

Which generates the following Java function:

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

Which is exactly what you're after! (Take a look under the hood if you're curious - it's all standard usage of typemaps).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM