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>
24 #include "gasyncresult.h"
30 * SECTION:gasyncresult
31 * @short_description: Asynchronous Function Results
33 * @see_also: #GSimpleAsyncResult
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. Applications may
56 * also take a reference to the #GAsyncResult and call "_finish()"
57 * later; however, the "_finish()" function may be called at most once.
59 * Example of a typical asynchronous operation flow:
61 * void _theoretical_frobnitz_async (Theoretical *t,
63 * GAsyncReadyCallback *cb,
66 * gboolean _theoretical_frobnitz_finish (Theoretical *t,
71 * frobnitz_result_func (GObject *source_object,
75 * gboolean success = FALSE;
77 * success = _theoretical_frobnitz_finish (source_object, res, NULL);
80 * g_printf ("Hurray!\n");
82 * g_printf ("Uh oh!\n");
84 * /<!-- -->* ... *<!-- -->/
88 * int main (int argc, void *argv[])
90 * /<!-- -->* ... *<!-- -->/
92 * _theoretical_frobnitz_async (theoretical_data,
94 * frobnitz_result_func,
97 * /<!-- -->* ... *<!-- -->/
101 * The callback for an asynchronous operation is called only once, and is
102 * always called, even in the case of a cancelled operation. On cancellation
103 * the result is a %G_IO_ERROR_CANCELLED error.
105 * Some asynchronous operations are implemented using synchronous calls.
106 * These are run in a separate thread, if #GThread has been initialized, but
107 * otherwise they are sent to the Main Event Loop and processed in an idle
108 * function. So, if you truly need asynchronous operations, make sure to
109 * initialize #GThread.
112 typedef GAsyncResultIface GAsyncResultInterface;
113 G_DEFINE_INTERFACE (GAsyncResult, g_async_result, G_TYPE_OBJECT)
116 g_async_result_default_init (GAsyncResultInterface *iface)
121 * g_async_result_get_user_data:
122 * @res: a #GAsyncResult.
124 * Gets the user data from a #GAsyncResult.
126 * Returns: the user data for @res.
129 g_async_result_get_user_data (GAsyncResult *res)
131 GAsyncResultIface *iface;
133 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
135 iface = G_ASYNC_RESULT_GET_IFACE (res);
137 return (* iface->get_user_data) (res);
141 * g_async_result_get_source_object:
142 * @res: a #GAsyncResult
144 * Gets the source object from a #GAsyncResult.
146 * Returns: a new reference to the source object for the @res,
147 * or %NULL if there is none.
150 g_async_result_get_source_object (GAsyncResult *res)
152 GAsyncResultIface *iface;
154 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
156 iface = G_ASYNC_RESULT_GET_IFACE (res);
158 return (* iface->get_source_object) (res);
161 #define __G_ASYNC_RESULT_C__
162 #include "gioaliasdef.c"