On Thu, Dec 06, 2007 at 03:08:09PM +0000, Olly Betts wrote:
> 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.

I had hoped that there would be an easy way to do this, and that someone
would know it.

> 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.

I wondered if not, whether it would be possible to override the XS code
generator to do this for every function, but even though it's now extracted
out from xsubpp into ExtUtils::ParseXS, there's no easy way to get the
new thin xsubpp to call into a different parser, is there?

> 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

I don't have any better ideas than this, I'm afraid.

Nicholas Clark

Reply via email to