Skip to content

Commit

Permalink
[MATLAB] Added funcptr example
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeandersson committed Apr 19, 2014
1 parent cf03733 commit cbcbdc1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Examples/matlab/funcptr/example.c
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;
8 changes: 8 additions & 0 deletions Examples/matlab/funcptr/example.h
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);
15 changes: 15 additions & 0 deletions Examples/matlab/funcptr/example.i
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);
18 changes: 18 additions & 0 deletions Examples/matlab/funcptr/runme.m
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

0 comments on commit cbcbdc1

Please sign in to comment.