[gnome-builder] glib: add ide_g_task_return_error_from_main()



commit 59629b750d93fe06210e4da54e6cb3cbcd4afae0
Author: Christian Hergert <chergert redhat com>
Date:   Thu Sep 15 12:07:47 2016 -0700

    glib: add ide_g_task_return_error_from_main()
    
    Helper like the others but for returning an error.

 libide/util/ide-glib.c |   34 ++++++++++++++++++++++++++++++++++
 libide/util/ide-glib.h |    2 ++
 2 files changed, 36 insertions(+), 0 deletions(-)
---
diff --git a/libide/util/ide-glib.c b/libide/util/ide-glib.c
index 36eecaa..5599a90 100644
--- a/libide/util/ide-glib.c
+++ b/libide/util/ide-glib.c
@@ -27,6 +27,7 @@ typedef struct
   union {
     gboolean v_bool;
     gint v_int;
+    GError *v_error;
     struct {
       gpointer pointer;
       GDestroyNotify destroy;
@@ -51,9 +52,18 @@ do_return (gpointer user_data)
 
     case G_TYPE_POINTER:
       g_task_return_pointer (state->task, state->u.v_ptr.pointer, state->u.v_ptr.destroy);
+      state->u.v_ptr.pointer = NULL;
+      state->u.v_ptr.destroy = NULL;
       break;
 
     default:
+      if (state->type == G_TYPE_ERROR)
+        {
+          g_task_return_error (state->task, state->u.v_error);
+          state->u.v_error = NULL;
+          break;
+        }
+
       g_assert_not_reached ();
     }
 
@@ -137,3 +147,27 @@ ide_g_task_return_pointer_from_main (GTask          *task,
 
   task_state_attach (state);
 }
+
+/**
+ * ide_g_task_return_error_from_main:
+ * @task: A #GTask
+ * @error: (transfer full): A #GError.
+ *
+ * Like g_task_return_error() but ensures we return to the main loop before
+ * dispatching the result.
+ */
+void
+ide_g_task_return_error_from_main (GTask  *task,
+                                   GError *error)
+{
+  TaskState *state;
+
+  g_return_if_fail (G_IS_TASK (task));
+
+  state = g_slice_new0 (TaskState);
+  state->type = G_TYPE_ERROR;
+  state->task = g_object_ref (task);
+  state->u.v_error = error;
+
+  task_state_attach (state);
+}
diff --git a/libide/util/ide-glib.h b/libide/util/ide-glib.h
index a43f6b3..971bb18 100644
--- a/libide/util/ide-glib.h
+++ b/libide/util/ide-glib.h
@@ -30,6 +30,8 @@ void ide_g_task_return_int_from_main     (GTask          *task,
 void ide_g_task_return_pointer_from_main (GTask          *task,
                                           gpointer        value,
                                           GDestroyNotify  notify);
+void ide_g_task_return_error_from_main   (GTask          *task,
+                                          GError         *error);
 
 G_END_DECLS
 


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