为通过参数返回的函数创建一个typemap

我正在转换C api> Java,我有以下函数原型。

/* Retrieves an individual field value from the current Line \param reader pointer to Text Reader object. \param field_num relative field [aka column] index: first field has index 0. \param type on completion this variable will contain the value type. \param value on completion this variable will contain the current field value. \return 0 on failure: any other value on success. */ extern int gaiaTextReaderFetchField (gaiaTextReaderPtr reader, int field_num, int *type, const char **value); 

我希望按预期返回状态,将“type”作为int返回,将“value”作为字符串返回(不要取消分配)

从文档中我发现您创建了几个可以保留返回值的结构。

有人可以请我帮我做第一个吗?

假设您的函数声明存在于名为header.h的文件中,您可以执行以下操作:

 %module test %{ #include "header.h" %} %inline %{ %immutable; struct FieldFetch { int status; int type; char *value; }; %mutable; struct FieldFetch gaiaTextReaderFetchField(gaiaTextReaderPtr reader, int field_num) { struct FieldFetch result; result.status = gaiaTextReaderFetchField(reader, field_num, &result.type, &result.value); return result; } %} %ignore gaiaTextReaderFetchField; %include "header.h" 

这隐藏了“真实的” gaiaTextReaderFetchField ,而是替换了一个版本,该版本在(不可修改的)结构中返回两个输出参数和调用结果。

(您可以将返回状态设置为0,因为如果您更愿意使用%javaexception而不是将其放在struct中,则会抛出exception)