[gedit] Simpler GeditApp singleton implementation



commit 4029230e9166c3431652723c7b655acd47d07240
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Sat Apr 9 18:53:38 2011 +0200

    Simpler GeditApp singleton implementation

 gedit/gedit-app.c |   41 ++++++++++++++++++-----------------------
 gedit/gedit-app.h |   20 ++++++++++----------
 2 files changed, 28 insertions(+), 33 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index b18de86..37b9e67 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -87,7 +87,9 @@ struct _GeditAppPrivate
 	PeasExtensionSet  *extensions;
 };
 
-G_DEFINE_ABSTRACT_TYPE(GeditApp, gedit_app, G_TYPE_INITIALLY_UNOWNED)
+static GeditApp *app_instance = NULL;
+
+G_DEFINE_ABSTRACT_TYPE(GeditApp, gedit_app, G_TYPE_OBJECT)
 
 static void
 gedit_app_finalize (GObject *object)
@@ -156,20 +158,20 @@ gedit_app_constructor (GType                  gtype,
                        guint                  n_construct_params,
                        GObjectConstructParam *construct_params)
 {
-	static GObject *app = NULL;
-
-	if (!app)
+	if (!app_instance)
 	{
-		app = G_OBJECT_CLASS (gedit_app_parent_class)->constructor (gtype,
-		                                                            n_construct_params,
-		                                                            construct_params);
+		GObject *obj = G_OBJECT_CLASS (
+			gedit_app_parent_class)->constructor (gtype,
+			                                      n_construct_params,
+			                                      construct_params);
 
-		g_object_add_weak_pointer (app, (gpointer *) &app);
+		app_instance = GEDIT_APP (obj);
+		g_object_add_weak_pointer (obj, (gpointer *) &app_instance);
 
-		return app;
+		return obj;
 	}
 
-	return g_object_ref (app);
+	return g_object_ref (app_instance);
 }
 
 static gboolean
@@ -547,9 +549,13 @@ gedit_app_init (GeditApp *app)
 GeditApp *
 gedit_app_get_default (void)
 {
-	GeditApp *app;
 	GType type;
 
+	if (app_instance != NULL)
+	{
+		return app_instance;
+	}
+
 #ifdef OS_OSX
 	type = GEDIT_TYPE_APP_OSX;
 #else
@@ -560,18 +566,7 @@ gedit_app_get_default (void)
 #endif
 #endif
 
-	app = GEDIT_APP (g_object_new (type, NULL));
-
-	if (g_object_is_floating (app))
-	{
-		g_object_ref_sink (app);
-	}
-	else
-	{
-		g_object_unref (app);
-	}
-
-	return app;
+	return GEDIT_APP (g_object_new (type, NULL));
 }
 
 static void
diff --git a/gedit/gedit-app.h b/gedit/gedit-app.h
index 34f71d6..4d2d2ff 100644
--- a/gedit/gedit-app.h
+++ b/gedit/gedit-app.h
@@ -2,7 +2,7 @@
  * gedit-app.h
  * This file is part of gedit
  *
- * Copyright (C) 2005 - Paolo Maggi 
+ * Copyright (C) 2005 - Paolo Maggi
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -16,14 +16,14 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, 
+ * Foundation, Inc., 59 Temple Place, Suite 330,
  * Boston, MA 02111-1307, USA.
  */
- 
+
 /*
- * Modified by the gedit Team, 2005. See the AUTHORS file for a 
- * list of people on the gedit Team.  
- * See the ChangeLog files for a list of changes. 
+ * Modified by the gedit Team, 2005. See the AUTHORS file for a
+ * list of people on the gedit Team.
+ * See the ChangeLog files for a list of changes.
  *
  * $Id$
  */
@@ -55,9 +55,9 @@ typedef struct _GeditAppPrivate GeditAppPrivate;
  */
 typedef struct _GeditApp GeditApp;
 
-struct _GeditApp 
+struct _GeditApp
 {
-	GInitiallyUnowned parent;
+	GObject parent;
 
 	/*< private > */
 	GeditAppPrivate *priv;
@@ -68,9 +68,9 @@ struct _GeditApp
  */
 typedef struct _GeditAppClass GeditAppClass;
 
-struct _GeditAppClass 
+struct _GeditAppClass
 {
-	GInitiallyUnownedClass parent_class;
+	GObjectClass parent_class;
 
 	gboolean (*last_window_destroyed)	(GeditApp    *app);
 



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