GTK + style style for function arguments

In GTK + calls, the arguments must (but not necessarily) be cast from GtkWidget to the most specific class required by the function before passing the argument. For example, sometimes I see

 some_call(GTK_WINDOW(window)); 

another time I see

 some_call((GtkWindow *) window); 

What's the difference?

+4
source share
3 answers

The first is a macro that can check if a cast is possible and does a cast. This is a GTK version / attempt of a safe type. You must use this.

+2
source

GTK_WINDOW is a macro that performs casting.

As seen here

 #define GTK_WINDOW(obj) (GTK_CHECK_CAST ((obj), GTK_TYPE_WINDOW, GtkWindow)) 

Again

 #define GTK_CHECK_CAST G_TYPE_CHECK_INSTANCE_CAST 

and

 #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type) (_G_TYPE_CIC ((instance), (g_type), c_type)) 

which is ...

 #define _G_TYPE_CIC(ip,gt,ct) \ ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt)) 

The code g_type_check_instance_cast can be found here

 GTypeInstance* g_type_check_instance_cast (GTypeInstance *type_instance, GType iface_type) { if (type_instance) { if (type_instance->g_class) { TypeNode *node, *iface; gboolean is_instantiatable, check; node = lookup_type_node_I (type_instance->g_class->g_type); is_instantiatable = node && node->is_instantiatable; iface = lookup_type_node_I (iface_type); check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE); if (check) return type_instance; if (is_instantiatable) g_warning ("invalid cast from `%s' to `%s'", type_descriptive_name_I (type_instance->g_class->g_type), type_descriptive_name_I (iface_type)); else g_warning ("invalid uninstantiatable type `%s' in cast to `%s'", type_descriptive_name_I (type_instance->g_class->g_type), type_descriptive_name_I (iface_type)); } else g_warning ("invalid unclassed pointer in cast to `%s'", type_descriptive_name_I (iface_type)); } return type_instance; } 
+7
source

GTK_WINDOW () is just a macro that will look something like this:

 #define GTK_WINDOW(a) ((GtkWindow*)a) 

This is identical to doing the explicit cast yourself, and the two statements you have in your message are identical.

-3
source

All Articles