gio/tests/file: skip the file monitor tests if using GPollFileMonitor
[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 "gsimpleasyncresult.h"
26 #include "glibintl.h"
27
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. You can pass
56  * %NULL for the #GAsyncReadyCallback if you don't need to take any
57  * action at all after the operation completes. Applications may also
58  * take a reference to the #GAsyncResult and call "_finish()" later;
59  * however, the "_finish()" function may be called at most once.
60  *
61  * Example of a typical asynchronous operation flow:
62  * |[
63  * void _theoretical_frobnitz_async (Theoretical         *t,
64  *                                   GCancellable        *c,
65  *                                   GAsyncReadyCallback *cb,
66  *                                   gpointer             u);
67  *
68  * gboolean _theoretical_frobnitz_finish (Theoretical   *t,
69  *                                        GAsyncResult  *res,
70  *                                        GError       **e);
71  *
72  * static void
73  * frobnitz_result_func (GObject      *source_object,
74  *                       GAsyncResult *res,
75  *                       gpointer      user_data)
76  * {
77  *   gboolean success = FALSE;
78  *
79  *   success = _theoretical_frobnitz_finish (source_object, res, NULL);
80  *
81  *   if (success)
82  *     g_printf ("Hurray!\n");
83  *   else
84  *     g_printf ("Uh oh!\n");
85  *
86  *   /<!-- -->* ... *<!-- -->/
87  *
88  * }
89  *
90  * int main (int argc, void *argv[])
91  * {
92  *    /<!-- -->* ... *<!-- -->/
93  *
94  *    _theoretical_frobnitz_async (theoretical_data,
95  *                                 NULL,
96  *                                 frobnitz_result_func,
97  *                                 NULL);
98  *
99  *    /<!-- -->* ... *<!-- -->/
100  * }
101  * ]|
102  *
103  * The callback for an asynchronous operation is called only once, and is
104  * always called, even in the case of a cancelled operation. On cancellation
105  * the result is a %G_IO_ERROR_CANCELLED error.
106  **/
107
108 typedef GAsyncResultIface GAsyncResultInterface;
109 G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
110
111 static void
112 g_async_result_default_init (GAsyncResultInterface *iface)
113 {
114 }
115
116 /**
117  * g_async_result_get_user_data:
118  * @res: a #GAsyncResult.
119  *
120  * Gets the user data from a #GAsyncResult.
121  *
122  * Returns: (transfer full): the user data for @res.
123  **/
124 gpointer
125 g_async_result_get_user_data (GAsyncResult *res)
126 {
127   GAsyncResultIface *iface;
128
129   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
130
131   iface = G_ASYNC_RESULT_GET_IFACE (res);
132
133   return (* iface->get_user_data) (res);
134 }
135
136 /**
137  * g_async_result_get_source_object:
138  * @res: a #GAsyncResult
139  *
140  * Gets the source object from a #GAsyncResult.
141  *
142  * Returns: (transfer full): a new reference to the source object for the @res,
143  *    or %NULL if there is none.
144  */
145 GObject *
146 g_async_result_get_source_object (GAsyncResult *res)
147 {
148   GAsyncResultIface *iface;
149
150   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
151
152   iface = G_ASYNC_RESULT_GET_IFACE (res);
153
154   return (* iface->get_source_object) (res);
155 }
156
157 /**
158  * g_async_result_legacy_propagate_error:
159  * @result: a #GAsyncResult
160  * @dest: (out): a location to propagate the error to.
161  *
162  * If @result is a #GSimpleAsyncResult, this is equivalent to
163  * g_simple_async_result_propagate_error(). Otherwise it returns
164  * %FALSE.
165  *
166  * This can be used for legacy error handling in async
167  * <literal>_finish ()</literal> wrapper functions that traditionally
168  * handled #GSimpleAsyncResult error returns themselves rather than
169  * calling into the virtual method. This should not be used in new
170  * code; #GAsyncResult errors that are set by virtual methods should
171  * also be extracted by virtual methods, to enable subclasses to chain
172  * up correctly.
173  *
174  * Returns: %TRUE if @error is has been filled in with an error from
175  *   @res, %FALSE if not.
176  *
177  * Since: 2.34
178  **/
179 gboolean
180 g_async_result_legacy_propagate_error (GAsyncResult  *res,
181                                        GError       **error)
182 {
183   /* This doesn't use a vmethod, because it's only for code that used
184    * to use GSimpleAsyncResult. (But it's a GAsyncResult method so
185    * that callers don't need to worry about GSimpleAsyncResult
186    * deprecation warnings in the future.)
187    */
188
189   if (G_IS_SIMPLE_ASYNC_RESULT (res))
190     {
191       return g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
192                                                     error);
193     }
194   else
195     return FALSE;
196 }
197
198 /**
199  * g_async_result_is_tagged:
200  * @result: a #GAsyncResult
201  * @source_tag: an application-defined tag
202  *
203  * Checks if @result has the given @source_tag (generally a function
204  * pointer indicating the function @result was created by).
205  *
206  * Returns: %TRUE if @result has the indicated @source_tag, %FALSE if
207  *   not.
208  *
209  * Since: 2.34
210  **/
211 gboolean
212 g_async_result_is_tagged (GAsyncResult  *res,
213                           gpointer       source_tag)
214 {
215   GAsyncResultIface *iface;
216
217   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
218
219   iface = G_ASYNC_RESULT_GET_IFACE (res);
220
221   if (!iface->is_tagged)
222     return FALSE;
223
224   return (* iface->is_tagged) (res, source_tag);
225 }