|
SWIG/Examples/tcl/funcptr/
Pointers to Functions$Header: /cvs/projects/SWIG/Examples/tcl/funcptr/index.html,v 1.1 2000/09/04 15:54:56 beazley Exp $Okay, just what in the heck does SWIG do with a declaration like this? Well, it creates a wrapper as usual. Of course, that does raise some questions about the third argument (the pointer to a function).int do_op(int a, int b, int (*op)(int, int)); 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: 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:/* Some callback function */ int add(int a, int b) { return a+b; } ... int r = do_op(x,y,add); Now, in a script, you would do this:%constant(int (*)(int,int)) ADD = add; set r [do_op $x $y $ADD] An ExampleHere are some files that illustrate this with a simple example:
Notes
|