[gjs: 5/8] jsapi-util: Move and rename GjsAutoParam



commit b056201c1564ec7477a9f08044fa17e499da149c
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Jun 9 12:31:32 2018 -0700

    jsapi-util: Move and rename GjsAutoParam
    
    We introduce a new unique_ptr variant that was previously used privately
    in object.cpp. We'll be using this in multiple files in a following
    commit.

 gi/object.cpp    | 42 +++++++++++++++++-------------------------
 gjs/jsapi-util.h | 17 +++++++++++++++++
 2 files changed, 34 insertions(+), 25 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index a56b45b6..58ca7b37 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -80,20 +80,13 @@ struct AutoGValueVector : public std::vector<GValue> {
     }
 };
 
-using ParamRef = std::unique_ptr<GParamSpec, decltype(&g_param_spec_unref)>;
-using PropertyCache = JS::GCHashMap<JS::Heap<JSString *>, ParamRef,
-                                    js::DefaultHasher<JSString *>,
-                                    js::SystemAllocPolicy>;
+using PropertyCache =
+    JS::GCHashMap<JS::Heap<JSString*>, GjsAutoParam,
+                  js::DefaultHasher<JSString*>, js::SystemAllocPolicy>;
 using FieldCache = JS::GCHashMap<JS::Heap<JSString *>, GjsAutoInfo<GIFieldInfo>,
                                  js::DefaultHasher<JSString *>,
                                  js::SystemAllocPolicy>;
 
-/* This tells the GCHashMap that the GC doesn't need to care about ParamRef */
-namespace JS {
-template<>
-struct GCPolicy<ParamRef> : public IgnoreGCPolicy<ParamRef> {};
-};
-
 class ObjectInstance {
     GIObjectInfo *m_info;
     GObject *m_gobj;  /* nullptr if we are the prototype and not an instance */
@@ -358,8 +351,8 @@ class ObjectInstance {
 
 static std::stack<JS::PersistentRootedObject> object_init_list;
 
-using ParamRefArray = std::vector<ParamRef>;
-static std::unordered_map<GType, ParamRefArray> class_init_properties;
+using AutoParamArray = std::vector<GjsAutoParam>;
+static std::unordered_map<GType, AutoParamArray> class_init_properties;
 
 static bool weak_pointer_callback = false;
 ObjectInstance *ObjectInstance::wrapped_gobject_list = nullptr;
@@ -571,8 +564,7 @@ ObjectInstance::find_param_spec_from_id(JSContext       *cx,
 
     gname = gjs_hyphen_from_camel(js_prop_name);
     GObjectClass *gobj_class = G_OBJECT_CLASS(g_type_class_ref(m_gtype));
-    ParamRef param_spec(g_object_class_find_property(gobj_class, gname),
-                        g_param_spec_unref);
+    GjsAutoParam param_spec = g_object_class_find_property(gobj_class, gname);
     g_type_class_unref(gobj_class);
     g_free(gname);
 
@@ -2940,11 +2932,11 @@ gjs_interface_init(GTypeInterface *g_iface,
     if (found == class_init_properties.end())
         return;
 
-    ParamRefArray& properties = found->second;
-    for (ParamRef& pspec : properties) {
-        g_param_spec_set_qdata(pspec.get(), gjs_is_custom_property_quark(),
+    AutoParamArray& properties = found->second;
+    for (GjsAutoParam& pspec : properties) {
+        g_param_spec_set_qdata(pspec, gjs_is_custom_property_quark(),
                                GINT_TO_POINTER(1));
-        g_object_interface_install_property(g_iface, pspec.get());
+        g_object_interface_install_property(g_iface, pspec);
     }
 
     class_init_properties.erase(found);
@@ -2964,12 +2956,12 @@ gjs_object_class_init(GObjectClass *klass,
     if (found == class_init_properties.end())
         return;
 
-    ParamRefArray& properties = found->second;
+    AutoParamArray& properties = found->second;
     unsigned i = 0;
-    for (ParamRef& pspec : properties) {
-        g_param_spec_set_qdata(pspec.get(), gjs_is_custom_property_quark(),
+    for (GjsAutoParam& pspec : properties) {
+        g_param_spec_set_qdata(pspec, gjs_is_custom_property_quark(),
                                GINT_TO_POINTER(1));
-        g_object_class_install_property(klass, ++i, pspec.get());
+        g_object_class_install_property(klass, ++i, pspec);
     }
 
     class_init_properties.erase(found);
@@ -3110,7 +3102,7 @@ save_properties_for_class_init(JSContext       *cx,
                                uint32_t         n_properties,
                                GType            gtype)
 {
-    ParamRefArray properties_native;
+    AutoParamArray properties_native;
     JS::RootedValue prop_val(cx);
     JS::RootedObject prop_obj(cx);
     for (uint32_t i = 0; i < n_properties; i++) {
@@ -3126,8 +3118,8 @@ save_properties_for_class_init(JSContext       *cx,
         if (!gjs_typecheck_param(cx, prop_obj, G_TYPE_NONE, true))
             return false;
 
-        properties_native.emplace_back(g_param_spec_ref(gjs_g_param_from_param(cx, prop_obj)),
-                                       g_param_spec_unref);
+        properties_native.emplace_back(
+            g_param_spec_ref(gjs_g_param_from_param(cx, prop_obj)));
     }
     class_init_properties[gtype] = std::move(properties_native);
     return true;
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index d751b87c..3597ff96 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -83,6 +83,23 @@ template<typename T>
 struct GCPolicy<GjsAutoInfo<T>> : public IgnoreGCPolicy<GjsAutoInfo<T>> {};
 }
 
+class GjsAutoParam
+    : public std::unique_ptr<GParamSpec, decltype(&g_param_spec_unref)> {
+    public:
+    GjsAutoParam(GParamSpec* ptr = nullptr)
+        : unique_ptr(ptr, g_param_spec_unref)
+    {
+    }
+
+    operator GParamSpec*() { return get(); }
+};
+
+/* For use of GjsAutoParam in GC hash maps */
+namespace JS {
+template<>
+struct GCPolicy<GjsAutoParam> : public IgnoreGCPolicy<GjsAutoParam> {};
+}  // namespace JS
+
 struct GjsJSFreeArgs {
     void operator() (char *str) {
         JS_free(nullptr, str);


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