Merge remote branch 'gvdb/master'
[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
28 /**
29  * SECTION:gasyncresult
30  * @short_description: Asynchronous Function Results
31  * @include: gio/gio.h
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, passing the object the
45  * function was called for, the #GAsyncResult instance, and (optionally)
46  * an @error to grab any error conditions that may have occurred.
47  *
48  * The "_finish()" function for an operation takes the generic result
49  * (of type #GAsyncResult) and returns the specific result that the
50  * operation in question yields (e.g. a #GFileEnumerator for a
51  * "enumerate children" operation). If the result or error status of the
52  * operation is not needed, there is no need to call the "_finish()"
53  * function; GIO will take care of cleaning up the result and error
54  * information after the #GAsyncReadyCallback returns. Applications may
55  * also take a reference to the #GAsyncResult and call "_finish()"
56  * later; however, the "_finish()" function may be called at most once.
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  * }
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  * The callback for an asynchronous operation is called only once, and is
101  * always called, even in the case of a cancelled operation. On cancellation
102  * the result is a %G_IO_ERROR_CANCELLED error.
103  *
104  * Some asynchronous operations are implemented using synchronous calls.
105  * These are run in a separate thread, if #GThread has been initialized, but
106  * otherwise they are sent to the Main Event Loop and processed in an idle
107  * function. So, if you truly need asynchronous operations, make sure to
108  * initialize #GThread.
109  **/
110
111 typedef GAsyncResultIface GAsyncResultInterface;
112 G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
113
114 static void
115 g_async_result_default_init (GAsyncResultInterface *iface)
116 {
117 }
118
119 /**
120  * g_async_result_get_user_data:
121  * @res: a #GAsyncResult.
122  *
123  * Gets the user data from a #GAsyncResult.
124  *
125  * Returns: (transfer full): the user data for @res.
126  **/
127 gpointer
128 g_async_result_get_user_data (GAsyncResult *res)
129 {
130   GAsyncResultIface *iface;
131
132   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
133
134   iface = G_ASYNC_RESULT_GET_IFACE (res);
135
136   return (* iface->get_user_data) (res);
137 }
138
139 /**
140  * g_async_result_get_source_object:
141  * @res: a #GAsyncResult
142  *
143  * Gets the source object from a #GAsyncResult.
144  *
145  * Returns: (transfer full): a new reference to the source object for the @res,
146  *    or %NULL if there is none.
147  */
148 GObject *
149 g_async_result_get_source_object (GAsyncResult *res)
150 {
151   GAsyncResultIface *iface;
152
153   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
154
155   iface = G_ASYNC_RESULT_GET_IFACE (res);
156
157   return (* iface->get_source_object) (res);
158 }