GSimpleAsyncResult: add error-taking variants
authorMatthias Clasen <mclasen@redhat.com>
Mon, 1 Nov 2010 02:38:38 +0000 (22:38 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 1 Nov 2010 02:38:38 +0000 (22:38 -0400)
Add g_simple_async_result_new_take_error and
g_simple_async_result_take_error, which take over ownership of the
given error. Based on a patch by Christian Persch.

https://bugzilla.gnome.org/show_bug.cgi?id=629247

gio/gio.symbols
gio/gsimpleasyncresult.c
gio/gsimpleasyncresult.h

index b8fa2a5..19fa68c 100644 (file)
@@ -790,6 +790,7 @@ g_simple_async_result_get_type  G_GNUC_CONST
 g_simple_async_result_new
 g_simple_async_result_new_error
 g_simple_async_result_new_from_error
+g_simple_async_result_new_take_error
 g_simple_async_result_set_op_res_gpointer
 g_simple_async_result_get_op_res_gpointer
 g_simple_async_result_set_op_res_gssize
@@ -802,6 +803,7 @@ g_simple_async_result_complete
 g_simple_async_result_complete_in_idle
 g_simple_async_result_run_in_thread
 g_simple_async_result_set_from_error
+g_simple_async_result_take_error
 g_simple_async_result_propagate_error
 g_simple_async_result_set_error
 g_simple_async_result_set_error_va
index c9563f4..eaada17 100644 (file)
@@ -72,7 +72,8 @@
  *
  * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
  * If the result needs to be created for a #GError, use
- * g_simple_async_result_new_from_error(). If a #GError is not available
+ * g_simple_async_result_new_from_error() or
+ * g_simple_async_result_new_take_error(). If a #GError is not available
  * (e.g. the asynchronous operation's doesn't take a #GError argument),
  * but the result still needs to be created for an error condition, use
  * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
@@ -335,7 +336,7 @@ g_simple_async_result_new (GObject             *source_object,
  * @source_object: a #GObject, or %NULL.
  * @callback: a #GAsyncReadyCallback.
  * @user_data: user data passed to @callback.
- * @error: a #GError location.
+ * @error: a #GError
  *
  * Creates a #GSimpleAsyncResult from an error condition.
  *
@@ -345,7 +346,7 @@ GSimpleAsyncResult *
 g_simple_async_result_new_from_error (GObject             *source_object,
                                       GAsyncReadyCallback  callback,
                                       gpointer             user_data,
-                                      GError              *error)
+                                      const GError        *error)
 {
   GSimpleAsyncResult *simple;
 
@@ -360,6 +361,38 @@ g_simple_async_result_new_from_error (GObject             *source_object,
 }
 
 /**
+ * g_simple_async_result_new_take_error:
+ * @source_object: (allow-none): a #GObject, or %NULL
+ * @callback: a #GAsyncReadyCallback
+ * @user_data: (allow-none): user data passed to @callback
+ * @error: a #GError
+ *
+ * Creates a #GSimpleAsyncResult from an error condition, and takes over the
+ * caller's ownership of @error, so the caller does not need to free it anymore.
+ *
+ * Returns: a #GSimpleAsyncResult
+ *
+ * Since: 2.26
+ **/
+GSimpleAsyncResult *
+g_simple_async_result_new_take_error (GObject             *source_object,
+                                      GAsyncReadyCallback  callback,
+                                      gpointer             user_data,
+                                      GError              *error)
+{
+  GSimpleAsyncResult *simple;
+
+  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
+
+  simple = g_simple_async_result_new (source_object,
+                                     callback,
+                                     user_data, NULL);
+  g_simple_async_result_take_error (simple, error);
+
+  return simple;
+}
+
+/**
  * g_simple_async_result_new_error:
  * @source_object: a #GObject, or %NULL.
  * @callback: a #GAsyncReadyCallback.
@@ -599,6 +632,29 @@ g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
 }
 
 /**
+ * g_simple_async_result_take_error:
+ * @simple: a #GSimpleAsyncResult
+ * @error: a #GError
+ *
+ * Sets the result from @error, and takes over the caller's ownership
+ * of @error, so the caller does not need to free it any more.
+ *
+ * Since: 2.26
+ **/
+void
+g_simple_async_result_take_error (GSimpleAsyncResult *simple,
+                                  GError             *error)
+{
+  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
+  g_return_if_fail (error != NULL);
+
+  if (simple->error)
+    g_error_free (simple->error);
+  simple->error = error;
+  simple->failed = TRUE;
+}
+
+/**
  * g_simple_async_result_set_error_va:
  * @simple: a #GSimpleAsyncResult.
  * @domain: a #GQuark (usually #G_IO_ERROR).
index 073796c..0abbd67 100644 (file)
@@ -62,6 +62,10 @@ GSimpleAsyncResult *g_simple_async_result_new_error        (GObject
 GSimpleAsyncResult *g_simple_async_result_new_from_error   (GObject                 *source_object,
                                                            GAsyncReadyCallback      callback,
                                                            gpointer                 user_data,
+                                                           const GError            *error);
+GSimpleAsyncResult *g_simple_async_result_new_take_error   (GObject                 *source_object,
+                                                           GAsyncReadyCallback      callback,
+                                                           gpointer                 user_data,
                                                            GError                  *error);
 
 void                g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult      *simple,
@@ -90,6 +94,8 @@ void                g_simple_async_result_run_in_thread    (GSimpleAsyncResult
                                                            GCancellable            *cancellable);
 void                g_simple_async_result_set_from_error   (GSimpleAsyncResult      *simple,
                                                            const GError            *error);
+void                g_simple_async_result_take_error       (GSimpleAsyncResult      *simple,
+                                                           GError            *error);
 gboolean            g_simple_async_result_propagate_error  (GSimpleAsyncResult      *simple,
                                                            GError                 **dest);
 void                g_simple_async_result_set_error        (GSimpleAsyncResult      *simple,