4

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?

3
  • 1
    Well, in C++, this line bool doSomething( unsigned int x, const myStruct1 typeOne[2], myStruct2 typeTwo[2]) is the same as bool doSomething( unsigned int x, const myStruct1* typeOne, myStruct2* typeTwo) In other words that [2] doesn't buy you anything, as C++ treats those as pointers to myStruct1 and myStruct2. Commented Aug 5, 2014 at 21:02
  • Cool. Still need to figure out the array problem from the Java side. Commented Aug 5, 2014 at 21:13
  • 1
    How about this: doSomething(unsigned int x, const myStruct1 (&typeOne)[2], myStruct2 (&typeTwo)[2]); Don't know if SWIG recognizes this though -- worth a shot. Commented Aug 5, 2014 at 21:26

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.