gasyncresult.c 7.55 KB
Newer Older
1
/* GIO - GLib Input, Output and Streaming Library
Matthias Clasen's avatar
Matthias Clasen committed
2
 *
3 4 5 6 7
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22
#include "gasyncresult.h"
23
#include "gsimpleasyncresult.h"
24 25
#include "glibintl.h"

26

27 28 29
/**
 * SECTION:gasyncresult
 * @short_description: Asynchronous Function Results
Matthias Clasen's avatar
Matthias Clasen committed
30
 * @include: gio/gio.h
31
 * @see_also: #GTask
Matthias Clasen's avatar
Matthias Clasen committed
32
 *
33
 * Provides a base class for implementing asynchronous function results.
Matthias Clasen's avatar
Matthias Clasen committed
34
 *
35 36
 * Asynchronous operations are broken up into two separate operations
 * which are chained together by a #GAsyncReadyCallback. To begin
Matthias Clasen's avatar
Matthias Clasen committed
37 38
 * an asynchronous operation, provide a #GAsyncReadyCallback to the
 * asynchronous function. This callback will be triggered when the
39 40 41 42 43 44 45 46 47 48
 * operation has completed, and must be run in a later iteration of
 * the [thread-default main context][g-main-context-push-thread-default]
 * from where the operation was initiated. It will be passed a
 * #GAsyncResult instance filled with the details of the operation's
 * success or failure, the object the asynchronous function was
 * started for and any error codes returned. The asynchronous callback
 * function is then expected to call the corresponding "_finish()"
 * function, passing the object the function was called for, the
 * #GAsyncResult instance, and (optionally) an @error to grab any
 * error conditions that may have occurred.
Matthias Clasen's avatar
Matthias Clasen committed
49
 *
50 51 52 53 54 55
 * The "_finish()" function for an operation takes the generic result
 * (of type #GAsyncResult) and returns the specific result that the
 * operation in question yields (e.g. a #GFileEnumerator for a
 * "enumerate children" operation). If the result or error status of the
 * operation is not needed, there is no need to call the "_finish()"
 * function; GIO will take care of cleaning up the result and error
56 57 58 59 60
 * information after the #GAsyncReadyCallback returns. You can pass
 * %NULL for the #GAsyncReadyCallback if you don't need to take any
 * action at all after the operation completes. Applications may also
 * take a reference to the #GAsyncResult and call "_finish()" later;
 * however, the "_finish()" function may be called at most once.
Matthias Clasen's avatar
Matthias Clasen committed
61
 *
62
 * Example of a typical asynchronous operation flow:
63
 * |[<!-- language="C" -->
Matthias Clasen's avatar
Matthias Clasen committed
64 65
 * void _theoretical_frobnitz_async (Theoretical         *t,
 *                                   GCancellable        *c,
Sébastien Wilmet's avatar
Sébastien Wilmet committed
66
 *                                   GAsyncReadyCallback  cb,
Matthias Clasen's avatar
Matthias Clasen committed
67
 *                                   gpointer             u);
68
 *
Matthias Clasen's avatar
Matthias Clasen committed
69 70 71
 * gboolean _theoretical_frobnitz_finish (Theoretical   *t,
 *                                        GAsyncResult  *res,
 *                                        GError       **e);
72
 *
Matthias Clasen's avatar
Matthias Clasen committed
73 74 75
 * static void
 * frobnitz_result_func (GObject      *source_object,
 *			 GAsyncResult *res,
Matthias Clasen's avatar
Matthias Clasen committed
76
 *			 gpointer      user_data)
77 78
 * {
 *   gboolean success = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
79 80 81
 *
 *   success = _theoretical_frobnitz_finish (source_object, res, NULL);
 *
82
 *   if (success)
83
 *     g_printf ("Hurray!\n");
Matthias Clasen's avatar
Matthias Clasen committed
84
 *   else
85
 *     g_printf ("Uh oh!\n");
Matthias Clasen's avatar
Matthias Clasen committed
86
 *
87
 *   ...
Matthias Clasen's avatar
Matthias Clasen committed
88
 *
89 90 91 92
 * }
 *
 * int main (int argc, void *argv[])
 * {
93
 *    ...
Matthias Clasen's avatar
Matthias Clasen committed
94
 *
Matthias Clasen's avatar
Matthias Clasen committed
95 96 97
 *    _theoretical_frobnitz_async (theoretical_data,
 *                                 NULL,
 *                                 frobnitz_result_func,
Matthias Clasen's avatar
Matthias Clasen committed
98
 *                                 NULL);
99
 *
100
 *    ...
Matthias Clasen's avatar
Matthias Clasen committed
101 102
 * }
 * ]|
103 104 105 106
 *
 * The callback for an asynchronous operation is called only once, and is
 * always called, even in the case of a cancelled operation. On cancellation
 * the result is a %G_IO_ERROR_CANCELLED error.
107
 *
108 109
 * ## I/O Priority # {#io-priority}
 *
110 111 112 113 114 115
 * Many I/O-related asynchronous operations have a priority parameter,
 * which is used in certain cases to determine the order in which
 * operations are executed. They are not used to determine system-wide
 * I/O scheduling. Priorities are integers, with lower numbers indicating
 * higher priority. It is recommended to choose priorities between
 * %G_PRIORITY_LOW and %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT
116
 * as a default.
117
 */
