小编典典

为通过参数返回的函数创建类型图

java

我正在转换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);

我想按预期返回状态,以整数形式返回“类型”,以字符串形式返回“值”(不被释放)

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

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


阅读 349

收藏
2020-11-30

共1个答案

小编典典

假设函数声明存在于一个名为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不是将其放置在结构中)

2020-11-30