gio/: fully remove gioalias hacks
[platform/upstream/glib.git] / gio / gasyncinitable.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gasyncinitable.h"
25 #include "gasyncresult.h"
26 #include "gsimpleasyncresult.h"
27 #include "glibintl.h"
28
29
30 /**
31  * SECTION:gasyncinitable
32  * @short_description: Asynchronously failable object initialization interface
33  * @include: gio/gio.h
34  * @see_also: #GInitable
35  *
36  * This is the asynchronous version of #GInitable; it behaves the same
37  * in all ways except that initialization is asynchronous. For more details
38  * see the descriptions on #GInitable.
39  *
40  * A class may implement both the #GInitable and #GAsyncInitable interfaces.
41  *
42  * Users of objects implementing this are not intended to use the interface
43  * method directly; instead it will be used automatically in various ways.
44  * For C applications you generally just call g_async_initable_new_async()
45  * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
46  * g_async_initable_init_async() under the cover, calling back with %NULL and
47  * a set %GError on failure.
48  */
49
50 static void     g_async_initable_real_init_async  (GAsyncInitable       *initable,
51                                                    int                   io_priority,
52                                                    GCancellable         *cancellable,
53                                                    GAsyncReadyCallback   callback,
54                                                    gpointer              user_data);
55 static gboolean g_async_initable_real_init_finish (GAsyncInitable       *initable,
56                                                    GAsyncResult         *res,
57                                                    GError              **error);
58
59
60 typedef GAsyncInitableIface GAsyncInitableInterface;
61 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
62
63
64 static void
65 g_async_initable_default_init (GAsyncInitableInterface *iface)
66 {
67   iface->init_async = g_async_initable_real_init_async;
68   iface->init_finish = g_async_initable_real_init_finish;
69 }
70
71 /**
72  * g_async_initable_init_async:
73  * @initable: a #GAsyncInitable.
74  * @io_priority: the <link linkend="io-priority">I/O priority</link>
75  *     of the operation.
76  * @cancellable: optional #GCancellable object, %NULL to ignore.
77  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
78  * @user_data: the data to pass to callback function
79  *
80  * Starts asynchronous initialization of the object implementing the
81  * interface. This must be done before any real use of the object after
82  * initial construction. If the object also implements #GInitable you can
83  * optionally call g_initable_init() instead.
84  *
85  * When the initialization is finished, @callback will be called. You can
86  * then call g_async_initable_init_finish() to get the result of the
87  * initialization.
88  *
89  * Implementations may also support cancellation. If @cancellable is not
90  * %NULL, then initialization can be cancelled by triggering the cancellable
91  * object from another thread. If the operation was cancelled, the error
92  * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
93  * the object doesn't support cancellable initialization, the error
94  * %G_IO_ERROR_NOT_SUPPORTED will be returned.
95  *
96  * If this function is not called, or returns with an error, then all
97  * operations on the object should fail, generally returning the
98  * error %G_IO_ERROR_NOT_INITIALIZED.
99  *
100  * Implementations of this method must be idempotent: i.e. multiple calls
101  * to this function with the same argument should return the same results.
102  * Only the first call initializes the object; further calls return the result
103  * of the first call. This is so that it's safe to implement the singleton
104  * pattern in the GObject constructor function.
105  *
106  * For classes that also support the #GInitable interface, the default
107  * implementation of this method will run the g_initable_init() function
108  * in a thread, so if you want to support asynchronous initialization via
109  * threads, just implement the #GAsyncInitable interface without overriding
110  * any interface methods.
111  *
112  * Since: 2.22
113  */
114 void
115 g_async_initable_init_async (GAsyncInitable      *initable,
116                              int                  io_priority,
117                              GCancellable        *cancellable,
118                              GAsyncReadyCallback  callback,
119                              gpointer             user_data)
120 {
121   GAsyncInitableIface *iface;
122
123   g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
124
125   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
126
127   (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
128 }
129
130 /**
131  * g_async_initable_init_finish:
132  * @initable: a #GAsyncInitable.
133  * @res: a #GAsyncResult.
134  * @error: a #GError location to store the error occuring, or %NULL to
135  * ignore.
136  *
137  * Finishes asynchronous initialization and returns the result.
138  * See g_async_initable_init_async().
139  *
140  * Returns: %TRUE if successful. If an error has occurred, this function
141  * will return %FALSE and set @error appropriately if present.
142  *
143  * Since: 2.22
144  */
145 gboolean
146 g_async_initable_init_finish (GAsyncInitable  *initable,
147                               GAsyncResult    *res,
148                               GError         **error)
149 {
150   GAsyncInitableIface *iface;
151
152   g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
153   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
154
155   if (G_IS_SIMPLE_ASYNC_RESULT (res))
156     {
157       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
158       if (g_simple_async_result_propagate_error (simple, error))
159         return FALSE;
160     }
161
162   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
163
164   return (* iface->init_finish) (initable, res, error);
165 }
166
167 static void
168 async_init_thread (GSimpleAsyncResult *res,
169                    GObject            *object,
170                    GCancellable       *cancellable)
171 {
172   GError *error = NULL;
173
174   if (!g_initable_init (G_INITABLE (object), cancellable, &error))
175     {
176       g_simple_async_result_set_from_error (res, error);
177       g_error_free (error);
178     }
179 }
180
181 static void
182 g_async_initable_real_init_async (GAsyncInitable      *initable,
183                                   int                  io_priority,
184                                   GCancellable        *cancellable,
185                                   GAsyncReadyCallback  callback,
186                                   gpointer             user_data)
187 {
188   GSimpleAsyncResult *res;
189
190   g_return_if_fail (G_IS_INITABLE (initable));
191
192   res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
193                                    g_async_initable_real_init_async);
194   g_simple_async_result_run_in_thread (res, async_init_thread,
195                                        io_priority, cancellable);
196   g_object_unref (res);
197 }
198
199 static gboolean
200 g_async_initable_real_init_finish (GAsyncInitable  *initable,
201                                    GAsyncResult    *res,
202                                    GError         **error)
203 {
204   return TRUE; /* Errors handled by base impl */
205 }
206
207 /**
208  * g_async_initable_new_async:
209  * @object_type: a #GType supporting #GAsyncInitable.
210  * @io_priority: the <link linkend="io-priority">I/O priority</link>
211  *     of the operation.
212  * @cancellable: optional #GCancellable object, %NULL to ignore.
213  * @callback: a #GAsyncReadyCallback to call when the initialization is
214  *     finished
215  * @user_data: the data to pass to callback function
216  * @first_property_name: the name of the first property, or %NULL if no
217  *     properties
218  * @...:  the value of the first property, followed by other property
219  *    value pairs, and ended by %NULL.
220  *
221  * Helper function for constructing #GAsyncInitiable object. This is
222  * similar to g_object_new() but also initializes the object asynchronously.
223  *
224  * When the initialization is finished, @callback will be called. You can
225  * then call g_async_initable_new_finish() to get the new object and check
226  * for any errors.
227  *
228  * Since: 2.22
229  */
230 void
231 g_async_initable_new_async (GType                object_type,
232                             int                  io_priority,
233                             GCancellable        *cancellable,
234                             GAsyncReadyCallback  callback,
235                             gpointer             user_data,
236                             const gchar         *first_property_name,
237                             ...)
238 {
239   va_list var_args;
240
241   va_start (var_args, first_property_name);
242   g_async_initable_new_valist_async (object_type,
243                                      first_property_name, var_args,
244                                      io_priority, cancellable,
245                                      callback, user_data);
246   va_end (var_args);
247 }
248
249 /**
250  * g_async_initable_newv_async:
251  * @object_type: a #GType supporting #GAsyncInitable.
252  * @n_parameters: the number of parameters in @parameters
253  * @parameters: the parameters to use to construct the object
254  * @io_priority: the <link linkend="io-priority">I/O priority</link>
255  *     of the operation.
256  * @cancellable: optional #GCancellable object, %NULL to ignore.
257  * @callback: a #GAsyncReadyCallback to call when the initialization is
258  *     finished
259  * @user_data: the data to pass to callback function
260  *
261  * Helper function for constructing #GAsyncInitiable object. This is
262  * similar to g_object_newv() but also initializes the object asynchronously.
263  *
264  * When the initialization is finished, @callback will be called. You can
265  * then call g_async_initable_new_finish() to get the new object and check
266  * for any errors.
267  *
268  * Since: 2.22
269  */
270 void
271 g_async_initable_newv_async (GType                object_type,
272                              guint                n_parameters,
273                              GParameter          *parameters,
274                              int                  io_priority,
275                              GCancellable        *cancellable,
276                              GAsyncReadyCallback  callback,
277                              gpointer             user_data)
278 {
279   GObject *obj;
280
281   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
282
283   obj = g_object_newv (object_type, n_parameters, parameters);
284
285   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
286                                io_priority, cancellable,
287                                callback, user_data);
288 }
289
290 /**
291  * g_async_initable_new_valist_async:
292  * @object_type: a #GType supporting #GAsyncInitable.
293  * @first_property_name: the name of the first property, followed by
294  * the value, and other property value pairs, and ended by %NULL.
295  * @var_args: The var args list generated from @first_property_name.
296  * @io_priority: the <link linkend="io-priority">I/O priority</link>
297  *     of the operation.
298  * @cancellable: optional #GCancellable object, %NULL to ignore.
299  * @callback: a #GAsyncReadyCallback to call when the initialization is
300  *     finished
301  * @user_data: the data to pass to callback function
302  *
303  * Helper function for constructing #GAsyncInitiable object. This is
304  * similar to g_object_new_valist() but also initializes the object
305  * asynchronously.
306  *
307  * When the initialization is finished, @callback will be called. You can
308  * then call g_async_initable_new_finish() to get the new object and check
309  * for any errors.
310  *
311  * Since: 2.22
312  */
313 void
314 g_async_initable_new_valist_async (GType                object_type,
315                                    const gchar         *first_property_name,
316                                    va_list              var_args,
317                                    int                  io_priority,
318                                    GCancellable        *cancellable,
319                                    GAsyncReadyCallback  callback,
320                                    gpointer             user_data)
321 {
322   GObject *obj;
323
324   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
325
326   obj = g_object_new_valist (object_type,
327                              first_property_name,
328                              var_args);
329
330   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
331                                io_priority, cancellable,
332                                callback, user_data);
333   g_object_unref (obj); /* Passed ownership to async call */
334 }
335
336 /**
337  * g_async_initable_new_finish:
338  * @initable: the #GAsyncInitable from the callback
339  * @res: the #GAsyncResult.from the callback
340  * @error: a #GError location to store the error occuring, or %NULL to
341  *     ignore.
342  *
343  * Finishes the async construction for the various g_async_initable_new calls,
344  * returning the created object or %NULL on error.
345  *
346  * Returns: a newly created #GObject, or %NULL on error. Free with
347  *     g_object_unref().
348  *
349  * Since: 2.22
350  */
351 GObject *
352 g_async_initable_new_finish (GAsyncInitable  *initable,
353                              GAsyncResult    *res,
354                              GError         **error)
355 {
356   if (g_async_initable_init_finish (initable, res, error))
357     return g_object_ref (initable);
358   else
359     return NULL;
360 }