GCredentials: add getter/setter for the Unix process ID
[glib.git] / gio / gasyncresult.c
blobc86b3dc59aa0e913801a484eef330a2da28c283a
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
24 #include "gasyncresult.h"
25 #include "gsimpleasyncresult.h"
26 #include "glibintl.h"
29 /**
30 * SECTION:gasyncresult
31 * @short_description: Asynchronous Function Results
32 * @include: gio/gio.h
33 * @see_also: #GTask
35 * Provides a base class for implementing asynchronous function results.
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.
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.
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);
68 * gboolean _theoretical_frobnitz_finish (Theoretical *t,
69 * GAsyncResult *res,
70 * GError **e);
72 * static void
73 * frobnitz_result_func (GObject *source_object,
74 * GAsyncResult *res,
75 * gpointer user_data)
76 * {
77 * gboolean success = FALSE;
79 * success = _theoretical_frobnitz_finish (source_object, res, NULL);
81 * if (success)
82 * g_printf ("Hurray!\n");
83 * else
84 * g_printf ("Uh oh!\n");
86 * /<!-- -->* ... *<!-- -->/
88 * }
90 * int main (int argc, void *argv[])
91 * {
92 * /<!-- -->* ... *<!-- -->/
94 * _theoretical_frobnitz_async (theoretical_data,
95 * NULL,
96 * frobnitz_result_func,
97 * NULL);
99 * /<!-- -->* ... *<!-- -->/
101 * ]|
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.
107 * <para id="io-priority"><indexterm><primary>I/O
108 * priority</primary></indexterm> Many I/O-related asynchronous
109 * operations have a priority parameter, which is used in certain
110 * cases to determine the order in which operations are executed. They
111 * are <emphasis>not</emphasis> used to determine system-wide I/O
112 * scheduling. Priorities are integers, with lower numbers indicating
113 * higher priority. It is recommended to choose priorities between
114 * %G_PRIORITY_LOW and %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT as a
115 * default. </para>
118 typedef GAsyncResultIface GAsyncResultInterface;
119 G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
121 static void
122 g_async_result_default_init (GAsyncResultInterface *iface)
127 * g_async_result_get_user_data:
128 * @res: a #GAsyncResult.
130 * Gets the user data from a #GAsyncResult.
132 * Returns: (transfer full): the user data for @res.
134 gpointer
135 g_async_result_get_user_data (GAsyncResult *res)
137 GAsyncResultIface *iface;
139 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
141 iface = G_ASYNC_RESULT_GET_IFACE (res);
143 return (* iface->get_user_data) (res);
147 * g_async_result_get_source_object:
148 * @res: a #GAsyncResult
150 * Gets the source object from a #GAsyncResult.
152 * Returns: (transfer full): a new reference to the source object for the @res,
153 * or %NULL if there is none.
155 GObject *
156 g_async_result_get_source_object (GAsyncResult *res)
158 GAsyncResultIface *iface;
160 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
162 iface = G_ASYNC_RESULT_GET_IFACE (res);
164 return (* iface->get_source_object) (res);
168 * g_async_result_legacy_propagate_error:
169 * @res: a #GAsyncResult
170 * @error: (out): a location to propagate the error to.
172 * If @res is a #GSimpleAsyncResult, this is equivalent to
173 * g_simple_async_result_propagate_error(). Otherwise it returns
174 * %FALSE.
176 * This can be used for legacy error handling in async
177 * <literal>_finish ()</literal> wrapper functions that traditionally
178 * handled #GSimpleAsyncResult error returns themselves rather than
179 * calling into the virtual method. This should not be used in new
180 * code; #GAsyncResult errors that are set by virtual methods should
181 * also be extracted by virtual methods, to enable subclasses to chain
182 * up correctly.
184 * Returns: %TRUE if @error is has been filled in with an error from
185 * @res, %FALSE if not.
187 * Since: 2.34
189 gboolean
190 g_async_result_legacy_propagate_error (GAsyncResult *res,
191 GError **error)
193 /* This doesn't use a vmethod, because it's only for code that used
194 * to use GSimpleAsyncResult. (But it's a GAsyncResult method so
195 * that callers don't need to worry about GSimpleAsyncResult
196 * deprecation warnings in the future.)
199 if (G_IS_SIMPLE_ASYNC_RESULT (res))
201 return g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
202 error);
204 else
205 return FALSE;
209 * g_async_result_is_tagged:
210 * @res: a #GAsyncResult
211 * @source_tag: an application-defined tag
213 * Checks if @res has the given @source_tag (generally a function
214 * pointer indicating the function @res was created by).
216 * Returns: %TRUE if @res has the indicated @source_tag, %FALSE if
217 * not.
219 * Since: 2.34
221 gboolean
222 g_async_result_is_tagged (GAsyncResult *res,
223 gpointer source_tag)
225 GAsyncResultIface *iface;
227 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
229 iface = G_ASYNC_RESULT_GET_IFACE (res);
231 if (!iface->is_tagged)
232 return FALSE;
234 return (* iface->is_tagged) (res, source_tag);