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_nn11">Constants</a>
<li><a href="#Scilab_nn12">Enums</a> <li><a href="#Scilab_nn12">Enums</a>
<li><a href="#Octave_nn13">Pointers</a> <li><a href="#Octave_nn13">Pointers</a>
<li><a href="#Octave_nn14">Structs</a>
</ul> </ul>
</ul> </ul>
</div> </div>
@ -355,5 +356,29 @@ scilab:4&gt; printf(" 42/37 = %d remainder %d\n",q,r);
we only need a real value instead. we only need a real value instead.
</p> </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 # file: runme.m
example 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

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