discuss-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Discuss-gnuradio] Q's regarding C++ <-> SWIG <-> Python


From: Eric Blossom
Subject: Re: [Discuss-gnuradio] Q's regarding C++ <-> SWIG <-> Python
Date: Fri, 7 Jul 2006 19:43:30 -0700
User-agent: Mutt/1.5.9i

On Fri, Jul 07, 2006 at 10:31:43PM -0400, Michael Dickens wrote:
> 
> OK.  So what's the difference between:
> 
>       const std::vector<float> &taps
> and
>       const std::vector<float> taps
> 
> in terms of the way Python -> SWIG -> C++ works?

Using the default typemaps that convert Python lists/tuples to
STL vectors, there is no difference.  The default typemaps will
convert the Python sequence into a STL vector by copying the
elements. 

> In C++, the "&" is a reference to - no copying, and "const" means
> that you can't change it at the destination (or, rather that the
> compiler will complain if you try to ;).  Does a reference (not
> copying) really hold true for moving data from Python to C++?

You're going to want to read up on SWIG typemaps...  Have fun ;)


> That would imply that the underlying  
> data structure is the same between them ... which I can understand  
> for the "usual" types (char, short, int, long, float, double,  
> etc...) ... so how would that work with a C++ class such as  
> std::vector?  Does Python actually implement a "list" (e.g. "[5,6]")  
> as a std::vector, and hence allow for reference passing?

Nope.  Two reasons:  

  [5, "second", [3, 4, lambda a, b: a + b], 28+3j]

  Python's coded in C

> OK.  That's a bit off-topic; just curious.  Sounds like I can use  
> wither "&foo" or "foo" so long as it's "const".  Good.  I'll try that  
> tomorrow.  Hopefully my problem was elsewhere.

OK.

> >>3) Is it possible to assign default values to only -some- arguments
> >>to the "friend" method?  I can see some codes which assign default
> >>values to -all- arguments, while most do none.
> >
> >It's a C++ thing.  Everything to the right of the first argument
> >with a default value must also have a default value.
> 
> Yes yes, standard C++ ;-)  In the context of how SWIG interprets the  
> declarations for the friend method(s), that's my question.  I tried  
> having the last 3 arguments of the friend method have defaults (of 7,  
> so 4 required, 3 optional), but then the instantiation from Python  
> doesn't work.  Could be that I messed up something else along the  
> way, but this was the last change I made (removing the 3 default  
> values) and -poof- things worked again.

Should work OK.  Be sure that the C++ .h and the .i files agree.

> >Yes, you can have multiple constructors, but they need to have
> >different names.  (If they have the same name, C++ can sort them out
> >at compile time based on argument type at the calling site, but Python
> >can't do that because of its dynamic typing.)
> 
> Ah, so just name the friend method differently than that of the  
> "default" friend method?  Hmmm, I'll try that tomorrow when I have a  
> chance.  Clearly for a given C++ class, the constructor names will be  
> the same but with different arguments ... so the difference has to  
> come in the friend naming.  I would guess it would require it's own  
> GR_SWIG_BLOCK_MAGIC stuff too, with the different friend name?

The second constructor will require a subset of what's in
GR_SWIG_BLOCK_MAGIC.  Probably just the renaming part.

Eric




reply via email to

[Prev in Thread] Current Thread [Next in Thread]