add INPUT/OUTPUT support

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11325 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Baozeng Ding 2009-06-27 09:08:23 +00:00
commit 5d23e5310a
8 changed files with 139 additions and 13 deletions

View file

@ -25,6 +25,7 @@
<li><a href="#scilab_nn10">Global variables</a>
<li><a href="#Scilab_nn11">Constants</a>
<li><a href="#Scilab_nn12">Enums</a>
<li><a href="#Octave_nn13">Pointers</a>
</ul>
</ul>
</div>
@ -297,4 +298,46 @@ scilab:4&gt; printf(" GREEN = %i\n", color.GREEN);
</pre></div>
<H3><a name="Octave_nn13"></a>27.3.5 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:
</p>
<div class="code"><pre>
void sub(int *x, int *y, int *result) {
*result = *x - *y;
}
int divide(int n, int d, int *r) {
int q;
q = n/d;
*r = n - q*d;
return q;
}
</pre></div>
<p> We could write a interface file:
</p>
<div class="code"><pre>%module example
%include typemaps.i
extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
%apply int *OUTPUT { int *r };
extern int divide(int n, int d, int *r);
</pre></div>
<p>Then run it in Scilab:
</p>
<div class="targetlang"><pre>
scilab:1&gt; r = sub(37,42);
scilab:2&gt; printf(" 37 - 42 = %i\n",r);
37 - 42 = -5
scilab:3&gt; [q,r] = divide(42,37);
scilab:4&gt; printf(" 42/37 = %d remainder %d\n",q,r);
42/37 = 1 remainder 5
</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.
</p>

View file

@ -0,0 +1,16 @@
/* File : example.c */
void add(int *x, int *y, int *result) {
*result = *x + *y;
}
void sub(int *x, int *y, int *result) {
*result = *x - *y;
}
int divide(int n, int d, int *r) {
int q;
q = n/d;
*r = n - q*d;
return q;
}

View file

@ -0,0 +1,30 @@
/* File : example.i */
%module example
%{
extern void add(int *, int *, int *);
extern void sub(int *, int *, int *);
extern int divide(int, int, int *);
%}
/* This example illustrates a couple of different techniques
for manipulating C pointers */
/* First we'll use the pointer library
extern void add(int *x, int *y, int *result);
%include cpointer.i
%pointer_functions(int, intp);*/
/* Next we'll use some typemaps */
%include typemaps.i
extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
/* Next we'll use typemaps and the %apply directive */
%apply int *OUTPUT { int *r };
extern int divide(int n, int d, int *r);

View file

@ -0,0 +1,15 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.c
TARGET = example
INTERFACE = example.i
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab
clean::
$(MAKE) -f $(TOP)/Makefile scilab_clean
rm -f *.sce *.so lib*lib.c
check: all

View file

@ -0,0 +1,20 @@
// builder the *.so
exec builder.sce
//loader the *.so
exec loader.sce
//Now try the typemap library
//This should be much easier. Now how it is no longer
//necessary to manufacture pointers.
printf("Trying the typemap library\n");
r = sub(37,42);
printf(" 37 - 42 = %i\n",r);
//Now try the version with multiple return values
printf("Testing multiple return values\n");
[q,r] = divide(42,37);
printf(" 42/37 = %d remainder %d\n",q,r);

View file

@ -31,7 +31,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_matrix || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfDouble(piAddrVar, &iRows, &iCols, &_piData);
$1=($1_ltype)*_piData;
@ -44,12 +44,13 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_strings || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfString(piAddrVar, &iRows, &iCols,&_piLength, &_pstStrings);
$1=($1_ltype)*_pstStrings;
}
/* Pointers */
%typemap(in) signed char *(int *piAddrVar, int iRows, int iCols) {
char* _piData;
@ -58,7 +59,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_ints || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfInteger8(piAddrVar, &iRows, &iCols, &_piData);
if($1!=NULL) {
@ -80,7 +81,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_ints || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfInteger16(piAddrVar, &iRows, &iCols, &_piData);
if($1!=NULL) {
@ -108,7 +109,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_ints || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfInteger32(piAddrVar, &iRows, &iCols, &_piData);
if($1!=NULL) {
@ -130,7 +131,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_matrix || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfDouble(piAddrVar, &iRows, &iCols, &_piData);
if($1!=NULL) {
@ -150,7 +151,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_strings || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfString(piAddrVar, &iRows, &iCols,&_piLength, &_pstStrings);
$1=strdup(_pstStrings);
@ -163,7 +164,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_ints || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfInteger8(piAddrVar, &iRows, &iCols, &_piData);
if($1!=NULL) {
@ -185,7 +186,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_ints || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfInteger16(piAddrVar, &iRows, &iCols, &_piData);
if($1!=NULL) {
@ -210,7 +211,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_ints || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfInteger32(piAddrVar, &iRows, &iCols, &_piData);
if($1!=NULL) {
@ -233,7 +234,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_matrix || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfDouble(piAddrVar, &iRows, &iCols, &_piData);
if($1!=NULL) {
@ -254,7 +255,7 @@
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_matrix || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfDouble(piAddrVar, &iRows, &iCols, &_piData);
$1=($1_ltype)*_piData;

View file

@ -68,7 +68,7 @@ or you can use the %apply directive :
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_matrix || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfDouble(piAddrVar, &iRows, &iCols, &_piData);
temp=($*1_ltype)*_piData;

View file

@ -35,6 +35,7 @@ public:
SCILAB():
f_builder_code(NewString("")), f_example_code(NewString("")), hasfunction_flag(false), hasconstant_flag(false) {
}
/* ------------------------------------------------------------
* main()