[gtkada] Gtk & Genericity

Emmanuel Briot briot at gnat.com
Wed Aug 16 11:54:44 CEST 2000


Frederic Gobry writes:
 > For teaching purposes, I try to build some generic packages for
 > manipulating generic data structures from a Gtk interface. My idea is
 > to have a generic package, let's call it Relations, which has a child
 > unit Relations.Edition containing primitives for UI manipulation. I
 > only want to provide simple high level functions in it, like Display
 > and Edit, and hide all the internal methods needed for Gtk (esp.
 > callbacks).
 > 
 > But I can't compile the enclosed code, due to genericity problems. Is
 > there a workaround for that ?
 > 
 > > gnatmake test_gen.adb `gtkada-config`
 > gcc -c -I/opt/gnat/include/gtkada test_gen.adb
 > simple_gen.adb:12:04: instantiation error at gtk-marshallers.ads:359
 > simple_gen.adb:12:04: instantiation error at gtk-handlers.ads:437
 > simple_gen.adb:12:04: parent type must not be outside generic body


As explained in the GtkAda documentation, you should not instanciate callback
packages outside of the library level (which is not the case in your
application since you have a generic package). The reason is that this
callbacks reference ('Access or 'Address) some internal functions, that needs
to be callable from C, and if you don't instantiate these packages from
library-level then the code might no longer exists when C needs to call it.

A workaround should be to instanciate Button_Callback in a separate unit, for
instance
    package Button_Callback is new
       Gtk.Handlers.Callback
       (Widget_Type => Gtk_Button_Record);
and in simple_gen.adb, add a "with Button_Callback";
This will take care of the first two instantiation errors.

Or you can create a common package with all the callbacks instanciations,
which might save space since GNAT doesn't have shared generics.


As for the last line, this is because a 'Access to a subprogram internal to
the generic package is referenced from a library-level generic package, and
this is not allowed.
A possible workaround (maybe not the cleanest one), is to use
'Unrestricted_Access instead of 'Access.


Emmanuel





More information about the gtkada mailing list