2007-11-26 17:13:05 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
2010-01-03 01:35:08 +01:00
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* 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
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Public License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "gasyncresult.h"
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
2007-11-28 13:39:07 +01:00
|
|
|
#include "gioalias.h"
|
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* SECTION:gasyncresult
|
|
|
|
* @short_description: Asynchronous Function Results
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-27 15:00:13 +01:00
|
|
|
* @see_also: #GSimpleAsyncResult
|
2010-01-03 01:35:08 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Provides a base class for implementing asynchronous function results.
|
2010-01-03 01:35:08 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Asynchronous operations are broken up into two separate operations
|
|
|
|
* which are chained together by a #GAsyncReadyCallback. To begin
|
2010-01-03 01:35:08 +01:00
|
|
|
* an asynchronous operation, provide a #GAsyncReadyCallback to the
|
|
|
|
* asynchronous function. This callback will be triggered when the
|
|
|
|
* operation has completed, and 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
|
2010-01-03 01:44:26 +01:00
|
|
|
* the corresponding "_finish()" function, passing the object the
|
|
|
|
* function was called for, the #GAsyncResult instance, and (optionally)
|
2007-11-27 15:00:13 +01:00
|
|
|
* an @error to grab any error conditions that may have occurred.
|
2010-01-03 01:35:08 +01:00
|
|
|
*
|
2010-01-03 01:44:26 +01:00
|
|
|
* 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
|
|
|
|
* information after the #GAsyncReadyCallback returns. Applications may
|
|
|
|
* also take a reference to the #GAsyncResult and call "_finish()"
|
|
|
|
* later; however, the "_finish()" function may be called at most once.
|
2010-01-03 01:35:08 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Example of a typical asynchronous operation flow:
|
2007-11-29 21:35:23 +01:00
|
|
|
* |[
|
2010-01-03 01:35:08 +01:00
|
|
|
* void _theoretical_frobnitz_async (Theoretical *t,
|
|
|
|
* GCancellable *c,
|
2007-11-27 15:00:13 +01:00
|
|
|
* GAsyncReadyCallback *cb,
|
2007-11-29 21:35:23 +01:00
|
|
|
* gpointer u);
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
2007-11-29 21:35:23 +01:00
|
|
|
* gboolean _theoretical_frobnitz_finish (Theoretical *t,
|
|
|
|
* GAsyncResult *res,
|
|
|
|
* GError **e);
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
2010-01-03 01:35:08 +01:00
|
|
|
* static void
|
|
|
|
* frobnitz_result_func (GObject *source_object,
|
|
|
|
* GAsyncResult *res,
|
2007-11-29 21:35:23 +01:00
|
|
|
* gpointer user_data)
|
2007-11-27 15:00:13 +01:00
|
|
|
* {
|
|
|
|
* gboolean success = FALSE;
|
2007-11-29 21:35:23 +01:00
|
|
|
*
|
|
|
|
* success = _theoretical_frobnitz_finish (source_object, res, NULL);
|
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* if (success)
|
2008-02-06 11:26:53 +01:00
|
|
|
* g_printf ("Hurray!\n");
|
2010-01-03 01:35:08 +01:00
|
|
|
* else
|
2008-02-06 11:26:53 +01:00
|
|
|
* g_printf ("Uh oh!\n");
|
2007-11-29 21:35:23 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* /<!-- -->* ... *<!-- -->/
|
2007-11-29 21:35:23 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* int main (int argc, void *argv[])
|
|
|
|
* {
|
|
|
|
* /<!-- -->* ... *<!-- -->/
|
2007-11-29 21:35:23 +01:00
|
|
|
*
|
2010-01-03 01:35:08 +01:00
|
|
|
* _theoretical_frobnitz_async (theoretical_data,
|
|
|
|
* NULL,
|
|
|
|
* frobnitz_result_func,
|
2007-11-29 21:35:23 +01:00
|
|
|
* NULL);
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* /<!-- -->* ... *<!-- -->/
|
2007-11-29 21:35:23 +01:00
|
|
|
* }
|
|
|
|
* ]|
|
2007-12-12 13:19:02 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2010-01-03 01:35:08 +01:00
|
|
|
*
|
|
|
|
* Some asynchronous operations are implemented using synchronous calls.
|
|
|
|
* These are run in a separate thread, if #GThread has been initialized, but
|
|
|
|
* otherwise they are sent to the Main Event Loop and processed in an idle
|
|
|
|
* function. So, if you truly need asynchronous operations, make sure to
|
|
|
|
* initialize #GThread.
|
2007-11-27 15:00:13 +01:00
|
|
|
**/
|
|
|
|
|
2009-12-01 10:42:58 +01:00
|
|
|
typedef GAsyncResultIface GAsyncResultInterface;
|
|
|
|
G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
static void
|
2009-12-01 10:42:58 +01:00
|
|
|
g_async_result_default_init (GAsyncResultInterface *iface)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_async_result_get_user_data:
|
|
|
|
* @res: a #GAsyncResult.
|
2010-01-03 01:35:08 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Gets the user data from a #GAsyncResult.
|
2010-01-03 01:35:08 +01:00
|
|
|
*
|
|
|
|
* Returns: the user data for @res.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
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:
|
2009-09-30 02:33:39 +02:00
|
|
|
* @res: a #GAsyncResult
|
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Gets the source object from a #GAsyncResult.
|
2009-09-30 02:33:39 +02:00
|
|
|
*
|
|
|
|
* Returns: a new reference to the source object for the @res,
|
|
|
|
* or %NULL if there is none.
|
|
|
|
*/
|
2007-11-26 17:13:05 +01:00
|
|
|
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);
|
|
|
|
}
|
2007-11-28 13:39:07 +01:00
|
|
|
|
|
|
|
#define __G_ASYNC_RESULT_C__
|
|
|
|
#include "gioaliasdef.c"
|