[gjs/wip/ptomato/785657: 4/6] object: Refactor out ConnectData



commit a234897b16ebac4549bef7defa2f66c48990b9b5
Author: Philip Chimento <philip endlessm com>
Date:   Tue Aug 22 12:10:12 2017 -0700

    object: Refactor out ConnectData
    
    The ConnectData struct is not exactly needed; we can keep a list of
    GClosure signal closures directly. The 'obj' member was only used in the
    invalidate notifier, so we can pass that as the notifier callback data
    instead of the ConnectData struct.

 gi/object.cpp |   34 +++++++++++-----------------------
 1 files changed, 11 insertions(+), 23 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 8d82ac8..260e288 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -55,8 +55,6 @@
 #include <util/hash-x32.h>
 #include <girepository.h>
 
-typedef struct _ConnectData ConnectData;
-
 struct ObjectInstance {
     GIObjectInfo *info;
     GObject *gobj; /* NULL if we are the prototype and not an instance */
@@ -64,7 +62,7 @@ struct ObjectInstance {
     GType gtype;
 
     /* a list of all signal connections, used when tracing */
-    std::set<ConnectData *> signals;
+    std::set<GClosure *> signals;
 
     /* the GObjectClass wrapped by this JS Object (only used for
        prototypes) */
@@ -76,11 +74,6 @@ struct ObjectInstance {
     unsigned js_object_finalized : 1;
 };
 
-struct _ConnectData {
-    ObjectInstance *obj;
-    GClosure *closure;
-};
-
 static std::stack<JS::PersistentRootedObject> object_init_list;
 static GHashTable *class_init_properties;
 
@@ -1283,10 +1276,10 @@ invalidate_all_signals(ObjectInstance *priv)
      * invalidate notifier */
     while (!priv->signals.empty()) {
         /* This will also free cd, through the closure invalidation mechanism */
-        ConnectData *cd = *priv->signals.begin();
-        g_closure_invalidate(cd->closure);
+        GClosure *closure = *priv->signals.begin();
+        g_closure_invalidate(closure);
         /* Erase element if not already erased */
-        priv->signals.erase(cd);
+        priv->signals.erase(closure);
     }
 }
 
@@ -1449,8 +1442,8 @@ object_instance_trace(JSTracer *tracer,
     if (priv == NULL)
         return;
 
-    for (ConnectData *cd : priv->signals)
-        gjs_closure_trace(cd->closure, tracer);
+    for (GClosure *closure : priv->signals)
+        gjs_closure_trace(closure, tracer);
 
     for (auto vfunc : priv->vfuncs)
         vfunc->js_function.trace(tracer, "ObjectInstance::vfunc");
@@ -1460,10 +1453,8 @@ static void
 signal_connection_invalidated(void     *data,
                               GClosure *closure)
 {
-    auto cd = static_cast<ConnectData *>(data);
-
-    cd->obj->signals.erase(cd);
-    g_slice_free(ConnectData, cd);
+    auto priv = static_cast<ObjectInstance *>(data);
+    priv->signals.erase(closure);
 }
 
 static void
@@ -1638,7 +1629,6 @@ real_connect_func(JSContext *context,
     guint signal_id;
     GjsAutoJSChar signal_name(context);
     GQuark signal_detail;
-    ConnectData *connect_data;
 
     gjs_debug_gsignal("connect obj %p priv %p argc %d", obj.get(), priv, argc);
     if (priv == NULL) {
@@ -1677,12 +1667,10 @@ real_connect_func(JSContext *context,
     if (closure == NULL)
         return false;
 
-    connect_data = g_slice_new0(ConnectData);
-    priv->signals.insert(connect_data);
-    connect_data->obj = priv;
     /* This is a weak reference, and will be cleared when the closure is invalidated */
-    connect_data->closure = closure;
-    g_closure_add_invalidate_notifier(closure, connect_data, signal_connection_invalidated);
+    priv->signals.insert(closure);
+    g_closure_add_invalidate_notifier(closure, priv,
+                                      signal_connection_invalidated);
 
     id = g_signal_connect_closure_by_id(priv->gobj,
                                         signal_id,


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