使用 SWIG 从 Java 访问结构中的指针数组 [英] Accessing an array of pointers within a structure from Java with SWIG

查看:51
本文介绍了使用 SWIG 从 Java 访问结构中的指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

typedef struct {
    char * content;
} Boo;

typedef struct {
    Boo **data;
    int size;
} Foo;

我想在 Java 中使用 SWIG 将 Boo ** 数据 转换为带有 Boo 元素 (Boo[]) 的数组.然后读取数组(我不想从 Java 代码编辑、删除和创建一个新数组).在 SWIG 文档中描述了如何使用 carrays.iarray_functions 执行此操作,但结构的成员 data 必须是 类型嘘*.我的问题有解决方案吗?

I want to convert Boo ** data to an array with Boo elements (Boo[]) in Java with SWIG. And then to read the array (I don't want to edit,delete and create a new array from Java code). In the SWIG documentation is described how to do this with carrays.i and array_functions, but the struct's member data must be of type Boo*. Is there a solution of my problem?

赶时间忘记写了想用SWIG生成Java类来配合C结构.

I've hurried and I've forgotten to write that I want to generate Java classes with SWIG to cooperate with C structures.

推荐答案

解决方案非常简单.只需在 swig 界面中使用:

The solution is very easy. Just use in a swig interface:

%include <carrays.i>
%array_functions(Boo *, boo_array);

然后从java访问:

SWIGTYPE_p_p_Boo results = foo.getData();
for(int i = 0; i < foo.getSize(); i++) {
    Boo booResult = foo.boo_array_getitem(results, i);
}

检索数组的内容.

这篇关于使用 SWIG 从 Java 访问结构中的指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