1

我正在转换 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”作为字符串返回(不被释放)

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

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

4

1 回答 1

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不是将其放入结构中)

于 2012-10-12T20:59:48.670 回答