[Vala] Creating a binding for a non-glib library and using delegate to map a function pointer



Hi all,

I have a procedural-style library that I want to use in oo-style
program in vala.

Following hints from this webpage
[http://mike.trausch.us/blog/2009/02/18/vala-bindings-for-non-gobject-type-c-libraries/]
as suggested on the mailing list, I try to build a vapi that maps
strictly the library and then make a class to wrap the functionality
in a object oriented way.

I encounter problems that, it seems to me, are related to the use of delegates.

The library has some functions that receive a void *(*f)(void *)
A testcase: I prepared the library sources.
 libfoo.c    [http://pastebin.com/u89bWnib]
 libfoo.h    [http://pastebin.com/kDr91Cfg]
Build the library with:
 gcc -c libfoo.c

First, test the library. I prepared a testing app in C.
 testfoo.c    [http://pastebin.com/UiZk6BiG]
Build the test app with:
 gcc -o testfoo testfoo.c libfoo.o
Run the testing app with:
 ./testfoo
 echo $?
 0

First pass, I prepared a direct mapping vapi.
 libfoo.vapi    [http://pastebin.com/rZb6JqAZ]
  [IMPORTANT note: if I don't miss something, I have
  [to create a delegate, named FunctionDelegate, in the vapi
  [file in order to map the function pointer argument.
Then I prepared a testing app in vala.
 test_foo.vala    [http://pastebin.com/6ecJLpic]
  [IMPORTANT note: test_foo.vala does not have
  [to explicitly use the delegate FunctionDelegate
Build:
 valac -C test_foo.vala --pkg libfoo --vapidir .
 gcc -o test_foo $(pkg-config --cflags --libs gobject-2.0) -I.
test_foo.c libfoo.o
Run the testing app with:
 ./test_foo
 echo $?
 0

Second pass, I prepared a wrapping module in vala.
 libwrapfoo.vala    [http://pastebin.com/z2JExW4v]
Then I prepared a testing app in vala.
 test_oo_foo.vala    [http://pastebin.com/XiAGk9Ww]
If i try to build with:
 valac -C test_oo_foo.vala libwrapfoo.vala --pkg libfoo --vapidir .
 gcc -o test_oo_foo $(pkg-config --cflags --libs gobject-2.0) -I.
test_oo_foo.c libwrapfoo.c libfoo.o
The second command fails because FunctionDelegate is not defined.
If i manually add
  typedef void *(*FunctionDelegate) (void *);
to both the generated files "test_oo_foo.c" and "libwrapfoo.c", then
it works as expected.
 gcc -o test_oo_foo $(pkg-config --cflags --libs gobject-2.0) -I.
test_oo_foo.c libwrapfoo.c libfoo.o
Run the testing app with:
 ./test_oo_foo
 echo $?
 0

So, what do I have to do to avoid this editing of the generated files?



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]