From 9f4ad6e7dc049913b1db3eb2f9a798f876a85be1 Mon Sep 17 00:00:00 2001
From: Simon Marchetto
-C/C++ pointers are fully supported by SWIG. They are mapped to the Scilab pointer type ("pointer", type ID: 128).
+Pointers are supported by SWIG. A pointer can be returned from a wrapped C/C++ function, stored in a Scilab variable, and used in input argument of another C/C++ function.
+
+Also, thanks to the SWIG runtime which stores informations about types, pointer types are tracked between exchanges Scilab and the native code. Indeed pointer types are stored alongside the pointer adress.
+A pointer is mapped to a Scilab structure (tlist), which contains as fields the pointer address and the pointer type (in fact a pointer to the type information structure in the SWIG runtime).
+
-Given a wrapping of some of the C file functions:
+Notes:
+
+Why a native pointer is not mapped to a Scilab pointer (type name: "pointer", type ID: 128) ? The big advantage of mapping to a tlist is that it exposes a new type for the pointer in Scilab, type which can be acessed in Scilab with the typeof function, and manipulated using the overloading mechanism.
+
+
+Following is an example of the wrapping of the C FILE* pointer:
@@ -772,20 +787,26 @@ int fclose(FILE *);
-These functions can be used in a natural way from Scilab: +These functions can be used the same way as in C from Scilab:
+--> example_Init();
+
--> f = fopen("junk", "w");
--> typeof(f)
ans =
- pointer
+ _p_FILE
--> fputs("Hello World", f);
--> fclose(f);
+Note: the type name _p_FILE which means "pointer to FILE". +
+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).
@@ -794,7 +815,7 @@ The user of a pointer is responsible for freeing it or, like in the example, clo-Most of time pointer manipulation is not needed in a scripting language such as Scilab. +As a scripting language, Scilab does not provide functions to manipulate pointers. However, in some cases it can be useful, such as for testing or debugging.
@@ -806,7 +827,11 @@ SWIG comes with two pointer utility functions:Following illustrates their use on the last example:
++Note: a pointer created by SWIG_ptr() does not have any type and is mapped as a Scilab pointer. +
+ +Following we use the utility functions on the previous example:
--> f = fopen("junk", "w");
@@ -817,15 +842,20 @@ SWIG comes with two pointer utility functions:
8219088.
--> p = SWIG_ptr(addr);
+--> typeof(p)
+ans =
+
+ pointer
+
--> fputs(" World", p);
--> fclose(f);
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: +
+Using the previous SWIG_this() and SWIG_ptr(), it is possible to create and check null pointers:
@@ -873,7 +903,7 @@ Several functions are generated:
+ Usage example:
@@ -931,6 +961,19 @@ ans = 20.+Note: the pointer to the struct works as described in Pointers. For example, the type of the struct pointer can be get with typeof, as following: +
+ +
+--> example_Init(); +--> b = new_Bar(); +--> typeof(b) + ans = + + _p_Bar +--> delete_Bar(b); +
+Note: like structs, class pointers are mapped as described in Pointers. Let's give an example which shows that each class pointer type is a new type in Scilab that can be used for example (through overloading) to implement a custom print for the Point class: +
+ +
+--> function %_p_Point_p(p)
+--> mprintf('[%d, %d]\n', Point_x_get(p), Point_y_get(p));
+--> endfunction
+
+--> example_Init();
+--> p = new_Point(1, 2)
+ p =
+
+[1, 2]
+
+--> delete_Point(p);
++As explained in 6.15 SWIG provides support for overloaded functions and constructors. +
+ +As SWIG knows pointer types, the overloading works also with pointer types, here is is an example with a function magnify overloaded for the previous classes Shape and Circle: + +
+
+%module example
+
+void magnify(Square *square, double factor) {
+ square->size *= factor;
+};
+
+void magnify(Circle *circle, double factor) {
+ square->radius *= factor;
+};
++
+--> example_Init(); +--> c = new_Circle(3); +--> s = new_Square(2); + +--> magnify(c, 10); +--> Circle_get_radius(c) + ans = + + 30; +--> magnify(s, 10); +--> Square_get_size(s) + ans = + + 20; +
@@ -1115,7 +1218,7 @@ 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.
-@@ -1123,8 +1226,7 @@ As in other languages, function and class templates are supported in SWIG Scilab
-You have to tell SWIG to create wrappers for a particular -template instantiation. The %template directive is used for this purpose. +You have to tell SWIG to create wrappers for a particular template instantiation. The %template directive is used for this purpose. For example:
@@ -1175,7 +1277,7 @@ Then in Scilab: More details on template support can be found in the templates documentation. -@@ -1228,7 +1330,7 @@ private: -
@@ -1306,7 +1408,7 @@ Note: the nspace feature is not supp
-@@ -1389,7 +1491,7 @@ More complex or custom exception types require specific exception typemaps to be See the SWIG C++ documentation for more details.
-@@ -1448,15 +1550,9 @@ The default behaviour is for SWIG to generate code that will give a runtime erro -
-The default mapped type for C/C++ non-primitive types is the Scilab pointer, for example for C structs, C++ classes, etc... -
- - -@@ -1511,7 +1607,7 @@ void printArray(int values[], int len) { [ 0 1 2 3 ] -
@@ -1584,7 +1680,7 @@ void print_matrix(double **M, int nbRows, int nbCols) { -
@@ -1677,7 +1773,7 @@ The remarks made earlier for arrays also apply here:
@@ -1884,8 +1980,8 @@ ans = The wrapped module contains an initialization function to:
From db29b6a00583b35d09a3fb460a2e271448f37b5b Mon Sep 17 00:00:00 2001
From: Simon Marchetto