Note that there are some explanatory texts on larger screens.

plurals
  1. POSWIG, boost shared pointers and inheritance
    primarykey
    data
    text
    <p>I'm having trouble with SWIG, shared pointers, and inheritance.</p> <p>I am creating various c++ classes which inherit from one another, using Boost shared pointers to refer to them, and then wrapping these shared pointers with SWIG to create the python classes.</p> <p>My problem is the following:</p> <ul> <li>B is a subclass of A</li> <li>sA is a shared pointer to A</li> <li>sB is a shared pointer to B</li> <li><p>f(sA) is a function expecting a shared pointer to A</p></li> <li><p>If I pass sB to f() then an error is raised.</p></li> <li>This error only occurs at the python level.</li> <li>At the C++ level I can pass sB to f() without a problem.</li> </ul> <p>I have boost 1.40 and swig 1.3.40.</p> <p>Below are the contents of 5 files which will reproduce the problem with:</p> <pre><code>python setup.py build_ext --inplace python test.py </code></pre> <p><strong>swig_shared_ptr.h</strong></p> <pre><code>#ifndef INCLUDED_SWIG_SHARED_PTR_H #define INCLUDED_SWIG_SHARED_PTR_H #include &lt;boost/shared_ptr.hpp&gt; class Base {}; class Derived : public Base {}; typedef boost::shared_ptr&lt;Base&gt; base_sptr; typedef boost::shared_ptr&lt;Derived&gt; derived_sptr; void do_something (base_sptr bs); base_sptr make_base(); derived_sptr make_derived(); #endif </code></pre> <p><strong>swig_shared_ptr.cc</strong></p> <pre><code>#include &lt;iostream&gt; #include "swig_shared_ptr.h" void do_something (base_sptr bs) { std::cout &lt;&lt; "Doing something." &lt;&lt; std::endl; } base_sptr make_base() { return base_sptr(new Base ()); }; derived_sptr make_derived() { return derived_sptr(new Derived ()); }; </code></pre> <p><strong>swig_shared_ptr.i</strong></p> <pre><code>%module(docstring=" Example module showing problems I am having with SWIG, shared pointers and inheritance. ") swig_shared_ptr %{ #include "swig_shared_ptr.h" %} %include &lt;swig_shared_ptr.h&gt; %include &lt;boost_shared_ptr.i&gt; %template(base_sptr) boost::shared_ptr&lt;Base&gt;; %template(derived_sptr) boost::shared_ptr&lt;Derived&gt;; </code></pre> <p><strong>setup.py</strong></p> <pre><code>""" setup.py file for swig_shared_ptr """ from distutils.core import setup, Extension swig_shared_ptr_module = Extension('_swig_shared_ptr', include_dirs = ['/usr/include/boost'], sources=['swig_shared_ptr.i', 'swig_shared_ptr.cc'], ) setup (name = 'swig_shared_ptr', version = '0.1', author = "Ben", description = """Example showing problems I am having with SWIG, shared pointers and inheritance.""", ext_modules = [swig_shared_ptr_module], py_modules = ["swig_shared_ptr"], ) </code></pre> <p><strong>test.py</strong></p> <pre><code>import swig_shared_ptr as ssp bs = ssp.make_base() dr = ssp.make_derived() # Works fine. ssp.do_something(bs) # Fails with "TypeError: in method 'do_something', argument 1 of type 'base_sptr'" ssp.do_something(dr) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload