[C++-sig] Boost.Python : Protected Destructor

Brian O'Kennedy brian.o'kennedy at vicon.com
Thu Jun 12 18:31:17 CEST 2008


Hi, 

 

I'm trying to wrap a class like the one below which has a protected dtor
and should be destructed via the Destroy method. 

 

class Foo

{

public:

  int bar() { return 1; }

  void Destroy() { delete this; }

protected:

  ~Foo() { Destroy(); }

};

 

My Boost.Python code looks like this:

 

   class_< Foo, boost::noncopyable > ("Foo", no_init )

    .def( "bar", &Foo::bar );

 

 

I had to add both the boost::noncopyable and the no_init to stop
compilation errors, but this means that I have no way of creating Foo
from within Python. 

 

I can get around this by creating a factory method and registering the
pointer:

  boost::shared_ptr< Foo > FooCreator()

  {

    return boost::shared_ptr< Foo >( new Foo(), boost::bind(
&Foo::Destroy, _1 ) );

  }

And 

  def("FooCreator", FooCreator );

  register_ptr_to_python< boost::shared_ptr< Foo > >();

 

 

This feels a little clunky though.  Am I doing something very wrong? Is
there an easier way to manage classes like this with protected
destructors?      

 

Thanks, 

 Brian

 

 


________________________________________________________________________
This e-mail, and any attachment, is confidential. If you have received it in error, do not use or disclose the information in any way, notify me immediately, and please delete it from your system.
________________________________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080612/a20ac411/attachment.htm>


More information about the Cplusplus-sig mailing list