If I have a C function (see below) that returns a unsigned char pointer to an array. How would you instruct SWIG to bind to the Java ArrayList data type for getFoo(). I'm not sure an ArrayList is possible, maybe an array (String[] in this case). I want to keep the Java return type of getFoo as generic as possible since the C getFoo may return an array of int, double, char..etc.
unsigned char * getFoo(int32_t *length){
static unsigned char foo[44];
foo[0]='a';
foo[1]='b';
foo[2]='c';
foo[3]='d';
return foo;
}
解决方案
The most generic answer is to expose the result as an unbouned C array in Java:
%module test
%include "carrays.i"
%array_class(unsigned char, ucArray);
unsigned char *getFoo();
This generates a class, called ucArray, which has gets and sets for a specific index. It's unbounded, with the same semantics as C arrays, but lightweight and usable. Since it's not bounded you can invoke Undefined Behaviour from Java, and it has no bounds which makes it impossible to use as an Iterator or Collection.
If your array is NULL/0 terminated it would be reasonably easy to use %extend to implement the Java Iterable interface too, rough, untested example:
%typemap(javainterfaces) ucArray "java.lang.Iterable"
%typemap(javacode) ucArray %{
public java.util.Iterator iterator() {
return new java.util.Iterator() {
private int pos = 0;
public Short next() {
// Mmmm autoboxing!
return getitem(pos++);
}
public void remove() {
throw new UnsupportedOperationException();
}
public boolean hasNext() {
// Check if we're at the end. A NULL/0 element represents the end
return getitem(pos) != 0;
}
};
}
%}
This needs to go somewhere before the %array_class macro gets called. It says that our ucArray class will implement Iterable and then implements that interface using an anonymous inner class in the iterator() (the only method of the Iterable interface) method to implement the Iterator interface.