Fix up a bunch of details in the docs.
[platform/upstream/glib.git] / gio / gasyncresult.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24 #include "gasyncresult.h"
25 #include "glibintl.h"
26
27 #include "gioalias.h"
28
29 /**
30  * SECTION:gasyncresult
31  * @short_description: Asynchronous Function Results
32  * @see_also: #GSimpleAsyncResult
33  * 
34  * Provides a base class for implementing asynchronous function results.
35  * 
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.
47  * 
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 
55  * returns. It is also allowed to take a reference to the #GAsyncResult and
56  * call "_finish()" later.
57  * 
58  * Example of a typical asynchronous operation flow:
59  * |[
60  * void _theoretical_frobnitz_async (Theoretical         *t, 
61  *                                   GCancellable        *c, 
62  *                                   GAsyncReadyCallback *cb,
63  *                                   gpointer             u);
64  *
65  * gboolean _theoretical_frobnitz_finish (Theoretical   *t,
66  *                                        GAsyncResult  *res,
67  *                                        GError       **e);
68  *
69  * static void 
70  * frobnitz_result_func (GObject      *source_object, 
71  *                       GAsyncResult *res, 
72  *                       gpointer      user_data)
73  * {
74  *   gboolean success = FALSE;
75  *
76  *   success = _theoretical_frobnitz_finish (source_object, res, NULL);
77  *
78  *   if (success)
79  *     g_printf ("Hurray!/n");
80  *   else 
81  *     g_printf ("Uh oh!/n");
82  *
83  *   /<!-- -->* ... *<!-- -->/
84  *
85  *   g_free (res);
86  * }
87  *
88  * int main (int argc, void *argv[])
89  * {
90  *    /<!-- -->* ... *<!-- -->/
91  *
92  *    _theoretical_frobnitz_async (theoretical_data, 
93  *                                 NULL, 
94  *                                 frobnitz_result_func, 
95  *                                 NULL);
96  *
97  *    /<!-- -->* ... *<!-- -->/
98  * }
99  * ]|
100  *
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.
104  * 
105  * Some ascynchronous operations are implemented using synchronous call. These
106  * are run in a separate #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.
109  **/
110
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);
114
115 GType
116 g_async_result_get_type (void)
117 {
118   static GType async_result_type = 0;
119
120   if (! async_result_type)
121     {
122       static const GTypeInfo async_result_info =
123       {
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 */
130         0,
131         0,              /* n_preallocs */
132         NULL
133       };
134
135       async_result_type =
136         g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncResult"),
137                                 &async_result_info, 0);
138
139       g_type_interface_add_prerequisite (async_result_type, G_TYPE_OBJECT);
140     }
141
142   return async_result_type;
143 }
144
145 static void
146 g_async_result_class_init (gpointer g_class,
147                            gpointer class_data)
148 {
149 }
150
151 static void
152 g_async_result_base_init (gpointer g_class)
153 {
154 }
155
156 /**
157  * g_async_result_get_user_data:
158  * @res: a #GAsyncResult.
159  * 
160  * Gets the user data from a #GAsyncResult.
161  * 
162  * Returns: the user data for @res. 
163  **/
164 gpointer
165 g_async_result_get_user_data (GAsyncResult *res)
166 {
167   GAsyncResultIface *iface;
168
169   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
170
171   iface = G_ASYNC_RESULT_GET_IFACE (res);
172
173   return (* iface->get_user_data) (res);
174 }
175
176 /**
177  * g_async_result_get_source_object:
178  * @res: a #GAsyncResult.
179  * 
180  * Gets the source object from a #GAsyncResult.
181  * 
182  * Returns: the source object for the @res.
183  **/
184 GObject *
185 g_async_result_get_source_object (GAsyncResult *res)
186 {
187   GAsyncResultIface *iface;
188
189   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
190
191   iface = G_ASYNC_RESULT_GET_IFACE (res);
192
193   return (* iface->get_source_object) (res);
194 }
195
196 #define __G_ASYNC_RESULT_C__
197 #include "gioaliasdef.c"