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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.