如何在SWIG中从C指针生成Java数组

如何在SWIG中从C指针生成Java数组,java,c,java-native-interface,swig,Java,C,Java Native Interface,Swig,给出了C代码: typedef struct _B { /* something */ } B; typedef struct _A { int numBs; /* The count of Bs in the array bellow */ B *b; } A; 我想在Java中以数组的形式访问A.b A a = new A(); B[] b = a.getB(); 你知道怎么用SWIG来做吗?我和凯瑞玩得很开心,但没有成功。考虑 B*B并不总是必须是一个数组,只有在这

给出了C代码:

typedef struct _B {
 /* something */
} B;

typedef struct _A {
    int numBs; /* The count of Bs in the array bellow */
    B *b;
} A;
我想在Java中以数组的形式访问A.b

A a = new A();
B[] b = a.getB();

你知道怎么用SWIG来做吗?我和凯瑞玩得很开心,但没有成功。考虑<代码> B*B并不总是必须是一个数组,只有在这个特定的结构中。

我们可以用SWIG来做。棘手的部分是,正如您所看到的,SWIG倾向于假设

B*
是指向
对象的指针,而不是某个数组的开头

由于数组包含许多非基本实例,每个实例都需要创建一个Java对象,该对象是C数组中相应
B
实例的代理。您可以在两个位置生成它们。您可以编写一些C来创建每个
对象
,并将其粘贴到
jobjectArray
中,或者编写一些Java来利用这样一个事实,即SWIG可以轻松地为任何特定成员生成特定代理。我认为后一种解决方案更简单、更干净,因此我用这种方法做了一个示例:

%module test

%ignore A_::b; // We will wrap this another way
%typemap(javacode) A %{
  public B[] getB() {
    B[] ret = new B[getNumBs()];
    for (int i = 0; i < ret.length; ++i) {
      ret[i] = getB(i);
    }
    return ret;
  }
%}

// Or %include etc.
%inline %{
typedef struct B_ {
 /* something */
} B;

typedef struct A_ {
    int numBs; /* The count of Bs in the array bellow */
    B *b;
} A;
%}

%javamethodmodifiers A_::getB(size_t pos) "private";
%extend A_ {
  // This defaults to non-owning, which is exactly what we want
  B *getB(size_t pos) {
    assert(pos < $self->numBs); // TODO: real error handling
    return $self->b+pos;
  }
}

关键是因为我们只忽略了
A
的特定成员,它不会影响
B*B

使用原始地址创建NIO直接字节缓冲区,然后使用NIO缓冲区方法作为数组访问内存。您好,谢谢。你能举例说明如何实现这个(SWIG接口文件)吗?我能得到的最好结果是
bb=a.getB()
,而不是
bb[]B=a.getB()