1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
//
// SWIG Typemap library
// Dave Beazley
// May 5, 1997
//
// Python implementation
//
// 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.
//
#ifdef AUTODOC
%section "Typemap Library (Python)",info,after,pre,nosort,skip=1,chop_left=3,chop_right=0,chop_top=0,chop_bottom=0
%text %{
%include typemaps.i
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
pointers.
%}
#endif
// ------------------------------------------------------------------------
// Pointer handling
//
// These mappings provide support for input/output arguments and common
// uses for C/C++ pointers.
// ------------------------------------------------------------------------
// INPUT typemaps.
// These remap a C pointer to be an "INPUT" value which is passed by value
// instead of reference.
#ifdef AUTODOC
%subsection "Input Methods"
%text %{
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.
int *INPUT
short *INPUT
long *INPUT
unsigned int *INPUT
unsigned short *INPUT
unsigned long *INPUT
unsigned char *INPUT
float *INPUT
double *INPUT
To use these, suppose you had a C function like this :
double fadd(double *a, double *b) {
return *a+*b;
}
You could wrap it with SWIG as follows :
%include typemaps.i
double fadd(double *INPUT, double *INPUT);
or you can use the %apply directive :
%include typemaps.i
%apply double *INPUT { double *a, double *b };
double fadd(double *a, double *b);
%}
#endif
%typemap(python,in) double *INPUT(double temp)
{
temp = PyFloat_AsDouble($source);
$target = &temp;
}
%typemap(python,in) float *INPUT(float temp)
{
temp = (float) PyFloat_AsDouble($source);
$target = &temp;
}
%typemap(python,in) int *INPUT(int temp)
{
temp = (int) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) short *INPUT(short temp)
{
temp = (short) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) long *INPUT(long temp)
{
temp = (long) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned int *INPUT(unsigned int temp)
{
temp = (unsigned int) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned short *INPUT(unsigned short temp)
{
temp = (unsigned short) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned long *INPUT(unsigned long temp)
{
temp = (unsigned long) PyInt_AsLong($source);
$target = &temp;
}
%typemap(python,in) unsigned char *INPUT(unsigned char temp)
{
temp = (unsigned char) PyInt_AsLong($source);
$target = &temp;
}
// OUTPUT typemaps. These typemaps are used for parameters that
// are output only. The output value is appended to the result as
// a list element.
#ifdef AUTODOC
%subsection "Output Methods"
%text %{
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, they are returned in the form of a Python list.
int *OUTPUT
short *OUTPUT
long *OUTPUT
unsigned int *OUTPUT
unsigned short *OUTPUT
unsigned long *OUTPUT
unsigned char *OUTPUT
float *OUTPUT
double *OUTPUT
A Python Tuple can also be replaced by using T_OUTPUT instead of 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).K:
double modf(double x, double *ip);
You could wrap it with SWIG as follows :
%include typemaps.i
double modf(double x, double *OUTPUT);
or you can use the %apply directive :
%include typemaps.i
%apply double *OUTPUT { double *ip };
double modf(double x, double *ip);
The Python output of the function would be a list containing both
output values.
%}
#endif
// Force the argument to be ignored.
%typemap(python,ignore) int *OUTPUT(int temp),
short *OUTPUT(short temp),
long *OUTPUT(long temp),
unsigned int *OUTPUT(unsigned int temp),
unsigned short *OUTPUT(unsigned short temp),
unsigned long *OUTPUT(unsigned long temp),
unsigned char *OUTPUT(unsigned char temp),
float *OUTPUT(float temp),
double *OUTPUT(double temp)
{
$target = &temp;
}
%typemap(python,argout) int *OUTPUT,
short *OUTPUT,
long *OUTPUT,
unsigned int *OUTPUT,
unsigned short *OUTPUT,
unsigned long *OUTPUT,
unsigned char *OUTPUT
{
PyObject *o;
o = PyInt_FromLong((long) (*$source));
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
%typemap(python,argout) float *OUTPUT,
double *OUTPUT
{
PyObject *o;
o = PyFloat_FromDouble((double) (*$source));
if (!$target) {
$target = o;
} else if ($target == Py_None) {
Py_DECREF(Py_None);
$target = o;
} else {
if (!PyList_Check($target)) {
PyObject *o2 = $target;
$target = PyList_New(0);
PyList_Append($target,o2);
Py_XDECREF(o2);
}
PyList_Append($target,o);
Py_XDECREF(o);
}
}
// These typemaps contributed by Robin Dunn
//----------------------------------------------------------------------
//
// T_OUTPUT typemap (and helper function) to return multiple argouts as
// a tuple instead of a list.
//
// Author: Robin Dunn
//----------------------------------------------------------------------
%{
static PyObject* t_output_helper(PyObject* target, PyObject* o) {
PyObject* o2;
PyObject* o3;
if (!target) {
target = o;
} else if (target == Py_None) {
Py_DECREF(Py_None);
target = o;
} else {
if (!PyTuple_Check(target)) {
o2 = target;
target = PyTuple_New(1);
PyTuple_SetItem(target, 0, o2);
}
o3 = PyTuple_New(1);
PyTuple_SetItem(o3, 0, o);
o2 = target;
target = PySequence_Concat(o2, o3);
Py_DECREF(o2);
Py_DECREF(o3);
}
return target;
}
%}
// Force the argument to be ignored.
%typemap(python,ignore) int *T_OUTPUT(int temp),
short *T_OUTPUT(short temp),
long *T_OUTPUT(long temp),
unsigned int *T_OUTPUT(unsigned int temp),
unsigned short *T_OUTPUT(unsigned short temp),
unsigned long *T_OUTPUT(unsigned long temp),
unsigned char *T_OUTPUT(unsigned char temp),
float *T_OUTPUT(float temp),
double *T_OUTPUT(double temp)
{
$target = &temp;
}
%typemap(python,argout) int *T_OUTPUT,
short *T_OUTPUT,
long *T_OUTPUT,
unsigned int *T_OUTPUT,
unsigned short *T_OUTPUT,
unsigned long *T_OUTPUT,
unsigned char *T_OUTPUT
{
PyObject *o;
o = PyInt_FromLong((long) (*$source));
$target = t_output_helper($target, o);
}
%typemap(python,argout) float *T_OUTPUT,
double *T_OUTPUT
{
PyObject *o;
o = PyFloat_FromDouble((double) (*$source));
$target = t_output_helper($target, o);
}
// BOTH
// Mappings for an argument that is both an input and output
// parameter
#ifdef AUTODOC
%subsection "Input/Output Methods"
%text %{
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 Python list. To return a Python tuple,
using T_BOTH instead.
int *BOTH
short *BOTH
long *BOTH
unsigned int *BOTH
unsigned short *BOTH
unsigned long *BOTH
unsigned char *BOTH
float *BOTH
double *BOTH
For example, suppose you were trying to wrap the following function :
void neg(double *x) {
*x = -(*x);
}
You could wrap it with SWIG as follows :
%include typemaps.i
void neg(double *BOTH);
or you can use the %apply directive :
%include typemaps.i
%apply double *BOTH { double *x };
void neg(double *x);
Unlike C, this mapping does not directly modify the input value (since
this makes no sense in Python). Rather, the modified input value shows
up as the return value of the function. Thus, to apply this function
to a Python variable you might do this :
x = neg(x)
%}
#endif
%typemap(python,in) int *BOTH = int *INPUT;
%typemap(python,in) short *BOTH = short *INPUT;
%typemap(python,in) long *BOTH = long *INPUT;
%typemap(python,in) unsigned *BOTH = unsigned *INPUT;
%typemap(python,in) unsigned short *BOTH = unsigned short *INPUT;
%typemap(python,in) unsigned long *BOTH = unsigned long *INPUT;
%typemap(python,in) unsigned char *BOTH = unsigned char *INPUT;
%typemap(python,in) float *BOTH = float *INPUT;
%typemap(python,in) double *BOTH = double *INPUT;
%typemap(python,argout) int *BOTH = int *OUTPUT;
%typemap(python,argout) short *BOTH = short *OUTPUT;
%typemap(python,argout) long *BOTH = long *OUTPUT;
%typemap(python,argout) unsigned *BOTH = unsigned *OUTPUT;
%typemap(python,argout) unsigned short *BOTH = unsigned short *OUTPUT;
%typemap(python,argout) unsigned long *BOTH = unsigned long *OUTPUT;
%typemap(python,argout) unsigned char *BOTH = unsigned char *OUTPUT;
%typemap(python,argout) float *BOTH = float *OUTPUT;
%typemap(python,argout) double *BOTH = double *OUTPUT;
%typemap(python,in) int *T_BOTH = int *INPUT;
%typemap(python,in) short *T_BOTH = short *INPUT;
%typemap(python,in) long *T_BOTH = long *INPUT;
%typemap(python,in) unsigned *T_BOTH = unsigned *INPUT;
%typemap(python,in) unsigned short *T_BOTH = unsigned short *INPUT;
%typemap(python,in) unsigned long *T_BOTH = unsigned long *INPUT;
%typemap(python,in) unsigned char *T_BOTH = unsigned char *INPUT;
%typemap(python,in) float *T_BOTH = float *INPUT;
%typemap(python,in) double *T_BOTH = double *INPUT;
%typemap(python,argout) int *T_BOTH = int *T_OUTPUT;
%typemap(python,argout) short *T_BOTH = short *T_OUTPUT;
%typemap(python,argout) long *T_BOTH = long *T_OUTPUT;
%typemap(python,argout) unsigned *T_BOTH = unsigned *T_OUTPUT;
%typemap(python,argout) unsigned short *T_BOTH = unsigned short *T_OUTPUT;
%typemap(python,argout) unsigned long *T_BOTH = unsigned long *T_OUTPUT;
%typemap(python,argout) unsigned char *T_BOTH = unsigned char *T_OUTPUT;
%typemap(python,argout) float *T_BOTH = float *T_OUTPUT;
%typemap(python,argout) double *T_BOTH = double *T_OUTPUT;
// --------------------------------------------------------------------
// Special types
//
// --------------------------------------------------------------------
#ifdef AUTODOC
%subsection "Special Methods"
%text %{
The typemaps.i library also provides the following mappings :
PyObject *
When a PyObject * appears as either an input value or return
value of a function, SWIG passes it through unmodified. Thus,
if you want to write a C function that operates on PyObjects,
it is easy to write. For example :
%include typemaps.i
PyObject *spam(PyObject *obj1, int n);
Unlike normal Python wrapper functions, These functions can
use any combination of parameters that you wish.
%}
#endif
// If a PyObject * appears as either an argument or a function return
// value, simply pass it straight through.
%typemap(python,in) PyObject * {
$target = $source;
}
%typemap(python,out) PyObject * {
$target = $source;
}
|