[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Discuss-gnuradio] SWIG, shared pointers and inheritance.
From: |
Ben Reynwar |
Subject: |
[Discuss-gnuradio] SWIG, shared pointers and inheritance. |
Date: |
Wed, 22 Dec 2010 21:51:58 -0700 |
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
Cheers,
Ben
---blah.h------------------------------------
#ifndef INCLUDED_BLAH_H
#define INCLUDED_BLAH_H
#include <boost/shared_ptr.hpp>
class Blah {};
class Bloop : public Blah {};
typedef boost::shared_ptr<Blah> blah_sptr;
typedef boost::shared_ptr<Bloop> bloop_sptr;
void do_something (blah_sptr bls);
blah_sptr make_blah();
bloop_sptr make_bloop();
#endif
---blah.cc-----------------------------------
#include <iostream>
#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 ()); };
---blah.i------------------------------------
%module(docstring="
This module does nothing useful.
") blah
%{
#include "blah.h"
%}
%include <blah.h>
%include <gr_shared_ptr.i>
%template(blah_sptr) boost::shared_ptr<Blah>;
%template(bloop_sptr) boost::shared_ptr<Bloop>;
---setup.py----------------------------------------------------
"""
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"],
)
---test.py---------------------------------------------
import blah
bla = blah.make_blah()
blp = blah.make_bloop()
# Works fine.
blah.do_something(bla)
# Fails with "TypeError: in method 'do_something', argument 1 of type
'blah_sptr'"
blah.do_something(blp)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Discuss-gnuradio] SWIG, shared pointers and inheritance.,
Ben Reynwar <=