Blame gio/gasyncresult.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gsimpleasyncresult.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gasyncresult
Packit ae235b
 * @short_description: Asynchronous Function Results
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GTask
Packit ae235b
 *
Packit ae235b
 * Provides a base class for implementing asynchronous function results.
Packit ae235b
 *
Packit ae235b
 * Asynchronous operations are broken up into two separate operations
Packit ae235b
 * which are chained together by a #GAsyncReadyCallback. To begin
Packit ae235b
 * an asynchronous operation, provide a #GAsyncReadyCallback to the
Packit ae235b
 * asynchronous function. This callback will be triggered when the
Packit ae235b
 * operation has completed, and must be run in a later iteration of
Packit ae235b
 * the [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * from where the operation was initiated. It will be passed a
Packit ae235b
 * #GAsyncResult instance filled with the details of the operation's
Packit ae235b
 * success or failure, the object the asynchronous function was
Packit ae235b
 * started for and any error codes returned. The asynchronous callback
Packit ae235b
 * function is then expected to call the corresponding "_finish()"
Packit ae235b
 * function, passing the object the function was called for, the
Packit ae235b
 * #GAsyncResult instance, and (optionally) an @error to grab any
Packit ae235b
 * error conditions that may have occurred.
Packit ae235b
 *
Packit ae235b
 * The "_finish()" function for an operation takes the generic result
Packit ae235b
 * (of type #GAsyncResult) and returns the specific result that the
Packit ae235b
 * operation in question yields (e.g. a #GFileEnumerator for a
Packit ae235b
 * "enumerate children" operation). If the result or error status of the
Packit ae235b
 * operation is not needed, there is no need to call the "_finish()"
Packit ae235b
 * function; GIO will take care of cleaning up the result and error
Packit ae235b
 * information after the #GAsyncReadyCallback returns. You can pass
Packit ae235b
 * %NULL for the #GAsyncReadyCallback if you don't need to take any
Packit ae235b
 * action at all after the operation completes. Applications may also
Packit ae235b
 * take a reference to the #GAsyncResult and call "_finish()" later;
Packit ae235b
 * however, the "_finish()" function may be called at most once.
Packit ae235b
 *
Packit ae235b
 * Example of a typical asynchronous operation flow:
Packit ae235b
 * |[
Packit ae235b
 * void _theoretical_frobnitz_async (Theoretical         *t,
Packit ae235b
 *                                   GCancellable        *c,
Packit ae235b
 *                                   GAsyncReadyCallback  cb,
Packit ae235b
 *                                   gpointer             u);
Packit ae235b
 *
Packit ae235b
 * gboolean _theoretical_frobnitz_finish (Theoretical   *t,
Packit ae235b
 *                                        GAsyncResult  *res,
Packit ae235b
 *                                        GError       **e);
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * frobnitz_result_func (GObject      *source_object,
Packit ae235b
 *			 GAsyncResult *res,
Packit ae235b
 *			 gpointer      user_data)
Packit ae235b
 * {
Packit ae235b
 *   gboolean success = FALSE;
Packit ae235b
 *
Packit ae235b
 *   success = _theoretical_frobnitz_finish (source_object, res, NULL);
Packit ae235b
 *
Packit ae235b
 *   if (success)
Packit ae235b
 *     g_printf ("Hurray!\n");
Packit ae235b
 *   else
Packit ae235b
 *     g_printf ("Uh oh!\n");
Packit ae235b
 *
Packit ae235b
 *   ...
Packit ae235b
 *
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * int main (int argc, void *argv[])
Packit ae235b
 * {
Packit ae235b
 *    ...
Packit ae235b
 *
Packit ae235b
 *    _theoretical_frobnitz_async (theoretical_data,
Packit ae235b
 *                                 NULL,
Packit ae235b
 *                                 frobnitz_result_func,
Packit ae235b
 *                                 NULL);
Packit ae235b
 *
Packit ae235b
 *    ...
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * The callback for an asynchronous operation is called only once, and is
Packit ae235b
 * always called, even in the case of a cancelled operation. On cancellation
Packit ae235b
 * the result is a %G_IO_ERROR_CANCELLED error.
Packit ae235b
 *
Packit ae235b
 * ## I/O Priority # {#io-priority}
Packit ae235b
 *
Packit ae235b
 * Many I/O-related asynchronous operations have a priority parameter,
Packit ae235b
 * which is used in certain cases to determine the order in which
Packit ae235b
 * operations are executed. They are not used to determine system-wide
Packit ae235b
 * I/O scheduling. Priorities are integers, with lower numbers indicating
Packit ae235b
 * higher priority. It is recommended to choose priorities between
Packit ae235b
 * %G_PRIORITY_LOW and %G_PRIORITY_HIGH, with %G_PRIORITY_DEFAULT
Packit ae235b
 * as a default.
Packit ae235b
 */
Packit ae235b
Packit ae235b
typedef GAsyncResultIface GAsyncResultInterface;
Packit ae235b
G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_async_result_default_init (GAsyncResultInterface *iface)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_async_result_get_user_data:
Packit ae235b
 * @res: a #GAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Gets the user data from a #GAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the user data for @res.
Packit ae235b
 **/
Packit ae235b
gpointer
Packit ae235b
g_async_result_get_user_data (GAsyncResult *res)
Packit ae235b
{
Packit ae235b
  GAsyncResultIface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
Packit ae235b
Packit ae235b
  iface = G_ASYNC_RESULT_GET_IFACE (res);
Packit ae235b
Packit ae235b
  return (* iface->get_user_data) (res);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_async_result_get_source_object:
Packit ae235b
 * @res: a #GAsyncResult
Packit ae235b
 *
Packit ae235b
 * Gets the source object from a #GAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full) (nullable): a new reference to the source
Packit ae235b
 *    object for the @res, or %NULL if there is none.
Packit ae235b
 */
Packit ae235b
GObject *
Packit ae235b
g_async_result_get_source_object (GAsyncResult *res)
Packit ae235b
{
Packit ae235b
  GAsyncResultIface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
Packit ae235b
Packit ae235b
  iface = G_ASYNC_RESULT_GET_IFACE (res);
Packit ae235b
Packit ae235b
  return (* iface->get_source_object) (res);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_async_result_legacy_propagate_error:
Packit ae235b
 * @res: a #GAsyncResult
Packit ae235b
 * @error: (out): a location to propagate the error to.
Packit ae235b
 *
Packit ae235b
 * If @res is a #GSimpleAsyncResult, this is equivalent to
Packit ae235b
 * g_simple_async_result_propagate_error(). Otherwise it returns
Packit ae235b
 * %FALSE.
Packit ae235b
 *
Packit ae235b
 * This can be used for legacy error handling in async *_finish()
Packit ae235b
 * wrapper functions that traditionally handled #GSimpleAsyncResult
Packit ae235b
 * error returns themselves rather than calling into the virtual method.
Packit ae235b
 * This should not be used in new code; #GAsyncResult errors that are
Packit ae235b
 * set by virtual methods should also be extracted by virtual methods,
Packit ae235b
 * to enable subclasses to chain up correctly.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @error is has been filled in with an error from
Packit ae235b
 *   @res, %FALSE if not.
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_async_result_legacy_propagate_error (GAsyncResult  *res,
Packit ae235b
				       GError       **error)
Packit ae235b
{
Packit ae235b
  /* This doesn't use a vmethod, because it's only for code that used
Packit ae235b
   * to use GSimpleAsyncResult. (But it's a GAsyncResult method so
Packit ae235b
   * that callers don't need to worry about GSimpleAsyncResult
Packit ae235b
   * deprecation warnings in the future.)
Packit ae235b
   */
Packit ae235b
Packit ae235b
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Packit ae235b
  if (G_IS_SIMPLE_ASYNC_RESULT (res))
Packit ae235b
    {
Packit ae235b
      return g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
Packit ae235b
						    error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return FALSE;
Packit ae235b
  G_GNUC_END_IGNORE_DEPRECATIONS
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_async_result_is_tagged:
Packit ae235b
 * @res: a #GAsyncResult
Packit ae235b
 * @source_tag: an application-defined tag
Packit ae235b
 *
Packit ae235b
 * Checks if @res has the given @source_tag (generally a function
Packit ae235b
 * pointer indicating the function @res was created by).
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @res has the indicated @source_tag, %FALSE if
Packit ae235b
 *   not.
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_async_result_is_tagged (GAsyncResult  *res,
Packit ae235b
			  gpointer       source_tag)
Packit ae235b
{
Packit ae235b
  GAsyncResultIface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
Packit ae235b
Packit ae235b
  iface = G_ASYNC_RESULT_GET_IFACE (res);
Packit ae235b
Packit ae235b
  if (!iface->is_tagged)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  return (* iface->is_tagged) (res, source_tag);
Packit ae235b
}