[libadwaita/msvc: 1/2] adw-tab-view.c: Implement free-after-return on MSVC
- From: Chun-wei Fan <fanchunwei src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libadwaita/msvc: 1/2] adw-tab-view.c: Implement free-after-return on MSVC
- Date: Wed, 19 Jan 2022 03:30:31 +0000 (UTC)
commit f4a41011a27acedcaf52b18d637b392623d5df3b
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 | 28 ++++++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
---
diff --git a/src/adw-tab-view.c b/src/adw-tab-view.c
index 5f936d3a..1da9356a 100644
--- a/src/adw-tab-view.c
+++ b/src/adw-tab-view.c
@@ -14,6 +14,27 @@
#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/__finally 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 +979,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 +991,7 @@ create_and_insert_page (AdwTabView *self,
insert_page (self, page, position);
return page;
+ ADW_TAB_PAGE_END_AUTOFREE (page)
}
static gboolean
@@ -2594,7 +2617,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 +2626,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]