Ruby and Perl support for shared_ptr (std and boost) would be extremely helpful.
I'm adding a little clarification because I know the documentation does mention "smart pointer" support for Ruby at http://swig.org/Doc2.0/Ruby.html#Ruby_nn24.
In Ruby, the current support for shared_ptr in SWIG 2.0.9 falls short in a few ways:
I have attached some sample code which compares SWIG Python behavior to SWIG Ruby behavior though a series of unit tests in each language. These Ruby tests demonstrate the shortcomings listed above. The Python tests show the expected behavior. A Makefile is provided to simplify building and running the tests on Linux (I used Ubuntu 12.04LTS to develop the sample code). Run "make ruby" to run the Ruby unit tests. Run "make python" to run the Python unit tests.
BTW, I have had partial success working around the empty-shared_ptr-as-nil issue by adding "out" typemaps along similar to:
%typemap(out) boost::shared_ptr< CONST TYPE >
$result = $1 ? SWIG_NewPointerObj( /* ... */ ) : Qnil;
This helps in most cases, but I have noticed that the order of %template directives can prevent the typemaps from being used in some situations. Using the sample code I attached in my previous comment, for example:
o = Thingy::Other.create # o is a shared_ptr
b = o.base(false) # b _should_ be nil
b.doof unless b.nil? # Crash because b is an empty shared_ptr
Moving the %template directives for shared_ptr<Other> to after the ones for shared_ptr<Base> and shared_ptr<Derived> seems to fix the issue but ensuring correct ordering when you have many classes is very difficult.
Ruby %shared_ptr support has been added to swig-3.0.8.
Log in to post a comment.