How to use weak_ptr in swig?

The SWIG main page says that shared_ptr is specially processed, but weak_ptr is not. Does this mean that weak_ptr support has some problems / issues in SWIG? If it is normal to use, how to use it? Can someone give a sample .i code? Many thanks.

+4
source share
1 answer

weak_ptr does not need special support in SWIG to use.

Operations that require special support in SWIG for shared_ptr are dereferencing and passing functions. This is because you never weak_ptr for a difference or create weak_ptr . Instead, when you usually use it in C ++, you call the lock() member function to update it to the full, stored shared_ptr or one of the other functions to request its state.

So, all you have to do in practice is wrap weak_ptr , like any other template, and use the existing shared_ptr support. For instance:

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

This can be implemented using some 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); } } 

At startup, this gives:

 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 

(Note that System.gc() completely ignored by my runtime, so the attempt to lock again really succeeds)

But it also works with the same .i file for the following Python:

 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 

And when run gives:

 g++ -Wall -Wextra -I/usr/include/python2.6 -std=c++0x -shared -o _test.so test_wrap.cxx && python run.py <test.Foo; proxy of <Swig Object of type 'std::shared_ptr< Foo > *' at 0xf7419710> > <test.FooWeakPtr; proxy of <Swig Object of type 'std::weak_ptr< Foo > *' at 0xf7419728> > <test.Foo; proxy of <Swig Object of type 'std::shared_ptr< Foo > *' at 0xf7419740> > 2 1 0 None 

If link counting instead of GC results in a lock() call, after which the last shared_ptr has no more references.

+3
source

Source: https://habr.com/ru/post/1210065/


All Articles