简体   繁体   English

扩展“不完整”类型(SWIG)

[英]extending 'incomplete' types (SWIG)

I'm looking for a way to extend (ie 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. 我正在寻找一种扩展(即使用%extend指令将新成员添加到类型中)在库文件本身中定义的类型的方法,而库的头文件仅提供该类型的前向声明。

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].

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. 我肯定有一个,因为SWIG的文档指出swig假定每次找到一个未知类型都是结构或联合。

Many thanks in advance! 提前谢谢了!

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, eg 您可以通过在接口中给它一个空的定义,非常容易地向在SWIG中预先声明的类型添加额外的方法,例如

test.h: 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
  }
}

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. 关键在于,在接口文件中,您实际上并不是正常地编写C或C ++,而是在告诉SWIG什么类型以及每种类型的哪些部分要包装。

Since you will presumably be relying on the library to create and destroy instances you'll also want to add: 由于您大概将依赖该库来创建和销毁实例,因此您还需要添加:

%nodefaultctor foo; 
%nodefaultdtor foo; 

In the interface file to suppress the constructor/destructor generation and force it to go through the library. 在接口文件中,禁止构造函数/析构函数的生成,并强制其通过库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM