GNOME Bugzilla – Bug 502840
derived type definition convenience macros
Last modified: 2008-11-07 07:13:18 UTC
Let's take a look at a typical foo_accessible_get_type() function as found in many GNOME modules, e.g. gtk+, evolution, libgail-gnome, gucharmap, control-centre, libgnomeui, gnome-media, eel, goocanvas, evince, ...: static GType gtk_icon_view_accessible_get_type (void) { static GType type = 0; if (!type) { static GTypeInfo tinfo = { 0, /* class size */ (GBaseInitFunc) NULL, /* base init */ (GBaseFinalizeFunc) NULL, /* base finalize */ (GClassInitFunc) gtk_icon_view_accessible_class_init, (GClassFinalizeFunc) NULL, /* class finalize */ NULL, /* class data */ 0, /* instance size */ 0, /* nb preallocs */ (GInstanceInitFunc) NULL, /* instance init */ NULL /* value table */ }; static const GInterfaceInfo atk_component_info = { (GInterfaceInitFunc) atk_component_interface_init, (GInterfaceFinalizeFunc) NULL, NULL }; static const GInterfaceInfo atk_selection_info = { (GInterfaceInitFunc) gtk_icon_view_accessible_selection_interface_init, (GInterfaceFinalizeFunc) NULL, NULL }; /* * Figure out the size of the class and instance * we are deriving from */ AtkObjectFactory *factory; GType derived_type; GTypeQuery query; GType derived_atk_type; derived_type = g_type_parent (GTK_TYPE_ICON_VIEW); factory = atk_registry_get_factory (atk_get_default_registry (), derived_type); derived_atk_type = atk_object_factory_get_accessible_type (factory); g_type_query (derived_atk_type, &query); tinfo.class_size = query.class_size; tinfo.instance_size = query.instance_size; type = g_type_register_static (derived_atk_type, I_("GtkIconViewAccessible"), &tinfo, 0); g_type_add_interface_static (type, ATK_TYPE_COMPONENT, &atk_component_info); g_type_add_interface_static (type, ATK_TYPE_SELECTION, &atk_selection_info); } return type; } There are two problems with it: - it is not thread-safe (see bug 65041) - it introduces unnecessary relocations, due to the use of the static GTypeInfo and GInterfaceInfo structs I therefore propose to introduce some convenience macros in the spirit of G_DEFINE_TYPE[_WITH_CODE], that will enable one to rewrite the above function to ATK_DEFINE_TYPE_WITH_CODE (GtkIconViewAccesssible, gtk_icon_view_accessible, GTK_TYPE_ICON_VIEW, G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init) G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, gtk_icon_view_accessible_selection_interface_init)) and so get a thread-safe, relocation-less get_type function.
Created attachment 100687 [details] [review] proposed patch
It's very useful, thanks! I think we can just add it to atkutil.h.
Created attachment 102038 [details] [review] new patch to add the marcos to atkutil.h
The patch has been committed on Jan 04.