Initial commit
[platform/upstream/glib2.0.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  * @include: gio/gio.h
33  * @see_also: #GSimpleAsyncResult
34  *
35  * Provides a base class for implementing asynchronous function results.
36  *
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, passing the object the
46  * function was called for, the #GAsyncResult instance, and (optionally)
47  * an @error to grab any error conditions that may have occurred.
48  *
49  * The "_finish()" function for an operation takes the generic result
50  * (of type #GAsyncResult) and returns the specific result that the
51  * operation in question yields (e.g. a #GFileEnumerator for a
52  * "enumerate children" operation). If the result or error status of the
53  * operation is not needed, there is no need to call the "_finish()"
54  * function; GIO will take care of cleaning up the result and error
55  * information after the #GAsyncReadyCallback returns. Applications may
56  * also take a reference to the #GAsyncResult and call "_finish()"
57  * later; however, the "_finish()" function may be called at most once.
58  *
59  * Example of a typical asynchronous operation flow:
60  * |[
61  * void _theoretical_frobnitz_async (Theoretical         *t,
62  *                                   GCancellable        *c,
63  *                                   GAsyncReadyCallback *cb,
64  *                                   gpointer             u);
65  *
66  * gboolean _theoretical_frobnitz_finish (Theoretical   *t,
67  *                                        GAsyncResult  *res,
68  *                                        GError       **e);
69  *
70  * static void
71  * frobnitz_result_func (GObject      *source_object,
72  *                       GAsyncResult *res,
73  *                       gpointer      user_data)
74  * {
75  *   gboolean success = FALSE;
76  *
77  *   success = _theoretical_frobnitz_finish (source_object, res, NULL);
78  *
79  *   if (success)
80  *     g_printf ("Hurray!\n");
81  *   else
82  *     g_printf ("Uh oh!\n");
83  *
84  *   /<!-- -->* ... *<!-- -->/
85  *
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 asynchronous operations are implemented using synchronous calls.
106  * These are run in a separate thread, if #GThread has been initialized, but
107  * otherwise they are sent to the Main Event Loop and processed in an idle
108  * function. So, if you truly need asynchronous operations, make sure to
109  * initialize #GThread.
110  **/
111
112 typedef GAsyncResultIface GAsyncResultInterface;
113 G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
114
115 static void
116 g_async_result_default_init (GAsyncResultInterface *iface)
117 {
118 }
119
120 /**
121  * g_async_result_get_user_data:
122  * @res: a #GAsyncResult.
123  *
124  * Gets the user data from a #GAsyncResult.
125  *
126  * Returns: the user data for @res.
127  **/
128 gpointer
129 g_async_result_get_user_data (GAsyncResult *res)
130 {
131   GAsyncResultIface *iface;
132
133   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
134
135   iface = G_ASYNC_RESULT_GET_IFACE (res);
136
137   return (* iface->get_user_data) (res);
138 }
139
140 /**
141  * g_async_result_get_source_object:
142  * @res: a #GAsyncResult
143  *
144  * Gets the source object from a #GAsyncResult.
145  *
146  * Returns: a new reference to the source object for the @res,
147  *    or %NULL if there is none.
148  */
149 GObject *
150 g_async_result_get_source_object (GAsyncResult *res)
151 {
152   GAsyncResultIface *iface;
153
154   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
155
156   iface = G_ASYNC_RESULT_GET_IFACE (res);
157
158   return (* iface->get_source_object) (res);
159 }
160
161 #define __G_ASYNC_RESULT_C__
162 #include "gioaliasdef.c"