signal's class handlers



Hi all,

Currently, all the code I looked at in both Gnome and GTK+ applications,
uses a class handler (a class function stored in the class structure)
for their signal's default handlers rather than using a simple closure
(with g_signal_newv) as shown below:

        GClosure *default_closure;
        GType param_types[1];
                                                                                                                                 
        default_closure = g_cclosure_new (G_CALLBACK (default_signal_handler),
                                          (gpointer)0xdeadbeaf /* user_data */,
                                          NULL /* destroy_data */);
                                                                                                                                 
        param_types[0] = G_TYPE_BOOLEAN;
        klass->action_signal_id =
                g_signal_newv ("do-action",
                               G_TYPE_FROM_CLASS (g_class),
                               G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
                               default_closure,
                               NULL /* accumulator */,
                               NULL /* accu_data */,
                               maman_foo_UINT__BOOLEAN,
                               G_TYPE_UINT /* return_type */,
                               1     /* n_params */,
                               param_types  /* param_types */);
                                                                                                                                 
        g_closure_unref (default_closure);


While I understand why people did not do this in the GTK 1.x era (there
was no other type of default signal handler), I wonder why people keep
on doing so: is it simply out of habbit and copy/pasting code from GTK+
?

Clearly, it would be much nicer not to export class functions for this
(I must confess I take the view that the less users know about a class,
the better). I am sure some will object to the code shown above because
it is much less simpler to write (you need to create a CClosure by hand
which is never a pleasant experience) but I wonder if there are really
other reasons not to use that code other than the 'habbit' part and the
lack of a trivial wrapper function.

Also, I'd like to point out that the class function approach is probably
slower than the default closure approach since class closures need an
extra de-referencing step before function invocation (you need to read
the class function pointer from the class structure).

I'd love to hear other explanations and comments.

Mathieu
-- 
Mathieu Lacage <mathieu gnu org>




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