Olly Betts
2007-12-06 15:08:09 UTC
I'm wrapping a C++ library, and wanting to add something like this
around (almost) every method:
try {
[code which calls the method]
} catch (...) {
convert_to_a_perl_exception();
}
And then convert_to_a_perl_exception() rethrows and recatches the
exception to find what type it is and so how to convert it to a Perl
exception.
Is there a way to insert a standard wrapper like this around every XS
wrapped method in a .xs file? Bonus points if certain methods can be
excluded (ones which are documented to never throw exceptions), although
there aren't many of those, so wrapping everything is acceptable.
Looking at the documentation, I can see how I can specify it for each
method using INIT and CLEANUP, but I don't see how to specify it just
once per file.
My best idea so far is to define C macros for the two chunks of
exception handling code, so at least I only have to insert four standard
lines for each method which requires exception handling. Something
like:
#define TRY try {
#define CATCH } catch (...) { convert_to_a_perl_exception(); }
[...]
INIT:
TRY
CLEANUP:
CATCH
Cheers,
Olly
around (almost) every method:
try {
[code which calls the method]
} catch (...) {
convert_to_a_perl_exception();
}
And then convert_to_a_perl_exception() rethrows and recatches the
exception to find what type it is and so how to convert it to a Perl
exception.
Is there a way to insert a standard wrapper like this around every XS
wrapped method in a .xs file? Bonus points if certain methods can be
excluded (ones which are documented to never throw exceptions), although
there aren't many of those, so wrapping everything is acceptable.
Looking at the documentation, I can see how I can specify it for each
method using INIT and CLEANUP, but I don't see how to specify it just
once per file.
My best idea so far is to define C macros for the two chunks of
exception handling code, so at least I only have to insert four standard
lines for each method which requires exception handling. Something
like:
#define TRY try {
#define CATCH } catch (...) { convert_to_a_perl_exception(); }
[...]
INIT:
TRY
CLEANUP:
CATCH
Cheers,
Olly