[grilo] core: Add grl_operation_set_data_full()



commit 67f4b69ad785bd55f2aefd01b857c5ec2da3cba0
Author: Emanuele Aina <emanuele aina collabora com>
Date:   Mon Aug 12 20:14:28 2013 +0200

    core: Add grl_operation_set_data_full()
    
    Let users specify a destroy function to offload to Grilo the management
    of the registered data lifecycle.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=705944

 src/grl-operation.c |   26 ++++++++++++++++++++++++++
 src/grl-operation.h |    2 ++
 2 files changed, 28 insertions(+), 0 deletions(-)
---
diff --git a/src/grl-operation.c b/src/grl-operation.c
index 222998d..c815154 100644
--- a/src/grl-operation.c
+++ b/src/grl-operation.c
@@ -30,6 +30,7 @@ typedef struct
   GDestroyNotify       destroy_cb;
   gpointer             private_data;
   gpointer             user_data;
+  GDestroyNotify       user_data_destroy_func;
 } OperationData;
 
 static guint       operations_id;
@@ -38,6 +39,11 @@ static GHashTable *operations;
 static void
 operation_data_free (OperationData *data)
 {
+  if (data->user_data_destroy_func && data->user_data) {
+    data->user_data_destroy_func (data->user_data);
+    data->user_data = NULL;
+  }
+
   if (data->destroy_cb) {
     data->destroy_cb (data->private_data);
   }
@@ -166,13 +172,33 @@ grl_operation_get_data (guint operation_id)
 void
 grl_operation_set_data (guint operation_id, gpointer user_data)
 {
+  grl_operation_set_data_full (operation_id, user_data, NULL);
+}
+
+/**
+ * grl_operation_set_data_full:
+ * @operation_id: the identifier of a running operation
+ * @user_data: (allow-none): the data to attach
+ * @destroy_func: (allow-none): function to release @user_data when the operation terminates
+ *
+ * Attach a pointer to the specific operation.
+ *
+ * Note that the @destroy_func callback is not called if @user_data is %NULL.
+ */
+void
+grl_operation_set_data_full (guint operation_id, gpointer user_data, GDestroyNotify destroy_func)
+{
   OperationData *data = g_hash_table_lookup (operations,
                                              GUINT_TO_POINTER (operation_id));
 
   if (!data) {
     GRL_WARNING ("Invalid operation %u", operation_id);
   } else {
+    if (data->user_data_destroy_func && data->user_data)
+      data->user_data_destroy_func (data->user_data);
+
     data->user_data = user_data;
+    data->user_data_destroy_func = destroy_func;
   }
 }
 
diff --git a/src/grl-operation.h b/src/grl-operation.h
index c96356c..715278f 100644
--- a/src/grl-operation.h
+++ b/src/grl-operation.h
@@ -37,6 +37,8 @@ gpointer grl_operation_get_data (guint operation_id);
 
 void grl_operation_set_data (guint operation_id, gpointer user_data);
 
+void grl_operation_set_data_full (guint operation_id, gpointer user_data, GDestroyNotify destroy_func);
+
 G_END_DECLS
 
 #endif /* _GRL_OPERATION_H_ */


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