[C++-sig] Chaining methods in C++ and Python

Alexey Goldin Alexey.Goldin at jpl.nasa.gov
Mon Mar 3 22:54:17 CET 2003


I have (I believe) a fairly simple for experts question.


I am trying to wrap a medium size high frequency circuit simulation
package (Supermix, at
http://www.submm.caltech.edu/supermix/default.html

I encountered the following problem. Many class methods return
reference to themself to allow chaining, something along the line

class microstrip{
      microstrip & top_strip(impedance & t);
      microstrip & ground_plane(impedance & g);
      microstrip & substrate(dielectric  s);
      microstrip & superstrate(dielectric s)
.................More stuff follows.............
}


These methods establish relationship between microstrip object and
objects like length, width, substrate, etc... (yes, length is also an
object --- it can be parameter dependent on other numbers)

Naturally, to protect objects like substrate, superstrate, length,
etc. from beeing freed by memory management, I am trying to use the
following mechanism (fragment from my wrapper):

 class_<microstrip,bases<trl_base> >("microstrip", init<>())
   .def(init<microstrip>())
   .def("substrate",   &microstrip::substrate,  
return_internal_reference<1,with_custodian_and_ward<1,2> >() )
   .def("superstrate", &microstrip::superstrate,
return_internal_reference<1,with_custodian_and_ward<1,2> >() )
   .def("top_strip",   &microstrip::top_strip,  
return_internal_reference<1,with_custodian_and_ward<1,2> >() )
  
.def("ground_plane",&microstrip::ground_plane,return_internal_reference<1,with_custodian_and_ward<1,2> >() );
 

This is working fine, if I am using the following code (Python):


  
SiO=const_diel(7.5,0.01)
air=const_diel(1.0,0)	
line = microstrip()

#line.top_strip(nb).ground_plane(nb).substrate(SiO)
line.top_strip(nb)
line.ground_plane(nb)
line.substrate(SiO)
return line

However, if I try to replace the last three lines with one  commented
out line with chained calls, 
the first time I try to call microstrip  methods I get 
error message:

pure virtual method called
Aborted 

This is the kind of message C++ runtime library is giving when I do
not protect referenced objects with_custodian_and_ward. However, in 
this case the error is triggered by going to chained call.



g++ -v
Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.4/specs
gcc version 2.95.4 20011002 (Debian prerelease)

I am using Pyton 2.2.2, latest CVS boost.

I'd really like to have Python very similar to one produced by C++


If the problem non-obvious, I will spend a day or two provide some
test case that will fit in few pages. I' however, hope that the
problem may be obvious to experts without it.


My suspicion is that chained call is creating a temporary object (new
one for each call of method like top_strip), the lifetime of
referenced object is tied to lifetime of this temporary object, which
is later destroyed. This does not happen If this is so, how can I
bypass this problem?


Thanks!

-- 
Alexey Goldin <Alexey.Goldin at jpl.nasa.gov>





More information about the Cplusplus-sig mailing list