问题描述
我正在寻找一种方法来扩展(即使用%新成员添加到一个类型延伸指令),该库文件本身的定义,同时该库的头文件只提供了一个向前声明类型类型。
I'm looking for a way to extend (i.e. add new members to a type using the %extend directive) a type that is defined in the library file itself while the header files of the library provide only a forward declaration for the type.
治疗的类型,如果它的定义是在编译时已知,会导致以下警告:
Treating the type as if its definition is known at compile time, leads to the following warning:
Warning 303: %extend defined for an undeclared class [name of the type].
是任何人都知道一个解决方案或替代方法这一问题的?我敢肯定有一个,因为SWIG的文档指出痛饮假定未知类型是一个结构或联合各找到一个时间。
Is anyone aware of a solution or a workaround for this problem? I'm sure there is one, since SWIG's documentation states that swig assumes that the unknown type is a struct or a union each time it finds one.
提前感谢!
推荐答案
您可以很方便地添加额外的方法已经在痛饮给它一个空定义的接口,例如向前声明的类型。
You can very easily add extra methods to a type which has been forward declared in SWIG by giving it an empty definition in the interface, e.g.
test.h:
// Forward declare foo
struct foo;
test.i:
test.i:
%module test
// Tell SWIG to wrap foo "properly", but that you don't know anything about it:
struct foo { };
%include "test.h"
%extend foo {
void bar() {
// Do stuff, probably with $self, here
}
}
最关键的是,在你没有真正通常意义上的写作C或C ++接口文件,你告诉SWIG什么类型和包装了每个类型的部分。
The key is that in the interface file you're not actually writing C or C++ in the normal sense, you're telling SWIG what types and what parts of each type to wrap.
既然你将presumably依托图书馆创建和销毁情况下,你还需要添加:
Since you will presumably be relying on the library to create and destroy instances you'll also want to add:
%nodefaultctor foo;
%nodefaultdtor foo;
在接口文件共进晚餐preSS构造函数/析构函数生成并迫使它要经过图书馆。
In the interface file to suppress the constructor/destructor generation and force it to go through the library.
这篇关于延长“不完整的”类型(夜风)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!