Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: XS from XS?

by etj (Priest)
on Apr 04, 2023 at 17:17 UTC ( [id://11151470]=note: print w/replies, xml ) Need Help??


in reply to Re: XS from XS?
in thread XS from XS?

This is a situation where "X macros" (https://en.wikipedia.org/wiki/X_Macro) really help, as used by PDL. It used to have highly redundant, multiple lists of the PDL API functions in several files. Now it uses the "X macro" technique, which for this application would look like (untested - see https://github.com/PDLPorters/pdl/blob/master/Basic/Core/pdlcore.h for defines and use, and https://github.com/PDLPorters/pdl/blob/master/Basic/Core/Core.xs for use, of PDL_CORE_LIST):
/* in C part of .xs, else in a .h */ #define SNOWBALL_FUNCS(X) \ X(list, 39) \ X(new, 38) \ X(delete, 41) \ X(stem, 39) \ X(length, 41) MODULE = Lingua::Stem::Snowball PACKAGE = Lingua::Stem::Snowball PROTOTYPES: disable BOOT: { #ifdef I_HAVE_C99 /* not a real symbol, don't use this */ #define X(symname, no, ...) \ SV *sb_stemmer_ ## symname ## _sv = newSViv(PTR2IV(sb_stemmer_ # +# symname)); \ hv_store(PL_modglobal, "Lingua::Stem::Snowball::sb_stemmer_" #symn +ame, no, sb_stemmer_ ## symname ## _sv, 0); SNOWBALL_FUNCS(X) #undef X #else #define X(symname, no, ...) \ SV *sb_stemmer_ ## symname ## _sv = newSViv(PTR2IV(sb_stemmer_ # +# symname)); SNOWBALL_FUNCS(X) #undef X #define X(symname, no, ...) \ hv_store(PL_modglobal, "Lingua::Stem::Snowball::sb_stemmer_" #symn +ame, no, sb_stemmer_ ## symname ## _sv, 0); SNOWBALL_FUNCS(X) #undef X #endif }
The biggest benefit is maintainability. You'll see the PDL example also captures the C functions' return types and argument types, so adding to the API is a matter of adding lines to the big macro calling X.

Edited to add how to use this if you're not using C99, where you can declare things anywhere.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11151470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2026-04-21 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.