new example

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@804 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-09-02 19:05:15 +00:00
commit 37a6e409e4
7 changed files with 278 additions and 2 deletions

View file

@ -20,6 +20,7 @@ certain C declarations are turned into constants.
<li><a href="value/index.html">value</a>. How to pass and return structures by value.
<li><a href="class/index.html">class</a>. How wrap a simple C++ class.
<li><a href="reference/index.html">reference</a>. C++ references.
<li><a href="pointer/index.html">pointer</a>. Simple pointer handling.
</ul>
<h2>Compilation Issues</h2>

View file

@ -0,0 +1,19 @@
TOP = ../..
SWIG = $(TOP)/../swig
SRCS = example.c
TARGET = my_tclsh
DLTARGET = example
INTERFACE = example.i
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(DLTARGET)' INTERFACE='$(INTERFACE)' tcl
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o my_tclsh *~ .~* core *.so *.sl
check: all

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,23 @@
/* File : example.i */
%module example
/* 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 pointer.i
/* 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,45 @@
# file: example.tcl
catch { load ./example.so example}
catch { load ./example.dll example} ;# Windows
# First create some objects using the pointer library.
puts "Testing the pointer library"
set a [ptrcreate int 37]
set b [ptrcreate int 42]
set c [ptrcreate int] ;# Memory for result
puts " a = $a"
puts " b = $b"
puts " c = $c"
# Call the add() function with some pointers
add $a $b $c
# Now get the result
set r [ptrvalue $c]
puts " 37 + 42 = $r"
# Clean up the pointers
ptrfree $a
ptrfree $b
ptrfree $c
# Now try the typemap library
# This should be much easier. Now how it is no longer
# necessary to manufacture pointers.
puts "Trying the typemap library"
set r [sub 37 42]
puts " 37 - 42 = $r"
# Now try the version with multiple return values
puts "Testing multiple return values"
set qr [divide 42 37]
set q [lindex $qr 0]
set r [lindex $qr 1]
puts " 42/37 = $q remainder $r"

View file

@ -0,0 +1,173 @@
<html>
<head>
<title>SWIG:Examples:tcl:pointer</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/tcl/pointer/</tt>
<hr>
<H2>Simple Pointer Handling</H2>
<tt>$Header$</tt><br>
<p>
This example illustrates a couple of techniques for handling
simple pointers in SWIG. The prototypical example is a C function
that operates on pointers such as this:
<blockquote>
<pre>
void add(int *x, int *y, int *r) {
*r = *x + *y;
}
</pre>
</blockquote>
By default, SWIG wraps this function exactly as specified and creates
an interface that expects pointer objects for arguments. The only
problem is how does one go about creating these objects from a script?
<h2>Possible Solutions</h2>
<ul>
<li>Write some helper functions to explicitly create objects. For
example:
<blockquote>
<pre>
int *new_int(int ivalue) {
int *i = (int *) malloc(sizeof(ivalue));
*i = ivalue;
return i;
}
int get_int(int *i) {
return *i;
}
void delete_int(int *i) {
free(i);
}
</pre>
</blockquote>
Now, in a script you would do this:
<blockquote>
<pre>
set a [new_int 37]
set b [new_int 42]
set c [new_int 0]
add $a $b $c
set r [get_int $c]
print "Result = $r"
delete_int $a
delete_int $b
delete_int $c
</pre>
</blockquote>
<p>
<li>Use the SWIG pointer library. For example, in the interface file
you would do this:
<blockquote>
<pre>
%include "pointer.i"
</pre>
</blockquote?
and in a script you would do this:
<blockquote>
<pre>
set a [ptrcreate int 37]
set b [ptrcreate int 42]
set c [ptrcreate int]
add $a $b $c
set r [ptrvalue $c]
print "Result = $r"
ptrfree $a
ptrfree $b
ptrfree $c
</pre>
</blockquote>
The advantage to using the pointer library is that it unifies some of the helper
functions behind a common set of names. For example, the same set of functions work
with int, double, float, and other fundamental types.
<p>
<li>Use the SWIG typemap library. This library allows you to completely
change the way arguments are processed by SWIG. For example:
<blockquote>
<pre>
%include "typemaps.i"
void add(int *INPUT, int *INPUT, int *OUTPUT);
</pre>
</blockquote>
And in a script:
<blockquote>
<pre>
set r [add 37 42]
puts "Result = $r"
</pre>
</blockquote>
Needless to say, this is substantially easier.
<p>
<li>A final alternative is to use the typemaps library in combination
with the %apply directive. This allows you to change the names of parameters
that behave as input or output parameters. For example:
<blockquote>
<pre>
%include "typemaps.i"
%apply int *INPUT {int *x, int *y};
%apply int *OUTPUT {int *r};
void add(int *x, int *y, int *r);
void sub(int *x, int *y, int *r);
void mul(int *x, int *y, int *r);
... etc ...
</pre>
</blockquote>
</ul>
<h2>Example</h2>
The following example illustrates the use of these features for pointer
extraction.
<ul>
<li> <a href="example.c">example.c</a> (C Source)
<li> <a href="example.i">example.i</a> (Swig interface)
<li> <a href="example.tcl">example.tcl</a> (Tcl Script)
</ul>
<h2>Notes</h2>
<ul>
<li>Since pointers are used for so many different things (arrays, output values,
etc...) the complexity of pointer handling can be as complicated as you want to
make it.
<p>
<li>More documentation on the typemaps.i and pointer.i library files can be
found in the SWIG user manual. The files also contain documentation.
<p>
<li>The pointer.i library is designed primarily for convenience. If you
are concerned about performance, you probably want to use a different
approach.
</ul>
<hr>
</body>
</html>

View file

@ -3,5 +3,4 @@
extern int gcd(int x, int y);
extern double Foo;