1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gasyncresult.h"
30 * SECTION:gasyncresult
31 * @short_description: Asynchronous Function Results
32 * @see_also: #GSimpleAsyncResult
34 * Provides a base class for implementing asynchronous function results.
36 * Asynchronous operations are broken up into two separate operations
37 * which are chained together by a #GAsyncReadyCallback. To begin
38 * an asynchronous operation, provide a #GAsyncReadyCallback to the
39 * asynchronous function. This callback will be triggered when the
40 * operation has completed, and will be passed a #GAsyncResult instance
41 * filled with the details of the operation's success or failure, the
42 * object the asynchronous function was started for and any error codes
43 * returned. The asynchronous callback function is then expected to call
44 * the corresponding "_finish()" function with the object the function
45 * was called for, and the #GAsyncResult instance, and optionally,
46 * an @error to grab any error conditions that may have occurred.
48 * The purpose of the "_finish()" function is to take the generic
49 * result of type #GAsyncResult and return the specific result
50 * that the operation in question yields (e.g. a #GFileEnumerator for
51 * a "enumerate children" operation). If the result or error status
52 * of the operation is not needed, there is no need to call the
53 * "_finish()" function, GIO will take care of cleaning up the
54 * result and error information after the #GAsyncReadyCallback
57 * Example of a typical asynchronous operation flow:
59 * void _theoretical_frobnitz_async (Theoretical *t,
61 * GAsyncReadyCallback *cb,
64 * gboolean _theoretical_frobnitz_finish (Theoretical *t,
69 * frobnitz_result_func (GObject *source_object,
73 * gboolean success = FALSE;
75 * success = _theoretical_frobnitz_finish (source_object, res, NULL);
78 * g_printf ("Hurray!/n");
80 * g_printf ("Uh oh!/n");
82 * /<!-- -->* ... *<!-- -->/
87 * int main (int argc, void *argv[])
89 * /<!-- -->* ... *<!-- -->/
91 * _theoretical_frobnitz_async (theoretical_data,
93 * frobnitz_result_func,
96 * /<!-- -->* ... *<!-- -->/
100 * Asynchronous jobs are threaded if #GThread is available, but also may
101 * be sent to the Main Event Loop and processed in an idle function.
104 static void g_async_result_base_init (gpointer g_class);
105 static void g_async_result_class_init (gpointer g_class,
106 gpointer class_data);
109 g_async_result_get_type (void)
111 static GType async_result_type = 0;
113 if (! async_result_type)
115 static const GTypeInfo async_result_info =
117 sizeof (GAsyncResultIface), /* class_size */
118 g_async_result_base_init, /* base_init */
119 NULL, /* base_finalize */
120 g_async_result_class_init,
121 NULL, /* class_finalize */
122 NULL, /* class_data */
129 g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncResult"),
130 &async_result_info, 0);
132 g_type_interface_add_prerequisite (async_result_type, G_TYPE_OBJECT);
135 return async_result_type;
139 g_async_result_class_init (gpointer g_class,
145 g_async_result_base_init (gpointer g_class)
150 * g_async_result_get_user_data:
151 * @res: a #GAsyncResult.
153 * Gets the user data from a #GAsyncResult.
155 * Returns: the user data for @res.
158 g_async_result_get_user_data (GAsyncResult *res)
160 GAsyncResultIface *iface;
162 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
164 iface = G_ASYNC_RESULT_GET_IFACE (res);
166 return (* iface->get_user_data) (res);
170 * g_async_result_get_source_object:
171 * @res: a #GAsyncResult.
173 * Gets the source object from a #GAsyncResult.
175 * Returns: the source object for the @res.
178 g_async_result_get_source_object (GAsyncResult *res)
180 GAsyncResultIface *iface;
182 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
184 iface = G_ASYNC_RESULT_GET_IFACE (res);
186 return (* iface->get_source_object) (res);
189 #define __G_ASYNC_RESULT_C__
190 #include "gioaliasdef.c"