soc.2010.detachablepurple: d784e773: I'm sick and tired of the constructor ba...

gillux at soc.pidgin.im gillux at soc.pidgin.im
Sun Aug 8 11:14:05 EDT 2010


----------------------------------------------------------------------
Revision: d784e7736b27d02ad23a9b516d4a76f6fbc61e8f
Parent:   e83dd268f180ec4c05aa313ce8db03da528814ad
Author:   gillux at soc.pidgin.im
Date:     08/06/10 13:36:15
Branch:   im.pidgin.soc.2010.detachablepurple
URL: http://d.pidgin.im/viewmtn/revision/info/d784e7736b27d02ad23a9b516d4a76f6fbc61e8f

Changelog: 

I'm sick and tired of the constructor based singleton. It requires to
g_object_new() and g_object_unref() everywhere, so I go for a more
efficient and easy to use _init(), _get_instance(), _uninit() system.

Changes against parent e83dd268f180ec4c05aa313ce8db03da528814ad

  patched  libpurple/dbus/callback.c
  patched  libpurple/dbus/callback.h
  patched  libpurple/dbus-server.c
  patched  purpled/purpled-account.c

-------------- next part --------------
============================================================
--- libpurple/dbus-server.c	10f2def749c56e3d6aaaec6fe29fdf8264556d69
+++ libpurple/dbus-server.c	31b8441d043abaa2aaac3714925f4ab349460266
@@ -822,8 +822,8 @@ purple_dbus_g_init(void)
 	}
 
 	/* In remote and daemon mode, we create our singleton objects */
-	/* Instantiate a PurpleDBusCallback */
-	g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	/* Instantiate the PurpleDBusCallback. */
+	purple_dbus_callback_do_init();
 
 	/* Instantiate and publish the PurpleConstructor */
 	purple_constructor_get_instance();
@@ -1021,6 +1021,8 @@ purple_dbus_g_uninit(void)
 purple_dbus_g_uninit(void)
 {
 	g_hash_table_destroy(dbus_path_gobjects);
+	/* Destroy the PurpleDBusCallback. */
+	purple_dbus_callback_do_uninit();
 }
 
 void
