[C++-sig] void* (void pointer) function arguments

Jim Bosch talljimbo at gmail.com
Mon Aug 15 20:44:36 CEST 2011


On 08/15/2011 08:11 AM, Holger Joukl wrote:
>
> Hi,
>
> I'm trying to wrap a C++-API that uses void* to pass around arbitrary,
> application-specific stuff.
>
> I am a bit unsure about how to work the void-pointers. A viable way seems
> to wrap the original
> API functions with thin wrappers that take a boost::python::object and hand
> the "raw" PyObject*
> into the original function:
>

I don't see anything wrong with this approach, aside from the fact that 
you have to make an explicit wrapper for all of your functions that take 
void pointers.  It might be a little less safe, but you should be able 
to make things more automatic by casting function pointers that accept a 
void* to a signature with void* replaced by PyObject*; Boost.Python does 
know how to wrap functions that take PyObject*.  For example, in the 
wrapper for your "Worker" class, you'd have:

.def("destroy2",
     (int (Worker::*)(DestructionCallback*,PyObject*))&Worker::destroy,
     (bp::arg("cb"), bp::arg("closure")=bp::object())
)

I think this should do the same thing, though you may want to check that 
default arguments work as expected.  Of course this could lead to big 
problems if you pass around things that aren't in fact PyObject* as void 
pointers in the same places, though I think your solution suffers from 
this too.

There really isn't anything automatic for dealing with void pointers in 
Boost.Python, because it's so template-based - it really can't learn 
anything from a void pointer.

Good luck!

Jim Bosch


More information about the Cplusplus-sig mailing list