// This library provides standard typemaps for modifying SWIG's behavior.
// With enough entries in this file, I hope that very few people actually
// ever need to write a typemap.
The SWIG typemap library provides a language independent mechanism for
supporting output arguments, input values, and other C function
calling mechanisms. The primary use of the library is to provide a
better interface to certain C function--especially those involving
// These remap a C pointer to be an "INPUT" value which is passed by value
The following methods can be applied to turn a pointer into a simple
"input" value. That is, instead of passing a pointer to an object,
you would use a real value instead.
unsigned long long *INPUT
To use these, suppose you had a C function like this :
double fadd(double *a, double *b) {
You could wrap it with SWIG as follows :
double fadd(double *INPUT, double *INPUT);
or you can use the %apply directive :
%apply double *INPUT { double *a, double *b };
double fadd(double *a, double *b);
%define INPUT_TYPEMAP(type, converter)
%typemap(in) type *INPUT(type temp), type &INPUT(type temp) {
temp = (type) converter($input);
%typemap(typecheck) type *INPUT = type;
%typemap(typecheck) type &INPUT = type;
INPUT_TYPEMAP(float, SvNV);
INPUT_TYPEMAP(double, SvNV);
INPUT_TYPEMAP(int, SvIV);
INPUT_TYPEMAP(long, SvIV);
INPUT_TYPEMAP(short, SvIV);
INPUT_TYPEMAP(signed char, SvIV);
INPUT_TYPEMAP(unsigned int, SvUV);
INPUT_TYPEMAP(unsigned long, SvUV);
INPUT_TYPEMAP(unsigned short, SvUV);
INPUT_TYPEMAP(unsigned char, SvUV);
%typemap(in) bool *INPUT(bool temp), bool &INPUT(bool temp) {
temp = SvIV($input) ? true : false;
%typemap(typecheck) bool *INPUT = bool;
%typemap(typecheck) bool &INPUT = bool;
%typemap(in) long long *INPUT($*1_ltype temp), long long &INPUT($*1_ltype temp) {
temp = strtoll(SvPV($input,PL_na), 0, 0);
%typemap(typecheck) long long *INPUT = long long;
%typemap(typecheck) long long &INPUT = long long;
%typemap(in) unsigned long long *INPUT($*1_ltype temp), unsigned long long &INPUT($*1_ltype temp) {
temp = strtoull(SvPV($input,PL_na), 0, 0);
%typemap(typecheck) unsigned long long *INPUT = unsigned long long;
%typemap(typecheck) unsigned long long &INPUT = unsigned long long;
// OUTPUT typemaps. These typemaps are used for parameters that
// are output only. The output value is appended to the result as
The following methods can be applied to turn a pointer into an "output"
value. When calling a function, no input value would be given for
a parameter, but an output value would be returned. In the case of
multiple output values, functions will return a Perl array.
unsigned long long *OUTPUT
For example, suppose you were trying to wrap the modf() function in the
C math library which splits x into integral and fractional parts (and
returns the integer part in one of its parameters).:
double modf(double x, double *ip);
You could wrap it with SWIG as follows :
double modf(double x, double *OUTPUT);
or you can use the %apply directive :
%apply double *OUTPUT { double *ip };
double modf(double x, double *ip);
The Perl output of the function would be an array containing both
// Force the argument to be ignored.
%typemap(in,numinputs=0) int *OUTPUT(int temp), int &OUTPUT(int temp),
short *OUTPUT(short temp), short &OUTPUT(short temp),
long *OUTPUT(long temp), long &OUTPUT(long temp),
unsigned int *OUTPUT(unsigned int temp), unsigned int &OUTPUT(unsigned int temp),
unsigned short *OUTPUT(unsigned short temp), unsigned short &OUTPUT(unsigned short temp),
unsigned long *OUTPUT(unsigned long temp), unsigned long &OUTPUT(unsigned long temp),
unsigned char *OUTPUT(unsigned char temp), unsigned char &OUTPUT(unsigned char temp),
signed char *OUTPUT(signed char temp), signed char &OUTPUT(signed char temp),
bool *OUTPUT(bool temp), bool &OUTPUT(bool temp),
float *OUTPUT(float temp), float &OUTPUT(float temp),
double *OUTPUT(double temp), double &OUTPUT(double temp),
long long *OUTPUT($*1_ltype temp), long long &OUTPUT($*1_ltype temp),
unsigned long long *OUTPUT($*1_ltype temp), unsigned long long &OUTPUT($*1_ltype temp)
%typemap(argout) int *OUTPUT, int &OUTPUT,
short *OUTPUT, short &OUTPUT,
long *OUTPUT, long &OUTPUT,
signed char *OUTPUT, signed char &OUTPUT,
bool *OUTPUT, bool &OUTPUT
$result = sv_newmortal();
sv_setiv($result,(IV) *($1));
%typemap(argout) unsigned int *OUTPUT, unsigned int &OUTPUT,
unsigned short *OUTPUT, unsigned short &OUTPUT,
unsigned long *OUTPUT, unsigned long &OUTPUT,
unsigned char *OUTPUT, unsigned char &OUTPUT
$result = sv_newmortal();
sv_setuv($result,(UV) *($1));
%typemap(argout) float *OUTPUT, float &OUTPUT,
double *OUTPUT, double &OUTPUT
$result = sv_newmortal();
sv_setnv($result,(double) *($1));
%typemap(argout) long long *OUTPUT, long long &OUTPUT {
sprintf(temp,"%lld", (long long)*($1));
$result = sv_newmortal();
%typemap(argout) unsigned long long *OUTPUT, unsigned long long &OUTPUT {
sprintf(temp,"%llu", (unsigned long long)*($1));
$result = sv_newmortal();
// Mappings for an argument that is both an input and output
The following methods can be applied to make a function parameter both
an input and output value. This combines the behavior of both the
"INPUT" and "OUTPUT" methods described earlier. Output values are
returned in the form of a Perl array.
unsigned long long *INOUT
For example, suppose you were trying to wrap the following function :
You could wrap it with SWIG as follows :
or you can use the %apply directive :
%apply double *INOUT { double *x };
Unlike C, this mapping does not directly modify the input value.
Rather, the modified input value shows up as the return value of the
function. Thus, to apply this function to a Perl variable you might
%typemap(in) int *INOUT = int *INPUT;
%typemap(in) short *INOUT = short *INPUT;
%typemap(in) long *INOUT = long *INPUT;
%typemap(in) unsigned *INOUT = unsigned *INPUT;
%typemap(in) unsigned short *INOUT = unsigned short *INPUT;
%typemap(in) unsigned long *INOUT = unsigned long *INPUT;
%typemap(in) unsigned char *INOUT = unsigned char *INPUT;
%typemap(in) signed char *INOUT = signed char *INPUT;
%typemap(in) bool *INOUT = bool *INPUT;
%typemap(in) float *INOUT = float *INPUT;
%typemap(in) double *INOUT = double *INPUT;
%typemap(in) long long *INOUT = long long *INPUT;
%typemap(in) unsigned long long *INOUT = unsigned long long *INPUT;
%typemap(in) int &INOUT = int &INPUT;
%typemap(in) short &INOUT = short &INPUT;
%typemap(in) long &INOUT = long &INPUT;
%typemap(in) unsigned &INOUT = unsigned &INPUT;
%typemap(in) unsigned short &INOUT = unsigned short &INPUT;
%typemap(in) unsigned long &INOUT = unsigned long &INPUT;
%typemap(in) unsigned char &INOUT = unsigned char &INPUT;
%typemap(in) signed char &INOUT = signed char &INPUT;
%typemap(in) bool &INOUT = bool &INPUT;
%typemap(in) float &INOUT = float &INPUT;
%typemap(in) double &INOUT = double &INPUT;
%typemap(in) long long &INOUT = long long &INPUT;
%typemap(in) unsigned long long &INOUT = unsigned long long &INPUT;
%typemap(argout) int *INOUT = int *OUTPUT;
%typemap(argout) short *INOUT = short *OUTPUT;
%typemap(argout) long *INOUT = long *OUTPUT;
%typemap(argout) unsigned *INOUT = unsigned *OUTPUT;
%typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT;
%typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT;
%typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT;
%typemap(argout) signed char *INOUT = signed char *OUTPUT;
%typemap(argout) bool *INOUT = bool *OUTPUT;
%typemap(argout) float *INOUT = float *OUTPUT;
%typemap(argout) double *INOUT = double *OUTPUT;
%typemap(argout) long long *INOUT = long long *OUTPUT;
%typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
%typemap(argout) int &INOUT = int &OUTPUT;
%typemap(argout) short &INOUT = short &OUTPUT;
%typemap(argout) long &INOUT = long &OUTPUT;
%typemap(argout) unsigned &INOUT = unsigned &OUTPUT;
%typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT;
%typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT;
%typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT;
%typemap(argout) signed char &INOUT = signed char &OUTPUT;
%typemap(argout) bool &INOUT = bool &OUTPUT;
%typemap(argout) float &INOUT = float &OUTPUT;
%typemap(argout) double &INOUT = double &OUTPUT;
%typemap(argout) long long &INOUT = long long &OUTPUT;
%typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;
// Accept Perl references as pointers
The following methods make Perl references work like simple C
pointers. References can only be used for simple input/output
values, not C arrays however. It should also be noted that
REFERENCES are specific to Perl and not supported in other
scripting languages at this time.
unsigned short *REFERENCE
For example, suppose you were trying to wrap the following function :
You could wrap it with SWIG as follows :
void neg(double *REFERENCE);
or you can use the %apply directive :
%apply double *REFERENCE { double *x };
Unlike the INOUT mapping described previous, this approach directly
modifies the value of a Perl reference. Thus, you could use it
print "$x\n"; # Should print out -3.
%typemap(in) double *REFERENCE (double dvalue), double &REFERENCE(double dvalue)
SWIG_croak("expected a reference");
if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
printf("Received %d\n", SvTYPE(tempsv));
SWIG_croak("Expected a double reference.");
%typemap(in) float *REFERENCE (float dvalue), float &REFERENCE(float dvalue)
SWIG_croak("expected a reference");
if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
SWIG_croak("expected a double reference");
dvalue = (float) SvNV(tempsv);
%typemap(in) int *REFERENCE (int dvalue), int &REFERENCE (int dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
%typemap(in) short *REFERENCE (short dvalue), short &REFERENCE(short dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
dvalue = (short) SvIV(tempsv);
%typemap(in) long *REFERENCE (long dvalue), long &REFERENCE(long dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
dvalue = (long) SvIV(tempsv);
%typemap(in) unsigned int *REFERENCE (unsigned int dvalue), unsigned int &REFERENCE(unsigned int dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
dvalue = (unsigned int) SvUV(tempsv);
%typemap(in) unsigned short *REFERENCE (unsigned short dvalue), unsigned short &REFERENCE(unsigned short dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
dvalue = (unsigned short) SvUV(tempsv);
%typemap(in) unsigned long *REFERENCE (unsigned long dvalue), unsigned long &REFERENCE(unsigned long dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
dvalue = (unsigned long) SvUV(tempsv);
%typemap(in) unsigned char *REFERENCE (unsigned char dvalue), unsigned char &REFERENCE(unsigned char dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
dvalue = (unsigned char) SvUV(tempsv);
%typemap(in) signed char *REFERENCE (signed char dvalue), signed char &REFERENCE(signed char dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
dvalue = (signed char) SvIV(tempsv);
%typemap(in) bool *REFERENCE (bool dvalue), bool &REFERENCE(bool dvalue)
SWIG_croak("expected a reference");
SWIG_croak("expected a integer reference");
dvalue = (bool) SvIV(tempsv);
%typemap(argout) double *REFERENCE, double &REFERENCE,
float *REFERENCE, float &REFERENCE
if (!$1) SWIG_croak("expected a reference");
sv_setnv(tempsv, (double) *$1);
%typemap(argout) int *REFERENCE, int &REFERENCE,
short *REFERENCE, short &REFERENCE,
long *REFERENCE, long &REFERENCE,
signed char *REFERENCE, unsigned char &REFERENCE,
bool *REFERENCE, bool &REFERENCE
if (!$1) SWIG_croak("expected a reference");
sv_setiv(tempsv, (IV) *$1);
%typemap(argout) unsigned int *REFERENCE, unsigned int &REFERENCE,
unsigned short *REFERENCE, unsigned short &REFERENCE,
unsigned long *REFERENCE, unsigned long &REFERENCE,
unsigned char *REFERENCE, unsigned char &REFERENCE
if (!$1) SWIG_croak("expected a reference");
sv_setuv(tempsv, (UV) *$1);
/* Overloading information */
%typemap(typecheck) double *INOUT = double;
%typemap(typecheck) bool *INOUT = bool;
%typemap(typecheck) signed char *INOUT = signed char;
%typemap(typecheck) unsigned char *INOUT = unsigned char;
%typemap(typecheck) unsigned long *INOUT = unsigned long;
%typemap(typecheck) unsigned short *INOUT = unsigned short;
%typemap(typecheck) unsigned int *INOUT = unsigned int;
%typemap(typecheck) long *INOUT = long;
%typemap(typecheck) short *INOUT = short;
%typemap(typecheck) int *INOUT = int;
%typemap(typecheck) float *INOUT = float;
%typemap(typecheck) long long *INOUT = long long;
%typemap(typecheck) unsigned long long *INOUT = unsigned long long;
%typemap(typecheck) double &INOUT = double;
%typemap(typecheck) bool &INOUT = bool;
%typemap(typecheck) signed char &INOUT = signed char;
%typemap(typecheck) unsigned char &INOUT = unsigned char;
%typemap(typecheck) unsigned long &INOUT = unsigned long;
%typemap(typecheck) unsigned short &INOUT = unsigned short;
%typemap(typecheck) unsigned int &INOUT = unsigned int;
%typemap(typecheck) long &INOUT = long;
%typemap(typecheck) short &INOUT = short;
%typemap(typecheck) int &INOUT = int;
%typemap(typecheck) float &INOUT = float;
%typemap(typecheck) long long &INOUT = long long;
%typemap(typecheck) unsigned long long &INOUT = unsigned long long;