g_signal_connect() and G_OBJECT

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

g_signal_connect() and G_OBJECT

Ingo Brückl
Hi,

it seems that it was common practice to cast the first argument of
g_signal_connect() to G_OBJECT when I started developing applications with
GTK+ quite a while ago. At least I've learned it that way and am doing it
ever since.

I repeatedly see usage of g_signal_connect() without that cast which seems
to make sense since "instance" only has to be a gpointer and will be checked
G_TYPE_CHECK_INSTANCE at run time.

So I'm just curious. Is/was there a reason for the G_OBJECT cast? Was it
necessary back in the days (when GTK+ 2 took over from GTK+ 1)?

Ingo
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: g_signal_connect() and G_OBJECT

Emmanuele Bassi
The first argument of `g_signal_connect()` is a gpointer (i.e. a
`void*`), so you don't need a cast at all — C will implicitly cast any
pointer to `void*`, and from `void*` to any other pointer.

Additionally, as you discovered, signals are GTypeInstance features —
you can emit signals on anything that inherits from GTypeInstance, not
just GObjects.

The reason why you see a cast macro is a layer of "extra security":
you can check that you're passing the object you meant to be passing,
instead of a NULL pointer or some garbage. This "extra security" is
mostly cargo-culted through tutorials, so people learn the habit and
transmit it.

Personally, I find it pointless; internal state should be checked with
`g_assert()`, and all cast macros can be compiled away with
`G_DISABLE_CAST_CHECKS`, so that buys you almost nothing.

Ciao,
 Emmanuele.

On 30 June 2017 at 11:20, Ingo Brückl <[hidden email]> wrote:

> Hi,
>
> it seems that it was common practice to cast the first argument of
> g_signal_connect() to G_OBJECT when I started developing applications with
> GTK+ quite a while ago. At least I've learned it that way and am doing it
> ever since.
>
> I repeatedly see usage of g_signal_connect() without that cast which seems
> to make sense since "instance" only has to be a gpointer and will be checked
> G_TYPE_CHECK_INSTANCE at run time.
>
> So I'm just curious. Is/was there a reason for the G_OBJECT cast? Was it
> necessary back in the days (when GTK+ 2 took over from GTK+ 1)?
>
> Ingo
> _______________________________________________
> gtk-app-devel-list mailing list
> [hidden email]
> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



--
https://www.bassi.io
[@] ebassi [@gmail.com]
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Loading...