diff --git a/Examples/tcl/index.html b/Examples/tcl/index.html index d3e853f6f..ef877bc4b 100644 --- a/Examples/tcl/index.html +++ b/Examples/tcl/index.html @@ -20,6 +20,7 @@ certain C declarations are turned into constants.
  • value. How to pass and return structures by value.
  • class. How wrap a simple C++ class.
  • reference. C++ references. +
  • pointer. Simple pointer handling.

    Compilation Issues

    diff --git a/Examples/tcl/pointer/Makefile b/Examples/tcl/pointer/Makefile new file mode 100644 index 000000000..fcb994d09 --- /dev/null +++ b/Examples/tcl/pointer/Makefile @@ -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 diff --git a/Examples/tcl/pointer/example.c b/Examples/tcl/pointer/example.c new file mode 100644 index 000000000..b877d9a5b --- /dev/null +++ b/Examples/tcl/pointer/example.c @@ -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; +} diff --git a/Examples/tcl/pointer/example.i b/Examples/tcl/pointer/example.i new file mode 100644 index 000000000..2ed2b5bbf --- /dev/null +++ b/Examples/tcl/pointer/example.i @@ -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); + + + + diff --git a/Examples/tcl/pointer/example.tcl b/Examples/tcl/pointer/example.tcl new file mode 100644 index 000000000..d953a55a5 --- /dev/null +++ b/Examples/tcl/pointer/example.tcl @@ -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" + + + diff --git a/Examples/tcl/pointer/index.html b/Examples/tcl/pointer/index.html new file mode 100644 index 000000000..2081f2bd9 --- /dev/null +++ b/Examples/tcl/pointer/index.html @@ -0,0 +1,173 @@ + + +SWIG:Examples:tcl:pointer + + + + +SWIG/Examples/tcl/pointer/ +
    + +

    Simple Pointer Handling

    + +$Header$
    + +

    +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: + +

    +
    +void add(int *x, int *y, int *r) { 
    +    *r = *x + *y;
    +}
    +
    +
    + +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? + +

    Possible Solutions

    + + + +

    Example

    + +The following example illustrates the use of these features for pointer +extraction. + + + +

    Notes

    + + + +
    + + diff --git a/Examples/tcl/simple/example.i b/Examples/tcl/simple/example.i index c16994fea..ed57d0796 100644 --- a/Examples/tcl/simple/example.i +++ b/Examples/tcl/simple/example.i @@ -3,5 +3,4 @@ extern int gcd(int x, int y); extern double Foo; - - +