[libpeas] Allow multiple instances of PeasEngine



commit e4e0058df2bbb6332fcb4bbfe13b4eb1b35e9b6a
Author: Garrett Regier <alias301 gmail com>
Date:   Wed Feb 9 23:55:29 2011 -0800

    Allow multiple instances of PeasEngine

 libpeas/peas-engine.c               |   58 ++++++++++++++++------------------
 libpeas/peas-engine.h               |    1 +
 tests/libpeas-gtk/testing/testing.c |    2 +-
 tests/libpeas/engine.c              |   49 +++++++++--------------------
 tests/libpeas/testing/testing.c     |    2 +-
 5 files changed, 46 insertions(+), 66 deletions(-)
---
diff --git a/libpeas/peas-engine.c b/libpeas/peas-engine.c
index 5ee843a..10776f7 100644
--- a/libpeas/peas-engine.c
+++ b/libpeas/peas-engine.c
@@ -354,15 +354,15 @@ peas_engine_constructor (GType                  type,
                "as it has been shutdown.");
     }
 
-  if (default_engine != NULL)
-    return g_object_ref (G_OBJECT (default_engine));
-
   object = G_OBJECT_CLASS (peas_engine_parent_class)->constructor (type,
                                                                    n_construct_params,
                                                                    construct_params);
 
-  default_engine = PEAS_ENGINE (object);
-  g_object_add_weak_pointer (object, (gpointer *) &default_engine);
+  if (default_engine == NULL)
+    {
+      default_engine = PEAS_ENGINE (object);
+      g_object_add_weak_pointer (object, (gpointer *) &default_engine);
+    }
 
   return object;
 }
@@ -1187,6 +1187,22 @@ peas_engine_set_loaded_plugins (PeasEngine   *engine,
 }
 
 /**
+ * peas_engine_new:
+ *
+ * Return a new instance of #PeasEngine.
+ * If no default #PeasEngine has been instantiated yet,
+ * the first call of this function will set the default
+ * engine as the new instance of #PeasEngine.
+ *
+ * Returns: a new instance of #PeasEngine.
+ */
+PeasEngine *
+peas_engine_new (void)
+{
+  return PEAS_ENGINE (g_object_new (PEAS_TYPE_ENGINE, NULL));
+}
+
+/**
  * peas_engine_get_default:
  *
  * Return the existing instance of #PeasEngine or a subclass of it.
@@ -1198,20 +1214,14 @@ peas_engine_set_loaded_plugins (PeasEngine   *engine,
 PeasEngine *
 peas_engine_get_default (void)
 {
+  /* peas_engine_new() will cause the default to be set for us */
   if (default_engine == NULL)
-    return PEAS_ENGINE (g_object_new (PEAS_TYPE_ENGINE, NULL));
+    return peas_engine_new ();
 
   return default_engine;
 }
 
-static void
-destroy_loaders (void)
-{
-  g_hash_table_destroy (loaders);
-  loaders = NULL;
-}
-
-/**
+/*
  * peas_engine_shutdown:
  *
  * Frees memory shared by PeasEngines.
@@ -1225,23 +1235,9 @@ peas_engine_shutdown (void)
 
   shutdown = TRUE;
 
-  if (default_engine != NULL)
+  if (loaders != NULL)
     {
-      /* Add a weak ref that will destroy the loaders hashtable,
-       * this way if the engine is still around it won't segfault
-       */
-      g_object_weak_ref (G_OBJECT (default_engine),
-                         (GWeakNotify) destroy_loaders, NULL);
-
-      g_object_unref (default_engine);
-
-      /* The weak-ref should set it to NULL */
-      if (default_engine != NULL)
-        {
-          default_engine = NULL;
-
-          g_warning ("libpeas failed to shutdown, "
-                     "the plugin engine was not freed.");
-        }
+      g_hash_table_destroy (loaders);
+      loaders = NULL;
     }
 }
diff --git a/libpeas/peas-engine.h b/libpeas/peas-engine.h
index e258fd5..0b569ee 100644
--- a/libpeas/peas-engine.h
+++ b/libpeas/peas-engine.h
@@ -70,6 +70,7 @@ struct _PeasEngineClass {
 };
 
 GType             peas_engine_get_type            (void) G_GNUC_CONST;
