使用SWIG的C函数的JNI包装器 - 应该是什么类型图?

joj*_*jek 12 c java java-native-interface swig

我正在尝试在C中为以下函数创建JNI包装器:

int err = new_instance(const char* name, instance_t* instance);
Run Code Online (Sandbox Code Playgroud)

name- 输入,instance- 输出

int err = get_value(const instance_t instance, int *val);
Run Code Online (Sandbox Code Playgroud)

instance- 输入,val- 输出

其中instance_t定义为:

typedef void* instance_t;
Run Code Online (Sandbox Code Playgroud)

我完全迷失在Java的SWIG手册中,因为它不仅仅支持输入参数作为输出类型.我用Python包装器没有任何问题(如下所示).

在Java的情况下使用typemap的正确方法是什么?

// instance_t [argout]
%typemap(in, numinputs=0) instance_t* instance (instance_t temp = 0) {
    $1 = &temp;
}
%typemap(argout) instance_t *instance {
    %append_output(PyLong_FromLongLong((long long)* $1));
}
// instance_t [in]
%typemap(in) instance_t instance {
    $1 = (instance_t) PyLong_AsLongLong($input);
}
Run Code Online (Sandbox Code Playgroud)

Fle*_*exo 4

您可以通过多种不同的方式使用 SWIG 和 Java 来完成此操作。根据您在问题中显示的内容,我创建了以下标题来说明我的所有示例:

typedef void* instance_t;

int new_instance(const char* name, instance_t * instance);
int get_value(const instance_t instance, int *val);
Run Code Online (Sandbox Code Playgroud)

在界面中编写一些Java:

我们可以使用SWIG 库中的cpointer.i为我们提供编写 Java 重载所需的函数,该重载调用默认版本new_instance(我们将其设为私有,因为它成为实现细节)。

typedef void* instance_t;

int new_instance(const char* name, instance_t * instance);
int get_value(const instance_t instance, int *val);
Run Code Online (Sandbox Code Playgroud)

请注意,此示例可能会按原样泄漏,因为ptr.value()默认情况下是非拥有的。

在界面中编写一些C:

在下一个示例中,我们仅用 C 编写一个“重载”(但因为我假设您正在编写 C 而不是 C++,所以我们必须使用%renameC 来实现这项工作),特别是针对 SWIG 接口。该函数的原始版本被完全忽略,因为它对我们来说毫无用处。

%module test

%{
#include "test.h"
%}

// Hide the default new_instance
%ignore new_instance;
%include "test.h"
// Pretend our wrapper specific "overload" was called new_instance all along
%rename(new_instance) new_instance_overload;
// Don't leak our new instance
%newobject new_instance;

// Declare, define and wrap a special version of new_instance
%inline %{
    instance_t new_instance_overload(const char* name) {
        instance_t result = NULL;
        const int err = new_instance(name, &result);
        if (err) {
            // See later on/other Q for cross language exception example
        }
        return result;
    }
%}
Run Code Online (Sandbox Code Playgroud)

使用类型映射

我们实际上可以使用 Java 类型映射做一些与 Python 示例非常相似的事情,尽管这个过程更加复杂,因为 Java 具有强类型,我们需要尊重这一点。

这个解决方案也与我在同一基础问题上的旧答案基本相似,SWIGTYPE_p_void当底层 typedef 代替结构体的前向声明时,在 Java 中获得强类型(而不仅仅是 )会更加棘手,这会带来额外的复杂性void*

%module test

%{
#include "test.h"
%}

%include <cpointer.i>
// Have SWIG create a helper class for "pointer to pointer" type of handle
%pointer_class(instance_t, inst_ptr);
// Hide default version of new_instance
%javamethodmodifiers new_instance "private";

// Supply  Java version of new_instance now with useful method signature
%pragma(java) modulecode=%{
  public static SWIGTYPE_p_void new_instance(String name) {
    inst_ptr ptr = new inst_ptr();
    final int err = new_instance(name, ptr.cast());
    if (0!=err) {
      // throw or whatever
    }
    return ptr.value();
  }
%}

%include "test.h"
Run Code Online (Sandbox Code Playgroud)

我鼓励您查看围绕调用生成的代码,new_instance()以充分理解这些类型映射的作用。

就调用而言,get_valueinstance_t从上面的接口自动处理,并且int*arg out 需要与上面的示例类似地进行处理,或者使用仅包含一个元素的数组的技巧:

%module test

%{
#include "test.h"
%}

// Hide the default new_instance
%ignore new_instance;
%include "test.h"
// Pretend our wrapper specific "overload" was called new_instance all along
%rename(new_instance) new_instance_overload;
// Don't leak our new instance
%newobject new_instance;

// Declare, define and wrap a special version of new_instance
%inline %{
    instance_t new_instance_overload(const char* name) {
        instance_t result = NULL;
        const int err = new_instance(name, &result);
        if (err) {
            // See later on/other Q for cross language exception example
        }
        return result;
    }
%}
Run Code Online (Sandbox Code Playgroud)

然后您可以将其称为:

%module test

%{
#include "test.h"
%}

// Provide 'instance' class for strong typing (instead of void* semantics)
%rename(Instance) instance;
%nodefaultctor;
struct instance {};
typedef instance * instance_t;

// Don't leak (not that we have a destructor yet, but...)
%newobject new_instance;

// Change new_instance to return instance of Instance

%typemap(jstype) int new_instance "$typemap(jstype,instance_t)";
%typemap(jni) int new_instance "$typemap(jni,instance_t)";
%typemap(jtype) int new_instance "$typemap(jtype,instance_t)";

// Hide the instance_t argument and use a temporary instead under the hood
%typemap(in,numinputs=0) instance_t * ($1_basetype tmp) %{
  $1 = &tmp;
%}
// After the call copy the result back
%typemap(argout) instance_t * %{
  *($1_ltype)&$result = *$1;
%}
// Inside Java construct the proxy with the correct long pointer
%typemap(javaout) int new_instance {
  return new $typemap(jstype,int new_instance)($jnicall, $owner);
}

// Some error handling
%javaexception("Exception") new_instance {
  $action
  if (!result) {
    // JNI code to raise exception, untested in this form
    jclass clazz = JCALL1(FindClass, jenv, "Exception");
    JCALL2(ThrowNew, jenv, clazz, "Failure creating thing");
    return $null;
  }
}

%include "test.h"
Run Code Online (Sandbox Code Playgroud)

当然,您可以采用该技巧并使用类似于%pragma(java) modulecode本答案中我的第一个示例的内容来提供另一个行为更自然的重载:

%include <typemaps.i>
%apply int *OUTPUT { int *val };

%include "test.h"
Run Code Online (Sandbox Code Playgroud)

(请注意,这个数组技巧也适用于该instance_t*问题的第四种解决方案,但因为它不是原始类型,所以需要做更多的工作而没有真正的收获)