From 5dba60943c038b178faf55b5ea03ef09798965ac Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 27 Jan 2015 19:00:15 +0000 Subject: Scilab html fixes --- Doc/Manual/Scilab.html | 243 ++++++++++++++++++++++++++++++------------------- 1 file changed, 151 insertions(+), 92 deletions(-) diff --git a/Doc/Manual/Scilab.html b/Doc/Manual/Scilab.html index b862afc57..9a1cb1700 100644 --- a/Doc/Manual/Scilab.html +++ b/Doc/Manual/Scilab.html @@ -8,7 +8,7 @@ -

37 SWIG and Scilab

+

39 SWIG and Scilab

-
  • A tour of basic C/C++ wrapping +
  • A basic tour of C/C++ wrapping -
  • @@ -76,7 +87,8 @@ This chapter explains how to use SWIG for Scilab. After this introduction, you s

    -

    37.1 Preliminaries

    +

    39.1 Preliminaries

    +

    SWIG for Scilab supports Linux. Other operating sytems haven't been tested. @@ -92,7 +104,8 @@ SWIG for Scilab supports C language. C++ is partially supported. See 37.2 Running SWIG +

    39.2 Running SWIG

    +

    Let's see how to use SWIG for Scilab on a small example. @@ -125,7 +138,8 @@ Note: a code in an %inline section is both parsed and wrapped by SWIG,

    -

    37.2.1 Generating the module

    +

    39.2.1 Generating the module

    +

    The module is generated using the swig executable and its -scilab option. @@ -145,7 +159,7 @@ This command generates two files:

    Note: if the following error is returned: -

    +

     :1: Error: Unable to find 'swig.swg'
    @@ -167,7 +181,8 @@ The swig executable has several other command line options you can use.
     

    -

    37.2.2 Building the module

    +

    39.2.2 Building the module

    +

    To be loaded in Scilab, the wrapper has to be built into a dynamic module (or shared library). @@ -186,7 +201,8 @@ $ gcc -shared example_wrap.o -o libexample.so Note: we supposed in this example that the path to the Scilab include directory is /usr/local/include/scilab (which is the case in a Debian environment), this should be changed for another environment.

    -

    37.2.3 Loading the module

    +

    39.2.3 Loading the module

    +

    Loading a module is done by running the loader script in Scilab: @@ -209,7 +225,8 @@ Link done. which means that Scilab has successfully loaded the shared library. The module functions and other symbols are now available in Scilab.

    -

    37.2.4 Using the module

    +

    39.2.4 Using the module

    +

    In Scilab, the function fact() is simply called as following: @@ -242,7 +259,8 @@ ans = Note: for conciseness, we assume in the subsequent Scilab code examples that the modules have been beforehand built and loaded in Scilab.

    -

    37.2.5 Scilab command line options

    +

    39.2.5 Scilab command line options

    +

    The following table lists the Scilab specific command line options in addition to the generic SWIG options: @@ -296,17 +314,20 @@ $ swig -scilab -help

    -

    37.3 A basic tour of C/C++ wrapping

    +

    39.3 A basic tour of C/C++ wrapping

    + + +

    39.3.1 Overview

    -

    37.3.1 Overview

    SWIG for Scilab provides only a low-level C interface for Scilab (see Scripting Languages for the general approach to wrapping). This means that functions, structs, classes, variables, etc... are interfaced through C functions. These C functions are mapped as Scilab functions. There are a few exceptions, such as constants and enumerations, which can be wrapped directly as Scilab variables. -

    +

    + +

    39.3.2 Identifiers

    -

    37.3.2 Identifiers

    In Scilab 5.x, identifier names are composed of 24 characters maximum (this limitation should disappear from Scilab 6.0 onwards). @@ -314,9 +335,9 @@ In Scilab 5.x, identifier names are composed of 24 characters maximum (this limi

    This happens especially when wrapping structs/classes, for which the wrapped function name is composed of the struct/class name and field names. In these cases, the %rename directive can be used to choose a different Scilab name. -

    +

    -

    37.3.3 Functions

    +

    39.3.3 Functions

    @@ -347,7 +368,8 @@ ans = 24. -

    Argument passing

    +

    39.3.3.1 Argument passing

    +

    In the above example, the function parameter is a primitive type and is marshalled by value. @@ -399,7 +421,8 @@ In Scilab, parameters are passed by value. The output (and inout) parameters are 7. -

    Multiple output arguments

    +

    39.3.3.2 Multiple output arguments

    +

    A C function can have several output parameters. They can all be returned as results of the wrapped function as Scilab supports multiple return values from a function @@ -431,6 +454,8 @@ int divide(int n, int d, int q*, int *r) {

    +

    +
     --> [ret, q, r] = divide(20, 6)
      r  =
    @@ -443,10 +468,10 @@ int divide(int n, int d, int q*, int *r) {
     
         1.
     
    -

    -

    37.3.4 Global variables

    +

    39.3.4 Global variables

    +

    Global variables are manipulated through generated accessor functions. @@ -514,9 +539,11 @@ It works the same:

    -

    37.3.5 Constants and enumerations

    +

    39.3.5 Constants and enumerations

    + + +

    39.3.5.1 Constants

    -

    Constants

    There is not any constant in Scilab. By default, C/C++ constants are wrapped as getter functions. For example, for the following constants: @@ -656,7 +683,8 @@ are mapped to Scilab variables, with the same name: 3.14 -

    Enumerations

    +

    39.3.5.2 Enumerations

    +

    The wrapping of enums is the same as for constants. @@ -700,6 +728,8 @@ typedef enum { RED, BLUE, GREEN } color;

    +

    +
     --> exec loader.sce;
     --> RED
    @@ -718,9 +748,9 @@ typedef enum { RED, BLUE, GREEN } color;
         2.
     
     
    -

    -

    37.3.6 Pointers

    +

    39.3.6 Pointers

    +

    C/C++ pointers are fully supported by SWIG. They are mapped to the Scilab pointer type ("pointer", type ID: 128). @@ -761,7 +791,8 @@ These functions can be used in a natural way from Scilab: The user of a pointer is responsible for freeing it or, like in the example, closing any resources associated with it (just as is required in a C program).

    -

    Utility functions

    +

    39.3.6.1 Utility functions

    +

    Most of time pointer manipulation is not needed in a scripting language such as Scilab. @@ -770,11 +801,11 @@ However, in some cases it can be useful, such as for testing or debugging.

    SWIG comes with two pointer utility functions: +

    -

    Following illustrates their use on the last example:

    @@ -791,7 +822,8 @@ SWIG comes with two pointer utility functions: --> fclose(f); -

    Null pointers

    +

    39.3.6.2 Null pointers

    +

    By default, Scilab does not provide a way to test or create null pointers. But it is possible to have a null pointer by using the previous functions SWIG_this() and SWIG_ptr(), like this: @@ -806,7 +838,7 @@ But it is possible to have a null pointer by using the previous functions SW -

    37.3.7 Structures

    +

    39.3.7 Structures

    @@ -834,13 +866,13 @@ typedef struct {

    Several functions are generated: +

    -

    Usage example: @@ -865,7 +897,7 @@ ans =

    -Members of a structure that are also structures are also accepted and wrapped as a pointer:

    +Members of a structure that are also structures are also accepted and wrapped as a pointer:

    @@ -885,6 +917,8 @@ typedef struct {
     

    +

    +
     --> b = new_Bar();
     --> Bar_x_set(b, 20.);
    @@ -898,10 +932,10 @@ ans  =
     
         20.
     
    -

    -

    37.3.8 C++ Classes

    +

    39.3.8 C++ Classes

    +

    Classes do not exist in Scilab. The classes are wrapped the same way as structs. @@ -922,7 +956,7 @@ class Point { public: int x, y; Point(int _x, int _y) : x(_x), y(_y) {} - double distance(const Point& rhs) { + double distance(const Point& rhs) { return sqrt(pow(x-rhs.x, 2) + pow(y-rhs.y, 2)); } void set(int _x, int _y) { @@ -950,7 +984,8 @@ ans = --> delete_Point(p2); -

    37.3.9 C++ inheritance

    +

    39.3.9 C++ inheritance

    +

    Inheritance is supported. SWIG knows the inheritance relationship between classes. @@ -1024,7 +1059,8 @@ But we can use either use the get_perimeter() function of the parent cl 18.84 -

    37.3.10 Pointers, references, values, and arrays

    +

    39.3.10 Pointers, references, values, and arrays

    +

    In C++ objects can be passed by value, pointer, reference, or by an array: @@ -1044,8 +1080,8 @@ public: int x; }; -void spam1(Foo *f) { sciprint("%d\n", f->x); } // Pass by pointer -void spam2(Foo &f) { sciprint("%d\n", f.x); } // Pass by reference +void spam1(Foo *f) { sciprint("%d\n", f->x); } // Pass by pointer +void spam2(Foo &f) { sciprint("%d\n", f.x); } // Pass by reference void spam3(Foo f) { sciprint("%d\n", f.x); } // Pass by value void spam4(Foo f[]) { sciprint("%d\n", f[0].x); } // Array of objects @@ -1081,7 +1117,8 @@ All these functions will return a pointer to an instance of Foo. As the function spam7 returns a value, new instance of Foo has to be allocated, and a pointer on this instance is returned.

    -

    37.3.11 C++ templates

    +

    39.3.11 C++ templates

    +

    As in other languages, function and class templates are supported in SWIG Scilab. @@ -1140,7 +1177,8 @@ Then in Scilab: More details on template support can be found in the templates documentation.

    -

    37.3.11 C++ operators

    +

    39.3.12 C++ operators

    +

    C++ operators are partially supported. @@ -1164,7 +1202,7 @@ class Complex { public: Complex(double re, double im) : real(re), imag(im) {}; - Complex operator+(const Complex& other) { + Complex operator+(const Complex& other) { double result_real = real + other.real; double result_imaginary = imag + other.imag; return Complex(result_real, result_imaginary); @@ -1179,6 +1217,8 @@ private:

    +

    +
     --> c1 = new_Complex(3, 7);
     
    @@ -1189,10 +1229,10 @@ private:
     
         4.
     
    -

    -

    34.3.12 C++ namespaces

    +

    39.3.13 C++ namespaces

    +

    SWIG is aware of C++ namespaces, but does not use it for wrappers. @@ -1209,7 +1249,7 @@ For example with one namespace Foo: namespace foo { int fact(int n) { - if (n > 1) + if (n > 1) return n * fact(n-1); else return 1; @@ -1269,7 +1309,8 @@ Note: the nspace feature is

    -

    37.3.13 C++ exceptions

    +

    39.3.14 C++ exceptions

    +

    Scilab does not natively support exceptions, but has errors. @@ -1288,19 +1329,19 @@ void throw_exception() throw(char const *) {

    +

    +
     -->throw_exception()
       !--error 999
     SWIG/Scilab: Exception (char const *) occured: Bye world !
     
    -

    Scilab has a try-catch mechanism (and a similar instruction execstr()) to handle exceptions. It can be used with the lasterror() function as following:

    -

     --> execstr('throw_exception()', 'errcatch');
      ans  =
    @@ -1312,7 +1353,6 @@ It can be used with the lasterror() function as following:
     
         SWIG/Scilab: Exception (char const *) occured: Bye world !
     
    -

    If the function has a throw exception specification, SWIG can automatically map the exception type and set an appropriate Scilab error message. @@ -1330,13 +1370,15 @@ void throw_int() throw(int) { } void throw_stl_invalid_arg(int i) throw(std::invalid_argument) { - if (i < 0) + if (i &lt 0) throw std::invalid_argument("argument is negative."); } %}

    +

    +
     --> throw_int();
                 !--error 999
    @@ -1346,29 +1388,31 @@ SWIG/Scilab: Exception (int) occured: 12
                               !--error 999
     SWIG/Scilab: ValueError: argument is negative.
     
    -

    More complex or custom exception types require specific exception typemaps to be implemented in order to specifically handle a thrown type. See the SWIG C++ documentation for more details. -

    +

    + +

    39.3.15 C++ STL

    -

    37.3.14 C++ STL

    The Standard Template Library (STL) is partially supported. See STL for more details.

    -

    37.4 Type mappings and libraries

    +

    39.4 Type mappings and libraries

    + + +

    39.4.1 Default primitive type mappings

    -

    37.4.1 Default primitive type mappings

    The following table provides the equivalent Scilab type for C/C++ primitive types.

    - +
    @@ -1393,6 +1437,7 @@ The following table provides the equivalent Scilab type for C/C++ primitive type

    Notes: +

    • In Scilab the double type is far more used than any integer type. This is why integer values (int32, uint32, ...) are automatically converted to Scilab double values when marshalled from C into Scilab. @@ -1406,17 +1451,18 @@ In SWIG for Scilab 5.x, the long long type is not supported, since Scil The default behaviour is for SWIG to generate code that will give a runtime error if long long type arguments are used from Scilab.
    -

    -

    37.4.2 Default type mappings for non-primitive types

    +

    39.4.2 Default type mappings for non-primitive types

    +

    The default mapped type for C/C++ non-primitive types is the Scilab pointer, for example for C structs, C++ classes, etc...

    -

    37.4.3 Arrays

    +

    39.4.3 Arrays

    +

    Typemaps are available by default for arrays. Primitive type arrays are automatically converted to/from Scilab matrices. @@ -1435,9 +1481,6 @@ and this C integer array is automatically converted on output into a Scilab Note that unlike scalars, no control is done for arrays when a double is converted into an integer.

    -

    -

    -

    The following example illustrates all this:

    @@ -1458,6 +1501,8 @@ void printArray(int values[], int len) {

    +

    +
     --> printArray([0 1 2 3], 4)
     [ 0  1  2  3 ]
    @@ -1470,10 +1515,10 @@ void printArray(int values[], int len) {
     
     --> printArray([0; 1; 2; 3], 4)
     [ 0  1  2  3 ]
    -
    -

    + + +

    39.4.4 Pointer-to-pointers

    -

    37.4.4 Pointer-to-pointers

    There are no specific typemaps for pointer-to-pointers, they are are mapped as pointers in Scilab. @@ -1545,7 +1590,8 @@ void print_matrix(double **M, int nbRows, int nbCols) { -

    37.4.5 Matrices

    +

    39.4.5 Matrices

    +

    The matrix.i library provides a set of typemaps which can be useful when working with one-dimensional and two-dimensional matrices. @@ -1562,32 +1608,33 @@ In order to use this library, just include it in the interface file:

    Several typemaps are available for the common Scilab matrix types: +

    • double
    • int
    • char *
    • bool
    -

    For example: for a matrix of int, we have the typemaps, for input: +

    • (int *IN, int IN_ROWCOUNT, int IN_COLCOUNT)
    • (int IN_ROWCOUNT, int IN_COLCOUNT, int *IN)
    • (int *IN, int IN_SIZE)
    • (int IN_SIZE, int *IN)
    -

    +

    and output: +

    • (int **OUT, int *OUT_ROWCOUNT, int *OUT_COLCOUNT)
    • (int *OUT_ROWCOUNT, int *OUT_COLCOUNT, int **OUT)
    • (int **OUT, int *OUT_SIZE)
    • (int *OUT_SIZE, int **OUT)
    -

    They marshall a Scilab matrix type into the appropriate 2 or 3 C parameters. @@ -1619,6 +1666,8 @@ void absolute(int *matrix, int matrixNbRow, int matrixNbCol,

    +

    +
     --> absolute([-0 1 -2; 3 4 -5])
      ans  =
    @@ -1626,38 +1675,39 @@ void absolute(int *matrix, int matrixNbRow, int matrixNbCol,
         0.    1.    2.
         3.    4.    5.
     
    -

    The remarks made earlier for arrays also apply here: +

    • The values of matrices in Scilab are column-major orderered,
    • There is no control while converting double values to integers, double values are truncated without any checking or warning.
    -

    -

    37.4.6 STL

    +

    39.4.6 STL

    +

    The STL library wraps some containers defined in the STL (Standard Template Library), so that they can be manipulated in Scilab. This library also provides the appropriate typemaps to use the containers in functions and variables. -

    +

    The list of wrapped sequence containers are: +

    • std::vector
    • std::list
    • std::deque
    -

    +

    And associative containers are: +

    • std::set
    • std::multiset
    -

    Typemaps are available for the following container types: @@ -1702,7 +1752,7 @@ See the Module initialization sectio

    Because in Scilab matrices exist for basic types only, a sequence container of pointers is mapped to a Scilab list. For other item types (double, int, string...) the sequence container is mapped to a Scilab matrix. -

    +

    The first example below shows how to create a vector (of int) in Scilab, add some values to the vector and pass it as an argument of a function. @@ -1732,6 +1782,8 @@ double average(std::vector<int> v) {

    +

    +
     --> example_Init();
     
    @@ -1753,7 +1805,6 @@ double average(std::vector<int> v) {
     
     --> delete_IntVector();
     
    -

    @@ -1790,7 +1841,7 @@ namespace std { std::set<PersonPtr> findPersonsByAge(std::set<PersonPtr> persons, int minAge, int maxAge) { std::set<PersonPtr> foundPersons; for (std::set<PersonPtr>::iterator it = persons.begin(); it != persons.end(); it++) { - if (((*it)->age >= minAge) && ((*it)->age <= maxAge)) { + if (((*it)->age >= minAge) && ((*it)->age <= maxAge)) { foundPersons.insert(*it); } } @@ -1801,6 +1852,8 @@ std::set<PersonPtr> findPersonsByAge(std::set<PersonPtr> persons, in

    +

    +
     --> example_Init();
     
    @@ -1832,9 +1885,9 @@ ans  =
     
     --> delete_PersonPtrSet(p);
     
    -

    -

    37.5 Module initialization

    +

    39.5 Module initialization

    +

    The wrapped module contains an initialization function to: @@ -1857,7 +1910,8 @@ For example, to initialize the module example: --> example_Init(); -

    37.6 Building modes

    +

    39.6 Building modes

    +

    The mechanism to load an external module in Scilab is called Dynamic Link and works with dynamic modules (or shared libraries, .so files). @@ -1871,7 +1925,8 @@ To produce a dynamic module, when generating the wrapper, there are two possibil

  • the builder mode. In this mode, Scilab is responsible of building. -

    37.6.1 No-builder mode

    +

    39.6.1 No-builder mode

    +

    In this mode, used by default, SWIG generates the wrapper sources, which have to be manually compiled and linked. @@ -1883,7 +1938,8 @@ This mode is the best option to use when you have to integrate the module build

    -

    37.6.2 Builder mode

    +

    39.6.2 Builder mode

    +

    In this mode, in addition to the wrapper sources, SWIG produces a builder Scilab script (builder.sce), which is executed in Scilab to build the module. @@ -1908,11 +1964,11 @@ In this mode, the following SWIG options may be used to setup the build:

    Let's give an example how to build a module example, composed of two sources, and using a library dependency: +

    • the sources are baa1.c and baa2.c (and are stored in in the current directory)
    • the library is libfoo in /opt/foo (headers stored in /opt/foo/include, and shared library in /opt/foo/lib)
    -

    The command is: @@ -1921,15 +1977,16 @@ The command is:

     $ swig -scilab -builder -buildercflags -I/opt/foo/include -builderldflags "-L/opt/foo/lib -lfoo" -buildersources baa1.cxx,baa2.cxx example.i
     
    -

    -

    37.7 Generated scripts

    +

    39.7 Generated scripts

    +

    In this part we give some details about the generated Scilab scripts.

    -

    37.7.1 Builder script

    +

    39.7.1 Builder script

    +

    builder.sce is the name of the builder script generated by SWIG in builder mode. It contains code like this: @@ -1953,7 +2010,8 @@ ilib_build(ilib_name,table,files,libs);

  • table: two column string matrix containing a table of pairs of 'scilab function name', 'C function name'.
  • -

    37.7.2 Loader script

    +

    39.7.2 Loader script

    +

    The loader script is used to load in Scilab all the module functions. When loaded, these functions can be used as other Scilab functions. @@ -1991,7 +2049,8 @@ clear get_file_path; -

    37.8 Other resources

    +

    39.8 Other resources

    +
    • Example use cases can be found in the Examples/scilab directory.
    • -- cgit v1.2.1
    C/C++ type Scilab type