要始终自动将相同的指针传递给函数,这在 SWIG 中相当简单。例如,给定“头”文件 test.h,它捕获了问题的核心部分:
struct context; // only used for pointers
void init_context(struct context **ctx) { *ctx=malloc(1); printf("Init: %p\n", *ctx); }
void release_context(struct context *ctx) { printf("Delete: %p\n", ctx); free(ctx); }
void foo(struct context *ctx) { printf("foo: %p\n", ctx); }
我们可以包装它,并通过执行以下操作自动将全局上下文传递到预期的任何地方:
%module test
%{
#include "test.h"
// this code gets put in the generated C output from SWIG, but not wrapped:
static struct context *get_global_ctx() {
static struct context *ctx = NULL;
if (!ctx)
init_context(&ctx);
return ctx;
}
%}
%typemap(in,numinputs=0) struct context *ctx "$1=get_global_ctx();"
%ignore init_context; // redundant since we call it automatically
%include "test.h"
这会为 struct context *ctx 设置一个类型映射,而不是从 Java 中获取输入,而是在匹配的任何地方自动调用 get_global_ctx()。
这可能足以让 Java 开发人员使用一个理智的接口,但它并不理想:它强制上下文是全局的,这意味着没有 Java 应用程序可以同时使用多个上下文。
鉴于 Java 是一种 OO 语言,一个更好的解决方案是使上下文成为第一类对象。我们也可以让 SWIG 为我们生成这样的接口,尽管它有点复杂。我们的 SWIG 模块文件变为:
%module test
%{
#include "test.h"
%}
// These get called automatically, no need to expose:
%ignore init_context;
%ignore delete_context;
// Fake struct to convince SWIG it should be an object:
struct context {
%extend {
context() {
// Constructor that gets called when this object is created from Java:
struct context *ret = NULL;
init_context(&ret);
return ret;
}
~context() {
release_context($self);
}
}
};
%include "test.h"
我们可以成功地运行这段代码:
public class run {
public static void main(String[] argv) {
System.loadLibrary("test");
context ctx = new context();
// You can't count on the finalizer if it exits:
ctx.delete();
ctx = null;
// System.gc() might also do the trick and in a longer
// running app it would happen at some point probably.
}
}
给予:
Init: 0xb66dab40
Delete: 0xb66dab40
在动态类型语言中,这将是困难的部分——我们可以使用一种或另一种形式的元编程来根据需要插入成员函数。因此,我们可以完全按照预期说出类似new context().foo(); 的内容。 Java 是静态类型的,所以我们需要更多的东西。我们可以通过多种方式在 SWIG 中执行此操作:
接受我们现在可以非常高兴地调用test.foo(new context()); - 尽管它看起来仍然很像 Java 中的 C,所以我建议如果你最终编写大量看起来像C.
-
使用%extend(手动)将方法添加到上下文类中,test.i 中的%extend 变为:
%extend {
context() {
// Constructor that gets called when this object is created from Java:
struct context *ret = NULL;
init_context(&ret);
return ret;
}
~context() {
release_context($self);
}
void foo() {
foo($self);
}
}
-
与%extend 一样,但在Java 端使用类型映射编写胶水:
%typemap(javacode) struct context %{
public void foo() {
$module.foo(this);
}
%}
(注意:这需要在接口文件中足够早才能工作)
请注意,我在这里没有向 SWIG 展示我的上下文结构的真正定义 - 它始终在需要真正定义的任何地方都遵循我的“库”,因此不透明指针保持完全不透明.
用双指针包装init_context 的更简单的解决方案是使用%inline 提供一个仅在包装器中使用的额外函数:
%module test
%{
#include "test.h"
%}
%inline %{
struct context* make_context() {
struct context *ctx;
init_context(&ctx);
return ctx;
}
%}
%ignore init_context;
%include "test.h"
足以让我们编写如下Java:
public class run {
public static void main(String[] argv) {
System.loadLibrary("test");
// This object behaves exactly like an opaque pointer in C:
SWIGTYPE_p_context ctx = test.make_context();
test.foo(ctx);
// Important otherwise it will leak, exactly like C
test.release_context(ctx);
}
}
替代但类似的方法包括使用cpointer.i library:
%module test
%{
#include "test.h"
%}
%include <cpointer.i>
%pointer_functions(struct context *,context_ptr);
%include "test.h"
然后您可以将其用作:
public class run {
public static void main(String[] argv) {
System.loadLibrary("test");
SWIGTYPE_p_p_context ctx_ptr = test.new_context_ptr();
test.init_context(ctx_ptr);
SWIGTYPE_p_context ctx = test.context_ptr_value(ctx_ptr);
// Don't leak the pointer to pointer, the thing it points at is untouched
test.delete_context_ptr(ctx_ptr);
test.foo(ctx);
// Important otherwise it will leak, exactly like C
test.release_context(ctx);
}
}
还有一个pointer_class 宏,它比这更面向对象,可能值得使用。关键是您提供了工具来处理 SWIG 用来表示它一无所知的指针的不透明指针对象,但避免了本质上颠覆类型系统的 getCPtr() 调用。