It seems as though I might be the first (or one of them) to be writing a
C++ extension to Perl. In particular none of the predefined typemaps
quite worked for me.
(Feel free to let me know if I've taken the wrong path, am reinventing
the wheel, etc.) I am leaving the process of compilation of XSUBs in C,
and linking it with C++ functions declared with C linkage (that is,
using the 'extern "C"' declaration of C++). This necessitates an extra
C++ wrapper function with C linkage for every C++ method I want to
appear in Perl, but the arguments I am converting back and forth to Perl
require complex enough logic that a wrapper is usually justified in any
case.
The class Marpa appears to C as a struct, so the return of constructors
and the first argument on non-static methods is of the form "struct
Marpa *" in C. If one uses the T_PTROBJ typemap, as suggested in the
perlxs pod, the result is not quite right: constructors returned refs
which appear to be blessed into packages "struct MarpaPtr". I slightly
modified the typemap T_PTROBJ so it bless objects into the package
specified in the XS file rather than a new package created from the type
name of the corresponding C struct.
The result follows:
TYPEMAP
struct Marpa* T_CLASS
INPUT
T_CLASS
if (sv_isa($arg, \"$Package\")) {
IV tmp = SvIV((SV*)SvRV($arg));
$var = ($type) tmp;
}
else
croak(\"$var is not of type Marpa\")
OUTPUT
T_CLASS
sv_setref_pv($arg, \"$Package\", (void*)$var);
Should the T_CLASS typemap be added to the list of defaults? Or is
my way of interfacing C++ to Perl idiosyncratic?
Cheers!
Jeffrey Kegler, Algorists, Inc.
jeffrey@algorists.com, http://www.best.com/~jeffrey
743 East El Camino Real #338, Sunnyvale CA 94087
C++ extension to Perl. In particular none of the predefined typemaps
quite worked for me.
(Feel free to let me know if I've taken the wrong path, am reinventing
the wheel, etc.) I am leaving the process of compilation of XSUBs in C,
and linking it with C++ functions declared with C linkage (that is,
using the 'extern "C"' declaration of C++). This necessitates an extra
C++ wrapper function with C linkage for every C++ method I want to
appear in Perl, but the arguments I am converting back and forth to Perl
require complex enough logic that a wrapper is usually justified in any
case.
The class Marpa appears to C as a struct, so the return of constructors
and the first argument on non-static methods is of the form "struct
Marpa *" in C. If one uses the T_PTROBJ typemap, as suggested in the
perlxs pod, the result is not quite right: constructors returned refs
which appear to be blessed into packages "struct MarpaPtr". I slightly
modified the typemap T_PTROBJ so it bless objects into the package
specified in the XS file rather than a new package created from the type
name of the corresponding C struct.
The result follows:
TYPEMAP
struct Marpa* T_CLASS
INPUT
T_CLASS
if (sv_isa($arg, \"$Package\")) {
IV tmp = SvIV((SV*)SvRV($arg));
$var = ($type) tmp;
}
else
croak(\"$var is not of type Marpa\")
OUTPUT
T_CLASS
sv_setref_pv($arg, \"$Package\", (void*)$var);
Should the T_CLASS typemap be added to the list of defaults? Or is
my way of interfacing C++ to Perl idiosyncratic?
Cheers!
Jeffrey Kegler, Algorists, Inc.
jeffrey@algorists.com, http://www.best.com/~jeffrey
743 East El Camino Real #338, Sunnyvale CA 94087