118

119 120
typedef GAsyncResultIface GAsyncResultInterface;
G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
121 122

static void
123
g_async_result_default_init (GAsyncResultInterface *iface)
124 125 126 127 128 129
{
}

/**
 * g_async_result_get_user_data:
 * @res: a #GAsyncResult.
Matthias Clasen's avatar
Matthias Clasen committed
130
 *
131
 * Gets the user data from a #GAsyncResult.
Matthias Clasen's avatar
Matthias Clasen committed
132
 *
133
 * Returns: (transfer full): the user data for @res.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
 **/
gpointer
g_async_result_get_user_data (GAsyncResult *res)
{
  GAsyncResultIface *iface;

  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);

  iface = G_ASYNC_RESULT_GET_IFACE (res);

  return (* iface->get_user_data) (res);
}

/**
 * g_async_result_get_source_object:
149 150
 * @res: a #GAsyncResult
 *
151
 * Gets the source object from a #GAsyncResult.
152
 *
153 154
 * Returns: (transfer full) (nullable): a new reference to the source
 *    object for the @res, or %NULL if there is none.
155
 */
156 157 158 159 160 161 162 163 164 165 166
GObject *
g_async_result_get_source_object (GAsyncResult *res)
{
  GAsyncResultIface *iface;

  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);

  iface = G_ASYNC_RESULT_GET_IFACE (res);

  return (* iface->get_source_object) (res);
}
167 168 169

/**
 * g_async_result_legacy_propagate_error:
Matthias Clasen's avatar
Matthias Clasen committed
170 171
 * @res: a #GAsyncResult
 * @error: (out): a location to propagate the error to.
172
 *
Matthias Clasen's avatar
Matthias Clasen committed
173
 * If @res is a #GSimpleAsyncResult, this is equivalent to
174 175 176
 * g_simple_async_result_propagate_error(). Otherwise it returns
 * %FALSE.
 *
177 178 179 180 181 182
 * This can be used for legacy error handling in async *_finish()
 * wrapper functions that traditionally handled #GSimpleAsyncResult
 * error returns themselves rather than calling into the virtual method.
 * This should not be used in new code; #GAsyncResult errors that are
 * set by virtual methods should also be extracted by virtual methods,
 * to enable subclasses to chain up correctly.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
 *
 * Returns: %TRUE if @error is has been filled in with an error from
 *   @res, %FALSE if not.
 *
 * Since: 2.34
 **/
gboolean
g_async_result_legacy_propagate_error (GAsyncResult  *res,
				       GError       **error)
{
  /* This doesn't use a vmethod, because it's only for code that used
   * to use GSimpleAsyncResult. (But it's a GAsyncResult method so
   * that callers don't need to worry about GSimpleAsyncResult
   * deprecation warnings in the future.)
   */

199
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
200 201 202 203 204 205 206
  if (G_IS_SIMPLE_ASYNC_RESULT (res))
    {
      return g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
						    error);
    }
  else
    return FALSE;
207
  G_GNUC_END_IGNORE_DEPRECATIONS
208
}
209 210 211

/**
 * g_async_result_is_tagged:
Matthias Clasen's avatar
Matthias Clasen committed
212
 * @res: a #GAsyncResult
213 214
 * @source_tag: an application-defined tag
 *
Matthias Clasen's avatar
Matthias Clasen committed
215 216
 * Checks if @res has the given @source_tag (generally a function
 * pointer indicating the function @res was created by).
217
 *
Matthias Clasen's avatar
Matthias Clasen committed
218
 * Returns: %TRUE if @res has the indicated @source_tag, %FALSE if
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
 *   not.
 *
 * Since: 2.34
 **/
gboolean
g_async_result_is_tagged (GAsyncResult  *res,
			  gpointer       source_tag)
{
  GAsyncResultIface *iface;

  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);

  iface = G_ASYNC_RESULT_GET_IFACE (res);

  if (!iface->is_tagged)
    return FALSE;

  return (* iface->is_tagged) (res, source_tag);
}