Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Using XS To Wrap A C++ Smart Pointer

by williams (Beadle)
on Sep 28, 2009 at 18:04 UTC ( #797970=perlquestion: print w/ replies, xml ) Need Help??
williams has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use XS to wrap a C++ class that is accessible only through a separate, smart pointer class. So it appears that Perl objects should store a pointer to a smart pointer object. The double dereference necessary to call the methods I'm after is going to lead to miles of this.
bool SmartPtr::isValid() CODE: RETVAL=(*THIS)->isValid(); OUTPUT: RETVAL const char *name SmartPtr::name() CODE: RETVAL=(*THIS)->name(); OUTPUT: RETVAL
I'd much rather the XS compiler somehow automate all of this repetition, given something more along these lines.
bool SmartPtr::isValid() const char * SmartPtr::name()
Is there some sneaky XS directive or typemap game that will get me there with minimal fuss? The best alternative I can come up with is to write an xsubpppp.

Thanks,

Jim

Comment on Using XS To Wrap A C++ Smart Pointer
Select or Download Code
Re: Using XS To Wrap A C++ Smart Pointer
by tsee (Curate) on Sep 28, 2009 at 21:09 UTC

    I'm not aware of a sort of "DELEGATE" XS directive. But maybe some hacking with INTERFACE and INTERFACE_MACRO could help reduce the amount of code (depending on the number of different C method signatures).

    Furthermore, you may be able to play tricks with typemaps, i.e. have the smart pointer dereferenced there.

    Finally, you may want to have a look at the ExtUtils::XSpp module on CPAN and specifically the ultra-simple example in its examples/ subdirectory. It's meant to make wrapping C++ easier and while its documentation isn't very complete, the code should rather accessible (compared to ExtUtils::ParseXS) and we are open for improvements. There is even a simple plugin mechanism (without docs so far).

    Cheers,
    Steffen

Re: Using XS To Wrap A C++ Smart Pointer
by Anonymous Monk on Sep 28, 2009 at 23:25 UTC
    I don't know enough about show an example but I think you need ExtUtils::XSpp -- XS for C++
Re: Using XS To Wrap A C++ Smart Pointer
by williams (Beadle) on Sep 29, 2009 at 18:39 UTC
    I was able to follow the typemap suggestion, adding a mapping that eliminates most cases of the repeated code I wanted to avoid. It's slightly nefarious, since I'm sneaking around the back of the smart pointer, accessing the underlying pointer directly, but I think it's justified within these internals.

    An issue I didn't mention in the original post was that the smart pointers were pointing at objects belonging to a large class hierarchy. I'm using boost::shared_ptr for this, since it provides nice support for implicit conversions from smart pointers to derived classes, to smart pointers to their base classes. Dealing with this in the XS required writing a small class hierarchy that wraps these smart pointers, but restores the original class hierarchy that the smart pointers concealed.

    Thanks for the help.

    Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://797970]
Approved by Bloodnok
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-07-24 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (158 votes), past polls