gobjectification: 0966d123: Ensure singleton-ness of PurpleAccountMa...

sadrul at pidgin.im sadrul at pidgin.im
Wed Mar 5 12:15:51 EST 2008


-----------------------------------------------------------------
Revision: 0966d12357c2171cfecfd7dbf257ca5c5635a8fd
Ancestor: 1eaba97ebedc622ca5a7c1866925e329d16524d8
Author: sadrul at pidgin.im
Date: 2008-03-04T18:12:07
Branch: im.pidgin.gobjectification
URL: http://d.pidgin.im/viewmtn/revision/info/0966d12357c2171cfecfd7dbf257ca5c5635a8fd

Modified files:
        libpurple/accountmanager.c libpurple/accountmanager.h
        libpurple/internal.h

ChangeLog: 

Ensure singleton-ness of PurpleAccountManager, and use some define goodness from glib.

-------------- next part --------------
============================================================
--- libpurple/accountmanager.c	440ffe8d502c367808238c552a39865b361c7682
+++ libpurple/accountmanager.c	ee48caf65b8e5217d359fe387e49ac981feccb1d
@@ -48,9 +48,21 @@ struct _PurpleAccountManagerPrivate
 
 #define PURPLE_ACCOUNT_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_ACCOUNT_MANAGER, PurpleAccountManagerPrivate))
 
+G_DEFINE_TYPE(PurpleAccountManager, purple_account_manager, G_TYPE_OBJECT)
+
+static GObject *
+purple_account_manager_constructor(GType type, guint n_cprops, GObjectConstructParam *cprops)
+{
+	static GObject *singleton = NULL;
+	if (singleton == NULL)
+		singleton = G_OBJECT_CLASS(purple_account_manager_parent_class)->constructor(type, n_cprops, cprops);
+	return singleton;
+}
+
 static void
 purple_account_manager_class_init(PurpleAccountManagerClass *klass)
 {
+	G_OBJECT_CLASS(klass)->constructor = purple_account_manager_constructor;
 	account_manager_signals[ACCOUNT_ADDED] =
 		g_signal_new("account-added",
 				G_OBJECT_CLASS_TYPE(klass),
@@ -84,32 +96,6 @@ purple_account_manager_init(PurpleAccoun
 	manager->priv->accounts_loaded = FALSE;
 }
 
-GType purple_account_manager_get_gtype(void)
-{
-	static GType type = 0;
-
-	if(type == 0) {
-		static const GTypeInfo info = {
-			sizeof(PurpleAccountManagerClass),
-			NULL,
-			NULL,
-			(GClassInitFunc)purple_account_manager_class_init,
-			NULL,
-			NULL,
-			sizeof(PurpleAccountManager),
-			0,
-			(GInstanceInitFunc)purple_account_manager_init,
-			NULL,
-		};
-
-		type = g_type_register_static(G_TYPE_OBJECT,
-				"PurpleAccountManager",
-				&info, 0);
-	}
-
-	return type;
-}
-
 PurpleAccountManager *purple_account_manager_get(void)
 {
 	static PurpleAccountManager *manager = NULL;
============================================================
--- libpurple/accountmanager.h	580172b9353db9c444dc68412da1a55ba244ecc4
+++ libpurple/accountmanager.h	8b6b2c2be2d057d196431dfceca52d1b658d6e1a
@@ -30,7 +30,7 @@
 #include <glib-object.h>
 #include "account.h"
 
-#define PURPLE_TYPE_ACCOUNT_MANAGER				(purple_account_manager_get_gtype())
+#define PURPLE_TYPE_ACCOUNT_MANAGER				(purple_account_manager_get_type())
 #define PURPLE_ACCOUNT_MANAGER(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_ACCOUNT_MANAGER, PurpleAccountManager))
 #define PURPLE_ACCOUNT_MANAGER_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_ACCOUNT_MANAGER, PurpleAccountManagerClass))
 #define PURPLE_IS_ACCOUNT_MANAGER(obj)			(G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_ACCOUNT_MANAGER))
============================================================
--- libpurple/internal.h	d338ef3d4c418c585227845e498b7a8014a12a66
+++ libpurple/internal.h	570d01bcd8d9fcf66ba7d6f7c4f44fed877d3d1e
@@ -198,6 +198,41 @@
 #	endif
 #endif
 
+#include <glib.h>
+#include <glib-object.h>
+
+#ifndef G_DEFINE_TYPE
+#define G_DEFINE_TYPE(TypeName, type_name, TYPE_PARENT) \
+\
+static void     type_name##_init              (TypeName        *self); \
+static void     type_name##_class_init        (TypeName##Class *klass); \
+static gpointer type_name##_parent_class = NULL; \
+static void     type_name##_class_intern_init (gpointer klass) \
+{ \
+  type_name##_parent_class = g_type_class_peek_parent (klass); \
+  type_name##_class_init ((TypeName##Class*) klass); \
+} \
+\
+GType \
+type_name##_get_type (void) \
+{ \
+  static GType g_define_type_id = 0; \
+  if (G_UNLIKELY (g_define_type_id == 0)) \
+    { \
+      g_define_type_id = \
+        g_type_register_static_simple (TYPE_PARENT, \
+                                       g_intern_static_string (#TypeName), \
+                                       sizeof (TypeName##Class), \
+                                       (GClassInitFunc)type_name##_class_intern_init, \
+                                       sizeof (TypeName), \
+                                       (GInstanceInitFunc)type_name##_init, \
+                                       (GTypeFlags) 0); \
+    }					\
+  return g_define_type_id;		\
+} /* closes type_name##_get_type() */
+
+#endif
+
 /* Safer ways to work with static buffers. When using non-static
  * buffers, either use g_strdup_* functions (preferred) or use
  * g_strlcpy/g_strlcpy directly. */


More information about the Commits mailing list