adf6f9c2688904072dd937d703cc3ce321d06d5b
[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. You can pass
55  * %NULL for the #GAsyncReadyCallback if you don't need to take any
56  * action at all after the operation completes. Applications may also
57  * take a reference to the #GAsyncResult and call "_finish()" later;
58  * however, the "_finish()" function may be called at most once.
59  *
60  * Example of a typical asynchronous operation flow:
61  * |[
62  * void _theoretical_frobnitz_async (Theoretical         *t,
63  *                                   GCancellable        *c,
64  *                                   GAsyncReadyCallback *cb,
65  *                                   gpointer             u);
66  *
67  * gboolean _theoretical_frobnitz_finish (Theoretical   *t,
68  *                                        GAsyncResult  *res,
69  *                                        GError       **e);
70  *
71  * static void
72  * frobnitz_result_func (GObject      *source_object,
73  *                       GAsyncResult *res,
74  *                       gpointer      user_data)
75  * {
76  *   gboolean success = FALSE;
77  *
78  *   success = _theoretical_frobnitz_finish (source_object, res, NULL);
79  *
80  *   if (success)
81  *     g_printf ("Hurray!\n");
82  *   else
83  *     g_printf ("Uh oh!\n");
84  *
85  *   /<!-- -->* ... *<!-- -->/
86  *
87  * }
88  *
89  * int main (int argc, void *argv[])
90  * {
91  *    /<!-- -->* ... *<!-- -->/
92  *
93  *    _theoretical_frobnitz_async (theoretical_data,
94  *                                 NULL,
95  *                                 frobnitz_result_func,
96  *                                 NULL);
97  *
98  *    /<!-- -->* ... *<!-- -->/
99  * }
100  * ]|
101  *
102  * The callback for an asynchronous operation is called only once, and is
103  * always called, even in the case of a cancelled operation. On cancellation
104  * the result is a %G_IO_ERROR_CANCELLED error.
105  **/
106
107 typedef GAsyncResultIface GAsyncResultInterface;
108 G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
109
110 static void
111 g_async_result_default_init (GAsyncResultInterface *iface)
112 {
113 }
114
115 /**
116  * g_async_result_get_user_data:
117  * @res: a #GAsyncResult.
118  *
119  * Gets the user data from a #GAsyncResult.
120  *
121  * Returns: (transfer full): the user data for @res.
122  **/
123 gpointer
124 g_async_result_get_user_data (GAsyncResult *res)
125 {
126   GAsyncResultIface *iface;
127
128   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
129
130   iface = G_ASYNC_RESULT_GET_IFACE (res);
131
132   return (* iface->get_user_data) (res);
133 }
134
135 /**
136  * g_async_result_get_source_object:
137  * @res: a #GAsyncResult
138  *
139  * Gets the source object from a #GAsyncResult.
140  *
141  * Returns: (transfer full): a new reference to the source object for the @res,
142  *    or %NULL if there is none.
143  */
144 GObject *
145 g_async_result_get_source_object (GAsyncResult *res)
146 {
147   GAsyncResultIface *iface;
148
149   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
150
151   iface = G_ASYNC_RESULT_GET_IFACE (res);
152
153   return (* iface->get_source_object) (res);
154 }