Hi YanXi,

On 10/17/2011 05:25 AM, YangXi wrote:
I'm asking a question about how to wrapping a refcounted class inside perl.

I have some experience on embedding perl in C++, but have little
knowledge on perl XS, and I found perldoc perlxs really puzzled.

Firstly, I have an simple refcounted C++ base class like this:

I'm sorry that this isn't a solution for your problem, but maybe it's helpful nonetheless. Object ownership and refcounts are something extremely nasty to deal with when interfacing between two languages that have a very different view of it. For this reason, it's easiest not to have C++ do the refcounting at all. I understand that this might not be possible in your case.

If you just have a regular, non-refcounting object in C++, you can easily use typemaps like O_OBJECT from

http://cpansearch.perl.org/src/MBARBON/ExtUtils-XSpp-0.1602/examples/Object-WithIntAndString/perlobject.map

to have the object wrapped into a Perl scalar. The C++ object will be owned by that scalar and the refcount will be perl's refcount of the SV*. In that case a simple DESTROY XS method will suffice along the lines of the DESTROY here:

http://cpansearch.perl.org/src/SMUELLER/Math-SimpleHisto-XS-1.23/XS.xs

The macro in that case can often be replaced by "delete self" for C++ objects.

I hope this has some general pointers that help a bit.

Best regards,
Steffen

Reply via email to