GAsyncInitable: fix subclassibility
[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  * A typical implementation might look something like this:
50  *
51  * |[
52  * enum {
53  *    NOT_INITIALIZED,
54  *    INITIALIZING,
55  *    INITIALIZED
56  * };
57  *
58  * static void
59  * _foo_ready_cb (Foo *self)
60  * {
61  *   GList *l;
62  *
63  *   self->priv->state = INITIALIZED;
64  *
65  *   for (l = self->priv->init_results; l != NULL; l = l->next)
66  *     {
67  *       GSimpleAsyncResult *simple = l->data;
68  *
69  *       if (!self->priv->success)
70  *         g_simple_async_result_set_error (simple, ...);
71  *
72  *       g_simple_async_result_complete (simple);
73  *       g_object_unref (simple);
74  *     }
75  *
76  *   g_list_free (self->priv->init_results);
77  *   self->priv->init_results = NULL;
78  * }
79  *
80  * static void
81  * foo_init_async (GAsyncInitable       *initable,
82  *                 int                   io_priority,
83  *                 GCancellable         *cancellable,
84  *                 GAsyncReadyCallback   callback,
85  *                 gpointer              user_data)
86  * {
87  *   Foo *self = FOO (initable);
88  *   GSimpleAsyncResult *simple;
89  *
90  *   simple = g_simple_async_result_new (G_OBJECT (initable)
91  *                                       callback,
92  *                                       user_data,
93  *                                       foo_init_async);
94  *
95  *   switch (self->priv->state)
96  *     {
97  *       case NOT_INITIALIZED:
98  *         _foo_get_ready (self);
99  *         self->priv->init_results = g_list_append (self->priv->init_results,
100  *                                                   simple);
101  *         self->priv->state = INITIALIZING;
102  *         break;
103  *       case INITIALIZING:
104  *         self->priv->init_results = g_list_append (self->priv->init_results,
105  *                                                   simple);
106  *         break;
107  *       case INITIALIZED:
108  *         if (!self->priv->success)
109  *           g_simple_async_result_set_error (simple, ...);
110  *
111  *         g_simple_async_result_complete_in_idle (simple);
112  *         g_object_unref (simple);
113  *         break;
114  *     }
115  * }
116  *
117  * static gboolean
118  * foo_init_finish (GAsyncInitable       *initable,
119  *                  GAsyncResult         *result,
120  *                  GError              **error)
121  * {
122  *   g_return_val_if_fail (g_simple_async_result_is_valid (result,
123  *       G_OBJECT (initable), foo_init_async), FALSE);
124  *
125  *   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
126  *           error))
127  *     return FALSE;
128  *
129  *   return TRUE;
130  * }
131  *
132  * static void
133  * foo_async_initable_iface_init (gpointer g_iface,
134  *                                gpointer data)
135  * {
136  *   GAsyncInitableIface *iface = g_iface;
137  *
138  *   iface->init_async = foo_init_async;
139  *   iface->init_finish = foo_init_finish;
140  * }
141  * ]|
142  */
143
144 static void     g_async_initable_real_init_async  (GAsyncInitable       *initable,
145                                                    int                   io_priority,
146                                                    GCancellable         *cancellable,
147                                                    GAsyncReadyCallback   callback,
148                                                    gpointer              user_data);
149 static gboolean g_async_initable_real_init_finish (GAsyncInitable       *initable,
150                                                    GAsyncResult         *res,
151                                                    GError              **error);
152
153
154 typedef GAsyncInitableIface GAsyncInitableInterface;
155 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
156
157
158 static void
159 g_async_initable_default_init (GAsyncInitableInterface *iface)
160 {
161   iface->init_async = g_async_initable_real_init_async;
162   iface->init_finish = g_async_initable_real_init_finish;
163 }
164
165 /**
166  * g_async_initable_init_async:
167  * @initable: a #GAsyncInitable.
168  * @io_priority: the <link linkend="io-priority">I/O priority</link>
169  *     of the operation.
170  * @cancellable: optional #GCancellable object, %NULL to ignore.
171  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
172  * @user_data: the data to pass to callback function
173  *
174  * Starts asynchronous initialization of the object implementing the
175  * interface. This must be done before any real use of the object after
176  * initial construction. If the object also implements #GInitable you can
177  * optionally call g_initable_init() instead.
178  *
179  * When the initialization is finished, @callback will be called. You can
180  * then call g_async_initable_init_finish() to get the result of the
181  * initialization.
182  *
183  * Implementations may also support cancellation. If @cancellable is not
184  * %NULL, then initialization can be cancelled by triggering the cancellable
185  * object from another thread. If the operation was cancelled, the error
186  * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
187  * the object doesn't support cancellable initialization, the error
188  * %G_IO_ERROR_NOT_SUPPORTED will be returned.
189  *
190  * As with #GInitable, if the object is not initialized, or initialization
191  * returns with an error, then all operations on the object except
192  * g_object_ref() and g_object_unref() are considered to be invalid, and
193  * have undefined behaviour. They will often fail with g_critical() or
194  * g_warning(), but this must not be relied on.
195  *
196  * Implementations of this method must be idempotent: i.e. multiple calls
197  * to this function with the same argument should return the same results.
198  * Only the first call initializes the object; further calls return the result
199  * of the first call. This is so that it's safe to implement the singleton
200  * pattern in the GObject constructor function.
201  *
202  * For classes that also support the #GInitable interface, the default
203  * implementation of this method will run the g_initable_init() function
204  * in a thread, so if you want to support asynchronous initialization via
205  * threads, just implement the #GAsyncInitable interface without overriding
206  * any interface methods.
207  *
208  * Since: 2.22
209  */
210 void
211 g_async_initable_init_async (GAsyncInitable      *initable,
212                              int                  io_priority,
213                              GCancellable        *cancellable,
214                              GAsyncReadyCallback  callback,
215                              gpointer             user_data)
216 {
217   GAsyncInitableIface *iface;
218
219   g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
220
221   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
222
223   (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
224 }
225
226 /**
227  * g_async_initable_init_finish:
228  * @initable: a #GAsyncInitable.
229  * @res: a #GAsyncResult.
230  * @error: a #GError location to store the error occurring, or %NULL to
231  * ignore.
232  *
233  * Finishes asynchronous initialization and returns the result.
234  * See g_async_initable_init_async().
235  *
236  * Returns: %TRUE if successful. If an error has occurred, this function
237  * will return %FALSE and set @error appropriately if present.
238  *
239  * Since: 2.22
240  */
241 gboolean
242 g_async_initable_init_finish (GAsyncInitable  *initable,
243                               GAsyncResult    *res,
244                               GError         **error)
245 {
246   GAsyncInitableIface *iface;
247
248   g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
249   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
250
251   if (G_IS_SIMPLE_ASYNC_RESULT (res))
252     {
253       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
254       if (g_simple_async_result_propagate_error (simple, error))
255         return FALSE;
256     }
257
258   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
259
260   return (* iface->init_finish) (initable, res, error);
261 }
262
263 static void
264 async_init_thread (GSimpleAsyncResult *res,
265                    GObject            *object,
266                    GCancellable       *cancellable)
267 {
268   GError *error = NULL;
269
270   if (!g_initable_init (G_INITABLE (object), cancellable, &error))
271     g_simple_async_result_take_error (res, error);
272 }
273
274 static void
275 g_async_initable_real_init_async (GAsyncInitable      *initable,
276                                   int                  io_priority,
277                                   GCancellable        *cancellable,
278                                   GAsyncReadyCallback  callback,
279                                   gpointer             user_data)
280 {
281   GSimpleAsyncResult *res;
282
283   g_return_if_fail (G_IS_INITABLE (initable));
284
285   res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
286                                    g_async_initable_real_init_async);
287   g_simple_async_result_run_in_thread (res, async_init_thread,
288                                        io_priority, cancellable);
289   g_object_unref (res);
290 }
291
292 static gboolean
293 g_async_initable_real_init_finish (GAsyncInitable  *initable,
294                                    GAsyncResult    *res,
295                                    GError         **error)
296 {
297   /* Although g_async_initable_init_finish() does this error handling
298    * as well, we do it here too, so that a class that reimplements
299    * GAsyncInitable can properly run its parent class's implementation
300    * by directly invoking its ->init_async() and ->init_finish().
301    */
302   if (G_IS_SIMPLE_ASYNC_RESULT (res))
303     {
304       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
305       if (g_simple_async_result_propagate_error (simple, error))
306         return FALSE;
307     }
308
309   return TRUE;
310 }
311
312 /**
313  * g_async_initable_new_async:
314  * @object_type: a #GType supporting #GAsyncInitable.
315  * @io_priority: the <link linkend="io-priority">I/O priority</link>
316  *     of the operation.
317  * @cancellable: optional #GCancellable object, %NULL to ignore.
318  * @callback: a #GAsyncReadyCallback to call when the initialization is
319  *     finished
320  * @user_data: the data to pass to callback function
321  * @first_property_name: the name of the first property, or %NULL if no
322  *     properties
323  * @...:  the value of the first property, followed by other property
324  *    value pairs, and ended by %NULL.
325  *
326  * Helper function for constructing #GAsyncInitable object. This is
327  * similar to g_object_new() but also initializes the object asynchronously.
328  *
329  * When the initialization is finished, @callback will be called. You can
330  * then call g_async_initable_new_finish() to get the new object and check
331  * for any errors.
332  *
333  * Since: 2.22
334  */
335 void
336 g_async_initable_new_async (GType                object_type,
337                             int                  io_priority,
338                             GCancellable        *cancellable,
339                             GAsyncReadyCallback  callback,
340                             gpointer             user_data,
341                             const gchar         *first_property_name,
342                             ...)
343 {
344   va_list var_args;
345
346   va_start (var_args, first_property_name);
347   g_async_initable_new_valist_async (object_type,
348                                      first_property_name, var_args,
349                                      io_priority, cancellable,
350                                      callback, user_data);
351   va_end (var_args);
352 }
353
354 /**
355  * g_async_initable_newv_async:
356  * @object_type: a #GType supporting #GAsyncInitable.
357  * @n_parameters: the number of parameters in @parameters
358  * @parameters: the parameters to use to construct the object
359  * @io_priority: the <link linkend="io-priority">I/O priority</link>
360  *     of the operation.
361  * @cancellable: optional #GCancellable object, %NULL to ignore.
362  * @callback: a #GAsyncReadyCallback to call when the initialization is
363  *     finished
364  * @user_data: the data to pass to callback function
365  *
366  * Helper function for constructing #GAsyncInitable object. This is
367  * similar to g_object_newv() but also initializes the object asynchronously.
368  *
369  * When the initialization is finished, @callback will be called. You can
370  * then call g_async_initable_new_finish() to get the new object and check
371  * for any errors.
372  *
373  * Since: 2.22
374  */
375 void
376 g_async_initable_newv_async (GType                object_type,
377                              guint                n_parameters,
378                              GParameter          *parameters,
379                              int                  io_priority,
380                              GCancellable        *cancellable,
381                              GAsyncReadyCallback  callback,
382                              gpointer             user_data)
383 {
384   GObject *obj;
385
386   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
387
388   obj = g_object_newv (object_type, n_parameters, parameters);
389
390   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
391                                io_priority, cancellable,
392                                callback, user_data);
393 }
394
395 /**
396  * g_async_initable_new_valist_async:
397  * @object_type: a #GType supporting #GAsyncInitable.
398  * @first_property_name: the name of the first property, followed by
399  * the value, and other property value pairs, and ended by %NULL.
400  * @var_args: The var args list generated from @first_property_name.
401  * @io_priority: the <link linkend="io-priority">I/O priority</link>
402  *     of the operation.
403  * @cancellable: optional #GCancellable object, %NULL to ignore.
404  * @callback: a #GAsyncReadyCallback to call when the initialization is
405  *     finished
406  * @user_data: the data to pass to callback function
407  *
408  * Helper function for constructing #GAsyncInitable object. This is
409  * similar to g_object_new_valist() but also initializes the object
410  * asynchronously.
411  *
412  * When the initialization is finished, @callback will be called. You can
413  * then call g_async_initable_new_finish() to get the new object and check
414  * for any errors.
415  *
416  * Since: 2.22
417  */
418 void
419 g_async_initable_new_valist_async (GType                object_type,
420                                    const gchar         *first_property_name,
421                                    va_list              var_args,
422                                    int                  io_priority,
423                                    GCancellable        *cancellable,
424                                    GAsyncReadyCallback  callback,
425                                    gpointer             user_data)
426 {
427   GObject *obj;
428
429   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
430
431   obj = g_object_new_valist (object_type,
432                              first_property_name,
433                              var_args);
434
435   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
436                                io_priority, cancellable,
437                                callback, user_data);
438   g_object_unref (obj); /* Passed ownership to async call */
439 }
440
441 /**
442  * g_async_initable_new_finish:
443  * @initable: the #GAsyncInitable from the callback
444  * @res: the #GAsyncResult from the callback
445  * @error: return location for errors, or %NULL to ignore
446  *
447  * Finishes the async construction for the various g_async_initable_new
448  * calls, returning the created object or %NULL on error.
449  *
450  * Returns: (transfer full): a newly created #GObject, or %NULL on error.
451  *     Free with g_object_unref().
452  *
453  * Since: 2.22
454  */
455 GObject *
456 g_async_initable_new_finish (GAsyncInitable  *initable,
457                              GAsyncResult    *res,
458                              GError         **error)
459 {
460   if (g_async_initable_init_finish (initable, res, error))
461     return g_object_ref (initable);
462   else
463     return NULL;
464 }