Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Terrible Trouble With Typemaps (XS Question)

by Molt (Chaplain)
on Sep 07, 2004 at 09:21 UTC ( [id://388972]=perlquestion: print w/replies, xml ) Need Help??

Molt has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently working on connecting Perl with C++ via XS, only I'm hitting a problem when it comes to typemaps.. or lack of same.

At the moment I'm handling C++'s method overloading with something with the following basic structure (plus extra error checking, removed here for clarity):

int MyClass::countItemsIn (...) CODE: if (sv_derived_from(ST(0), 'MyList')) { RETVAL=THIS->countItemsIn( *(MyList *) SvIV((SV*)SvRV(ST(0))) ); } elsif (sv_derived_from(ST(0), 'MyHash')) { RETVAL=THIS->countItemsIn( *(MyHash *) SvIV((SV*)SvRV(ST(0))) ); } OUTPUT: RETVAL

..now I can happily write this kind of code, but what's worrying me is "Where are the typemaps?". If I were to do all of the conversions manually I'm a lot more likely to make mistakes, and basically mess it all up. I'm also considering wrapping things in a very different way so being able to change the effective mapping would be exceptionally useful.

What is the best way to approach this, is there a way to call typemap conversions as 'functions' in XS? If not, shall I wrap the XS code for the typemap conversion in a nice little method and call that?

EDIT: Changed wording slightly, this doesn't seem to be the 'best' version to me at all..

Replies are listed 'Best First'.
Re: Terrible Trouble With Typemaps (XS Question)
by dragonchild (Archbishop) on Sep 07, 2004 at 12:25 UTC
    (I've never written XS, so take me with a grain of salt.)

    When dealing with this kind of thing in C, we'd use a macro #define to make our lives easier. So, I could see something like:

    #define CALL_COUNT_ITEMS_IF( type ) \ if (sv_derived_from(ST(0), type)) { \ RETVAL=THIS->countItemsIn( \ *(type *) SvIV((SV*)SvRV(ST(0))) \ ); \ } int MyClass::countItemsIn (...) CODE: CALL_COUNT_ITEMS_IF( 'MyList' ); CALL_COUNT_ITEMS_IF( 'MyHash' ); OUTPUT: RETVAL

    Again, I have no idea if XS provides for a pre-processor or not. Hope this helps!

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      Thanks for the help, to be honest I didn't consider the preprocessor since I tend to avoid it as much as possible in the interests of readability.. but in this case it may have a use.

      Just so you know, it will be more a case of:

      int MyClass::countItemsIn (...) CODE: if (SV_IS_MYLIST(ST(0))) { THIS->countItemsIn ( CAST_SV_TO_MYLIST(ST(0)) ); } elsif (SV_IS_MYHASH(ST(0)) { THIS->countItemsIn ( CAST_SV_TO_MYHASH(ST(0)) ); }; OUTPUT: RETVAL

      ..where the SV_IS_MYLIST/CAST_SV_TO_MYLIST parts are all macros.

        What about the following:
        CODE: if ( MY_SV_IS( ST(0), 'MyList' )) { THIS->count_Items_in( MY_CAST_SV( ST(0), 'MyList' ) } elsif (MY_SV_IS( ST(0), 'MyHash' )) { THIS->count_Items_in( MY_CAST_SV( ST(0), 'MyHash' ) }

        This way, you don't have a plethora of macros. Remember, a preprocessor macro is just a direct text substitution. So, you can put text in there, kinda like what I was doing in my first reply.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://388972]
Approved by Arunbear
Front-paged by dragonchild
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found