[gjs] gi/boxed.cpp, gjs/importer.cpp: Partially use std::unique_ptr



commit 4b6bc05030111ebe548f19e61c544d2e186e12e6
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Tue Feb 7 15:56:53 2017 +0800

    gi/boxed.cpp, gjs/importer.cpp: Partially use std::unique_ptr
    
    This makes the declarations of variables using g_autoptr() and g_autofree
    which allocates memory using GLib/GIO APIs.
    
    There are other uses of these that need to be changed, but they will
    involve changing much larger amount of things as internal utility
    function(s) need(s) to be updated and subsequently all the calls must be
    updated as well, which will be done in a subsequent patch.
    
    Also based on patch suggested by Philip Chimento in this bug series.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=777597

 gi/boxed.cpp     |    8 ++++----
 gjs/importer.cpp |   24 ++++++++++++------------
 gjs/jsapi-util.h |   28 ++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 16 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index bf6e478..cb5f5c2 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -848,10 +848,10 @@ define_boxed_class_fields(JSContext       *cx,
     for (i = 0; i < n_fields; i++) {
         GIFieldInfo *field = g_struct_info_get_field (priv->info, i);
         const char *field_name = g_base_info_get_name ((GIBaseInfo *)field);
-        g_autofree char *getter_name = g_strconcat("boxed_field_get::",
-                                                   field_name, NULL);
-        g_autofree char *setter_name = g_strconcat("boxed_field_set::",
-                                                   field_name, NULL);
+        GjsAutoChar getter_name = g_strconcat("boxed_field_get::",
+                                              field_name, NULL);
+        GjsAutoChar setter_name = g_strconcat("boxed_field_set::",
+                                              field_name, NULL);
         g_base_info_unref ((GIBaseInfo *)field);
 
         /* In order to have one getter and setter for all the properties
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index bdaaf82..334474b 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -71,15 +71,16 @@ importer_to_string(JSContext *cx,
         return false;
 
     g_autofree char *path = NULL;
+    GjsAutoChar output;
+
     if (module_path.isNull()) {
-        path = g_strdup("root");
+        output = g_strdup_printf("[%s root]", klass->name);
     } else {
         if (!gjs_string_to_utf8(cx, module_path, &path))
             return false;
+        output = g_strdup_printf("[%s %s]", klass->name, path);
     }
 
-    g_autofree char *output = g_strdup_printf("[%s %s]", klass->name, path);
-
     args.rval().setString(JS_NewStringCopyZ(cx, output));
     return true;
 }
@@ -127,7 +128,7 @@ define_meta_properties(JSContext       *context,
                                      &parent_module_path))
             return false;
 
-        g_autofree char *module_path_buf = NULL;
+        GjsAutoChar module_path_buf;
         if (parent_module_path.isNull()) {
             module_path_buf = g_strdup(module_name);
         } else {
@@ -279,7 +280,7 @@ module_to_string(JSContext *cx,
     g_autofree char *path = NULL;
     if (!gjs_string_to_utf8(cx, module_path, &path))
         return false;
-    g_autofree char *output = g_strdup_printf("[GjsModule %s]", path);
+    GjsAutoChar output = g_strdup_printf("[GjsModule %s]", path);
 
     args.rval().setString(JS_NewStringCopyZ(cx, output));
     return true;
@@ -371,7 +372,6 @@ load_module_init(JSContext       *context,
                  const char      *full_path)
 {
     bool found;
-    g_autoptr(GFile) file = NULL;
 
     /* First we check if js module has already been loaded  */
     if (gjs_object_has_property(context, in_object, GJS_STRING_MODULE_INIT,
@@ -385,7 +385,7 @@ load_module_init(JSContext       *context,
     }
 
     JS::RootedObject module_obj(context, create_module_object(context));
-    file = g_file_new_for_commandline_arg(full_path);
+    GjsAutoUnref<GFile> file = g_file_new_for_commandline_arg(full_path);
     if (!import_file (context, "__init__", file, module_obj))
         return module_obj;
 
@@ -434,8 +434,8 @@ import_symbol_from_init_js(JSContext       *cx,
                            bool            *result)
 {
     bool found;
-    g_autofree char *full_path = g_build_filename(dirname, MODULE_INIT_FILENAME,
-                                                  NULL);
+    GjsAutoChar full_path = g_build_filename(dirname, MODULE_INIT_FILENAME,
+                                             NULL);
 
     JS::RootedObject module_obj(cx, load_module_init(cx, importer, full_path));
     if (!module_obj || !JS_AlreadyHasOwnProperty(cx, module_obj, name, &found))
@@ -796,9 +796,9 @@ importer_new_enumerate(JSContext  *context,
             g_free(init_path);
 
             /* new_for_commandline_arg handles resource:/// paths */
-            g_autoptr(GFile) dir = g_file_new_for_commandline_arg(dirname);
+            GjsAutoUnref<GFile> dir = g_file_new_for_commandline_arg(dirname);
             g_free(dirname);
-            g_autoptr(GFileEnumerator) direnum =
+            GjsAutoUnref<GFileEnumerator> direnum =
                 g_file_enumerate_children(dir, G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                           G_FILE_QUERY_INFO_NONE, NULL, NULL);
 
@@ -810,7 +810,7 @@ importer_new_enumerate(JSContext  *context,
                 if (info == NULL || file == NULL)
                     break;
 
-                g_autofree char *filename = g_file_get_basename(file);
+                GjsAutoChar filename = g_file_get_basename(file);
 
                 /* skip hidden files and directories (.svn, .git, ...) */
                 if (filename[0] == '.')
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index d41f45d..235797d 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -24,6 +24,7 @@
 #ifndef __GJS_JSAPI_UTIL_H__
 #define __GJS_JSAPI_UTIL_H__
 
+#include <memory>
 #include <stdbool.h>
 
 #include <glib-object.h>
@@ -32,6 +33,33 @@
 #include "gjs/runtime.h"
 #include "gi/gtype.h"
 
+class GjsAutoChar : public std::unique_ptr<char, decltype(&g_free)> {
+public:
+    typedef std::unique_ptr<char, decltype(&g_free)> U;
+
+    GjsAutoChar(char *str = nullptr) : U(str, g_free) {}
+
+    operator const char *() {
+        return get();
+    }
+
+    void operator= (const char* str) {
+        reset(const_cast<char*>(str));
+    }
+};
+
+template <typename T>
+class GjsAutoUnref : public std::unique_ptr<T, decltype(&g_object_unref)> {
+public:
+    typedef std::unique_ptr<T, decltype(&g_object_unref)> U;
+
+    GjsAutoUnref(T *ptr = nullptr) : U(ptr, g_object_unref) {}
+
+    operator T *() {
+        return U::get();
+    }
+};
+
 G_BEGIN_DECLS
 
 #define GJS_UTIL_ERROR gjs_util_error_quark ()


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