user1446426 user1446426 - 8 months ago 27
Perl Question

Perl XS and C++ passing pointer to buffer

I know almost no C++ so that's not helping, and my XS isn't much better. I'm creating an XS interface for a C++ library and I have almost all my methods working except one.

The method in Perl should look like this:

$return_data = $obj->readPath( $path );

The method is defined as this the .h file:

int readPath(const char* path, char* &buffer, bool flag=true);

The "buffer" will get allocated if it's passed in NULL.

There's two additional versions of readPath with different signatures, but they are not the ones I want. (And interestingly, when I try and compile it tells me the "candidates" are the two I don't want.) Is that because it's not understanding the "char * &"?

Can someone help with the xsub I need to write?

I'm on Perl 5.14.2.

BTW -- I've also used a typemap "long long int" to T_IV. I cannot find any documentation on how to correctly typemap long long. Any suggestions how I should typemap long long?



I've never dealt with C++ from C or XS. If it was C, it would be:

readPath(SV* sv_path)
         char*  path   = SvPVbyte_nolen(sv_path, len);
         char*  buffer = NULL;

         if (!readPath(path, &buffer, 0))

         ST(0) = sv_2mortal(newSVpv(buffer, 0));


Hopefully, that works or you can adjust it to work.

I assumed:

  • readPath returns true/false for success/failure.
  • buffer isn't allocated on failure.
  • The deallocator for buffer is free.