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

    Compilation Issues

    diff --git a/Examples/perl5/pointer/Makefile b/Examples/perl5/pointer/Makefile new file mode 100644 index 000000000..e5944f9af --- /dev/null +++ b/Examples/perl5/pointer/Makefile @@ -0,0 +1,18 @@ +TOP = ../.. +SWIG = $(TOP)/../swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +SWIGOPT = +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' perl5 + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='myperl' INTERFACE='$(INTERFACE)' perl5_static + +clean:: + rm -f *_wrap* *.o core *~ *.so *.pm myperl + +check: all diff --git a/Examples/perl5/pointer/example.c b/Examples/perl5/pointer/example.c new file mode 100644 index 000000000..b877d9a5b --- /dev/null +++ b/Examples/perl5/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/perl5/pointer/example.i b/Examples/perl5/pointer/example.i new file mode 100644 index 000000000..2ed2b5bbf --- /dev/null +++ b/Examples/perl5/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/perl5/pointer/example.pl b/Examples/perl5/pointer/example.pl new file mode 100644 index 000000000..dc18144cf --- /dev/null +++ b/Examples/perl5/pointer/example.pl @@ -0,0 +1,42 @@ +# file: example.pl + +use example; + +# First create some objects using the pointer library. +print "Testing the pointer library\n"; +$a = example::ptrcreate("int",37); +$b = example::ptrcreate("int",42); +$c = example::ptrcreate("int"); + +print " a = $a\n"; +print " b = $b\n"; +print " c = $c\n"; + +# Call the add() function with some pointers +example::add($a,$b,$c); + +# Now get the result +$r = example::ptrvalue($c); +print " 37 + 42 = $r\n"; + +# Clean up the pointers +example::ptrfree($a); +example::ptrfree($b); +example::ptrfree($c); + +# Now try the typemap library +# This should be much easier. Now how it is no longer +# necessary to manufacture pointers. + +print "Trying the typemap library\n"; +$r = example::sub(37,42); +print " 37 - 42 = $r\n"; + +# Now try the version with multiple return values + +print "Testing multiple return values\n"; +($q,$r) = example::divide(42,37); +print " 42/37 = $q remainder $r\n"; + + + diff --git a/Examples/perl5/pointer/index.html b/Examples/perl5/pointer/index.html new file mode 100644 index 000000000..625c994ba --- /dev/null +++ b/Examples/perl5/pointer/index.html @@ -0,0 +1,173 @@ + + +SWIG:Examples:perl5:pointer + + + + +SWIG/Examples/perl5/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

    + + + +
    + +