From 68b689c95c8aaba5a05366450d9c224fb949a897 Mon Sep 17 00:00:00 2001 From: Richard Cohen Date: Tue, 6 Jun 2023 20:43:38 +0100 Subject: [PATCH] Refactor: remove GNC_DEFINE_TYPE, GNC_IMPLEMENT_INTERFACE Use GObject constructed() instead --- gnucash/gnome-utils/gnc-embedded-window.c | 32 +++++++---- gnucash/gnome-utils/gnc-gobject-utils.c | 19 ++---- gnucash/gnome-utils/gnc-gobject-utils.h | 70 +---------------------- gnucash/gnome-utils/gnc-main-window.cpp | 34 ++++++----- gnucash/gnome-utils/gnc-plugin-page.c | 35 +++++++----- gnucash/gnome-utils/gnc-plugin.c | 28 ++++++--- gnucash/gnome-utils/gnc-tree-model.c | 25 ++++++-- gnucash/gnome-utils/gnc-tree-view.c | 25 +++++--- 8 files changed, 124 insertions(+), 144 deletions(-) diff --git a/gnucash/gnome-utils/gnc-embedded-window.c b/gnucash/gnome-utils/gnc-embedded-window.c index a6588f3a44f..eb2f6579669 100644 --- a/gnucash/gnome-utils/gnc-embedded-window.c +++ b/gnucash/gnome-utils/gnc-embedded-window.c @@ -55,6 +55,7 @@ static QofLogModule log_module = GNC_MOD_GUI; /* Declarations *********************************************************/ +static void gnc_embedded_window_constructed (GObject *object); static void gnc_embedded_window_finalize (GObject *object); static void gnc_embedded_window_dispose (GObject *object); @@ -98,10 +99,10 @@ typedef struct GncEmbeddedWindowPrivate GtkWidget *parent_window; } GncEmbeddedWindowPrivate; -GNC_DEFINE_TYPE_WITH_CODE(GncEmbeddedWindow, gnc_embedded_window, GTK_TYPE_BOX, +G_DEFINE_TYPE_WITH_CODE(GncEmbeddedWindow, gnc_embedded_window, GTK_TYPE_BOX, G_ADD_PRIVATE(GncEmbeddedWindow) - GNC_IMPLEMENT_INTERFACE(GNC_TYPE_WINDOW, - gnc_window_embedded_window_init)) + G_IMPLEMENT_INTERFACE(GNC_TYPE_WINDOW, + gnc_window_embedded_window_init)) #define GNC_EMBEDDED_WINDOW_GET_PRIVATE(o) \ ((GncEmbeddedWindowPrivate*)gnc_embedded_window_get_instance_private((GncEmbeddedWindow*)o)) @@ -187,6 +188,7 @@ gnc_embedded_window_class_init (GncEmbeddedWindowClass *klass) ENTER("klass %p", klass); object_class = G_OBJECT_CLASS (klass); + object_class->constructed = gnc_embedded_window_constructed; object_class->finalize = gnc_embedded_window_finalize; object_class->dispose = gnc_embedded_window_dispose; @@ -215,17 +217,14 @@ gnc_embedded_window_class_init (GncEmbeddedWindowClass *klass) /** Initialize a new instance of a gnucash embedded window. This - * function initializes the object private storage space. It also - * adds the new object to a list (for memory tracking purposes). + * function initializes the object private storage space. * * @param view The new object instance created by the object system. * - * @param klass A pointer to the class data structure for this - * object. */ + * */ static void -gnc_embedded_window_init (GncEmbeddedWindow *window, void *data) +gnc_embedded_window_init (GncEmbeddedWindow *window) { - GncEmbeddedWindowClass *klass = (GncEmbeddedWindowClass*)data; ENTER("window %p", window); gtk_orientable_set_orientation (GTK_ORIENTABLE(window), GTK_ORIENTATION_VERTICAL); @@ -235,11 +234,22 @@ gnc_embedded_window_init (GncEmbeddedWindow *window, void *data) gnc_embedded_window_setup_window (window); - gnc_gobject_tracking_remember (G_OBJECT(window), - G_OBJECT_CLASS(klass)); LEAVE(" "); } +/** The object has been fully constructed. + * This function adds the object to the tracking system. + * + * @param obj The new object instance created by the object + * system. + */ + static void +gnc_embedded_window_constructed (GObject *obj) +{ + gnc_gobject_tracking_remember(obj); + + G_OBJECT_CLASS (gnc_embedded_window_parent_class)->constructed (obj); +} /** Finish destruction of an embedded window. * diff --git a/gnucash/gnome-utils/gnc-gobject-utils.c b/gnucash/gnome-utils/gnc-gobject-utils.c index 8399e629727..fe43f036a61 100644 --- a/gnucash/gnome-utils/gnc-gobject-utils.c +++ b/gnucash/gnome-utils/gnc-gobject-utils.c @@ -120,25 +120,16 @@ gnc_gobject_tracking_dump (void) /** Tell gnucash to remember this object in the database. */ void -gnc_gobject_tracking_remember (GObject *object, GObjectClass *klass) +gnc_gobject_tracking_remember (GObject *object) { - GHashTable *table; - GList *list; - const gchar *name; - g_return_if_fail(G_IS_OBJECT(object)); - /* Little dance here to handle startup conditions. During object - * initialization the object type changes as each parent class - * is initialized. The class passed to the initialization function - * is always the ultimate class of the object. */ - if (klass == NULL) - klass = G_OBJECT_GET_CLASS(object); - name = g_type_name(G_TYPE_FROM_CLASS(klass)); + GObjectClass *klass = G_OBJECT_GET_CLASS(object); + const gchar *name = g_type_name(G_TYPE_FROM_CLASS(klass)); //printf("Enter %s: object %p of type %s\n", G_STRFUNC, object, name); - table = gnc_gobject_tracking_table(); - list = g_hash_table_lookup(table, name); + GHashTable *table = gnc_gobject_tracking_table(); + GList *list = g_hash_table_lookup(table, name); if (g_list_index(list, object) != -1) { diff --git a/gnucash/gnome-utils/gnc-gobject-utils.h b/gnucash/gnome-utils/gnc-gobject-utils.h index 3d066e45cda..93db5a6e306 100644 --- a/gnucash/gnome-utils/gnc-gobject-utils.h +++ b/gnucash/gnome-utils/gnc-gobject-utils.h @@ -71,19 +71,9 @@ extern "C" { /** Tell gnucash to remember this object in the database. * - * @param object The object to be tracked. This can be a fully or - * partially instantiated object. - * - * @param klass The class structure for the object. This argument - * may be NULL if a fully instantiated object is passed in as the - * first argument. If a partially instantiated object is provided - * (I.E. a parent class called this function) then this argument is - * required. This is necessary because the class of the object - * changes as each of the parent class is instantiated. The class - * structure, however, status constant and always reflects the fully - * instantiated object. + * @param object The fully constructed object to be tracked. */ -void gnc_gobject_tracking_remember (GObject *object, GObjectClass *klass); +void gnc_gobject_tracking_remember (GObject *object); /** Tell gnucash to drop this object from the database. * @@ -113,62 +103,6 @@ void gnc_gobject_tracking_dump (void); /** @} */ -/** Some macros derived from glib type macros. - * In glib type_name##init function only has one parameter. We need - * the 2nd class parameter in certain calls. The main difference is - * static void type_name##_init (TypeName *self, void *class); - * instead of - * static void type_name##_init (TypeName *self); - * this code may need updating in future releases as glib changes. - **/ -#define GNC_IMPLEMENT_INTERFACE(TYPE_IFACE, iface_init) { \ - const GInterfaceInfo g_implement_interface_info = { \ - (GInterfaceInitFunc)(void (*)(void *, void *)) iface_init, NULL, NULL \ - }; \ - g_type_add_interface_static (g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \ -} - -#define GNC_DEFINE_TYPE_WITH_CODE(TN, t_n, T_P, _C_) _GNC_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, 0) {_C_;} _GNC_DEFINE_TYPE_EXTENDED_END() - -#define _GNC_DEFINE_TYPE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PARENT, flags) \ -\ -static void type_name##_init (TypeName *self, void *klass); \ -static void type_name##_class_init (TypeName##Class *klass); \ -static gpointer type_name##_parent_class = NULL; \ -static gint TypeName##_private_offset; \ -\ -_G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \ -\ -G_GNUC_UNUSED \ -static inline gpointer \ -type_name##_get_instance_private (TypeName *self) \ -{ \ - return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \ -} \ -\ -GType \ -type_name##_get_type (void) \ -{ \ - static gsize g_define_type_id_static = 0; \ - if (g_once_init_enter (&g_define_type_id_static)) \ - { \ - GType g_define_type_id = \ - g_type_register_static_simple (TYPE_PARENT, \ - g_intern_static_string (#TypeName), \ - sizeof (TypeName##Class), \ - (GClassInitFunc) type_name##_class_intern_init, \ - sizeof (TypeName), \ - (GInstanceInitFunc) type_name##_init, \ - (GTypeFlags) flags); \ - { /* custom code follows */ -#define _GNC_DEFINE_TYPE_EXTENDED_END() \ - /* following custom code */ \ - } \ - g_once_init_leave (&g_define_type_id_static, g_define_type_id); \ - } \ - return g_define_type_id_static; \ -} /* closes type_name##_get_type() */ - #ifdef __cplusplus } #endif diff --git a/gnucash/gnome-utils/gnc-main-window.cpp b/gnucash/gnome-utils/gnc-main-window.cpp index 51540ea4243..249c8d414ca 100644 --- a/gnucash/gnome-utils/gnc-main-window.cpp +++ b/gnucash/gnome-utils/gnc-main-window.cpp @@ -150,6 +150,7 @@ static guint secs_to_save = 0; #define MSG_AUTO_SAVE _("Changes will be saved automatically in %u seconds") /* Declarations *********************************************************/ +static void gnc_main_window_constructed (GObject *object); static void gnc_main_window_finalize (GObject *object); static void gnc_main_window_destroy (GtkWidget *widget); @@ -259,7 +260,7 @@ typedef struct GncMainWindowPrivate } GncMainWindowPrivate; -GNC_DEFINE_TYPE_WITH_CODE(GncMainWindow, gnc_main_window, GTK_TYPE_APPLICATION_WINDOW, +G_DEFINE_TYPE_WITH_CODE(GncMainWindow, gnc_main_window, GTK_TYPE_APPLICATION_WINDOW, G_ADD_PRIVATE (GncMainWindow) G_IMPLEMENT_INTERFACE (GNC_TYPE_WINDOW, gnc_window_main_window_init)) @@ -2622,6 +2623,7 @@ gnc_main_window_class_init (GncMainWindowClass *klass) window_type = g_quark_from_static_string ("gnc-main-window"); + object_class->constructed = gnc_main_window_constructed; object_class->finalize = gnc_main_window_finalize; /* GtkWidget signals */ @@ -2704,21 +2706,14 @@ gnc_main_window_class_init (GncMainWindowClass *klass) /** Initialize a new instance of a gnucash main window. This function - * initializes the object private storage space. It also adds the - * new object to a list (for memory tracking purposes). + * initializes the object private storage space. * * @param window The new object instance created by the object system. - * - * @param klass A pointer to the class data structure for this - * object. */ + * */ static void -gnc_main_window_init (GncMainWindow *window, void *data) +gnc_main_window_init (GncMainWindow *window) { - GncMainWindowPrivate *priv; - - GncMainWindowClass *klass = (GncMainWindowClass*)data; - - priv = GNC_MAIN_WINDOW_GET_PRIVATE(window); + GncMainWindowPrivate *priv = GNC_MAIN_WINDOW_GET_PRIVATE(window); // Set the name for this dialog so it can be easily manipulated with css gtk_widget_set_name (GTK_WIDGET(window), "gnc-id-main-window"); @@ -2745,10 +2740,21 @@ gnc_main_window_init (GncMainWindow *window, void *data) window); gnc_main_window_setup_window (window); - gnc_gobject_tracking_remember(G_OBJECT(window), - G_OBJECT_CLASS(klass)); } +/** The object has been fully constructed. + * This function adds the object to the tracking system. + * + * @param obj The new object instance created by the object + * system. + */ +static void +gnc_main_window_constructed (GObject *obj) +{ + gnc_gobject_tracking_remember(obj); + + G_OBJECT_CLASS (gnc_main_window_parent_class)->constructed (obj); +} /** Finalize the GncMainWindow object. This function is called from * the G_Object level to complete the destruction of the object. It diff --git a/gnucash/gnome-utils/gnc-plugin-page.c b/gnucash/gnome-utils/gnc-plugin-page.c index 623b4402050..61a22f5df06 100644 --- a/gnucash/gnome-utils/gnc-plugin-page.c +++ b/gnucash/gnome-utils/gnc-plugin-page.c @@ -43,6 +43,7 @@ /** The debugging module that this .o belongs to. */ static QofLogModule log_module = GNC_MOD_GUI; +static void gnc_plugin_page_constructed (GObject *object); static void gnc_plugin_page_finalize (GObject *object); static void gnc_plugin_page_set_property (GObject *object, guint prop_id, @@ -106,7 +107,7 @@ typedef struct _GncPluginPagePrivate } GncPluginPagePrivate; -GNC_DEFINE_TYPE_WITH_CODE(GncPluginPage, gnc_plugin_page, G_TYPE_OBJECT, +G_DEFINE_TYPE_WITH_CODE(GncPluginPage, gnc_plugin_page, G_TYPE_OBJECT, G_ADD_PRIVATE(GncPluginPage)) #define GNC_PLUGIN_PAGE_GET_PRIVATE(o) \ @@ -351,6 +352,7 @@ gnc_plugin_page_class_init (GncPluginPageClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + gobject_class->constructed = gnc_plugin_page_constructed; gobject_class->finalize = gnc_plugin_page_finalize; gobject_class->set_property = gnc_plugin_page_set_property; gobject_class->get_property = gnc_plugin_page_get_property; @@ -451,21 +453,14 @@ gnc_plugin_page_class_init (GncPluginPageClass *klass) /** Initialize a new instance of a gnucash content plugin. This - * function initializes the object private storage space, and adds - * the object to the tracking system. + * function initializes the object private storage space. * * @param page The new object instance created by the object system. - * - * @param klass A pointer to the class data structure for this - * object. */ + * */ static void -gnc_plugin_page_init (GncPluginPage *page, void *data) +gnc_plugin_page_init (GncPluginPage *page) { - GncPluginPagePrivate *priv; - - GncPluginPageClass *klass = (GncPluginPageClass*)data; - - priv = GNC_PLUGIN_PAGE_GET_PRIVATE(page); + GncPluginPagePrivate *priv = GNC_PLUGIN_PAGE_GET_PRIVATE(page); priv->page_name = NULL; priv->page_color = NULL; priv->page_changed_id = 0; @@ -474,11 +469,21 @@ gnc_plugin_page_init (GncPluginPage *page, void *data) page->window = NULL; page->summarybar = NULL; - - gnc_gobject_tracking_remember (G_OBJECT(page), - G_OBJECT_CLASS(klass)); } +/** The object has been fully constructed. + * This function adds the object to the tracking system. + * + * @param obj The new object instance created by the object + * system. + */ + static void +gnc_plugin_page_constructed (GObject *obj) +{ + gnc_gobject_tracking_remember(obj); + + G_OBJECT_CLASS (gnc_plugin_page_parent_class)->constructed (obj); +} /** Finalize the gnucash plugin object. This function is called from * the G_Object level to complete the destruction of the object. It diff --git a/gnucash/gnome-utils/gnc-plugin.c b/gnucash/gnome-utils/gnc-plugin.c index 521f42fce4b..1dd26f14542 100644 --- a/gnucash/gnome-utils/gnc-plugin.c +++ b/gnucash/gnome-utils/gnc-plugin.c @@ -47,6 +47,7 @@ /** The debugging module that this .o belongs to. */ static QofLogModule log_module = GNC_MOD_GUI; +static void gnc_plugin_constructed (GObject *object); static void gnc_plugin_finalize (GObject *object); @@ -57,8 +58,8 @@ typedef struct GncPluginPrivate gpointer dummy; } GncPluginPrivate; -GNC_DEFINE_TYPE_WITH_CODE(GncPlugin, gnc_plugin, G_TYPE_OBJECT, - G_ADD_PRIVATE(GncPlugin)) +G_DEFINE_TYPE_WITH_CODE(GncPlugin, gnc_plugin, G_TYPE_OBJECT, + G_ADD_PRIVATE(GncPlugin)) #define GNC_PLUGIN_GET_PRIVATE(o) \ ((GncPluginPrivate*)gnc_plugin_get_instance_private((GncPlugin*)o)) @@ -75,6 +76,7 @@ gnc_plugin_class_init (GncPluginClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + gobject_class->constructed = gnc_plugin_constructed; gobject_class->finalize = gnc_plugin_finalize; } @@ -82,18 +84,26 @@ gnc_plugin_class_init (GncPluginClass *klass) /** Initialize a new instance of a gnucash menu-only plugin. This * function adds the object to the tracking system. * - * @param plugin_page The new object instance created by the object + * @param plugin The new object instance created by the object * system. + */ +static void +gnc_plugin_init (GncPlugin *plugin) +{ +} + +/** The object has been fully constructed. + * This function adds the object to the tracking system. * - * @param klass A pointer to the class data structure for this - * object. */ + * @param obj The new object instance created by the object + * system. + */ static void -gnc_plugin_init (GncPlugin *plugin_page, void *data) +gnc_plugin_constructed (GObject *obj) { - GncPluginClass *klass = (GncPluginClass*)data; + gnc_gobject_tracking_remember(obj); - gnc_gobject_tracking_remember(G_OBJECT(plugin_page), \ - G_OBJECT_CLASS(klass)); + G_OBJECT_CLASS (gnc_plugin_parent_class)->constructed (obj); } diff --git a/gnucash/gnome-utils/gnc-tree-model.c b/gnucash/gnome-utils/gnc-tree-model.c index a990469bc96..0a7a21273d1 100644 --- a/gnucash/gnome-utils/gnc-tree-model.c +++ b/gnucash/gnome-utils/gnc-tree-model.c @@ -36,6 +36,7 @@ static QofLogModule log_module = GNC_MOD_GUI; /** Declarations *********************************************************/ +static void gnc_tree_model_constructed (GObject *object); static void gnc_tree_model_finalize (GObject *object); /** The instance private data for a generic tree model. */ @@ -44,7 +45,7 @@ typedef struct GncTreeModelPrivate gpointer dummy; } GncTreeModelPrivate; -GNC_DEFINE_TYPE_WITH_CODE(GncTreeModel, gnc_tree_model, G_TYPE_OBJECT, +G_DEFINE_TYPE_WITH_CODE(GncTreeModel, gnc_tree_model, G_TYPE_OBJECT, G_ADD_PRIVATE(GncTreeModel)) #define GNC_TREE_MODEL_GET_PRIVATE(o) \ @@ -63,17 +64,29 @@ gnc_tree_model_class_init (GncTreeModelClass *klass) o_class = G_OBJECT_CLASS (klass); /* GObject signals */ + o_class->constructed = gnc_tree_model_constructed; o_class->finalize = gnc_tree_model_finalize; } static void -gnc_tree_model_init (GncTreeModel *model, void *data) +gnc_tree_model_init (GncTreeModel *model) { - GncTreeModelClass *klass = (GncTreeModelClass*)data; +} + +/** The object has been fully constructed. + * This function adds the object to the tracking system. + * + * @param obj The new object instance created by the object + * system. + */ +static void +gnc_tree_model_constructed (GObject *obj) +{ + ENTER("model %p", obj); + + gnc_gobject_tracking_remember(obj); - ENTER("model %p", model); - gnc_gobject_tracking_remember(G_OBJECT(model), - G_OBJECT_CLASS(klass)); + G_OBJECT_CLASS (gnc_tree_model_parent_class)->constructed (obj); LEAVE(" "); } diff --git a/gnucash/gnome-utils/gnc-tree-view.c b/gnucash/gnome-utils/gnc-tree-view.c index be9d4d11390..d9e6b3d6e57 100644 --- a/gnucash/gnome-utils/gnc-tree-view.c +++ b/gnucash/gnome-utils/gnc-tree-view.c @@ -77,6 +77,7 @@ enum static QofLogModule log_module = GNC_MOD_GUI; /**** Declarations ******************************************************/ +static void gnc_tree_view_constructed (GObject *object); static void gnc_tree_view_finalize (GObject *object); static void gnc_tree_view_destroy (GtkWidget *widget); static void gnc_tree_view_set_property (GObject *object, @@ -126,7 +127,7 @@ typedef struct GncTreeViewPrivate gulong size_allocate_cb_id; } GncTreeViewPrivate; -GNC_DEFINE_TYPE_WITH_CODE(GncTreeView, gnc_tree_view, GTK_TYPE_TREE_VIEW, +G_DEFINE_TYPE_WITH_CODE(GncTreeView, gnc_tree_view, GTK_TYPE_TREE_VIEW, G_ADD_PRIVATE(GncTreeView)) #define GNC_TREE_VIEW_GET_PRIVATE(o) \ @@ -177,6 +178,7 @@ gnc_tree_view_class_init (GncTreeViewClass *klass) G_PARAM_READWRITE)); /* GObject signals */ + gobject_class->constructed = gnc_tree_view_constructed; gobject_class->finalize = gnc_tree_view_finalize; /* GtkWidget signals */ @@ -230,17 +232,12 @@ gnc_tree_view_select_column_icon_cb (GtkWidget *widget, GdkEventButton *event, g * @internal */ static void -gnc_tree_view_init (GncTreeView *view, void *data) +gnc_tree_view_init (GncTreeView *view) { GncTreeViewPrivate *priv; GtkTreeViewColumn *column; GtkWidget *sep, *icon; - GncTreeViewClass *klass = (GncTreeViewClass*)data; - - gnc_gobject_tracking_remember (G_OBJECT(view), - G_OBJECT_CLASS(klass)); - priv = GNC_TREE_VIEW_GET_PRIVATE(view); priv->column_menu = NULL; priv->show_column_menu = FALSE; @@ -322,6 +319,20 @@ gnc_tree_view_init (GncTreeView *view, void *data) gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED); } +/** The object has been fully constructed. + * This function adds the object to the tracking system. + * + * @param obj The new object instance created by the object + * system. + */ + static void +gnc_tree_view_constructed (GObject *obj) +{ + gnc_gobject_tracking_remember(obj); + + G_OBJECT_CLASS (gnc_tree_view_parent_class)->constructed (obj); +} + /** Finalize the GncTreeView object. This function is called from the * G_Object level to complete the destruction of the object. It * should release any memory not previously released by the destroy