SV * newSVObj(void *object, char * classname)
{
SV * result;
if (!object)
return newSVsv(&PL_sv_undef);
HV * h = newHV();
hv_store(h, "ETK", 3, newSViv((long)object), 0);
result = newRV((SV*)h);
sv_bless(result, gv_stashpv(classname, FALSE));
SvREFCNT_dec(h);
return result;
}
As you can see, the SV is a hashref storing only a pointer to the original object, and through the class I keep track of the type of the object, so the typemap calls functions like:
SV * newSVEtkButtonPtr(Etk_Button *o) {
return newSVObj(o, "Etk::Button");
}
And when these SVs are passed around as data, the get wrapped into a new SV that is made mortal, for example, the code passing data to the callback function: (cbd being a certain struct that hold relevant information to the callback)
PUSHMARK(SP) ;
XPUSHs(sv_2mortal(newSVsv(cbd->perl_object))); // self
XPUSHs(sv_2mortal(newSVsv(cbd->perl_data))); // data
PUTBACK ;
call_sv(cbd->perl_callback, G_DISCARD);
--
Leviathan. |