Glib closures

potyl on 2009-05-18T20:31:40

Libchamplain had a major release a few weeks ago and it introduced a MapSourceFactory which allows map sources to be registered, instantiated and easily swapped. This is a great addition as all map sources are now created in the same way. Displaying a combo box with all maps available is truly simple now.

The factory also allows user code to provide their own map source and to register it with libchamplain. Which means that once the factory will be properly binded in Perl it would be possible to implement a MapSource in pure Perl!

I was anxious to get this new feature but it came at a price: the bindings have to support the passing of a Perl sub (CODEREF) and to execute it when a map source is implemented in Perl. While, Glib and Gtk2 are overloaded with CODEREFS (after all the signal systems is implemented through callbacks), I had no idea how to implement this. To make things worse the callback for creating a new MapSource had no arguments which made the task impossible.

I finally had the time to refactor libchamplain and to modify the C callback signature in order to accept an extra parameter. Exactly as all Glib and Gtk2 callbacks do. This made it possible for me to reuse Glib's mechanism for binding callbacks. Thus, now it possible to create a MapSource in Perl!

This was the last bit that was missing in the Perl bindings. Now Champlain.pm has a total coverage of all functions and variables defined in libchamplain. The code needs still to be cleaned up a little bit and some pieces need to be refactored. But at least and at last I have a working version.