More consistent formatting of examples in documentation
This commit is contained in:
parent
8753f9652c
commit
7ee76f93f9
9 changed files with 96 additions and 99 deletions
|
|
@ -5278,7 +5278,7 @@ void * operator new(size_t t) {
|
||||||
throw bad_alloc();
|
throw bad_alloc();
|
||||||
pJalloc->ref = 0;
|
pJalloc->ref = 0;
|
||||||
return static_cast<void *>(
|
return static_cast<void *>(
|
||||||
static_cast<char *>(static_cast<void *>(pJalloc)) + sizeof(Jalloc));
|
static_cast<char *>(static_cast<void *>(pJalloc)) + sizeof(Jalloc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -7240,7 +7240,7 @@ public class runme {
|
||||||
example.print_args(animals);
|
example.print_args(animals);
|
||||||
String args[] = example.get_args();
|
String args[] = example.get_args();
|
||||||
for (int i=0; i<args.length; i++)
|
for (int i=0; i<args.length; i++)
|
||||||
System.out.println(i + ":" + args[i]);
|
System.out.println(i + ":" + args[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
|
||||||
|
|
@ -1008,11 +1008,10 @@ The following operators cannot be overloaded (mainly because they are not suppor
|
||||||
<p>
|
<p>
|
||||||
SWIG also accepts the <tt>__str__()</tt> member function which converts an object to a string. This function should return a const char*, preferably to static memory. This will be used for the <tt>print()</tt> and <tt>tostring()</tt> functions in Lua. Assuming the complex class has a function
|
SWIG also accepts the <tt>__str__()</tt> member function which converts an object to a string. This function should return a const char*, preferably to static memory. This will be used for the <tt>print()</tt> and <tt>tostring()</tt> functions in Lua. Assuming the complex class has a function
|
||||||
</p>
|
</p>
|
||||||
<div class="code"><pre>const char* __str__()
|
<div class="code"><pre>const char* __str__() {
|
||||||
{
|
static char buffer[255];
|
||||||
static char buffer[255];
|
sprintf(buffer, "Complex(%g, %g)", this->re(), this->im());
|
||||||
sprintf(buffer, "Complex(%g, %g)", this->re(), this->im());
|
return buffer;
|
||||||
return buffer;
|
|
||||||
}
|
}
|
||||||
</pre></div>
|
</pre></div>
|
||||||
<p>
|
<p>
|
||||||
|
|
@ -1031,11 +1030,10 @@ Complex(10, 12)
|
||||||
<p>
|
<p>
|
||||||
It is also possible to overload the operator<tt>[]</tt>, but currently this cannot be automatically performed. To overload the operator<tt>[]</tt> you need to provide two functions, <tt>__getitem__()</tt> and <tt>__setitem__()</tt>
|
It is also possible to overload the operator<tt>[]</tt>, but currently this cannot be automatically performed. To overload the operator<tt>[]</tt> you need to provide two functions, <tt>__getitem__()</tt> and <tt>__setitem__()</tt>
|
||||||
</p>
|
</p>
|
||||||
<div class="code"><pre>class Complex
|
<div class="code"><pre>class Complex {
|
||||||
{
|
//....
|
||||||
//....
|
double __getitem__(int i)const; // i is the index, returns the data
|
||||||
double __getitem__(int i)const; // i is the index, returns the data
|
void __setitem__(int i, double d); // i is the index, d is the data
|
||||||
void __setitem__(int i, double d); // i is the index, d is the data
|
|
||||||
};
|
};
|
||||||
</pre></div>
|
</pre></div>
|
||||||
<p>
|
<p>
|
||||||
|
|
|
||||||
|
|
@ -3279,9 +3279,9 @@ suffice in most cases:
|
||||||
<div class="code">
|
<div class="code">
|
||||||
<pre>
|
<pre>
|
||||||
%feature("director:except") {
|
%feature("director:except") {
|
||||||
if ($error != NULL) {
|
if ($error != NULL) {
|
||||||
throw Swig::DirectorMethodException();
|
throw Swig::DirectorMethodException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1175,9 +1175,9 @@ should suffice in most cases:
|
||||||
<div class="code">
|
<div class="code">
|
||||||
<pre>
|
<pre>
|
||||||
%feature("director:except") {
|
%feature("director:except") {
|
||||||
if ($error == FAILURE) {
|
if ($error == FAILURE) {
|
||||||
throw Swig::DirectorMethodException();
|
throw Swig::DirectorMethodException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -226,16 +226,15 @@ resulting C file should be built as a python extension, inserting the module
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
|
|
||||||
int fact(int n) {
|
int fact(int n) {
|
||||||
if (n < 0){ /* This should probably return an error, but this is simpler */
|
if (n < 0) { /* This should probably return an error, but this is simpler */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
} else {
|
||||||
else {
|
/* testing for overflow would be a good idea here */
|
||||||
/* testing for overflow would be a good idea here */
|
return n * fact(n-1);
|
||||||
return n * fact(n-1);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
</pre>
|
</pre>
|
||||||
|
|
@ -3133,9 +3132,9 @@ suffice in most cases:
|
||||||
<div class="code">
|
<div class="code">
|
||||||
<pre>
|
<pre>
|
||||||
%feature("director:except") {
|
%feature("director:except") {
|
||||||
if ($error != NULL) {
|
if ($error != NULL) {
|
||||||
throw Swig::DirectorMethodException();
|
throw Swig::DirectorMethodException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -4142,11 +4141,11 @@ Sometimes a C function expects an array to be passed as a pointer. For example,
|
||||||
<div class="code">
|
<div class="code">
|
||||||
<pre>
|
<pre>
|
||||||
int sumitems(int *first, int nitems) {
|
int sumitems(int *first, int nitems) {
|
||||||
int i, sum = 0;
|
int i, sum = 0;
|
||||||
for (i = 0; i < nitems; i++) {
|
for (i = 0; i < nitems; i++) {
|
||||||
sum += first[i];
|
sum += first[i];
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -6526,7 +6525,7 @@ string that cannot be completely decoded as UTF-8:
|
||||||
%inline %{
|
%inline %{
|
||||||
|
|
||||||
const char* non_utf8_c_str(void) {
|
const char* non_utf8_c_str(void) {
|
||||||
return "h\xe9llo w\xc3\xb6rld";
|
return "h\xe9llo w\xc3\xb6rld";
|
||||||
}
|
}
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
|
||||||
|
|
@ -1266,12 +1266,12 @@ pointers. As a result, SWIG creates a wrapper like this:
|
||||||
|
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
Vector *wrap_cross_product(Vector *v1, Vector *v2) {
|
Vector *wrap_cross_product(Vector *v1, Vector *v2) {
|
||||||
Vector x = *v1;
|
Vector x = *v1;
|
||||||
Vector y = *v2;
|
Vector y = *v2;
|
||||||
Vector *result;
|
Vector *result;
|
||||||
result = (Vector *) malloc(sizeof(Vector));
|
result = (Vector *) malloc(sizeof(Vector));
|
||||||
*(result) = cross(x, y);
|
*(result) = cross(x, y);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
|
|
@ -1280,10 +1280,10 @@ or if SWIG was run with the <tt>-c++</tt> option:</p>
|
||||||
|
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
Vector *wrap_cross(Vector *v1, Vector *v2) {
|
Vector *wrap_cross(Vector *v1, Vector *v2) {
|
||||||
Vector x = *v1;
|
Vector x = *v1;
|
||||||
Vector y = *v2;
|
Vector y = *v2;
|
||||||
Vector *result = new Vector(cross(x, y)); // Uses default copy constructor
|
Vector *result = new Vector(cross(x, y)); // Uses default copy constructor
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2958,29 +2958,29 @@ To illustrate, consider the following template definition:
|
||||||
<div class="code"><pre>
|
<div class="code"><pre>
|
||||||
template<class T> class List {
|
template<class T> class List {
|
||||||
private:
|
private:
|
||||||
T *data;
|
T *data;
|
||||||
int nitems;
|
int nitems;
|
||||||
int maxitems;
|
int maxitems;
|
||||||
public:
|
public:
|
||||||
List(int max) {
|
List(int max) {
|
||||||
data = new T [max];
|
data = new T [max];
|
||||||
nitems = 0;
|
nitems = 0;
|
||||||
maxitems = max;
|
maxitems = max;
|
||||||
}
|
}
|
||||||
~List() {
|
~List() {
|
||||||
delete [] data;
|
delete [] data;
|
||||||
};
|
};
|
||||||
void append(T obj) {
|
void append(T obj) {
|
||||||
if (nitems < maxitems) {
|
if (nitems < maxitems) {
|
||||||
data[nitems++] = obj;
|
data[nitems++] = obj;
|
||||||
}
|
|
||||||
}
|
|
||||||
int length() {
|
|
||||||
return nitems;
|
|
||||||
}
|
|
||||||
T get(int n) {
|
|
||||||
return data[n];
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
int length() {
|
||||||
|
return nitems;
|
||||||
|
}
|
||||||
|
T get(int n) {
|
||||||
|
return data[n];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
|
|
@ -3704,10 +3704,10 @@ template <class T> class OuterTemplateClass {};
|
||||||
// Don't forget to use %feature("flatnested") for OuterClass::InnerStruct and
|
// Don't forget to use %feature("flatnested") for OuterClass::InnerStruct and
|
||||||
// OuterClass::InnerClass if the target language doesn't support nested classes.
|
// OuterClass::InnerClass if the target language doesn't support nested classes.
|
||||||
class OuterClass {
|
class OuterClass {
|
||||||
public:
|
public:
|
||||||
// Forward declarations:
|
// Forward declarations:
|
||||||
struct InnerStruct;
|
struct InnerStruct;
|
||||||
class InnerClass;
|
class InnerClass;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct OuterClass::InnerStruct {};
|
struct OuterClass::InnerStruct {};
|
||||||
|
|
@ -4533,13 +4533,13 @@ around some other class. For example:
|
||||||
<pre>
|
<pre>
|
||||||
// Smart-pointer class
|
// Smart-pointer class
|
||||||
template<class T> class SmartPtr {
|
template<class T> class SmartPtr {
|
||||||
T *pointee;
|
T *pointee;
|
||||||
public:
|
public:
|
||||||
SmartPtr(T *p) : pointee(p) { ... }
|
SmartPtr(T *p) : pointee(p) { ... }
|
||||||
T *operator->() {
|
T *operator->() {
|
||||||
return pointee;
|
return pointee;
|
||||||
}
|
}
|
||||||
...
|
...
|
||||||
};
|
};
|
||||||
|
|
||||||
// Ordinary class
|
// Ordinary class
|
||||||
|
|
|
||||||
|
|
@ -121,15 +121,15 @@ In this example we bind from C a function and a global variable into Scilab. The
|
||||||
double Foo = 3.0;
|
double Foo = 3.0;
|
||||||
|
|
||||||
int fact(int n) {
|
int fact(int n) {
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if (n == 0) {
|
else if (n == 0) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return n * fact(n-1);
|
return n * fact(n-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
@ -896,8 +896,8 @@ Let's see it on an example of a struct with two members:
|
||||||
%inline %{
|
%inline %{
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int x;
|
int x;
|
||||||
int arr[4];
|
int arr[4];
|
||||||
} Foo;
|
} Foo;
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
@ -1143,11 +1143,11 @@ As explained in <a href="SWIGPlus.html#SWIGPlus_overloaded_methods">6.15</a> SWI
|
||||||
%module example
|
%module example
|
||||||
|
|
||||||
void magnify(Square *square, double factor) {
|
void magnify(Square *square, double factor) {
|
||||||
square->size *= factor;
|
square->size *= factor;
|
||||||
};
|
};
|
||||||
|
|
||||||
void magnify(Circle *circle, double factor) {
|
void magnify(Circle *circle, double factor) {
|
||||||
square->radius *= factor;
|
square->radius *= factor;
|
||||||
};
|
};
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3215,28 +3215,28 @@ helper functions to access arrays :
|
||||||
|
|
||||||
%inline %{
|
%inline %{
|
||||||
double *new_double(int size) {
|
double *new_double(int size) {
|
||||||
return (double *) malloc(size*sizeof(double));
|
return (double *) malloc(size*sizeof(double));
|
||||||
}
|
}
|
||||||
void delete_double(double *a) {
|
void delete_double(double *a) {
|
||||||
free(a);
|
free(a);
|
||||||
}
|
}
|
||||||
double get_double(double *a, int index) {
|
double get_double(double *a, int index) {
|
||||||
return a[index];
|
return a[index];
|
||||||
}
|
}
|
||||||
void set_double(double *a, int index, double val) {
|
void set_double(double *a, int index, double val) {
|
||||||
a[index] = val;
|
a[index] = val;
|
||||||
}
|
}
|
||||||
int *new_int(int size) {
|
int *new_int(int size) {
|
||||||
return (int *) malloc(size*sizeof(int));
|
return (int *) malloc(size*sizeof(int));
|
||||||
}
|
}
|
||||||
void delete_int(int *a) {
|
void delete_int(int *a) {
|
||||||
free(a);
|
free(a);
|
||||||
}
|
}
|
||||||
int get_int(int *a, int index) {
|
int get_int(int *a, int index) {
|
||||||
return a[index];
|
return a[index];
|
||||||
}
|
}
|
||||||
int set_int(int *a, int index, int val) {
|
int set_int(int *a, int index, int val) {
|
||||||
a[index] = val;
|
a[index] = val;
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue