-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf03733
commit cbcbdc1
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* File : example.c */ | ||
|
||
int do_op(int a, int b, int (*op)(int,int)) { | ||
return (*op)(a,b); | ||
} | ||
|
||
int add(int a, int b) { | ||
return a+b; | ||
} | ||
|
||
int sub(int a, int b) { | ||
return a-b; | ||
} | ||
|
||
int mul(int a, int b) { | ||
return a*b; | ||
} | ||
|
||
int (*funcvar)(int,int) = add; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* 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); | ||
|
||
extern int (*funcvar)(int,int); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* File : example.i */ | ||
%module swigexample | ||
%{ | ||
#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 (*ADD)(int,int) = add; | ||
%constant int (*SUB)(int,int) = sub; | ||
%constant int (*MUL)(int,int) = mul; | ||
|
||
extern int (*funcvar)(int,int); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
% file: runme.m | ||
|
||
a = 37 | ||
b = 42 | ||
|
||
% Now call our C function with a bunch of callbacks | ||
|
||
disp(sprintf('Trying some C callback functions')) | ||
disp(sprintf(' a = %i', a)) | ||
disp(sprintf(' b = %i', b)) | ||
disp(sprintf(' ADD(a,b) = %i', swigexample.do_op(a,b,swigexample.ADD))) | ||
disp(sprintf(' SUB(a,b) = %i', swigexample.do_op(a,b,swigexample.SUB))) | ||
disp(sprintf(' MUL(a,b) = %i', swigexample.do_op(a,b,swigexample.MUL))) | ||
|
||
disp(sprintf('Here is what the C callback function objects look like in Octave')) | ||
swigexample.ADD | ||
swigexample.SUB | ||
swigexample.MUL |