SWIG, shared pointers and inheritance

I’m having trouble with SWIG, shared pointers, and inheritance.

I’m trying to create a base gr_constellation class, which would be a
helper object for
doing QAM-like modulations. It would contain a list of the
constellation points, and
a decision_maker function for demodulation. To implement different
QAM-like modulations you
would subclass gr_constellation.

I thought I should try to use shared pointers for the gr_constellation
objects (since that’s what’s used
for the blocks and presumably there are good reasons). I can get
everything working fine
until I subclass gr_constellation and then pass a shared pointer to
the subclass to a function expecting
the parent shared pointer object.

i.e. B is a subclass of A
sA is a shared pointer to A
sB is a shared pointer to B
f(sA) is a function expecting a shared pointer to A

 If I pass sB to f() then an error is raised.
 This error only occurs at the python level.
 At the C++ level I can pass sB to f() without a problem.

This problem appears to have been solved in gnuradio already, since
the connect functions accept a shared
pointer to any of the blocks. However, I haven’t been able to work
out how it’s done. Does anyone know
how to do this? I thought perhaps gr_basic_block::basic_block() has
something to do with this but
haven’t been able to track it down.

I’m now including the contents of a few files that reproduce this
problem independently of gnuradio, although
they require the gr_shared_ptr.i file.

To compile I run:

swig -c++ -python -Iboost blah.i
python setup.py build_ext --inplace

And then test with:

python test.py




#include <boost/shared_ptr.hpp>

class Blah {};

class Bloop : public Blah {};

typedef boost::shared_ptr blah_sptr;
typedef boost::shared_ptr bloop_sptr;

void do_something (blah_sptr bls);

blah_sptr make_blah();
bloop_sptr make_bloop();



#include “blah.h”

void do_something (blah_sptr bls)
std::cout << “Doing something.” << std::endl;

blah_sptr make_blah() { return blah_sptr(new Blah ()); };
bloop_sptr make_bloop() { return bloop_sptr(new Bloop ()); };


This module does nothing useful.
") blah

#include “blah.h”

%include <blah.h>
%include <gr_shared_ptr.i>
%template(blah_sptr) boost::shared_ptr;
%template(bloop_sptr) boost::shared_ptr;


setup.py file for SWIG example

from distutils.core import setup, Extension

blah_module = Extension(’_blah’,
include_dirs = [’/usr/include/boost’],
sources=[‘blah_wrap.cxx’, ‘blah.cc’],

setup (name = ‘blah’,
version = ‘0.1’,
author = “SWIG Docs”,
description = “”“Simple swig example”"",
ext_modules = [blah_module],
py_modules = [“blah”],


import blah

bla = blah.make_blah()
blp = blah.make_bloop()

Works fine.


Fails with "TypeError: in method ‘do_something’, argument 1 of type