[libpeas] Use properties to store python and C extension instances



commit 4a4b41dab2db8f51e2076f8aadbcab4c96b7be30
Author: Steve Frécinaux <code istique net>
Date:   Sun Mar 6 19:05:09 2011 +0100

    Use properties to store python and C extension instances
    
    This is meant to make testing of loader-specific stuff easier.

 loaders/c/peas-extension-c.c           |   46 +++++++++++++++++++++++++-----
 loaders/python/peas-extension-python.c |   48 +++++++++++++++++++++++++++----
 2 files changed, 79 insertions(+), 15 deletions(-)
---
diff --git a/loaders/c/peas-extension-c.c b/loaders/c/peas-extension-c.c
index 9202054..ea8d293 100644
--- a/loaders/c/peas-extension-c.c
+++ b/loaders/c/peas-extension-c.c
@@ -30,11 +30,34 @@
 
 G_DEFINE_TYPE (PeasExtensionC, peas_extension_c, PEAS_TYPE_EXTENSION);
 
+enum {
+  PROP_0,
+  PROP_INSTANCE
+};
+
 static void
 peas_extension_c_init (PeasExtensionC *cexten)
 {
 }
 
+static void
+peas_extension_c_set_property (GObject      *object,
+                               guint         prop_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+  PeasExtensionC *cexten = PEAS_EXTENSION_C (object);
+
+  switch (prop_id)
+    {
+    case PROP_INSTANCE:
+      cexten->instance = g_value_get_object (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
 static gboolean
 peas_extension_c_call (PeasExtension *exten,
                        GType          gtype,
@@ -54,7 +77,7 @@ static void
 peas_extension_c_dispose (GObject *object)
 {
   PeasExtensionC *cexten = PEAS_EXTENSION_C (object);
-  
+
   if (cexten->instance)
     {
       g_object_unref (cexten->instance);
@@ -70,23 +93,30 @@ peas_extension_c_class_init (PeasExtensionCClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   PeasExtensionClass *extension_class = PEAS_EXTENSION_CLASS (klass);
 
+  object_class->set_property = peas_extension_c_set_property;
   object_class->dispose = peas_extension_c_dispose;
 
   extension_class->call = peas_extension_c_call;
+
+  g_object_class_install_property (object_class,
+                                   PROP_INSTANCE,
+                                   g_param_spec_object ("instance",
+                                                        "Extension Instance",
+                                                        "The C Extension Instance",
+                                                        G_TYPE_OBJECT,
+                                                        G_PARAM_WRITABLE |
+                                                        G_PARAM_CONSTRUCT_ONLY));
 }
 
 PeasExtension *
 peas_extension_c_new (GType    gtype,
                       GObject *instance)
 {
-  PeasExtensionC *cexten;
   GType real_type;
 
   real_type = peas_extension_register_subclass (PEAS_TYPE_EXTENSION_C, gtype);
-  cexten = PEAS_EXTENSION_C (g_object_new (real_type,
-                                           "extension-type", gtype,
-                                           NULL));
-  cexten->instance = instance;
-
-  return PEAS_EXTENSION (cexten);
+  return PEAS_EXTENSION (g_object_new (real_type,
+                                       "extension-type", gtype,
+                                       "instance", instance,
+                                       NULL));
 }
diff --git a/loaders/python/peas-extension-python.c b/loaders/python/peas-extension-python.c
index 5277742..24ab7ed 100644
--- a/loaders/python/peas-extension-python.c
+++ b/loaders/python/peas-extension-python.c
@@ -35,11 +35,39 @@
 
 G_DEFINE_TYPE (PeasExtensionPython, peas_extension_python, PEAS_TYPE_EXTENSION);
 
+enum {
+  PROP_0,
+  PROP_INSTANCE
+};
+
 static void
 peas_extension_python_init (PeasExtensionPython *pyexten)
 {
 }
 
+static void
+peas_extension_python_set_property (GObject      *object,
+                                    guint         prop_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+  PeasExtensionPython *pyexten = PEAS_EXTENSION_PYTHON (object);
+  PyGILState_STATE state;
+
+  switch (prop_id)
+    {
+    case PROP_INSTANCE:
+      pyexten->instance = g_value_get_pointer (value);
+
+      state = pyg_gil_state_ensure ();
+      Py_INCREF (pyexten->instance);
+      pyg_gil_state_release (state);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
 static gboolean
 peas_extension_python_call (PeasExtension *exten,
                             GType          gtype,
@@ -88,9 +116,18 @@ peas_extension_python_class_init (PeasExtensionPythonClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   PeasExtensionClass *extension_class = PEAS_EXTENSION_CLASS (klass);
 
+  object_class->set_property = peas_extension_python_set_property;
   object_class->dispose = peas_extension_python_dispose;
 
   extension_class->call = peas_extension_python_call;
+
+  g_object_class_install_property (object_class,
+                                   PROP_INSTANCE,
+                                   g_param_spec_pointer ("instance",
+                                                        "Extension Instance",
+                                                        "The Python Extension Instance",
+                                                        G_PARAM_WRITABLE |
+                                                        G_PARAM_CONSTRUCT_ONLY));
 }
 
 PeasExtension *
@@ -101,11 +138,8 @@ peas_extension_python_new (GType     gtype,
   GType real_type;
 
   real_type = peas_extension_register_subclass (PEAS_TYPE_EXTENSION_PYTHON, gtype);
-  pyexten = PEAS_EXTENSION_PYTHON (g_object_new (real_type,
-                                                 "extension-type", gtype,
-                                                 NULL));
-  pyexten->instance = instance;
-  Py_INCREF (instance);
-
-  return PEAS_EXTENSION (pyexten);
+  return PEAS_EXTENSION (g_object_new (real_type,
+                                       "extension-type", gtype,
+                                       "instance", instance,
+                                       NULL));
 }



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