GSimpleAsyncResult: add error-taking variants

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
This commit is contained in:
Matthias Clasen 2010-10-31 22:38:38 -04:00
parent 7887103193
commit 85ba8596e2
3 changed files with 67 additions and 3 deletions

View 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

View 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.
*
@ -343,6 +344,38 @@ g_simple_async_result_new (GObject *source_object,
**/
GSimpleAsyncResult *
g_simple_async_result_new_from_error (GObject *source_object,
GAsyncReadyCallback callback,
gpointer user_data,
const 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_set_from_error (simple, error);
return simple;
}
/**
* 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)
@ -354,7 +387,7 @@ g_simple_async_result_new_from_error (GObject *source_object,
simple = g_simple_async_result_new (source_object,
callback,
user_data, NULL);
g_simple_async_result_set_from_error (simple, error);
g_simple_async_result_take_error (simple, error);
return simple;
}
@ -598,6 +631,29 @@ g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
simple->failed = TRUE;
}
/**
* 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.

View File

@ -60,6 +60,10 @@ GSimpleAsyncResult *g_simple_async_result_new_error (GObject
const char *format,
...) G_GNUC_PRINTF (6, 7);
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);
@ -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,