My first typemap

ethan on 2002-12-12T10:22:07

I just came up with my first working INPUT custom type-map that converts a hash-ref into an aa_renderparams C-struct. Looks slightly untypical for a typemap but it gets the job done quite nicely:

AA_RENDER { SV **val; HV *h = (HV*)SvRV($arg); $var = (aa_renderparams*)safemalloc(sizeof(aa_renderparams)); val = hv_fetch(h, \"-bright\", 7, FALSE); $var->bright = val ? (int) SvIV(*val) : 0; val = hv_fetch(h, \"-contrast\", 9, FALSE); $var->contrast = val ? (int) SvIV(*val) : 127; /* and so forth for the other members */ }

You really gotta love these typemaps.

After a while one starts to understand how XS works: It's basically one big preprocessor. xsubpp takes the default typemap plus your custom one and along with that runs through your .xs file to produce an eventually compilable .c file. This is very nice for Perl-programmers because if there is one thing they know about it is text-processing. And if anything doesn't quite work, you just look into the generated .c code and quickly find the delinquent.

I wonder how long it took other people to find it out (I was probably one of the slower chaps).

And before I forget! Kudos to nicholas for his ExtUtils::Constant module. I followed his advice to use 5.8.0 as development environment, passed the -b switch to h2xs and am happy ever since! It even produced a test-file for all the constants (something I previously had to do myself for MP3::Mplib). I was a little anxious at first because I thought that using this module would clearly infer some extra work (like some adaptions to the Makefile.PL or so) to make it work properly. But none at all! All worked splendidly out of the box. Thanks, Nick!