After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 502840 - derived type definition convenience macros
derived type definition convenience macros
Status: RESOLVED FIXED
Product: atk
Classification: Platform
Component: atk
unspecified
Other Linux
: Urgent enhancement
: ---
Assigned To: Li Yuan
Li Yuan
Depends on:
Blocks:
 
 
Reported: 2007-12-10 13:15 UTC by Christian Persch
Modified: 2008-11-07 07:13 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
proposed patch (5.11 KB, patch)
2007-12-10 13:16 UTC, Christian Persch
none Details | Review
new patch to add the marcos to atkutil.h (3.86 KB, patch)
2008-01-03 10:36 UTC, Li Yuan
committed Details | Review

Description Christian Persch 2007-12-10 13:15:44 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.
Comment 1 Christian Persch 2007-12-10 13:16:34 UTC
Created attachment 100687 [details] [review]
proposed patch
Comment 2 Li Yuan 2008-01-03 10:35:38 UTC
It's very useful, thanks!

I think we can just add it to atkutil.h.
Comment 3 Li Yuan 2008-01-03 10:36:25 UTC
Created attachment 102038 [details] [review]
new patch to add the marcos to atkutil.h
Comment 4 Li Yuan 2008-11-07 07:13:18 UTC
The patch has been committed on Jan 04.