develooper Front page | perl.wxperl.users | Postings from August 2010

Re: Questions about XS, C++ and Perl.

Thread Previous | Thread Next
From:
Mattia Barbon
Date:
August 7, 2010 03:54
Subject:
Re: Questions about XS, C++ and Perl.
Message ID:
4C5D3B3A.1070407@libero.it
Ryan Dietrich wrote:

   Hi,

> Greetings, I've been studying how to interface C++ and Perl using XS.
> I was aware of this project, and saw that it is doing things that are
> currently beyond my skillset.
> 
> I was hoping I could get a few answers.
> 
> 1. What is the general approach of handling exceptions?

   At the moment there is partial support for handling exceptions in 
XS++: by default all exceptions are caught, and the XS wrapper die()s 
with a message.  If the exception is derived from std::exception, the 
message includes the text of the what() method, otherwise a generic 
error message is thrown.  Support for propagating the complete exception 
object to Perl is planned but not implemented yet.

   If for any reason you don't want to use XS++, you need to put a 
try/catch block around the XS code and handle the exceptions manually

int
foo(int bar)
   CODE:
     try [
       RETVAL = foo(bar);
     } catch (...) {
       ...
     }
   OUTPUT: RETVAL

> 2. How do you map multiple custom C++ objects out of a single header file
> using XS?

   There are two approaches:

1 - you can split the XS code in multiple XS files and then include them 
all in a single top-level file, using the INCLUDE: directive; in this 
case you do not need to do anything special
2 - if you do it the way Wx does, you need to list all the object files 
for XS top-level modules in the OBJECTS MakeMaker parameter; every 
top-level XS file must have a different MODULE directive; one of the XS 
files must have a MODULE directive matching the .pm module name, and in 
this module you need to add this code:

// in the C section
extern "C" {
     XS( boot_SubModule1 );
     XS( boot_Another_Sub_Module );
     ...
}

// in the XS section:
BOOT:
   newXSproto( "My::_boot_SubModule1", boot_SubModule1, file, "$$" );
   ...

// in the My.pm file
_boot_SubModule1( 'My', $XS_VERSION );

   Note that Wx uses a hybrid approach (multiple top-level XS files that 
each include multiple XS files from the XS subdirectory).

> For reference, here's my post on Perlmonks (it's a little wordy).
> 
> http://perlmonks.org/?node_id=853194

HTH,
Mattia


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About