jhass / crystal-gobject

gobject-introspection for Crystal

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

About fun _init_AnyClass = gtk_any_class_get_type

viachpaliy opened this issue · comments

I think, all this fun (for example, _init_AboutDialog) only return GType(a numerical value which represents the unique identifier of a registered type), not initialize any instance of class.Their uses in initialize methods make no sense.
It is only my opinion.

They are used to provide generic constructors using gobject_new_with_properties. See also https://developer.gnome.org/gobject/2.64/chapter-gobject.html#gobject-instantiation

Calling them makes sure the GObject type is actually initialized and registered into the type system, see g_registered_type_info_get_type_init.

I actually figured this out from crashes of not knowing you had to do this initially :)

So yeah, while you technically don't need to this for every call to initialize, only the first, I didn't figure out a better place to do this.

Let's try through a popular provided constructor method: gtk_window_new.

It calls gobject_new with GTK_TYPE_WINDOW which is defined as:

#define GTK_TYPE_WINDOW			(gtk_window_get_type ())

which is defined in this case by a call to G_DEFINE_TYPE_WITH_CODE

G_DEFINE_TYPE_WITH_CODE (GtkWindow, gtk_window, ...
#define G_DEFINE_TYPE_WITH_CODE(TN, t_n, T_P, _C_)	    _G_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, 0) {_C_;} _G_DEFINE_TYPE_EXTENDED_END()

yada yada yada

GType \
type_name##_get_type (void) \
{ \
  static volatile gsize g_define_type_id__volatile = 0;
  /* Prelude goes here */

/* Added for _G_DEFINE_TYPE_EXTENDED_WITH_PRELUDE */
#define _G_DEFINE_TYPE_EXTENDED_BEGIN_REGISTER(TypeName, type_name, TYPE_PARENT, flags) \
  if (g_once_init_enter (&g_define_type_id__volatile))  \
    { \
      GType g_define_type_id = type_name##_get_type_once (); \
      g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
    }					\
  return g_define_type_id__volatile;	\
} /* closes type_name##_get_type() */ \

And no wonder, gtk_window_get_type is exactly what's returned by g_registered_type_info_get_type_init for GtkWindow.

Alright, the above made me realize we can actually rely on that function to return the gtype, so I pushed a commit that makes use of that.

Thank you for answer!