adding struct example

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11457 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Baozeng Ding 2009-07-27 02:57:09 +00:00
commit 8217f59d55
8 changed files with 118 additions and 27 deletions

View file

@ -26,6 +26,7 @@
<li><a href="#Scilab_nn11">Constants</a>
<li><a href="#Scilab_nn12">Enums</a>
<li><a href="#Octave_nn13">Pointers</a>
<li><a href="#Octave_nn14">Structs</a>
</ul>
</ul>
</div>
@ -355,5 +356,29 @@ scilab:4&gt; printf(" 42/37 = %d remainder %d\n",q,r);
we only need a real value instead.
</p>
<H3><a name="Octave_nn14"></a>27.3.6 Structs</H3>
<p>
SWIG creates a set of accessor functions when encountering a structure or union. For example:
</p>
<div class="code"><pre>%module example
%inline %{
typedef struct {
int x;
} Foo;
%}
</pre></div>
<p> When wrappered, it would generate two main function: Foo_x_set(), which set the data value of the structrure and Foo_x_get() which could obtain the value of the structrure. Run it in Scilab:
</p>
<div class="targetlang"><pre>
a=new_Foo();
Foo_x_set(a,100);
Foo_x_get(a)
ans =
100
</pre></div>

View file

@ -1,3 +1,4 @@
#!/usr/bin/octave
# file: runme.m
example

View file

@ -0,0 +1,17 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.i
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 *_wrap.c
check: all
$(MAKE) -f $(TOP)/Makefile scilab_run

View file

@ -0,0 +1,11 @@
%module example
%rename(Bar) Foo;
%inline %{
typedef struct {
int x;
} Foo;
%}

View file

@ -0,0 +1,14 @@
// builder the *.so
exec builder.sce
//loader the *.so
exec loader.sce
//create a struct
a=new_Bar();
Bar_x_set(a,100);
Bar_x_get(a)
exit

View file

@ -1,7 +1,7 @@
%insert(runtime) %{
void SWIG_Error(int code, const char *msg) {
Scierror(code,_("%s\n"),msg);
Scierror(code, _("%s\n"), msg);
}
#define SWIG_contract_assert(expr, msg) if (!(expr)) { SWIG_Error(999, msg); } else

View file

@ -318,7 +318,7 @@
}
%typemap(in) SWIGTYPE *(int *piAddrVar, int iRows, int iCols) {
$&1_ltype _piData=($&1_ltype)0;
$&1_ltype _piData = ($&1_ltype)0;
getVarAddressFromPosition($argnum, &piAddrVar);
getVarDimension(piAddrVar, &iRows, &iCols);
@ -328,6 +328,7 @@
getMatrixOfDouble(piAddrVar, &iRows, &iCols, &_piData);
$1 = ($1_ltype)*_piData;
}
%typemap(in) SWIGTYPE {
}
@ -338,28 +339,34 @@
/* Basic C types */
%typemap(out) signed char (int iRowsOut, int iColsOut) {
char temp;
temp = (char)($result);
iRowsOut = 1;
iColsOut = 1;
createMatrixOfInteger8(iVarOut, iRowsOut, iColsOut, &temp);
createMatrixOfInteger8(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(out) short (int iRowsOut, int iColsOut),
unsigned char (int iRowsOut, int iColsOut) {
short temp;
temp = (short)($result);
%typemap(out) unsigned char (int iRowsOut, int iColsOut) {
iRowsOut = 1;
iColsOut = 1;
createMatrixOfInteger16(iVarOut, iRowsOut, iColsOut, &temp);
createMatrixOfUnsignedInteger8(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(out) short (int iRowsOut, int iColsOut) {
iRowsOut = 1;
iColsOut = 1;
createMatrixOfInteger16(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(out) unsigned short (int iRowsOut, int iColsOut) {
iRowsOut = 1;
iColsOut = 1;
createMatrixOfUnsignedInteger16(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(out) int (int iRowsOut, int iColsOut),
unsigned int (int iRowsOut, int iColsOut),
unsigned short (int iRowsOut, int iColsOut),
unsigned long (int iRowsOut, int iColsOut),
long (int iRowsOut, int iColsOut) {
int temp;
temp = (int)($result);
@ -369,6 +376,16 @@
LhsVar(iOutNum) = iVarOut;
}
%typemap(out) unsigned int (int iRowsOut, int iColsOut),
unsigned long (int iRowsOut, int iColsOut) {
int temp;
temp = (int)($result);
iRowsOut = 1;
iColsOut = 1;
createMatrixOfUnsignedInteger32(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut;
}
%typemap(out) double (int iRowsOut, int iColsOut),
float (int iRowsOut, int iColsOut) {
double temp;
@ -436,7 +453,7 @@
iVarOut++;
}
%typemap(out) char *(int iRowsOut, int iColsOut){
%typemap(out) char *(int iRowsOut, int iColsOut) {
iRowsOut = 1;
iColsOut = 1;
createMatrixOfString(iVarOut, iRowsOut, iColsOut, &($result));
@ -627,27 +644,33 @@
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) unsigned char {
createMatrixOfUnsignedInteger8(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) short {
createMatrixOfInteger16(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) unsigned char {
short temp;
temp = $result;
createMatrixOfInteger16(iVarOut, iRowsOut, iColsOut, &temp);
%typemap(varout,noblock=1) unsigned short {
createMatrixOfUnsignedInteger16(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) int,
unsigned int,
unsigned short,
unsigned long,
long {
createMatrixOfInteger32(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) unsigned int,
unsigned long {
createMatrixOfUnsignedInteger32(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) double {
createMatrixOfDouble(iVarOut, iRowsOut, iColsOut, &$result);
LhsVar(iOutNum) = iVarOut;

View file

@ -306,12 +306,12 @@ public:
else {
flag = 1;
}
Printf(f->def, "CheckRhs(%d,%d);\n",num_required,num_required);
Printf(f->def, "CheckLhs(%d,%d);\n",out_required,out_required);
Printf(f->def, "CheckRhs(%d, %d);\n",num_required,num_required);
Printf(f->def, "CheckLhs(%d, %d);\n",out_required,out_required);
/* Insert the order of output parameters*/
if(flag)
Printf(f->def, "\nint iOutNum=1;\nint iVarOut=Rhs+1;");
Printf(f->def, "\nint iOutNum = 1;\nint iVarOut = Rhs + 1;");
/* Finish the the code for the function */
Printf(f->code, "return 0;\n");
@ -404,8 +404,8 @@ public:
Printv(getf->def, "int ", getname, " (char *fname,unsigned long fname_len){\n", NIL);
/* Check the number of input and output */
Printf(getf->def, "CheckRhs(0,0);\n");
Printf(getf->def, "CheckLhs(1,1);\n");
Printf(getf->def, "CheckRhs(0, 0);\n");
Printf(getf->def, "CheckLhs(1, 1);\n");
/* Insert the order of output parameters*/
Printf(getf->def, "\nint iOutNum=1;\nint iVarOut=Rhs+1;");
@ -533,7 +533,7 @@ public:
value = Getattr(n, "value");
/* write into the code string */
Printf(f_example_code, "%s.%s=%s;\n", parentName, iname, value);
Printf(f_example_code, "%s.%s = %s;\n", parentName, iname, value);
return SWIG_OK;
}