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

时间:2012-10-09 06:23:16

标签: java c swig

我正在转换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”作为字符串返回,将“value”作为字符串返回(不要取消分配)

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

有人可以请我帮忙制作第一个吗?

1 个答案:

答案 0 :(得分:0)

假设您的函数声明存在于名为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而不是将其放在结构中,则会抛出异常)

相关问题