[libpeas] Set the default engine in init() and not constructor()



commit 672a2b04dcfd2badd81e3751b89ac5cb7b28efd1
Author: Garrett Regier <garrettregier gmail com>
Date:   Sun Feb 12 06:25:44 2012 -0800

    Set the default engine in init() and not constructor()
    
    We set the default engine in init() because if a plugin is loaded
    and it needs the default engine then a different engine would be returned
    as the default engine would not have been set yet.

 libpeas/peas-engine.c  |   27 ++++++++++++++-------------
 tests/libpeas/engine.c |   19 +++++++++++++++++++
 2 files changed, 33 insertions(+), 13 deletions(-)
---
diff --git a/libpeas/peas-engine.c b/libpeas/peas-engine.c
index 58c60c4..619d870 100644
--- a/libpeas/peas-engine.c
+++ b/libpeas/peas-engine.c
@@ -325,6 +325,17 @@ loader_destroy (LoaderInfo *info)
 static void
 peas_engine_init (PeasEngine *engine)
 {
+  /* Set the default engine here and not in constructor() to make sure
+   * that if a plugin is loaded and calls peas_engine_get_default()
+   * that this engine is returned and not another.
+   */
+  if (default_engine == NULL)
+    {
+      default_engine = engine;
+      g_object_add_weak_pointer (G_OBJECT (engine),
+                                 (gpointer *) &default_engine);
+    }
+
   engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine,
                                               PEAS_TYPE_ENGINE,
                                               PeasEnginePrivate);
@@ -364,8 +375,6 @@ peas_engine_constructor (GType                  type,
                          guint                  n_construct_params,
                          GObjectConstructParam *construct_params)
 {
-  GObject *object;
-
   /* We don't support calling PeasEngine API without module support */
   if (!g_module_supported ())
     {
@@ -379,17 +388,9 @@ peas_engine_constructor (GType                  type,
                "as it has been shutdown.");
     }
 
-  object = G_OBJECT_CLASS (peas_engine_parent_class)->constructor (type,
-                                                                   n_construct_params,
-                                                                   construct_params);
-
-  if (default_engine == NULL)
-    {
-      default_engine = PEAS_ENGINE (object);
-      g_object_add_weak_pointer (object, (gpointer *) &default_engine);
-    }
-
-  return object;
+  return G_OBJECT_CLASS (peas_engine_parent_class)->constructor (type,
+                                                                 n_construct_params,
+                                                                 construct_params);
 }
 
 static void
diff --git a/tests/libpeas/engine.c b/tests/libpeas/engine.c
index cd9b82d..7c58d5b 100644
--- a/tests/libpeas/engine.c
+++ b/tests/libpeas/engine.c
@@ -93,9 +93,28 @@ test_engine_dispose (PeasEngine *engine)
 static void
 test_engine_get_default (void)
 {
+  GType the_type;
+  PeasEngine *test_engine;
+
   g_assert (peas_engine_get_default () == peas_engine_get_default ());
 
   g_object_unref (peas_engine_get_default ());
+
+
+  /* Check that the default engine is the newly created engine
+   * even when peas_engine_get_default() is called during init().
+   */
+  the_type = g_type_register_static_simple (PEAS_TYPE_ENGINE,
+                                            "TestEngineGetDefault",
+                                            sizeof (PeasEngineClass), NULL,
+                                            sizeof (PeasEngine),
+                                            (GInstanceInitFunc) peas_engine_get_default,
+                                            0);
+  test_engine = PEAS_ENGINE (g_object_new (the_type, NULL));
+
+  g_assert (peas_engine_get_default () == test_engine);
+
+  g_object_unref (test_engine);
 }
 
 static void



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