#ifndef __PerlFunction_h__ #define __PerlFunction_h__ /** * No special support needed. SWIG type conversions are included which * allow passing anonymous subs as callbacks of type boost::function * or void(void). You may pass subroutine references or anonymous subs * * Note this never deletes the helper class created. So each and every * callback creation will leak memory. Live with it. (it shouldn't be * a problem unless you're deregistering and reregistering callbacks * all day) **/ #include #include #include #ifdef SWIG %typemap(in) boost::function { PerlFunction* pp = new PerlFunction($input); $1=pp->cCallback(); } %typemap(in) void(void) = boost::function; %{ #include "PerlFunction.h" %} #endif class PerlFunction : private boost::noncopyable { private: SV * _cvoid; void cbwrap_cvoid() { SV *sv; dSP; if(!_cvoid)return ; ENTER; SAVETMPS; PUSHMARK(sp); PUTBACK; perl_call_sv(SvRV(_cvoid),G_EVAL|G_SCALAR); SPAGAIN; sv=POPs; PUTBACK; FREETMPS; LEAVE; return ; } public: PerlFunction(SV* plfunc) : _cvoid((SV*)NULL) { if(!SvROK(plfunc) || SvTYPE(SvRV(plfunc))!=SVt_PVCV) { croak("Not a Callback %_ for PerlFunction",plfunc); } _cvoid = newSVsv(plfunc); } boost::function cCallback() { return boost::bind(&PerlFunction::cbwrap_cvoid,this); } }; #endif