scilab: add help on primitive type mappings

This commit is contained in:
Simon Marchetto 2014-02-14 14:13:00 +01:00
commit 1eeb729487

View file

@ -27,6 +27,7 @@
<li><a href="#Scilab_wrapping_identifiers">Identifiers</a>
<li><a href="#Scilab_wrapping_modules">Modules</a>
<li><a href="#Scilab_wrapping_functions">Functions</a>
<li><a href="#Scilab_wrapping_type_mappings">Default primitive type mappings</a>
<li><a href="#Scilab_wrapping_global_variables">Global variables</a>
<li><a href="#Scilab_wrapping_constants">Constants</a>
<li><a href="#Scilab_wrapping_enums">Enums</a>
@ -55,11 +56,11 @@
<p>
Scilab is a scientific software package for numerical computations providing a powerful open computing environment for engineering and scientific applications that is mostly compatible with MATLAB. More information can be found at <a href="http://www.scilab.org">www.scilab.org</a>.
Scilab is a scientific software package for numerical computations providing a powerful open computing environment for engineering and scientific applications that is mostly compatible with MATLAB. More information can be found at <a href="http://www.scilab.org">www.scilab.org</a>.
</p>
<p>
This chapter explains how to use SWIG for Scilab. After this introduction, you should be able to generate with SWIG a Scilab external module from a C/C++ library.
This chapter explains how to use SWIG for Scilab. After this introduction, you should be able to generate with SWIG a Scilab external module from a C/C++ library.
</p>
@ -333,11 +334,63 @@ Creates a built-in function <tt>fact(n)</tt> that works exactly like you think i
ans=24
</pre></div>
<H3><a name="Scilab_wrapping_global_variables"></a>37.3.4 Global variables</H3>
<H3><a name="Scilab_wrapping_primitive_type_mappings"></a>37.3.4 Default primitive type mappings</H3>
<p>
The following table give for each C/C++ primitive type the equivalent Scilab type.
</p>
<div class="table">
<table border="1" sumary="Scilab default primitive type mappings">
<tr>
<td><b>C/C++ type</b></td>
<td><b>Scilab type</b></td>
</tr>
<tr><td>bool</td><td>boolean</td></tr>
<tr><td>char</td><td>string</td></tr>
<tr><td>signed char</td><td>double or int8</td></tr>
<tr><td>unsigned char</td><td>uint8</td></tr>
<tr><td>short</td><td>double or int16</td></tr>
<tr><td>unsigned short</td><td>uint16</td></tr>
<tr><td>int</td><td>double or int32</td></tr>
<tr><td>unsigned int</td><td>uint32</td></tr>
<tr><td>long</td><td>double or int32</td></tr>
<tr><td>unsigned long</td><td>uint32</td></tr>
<tr><td>signed long long</td><td>not supported with Scilab 5.x</td></tr>
<tr><td>unsigned long long</td><td>not supported with Scilab 5.x</td></tr>
<tr><td>float</td><td>double</td></tr>
<tr><td>double</td><td>double</td></tr>
<tr><td>char* or char[]</td><td>string</td></tr>
</table>
</div>
<p>
Notes:
<ul>
<li>Double type in Scilab is far more used than integer type.
That's why signed integer values (short, int, integer, long) are automatically converted to Scilab double values in output of a C function.
Also in input, double values are converted from doubles into the appropriate integer type.
Note that this conversion does not occur with unsigned integers.
</li>
<li>
In SWIG for Scilab 5.x long long type is not supported since Scilab 5.x does not have a 64-bit integer type.
In that case, SWIG displays an error when wrapping a function that has long long type arguments.
</li>
</ul>
</p>
<H3><a name="Scilab_wrapping_non-primitive_type_mappings"></a>37.3.5 Default type mappings for non-primitive types</H3>
<p>
The default mapped type for C/C++ non-primitive types is the Scilab pointer. That is the case for exemple for C structs, C++ classes, ...
But there are many type mappings for non-primitive types (such as enums, arrays, STL types, etc...). Each of them is described further in this document.
</p>
<H3><a name="Scilab_wrapping_global_variables"></a>37.3.6 Global variables</H3>
<p>
To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_get would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variable value into the C variable.
To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_get would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variable value into the C variable.
</p>
<div class="targetlang"><pre>
@ -353,11 +406,11 @@ c = 3
ans = 4
</pre></div>
<H3><a name="Scilab_wrapping_constants"></a>37.3.5 Constants</H3>
<H3><a name="Scilab_wrapping_constants"></a>37.3.7 Constants</H3>
<p>
C constants are not really constant in Scilab. When dealing with the constants, a get function will be generated. For example given some constants:
C constants are not really constant in Scilab. When dealing with the constants, a get function will be generated. For example given some constants:
</p>
<div class="code"><pre>
@ -395,10 +448,10 @@ ans= 37
ans= 3.14
</pre></div>
<H3><a name="Scilab_wrapping_enums"></a>37.3.6 Enums</H3>
<H3><a name="Scilab_wrapping_enums"></a>37.3.8 Enums</H3>
<p> The way SWIG deals with the enums is similar to constants. For example:
<p> The way SWIG deals with the enums is similar to constants. For example:
</p>
<div class="code"><pre>%module example
@ -423,11 +476,11 @@ typedef enum { RED, BLUE, GREEN } color;
</pre></div>
<H3><a name="Scilab_wrapping_pointers"></a>37.3.7 Pointers</H3>
<H3><a name="Scilab_wrapping_pointers"></a>37.3.9 Pointers</H3>
<p>
Pointers are fully supported by SWIG. One way to deal with the pointers is using the INPUT and OUTPUT typemaps. For example, in order to call C functions as the following:
Pointers are fully supported by SWIG. One way to deal with the pointers is using the INPUT and OUTPUT typemaps. For example, in order to call C functions as the following:
</p>
<div class="code"><pre>
@ -468,10 +521,10 @@ extern int divide(int n, int d, int *r);
</pre></div>
<p> From the example above, it is clear that instead of passing a pointer to an object,
we only need a real value instead.
we only need a real value instead.
</p>
<H3><a name="Scilab_wrapping_structs"></a>37.3.8 Structs</H3>
<H3><a name="Scilab_wrapping_structs"></a>37.3.10 Structs</H3>
<p>
@ -495,11 +548,11 @@ typedef struct {
--&gt; Foo_x_set(a,100);
--&gt; Foo_x_get(a)
ans =
100
100
</pre></div>
<H3><a name="Scilab_wrapping_arrays"></a>37.3.9 Arrays</H3>
<H3><a name="Scilab_wrapping_arrays"></a>37.3.11 Arrays</H3>
<p>
@ -518,17 +571,17 @@ void initArray()
int i, n;
n = sizeof(x)/sizeof(x[0]);
for(i = 0; i &gt; n; i++)
for(i = 0; i &gt; n; i++)
x[i] = i;
n = sizeof(y)/sizeof(y[0]);
for(i = 0; i &lt; n; i++)
for(i = 0; i &lt; n; i++)
y[i] = ((double) i)/ ((double) n);
return;
%}
</pre></div>
<p> When wrapped, the following functions are generated: x_set(), x_get(), y_set(), y_get(), and _wrap_initArray.
<p> When wrapped, the following functions are generated: x_set(), x_get(), y_set(), y_get(), and _wrap_initArray.
They can be used like this:
</p>
@ -538,15 +591,15 @@ They can be used like this:
--&gt; initArray();
--&gt; x_get()
ans =
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
--&gt; y_get()
ans =
0. 0.1428571 0.2857143 0.4285714 0.5714286 0.7142857 0.8571429
</pre></div>
<H3><a name="Scilab_wrapping_matrices"></a>37.3.10 Matrices</H3>
<H3><a name="Scilab_wrapping_matrices"></a>37.3.12 Matrices</H3>
<p>
@ -562,7 +615,7 @@ double **new_matrix() {
M = (double **) malloc(4 * sizeof(double *));
M[0] = (double *) malloc(16 * sizeof(double));
for (i = 0; i &lt; 4; i++) {
M[i] = M[0] + 4 * i;
}
@ -594,10 +647,10 @@ void mat_mult(double **m1, double **m2, double **m3) {
int i,j,k;
double temp[4][4];
for (i = 0; i &lt; 4; i++)
for (i = 0; i &lt; 4; i++)
for (j = 0; j &lt; 4; j++) {
temp[i][j] = 0;
for (k = 0; k &lt; 4; k++)
for (k = 0; k &lt; 4; k++)
temp[i][j] += m1[i][k] * m2[k][j];
}
@ -612,13 +665,13 @@ void mat_mult(double **m1, double **m2, double **m3) {
</p>
<p>_wrap_new_matrix(): generate a new matrix.
</p>
<p>_wrap_set_m(M, i, j, a): set M(i, j) to be value a.
<p>_wrap_set_m(M, i, j, a): set M(i, j) to be value a.
</p>
<p>_wrap_get_m(M, i, j): get the value of M(i, j).
<p>_wrap_get_m(M, i, j): get the value of M(i, j).
</p>
<p>_wrap_print_matrix(M): print the matrix M.
<p>_wrap_print_matrix(M): print the matrix M.
</p>
<p>_wrap_mat_mult(A, B, C): compute the A * B and the result is stored into C.
<p>_wrap_mat_mult(A, B, C): compute the A * B and the result is stored into C.
</p>
<p>It can be used like this:
</p>
@ -660,7 +713,7 @@ void mat_mult(double **m1, double **m2, double **m3) {
</pre></div>
<H3><a name="Scilab_wrapping_classes"></a>37.4.11 Classes</H3>
<H3><a name="Scilab_wrapping_classes"></a>37.4.13 Classes</H3>
<p>
The classes are wrapped in the same manner as structs, through functions. For example, the following class:
@ -694,14 +747,14 @@ ans =
<H3><a name="Scilab_wrapping_templates"></a>37.4.12 Templates</H3>
<H3><a name="Scilab_wrapping_templates"></a>37.4.14 Templates</H3>
<p>
Templates are supported. See the SWIG general documentation on how templates are interfaced in SWIG.<br>
An example of templates can be found in <tt>Examples/scilab/templates</tt>.
</p>
<H3><a name="Scilab_wrapping_stl"></a>37.4.13 STL</H3>
<H3><a name="Scilab_wrapping_stl"></a>37.4.15 STL</H3>
<p>
Standard Template Library (STL) is partially supported.