[libadwaita/msvc] adw-tab-view.c: Implement free-after-return on MSVC



commit e09f934c87b832c21541fc1c7d21ecc499a80f5e
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Wed Jan 19 10:58:09 2022 +0800

    adw-tab-view.c: Implement free-after-return on MSVC
    
    Define macros to help us to free up the AdwTabPage objects after we returned
    them to the callers, by doing the following:
    
    *  On CLang and GCC, use g_autoptr() as we did before.
    *  On Visual Studio, use the Microsoft-specific __try/__finally constructs
       to do the g_object_unref() after returning for us.
    
    Note that the Visual Studio-specific implementation is not enough to
    generically define the G_AUTO* et al macros, but should cover our use case
    here.

 src/adw-tab-view.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
---
diff --git a/src/adw-tab-view.c b/src/adw-tab-view.c
index 5f936d3a..84105d2e 100644
--- a/src/adw-tab-view.c
+++ b/src/adw-tab-view.c
@@ -14,6 +14,28 @@
 #include "adw-macros-private.h"
 #include "adw-widget-utils-private.h"
 
+/*
+ * Define for different compilers on how we deal with
+ * g_object_unref()'ing an AdwTabPage * after we return
+ * it after allocating it.  For GCC and CLang, use g_autoptr();
+ * for Visual Studio, use the MSVC-specific __try/__catch constructs.
+ */
+#if defined (__clang__) || defined (__GNUC__)
+# define ADW_TAB_PAGE_BEGIN_AUTOFREE(var) g_autoptr (AdwTabPage) var = NULL;
+# define ADW_TAB_PAGE_END_AUTOFREE(var)
+#elif defined (_MSC_VER)
+# define ADW_TAB_PAGE_BEGIN_AUTOFREE(var) \
+    AdwTabPage *var = NULL; \
+    __try {
+# define ADW_TAB_PAGE_END_AUTOFREE(var) \
+    } \
+    __finally { \
+      g_object_unref (var); \
+    }
+#else
+# error Define a way to do RAII for your C compiler
+#endif
+
 /* FIXME replace with groups */
 static GSList *tab_view_list;
 
@@ -958,7 +980,8 @@ create_and_insert_page (AdwTabView *self,
                         int         position,
                         gboolean    pinned)
 {
-  AdwTabPage *page =
+  ADW_TAB_PAGE_BEGIN_AUTOFREE (page)
+  page =
     g_object_new (ADW_TYPE_TAB_PAGE,
                   "child", child,
                   "parent", parent,
@@ -969,6 +992,7 @@ create_and_insert_page (AdwTabView *self,
   insert_page (self, page, position);
 
   return page;
+  ADW_TAB_PAGE_END_AUTOFREE (page)
 }
 
 static gboolean
@@ -2594,7 +2618,7 @@ AdwTabPage *
 adw_tab_view_get_nth_page (AdwTabView *self,
                            int         position)
 {
-  AdwTabPage *page = NULL;
+  ADW_TAB_PAGE_BEGIN_AUTOFREE (page)
 
   g_return_val_if_fail (ADW_IS_TAB_VIEW (self), NULL);
   g_return_val_if_fail (position >= 0, NULL);
@@ -2603,6 +2627,7 @@ adw_tab_view_get_nth_page (AdwTabView *self,
   page = g_list_model_get_item (G_LIST_MODEL (self->children), (guint) position);
 
   return page;
+  ADW_TAB_PAGE_END_AUTOFREE (page)
 }
 
 /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]