Add new error codes for when compilation fails and make compilation error
[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.
56  * 
57  * Example of a typical asynchronous operation flow:
58  * |[
59  * void _theoretical_frobnitz_async (Theoretical         *t, 
60  *                                   GCancellable        *c, 
61  *                                   GAsyncReadyCallback *cb,
62  *                                   gpointer             u);
63  *
64  * gboolean _theoretical_frobnitz_finish (Theoretical   *t,
65  *                                        GAsyncResult  *res,
66  *                                        GError       **e);
67  *
68  * static void 
69  * frobnitz_result_func (GObject      *source_object, 
70  *                       GAsyncResult *res, 
71  *                       gpointer      user_data)
72  * {
73  *   gboolean success = FALSE;
74  *
75  *   success = _theoretical_frobnitz_finish (source_object, res, NULL);
76  *
77  *   if (success)
78  *     g_printf ("Hurray!/n");
79  *   else 
80  *     g_printf ("Uh oh!/n");
81  *
82  *   /<!-- -->* ... *<!-- -->/
83  *
84  *   g_free (res);
85  * }
86  *
87  * int main (int argc, void *argv[])
88  * {
89  *    /<!-- -->* ... *<!-- -->/
90  *
91  *    _theoretical_frobnitz_async (theoretical_data, 
92  *                                 NULL, 
93  *                                 frobnitz_result_func, 
94  *                                 NULL);
95  *
96  *    /<!-- -->* ... *<!-- -->/
97  * }
98  * ]|
99  * 
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.
102  **/
103
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);
107
108 GType
109 g_async_result_get_type (void)
110 {
111   static GType async_result_type = 0;
112
113   if (! async_result_type)
114     {
115       static const GTypeInfo async_result_info =
116       {
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 */
123         0,
124         0,              /* n_preallocs */
125         NULL
126       };
127
128       async_result_type =
129         g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncResult"),
130                                 &async_result_info, 0);
131
132       g_type_interface_add_prerequisite (async_result_type, G_TYPE_OBJECT);
133     }
134
135   return async_result_type;
136 }
137
138 static void
139 g_async_result_class_init (gpointer g_class,
140                            gpointer class_data)
141 {
142 }
143
144 static void
145 g_async_result_base_init (gpointer g_class)
146 {
147 }
148
149 /**
150  * g_async_result_get_user_data:
151  * @res: a #GAsyncResult.
152  * 
153  * Gets the user data from a #GAsyncResult.
154  * 
155  * Returns: the user data for @res. 
156  **/
157 gpointer
158 g_async_result_get_user_data (GAsyncResult *res)
159 {
160   GAsyncResultIface *iface;
161
162   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
163
164   iface = G_ASYNC_RESULT_GET_IFACE (res);
165
166   return (* iface->get_user_data) (res);
167 }
168
169 /**
170  * g_async_result_get_source_object:
171  * @res: a #GAsyncResult.
172  * 
173  * Gets the source object from a #GAsyncResult.
174  * 
175  * Returns: the source object for the @res.
176  **/
177 GObject *
178 g_async_result_get_source_object (GAsyncResult *res)
179 {
180   GAsyncResultIface *iface;
181
182   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
183
184   iface = G_ASYNC_RESULT_GET_IFACE (res);
185
186   return (* iface->get_source_object) (res);
187 }
188
189 #define __G_ASYNC_RESULT_C__
190 #include "gioaliasdef.c"