如何在swig中使用weak_ptr?

SWIG主页说shared_ptr是专门处理的,但是weak_ptr没有。 这是否意味着weak_ptr支持在SWIG中存在一些错误/问题? 如果可以使用,如何使用它? 任何人都可以提供样品.i代码? 非常感谢。

weak_ptr在SWIG中不需要任何特殊支持即可使用。

需要SWIG for shared_ptr特殊支持的操作是取消引用并传递给函数。 这是因为您从不直接取消引用或创建weak_ptr 。 相反,当您在C ++中正常使用它时,您将调用lock()成员函数将其升级为完整的,保留的shared_ptr或其他函数之一以查询其状态。

因此,您在实践中需要做的就是像任何其他模板一样包装weak_ptr并结合使用现有的shared_ptr支持。 例如:

 %module test %{ #include  %} %include  %shared_ptr(Foo) namespace std { template class weak_ptr { public: typedef Ty element_type; weak_ptr(); weak_ptr(const weak_ptr&); template weak_ptr(const weak_ptr&); template weak_ptr(const shared_ptr&); weak_ptr(const shared_ptr&); void swap(weak_ptr&); void reset(); long use_count() const; bool expired() const; shared_ptr lock() const; }; } %inline %{ struct Foo { }; %} %template(FooWeakPtr) std::weak_ptr; 

这可以用一些Java来实现:

 public class run { public static void main(String[] argv) { System.loadLibrary("test"); Foo a = new Foo(); System.out.println(a); FooWeakPtr b=new FooWeakPtr(a); System.out.println(b); Foo c=b.lock(); System.out.println(c); System.out.println(b.use_count()); a=null; System.gc(); System.out.println(b.use_count()); c=null; System.gc(); System.out.println(b.use_count()); Foo d=b.lock(); System.out.println(d); } } 

运行时,这给出:

 swig2.0 -c++ -Wall -java test.i && g++ -Wall -Wextra -I/usr/lib/jvm/java-6-sun/include -I/usr/lib/jvm/java-6-sun/include/linux -std=c++0x -shared -o libtest.so test_wrap.cxx && javac run.java && LD_LIBRARY_PATH=. java run Foo@42719c FooWeakPtr@119298d Foo@f72617 2 2 2 Foo@dc8569 

(请注意,我的运行时已完全忽略System.gc() ,因此再次尝试锁定确实成功了)

但它也适用于以下Python的相同.i文件:

 import test a=test.Foo() print a b=test.FooWeakPtr(a) print b c=b.lock() print c print b.use_count() a=None print b.use_count() c=None print b.use_count() d=b.lock() print d 

当跑步时给出:

 g++ -Wall -Wextra -I/usr/include/python2.6 -std=c++0x -shared -o _test.so test_wrap.cxx && python run.py  *' at 0xf7419710> >  *' at 0xf7419728> >  *' at 0xf7419740> > 2 1 0 None 

在最后一个shared_ptr没有更多引用之后,引用计数而不是GC确实导致对lock()的调用失败。