[C++-sig] optimizing away calls to the python runtime -- was [detecting if a function is written in python or C++]
Mathieu Lacage
Mathieu.Lacage at sophia.inria.fr
Fri Jan 27 10:14:26 CET 2006
On Wed, 2006-01-18 at 15:09 -0500, François Duranleau wrote:
> You can do something similar with methods by adding a member 'self' in the
> wrapper (pointer to the object), a constructor with the object to bind to
> the method as an argument, and calling the method with ->. in operator().
> Actually, I think you could even have a single wrapper for both functions
> and method using boost::function and boost::bind.
I ended up doing this:
template<typename TAG, typename R, typename T, typename T1>
struct CallbackGenerator {
typedef R (T::*member_type) (T1);
static std::auto_ptr<Callback<R (T1)> >
make_callback (T *self){
return std::auto_ptr<Callback<R (T1)> > (yans::make_callback (CallbackGenerator::m_member, self));
}
static member_type m_member;
};
template<typename TAG,typename R,typename T,typename T1>
R (T::* CallbackGenerator<TAG,R,T,T1>::m_member ) (T1);
template <typename TAG, typename R, typename T, typename T1>
void
export_method_as_callback (R (T::*member) (T1), char const *name)
{
CallbackGenerator<TAG, R, T, T1>::m_member = member;
def (name, &CallbackGenerator<TAG,R,T,T1>::make_callback);
class_<Callback<R (T1)>, std::auto_ptr<Callback<R (T1)> >, boost::noncopyable> ("Callback", no_init);
}
with this:
export_method_as_callback<struct foo> (&TrafficAnalyser::receive,
"make_TrafficAnalyser_receive_callback");
and that:
void set_send_callback_cpp (PeriodicGenerator *self, std::auto_ptr<Callback<void (Packet *)> > *callback)
{
self->set_send_callback (callback->get ());
callback->release ();
}
and this results in this:
without the call to export_method_as_callback:
[mlacage at chronos yans-current]$ time -p ./bin/python/test-periodic-generator.py >/dev/null
real 12.96
user 12.81
sys 0.02
with the call:
[mlacage at chronos yans-current]$ time -p ./bin/python/test-periodic-generator.py >/dev/null
real 3.10
user 2.99
sys 0.01
which I think is very nice.
thanks again for your help,
Mathieu
--
More information about the Cplusplus-sig
mailing list