[C++-sig] wrapping a to_python_converter
Andreas Beyer
beyer at imb-jena.de
Fri Jun 24 03:29:42 CEST 2005
Assume I have a class A and a shared pointer to it A_ptr. BP converts
A_ptr correctly to python after regestring the pointer:
BOOST_PYTHON_MODULE(ptr_test) {
...
python::register_ptr_to_python< A::A_ptr >();
...
}
Now, I want to call a function f() each time a pointer of type A_ptr is
returned to python. The function f() does not modify A, nor does it
change the way A's should be converted to python. In fact, f() only
registers the pointer (see below).
I guess I would somehow have to wrap the existing converter. Is that the
correct idea?
But "where" is this converter? (Thus, how/where does the
register_ptr_to_python template create and register the actual
converter?) Does anybody have an example of such wrapped
to_python_converter?
The questions may sound a bit stupid, but I never have done anything
low-level with BP. Specifically I never wrote a to_python_converter.
In the concrete example I want to call this function before returning A_ptr:
template <class T>
void hold_python(shared_ptr<T>& x)
{
x = python::extract<shared_ptr<T> >( python::object(x) );
}
Here python::object(x) will call the to_python_converter. Thus, if
hold_python() would be called from within a to_python_converter I would
have to avoid recursion, right? I guess hold_python() must explicitely
call the standard to_python_converter. How do I do that?
Thanks!!
Andreas
More information about the Cplusplus-sig
mailing list