In my XS file I have:
As my new method:
matrix *
matrix::new( size_t ncols, size_t nrows )
which returns a matrix
object like it should and I can invoke methods.
Then I have a method call which creates a new matrix object and is supposed to return it as a new matrix:
matrix *
matrix::getInnerMatrix( )
PREINIT:
char * CLASS = (char *)SvPV_nolen(ST(0));
CODE:
RETVAL = static_cast<matrix*>(THIS->matrix::getInnerMatrix());
OUTPUT:
RETVAL
However the returned type is matrix=SCALAR(0x122f81c)
and therefore I am unable to invoke any method calls from this object as the perl interpreter seems to be viewing the returned type as a scalar value type instead of a 'matrix' object. Here is a test script:
$m1 = matrix::new(matrix,4,4);
@arr = ( 1 .. 16 );
$aref = [@arr];
$m1->assign_aref($aref);
my $m2 = $m1->getInnerMatrix();
print ref $m1; # returns "matrix" (like it should)
print "\n\n";
print ref $m2; # returns "matrix=SCALAR(0x122f81c)" (wrong)
Here is my typemap:
TYPEMAP
matrix * O_MATRIX
OUTPUT
O_MATRIX
sv_setref_pv( $arg, CLASS, (void*)$var );
INPUT
O_MATRIX
if ( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) ) {
$var = ($type)SvIV((SV*)SvRV( $arg ));
}
else {
warn( \"${Package}::$func_name() -- ${var} not a blessed SV reference\" );
XSRETURN_UNDEF;
}
What changes must I make in my XS file, or any other file to ensure that a pure matrix
object is returned?
CLASS
? Note thatSvPV_nolen(scalar)
works pretty much like"$scalar"
, i.e. stringifies the reference which by default looks likeClassName=HASH(0xabc123)
or similar. – Baronetagematrix*
as well? – Baronetagechar * CLASS = (char *)SvPV_nolen(ST(0));
because before, when I didn't have it in there, I was getting the compiler errorerror: 'CLASS' was not declared in this scope sv_setref_pv( RETVALSV, CLASS, (void*)RETVAL );
. After adding that line, it compiles. – Maitildenew
a method?! – Kickmatrix->new(...);
to create an object. – Kicknew
is correct. You just need to replace$m1 = matrix::new(matrix,4,4);
withmy $m1 = matrix->new(4,4);
. Your version doesn't even work withuse strict;
. Always useuse strict; use warnings qw( all );
!!! – Kick