[C++-sig] Re: using shared_ptrs with the vector_indexing_suite

Arthur Peters amp at singingwizard.org
Thu Feb 3 16:36:46 CET 2005


Hello all,
There were a couple of messages on the subject of shared_ptrs used with
vector_indexing_suite.

Below is the most resent email on the subject. I am having the exact same
problem and I have found a solution, and I thought I'd post it for the
benafit of others who run into the same thing.

Turn off proxies by passing true as the NoProxy template parameter.
shared_ptrs don't need proxies because calls on one a copy of the the
shared_ptr will affect all of them (duh!).

If you have:
class_<std::vector<boost::shared_ptr<CPropertySet> >
>("vector_CPropertySet")
  .def(vector_indexing_suite<std::vector<boost::shared_ptr<CPropertySet>
> >());
Change it to:
class_<std::vector<boost::shared_ptr<CPropertySet> >
>("vector_CPropertySet")
  .def(vector_indexing_suite<std::vector<boost::shared_ptr<CPropertySet>
>,
                             true >());

The quoted message has a more extensive description of the initail
problem.

Hope this helps someone
-Arthur

>Date: Fri, 7 Jan 2005 14:56:55 -0800
>From: Greg Landrum <greg.landrum at gmail.com>
>Subject: [C++-sig] using shared_ptrs with the vector_indexing_suite
>To: c++-sig at python.org
>Message-ID: <60825b0f05010714564d71e769 at mail.gmail.com>
>Content-Type: text/plain; charset="us-ascii"
>
>I'm attempting to get shared_ptrs working in combination with the
>vector_indexing_suite in order to make it easy to return std::vectors
>of shared_ptrs from our code.
>
>I feel like I've got a reasonable grasp of what's required to return
>shared_ptrs (given how easy that is now, this isn't much of an
>accomplishment), and I've even managed to get them working with the
>iterator interface, but the combination with vector_indexing_suite
>eludes me.
>
>The attached module and testing code demonstrate the problem.  When I
>build this module and run the test (linux, python 2.3.4, g++ 3.2,
>boost 1.32), all the tests pass except for test5 and test5b, which use
>std::vectors of shared_ptrs. Both of those tests fail with the error:
>
>TypeError: No Python class registered for C++ class boost::shared_ptr<DemoKlass>
>
>Because the basic shared_ptr operations and the more complicated
>iterator stuff work, I'm guessing this has something to do with the
>vector_indexing_suite itself.
>
>Thanks in advance for any help,
>-greg
>-------------- next part --------------
>// Copyright Rational Discovery LLC 2005.
>// Distributed under the Boost Software License, Version 1.0. (See
>// accompanying file LICENSE_1_0.txt or copy at
>// http://www.boost.org/LICENSE_1_0.txt)
>
>
>#include <boost/python.hpp>
>#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
>#include <boost/python/detail/api_placeholder.hpp>
>#include <boost/shared_ptr.hpp>
>
>#include <vector>
>
>namespace python = boost::python;
>
>class DemoKlass {
>public:
>  explicit DemoKlass(int v) : val_(v) {};
>  int getVal() const { return val_; };
>private:
>  int val_;
>};
>typedef boost::shared_ptr<DemoKlass> DemoKlassSPtr;
>typedef std::vector<DemoKlass*> DemoKlassPtrVect;
>typedef std::vector<DemoKlassSPtr> DemoKlassSPtrVect;
>
>DemoKlass *buildPtr(int v) {
>  return new DemoKlass(v);
>}
>DemoKlassSPtr buildSPtr(int v) {
>  return DemoKlassSPtr(new DemoKlass(v));
>}
>
>DemoKlassPtrVect buildPtrVector(unsigned int sz){
>  DemoKlassPtrVect res;
>  for(unsigned int i=0;i<sz;i++){
>    res.push_back(new DemoKlass(i));
>  }
>  return res;
>}
>
>DemoKlassSPtrVect buildSPtrVector(unsigned int sz){
>  DemoKlassSPtrVect res;
>  for(unsigned int i=0;i<sz;i++){
>    res.push_back(DemoKlassSPtr(new DemoKlass(i)));
>  }
>  return res;
>}
>
>
>class DemoContainer {
>public:
>  typedef DemoKlassSPtrVect::iterator iterator;
>  typedef DemoKlassSPtrVect::const_iterator const_iterator;
>  explicit DemoContainer(unsigned int sz) {
>    vect_ = buildSPtrVector(sz);
>  }
>  iterator begin() {
>    return vect_.begin();
>  }
>  iterator end() {
>    return vect_.end();
>  }
>  const_iterator begin() const {
>    return vect_.begin();
>  }
>  const_iterator end() const {
>    return vect_.end();
>  }
>
>private:
>  DemoKlassSPtrVect vect_;
>};
>
>
>
>BOOST_PYTHON_MODULE(SPtrTestModule)
>{
>  python::class_<DemoKlass,DemoKlassSPtr >("DemoKlass","demo class",python::init<int>())
>    .def("GetVal",&DemoKlass::getVal)
>    ;
>
>  python::def("buildPtr",buildPtr,python::return_value_policy<python::manage_new_object>());
>  python::def("buildSPtr",buildSPtr);
>
>
>  python::class_<DemoKlassPtrVect>("DemoKlassPtrVec")
>    .def(python::vector_indexing_suite<DemoKlassPtrVect>())
>    ;
>  python::def("buildPtrVector",buildPtrVector);
>
>  python::class_<DemoKlassSPtrVect>("DemoKlassSPtrVec")
>    .def(python::vector_indexing_suite<DemoKlassSPtrVect>())
>    ;
>  python::def("buildSPtrVector",buildSPtrVector);
>
>
>  python::class_<DemoContainer>("DemoContainer","demo container",python::init<unsigned int>())
>    .def("__iter__",python::iterator<DemoContainer>())
>    ;
>
>
>}
>-------------- next part --------------
>A non-text attachment was scrubbed...
>Name: test.py
>Type: text/x-python
>Size: 1321 bytes
>Desc: not available
>Url : http://mail.python.org/pipermail/c++-sig/attachments/20050107/4ffbdf8a/test-0001.py
>
>------------------------------



More information about the Cplusplus-sig mailing list