global variable
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11249 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
4aa72f9b01
commit
25f17ae463
5 changed files with 183 additions and 72 deletions
|
|
@ -22,6 +22,7 @@
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="#Scilab_nn8">Modules</a>
|
<li><a href="#Scilab_nn8">Modules</a>
|
||||||
<li><a href="#Scilab_nn9">Functions</a>
|
<li><a href="#Scilab_nn9">Functions</a>
|
||||||
|
<li><a href="#scilab_nn10">Global variables</a>
|
||||||
</ul>
|
</ul>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -172,4 +173,19 @@ int fact(int n); </pre></div>
|
||||||
|
|
||||||
<div class="targetlang"><pre>Scilab:1>fact(4)
|
<div class="targetlang"><pre>Scilab:1>fact(4)
|
||||||
ant=24 </pre></div>
|
ant=24 </pre></div>
|
||||||
|
<H3><a name="scilab_nn10"></a>27.3.3 Global variables</H3>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_set 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 variables onto the C variable.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="targetlang"><pre>scilab:1> exec loader.sce;
|
||||||
|
scilab:2> c=Foo
|
||||||
|
c = 3
|
||||||
|
scilab:3> Foo=4;
|
||||||
|
scilab:4> c
|
||||||
|
c = 3
|
||||||
|
scilab:5> Foo
|
||||||
|
ans = 4</pre></div>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ double dvar = 0;
|
||||||
char *strvar = 0;
|
char *strvar = 0;
|
||||||
const char cstrvar[] = "Goodbye";
|
const char cstrvar[] = "Goodbye";
|
||||||
int *iptrvar = 0;
|
int *iptrvar = 0;
|
||||||
char name[256] = "Dave";
|
char name[5] = "Dave";
|
||||||
char path[256] = "/home/beazley";
|
char path[256] = "/home/beazley";
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -53,8 +53,8 @@ void print_vars() {
|
||||||
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
||||||
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
|
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
|
||||||
printf("iptrvar = %p\n", iptrvar);
|
printf("iptrvar = %p\n", iptrvar);
|
||||||
printf("name = %s\n", name);
|
printf("name = %c%c%c%c%c\n", name[0],name[1],name[2],name[3],name[4]);
|
||||||
printf("ptptr = %p (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0);
|
printf("ptptr = %p %s\n", ptptr, Point_print( ptptr ) );
|
||||||
printf("pt = (%d, %d)\n", pt.x, pt.y);
|
printf("pt = (%d, %d)\n", pt.x, pt.y);
|
||||||
printf("status = %d\n", status);
|
printf("status = %d\n", status);
|
||||||
}
|
}
|
||||||
|
|
@ -67,6 +67,10 @@ int *new_int(int value) {
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int value_int(int *value) {
|
||||||
|
return *value;
|
||||||
|
}
|
||||||
|
|
||||||
/* A function to create a point */
|
/* A function to create a point */
|
||||||
|
|
||||||
Point *new_Point(int x, int y) {
|
Point *new_Point(int x, int y) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
%include <typemaps/swigmacros.swg>
|
%include <typemaps/swigmacros.swg>
|
||||||
%include <typemaps/fragments.swg>
|
%include <typemaps/fragments.swg>
|
||||||
|
%include <sciprimtype.swg>
|
||||||
%include <scitypemaps.swg>
|
%include <scitypemaps.swg>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,49 +12,69 @@
|
||||||
//%include <typemaps/swigtypemaps.swg>
|
//%include <typemaps/swigtypemaps.swg>
|
||||||
|
|
||||||
|
|
||||||
%typemap(in) char (int m, int n,int l),
|
%typemap(in) char (int *piAddrVar, int iRows, int iCols),
|
||||||
signed char (int m,int n,int l),
|
signed char (int *piAddrVar, int iRows, int iCols),
|
||||||
unsigned char(int m,int n,int l)
|
unsigned char(int *piAddrVar, int iRows, int iCols)
|
||||||
{
|
{
|
||||||
if (GetType($argnum) == sci_strings)
|
char* _piData8;
|
||||||
{
|
getVarAddressFromNumber($argnum, &piAddrVar);
|
||||||
GetRhsVar($argnum,STRING_DATATYPE,&m,&n,&l);
|
getVarDimension(piAddrVar, &iRows, &iCols);
|
||||||
$1=($1_ltype)(*cstk(l));
|
if(getVarType(piAddrVar) != sci_matrix || iRows1 != 1 || iCols1 != 1 || isVarComplex(piAddrVar))
|
||||||
}
|
{
|
||||||
else
|
Scierror(999,_("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
|
||||||
Scierror(999,"error ...\n");
|
}
|
||||||
|
getMatrixOfInteger8(piAddrVar, &iRows, &iCols, &_piData8);
|
||||||
|
$1=_piData8[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
%typemap(in) short (int m, int n,int l),
|
%typemap(in) short (int *piAddrVar, int iRows, int iCols),
|
||||||
unsigned short (int m,int n,int l),
|
unsigned short (int *piAddrVar, int iRows, int iCols)
|
||||||
int(int m,int n,int l),
|
|
||||||
unsigned int(int m, int n,int l),
|
|
||||||
long(int m,int n,int l),
|
|
||||||
unsigned long(int m,int n,int l),
|
|
||||||
double(int m,int n,int l)
|
|
||||||
|
|
||||||
|
{ short* _piData16;
|
||||||
|
getVarAddressFromNumber($argnum, &piAddrVar);
|
||||||
|
getVarDimension(piAddrVar, &iRows, &iCols);
|
||||||
|
if(getVarType(piAddrVar) != sci_matrix || iRows1 != 1 || iCols1 != 1 || isVarComplex(piAddrVar))
|
||||||
|
{
|
||||||
|
Scierror(999,_("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
|
||||||
|
}
|
||||||
|
getMatrixOfInteger16(piAddrVar, &iRows, &iCols, &_piData16);
|
||||||
|
$1= _piData16[0];
|
||||||
|
|
||||||
{
|
|
||||||
if (GetType($argnum) == sci_matrix)
|
|
||||||
{
|
|
||||||
GetRhsVar($argnum,MATRIX_OF_DOUBLE_DATATYPE,&m,&n,&l);
|
|
||||||
$1=($1_ltype)(*stk(l));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
Scierror(999,"error ...\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
%typemap(in) float (int m, int n,int l)
|
%typemap(in) int (int *piAddrVar, int iRows, int iCols),
|
||||||
{
|
unsigned int (int *piAddrVar, int iRows, int iCols)
|
||||||
if (GetType($argnum) == sci_matrix)
|
|
||||||
{
|
{ int* _piData32;
|
||||||
GetRhsVar($argnum,MATRIX_OF_DOUBLE_DATATYPE,&m,&n,&l);
|
getVarAddressFromNumber($argnum, &piAddrVar);
|
||||||
$1=($1_ltype)(*stk(l));
|
getVarDimension(piAddrVar, &iRows, &iCols);
|
||||||
}
|
if(getVarType(piAddrVar) != sci_matrix || iRows1 != 1 || iCols1 != 1 || isVarComplex(piAddrVar))
|
||||||
else
|
{
|
||||||
Scierror(999,"error ...\n");
|
Scierror(999,_("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
|
||||||
|
}
|
||||||
|
getMatrixOfInteger16(piAddrVar, &iRows, &iCols, &_piData32);
|
||||||
|
$1= _piData32[0];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
%typemap(in) long(int* piAddrVar,int iRows,int iCols),
|
||||||
|
unsigned long(int* piAddrVar,int iRows,int iCols),
|
||||||
|
double(int* piAddrVar,int iRows,int iCols),
|
||||||
|
float (int* piAddrVar,int iRows,int iCols)
|
||||||
|
|
||||||
|
{ double *pdblReal;
|
||||||
|
getVarAddressFromNumber($argnum, &piAddrVar);
|
||||||
|
getVarDimension(piAddrVar, &iRows, &iCols);
|
||||||
|
if(getVarType(piAddrVar) != sci_matrix || iRows1 != 1 || iCols1 != 1 || isVarComplex(piAddrVar))
|
||||||
|
{
|
||||||
|
Scierror(999,_("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1);
|
||||||
|
}
|
||||||
|
getMatrixOfDouble(piAddrVar, &iRows, &iCols, &pdblReal);
|
||||||
|
$1= pdblReal[0];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
%typemap(in) char *(int m,int n,int l)
|
%typemap(in) char *(int m,int n,int l)
|
||||||
{
|
{
|
||||||
if (GetType($argnum) == sci_strings)
|
if (GetType($argnum) == sci_strings)
|
||||||
|
|
@ -65,54 +85,53 @@
|
||||||
else
|
else
|
||||||
Scierror(999,"error ...\n");
|
Scierror(999,"error ...\n");
|
||||||
|
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
%typemap(out) char (int m, int n,int l),
|
%typemap(out) char (int iRowsOut,int iColsOut,int* _piAddress),
|
||||||
signed char (int m,int n,int l),
|
signed char (int iRowsOut,int iColsOut,int* _piAddress),
|
||||||
unsigned char(int m,int n,int l)
|
unsigned char(int iRowsOut,int iColsOut,int* _piAddress)
|
||||||
{
|
{
|
||||||
m=1,n=1;
|
iRowsOut=1;
|
||||||
CreateVar(Rhs+1,STRING_DATATYPE,&m,&n,&l);
|
iColsOut=1;
|
||||||
*cstk(l)=$1;
|
createMatrixOfInteger8(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress);
|
||||||
LhsVar(1)=Rhs+1;
|
LhsVar(1)=Rhs+1;
|
||||||
|
PutLhsVar();
|
||||||
}
|
}
|
||||||
|
|
||||||
%typemap(out) short (int m, int n,int l),
|
%typemap(out) short (int iRowsOut,int iColsOut,int* _piAddress),
|
||||||
unsigned short (int m,int n,int l),
|
unsigned short(int iRowsOut,int iColsOut,int* _piAddress)
|
||||||
int(int m,int n,int l),
|
|
||||||
unsigned int(int m, int n,int l),
|
|
||||||
long(int m,int n,int l),
|
|
||||||
unsigned long(int m,int n,int l),
|
|
||||||
double(int m,int n,int l)
|
|
||||||
{
|
{
|
||||||
m=1,n=1;
|
iRowsOut=1;
|
||||||
CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&m,&n,&l);
|
iColsOut=1;
|
||||||
*stk(l)=(double)$1;
|
createMatrixOfInteger16(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress);
|
||||||
LhsVar(1)=Rhs+1;
|
LhsVar(1)=Rhs+1;
|
||||||
|
PutLhsVar();
|
||||||
}
|
}
|
||||||
|
|
||||||
%typemap(out) float (int m, int n,int l)
|
%typemap(out) int (int iRowsOut,int iColsOut,int* _piAddress),
|
||||||
|
unsigned int(int iRowsOut,int iColsOut,int* _piAddress)
|
||||||
|
|
||||||
{
|
{
|
||||||
m=1,n=1;
|
iRowsOut=1;
|
||||||
CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&m,&n,&l);
|
iColsOut=1;
|
||||||
*stk(l)=(double)$1;
|
createMatrixOfInteger32(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress);
|
||||||
LhsVar(1)=Rhs+1;
|
LhsVar(1)=Rhs+1;
|
||||||
|
PutLhsVar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
%typemap(out) char *(int m,int n, int l)
|
%typemap(out) long(int iRowsOut,int iColsOut,int* _piAddress),
|
||||||
|
unsigned long(int iRowsOut,int iColsOut,int* _piAddress),
|
||||||
|
double(int iRowsOut,int iColsOut,int* _piAddress),
|
||||||
|
float(int iRowsOut,int iColsOut,int* _piAddress)
|
||||||
{
|
{
|
||||||
m=1;
|
iRowsOut=1;
|
||||||
if ($1)
|
iColsOut=1;
|
||||||
n = (int)strlen($1);
|
createMatrixDouble(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress);
|
||||||
else
|
LhsVar(1)=Rhs+1;
|
||||||
n = (int)strlen("");
|
PutLhsVar();
|
||||||
|
|
||||||
CreateVar(Rhs+1,STRING_DATATYPE ,&m,&n,&l);
|
|
||||||
if ($1) strcpy(cstk(l),$1);
|
|
||||||
else strcpy(cstk(l),"");
|
|
||||||
LhsVar(1) = Rhs+1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -276,9 +276,80 @@ class SCILAB:public Language {
|
||||||
|
|
||||||
virtual int variableWrapper(Node *n) {
|
virtual int variableWrapper(Node *n) {
|
||||||
|
|
||||||
Language::variableWrapper(n); /* Default to functions */
|
String *name = Getattr(n, "name");
|
||||||
|
String *iname = Getattr(n, "sym:name");
|
||||||
|
SwigType *t = Getattr(n, "type");
|
||||||
|
|
||||||
|
if (!addSymbol(iname, n))
|
||||||
|
return SWIG_ERROR;
|
||||||
|
|
||||||
|
String *tm;
|
||||||
|
Wrapper *getf = NewWrapper();
|
||||||
|
Wrapper *setf = NewWrapper();
|
||||||
|
|
||||||
|
String *getname = Swig_name_get(iname);
|
||||||
|
String *setname = Swig_name_set(iname);
|
||||||
|
|
||||||
|
Printv(setf->def, "int ", setname, " (char *fname){", NIL);
|
||||||
|
|
||||||
|
Wrapper_add_local(setf, "piAddrVar", "int *piAddrVar");
|
||||||
|
Wrapper_add_local(setf, "iRows", "int iRows");
|
||||||
|
Wrapper_add_local(setf, "iCols", "int iCols");
|
||||||
|
// Wrapper_add_local(setf, "pdblReal", "double *pdblReal");
|
||||||
|
|
||||||
|
|
||||||
|
if (is_assignable(n)) {
|
||||||
|
Setattr(n, "wrap:name", setname);
|
||||||
|
if ((tm = Swig_typemap_lookup("in", n, name, 0))) {
|
||||||
|
Replaceall(tm, "$source", "args(0)");
|
||||||
|
Replaceall(tm, "$target", name);
|
||||||
|
Replaceall(tm, "$input", "args(0)");
|
||||||
|
Replaceall(tm, "$argnum", "1");
|
||||||
|
//if (Getattr(n, "tmap:varin:implicitconv")) {
|
||||||
|
//Replaceall(tm, "$implicitconv", get_implicitconv_flag(n));
|
||||||
|
//}
|
||||||
|
emit_action_code(n, setf->code, tm);
|
||||||
|
Delete(tm);
|
||||||
|
} else {
|
||||||
|
Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(t, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
//Printf(setf->code, "return octave_set_immutable(args,nargout);");
|
||||||
|
}
|
||||||
|
Append(setf->code, "}\n");
|
||||||
|
Wrapper_print(setf, f_wrappers);
|
||||||
|
Printf(f_builder, "\"%s\",\"%s\";",iname,setname);
|
||||||
|
|
||||||
|
Setattr(n, "wrap:name", getname);
|
||||||
|
int addfail = 0;
|
||||||
|
Printv(getf->def, "int ", getname, " (char *fname){", NIL);
|
||||||
|
|
||||||
|
Wrapper_add_local(getf, "piAddrOut", "int* _piAddress");
|
||||||
|
Wrapper_add_local(getf, "iRows", "int iRowsOut");
|
||||||
|
Wrapper_add_local(getf, "iColsOut", "int iColsOut ");
|
||||||
|
|
||||||
|
if ((tm = Swig_typemap_lookup("out", n, name, 0))) {
|
||||||
|
Replaceall(tm, "$source", name);
|
||||||
|
Replaceall(tm, "$target", "obj");
|
||||||
|
Replaceall(tm, "$result", "obj");
|
||||||
|
addfail = emit_action_code(n, getf->code, tm);
|
||||||
|
Delete(tm);
|
||||||
|
} else {
|
||||||
|
Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0));
|
||||||
|
}
|
||||||
|
//Append(getf->code, " return obj;\n");
|
||||||
|
//if (addfail) {
|
||||||
|
//Append(getf->code, "fail:\n");
|
||||||
|
//Append(getf->code, " return octave_value_list();\n");
|
||||||
|
//}
|
||||||
|
Append(getf->code, "}\n");
|
||||||
|
Wrapper_print(getf, f_wrappers);
|
||||||
|
Printf(f_builder, "\"%s\",\"%s\";",iname,getname);
|
||||||
|
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue