[gjs: 14/18] jsapi-util: Add SmartPointer for G(S)List and use it




commit 91c18394c23a6dc4bee5595e4172f0befcafa002
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Oct 14 14:09:38 2020 +0200

    jsapi-util: Add SmartPointer for G(S)List and use it
    
    It can make some code easier to read as well.

 gi/arg.cpp       |  7 +------
 gjs/jsapi-util.h | 10 ++++++++++
 gjs/stack.cpp    |  4 ++--
 3 files changed, 13 insertions(+), 8 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index fb4d77df..487c6352 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -2052,12 +2052,7 @@ GJS_JSAPI_RETURN_CONVENTION static bool gjs_g_arg_release_g_list(
     JSContext* cx, GITransfer transfer, GITypeInfo* type_info,
     GIArgument* arg) {
     static_assert(std::is_same_v<T, GList> || std::is_same_v<T, GSList>);
-    using GjsAutoList =
-        std::conditional_t<std::is_same_v<T, GList>,
-                           GjsAutoPointer<GList, GList, g_list_free>,
-                           GjsAutoPointer<GSList, GSList, g_slist_free>>;
-
-    GjsAutoList list = gjs_arg_steal<T*>(arg);
+    GjsSmartPointer<T> list = gjs_arg_steal<T*>(arg);
 
     if (transfer == GI_TRANSFER_CONTAINER)
         return true;
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 0ae3e0c8..93bb29ab 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -326,6 +326,16 @@ struct GjsSmartPointer<GVariant> : GjsAutoGVariant {
     using GjsAutoGVariant::GjsAutoPointer;
 };
 
+template <>
+struct GjsSmartPointer<GList> : GjsAutoPointer<GList, GList, g_list_free> {
+    using GjsAutoPointer::GjsAutoPointer;
+};
+
+template <>
+struct GjsSmartPointer<GSList> : GjsAutoPointer<GSList, GSList, g_slist_free> {
+    using GjsAutoPointer::GjsAutoPointer;
+};
+
 /* For use of GjsAutoInfo<TAG> in GC hash maps */
 namespace JS {
 template <GIInfoType TAG>
diff --git a/gjs/stack.cpp b/gjs/stack.cpp
index c4a4e1f3..a6ad8539 100644
--- a/gjs/stack.cpp
+++ b/gjs/stack.cpp
@@ -12,6 +12,7 @@
 #include <jsfriendapi.h>  // for DumpBacktrace
 
 #include "gjs/context.h"
+#include "gjs/jsapi-util.h"
 
 // Avoid static_assert in MSVC builds
 namespace JS {
@@ -33,7 +34,7 @@ gjs_context_print_stack_stderr(GjsContext *context)
 void
 gjs_dumpstack(void)
 {
-    GList *contexts = gjs_context_get_all();
+    GjsSmartPointer<GList> contexts = gjs_context_get_all();
     GList *iter;
 
     for (iter = contexts; iter; iter = iter->next) {
@@ -41,5 +42,4 @@ gjs_dumpstack(void)
         gjs_context_print_stack_stderr(context);
         g_object_unref(context);
     }
-    g_list_free(contexts);
 }


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