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

Holger Joukl Holger.Joukl at LBBW.de
Tue Aug 16 11:31:03 CEST 2011


Hi Jim,

> > 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:

Just tested, it works:

BOOST_PYTHON_MODULE(void_ptr_cb)
{
    bp::class_<DestructionCallbackWrap, boost::noncopyable>
("DestructionCallback", bp::init<>())
        .def("callback", bp::pure_virtual(&DestructionCallback::callback))
    ;
    bp::class_<WorkerWrap, boost::noncopyable>("Worker")
        .def("destroy", &Worker::destroy, &WorkerWrap::default_destroy)
        .def("destroy2", (int (Worker::*)(DestructionCallback*,
PyObject*))&Worker::destroy2,
             (bp::arg("cb"), bp::arg("closure")=bp::object()))
        ;
};

So I could save the thin wrappers that get called from the Python side.

> 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.

Absolutely :).
The default args seem to work fine with your proposal, though.

> Good luck!

Thanks a lot and all the best

Holger

Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart



More information about the Cplusplus-sig mailing list