[libpeas] Make PeasEngine a proper singleton
- From: Garrett Regier <gregier src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libpeas] Make PeasEngine a proper singleton
- Date: Wed, 9 Feb 2011 08:39:41 +0000 (UTC)
commit 8a9a4795aedb10399cd865e66ba565bc3f7f69c6
Author: Garrett Regier <alias301 gmail com>
Date: Sun Feb 6 03:03:48 2011 -0800
Make PeasEngine a proper singleton
Before it would assert if you used g_object_new()
after the default instance was created.
libpeas/peas-engine.c | 27 +++++++++++++++++++++------
tests/libpeas/engine.c | 21 ++++++++++++++++++++-
2 files changed, 41 insertions(+), 7 deletions(-)
---
diff --git a/libpeas/peas-engine.c b/libpeas/peas-engine.c
index 9aecbe7..1c6257d 100644
--- a/libpeas/peas-engine.c
+++ b/libpeas/peas-engine.c
@@ -295,12 +295,6 @@ add_loader (PeasEngine *engine,
static void
peas_engine_init (PeasEngine *engine)
{
- /* Set the default engine pointer, for peas_engine_get_default().
- * We only allow one single instance of a PeasEngine subclass. */
- g_assert (default_engine == NULL);
- default_engine = engine;
- g_object_add_weak_pointer (G_OBJECT (engine), (gpointer *) &default_engine);
-
if (!g_module_supported ())
{
g_warning ("libpeas is not able to initialize the plugins engine.");
@@ -345,6 +339,26 @@ peas_engine_garbage_collect (PeasEngine *engine)
NULL);
}
+static GObject *
+peas_engine_constructor (GType type,
+ guint n_construct_params,
+ GObjectConstructParam *construct_params)
+{
+ GObject *object;
+
+ 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);
+
+ return object;
+}
+
static void
peas_engine_set_property (GObject *object,
guint prop_id,
@@ -448,6 +462,7 @@ peas_engine_class_init (PeasEngineClass *klass)
GType the_type = G_TYPE_FROM_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ object_class->constructor = peas_engine_constructor;
object_class->set_property = peas_engine_set_property;
object_class->get_property = peas_engine_get_property;
object_class->dispose = peas_engine_dispose;
diff --git a/tests/libpeas/engine.c b/tests/libpeas/engine.c
index 3cfc0e7..f53e749 100644
--- a/tests/libpeas/engine.c
+++ b/tests/libpeas/engine.c
@@ -57,9 +57,27 @@ test_runner (TestFixture *fixture,
}
static void
-test_engine_get_default (PeasEngine *engine)
+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);
+
g_assert (engine != NULL);
+ g_assert (engine == new_engine);
+
+ /* g_object_new() will give us a new ref */
+ g_object_unref (new_engine);
+}
+
+static void
+test_engine_get_default (PeasEngine *engine)
+{
g_assert (engine == peas_engine_get_default ());
}
@@ -304,6 +322,7 @@ main (int argc,
(gpointer) test_engine_##ftest, \
test_setup, test_runner, test_teardown)
+ TEST ("new", new);
TEST ("get-default", get_default);
TEST ("load-plugin", load_plugin);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]