============================================================
--- libpurple/dbus/callback.c	90e4b24b5e62b23d97180306b2020447a6180b3c
+++ libpurple/dbus/callback.c	11cc27cea867ac8c915274a33433d418f1f78701
@@ -75,19 +75,34 @@ static void request_closed_cb(DBusGProxy
 static void run_callback_cb(DBusGProxy *proxy, const guint64 callback_id, GPtrArray *params, gpointer data);
 static void request_closed_cb(DBusGProxy *proxy, const guint request_id, gpointer data);
 
-/* This constructor make the singleton thing, using the class field "instance"
- * to store the unique instance of PurpleDBusCallback */
-static GObject*
-purple_dbus_callback_constructor(GType type, guint n_params,
-                                 GObjectConstructParam *params)
+void
+purple_dbus_callback_do_init(void)
 {
+	purple_dbus_callback_get_instance();
+}
+
+void
+purple_dbus_callback_do_uninit(void)
+{
+	PurpleDBusCallbackClass *klass;
+
+	klass = g_type_class_peek(PURPLE_TYPE_DBUS_CALLBACK);
+	g_return_if_fail(klass->instance != NULL);
+	g_object_unref(klass->instance);
+}
+
+PurpleDBusCallback*
+purple_dbus_callback_get_instance(void)
+{
 	DBusGProxy* proxy;
 	PurpleDBusCallbackClass *klass;
+	PurpleDBusCallback *instance;
 
 	klass = g_type_class_peek(PURPLE_TYPE_DBUS_CALLBACK);
-	if (!klass->instance) {
-		/* Call parent constructor */
-		klass->instance = G_OBJECT_CLASS(purple_dbus_callback_parent_class)->constructor(type, n_params, params);
+	if (!klass) {
+		instance = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+		klass = g_type_class_peek(PURPLE_TYPE_DBUS_CALLBACK);
+		klass->instance = instance;
 
 		/* Register this object on DBus */
 		purple_object_install_dbus_infos(PURPLE_OBJECT(klass->instance),
@@ -110,8 +125,6 @@ purple_dbus_callback_constructor(GType t
 			                        G_CALLBACK(request_closed_cb),
 			                        klass->instance, NULL);
 		}
-	} else {
-		klass->instance = g_object_ref(klass->instance);
 	}
 
 	return klass->instance;
@@ -137,7 +150,6 @@ purple_dbus_callback_class_init(PurpleDB
 {
 	GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
 
-	gobject_class->constructor  = purple_dbus_callback_constructor;
 	gobject_class->finalize     = purple_dbus_callback_finalize;
 	klass->instance = NULL;
 
@@ -191,7 +203,7 @@ purple_dbus_callback_new_id(void)
 guint64
 purple_dbus_callback_new_id(void)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	return ++handler->last_callback_id;
 }
 
@@ -233,7 +245,7 @@ purple_dbus_callback__b(void *obj, gbool
 void
 purple_dbus_callback__b(void *obj, gboolean arg1, guint64 *id)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	GPtrArray* params = build_args_list(1, G_TYPE_BOOLEAN, arg1);
 	g_signal_emit(handler, signals[SIG_DBUS_RUN_CALLBACK], 0, *id, params);
 	g_ptr_array_foreach(params, (GFunc)g_value_unset, NULL);
@@ -247,7 +259,7 @@ run_callback_cb(DBusGProxy *proxy, const
 static void
 run_callback_cb(DBusGProxy *proxy, const guint64 callback_id, GPtrArray *params, gpointer data)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	GClosure *closure;
 	GObject *obj;
 	GValue *values;
@@ -295,7 +307,7 @@ purple_dbus_callback_register(guint64 id
 void
 purple_dbus_callback_register(guint64 id, GClosure *closure)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	guint64 *new_id;
 
 	g_return_if_fail(closure != NULL);
@@ -315,7 +327,7 @@ purple_callback_add_req(guint request_id
 static void
 purple_callback_add_req(guint request_id, guint n_closures, va_list ap)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	GPtrArray *closures;
 	int i;
 	void *key;
@@ -337,7 +349,7 @@ purple_callback_register_req(guint n_clo
 guint
 purple_callback_register_req(guint n_closures, ...)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	va_list ap;
 
 	g_return_val_if_fail(handler != NULL, 0);
@@ -352,7 +364,7 @@ purple_callback_register_req_id(guint re
 void
 purple_callback_register_req_id(guint request_id, GClosure *close, void *handle)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	PurpleDBusClientPendingRequest *req;
 	void *key = GINT_TO_POINTER(request_id);
 
@@ -369,7 +381,7 @@ purple_callback_run_request_RPC(guint re
 void
 purple_callback_run_request_RPC(guint request_id, guint choice)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	DBusGProxy* proxy;
 	GError *error = NULL;
 
@@ -388,7 +400,7 @@ purple_callback_run_request(guint reques
 gboolean
 purple_callback_run_request(guint request_id, guint choice)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	GPtrArray *closures;
 	GClosure *closure;
 	void *key = GINT_TO_POINTER(request_id);
@@ -425,7 +437,7 @@ purple_callback_close_client_req(guint r
 void
 purple_callback_close_client_req(guint request_id)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	PurpleDBusClientPendingRequest *req;
 	void *key = GINT_TO_POINTER(request_id);
 
@@ -455,7 +467,7 @@ purple_callback_get_req_id(void *ui_hand
 guint
 purple_callback_get_req_id(void *ui_handle)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	PurpleDBusClientPendingRequest *req;
 
 	g_return_val_if_fail(handler != NULL, 0);
============================================================
--- libpurple/dbus/callback.h	d4c1e33f673812ec72c5c93e27748aa78dc6b2d0
+++ libpurple/dbus/callback.h	ec53567a1a2d96588acbb572377643dd3b1bde4d
@@ -33,11 +33,6 @@ typedef struct {
 GType purple_dbus_callback_get_type(void);
 
 typedef struct {
-	PurpleObjectClass parent;
-	GObject *instance;
-} PurpleDBusCallbackClass;
-
-typedef struct {
 	GClosure *close;
 	void *handle;
 	/* Request ids are already the keys for the client_reqs hash table,
@@ -57,11 +52,31 @@ typedef struct {
 	GHashTable *client_reqs; /* id -> PurpleDBusClientPendingRequest */
 } PurpleDBusCallback;
 
+typedef struct {
+	PurpleObjectClass parent;
+	PurpleDBusCallback *instance;
+} PurpleDBusCallbackClass;
+
 /* DBus transfered data formats */
 #define DBUS_GVALUE_ARRAY \
 	dbus_g_type_get_collection("GPtrArray",	G_TYPE_VALUE)
 
 /**
+ * Creates the PurpleDBusCallback singleton.
+ */
+void purple_dbus_callback_do_init(void);
+
+/**
+ * Destroys the PurpleDBusCallback singleton.
+ */
+void purple_dbus_callback_do_uninit(void);
+
+/**
+ * Gets the PurpleDBusCallback singleton.
+ */
+PurpleDBusCallback* purple_dbus_callback_get_instance(void);
+
+/**
  * Generates a new unique callback id. Only used in daemon mode.
  *
  * @return The new unique callback id.
============================================================
--- purpled/purpled-account.c	fd94f2e1d101e208c25275570432b25e6e17d7ce
+++ purpled/purpled-account.c	fc647ab9bbe955b39ea36dfc04aa9ac54ee029d8
@@ -54,7 +54,7 @@ purpled_close_account_request(void *ui_h
 static void
 purpled_close_account_request(void *ui_handle)
 {
-	PurpleDBusCallback* handler = g_object_new(PURPLE_TYPE_DBUS_CALLBACK, NULL);
+	PurpleDBusCallback* handler = purple_dbus_callback_get_instance();
 	guint req_id = GPOINTER_TO_INT(ui_handle);
 
 	g_return_if_fail(req_id > 0);


More information about the Commits mailing list