hook gvariant vectors up to kdbus
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22 #include "gasyncresult.h"
23 #include "gsimpleasyncresult.h"
24 #include "glibintl.h"
25
26
27 /**
28  * SECTION:gasyncresult
29  * @short_description: Asynchronous Function Results
30  * @include: gio/gio.h
31  * @see_also: #GTask
32  *
33  * Provides a base class for implementing asynchronous function results.
34  *
35  * Asynchronous operations are broken up into two separate operations
36  * which are chained together by a #GAsyncReadyCallback. To begin
37  * an asynchronous operation, provide a #GAsyncReadyCallback to the
38  * asynchronous function. This callback will be triggered when the
39  * operation has completed, and will be passed a #GAsyncResult instance
40  * filled with the details of the operation's success or failure, the
41  * object the asynchronous function was started for and any error codes
42  * returned. The asynchronous callback function is then expected to call
43  * the corresponding "_finish()" function, passing the object the
44  * function was called for, the #GAsyncResult instance, and (optionally)
45  * an @error to grab any error conditions that may have occurred.
46  *
47  * The "_finish()" function for an operation takes the generic result
48  * (of type #GAsyncResult) and returns the specific result that the
49  * operation in question yields (e.g. a #GFileEnumerator for a
50  * "enumerate children" operation). If the result or error status of the
51  * operation is not needed, there is no need to call the "_finish()"
52  * function; GIO will take care of cleaning up the result and error
53  * information after the #GAsyncReadyCallback returns. You can pass
54  * %NULL for the #GAsyncReadyCallback if you don't need to take any
55  * action at all after the operation completes. Applications may also
56  * take a reference to the #GAsyncResult and call "_finish()" later;
57  * however, the "_finish()" function may be called at most once.
58  *
59  * Example of a typical asynchronous operation flow:
60  * |[<!-- language="C" -->
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  * ## I/O Priority # {#io-priority}
106  *
107  * Many I/O-related asynchronous operations have a priority parameter,
108  * which is used in certain cases to determine the order in which
109  * operations are executed. They are not used to determine system-wide
110  * I/O scheduling. Priorities are integers, with lower numbers indicating
111  * higher priority. It is recommended to choose priorities between
112  * %G_PRIORITY_LOW and %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT
113  * as a default.
114  */
115
116 typedef GAsyncResultIface GAsyncResultInterface;
117 G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
118
119 static void
120 g_async_result_default_init (GAsyncResultInterface *iface)
121 {
122 }
123
124 /**
125  * g_async_result_get_user_data:
126  * @res: a #GAsyncResult.
127  *
128  * Gets the user data from a #GAsyncResult.
129  *
130  * Returns: (transfer full): the user data for @res.
131  **/
132 gpointer
133 g_async_result_get_user_data (GAsyncResult *res)
134 {
135   GAsyncResultIface *iface;
136
137   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
138
139   iface = G_ASYNC_RESULT_GET_IFACE (res);
140
141   return (* iface->get_user_data) (res);
142 }
143
144 /**
145  * g_async_result_get_source_object:
146  * @res: a #GAsyncResult
147  *
148  * Gets the source object from a #GAsyncResult.
149  *
150  * Returns: (transfer full): a new reference to the source object for the @res,
151  *    or %NULL if there is none.
152  */
153 GObject *
154 g_async_result_get_source_object (GAsyncResult *res)
155 {
156   GAsyncResultIface *iface;
157
158   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
159
160   iface = G_ASYNC_RESULT_GET_IFACE (res);
161
162   return (* iface->get_source_object) (res);
163 }
164
165 /**
166  * g_async_result_legacy_propagate_error:
167  * @res: a #GAsyncResult
168  * @error: (out): a location to propagate the error to.
169  *
170  * If @res is a #GSimpleAsyncResult, this is equivalent to
171  * g_simple_async_result_propagate_error(). Otherwise it returns
172  * %FALSE.
173  *
174  * This can be used for legacy error handling in async *_finish()
175  * wrapper functions that traditionally handled #GSimpleAsyncResult
176  * error returns themselves rather than calling into the virtual method.
177  * This should not be used in new code; #GAsyncResult errors that are
178  * set by virtual methods should also be extracted by virtual methods,
179  * to enable subclasses to chain up correctly.
180  *
181  * Returns: %TRUE if @error is has been filled in with an error from
182  *   @res, %FALSE if not.
183  *
184  * Since: 2.34
185  **/
186 gboolean
187 g_async_result_legacy_propagate_error (GAsyncResult  *res,
188                                        GError       **error)
189 {
190   /* This doesn't use a vmethod, because it's only for code that used
191    * to use GSimpleAsyncResult. (But it's a GAsyncResult method so
192    * that callers don't need to worry about GSimpleAsyncResult
193    * deprecation warnings in the future.)
194    */
195
196   if (G_IS_SIMPLE_ASYNC_RESULT (res))
197     {
198       return g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
199                                                     error);
200     }
201   else
202     return FALSE;
203 }
204
205 /**
206  * g_async_result_is_tagged:
207  * @res: a #GAsyncResult
208  * @source_tag: an application-defined tag
209  *
210  * Checks if @res has the given @source_tag (generally a function
211  * pointer indicating the function @res was created by).
212  *
213  * Returns: %TRUE if @res has the indicated @source_tag, %FALSE if
214  *   not.
215  *
216  * Since: 2.34
217  **/
218 gboolean
219 g_async_result_is_tagged (GAsyncResult  *res,
220                           gpointer       source_tag)
221 {
222   GAsyncResultIface *iface;
223
224   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
225
226   iface = G_ASYNC_RESULT_GET_IFACE (res);
227
228   if (!iface->is_tagged)
229     return FALSE;
230
231   return (* iface->is_tagged) (res, source_tag);
232 }