Ellipsis in perl XS
tbusch
created: 2006-09-04 18:20:09
Hi all, according to the perl XS documentation "XSUBs can have variable-length parameter lists by specifying an ellipsis (...) in the parameter list.". It seems however that I cannot pass an array of objects that would require a typemap. Is that true ? Does anyone have an example that proves the contrary ?
Re: Ellipsis in perl XS
created: 2006-09-04 18:32:06

That's exactly right.

If you want to pass a list of objects (let's say pointers of type foo*) that require a typemap you usually create a typemap that simply calls a function (let's say SvFoo()) to convert a foo pointer to an SV:

TYPEMAP
foo* T_FOO

INPUT
T_FOO
    $var = ($type)SvFoo($var);

And in your XS code do:

void
my_xsub(...)
    PREINIT:
        int i;
        foo **foos = NULL;
    CODE:
        foos = (foo**)malloc(sizeof(foo*) * items);
        for (i = 0; i < items; i++) {
            foos[i] = SvFoo(ST(i));
        }
        # do something with foos

Now the only thing you need to do is to define a SvFoo function which might look like that:

foo*
SvFoo(SV* foo) {
    SV* ret;
    IV tmp = SvIV((SV*)SvRV(foo));
    ret = INT2PTR(foo*, tmp);
}

Cheers, Flo

perlmonks.org content © perlmonks.org and rafl, tbusch

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03