On Thu, 05 Jun 2003 19:04:43 +0200, Simon Strandgaard wrote:

> On Thu, 05 Jun 2003 17:07:03 +0200, Christian Szegedy wrote:
> 
>> In out project, there are a lot of functions which
>> take a function object parameter (Apply<T>)
>> and call then may call the call method of this
>> object. I would like to wrap it with a Ruby code
>> which takes a block and the creates a new apply
>> object whose call function calls the block.
> 
> It is possible.. But I don't know how to exactly do this.


Have you looked in the examples supplyed with SWIG ?

There is a good example of a funcptr.


> pwd
/usr/home/neoneye/install/language/SWIG-1.3.19/Examples/ruby/funcptr
> ls
Makefile        example.h       index.html
example.c       example.i       runme.rb
> cat example.h
/* 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);

> cat example.c
/* 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;
}
> cat example.i
/* 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 (*ADD)(int,int) = add;
%constant int (*SUB)(int,int) = sub;
%constant int (*MUL)(int,int) = mul;


> cat runme.rb
# file: runme.rb

require 'example'

a = 37
b = 42

# Now call our C function with a bunch of callbacks

print "Trying some C callback functions\n"
print "    a        = #{a}\n"
print "    b        = #{b}\n"
print "    ADD(a,b) = ", Example::do_op(a,b,Example::ADD),"\n"
print "    SUB(a,b) = ", Example::do_op(a,b,Example::SUB),"\n"
print "    MUL(a,b) = ", Example::do_op(a,b,Example::MUL),"\n"

print "Here is what the C callback function objects look like in Ruby\n"
print "    ADD      = #{Example::ADD}\n"
print "    SUB      = #{Example::SUB}\n"
print "    MUL      = #{Example::MUL}\n"

>


--
Simon Strandgaard