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
33 * @see_also: #GSimpleAsyncResult
35 * Provides a base class for implementing asynchronous function results.
37 * Asynchronous operations are broken up into two separate operations
38 * which are chained together by a #GAsyncReadyCallback. To begin
39 * an asynchronous operation, provide a #GAsyncReadyCallback to the
40 * asynchronous function. This callback will be triggered when the
41 * operation has completed, and will be passed a #GAsyncResult instance
42 * filled with the details of the operation's success or failure, the
43 * object the asynchronous function was started for and any error codes
44 * returned. The asynchronous callback function is then expected to call
45 * the corresponding "_finish()" function with the object the function
46 * was called for, and the #GAsyncResult instance, and optionally,
47 * an @error to grab any error conditions that may have occurred.
49 * The purpose of the "_finish()" function is to take the generic
50 * result of type #GAsyncResult and return the specific result
51 * that the operation in question yields (e.g. a #GFileEnumerator for
52 * a "enumerate children" operation). If the result or error status
53 * of the operation is not needed, there is no need to call the
54 * "_finish()" function, GIO will take care of cleaning up the
55 * result and error information after the #GAsyncReadyCallback
56 * returns. It is also allowed to take a reference to the #GAsyncResult and
57 * call "_finish()" later.
59 * Example of a typical asynchronous operation flow:
61 * void _theoretical_frobnitz_async (Theoretical *t,
63 * GAsyncReadyCallback *cb,
66 * gboolean _theoretical_frobnitz_finish (Theoretical *t,
71 * frobnitz_result_func (GObject *source_object,
75 * gboolean success = FALSE;
77 * success = _theoretical_frobnitz_finish (source_object, res, NULL);
80 * g_printf ("Hurray!\n");
82 * g_printf ("Uh oh!\n");
84 * /<!-- -->* ... *<!-- -->/
88 * int main (int argc, void *argv[])
90 * /<!-- -->* ... *<!-- -->/
92 * _theoretical_frobnitz_async (theoretical_data,
94 * frobnitz_result_func,
97 * /<!-- -->* ... *<!-- -->/
101 * The callback for an asynchronous operation is called only once, and is
102 * always called, even in the case of a cancelled operation. On cancellation
103 * the result is a %G_IO_ERROR_CANCELLED error.
105 * Some ascynchronous operations are implemented using synchronous calls. These
106 * are run in a separate thread, if #GThread has been initialized, but otherwise they
107 * are sent to the Main Event Loop and processed in an idle function. So, if you
108 * truly need asynchronous operations, make sure to initialize #GThread.
111 static void g_async_result_base_init (gpointer g_class);
112 static void g_async_result_class_init (gpointer g_class,
113 gpointer class_data);
116 g_async_result_get_type (void)
118 static volatile gsize g_define_type_id__volatile = 0;
120 if (g_once_init_enter (&g_define_type_id__volatile))
122 const GTypeInfo async_result_info =
124 sizeof (GAsyncResultIface), /* class_size */
125 g_async_result_base_init, /* base_init */
126 NULL, /* base_finalize */
127 g_async_result_class_init,
128 NULL, /* class_finalize */
129 NULL, /* class_data */
134 GType g_define_type_id =
135 g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncResult"),
136 &async_result_info, 0);
138 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
140 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
143 return g_define_type_id__volatile;
147 g_async_result_class_init (gpointer g_class,
153 g_async_result_base_init (gpointer g_class)
158 * g_async_result_get_user_data:
159 * @res: a #GAsyncResult.
161 * Gets the user data from a #GAsyncResult.
163 * Returns: the user data for @res.
166 g_async_result_get_user_data (GAsyncResult *res)
168 GAsyncResultIface *iface;
170 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
172 iface = G_ASYNC_RESULT_GET_IFACE (res);
174 return (* iface->get_user_data) (res);
178 * g_async_result_get_source_object:
179 * @res: a #GAsyncResult.
181 * Gets the source object from a #GAsyncResult.
183 * Returns: the source object for the @res.
186 g_async_result_get_source_object (GAsyncResult *res)
188 GAsyncResultIface *iface;
190 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
192 iface = G_ASYNC_RESULT_GET_IFACE (res);
194 return (* iface->get_source_object) (res);
197 #define __G_ASYNC_RESULT_C__
198 #include "gioaliasdef.c"