java - 为通过参数返回的函数创建类型映射

标签 java c swig

我正在转换 C api > Java,并且我有以下函数原型(prototype)。

/*
 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);

我想按预期返回状态,将“类型”作为 int 返回,将“值”作为字符串返回(不被释放)

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

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

最佳答案

假设您的函数声明存在于名为 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,取而代之的是一个返回输出参数和调用结果的版本(不可修改)。

(如果您愿意,可以使用 %javaexception 而不是将其放在结构中,而是使返回状态为 0 导致抛出异常)

关于java - 为通过参数返回的函数创建类型映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12793973/

相关文章:

java - 如何在 java 和 spring 的 Controller 中创建 HashMap

java - Swig,将c指针转换为float[]

java - 如何在嵌入式tomcat中添加ServletContextListener

Java JNI C 程序适用于 Mingw32,但不适用于 Cygwin64

c - 如何通过 RPC 发送数组?

arrays - 初始化从指针目标类型中丢弃 ‘const’ 限定符

c - 如何使用 SWIG 在 C 中构建 Perl 哈希?

python - 如何为双指针结构参数应用 SWIG 类型映射

java - 使用命令行工具构建时如何添加 .jar 文件依赖项?

java - 具有不同查询参数类型的 Spring REST 端点