I am developing some SWIG-generated Java bindings for a C library. The library contains functions that take parameters of type void *. On the C side these would typically be passed as a pointer to an array of type float or int cast to void *. In the generated Java bindings, this results in methods that take parameters of type SWIGTYPE_p_void.
What is the best way to construct an array of floats/ints in the Java bindings so that they can be passed as type SWIGTYPE_p_void to these methods?
At the moment I am defining a helper function in my example.i file:
void *floata_to_voidp(float f[])
{
return (void *)f;
}
And then on the Java side doing something like this:
float foo[] = new float[2];
SWIGTYPE_p_void av = null;
// do something with foo
av = example.floata_to_voidp(foo);
example.myfunction(av);
This seems rather ugly, especially as I would also need an inta_to_voidp() etc in my SWIG interface file for each type conversion I want to support.
Is there a way to do this without helper functions and involving less extra code on the Java side to convert data types?
UPDATE (17/6/12): to give additional detail to the question: what I’m trying to do is take a set of C functions, with prototype int foo(const float *data, int N, const void *argv, float *result) and map them to methods on the Java side where an array of arbitrary type can be passed in as argv. Note that argv is const void * and not void *.
There’s an alternative to this answer, it’s very different and gives a more natural solution to this problem, closer to what you were looking for originally. The other suggestions were focused on adding overloads (tedious, manual) or making the
array_classes implement a common interface one way or another.What it overlooks is that
Objectis a good match forvoid*in Java most of the time. Even arrays in Java areObjects. This means if you have SWIG mapvoid*toObjectit’ll accept as inputs any arrays you might want to pass in. With a bit of care and some JNI we can then get a pointer to the start of that array to pass in to the function. Obviously we need to reject non arrayObjects with an exception though.We still end up writing some (private) helper functions to arrange extraction of the real underlying pointer and release it when done, but the nice thing about this solution is that we only have to do this once and then we end up with a typemap that can be used for any functions which take an array as
void*like this.I ended up with the following SWIG interface for this solution:
This implements it for two array types, there’s a small finite number and you could use fragments or macros to help with this too. Internally SWIG uses a
jlongto represent pointers. So for each array type we need a function that returns a pointer for a given array and another one to release it. These are private and part of the module class – nobody other than the module needs to know how this works.There’s then two functions which take the
Objectand useinstanceof(ugly, but arrays in Java don’t have any other common base or interface and generics don’t help) and call the correct function to get/release the pointers.With these then it’s just two typemaps to set up SWIG to use it for all
void *arrarguments. The jstype typemap instructs SWIG to useObjectforvoid*in these cases. The javain typemap arranges for a temporary local variable to hold the pointer (in along) and then for it to be used to make the call and to be cleaned up once the call has succeed or failed.