[C++-sig] Re: vector<foo*> -> list, preserving identity

Raoul Gough RaoulGough at yahoo.co.uk
Fri Nov 28 16:46:18 CET 2003


Raoul Gough <RaoulGough at yahoo.co.uk> writes:

> Jacek Generowicz <jacek.generowicz at cern.ch> writes:
>
>> Imagine I have wrapped (using Boost.Python)
>>
>> 1. a C++ class
>>
>> 2. a function (or method) which returns vector of pointers to said class.
[snip]
> std::vector<boost::shared_ptr<const foo> > makefoos(int n) {
>   // ...
> }
>
> BOOST_PYTHON_MODULE( nocopy ) {
>   class_<foo, boost::shared_ptr<foo> >("foo", no_init)
>     .def("pointer", &foo::pointer);
>
>   class_<std::vector<boost::shared_ptr<foo const> > > ("foo_vector")
>     .def (SOMETHING<std::vector<boost::shared_ptr<foo const> >());
>
>   def ("makefoos", makefoos);
> }
>
> Where SOMETHING would use either the current indexing suite
[snip]

I should have mentioned the other obvious alternative - package the
shared pointers directly into a real Python list. e.g.

boost::python::list makefoos (int n) {
  boost::python::list result;

  for (...) {
    result.append (boost::shared_ptr<foo const> (new ...));
  }

  return result;
}

-- 
Raoul Gough.
export LESS='-X'





More information about the Cplusplus-sig mailing list