I am using Swig to create a Java interface. Basically, in C++ I have template (
template<class T> class TSizedArray
{
int GetLength();
T* GetElements() const;
...
}
class Producer
{
TSizedArray<long> GetLongs();
...
}
This template is used as return value of some functions of API I need to export to Java via SWIG. However, in Java we want to use native Java arrays instead of this simple wrapper:
Java:
long[] result = Producer.GetLongs();
I am not sure how to achieve this with SWIG. The template has multiple instantiations, and is used as input and output of multiple methods in different classes. I was able to use:
%typemap(jstype) TCustomElementList "long[]"
to get the generated Java interface as long[] GetLongs(), but the generated implementation creates new object of type TCustomElementList.
Thanks for any ideas.
You’re on the right lines with your typemap, but you need a few more than just that one:
(This interface ran through SWIG fine, but hasn’t been tested further).
You need to set the return types for both the Proxy class and the JNI class (jtype and jstype typemaps). You also need to instruct the proxy to pass through what the JNI code returns (javaout typemap). Then you need provide a bit of JNI glue code that gets inserted to make a new Java array in the C++ side (out typemap).