Using a custom smart pointer with Boost.Python

I’m currently working on creating a python wrapper for XCF using Boost.Python. During that project i stumbled upon the problem that i was wrapping some classes that used a custom smart pointer (i.e. not one from the boost library). Boost.Python is quite good at wrapping classes using smart pointers, but it has to know some things about the smart pointer used first.

Most importantly, it has to know how to get the “raw” pointer from the smart pointer and how to determine the type of the object that is being pointed to (given the type of the smart pointer). Finding documentation about telling Boost.Python how to work with an unknown type of smart pointer wasn’t easy (which is why i am blogging this), but fortunately at some point i stumbled upon this. It’s from the documentation of the Py++-package, but you don’t have to be using Py++ to use the technique described in there.

You have to keep a lot of things in mind when implementing this (especially when the class your smart pointer is pointing to might also have some derived subclasses, in which case you should really take a look at the original example), but in essence it all boils down to this. In that case, IceUtil::Handle<T> is the smart pointer i am working with. This class is coming from Ice, so if you’re also using that, you might try creating something similar.

This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Germany License.

2 comments to Using a custom smart pointer with Boost.Python

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>