diff --git a/Doc/Manual/Scilab.html b/Doc/Manual/Scilab.html index f5654940a..36d6320a4 100644 --- a/Doc/Manual/Scilab.html +++ b/Doc/Manual/Scilab.html @@ -39,7 +39,9 @@
-SWIG for Scilab provides only low-level C interface only for Scilab. This means that functions, structs, classes, variables, etc... are interfaced through C functions. These C functions are mapped as Scilab functions. +SWIG for Scilab provides only low-level C interface for Scilab. This means that functions, structs, classes, variables, etc... are interfaced through C functions. These C functions are mapped as Scilab functions.
- 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. +Global variables are manipulated through generated accessor functions. +For example, for a given Foo global variable, SWIG actually generates two functions: Foo_get() to get the value of Foo, and Foo_set() to set the value. +These functions are used as following:
@@ -358,6 +362,51 @@ c = 3 ans = 4
+It works for primitive type variables, but also for other type variables. +For example with two global arrays x and y: +
+ +
+%module example
+
+%inline %{
+int x[10];
+double y[7];
+
+void initArrays()
+{
+ int i;
+ for (i = 0; i < 10; i++)
+ x[i] = 1;
+ for (i = 0; i < 7; i++)
+ y[i] = 1.0f;
+}
+%}
++It works the same:
+ ++--> exec loader.sce + +--> initArrays(); +--> x_get() +ans = + + 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. + +--> y_set([0:6] / 10); +--> y_get() + +--> +ans = + + 0. 0.1 0.2 0.3 0.4 0.5 0.6 +
-One-dimensional arrays are supported whether as global variables or functions arguments. -Arrays are mapped in SWIG as pointers. But primitive type arrays are automatically converted from/to Scilab matrices. -
- --Global arrays are manipulated in Scilab through accessor functions. -For example with two global arrays x and y: -
- -
-%module example
-
-%inline %{
-int x[10];
-double y[7];
-
-void initArrays()
-{
- int i;
- for (i = 0; i < 10; i++)
- x[i] = 1;
- for (i = 0; i < 7; i++)
- y[i] = 1.0f;
-}
-%}
-Two Scilab functions are generated for each array: a getter _get() and a setter _set(), prefixed by the array name. -Following is an example of use of these functions: -
- ----> exec loader.sce - ---> initArrays(); ---> x_get() -ans = - - 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. - ---> y_set([0:6] / 10); ---> y_get() - ---> -ans = - - 0. 0.1 0.2 0.3 0.4 0.5 0.6 -
-The type mappings used for arrays is described in 37.4.1. -It means that, if needed, a Scilab double vector is converted in input into a C int array. -And this C int array is automatically converted in output to a Scilab double vector. -
- - --Matrices can be implemented in several ways in C, here we focus on matrices implemented with pointer-to-pointer (ex: double**). -
- --These matrices are mapped by default in SWIG as pointers. -There is no automatic conversion with Scilab matrices, for this, the matrix.i library has to be used. -
- -- Following is an example with functions working with matrices: -
- -
-%module example
-%inline %{
-
-// Returns the matrix [1 2; 3 4];
-double **create_matrix() {
- double **M;
- int i;
- M = (double **) malloc(2 * sizeof(double *));
- for (i = 0; i < 2; i++) {
- M[i] = (double *) malloc(2 * sizeof(double));
- M[i][0] = 2 * i + 1;
- M[i][1] = 2 * i + 2;
- }
- return M;
-}
-
-// Gets the item M(i,j) value
-double get_matrix(double **M, int i, int j) {
- return M[i][j];
-}
-
-// Sets the item M(i,j) value to be val
-void set_matrix(double **M, int i, int j, double val) {
- M[i][j] = val;
-}
-
-// Prints a matrix (2,2) to console
-void print_matrix(double **M, int nbRows, int nbCols) {
- int i, j;
- for (i = 0; i < 2; i++) {
- for (j = 0; j < 2; j++) {
- printf("%3g ", M[i][j]);
- }
- printf("\n");
- }
-}
-
-%}
-- These functions are used like this in Scilab: -
- ----> m = create_matrix(); - ---> print_matrix(m); - 1. 2. - 3. 4. - ---> set_matrix(m, 1, 1, 5.); - ---> get_matrix(m, 1, 1) - ans = - - 5. -
The classes are wrapped in the same manner as structs, through functions. For example, the following class: @@ -740,15 +655,14 @@ ans = - -
Templates are supported. See the SWIG general documentation on how templates are interfaced in SWIG.
An example of templates can be found in Examples/scilab/templates.
The Standard Template Library (STL) is partially supported. @@ -824,6 +738,8 @@ At last, the module initialization function has to be executed first in Scilab, See 37.5.6 for more details.
+ +Notes:
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, etc...
-- +Typemaps are available by default for arrays. Primitive type arrays are automatically converted from/to Scilab matrices.
++The type mappings used for arrays is the same for primtive types, described here. +It means that, if needed, a Scilab double vector is converted in input into a C int array. +And this C int array is automatically converted in output to a Scilab double vector. +
+ + ++There is no specific typemap for pointer-to-pointers, they are are mapped as pointers in Scilab. +
+ ++Pointer-to-pointers are sometimes used to implement matrices in C. Following is a an example of this: +
+ + +
+%module example
+%inline %{
+
+// Returns the matrix [1 2; 3 4];
+double **create_matrix() {
+ double **M;
+ int i;
+ M = (double **) malloc(2 * sizeof(double *));
+ for (i = 0; i < 2; i++) {
+ M[i] = (double *) malloc(2 * sizeof(double));
+ M[i][0] = 2 * i + 1;
+ M[i][1] = 2 * i + 2;
+ }
+ return M;
+}
+
+// Gets the item M(i,j) value
+double get_matrix(double **M, int i, int j) {
+ return M[i][j];
+}
+
+// Sets the item M(i,j) value to be val
+void set_matrix(double **M, int i, int j, double val) {
+ M[i][j] = val;
+}
+
+// Prints a matrix (2,2) to console
+void print_matrix(double **M, int nbRows, int nbCols) {
+ int i, j;
+ for (i = 0; i < 2; i++) {
+ for (j = 0; j < 2; j++) {
+ printf("%3g ", M[i][j]);
+ }
+ printf("\n");
+ }
+}
+
+%}
++ These functions are used like this in Scilab: +
+ ++--> m = create_matrix(); + +--> print_matrix(m); + 1. 2. + 3. 4. + +--> set_matrix(m, 1, 1, 5.); + +--> get_matrix(m, 1, 1) + ans = + + 5. +