diff --git a/CHANGES.current b/CHANGES.current index 18ab3ba45..8ab562781 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,11 @@ See the RELEASENOTES file for a summary of changes in each release. Version 3.0.9 (in progress) =========================== +2016-04-06: smarchetto + [Scilab] #552 Make Scilab runtime keep track of pointer types + Instead of a Scilab pointer which has no type, SWIG Scilab maps a + pointer to a structure tlist containing the pointer adress and its type. + 2016-04-02: ahnolds [Python] Apply #598. Fix misleading error message when attempting to read a non-existent attribute. The previous cryptic error message: @@ -13,7 +18,7 @@ Version 3.0.9 (in progress) AttributeError: 'Foo' object has no attribute 'bar' 2016-04-02: derkuci - [Python] Patch #610 to fix #607. + [Python] Patch #610 to fix #607. Fix single arguments when using python -builtin -O with %feature("compactdefaultargs") 2016-03-31: wsfulton @@ -79,7 +84,7 @@ Version 3.0.9 (in progress) strips the symbol's suffix instead of the prefix. The example below will rename SomeThingCls to SomeThing and AnotherThingCls to AnotherThing: - %rename("%(rstrip:[Cls])s") ""; + %rename("%(rstrip:[Cls])s") ""; class SomeThingCls {}; struct AnotherThingCls {}; diff --git a/Doc/Manual/Scilab.html b/Doc/Manual/Scilab.html index 3e9e1c1a2..aa4a3e99f 100644 --- a/Doc/Manual/Scilab.html +++ b/Doc/Manual/Scilab.html @@ -45,6 +45,7 @@
-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).
+
+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.
-Given a wrapping of some of the C file functions: +Notes: +
+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:
diff --git a/Lib/scilab/scirun.swg b/Lib/scilab/scirun.swg index c59ad61dd..5625b5298 100644 --- a/Lib/scilab/scirun.swg +++ b/Lib/scilab/scirun.swg @@ -245,7 +245,7 @@ SwigScilabPtrFromObject(void *pvApiCtx, int iVarOut, void *pvObj, swig_type_info return SWIG_ERROR; } - pstString = SWIG_TypePrettyName(descriptor); + pstString = SWIG_TypeName(descriptor); sciErr = createMatrixOfStringInList(pvApiCtx, SWIG_NbInputArgument(pvApiCtx) + iVarOut, piTListAddr, 1, 1, 1, &pstString); if (sciErr.iErr) { printError(&sciErr, 0);