java char指针数组,使用SWIG将C无符号字符指针绑定到Java ArrayList或集合结构

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值