+PeasEngine       *peas_engine_new                 (void);
 PeasEngine       *peas_engine_get_default         (void);
 
 void              peas_engine_add_search_path     (PeasEngine      *engine,
diff --git a/tests/libpeas-gtk/testing/testing.c b/tests/libpeas-gtk/testing/testing.c
index 9b58050..34a5390 100644
--- a/tests/libpeas-gtk/testing/testing.c
+++ b/tests/libpeas-gtk/testing/testing.c
@@ -114,7 +114,7 @@ testing_engine_new (void)
     }
 
   /* Must be after requiring typelibs */
-  engine = peas_engine_get_default ();
+  engine = peas_engine_new ();
 
   g_object_add_weak_pointer (G_OBJECT (engine), (gpointer *) &engine);
 
diff --git a/tests/libpeas/engine.c b/tests/libpeas/engine.c
index 8506636..fe593a3 100644
--- a/tests/libpeas/engine.c
+++ b/tests/libpeas/engine.c
@@ -63,17 +63,16 @@ test_engine_new (PeasEngine *engine)
 {
   PeasEngine *new_engine;
 
-  /* Some bindings may allow creating a PeasEngine with
-   * g_object_new(). So make sure that we get the default
-   * engine and not a new engine or an assert.
-   */
-
-  new_engine = g_object_new (PEAS_TYPE_ENGINE, NULL);
+  new_engine = peas_engine_new ();
 
   g_assert (engine != NULL);
-  g_assert (engine == new_engine);
+  g_assert (new_engine != NULL);
+
+  /* Does not return the same engine*/
+  g_assert (engine != new_engine);
+  /* peas_engine_new() sets the default engine */
+  g_assert (engine == peas_engine_get_default ());
 
-  /* g_object_new() will give us a new ref */
   g_object_unref (new_engine);
 }
 
@@ -89,7 +88,14 @@ test_engine_dispose (PeasEngine *engine)
 static void
 test_engine_get_default (PeasEngine *engine)
 {
-  g_assert (engine == peas_engine_get_default ());
+  /* testing_engine_new() uses peas_engine_new()
+   * so this makes sure that peas_engine_get_default()
+   * acutally sets the default engine
+   */
+
+  g_object_unref (engine);
+
+  g_assert (peas_engine_get_default () == peas_engine_get_default ());
 }
 
 static void
@@ -322,29 +328,6 @@ test_engine_enable_loader (PeasEngine *engine)
 static void
 test_engine_shutdown (PeasEngine *engine)
 {
-  PeasPluginInfo *info;
-
-  /* Ref the engine to cause the shutdown to fail */
-  g_object_ref (engine);
-
-  /* libpeas fails to shutdown because the engine still has a ref */
-  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
-    {
-      peas_engine_shutdown ();
-      exit (0);
-    }
-  g_test_trap_assert_failed ();
-  g_test_trap_assert_stderr ("*libpeas failed to shutdown*"
-                             "*plugin engine was not freed*");
-
-  /* Make sure that if an engine is around
-   * after shutdown the engine does not segfault.
-   */
-  info = peas_engine_get_plugin_info (engine, "loadable");
-  g_assert (peas_engine_load_plugin (engine, info));
-
-  /* Free our extra ref and then the engine */
-  g_object_unref (engine);
   testing_engine_free (engine);
 
   /* Should be able to shutdown multiple times */
@@ -353,7 +336,7 @@ test_engine_shutdown (PeasEngine *engine)
   /* Cannot get the default as libpeas has been shutdown */
   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
     {
-      peas_engine_get_default ();
+      peas_engine_new ();
       exit (0);
     }
   g_test_trap_assert_failed ();
diff --git a/tests/libpeas/testing/testing.c b/tests/libpeas/testing/testing.c
index 93a54c1..15554af 100644
--- a/tests/libpeas/testing/testing.c
+++ b/tests/libpeas/testing/testing.c
@@ -128,7 +128,7 @@ testing_engine_new (void)
   testing_init ();
 
   /* Must be after requiring typelibs */
-  engine = peas_engine_get_default ();
+  engine = peas_engine_new ();
 
   g_object_add_weak_pointer (G_OBJECT (engine), (gpointer *) &engine);
 



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