Consistent formatting of example code in the docs
This commit is contained in:
parent
129ef8ea8f
commit
268b942865
25 changed files with 1705 additions and 1714 deletions
|
|
@ -1262,12 +1262,12 @@ consider writing some helper functions instead. For example:
|
|||
%inline %{
|
||||
/* C-style cast */
|
||||
Bar *FooToBar(Foo *f) {
|
||||
return (Bar *) f;
|
||||
return (Bar *) f;
|
||||
}
|
||||
|
||||
/* C++-style cast */
|
||||
Foo *BarToFoo(Bar *b) {
|
||||
return dynamic_cast<Foo*>(b);
|
||||
return dynamic_cast<Foo*>(b);
|
||||
}
|
||||
|
||||
Foo *IncrFoo(Foo *f, int i) {
|
||||
|
|
@ -1355,12 +1355,12 @@ can also be forced to be read-only using the <tt>%immutable</tt> directive. For
|
|||
<div class="code">
|
||||
<pre>
|
||||
struct Foo {
|
||||
...
|
||||
%immutable;
|
||||
int x; /* Read-only members */
|
||||
char *name;
|
||||
%mutable;
|
||||
...
|
||||
...
|
||||
%immutable;
|
||||
int x; /* Read-only members */
|
||||
char *name;
|
||||
%mutable;
|
||||
...
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -1427,11 +1427,11 @@ pointer. For example, suppose you have two structures like this:
|
|||
<div class="code">
|
||||
<pre>
|
||||
struct Foo {
|
||||
int a;
|
||||
int a;
|
||||
};
|
||||
|
||||
struct Bar {
|
||||
Foo f;
|
||||
Foo f;
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -1525,9 +1525,8 @@ suppose you have a class like this:
|
|||
<pre>
|
||||
class Spam {
|
||||
public:
|
||||
static void foo();
|
||||
static int bar;
|
||||
|
||||
static void foo();
|
||||
static int bar;
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -1904,10 +1903,10 @@ submodules or packages. For example, if you have a file like this,
|
|||
%module example
|
||||
|
||||
namespace foo {
|
||||
int fact(int n);
|
||||
struct Vector {
|
||||
double x,y,z;
|
||||
};
|
||||
int fact(int n);
|
||||
struct Vector {
|
||||
double x,y,z;
|
||||
};
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -1976,13 +1975,13 @@ For example:
|
|||
|
||||
template<class T1, class T2>
|
||||
struct pair {
|
||||
typedef T1 first_type;
|
||||
typedef T2 second_type;
|
||||
T1 first;
|
||||
T2 second;
|
||||
pair();
|
||||
pair(const T1&, const T2&);
|
||||
~pair();
|
||||
typedef T1 first_type;
|
||||
typedef T2 second_type;
|
||||
T1 first;
|
||||
T2 second;
|
||||
pair();
|
||||
pair(const T1&, const T2&);
|
||||
~pair();
|
||||
};
|
||||
|
||||
%template(pairii) pair<int,int>;
|
||||
|
|
@ -2037,9 +2036,9 @@ that implements <tt>operator->()</tt> like this:
|
|||
<div class="code">
|
||||
<pre>
|
||||
template<class T> class SmartPtr {
|
||||
...
|
||||
T *operator->();
|
||||
...
|
||||
...
|
||||
T *operator->();
|
||||
...
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -2052,8 +2051,8 @@ Then, if you have a class like this,
|
|||
<pre>
|
||||
class Foo {
|
||||
public:
|
||||
int x;
|
||||
int bar();
|
||||
int x;
|
||||
int bar();
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -2154,9 +2153,9 @@ have a class like this
|
|||
<pre>
|
||||
class Foo {
|
||||
public:
|
||||
int x;
|
||||
int spam(int);
|
||||
...
|
||||
int x;
|
||||
int spam(int);
|
||||
...
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
|
|
@ -2749,10 +2748,10 @@ change the ownership of an object. For instance, consider code like this:
|
|||
<div class="code">
|
||||
<pre>
|
||||
class Node {
|
||||
Object *value;
|
||||
Object *value;
|
||||
public:
|
||||
void set_value(Object *v) { value = v; }
|
||||
...
|
||||
void set_value(Object *v) { value = v; }
|
||||
...
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -3319,16 +3318,16 @@ functions. Just use the <tt>%inline</tt> directive. For example:
|
|||
%inline %{
|
||||
/* Note: double[4][4] is equivalent to a pointer to an array double (*)[4] */
|
||||
double (*new_mat44())[4] {
|
||||
return (double (*)[4]) malloc(16*sizeof(double));
|
||||
return (double (*)[4]) malloc(16*sizeof(double));
|
||||
}
|
||||
void free_mat44(double (*x)[4]) {
|
||||
free(x);
|
||||
free(x);
|
||||
}
|
||||
void mat44_set(double x[4][4], int i, int j, double v) {
|
||||
x[i][j] = v;
|
||||
x[i][j] = v;
|
||||
}
|
||||
double mat44_get(double x[4][4], int i, int j) {
|
||||
return x[i][j];
|
||||
return x[i][j];
|
||||
}
|
||||
%}
|
||||
</pre>
|
||||
|
|
@ -3374,12 +3373,12 @@ void set_transform(Image *im, double x[4][4]);
|
|||
/* Rewrite the high level interface to set_transform */
|
||||
%pythoncode %{
|
||||
def set_transform(im,x):
|
||||
a = new_mat44()
|
||||
for i in range(4):
|
||||
for j in range(4):
|
||||
mat44_set(a,i,j,x[i][j])
|
||||
_example.set_transform(im,a)
|
||||
free_mat44(a)
|
||||
a = new_mat44()
|
||||
for i in range(4):
|
||||
for j in range(4):
|
||||
mat44_set(a,i,j,x[i][j])
|
||||
_example.set_transform(im,a)
|
||||
free_mat44(a)
|
||||
%}
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -3529,11 +3528,11 @@ proxy, just before the return statement.
|
|||
// Add python code to bar()
|
||||
|
||||
%feature("pythonprepend") Foo::bar(int) %{
|
||||
#do something before C++ call
|
||||
#do something before C++ call
|
||||
%}
|
||||
|
||||
%feature("pythonappend") Foo::bar(int) %{
|
||||
#do something after C++ call
|
||||
#do something after C++ call
|
||||
%}
|
||||
|
||||
|
||||
|
|
@ -3558,11 +3557,11 @@ SWIG version 1.3.28 you can use the directive forms
|
|||
// Add python code to bar()
|
||||
|
||||
%pythonprepend Foo::bar(int) %{
|
||||
#do something before C++ call
|
||||
#do something before C++ call
|
||||
%}
|
||||
|
||||
%pythonappend Foo::bar(int) %{
|
||||
#do something after C++ call
|
||||
#do something after C++ call
|
||||
%}
|
||||
|
||||
|
||||
|
|
@ -3587,11 +3586,11 @@ as it will then get attached to all the overloaded C++ methods. For example:
|
|||
// Add python code to bar()
|
||||
|
||||
%pythonprepend Foo::bar %{
|
||||
#do something before C++ call
|
||||
#do something before C++ call
|
||||
%}
|
||||
|
||||
%pythonappend Foo::bar %{
|
||||
#do something after C++ call
|
||||
#do something after C++ call
|
||||
%}
|
||||
|
||||
|
||||
|
|
@ -3625,22 +3624,22 @@ Here is a simple example:
|
|||
%}
|
||||
|
||||
struct Vector {
|
||||
double x,y,z;
|
||||
double x,y,z;
|
||||
};
|
||||
|
||||
%extend Vector {
|
||||
char *__str__() {
|
||||
static char tmp[1024];
|
||||
sprintf(tmp,"Vector(%g,%g,%g)", $self->x,$self->y,$self->z);
|
||||
return tmp;
|
||||
}
|
||||
Vector(double x, double y, double z) {
|
||||
Vector *v = (Vector *) malloc(sizeof(Vector));
|
||||
v->x = x;
|
||||
v->y = y;
|
||||
v->z = z;
|
||||
return v;
|
||||
}
|
||||
char *__str__() {
|
||||
static char tmp[1024];
|
||||
sprintf(tmp,"Vector(%g,%g,%g)", $self->x,$self->y,$self->z);
|
||||
return tmp;
|
||||
}
|
||||
Vector(double x, double y, double z) {
|
||||
Vector *v = (Vector *) malloc(sizeof(Vector));
|
||||
v->x = x;
|
||||
v->y = y;
|
||||
v->z = z;
|
||||
return v;
|
||||
}
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -3666,13 +3665,13 @@ For example, if you wanted to overload a Python operator, you might do this:
|
|||
<div class="code">
|
||||
<pre>
|
||||
%extend Vector {
|
||||
Vector __add__(Vector *other) {
|
||||
Vector v;
|
||||
v.x = $self->x + other->x;
|
||||
v.y = $self->y + other->y;
|
||||
v.z = $self->z + other->z;
|
||||
return v;
|
||||
}
|
||||
Vector __add__(Vector *other) {
|
||||
Vector v;
|
||||
v.x = $self->x + other->x;
|
||||
v.y = $self->y + other->y;
|
||||
v.z = $self->z + other->z;
|
||||
return v;
|
||||
}
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -3716,8 +3715,8 @@ or a NULL pointer perhaps). Here is a simple example of how you might handle th
|
|||
%exception malloc {
|
||||
$action
|
||||
if (!result) {
|
||||
PyErr_SetString(PyExc_MemoryError,"Not enough memory");
|
||||
SWIG_fail;
|
||||
PyErr_SetString(PyExc_MemoryError,"Not enough memory");
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
void *malloc(size_t nbytes);
|
||||
|
|
@ -3746,11 +3745,11 @@ that. For example:
|
|||
<div class="code">
|
||||
<pre>
|
||||
%exception {
|
||||
$action
|
||||
if (err_occurred()) {
|
||||
PyErr_SetString(PyExc_RuntimeError, err_message());
|
||||
SWIG_fail;
|
||||
}
|
||||
$action
|
||||
if (err_occurred()) {
|
||||
PyErr_SetString(PyExc_RuntimeError, err_message());
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -3766,18 +3765,18 @@ C++ exceptions are also easy to handle. For example, you can write code like th
|
|||
<div class="code">
|
||||
<pre>
|
||||
%exception getitem {
|
||||
try {
|
||||
$action
|
||||
} catch (std::out_of_range &e) {
|
||||
PyErr_SetString(PyExc_IndexError, const_cast<char*>(e.what()));
|
||||
SWIG_fail;
|
||||
}
|
||||
try {
|
||||
$action
|
||||
} catch (std::out_of_range &e) {
|
||||
PyErr_SetString(PyExc_IndexError, const_cast<char*>(e.what()));
|
||||
SWIG_fail;
|
||||
}
|
||||
}
|
||||
|
||||
class Base {
|
||||
public:
|
||||
Foo *getitem(int index); // Exception handled added
|
||||
...
|
||||
Foo *getitem(int index); // Exception handled added
|
||||
...
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -3852,7 +3851,7 @@ example:
|
|||
<div class="code">
|
||||
<pre>
|
||||
void add(int x, int y, int *result) {
|
||||
*result = x + y;
|
||||
*result = x + y;
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -3864,7 +3863,7 @@ or perhaps
|
|||
<div class="code">
|
||||
<pre>
|
||||
int sub(int *x, int *y) {
|
||||
return *x-*y;
|
||||
return *x-*y;
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -3929,7 +3928,7 @@ If a function mutates one of its parameters like this,
|
|||
<div class="code">
|
||||
<pre>
|
||||
void negate(int *x) {
|
||||
*x = -(*x);
|
||||
*x = -(*x);
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -4465,8 +4464,8 @@ Typemaps can also be defined for groups of consecutive arguments. For example:
|
|||
<div class="code">
|
||||
<pre>
|
||||
%typemap(in) (char *str, int len) {
|
||||
$1 = PyString_AsString($input);
|
||||
$2 = PyString_Size($input);
|
||||
$1 = PyString_AsString($input);
|
||||
$2 = PyString_Size($input);
|
||||
};
|
||||
|
||||
int count(char c, char *str, int len);
|
||||
|
|
@ -4781,12 +4780,12 @@ object to be used as a <tt>char **</tt> object.
|
|||
// Now a test function
|
||||
%inline %{
|
||||
int print_args(char **argv) {
|
||||
int i = 0;
|
||||
while (argv[i]) {
|
||||
printf("argv[%d] = %s\n", i,argv[i]);
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
int i = 0;
|
||||
while (argv[i]) {
|
||||
printf("argv[%d] = %s\n", i,argv[i]);
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
%}
|
||||
|
||||
|
|
@ -5003,7 +5002,7 @@ no meaningful input value), an additional typemap can be written. For example:
|
|||
|
||||
<div class="code"><pre>
|
||||
%typemap(in,numinputs=0) double *OutValue(double temp) {
|
||||
$1 = &temp;
|
||||
$1 = &temp;
|
||||
}
|
||||
|
||||
</pre></div>
|
||||
|
|
@ -5085,22 +5084,22 @@ arrays of different sizes. To do this, you might write a typemap as follows:
|
|||
%typemap(in) double[ANY](double temp[$1_dim0]) {
|
||||
int i;
|
||||
if (!PySequence_Check($input)) {
|
||||
PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
|
||||
SWIG_fail;
|
||||
PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
|
||||
SWIG_fail;
|
||||
}
|
||||
if (PyObject_Length($input) != $1_dim0) {
|
||||
PyErr_SetString(PyExc_ValueError,"Expecting a sequence with $1_dim0 elements");
|
||||
SWIG_fail;
|
||||
PyErr_SetString(PyExc_ValueError,"Expecting a sequence with $1_dim0 elements");
|
||||
SWIG_fail;
|
||||
}
|
||||
for (i =0; i < $1_dim0; i++) {
|
||||
PyObject *o = PySequence_GetItem($input,i);
|
||||
if (!PyFloat_Check(o)) {
|
||||
Py_XDECREF(o);
|
||||
PyErr_SetString(PyExc_ValueError,"Expecting a sequence of floats");
|
||||
SWIG_fail;
|
||||
}
|
||||
temp[i] = PyFloat_AsDouble(o);
|
||||
Py_DECREF(o);
|
||||
PyObject *o = PySequence_GetItem($input,i);
|
||||
if (!PyFloat_Check(o)) {
|
||||
Py_XDECREF(o);
|
||||
PyErr_SetString(PyExc_ValueError,"Expecting a sequence of floats");
|
||||
SWIG_fail;
|
||||
}
|
||||
temp[i] = PyFloat_AsDouble(o);
|
||||
Py_DECREF(o);
|
||||
}
|
||||
$1 = &temp[0];
|
||||
}
|
||||
|
|
@ -5131,32 +5130,32 @@ to use a helper function instead. This will greatly reduce the amount of wrappe
|
|||
static int convert_darray(PyObject *input, double *ptr, int size) {
|
||||
int i;
|
||||
if (!PySequence_Check(input)) {
|
||||
PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
|
||||
return 0;
|
||||
PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
|
||||
return 0;
|
||||
}
|
||||
if (PyObject_Length(input) != size) {
|
||||
PyErr_SetString(PyExc_ValueError,"Sequence size mismatch");
|
||||
return 0;
|
||||
PyErr_SetString(PyExc_ValueError,"Sequence size mismatch");
|
||||
return 0;
|
||||
}
|
||||
for (i =0; i < size; i++) {
|
||||
PyObject *o = PySequence_GetItem(input,i);
|
||||
if (!PyFloat_Check(o)) {
|
||||
Py_XDECREF(o);
|
||||
PyErr_SetString(PyExc_ValueError,"Expecting a sequence of floats");
|
||||
return 0;
|
||||
}
|
||||
ptr[i] = PyFloat_AsDouble(o);
|
||||
Py_DECREF(o);
|
||||
PyObject *o = PySequence_GetItem(input,i);
|
||||
if (!PyFloat_Check(o)) {
|
||||
Py_XDECREF(o);
|
||||
PyErr_SetString(PyExc_ValueError,"Expecting a sequence of floats");
|
||||
return 0;
|
||||
}
|
||||
ptr[i] = PyFloat_AsDouble(o);
|
||||
Py_DECREF(o);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
%}
|
||||
|
||||
%typemap(in) double [ANY](double temp[$1_dim0]) {
|
||||
if (!convert_darray($input,temp,$1_dim0)) {
|
||||
SWIG_fail;
|
||||
}
|
||||
$1 = &temp[0];
|
||||
if (!convert_darray($input,temp,$1_dim0)) {
|
||||
SWIG_fail;
|
||||
}
|
||||
$1 = &temp[0];
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
|
@ -5450,7 +5449,7 @@ resulting in
|
|||
<div class="targetlang">
|
||||
<pre>
|
||||
def function_name(*args, **kwargs):
|
||||
"""
|
||||
"""
|
||||
function_name(x, y, foo=None, bar=None) -> bool
|
||||
|
||||
Parameters
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue