Commit 82d914d8 authored by Dan Winship's avatar Dan Winship

gio: add g_async_result_is_tagged()

Rather than doing a two step first-check-the-GAsyncResult-subtype-then-
check-the-tag, add a GAsyncResult-level method so that you can do them
both at once, simplifying the code for "short-circuit" async return
values where the vmethod never gets called.

https://bugzilla.gnome.org/show_bug.cgi?id=661767
parent f8532a13
......@@ -1199,6 +1199,7 @@ GAsyncResultIface
GAsyncReadyCallback
g_async_result_get_user_data
g_async_result_get_source_object
g_async_result_is_tagged
g_async_result_legacy_propagate_error
<SUBSECTION Standard>
G_ASYNC_RESULT
......
......@@ -194,3 +194,32 @@ g_async_result_legacy_propagate_error (GAsyncResult *res,
else
return FALSE;
}
/**
* g_async_result_is_tagged:
* @result: a #GAsyncResult
* @source_tag: an application-defined tag
*
* Checks if @result has the given @source_tag (generally a function
* pointer indicating the function @result was created by).
*
* Returns: %TRUE if @result has the indicated @source_tag, %FALSE if
* not.
*
* Since: 2.34
**/
gboolean
g_async_result_is_tagged (GAsyncResult *res,
gpointer source_tag)
{
GAsyncResultIface *iface;
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
iface = G_ASYNC_RESULT_GET_IFACE (res);
if (!iface->is_tagged)
return FALSE;
return (* iface->is_tagged) (res, source_tag);
}
......@@ -50,6 +50,7 @@ typedef struct _GAsyncResultIface GAsyncResultIface;
* @g_iface: The parent interface.
* @get_user_data: Gets the user data passed to the callback.
* @get_source_object: Gets the source object that issued the asynchronous operation.
* @is_tagged: Checks if a result is tagged with a particular source.
*
* Interface definition for #GAsyncResult.
**/
......@@ -61,6 +62,9 @@ struct _GAsyncResultIface
gpointer (* get_user_data) (GAsyncResult *res);
GObject * (* get_source_object) (GAsyncResult *res);
gboolean (* is_tagged) (GAsyncResult *res,
gpointer tag);
};
GType g_async_result_get_type (void) G_GNUC_CONST;
......@@ -71,6 +75,9 @@ GObject *g_async_result_get_source_object (GAsyncResult *res);
GLIB_AVAILABLE_IN_2_34
gboolean g_async_result_legacy_propagate_error (GAsyncResult *res,
GError **error);
GLIB_AVAILABLE_IN_2_34
gboolean g_async_result_is_tagged (GAsyncResult *res,
gpointer source_tag);
G_END_DECLS
......
......@@ -542,7 +542,6 @@ g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
GBufferedInputStreamClass *class;
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
......@@ -550,14 +549,10 @@ g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
if (g_async_result_legacy_propagate_error (result, error))
return -1;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_buffered_input_stream_fill_async))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Special case read of 0 bytes */
if (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_fill_async)
return 0;
return 0;
}
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
......
......@@ -400,21 +400,16 @@ g_file_enumerator_next_files_finish (GFileEnumerator *enumerator,
GError **error)
{
GFileEnumeratorClass *class;
GSimpleAsyncResult *simple;
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
if (g_async_result_legacy_propagate_error (result, error))
return NULL;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
else if (g_async_result_is_tagged (result, g_file_enumerator_next_files_async))
{
/* Special case read of 0 files */
if (g_simple_async_result_get_source_tag (simple) == g_file_enumerator_next_files_async)
return NULL;
return NULL;
}
class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
......
......@@ -649,7 +649,6 @@ g_input_stream_read_finish (GInputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
GInputStreamClass *class;
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
......@@ -657,14 +656,10 @@ g_input_stream_read_finish (GInputStream *stream,
if (g_async_result_legacy_propagate_error (result, error))
return -1;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_input_stream_read_async))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Special case read of 0 bytes */
if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
return 0;
return 0;
}
class = G_INPUT_STREAM_GET_CLASS (stream);
......@@ -889,7 +884,6 @@ g_input_stream_skip_finish (GInputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
GInputStreamClass *class;
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
......@@ -897,14 +891,10 @@ g_input_stream_skip_finish (GInputStream *stream,
if (g_async_result_legacy_propagate_error (result, error))
return -1;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_input_stream_skip_async))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Special case skip of 0 bytes */
if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
return 0;
return 0;
}
class = G_INPUT_STREAM_GET_CLASS (stream);
......@@ -988,7 +978,6 @@ g_input_stream_close_finish (GInputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
GInputStreamClass *class;
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
......@@ -996,14 +985,10 @@ g_input_stream_close_finish (GInputStream *stream,
if (g_async_result_legacy_propagate_error (result, error))
return FALSE;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_input_stream_close_async))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Special case already closed */
if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
return TRUE;
return TRUE;
}
class = G_INPUT_STREAM_GET_CLASS (stream);
......
......@@ -119,6 +119,7 @@ g_desktop_app_info_set_desktop_env
g_async_result_get_type
g_async_result_get_user_data
g_async_result_get_source_object
g_async_result_is_tagged
g_async_result_legacy_propagate_error
g_buffered_input_stream_get_type
g_buffered_input_stream_new
......
......@@ -515,7 +515,6 @@ g_io_stream_close_finish (GIOStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
GIOStreamClass *class;
g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
......@@ -523,14 +522,10 @@ g_io_stream_close_finish (GIOStream *stream,
if (g_async_result_legacy_propagate_error (result, error))
return FALSE;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_io_stream_close_async))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Special case already closed */
if (g_simple_async_result_get_source_tag (simple) == g_io_stream_close_async)
return TRUE;
return TRUE;
}
class = G_IO_STREAM_GET_CLASS (stream);
......
......@@ -835,7 +835,6 @@ g_output_stream_write_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
GOutputStreamClass *class;
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
......@@ -843,14 +842,10 @@ g_output_stream_write_finish (GOutputStream *stream,
if (g_async_result_legacy_propagate_error (result, error))
return -1;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_output_stream_write_async))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Special case writes of 0 bytes */
if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
return 0;
return 0;
}
class = G_OUTPUT_STREAM_GET_CLASS (stream);
......@@ -1161,7 +1156,6 @@ g_output_stream_flush_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
GOutputStreamClass *klass;
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
......@@ -1169,14 +1163,10 @@ g_output_stream_flush_finish (GOutputStream *stream,
if (g_async_result_legacy_propagate_error (result, error))
return FALSE;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_output_stream_flush_async))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Special case default implementation */
if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
return TRUE;
return 0;
}
klass = G_OUTPUT_STREAM_GET_CLASS (stream);
......@@ -1284,7 +1274,6 @@ g_output_stream_close_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
GOutputStreamClass *class;
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
......@@ -1292,14 +1281,10 @@ g_output_stream_close_finish (GOutputStream *stream,
if (g_async_result_legacy_propagate_error (result, error))
return FALSE;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_output_stream_close_async))
{
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Special case already closed */
if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
return TRUE;
return TRUE;
}
class = G_OUTPUT_STREAM_GET_CLASS (stream);
......
......@@ -447,19 +447,14 @@ g_resolver_lookup_by_name_finish (GResolver *resolver,
if (g_async_result_legacy_propagate_error (result, error))
return NULL;
if (G_IS_SIMPLE_ASYNC_RESULT (result))
else if (g_async_result_is_tagged (result, g_resolver_lookup_by_name_async))
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
GInetAddress *addr;
/* Handle the stringified-IP-addr case */
if (g_simple_async_result_get_source_tag (simple) == g_resolver_lookup_by_name_async)
{
GInetAddress *addr;
addr = g_simple_async_result_get_op_res_gpointer (simple);
return g_list_append (NULL, g_object_ref (addr));
}
addr = g_simple_async_result_get_op_res_gpointer (simple);
return g_list_append (NULL, g_object_ref (addr));
}
addrs = G_RESOLVER_GET_CLASS (resolver)->
......
......@@ -457,11 +457,19 @@ g_simple_async_result_get_source_object (GAsyncResult *res)
return NULL;
}
static gboolean
g_simple_async_result_is_tagged (GAsyncResult *res,
gpointer source_tag)
{
return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
}
static void
g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
{
iface->get_user_data = g_simple_async_result_get_user_data;
iface->get_source_object = g_simple_async_result_get_source_object;
iface->is_tagged = g_simple_async_result_is_tagged;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment