diff --git a/SWIG/Examples/python/class/example.h b/SWIG/Examples/python/class/example.h index b01e8bb0a..c0f9b1d57 100644 --- a/SWIG/Examples/python/class/example.h +++ b/SWIG/Examples/python/class/example.h @@ -9,11 +9,9 @@ public: nshapes--; }; double x, y; - char foo[256]; void move(double dx, double dy); virtual double area() = 0; virtual double perimeter() = 0; - char *name() { return "Shape"; } static int nshapes; }; @@ -25,13 +23,6 @@ public: virtual double area(); virtual double perimeter(); }; - -class XCircle : public Circle { - public: - XCircle(double r) : Circle(r) { }; - virtual double area(); - char *name() { return "XCircle"; } -}; class Square : public Shape { private: diff --git a/SWIG/Examples/python/class/example.i b/SWIG/Examples/python/class/example.i index 3cd206c26..75700b305 100644 --- a/SWIG/Examples/python/class/example.i +++ b/SWIG/Examples/python/class/example.i @@ -6,9 +6,5 @@ %} /* Let's just grab the original header file here */ -%typemap(memberin) char [ANY] { - strncpy($target,$source,$dim0); -} - %include "example.h" diff --git a/SWIG/Examples/python/funcptr/example.h b/SWIG/Examples/python/funcptr/example.h new file mode 100644 index 000000000..58989db79 --- /dev/null +++ b/SWIG/Examples/python/funcptr/example.h @@ -0,0 +1,7 @@ +/* file: example.h */ + +extern int do_op(int,int, int (*op)(int,int)); +extern int add(int,int); +extern int sub(int,int); +extern int mul(int,int); + diff --git a/SWIG/Examples/python/funcptr/example.i b/SWIG/Examples/python/funcptr/example.i new file mode 100644 index 000000000..73cc6eb8c --- /dev/null +++ b/SWIG/Examples/python/funcptr/example.i @@ -0,0 +1,15 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +/* Wrap a function taking a pointer to a function */ +extern int do_op(int a, int b, int (*op)(int, int)); + +/* Now install a bunch of "ops" as constants */ +%constant(int (*)(int,int)) ADD = add; +%constant(int (*)(int,int)) SUB = sub; +%constant(int (*)(int,int)) MUL = mul; + + diff --git a/SWIG/Examples/python/funcptr/example.py b/SWIG/Examples/python/funcptr/example.py new file mode 100644 index 000000000..a85551bed --- /dev/null +++ b/SWIG/Examples/python/funcptr/example.py @@ -0,0 +1,20 @@ +# file: example.py + +import example + +a = 37 +b = 42 + +# Now call our C function with a bunch of callbacks + +print "Trying some C callback functions" +print " a =", a +print " b =", b +print " ADD(a,b) =", example.do_op(a,b,example.ADD) +print " SUB(a,b) =", example.do_op(a,b,example.SUB) +print " MUL(a,b) =", example.do_op(a,b,example.MUL) + +print "Here is what the C callback function objects look like in Python" +print " ADD =", example.ADD +print " SUB =", example.SUB +print " MUL =", example.MUL diff --git a/SWIG/Examples/python/funcptr/index.html b/SWIG/Examples/python/funcptr/index.html new file mode 100644 index 000000000..11c5ae43e --- /dev/null +++ b/SWIG/Examples/python/funcptr/index.html @@ -0,0 +1,92 @@ + + +SWIG:Examples:python:funcptr + + + + + +SWIG/Examples/python/funcptr/ +
+ +

Pointers to Functions

+ +$Header$
+ +

+Okay, just what in the heck does SWIG do with a declaration like this? + +

+
+int do_op(int a, int b, int (*op)(int, int));
+
+
+ +Well, it creates a wrapper as usual. Of course, that does raise some +questions about the third argument (the pointer to a function). + +

+In this case, SWIG will wrap the function pointer as it does for all other +pointers. However, in order to actually call this function from a script, +you will need to pass some kind of C function pointer object. In C, +this is easy, you just supply a function name as an argument like this: + +

+
+/* Some callback function */
+int add(int a, int b) {
+   return a+b;
+} 
+...
+int r = do_op(x,y,add);
+
+
+ +To make this work with SWIG, you will need to do a little extra work. Specifically, +you need to create some function pointer objects using the %constant directive like this: + +
+
+%constant(int (*)(int,int)) ADD = add;
+
+
+ +Now, in a script, you would do this: + +
+
+r = do_op(x,y, ADD)
+
+
+ +

An Example

+ +Here are some files that illustrate this with a simple example: + + + +

Notes

+ + + +
+ + + + + +