[glib] gio: add g_async_result_is_tagged()
- From: Dan Winship <danw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] gio: add g_async_result_is_tagged()
- Date: Tue, 10 Jul 2012 14:50:49 +0000 (UTC)
commit 82d914d808c616d14d489c0272c6d5afc4bfbd5a
Author: Dan Winship <danw gnome org>
Date: Thu May 10 11:09:52 2012 -0400
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
docs/reference/gio/gio-sections.txt | 1 +
gio/gasyncresult.c | 29 +++++++++++++++++++++++++++++
gio/gasyncresult.h | 7 +++++++
gio/gbufferedinputstream.c | 9 ++-------
gio/gfileenumerator.c | 11 +++--------
gio/ginputstream.c | 27 ++++++---------------------
gio/gio.symbols | 1 +
gio/giostream.c | 9 ++-------
gio/goutputstream.c | 27 ++++++---------------------
gio/gresolver.c | 13 ++++---------
gio/gsimpleasyncresult.c | 8 ++++++++
11 files changed, 69 insertions(+), 73 deletions(-)
---
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index f0d4990..e709e83 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -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
diff --git a/gio/gasyncresult.c b/gio/gasyncresult.c
index b578efc..8cba495 100644
--- a/gio/gasyncresult.c
+++ b/gio/gasyncresult.c
@@ -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);
+}
diff --git a/gio/gasyncresult.h b/gio/gasyncresult.h
index 773d57e..e898401 100644
--- a/gio/gasyncresult.h
+++ b/gio/gasyncresult.h
@@ -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
diff --git a/gio/gbufferedinputstream.c b/gio/gbufferedinputstream.c
index b8e1b11..dd98d98 100644
--- a/gio/gbufferedinputstream.c
+++ b/gio/gbufferedinputstream.c
@@ -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);
diff --git a/gio/gfileenumerator.c b/gio/gfileenumerator.c
index b4f7424..a6db242 100644
--- a/gio/gfileenumerator.c
+++ b/gio/gfileenumerator.c
@@ -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);
diff --git a/gio/ginputstream.c b/gio/ginputstream.c
index db90aeb..7ad7d6f 100644
--- a/gio/ginputstream.c
+++ b/gio/ginputstream.c
@@ -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);
diff --git a/gio/gio.symbols b/gio/gio.symbols
index 4025da8..6db9bbd 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -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
diff --git a/gio/giostream.c b/gio/giostream.c
index d2f40c4..574eea3 100644
--- a/gio/giostream.c
+++ b/gio/giostream.c
@@ -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);
diff --git a/gio/goutputstream.c b/gio/goutputstream.c
index a619c05..aaf82f5 100644
--- a/gio/goutputstream.c
+++ b/gio/goutputstream.c
@@ -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);
diff --git a/gio/gresolver.c b/gio/gresolver.c
index 22f815f..1785543 100644
--- a/gio/gresolver.c
+++ b/gio/gresolver.c
@@ -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)->
diff --git a/gio/gsimpleasyncresult.c b/gio/gsimpleasyncresult.c
index 1b95119..cd56b2a 100644
--- a/gio/gsimpleasyncresult.c
+++ b/gio/gsimpleasyncresult.c
@@ -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;
}
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]