GIO Reference Manual | ||||
---|---|---|---|---|
Top | Description | Object Hierarchy | Implemented Interfaces |
#include <gio/gio.h> GSimpleAsyncResult; void (*GSimpleAsyncThreadFunc) (GSimpleAsyncResult *res, GObject *object, GCancellable *cancellable); GSimpleAsyncResult * g_simple_async_result_new (GObject *source_object, GAsyncReadyCallback callback, gpointer user_data, gpointer source_tag); GSimpleAsyncResult * g_simple_async_result_new_error (GObject *source_object, GAsyncReadyCallback callback, gpointer user_data, GQuark domain, gint code, const char *format, ...); GSimpleAsyncResult * g_simple_async_result_new_from_error (GObject *source_object, GAsyncReadyCallback callback, gpointer user_data, GError *error); void g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple, gpointer op_res, GDestroyNotify destroy_op_res); gpointer g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple); void g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple, gssize op_res); gssize g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple); void g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple, gboolean op_res); gboolean g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple); gpointer g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple); gboolean g_simple_async_result_is_valid (GAsyncResult *result, GObject *source, gpointer source_tag); void g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple, gboolean handle_cancellation); void g_simple_async_result_complete (GSimpleAsyncResult *simple); void g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple); void g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple, GSimpleAsyncThreadFunc func, int io_priority, GCancellable *cancellable); void g_simple_async_result_set_from_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, GQuark domain, gint code, const char *format, ...); void g_simple_async_result_set_error_va (GSimpleAsyncResult *simple, GQuark domain, gint code, const char *format, va_list args); void g_simple_async_report_error_in_idle (GObject *object, GAsyncReadyCallback callback, gpointer user_data, GQuark domain, gint code, const char *format, ...); void g_simple_async_report_gerror_in_idle (GObject *object, GAsyncReadyCallback callback, gpointer user_data, GError *error);
Implements GAsyncResult for simple cases. Most of the time, this will be all an application needs, and will be used transparently. Because of this, GSimpleAsyncResult is used throughout GIO for handling asynchronous functions.
GSimpleAsyncResult handles GAsyncReadyCallbacks, error reporting, operation cancellation and the final state of an operation, completely transparent to the application. Results can be returned as a pointer e.g. for functions that return data that is collected asynchronously, a boolean value for checking the success or failure of an operation, or a gssize for operations which return the number of bytes modified by the operation; all of the simple return cases are covered.
Most of the time, an application will not need to know of the details of this API; it is handled transparently, and any necessary operations are handled by GAsyncResult's interface. However, if implementing a new GIO module, for writing language bindings, or for complex applications that need better control of how asynchronous operations are completed, it is important to understand this functionality.
GSimpleAsyncResults are tagged with the calling function to ensure that asynchronous functions and their finishing functions are used together correctly.
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
(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()
if your application or binding requires passing a variable argument list
directly), and the error can then be propegated through the use of
g_simple_async_result_propagate_error()
.
An asynchronous operation can be made to ignore a cancellation event by
calling g_simple_async_result_set_handle_cancellation()
with a
GSimpleAsyncResult for the operation and FALSE
. This is useful for
operations that are dangerous to cancel, such as close (which would
cause a leak if cancelled before being run).
GSimpleAsyncResult can integrate into GLib's event loop, GMainLoop,
or it can use GThreads if available.
g_simple_async_result_complete()
will finish an I/O task directly within
the main event loop. g_simple_async_result_complete_in_idle()
will
integrate the I/O task into the main event loop as an idle function and
g_simple_async_result_run_in_thread()
will run the job in a separate
thread.
To set the results of an asynchronous function,
g_simple_async_result_set_op_res_gpointer()
,
g_simple_async_result_set_op_res_gboolean()
, and
g_simple_async_result_set_op_res_gssize()
are provided, setting the operation's result to a gpointer, gboolean, or
gssize, respectively.
Likewise, to get the result of an asynchronous function,
g_simple_async_result_get_op_res_gpointer()
,
g_simple_async_result_get_op_res_gboolean()
, and
g_simple_async_result_get_op_res_gssize()
are
provided, getting the operation's result as a gpointer, gboolean, and
gssize, respectively.
typedef struct _GSimpleAsyncResult GSimpleAsyncResult;
A simple implementation of GAsyncResult.
void (*GSimpleAsyncThreadFunc) (GSimpleAsyncResult *res, GObject *object, GCancellable *cancellable);
Simple thread function that runs an asynchronous operation and checks for cancellation.
|
a GSimpleAsyncResult. |
|
a GObject. |
|
optional GCancellable object, NULL to ignore.
|
GSimpleAsyncResult * g_simple_async_result_new (GObject *source_object, GAsyncReadyCallback callback, gpointer user_data, gpointer source_tag);
Creates a GSimpleAsyncResult.
|
a GObject the asynchronous function was called with,
or NULL .
|
|
a GAsyncReadyCallback. |
|
user data passed to callback .
|
|
the asynchronous function. |
Returns : |
a GSimpleAsyncResult. |
GSimpleAsyncResult * g_simple_async_result_new_error (GObject *source_object, GAsyncReadyCallback callback, gpointer user_data, GQuark domain, gint code, const char *format, ...);
Creates a new GSimpleAsyncResult with a set error.
|
a GObject, or NULL .
|
|
a GAsyncReadyCallback. |
|
user data passed to callback .
|
|
a GQuark. |
|
an error code. |
|
a string with format characters. |
|
a list of values to insert into format .
|
Returns : |
a GSimpleAsyncResult. |
GSimpleAsyncResult * g_simple_async_result_new_from_error (GObject *source_object, GAsyncReadyCallback callback, gpointer user_data, GError *error);
Creates a GSimpleAsyncResult from an error condition.
|
a GObject, or NULL .
|
|
a GAsyncReadyCallback. |
|
user data passed to callback .
|
|
a GError location. |
Returns : |
a GSimpleAsyncResult. |
void g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple, gpointer op_res, GDestroyNotify destroy_op_res);
Sets the operation result within the asynchronous result to a pointer.
|
a GSimpleAsyncResult. |
|
a pointer result from an asynchronous function. |
|
a GDestroyNotify function. |
gpointer g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple);
Gets a pointer result as returned by the asynchronous function.
|
a GSimpleAsyncResult. |
Returns : |
a pointer from the result. |
void g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple, gssize op_res);
Sets the operation result within the asynchronous result to
the given op_res
.
|
a GSimpleAsyncResult. |
|
a gssize. |
gssize g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple);
Gets a gssize from the asynchronous result.
|
a GSimpleAsyncResult. |
Returns : |
a gssize returned from the asynchronous function. |
void g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple, gboolean op_res);
Sets the operation result to a boolean within the asynchronous result.
|
a GSimpleAsyncResult. |
|
a gboolean. |
gboolean g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple);
Gets the operation result boolean from within the asynchronous result.
|
a GSimpleAsyncResult. |
Returns : |
TRUE if the operation's result was TRUE , FALSE
if the operation's result was FALSE .
|
gpointer g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple);
Gets the source tag for the GSimpleAsyncResult.
|
a GSimpleAsyncResult. |
Returns : |
a gpointer to the source object for the GSimpleAsyncResult. |
gboolean g_simple_async_result_is_valid (GAsyncResult *result, GObject *source, gpointer source_tag);
Ensures that the data passed to the _finish function of an async operation is consistent. Three checks are performed.
First, result
is checked to ensure that it is really a
GSimpleAsyncResult. Second, source
is checked to ensure that it
matches the source object of result
. Third, source_tag
is
checked to ensure that it is equal to the source_tag argument given
to g_simple_async_result_new()
(which, by convention, is a pointer
to the _async function corresponding to the _finish function from
which this function is called).
|
the GAsyncResult passed to the _finish function. |
|
the GObject passed to the _finish function. |
|
the asynchronous function. |
Returns : |
TRUE if all checks passed or FALSE if any failed. |
void g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple, gboolean handle_cancellation);
Sets whether to handle cancellation within the asynchronous operation.
|
a GSimpleAsyncResult. |
|
a gboolean. |
void g_simple_async_result_complete (GSimpleAsyncResult *simple);
Completes an asynchronous I/O job.
Must be called in the main thread, as it invokes the callback that
should be called in the main thread. If you are in a different thread
use g_simple_async_result_complete_in_idle()
.
|
a GSimpleAsyncResult. |
void g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple);
Completes an asynchronous function in the main event loop using an idle function.
|
a GSimpleAsyncResult. |
void g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple, GSimpleAsyncThreadFunc func, int io_priority, GCancellable *cancellable);
Runs the asynchronous job in a separated thread.
|
a GSimpleAsyncResult. |
|
a GSimpleAsyncThreadFunc. |
|
the io priority of the request. |
|
optional GCancellable object, NULL to ignore.
|
void g_simple_async_result_set_from_error (GSimpleAsyncResult *simple, GError *error);
Sets the result from a GError.
|
a GSimpleAsyncResult. |
|
GError. |
gboolean g_simple_async_result_propagate_error (GSimpleAsyncResult *simple, GError **dest);
Propagates an error from within the simple asynchronous result to a given destination.
|
a GSimpleAsyncResult. |
|
a location to propegate the error to. |
Returns : |
TRUE if the error was propegated to dest . FALSE otherwise.
|
void g_simple_async_result_set_error (GSimpleAsyncResult *simple, GQuark domain, gint code, const char *format, ...);
Sets an error within the asynchronous result without a GError.
|
a GSimpleAsyncResult. |
|
a GQuark (usually G_IO_ERROR). |
|
an error code. |
|
a formatted error reporting string. |
|
a list of variables to fill in format .
|
void g_simple_async_result_set_error_va (GSimpleAsyncResult *simple, GQuark domain, gint code, const char *format, va_list args);
Sets an error within the asynchronous result without a GError.
Unless writing a binding, see g_simple_async_result_set_error()
.
|
a GSimpleAsyncResult. |
|
a GQuark (usually G_IO_ERROR). |
|
an error code. |
|
a formatted error reporting string. |
|
va_list of arguments. |
void g_simple_async_report_error_in_idle (GObject *object, GAsyncReadyCallback callback, gpointer user_data, GQuark domain, gint code, const char *format, ...);
Reports an error in an asynchronous function in an idle function by directly setting the contents of the GAsyncResult with the given error information.
|
a GObject. |
|
a GAsyncReadyCallback. |
|
user data passed to callback .
|
|
a GQuark containing the error domain (usually G_IO_ERROR). |
|
a specific error code. |
|
a formatted error reporting string. |
|
a list of variables to fill in format .
|
void g_simple_async_report_gerror_in_idle (GObject *object, GAsyncReadyCallback callback, gpointer user_data, GError *error);
Reports an error in an idle function. Similar to
g_simple_async_report_error_in_idle()
, but takes a GError rather
than building a new one.
|
a GObject. |
|
a GAsyncReadyCallback. |
|
user data passed to callback .
|
|
the GError to report |