Exporting C++ hier_block2s via SWIG

Hi,

I was wondering if it’s possible to write a gr_hier_block2 in C++ and
then export it to SWIG just like the the gr_blocks are. I tried using
GR_SWIG_BLOCK_MAGIC, but not surprisingly the SWIG code fails to compile
saying

/usr/include/boost/shared_ptr.hpp:211: Error: error: cannot convert
‘specest_welch* const’ to ‘gr_block*’ in initialization.

Is there any easy way to do this? Basically, what I want is a
GR_SWIG_HIERBLOCK_MAGIC() function, I suppose.
Has anyone done something like this before?

Cheers,
MB

On Mon, Feb 23, 2009 at 05:18:57PM +0100, Martin B. wrote:

I was wondering if it’s possible to write a gr_hier_block2 in C++ and
then export it to SWIG just like the the gr_blocks are.
[…]
Is there any easy way to do this? Basically, what I want is a
GR_SWIG_HIERBLOCK_MAGIC() function, I suppose.
Has anyone done something like this before?

I had a bash at it (not very elegant, though), and tried copying
gnuradio-core/src/lib/swig/gr_swig_block_magic.i into my own i-file. I
then simply modified everything from gr_block to gr_hier_block2. It now
looks like this (slightly truncated for readability):

<<<<< CODE >>>>>
%include “gnuradio.i”

%{
#include “specest_welch.h”
%}

// ----------------------------------------------------------------
class specest_welch;
typedef boost::shared_ptr<specest_welch> specest_welch_sptr;
%template(specest_welch_sptr) boost::shared_ptr<specest_welch>;
%rename(welch) specest_make_welch;
%inline {
gr_hier_block2_sptr specest_welch_block (specest_welch_sptr r)
{
return gr_hier_block2_sptr®;
}
}

%pythoncode %{
specest_welch_sptr.block = lambda self: specest_welch_block(self)
specest_welch_sptr.repr = lambda self: “<gr_hier_block2 %s (%d)>” %
(self.name(), self.unique_id ())
%}

%ignore specest_welch;

specest_welch_sptr
specest_make_welch(unsigned fft_len, int overlap = -1, int ma_len = 8)
throw(std::invalid_argument);

class specest_welch : public gr_hier_block2
{
<<<<< CODE >>>>>

It kind of works, but not as smoothly as I could. Here’s an example
python session:

<<<<< COMMAND LINE >>>>>>
[insfl4:gnuradio]% python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

from gnuradio import specest
Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/lib/python2.5/site-packages/gnuradio/specest.py”,
line 6, in
import _specest
ImportError:
/usr/local/lib/python2.5/site-packages/gnuradio/_specest.so: undefined
symbol: _ZN14gr_hier_block24lockEv

from gnuradio import gr,specest

<<<<< COMMAND LINE >>>>>>

As you can see, I get an error when I only import specest, but not if
import gr and specest. Yep, I’m a nitpicker, but if I run ‘from gnuradio
import trellis’, I don’t get this behaviour, so there must be a bug
somewhere down the line. If there’s some SWIG experts out there, I’d
appreciate any advice, but this is already a good enough start for me.

Cheers,
MB