假设我正确理解了您的问题,您有许多不透明的“句柄”,在 C 中实际上是 typedefs 到 void*,但在您生成的界面中,您希望强制执行更强的类型检查。 (请注意,此处的默认行为是正确的,因为它允许使用与 C 完全一致的用法)。您想防止将一种句柄意外地提供给采用“不同”void* 的函数,即将typedef 公开为一种strong typedef。
您可以使用 SWIG 轻松做到这一点。要记住的关键是您提供给 SWIG 的接口文件并不总是必须与 real C 类型完全匹配,只要最终生成的代码仍然正确且合法。
我举了一个例子来说明这一点。给定一个头文件,这在原理上可能与您的 ogr_api.h 相似:
#include <stdio.h>
typedef void * HandleType1;
typedef void * HandleType2;
void foo(HandleType1) {printf("foo\n");}
void bar(HandleType2) {printf("bar\n");}
您希望只能使用HandleType1 调用foo 和使用HandleType2 调用bar。
我使用以下接口来获取此类行为:
%module test
%{
#include "test.h"
%}
%nodefaultctor HandleType1;
struct HandleType1 {};
%nodefaultctor HandleType2;
struct HandleType2 {};
void foo(HandleType1*);
void bar(HandleType2*);
%inline {
// just for testing - presumably you have a creator in your real API
HandleType1* geth1() { return NULL; }
HandleType2* geth2() { return NULL; }
}
由此生成的代码非常好,因为它不会尝试做任何 void* 无法完成的事情,而且它们都被视为包装器中的指针。
需要%nodefaultctor 以防止 SWIG 尝试根据我们告诉它的谎言构造新句柄,否则会出现编译器错误。 (你可能也想抑制析构函数,或者自定义它,因为这将调用free)。
这会生成一个 Java 接口,该接口只允许为每个函数使用正确的句柄。我对此进行了测试:
public class run {
public static void main(String[] args) {
System.loadLibrary("test");
test.foo(test.geth1());
//test.bar(test.geth1());
//test.foo(test.geth2());
test.bar(test.geth2());
}
}
这有点花招,但它确实有效,看看生成的包装器来说服自己。
按预期编译和运行。如您所愿,注释掉的行会给出错误。
对于特定于 D 的解决方案,我知道 typedef 提供了强大的 typedef(不像 alias 更像 C 中的 typedef)我认为您可以使用类似的东西:
%module test
typedef void * HandleType1;
typedef void * HandleType2;
%pragma(d) proxydmodulecode = %{
typedef void * HandleType1;
typedef void * HandleType2;
%}
%typemap(dtype) HandleType1* "HandleType1";
%typemap(dtype) HandleType2* "HandleType2";
void foo(HandleType1* h1);
void bar(HandleType2* h2);
生成你想要的界面。 typemaps修改D接口中使用的类型,%pragma将typedef插入到生成接口的(代理)部分。