[C++-sig] Re: Iterators + shared_ptr + Abstract base classes + Pass by reference = help :)

David Abrahams dave at boost-consulting.com
Thu Jan 9 00:12:32 CET 2003


David Sankel <camio at yahoo.com> writes:

>> > I would like the Python derived class to see a list of Abstracts.
>> 
>> And what do you expect it to be able to do to the
>> list?  I'm not asking about the items it contains,
>> yet.  Do you need to be able to modify the length of
>> the list, or move items within it, etc?
>
> I would like to have the ability to do everything
> that you can do with a normal Python list.  Such as
> modify the length, move items within it, etc.

If you expect these changes to affect the std::vector<>
that was used to produce the list, then you're out of
luck.  There's no way to get Python to notify us when
you change the list so that we can update the vector.
The approach you are using below will never allow you
to manipulate the original vector as a list from
Python.

Are you sure it wouldn't work to just expose
std::vector<shared_ptr<Abstract> > using class_ and
write all the neccessary methods so that it looks like
a Python List?  The only way your Python code could
tell the difference would be if you inspected its
__class__ or did isinstance(x, list) or the like.


> That was my misunderstanding.
>
>> Have you looked at
>>
> http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/python/doc/v2/data_members.html
>> ?
>> 
>> In order to use this, you'll need to expose the type
>> of the Filter::data member.  Unless...
>> 
>> ...I suppose you want Filter::data to show up as a
>> Python list also?
>
> Yes, if possible.
>
>> It sounds like you may want to explore
>>
> http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/python/doc/v2/faq.html#question2
>
> Explored.  Thanks a lot for your help with this thus
> far.  I really appreciate it.  Here is where I am now
> (I'm using the container conversions pointed to by the
> faq):

<snip>

> class PythonFilter : public Filter
> {
> public:
>   PythonFilter( PyObject * self )
>     : m_self( self ), m_initialized(true) { };
>     void doFilter( vector<shared_ptr<Abstract> > &result)
>     {
>     if( m_initialized )
>     {
>       cout << "Got into doFilter with " << name() << endl;
>       call_method< 
>         void, 
>         vector< shared_ptr< Abstract > > >
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You should not be specifying this parameter; it defeats
the use of ref().

>           ( m_self, "doFilter", ref( result ) );
>     }
>     }
>   virtual std::string name() const
>   {
>     return call_method< std::string > ( m_self, "name" );
>   }
>   virtual ~PythonFilter(){}
> private:
>   PyObject * m_self;
>   bool m_initialized;
> };
>

<snip>

> But I am still missing something because I cannot get
> the following python code without the comments to
> work:

If you want help, please always specify precisely:
  * what you tried to do (i.e. what you did with SpecificFilter)
  * the result you expected
  * the result you actually got 

Just syaing "I can't get this to work", especially when
it's just a declaration, doesn't give us much to go on.

> from embedded_Filter import *
> class SpecificFilter( Filter ):
>   def name( self ):
>     return 'SpecificFilterA'
>   def doFilter( self, result ):
>     print("In SpecificFilter.doFilter\n")
>     print( len( self.data ) ) ; #self.data is a normal python list
>     print( len( result ) );     #So is result
>     p = [x.clone() for x in self.data] #The members of self.data can
>                                         #have their methods called
>     result = p       #result can be modified as a python list
>     result.reverse() #another modification to result.

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list