From: François Duranleau (duranlef_at_[hidden])
Date: 2007-02-23 11:48:39
I have been struggling again recently with Boost.Function and Python
bindings, and in order to use this library transparently from the Python
side, I need to extract the contained function/functor, but only the type
information available is a std::type_info object (available now with the
new target_type() member function in RC_1_34_0), so I can not simply use
the member function target<>(). Thus, I need an extra function to get the
"raw" pointer to the contained function/functor.
I played with the Boost.Function source code a little bit to see how it
could be done. We only need to patch the file
boost/function/function_base.hpp. The patched file is available here:
I also put a patch file (a diff with the original function_base.hpp in
RC_1_34_0) and a test file.
The idea is, of course, to expose in some way the internal functor member
(a detail::function::function_buffer object). I chose to simply add two
void (*func_ptr() const)();
const void* obj_ptr() const;
The distinction is required because, as we know, pointers to function and
regular pointers can not be cast to and from each other. func_ptr()
returns 0 if the content is actually a functor, and similarly, obj_ptr()
returns 0 if the content is actually a pointer to function. Thus at the
same time, it provides a way to test if the content is a pointer to
function or not.
Is there any interest to add this functionality to Boost.Function?
-- François Duranleau LIGUM, Université de Montréal
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk