From ab25fa42cc6906df31352877154bb7b07f39552f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 13 Dec 2019 20:07:51 +0100 Subject: [PATCH] gtype: Define auto-cleanup functions for Module class While we automatically define cleanup functions for the module, we don't do it for the module class. This will allow to manage the ownership of the class when reffing it without having to cast it to GTypeClass. --- gobject/gtype.h | 2 ++ gobject/tests/autoptr.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/gobject/gtype.h b/gobject/gtype.h index 797ab48ee..479b405e9 100644 --- a/gobject/gtype.h +++ b/gobject/gtype.h @@ -1406,6 +1406,7 @@ guint g_type_get_type_registration_serial (void); typedef struct { ParentName##Class parent_class; } ModuleObjName##Class; \ \ _GLIB_DEFINE_AUTOPTR_CHAINUP (ModuleObjName, ParentName) \ + G_DEFINE_AUTOPTR_CLEANUP_FUNC (ModuleObjName##Class, g_type_class_unref) \ \ G_GNUC_UNUSED static inline ModuleObjName * MODULE##_##OBJ_NAME (gpointer ptr) { \ return G_TYPE_CHECK_INSTANCE_CAST (ptr, module_obj_name##_get_type (), ModuleObjName); } \ @@ -1497,6 +1498,7 @@ guint g_type_get_type_registration_serial (void); struct _##ModuleObjName { ParentName parent_instance; }; \ \ _GLIB_DEFINE_AUTOPTR_CHAINUP (ModuleObjName, ParentName) \ + G_DEFINE_AUTOPTR_CLEANUP_FUNC (ModuleObjName##Class, g_type_class_unref) \ \ G_GNUC_UNUSED static inline ModuleObjName * MODULE##_##OBJ_NAME (gpointer ptr) { \ return G_TYPE_CHECK_INSTANCE_CAST (ptr, module_obj_name##_get_type (), ModuleObjName); } \ diff --git a/gobject/tests/autoptr.c b/gobject/tests/autoptr.c index 1f68cb3f9..19421c220 100644 --- a/gobject/tests/autoptr.c +++ b/gobject/tests/autoptr.c @@ -29,6 +29,18 @@ struct _TestAutoCleanupBaseClass { GObjectClass parent_class; }; +G_DEFINE_TYPE (TestAutoCleanupBase, test_base_auto_cleanup, G_TYPE_OBJECT) + +static void +test_base_auto_cleanup_class_init (TestAutoCleanupBaseClass *class) +{ +} + +static void +test_base_auto_cleanup_init (TestAutoCleanupBase *tac) +{ +} + G_DECLARE_FINAL_TYPE (TestAutoCleanup, test_auto_cleanup, TEST, AUTO_CLEANUP, TestAutoCleanupBase) struct _TestAutoCleanup @@ -195,6 +207,19 @@ test_autoqueue (void) g_assert_null (tac3); } +static void +test_autoclass (void) +{ + g_autoptr (TestAutoCleanupBaseClass) base_class_ptr = NULL; + g_autoptr (TestAutoCleanupClass) class_ptr = NULL; + + base_class_ptr = g_type_class_ref (test_base_auto_cleanup_get_type ()); + class_ptr = g_type_class_ref (test_auto_cleanup_get_type ()); + + g_assert_nonnull (base_class_ptr); + g_assert_nonnull (class_ptr); +} + int main (int argc, gchar *argv[]) { @@ -205,6 +230,7 @@ main (int argc, gchar *argv[]) g_test_add_func ("/autoptr/autolist", test_autolist); g_test_add_func ("/autoptr/autoslist", test_autoslist); g_test_add_func ("/autoptr/autoqueue", test_autoqueue); + g_test_add_func ("/autoptr/autoclass", test_autoclass); return g_test_run (); }