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.
+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?
+
+
+
+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);
+}
+
+
+
+Now, in a script you would do this:
+
+++ ++$a = new_int(37); +$b = new_int(42); +$c = new_int(0): +add($a,$b,$c); +$r = get_int($c); +print "Result = $r\n"; +delete_int($a); +delete_int($b); +delete_int($c); ++
+
+++%include "pointer.i" ++
+$a = ptrcreate("int",37);
+$b = ptrcreate("int",42);
+$c = ptrcreate("int");
+add($a,$b,$c);
+$r = ptrvalue($c);
+print "Result = $r\n";
+ptrfree($a);
+ptrfree($b);
+ptrfree($c);
+
+
+
+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.
+
++
++ +And in a script: + ++%include "typemaps.i" +void add(int *INPUT, int *INPUT, int *OUTPUT); ++
++Needless to say, this is substantially easier. + ++$r = add(37,42); +print "Result = $r\n"; ++
+
+
+%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 ...
+
+
+
++
+