mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-20 07:38:54 +02:00
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
This commit is contained in:
@@ -1199,6 +1199,7 @@ GAsyncResultIface
|
|||||||
GAsyncReadyCallback
|
GAsyncReadyCallback
|
||||||
g_async_result_get_user_data
|
g_async_result_get_user_data
|
||||||
g_async_result_get_source_object
|
g_async_result_get_source_object
|
||||||
|
g_async_result_is_tagged
|
||||||
g_async_result_legacy_propagate_error
|
g_async_result_legacy_propagate_error
|
||||||
<SUBSECTION Standard>
|
<SUBSECTION Standard>
|
||||||
G_ASYNC_RESULT
|
G_ASYNC_RESULT
|
||||||
|
@@ -194,3 +194,32 @@ g_async_result_legacy_propagate_error (GAsyncResult *res,
|
|||||||
else
|
else
|
||||||
return FALSE;
|
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.
|
* @g_iface: The parent interface.
|
||||||
* @get_user_data: Gets the user data passed to the callback.
|
* @get_user_data: Gets the user data passed to the callback.
|
||||||
* @get_source_object: Gets the source object that issued the asynchronous operation.
|
* @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.
|
* Interface definition for #GAsyncResult.
|
||||||
**/
|
**/
|
||||||
@@ -61,6 +62,9 @@ struct _GAsyncResultIface
|
|||||||
|
|
||||||
gpointer (* get_user_data) (GAsyncResult *res);
|
gpointer (* get_user_data) (GAsyncResult *res);
|
||||||
GObject * (* get_source_object) (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;
|
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
|
GLIB_AVAILABLE_IN_2_34
|
||||||
gboolean g_async_result_legacy_propagate_error (GAsyncResult *res,
|
gboolean g_async_result_legacy_propagate_error (GAsyncResult *res,
|
||||||
GError **error);
|
GError **error);
|
||||||
|
GLIB_AVAILABLE_IN_2_34
|
||||||
|
gboolean g_async_result_is_tagged (GAsyncResult *res,
|
||||||
|
gpointer source_tag);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
|
@@ -542,7 +542,6 @@ g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
|
|||||||
GAsyncResult *result,
|
GAsyncResult *result,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
GBufferedInputStreamClass *class;
|
GBufferedInputStreamClass *class;
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return -1;
|
return -1;
|
||||||
|
else if (g_async_result_is_tagged (result, g_buffered_input_stream_fill_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case read of 0 bytes */
|
/* 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);
|
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
|
||||||
|
@@ -400,21 +400,16 @@ g_file_enumerator_next_files_finish (GFileEnumerator *enumerator,
|
|||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GFileEnumeratorClass *class;
|
GFileEnumeratorClass *class;
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
|
g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
|
||||||
g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
|
g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
|
||||||
|
|
||||||
if (g_async_result_legacy_propagate_error (result, error))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
else if (g_async_result_is_tagged (result, g_file_enumerator_next_files_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
{
|
||||||
{
|
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case read of 0 files */
|
/* 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);
|
class = G_FILE_ENUMERATOR_GET_CLASS (enumerator);
|
||||||
|
@@ -649,7 +649,6 @@ g_input_stream_read_finish (GInputStream *stream,
|
|||||||
GAsyncResult *result,
|
GAsyncResult *result,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
GInputStreamClass *class;
|
GInputStreamClass *class;
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return -1;
|
return -1;
|
||||||
|
else if (g_async_result_is_tagged (result, g_input_stream_read_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case read of 0 bytes */
|
/* 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);
|
class = G_INPUT_STREAM_GET_CLASS (stream);
|
||||||
@@ -889,7 +884,6 @@ g_input_stream_skip_finish (GInputStream *stream,
|
|||||||
GAsyncResult *result,
|
GAsyncResult *result,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
GInputStreamClass *class;
|
GInputStreamClass *class;
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return -1;
|
return -1;
|
||||||
|
else if (g_async_result_is_tagged (result, g_input_stream_skip_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case skip of 0 bytes */
|
/* 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);
|
class = G_INPUT_STREAM_GET_CLASS (stream);
|
||||||
@@ -988,7 +978,6 @@ g_input_stream_close_finish (GInputStream *stream,
|
|||||||
GAsyncResult *result,
|
GAsyncResult *result,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
GInputStreamClass *class;
|
GInputStreamClass *class;
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
else if (g_async_result_is_tagged (result, g_input_stream_close_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case already closed */
|
/* 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);
|
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_type
|
||||||
g_async_result_get_user_data
|
g_async_result_get_user_data
|
||||||
g_async_result_get_source_object
|
g_async_result_get_source_object
|
||||||
|
g_async_result_is_tagged
|
||||||
g_async_result_legacy_propagate_error
|
g_async_result_legacy_propagate_error
|
||||||
g_buffered_input_stream_get_type
|
g_buffered_input_stream_get_type
|
||||||
g_buffered_input_stream_new
|
g_buffered_input_stream_new
|
||||||
|
@@ -515,7 +515,6 @@ g_io_stream_close_finish (GIOStream *stream,
|
|||||||
GAsyncResult *result,
|
GAsyncResult *result,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
GIOStreamClass *class;
|
GIOStreamClass *class;
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_IO_STREAM (stream), FALSE);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
else if (g_async_result_is_tagged (result, g_io_stream_close_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case already closed */
|
/* 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);
|
class = G_IO_STREAM_GET_CLASS (stream);
|
||||||
|
@@ -835,7 +835,6 @@ g_output_stream_write_finish (GOutputStream *stream,
|
|||||||
GAsyncResult *result,
|
GAsyncResult *result,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
GOutputStreamClass *class;
|
GOutputStreamClass *class;
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return -1;
|
return -1;
|
||||||
|
else if (g_async_result_is_tagged (result, g_output_stream_write_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case writes of 0 bytes */
|
/* 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);
|
class = G_OUTPUT_STREAM_GET_CLASS (stream);
|
||||||
@@ -1161,7 +1156,6 @@ g_output_stream_flush_finish (GOutputStream *stream,
|
|||||||
GAsyncResult *result,
|
GAsyncResult *result,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
GOutputStreamClass *klass;
|
GOutputStreamClass *klass;
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
else if (g_async_result_is_tagged (result, g_output_stream_flush_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case default implementation */
|
/* Special case default implementation */
|
||||||
if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
|
return 0;
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
klass = G_OUTPUT_STREAM_GET_CLASS (stream);
|
klass = G_OUTPUT_STREAM_GET_CLASS (stream);
|
||||||
@@ -1284,7 +1274,6 @@ g_output_stream_close_finish (GOutputStream *stream,
|
|||||||
GAsyncResult *result,
|
GAsyncResult *result,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple;
|
|
||||||
GOutputStreamClass *class;
|
GOutputStreamClass *class;
|
||||||
|
|
||||||
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
else if (g_async_result_is_tagged (result, g_output_stream_close_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
simple = G_SIMPLE_ASYNC_RESULT (result);
|
|
||||||
|
|
||||||
/* Special case already closed */
|
/* 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);
|
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))
|
if (g_async_result_legacy_propagate_error (result, error))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
else if (g_async_result_is_tagged (result, g_resolver_lookup_by_name_async))
|
||||||
if (G_IS_SIMPLE_ASYNC_RESULT (result))
|
|
||||||
{
|
{
|
||||||
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
|
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
|
||||||
|
GInetAddress *addr;
|
||||||
|
|
||||||
/* Handle the stringified-IP-addr case */
|
/* Handle the stringified-IP-addr case */
|
||||||
if (g_simple_async_result_get_source_tag (simple) == g_resolver_lookup_by_name_async)
|
addr = g_simple_async_result_get_op_res_gpointer (simple);
|
||||||
{
|
return g_list_append (NULL, g_object_ref (addr));
|
||||||
GInetAddress *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)->
|
addrs = G_RESOLVER_GET_CLASS (resolver)->
|
||||||
|
@@ -457,11 +457,19 @@ g_simple_async_result_get_source_object (GAsyncResult *res)
|
|||||||
return NULL;
|
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
|
static void
|
||||||
g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
|
g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
|
||||||
{
|
{
|
||||||
iface->get_user_data = g_simple_async_result_get_user_data;
|
iface->get_user_data = g_simple_async_result_get_user_data;
|
||||||
iface->get_source_object = g_simple_async_result_get_source_object;
|
iface->get_source_object = g_simple_async_result_get_source_object;
|
||||||
|
iface->is_tagged = g_simple_async_result_is_tagged;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user